]> granicus.if.org Git - icinga2/blob - lib/base/process.hpp
Update copyright headers for 2016
[icinga2] / lib / base / process.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
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 <boost/function.hpp>
26 #include <sstream>
27 #include <deque>
28 #include <vector>
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 I2_BASE_API Process : 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(const Arguments& arguments, const Dictionary::Ptr& extraEnvironment = Dictionary::Ptr());
71         ~Process(void);
72
73         void SetTimeout(double timeout);
74         double GetTimeout(void) const;
75
76         void Run(const boost::function<void (const ProcessResult&)>& callback = boost::function<void (const ProcessResult&)>());
77
78         pid_t GetPID(void) const;
79
80         static Arguments PrepareCommand(const Value& command);
81
82         static void StaticInitialize(void);
83         static void ThreadInitialize(void);
84
85         static String PrettyPrintArguments(const Arguments& arguments);
86
87 private:
88         Arguments m_Arguments;
89         Dictionary::Ptr m_ExtraEnvironment;
90
91         double m_Timeout;
92
93         ProcessHandle m_Process;
94         pid_t m_PID;
95         ConsoleHandle m_FD;
96
97 #ifdef _WIN32
98         bool m_ReadPending;
99         bool m_ReadFailed;
100         OVERLAPPED m_Overlapped;
101         char m_ReadBuffer[1024];
102 #endif /* _WIN32 */
103
104         std::ostringstream m_OutputStream;
105         boost::function<void (const ProcessResult&)> m_Callback;
106         ProcessResult m_Result;
107
108         static void IOThreadProc(int tid);
109         bool DoEvents(void);
110         int GetTID(void) const;
111 };
112
113 }
114
115 #endif /* PROCESS_H */