]> granicus.if.org Git - icinga2/blob - lib/base/process.h
Various bugfixes.
[icinga2] / lib / base / process.h
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://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 namespace icinga
24 {
25
26 /**
27  * The result of a Process task.
28  *
29  * @ingroup base
30  */
31 struct ProcessResult
32 {
33         double ExecutionStart;
34         double ExecutionEnd;
35         long ExitStatus;
36         String Output;
37 };
38
39 /**
40  * A process task. Executes an external application and returns the exit
41  * code and console output.
42  *
43  * @ingroup base
44  */
45 class I2_BASE_API Process : public AsyncTask<Process, ProcessResult>
46 {
47 public:
48         typedef shared_ptr<Process> Ptr;
49         typedef weak_ptr<Process> WeakPtr;
50
51         static const deque<Process::Ptr>::size_type MaxTasksPerThread = 512;
52
53         Process(const vector<String>& arguments, const Dictionary::Ptr& extraEnvironment = Dictionary::Ptr());
54
55         static vector<String> SplitCommand(const Value& command);
56 private:
57         vector<String> m_Arguments;
58         Dictionary::Ptr m_ExtraEnvironment;
59
60 #ifndef _WIN32
61         pid_t m_Pid;
62         int m_FD;
63 #endif /* _WIN32 */
64
65         stringstream m_OutputStream;
66
67         ProcessResult m_Result;
68
69         virtual void Run(void);
70
71         static boost::mutex m_Mutex;
72         static deque<Process::Ptr> m_Tasks;
73 #ifndef _WIN32
74         static condition_variable m_CV;
75         static int m_TaskFd;
76
77         static Timer::Ptr m_StatusTimer;
78 #endif /* _WIN32 */
79
80         void QueueTask(void);
81
82         void SpawnTask(void);
83
84 #ifdef _WIN32
85         static void WorkerThreadProc(void);
86 #else /* _WIN32 */
87         static void WorkerThreadProc(int taskFd);
88
89         static void StatusTimerHandler(void);
90 #endif /* _WIN32 */
91
92         void InitTask(void);
93         bool RunTask(void);
94
95         static boost::once_flag m_ThreadOnce;
96         static void Initialize(void);
97 };
98
99 }
100
101 #endif /* PROCESS_H */