]> granicus.if.org Git - icinga2/blob - lib/base/process.hpp
lib->compat->statusdatawriter: fix notifications_enabled
[icinga2] / lib / base / process.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #ifndef PROCESS_H
21 #define PROCESS_H
22
23 #include "base/i2-base.hpp"
24 #include "base/dictionary.hpp"
25 #include <iosfwd>
26 #include <deque>
27 #include <vector>
28 #include <sstream>
29
30 namespace icinga
31 {
32
33 /**
34  * The result of a Process task.
35  *
36  * @ingroup base
37  */
38 struct ProcessResult
39 {
40         pid_t PID;
41         double ExecutionStart;
42         double ExecutionEnd;
43         long ExitStatus;
44         String Output;
45 };
46
47 /**
48  * A process task. Executes an external application and returns the exit
49  * code and console output.
50  *
51  * @ingroup base
52  */
53 class Process final : public Object
54 {
55 public:
56         DECLARE_PTR_TYPEDEFS(Process);
57
58 #ifdef _WIN32
59         typedef String Arguments;
60         typedef HANDLE ProcessHandle;
61         typedef HANDLE ConsoleHandle;
62 #else /* _WIN32 */
63         typedef std::vector<String> Arguments;
64         typedef pid_t ProcessHandle;
65         typedef int ConsoleHandle;
66 #endif /* _WIN32 */
67
68         static const std::deque<Process::Ptr>::size_type MaxTasksPerThread = 512;
69
70         Process(Arguments arguments, Dictionary::Ptr extraEnvironment = nullptr);
71         ~Process() override;
72
73         void SetTimeout(double timeout);
74         double GetTimeout() const;
75
76         void SetAdjustPriority(bool adjust);
77         bool GetAdjustPriority() const;
78
79         void Run(const std::function<void (const ProcessResult&)>& callback = std::function<void (const ProcessResult&)>());
80
81         pid_t GetPID() const;
82
83         static Arguments PrepareCommand(const Value& command);
84
85         static void ThreadInitialize();
86
87         static String PrettyPrintArguments(const Arguments& arguments);
88
89 #ifndef _WIN32
90         static void InitializeSpawnHelper();
91 #endif /* _WIN32 */
92
93 private:
94         Arguments m_Arguments;
95         Dictionary::Ptr m_ExtraEnvironment;
96
97         double m_Timeout;
98         bool m_AdjustPriority;
99
100         ProcessHandle m_Process;
101         pid_t m_PID;
102         ConsoleHandle m_FD;
103
104 #ifdef _WIN32
105         bool m_ReadPending;
106         bool m_ReadFailed;
107         OVERLAPPED m_Overlapped;
108         char m_ReadBuffer[1024];
109 #endif /* _WIN32 */
110
111         std::ostringstream m_OutputStream;
112         std::function<void (const ProcessResult&)> m_Callback;
113         ProcessResult m_Result;
114
115         static void IOThreadProc(int tid);
116         bool DoEvents();
117         int GetTID() const;
118 };
119
120 }
121
122 #endif /* PROCESS_H */