1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
23 #include "base/i2-base.hpp"
24 #include "base/dictionary.hpp"
34 * The result of a Process task.
41 double ExecutionStart;
48 * A process task. Executes an external application and returns the exit
49 * code and console output.
53 class Process final : public Object
56 DECLARE_PTR_TYPEDEFS(Process);
59 typedef String Arguments;
60 typedef HANDLE ProcessHandle;
61 typedef HANDLE ConsoleHandle;
63 typedef std::vector<String> Arguments;
64 typedef pid_t ProcessHandle;
65 typedef int ConsoleHandle;
68 static const std::deque<Process::Ptr>::size_type MaxTasksPerThread = 512;
70 Process(Arguments arguments, Dictionary::Ptr extraEnvironment = nullptr);
73 void SetTimeout(double timeout);
74 double GetTimeout() const;
76 void SetAdjustPriority(bool adjust);
77 bool GetAdjustPriority() const;
79 void Run(const std::function<void (const ProcessResult&)>& callback = std::function<void (const ProcessResult&)>());
83 static Arguments PrepareCommand(const Value& command);
85 static void ThreadInitialize();
87 static String PrettyPrintArguments(const Arguments& arguments);
90 static void InitializeSpawnHelper();
94 Arguments m_Arguments;
95 Dictionary::Ptr m_ExtraEnvironment;
98 bool m_AdjustPriority;
100 ProcessHandle m_Process;
107 OVERLAPPED m_Overlapped;
108 char m_ReadBuffer[1024];
111 std::ostringstream m_OutputStream;
112 std::function<void (const ProcessResult&)> m_Callback;
113 ProcessResult m_Result;
115 static void IOThreadProc(int tid);
122 #endif /* PROCESS_H */