1 | // |
---|
2 | // Process.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/Process.h#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Processes |
---|
8 | // Module: Process |
---|
9 | // |
---|
10 | // Definition of the Process class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_Process_INCLUDED |
---|
40 | #define Foundation_Process_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | |
---|
45 | |
---|
46 | #if defined(POCO_OS_FAMILY_WINDOWS) && defined(POCO_WIN32_UTF8) |
---|
47 | #include "Poco/Process_WIN32U.h" |
---|
48 | #elif defined(POCO_OS_FAMILY_WINDOWS) |
---|
49 | #include "Poco/Process_WIN32.h" |
---|
50 | #elif defined(POCO_OS_FAMILY_UNIX) |
---|
51 | #include "Poco/Process_UNIX.h" |
---|
52 | #else |
---|
53 | #include "Poco/Process_VMS.h" |
---|
54 | #endif |
---|
55 | |
---|
56 | |
---|
57 | namespace Poco { |
---|
58 | |
---|
59 | |
---|
60 | class Pipe; |
---|
61 | |
---|
62 | |
---|
63 | class Foundation_API ProcessHandle |
---|
64 | /// A handle for a process created with Process::launch(). |
---|
65 | /// |
---|
66 | /// This handle can be used to determine the process ID of |
---|
67 | /// the newly created process and it can be used to wait for |
---|
68 | /// the completion of a process. |
---|
69 | { |
---|
70 | public: |
---|
71 | typedef ProcessImpl::PIDImpl PID; |
---|
72 | |
---|
73 | ProcessHandle(const ProcessHandle& handle); |
---|
74 | /// Creates a ProcessHandle by copying another one. |
---|
75 | |
---|
76 | ~ProcessHandle(); |
---|
77 | /// Destroys the ProcessHandle. |
---|
78 | |
---|
79 | ProcessHandle& operator = (const ProcessHandle& handle); |
---|
80 | /// Assigns another handle. |
---|
81 | |
---|
82 | PID id() const; |
---|
83 | /// Returns the process ID. |
---|
84 | |
---|
85 | int wait() const; |
---|
86 | /// Waits for the process to terminate |
---|
87 | /// and returns the exit code of the process. |
---|
88 | |
---|
89 | protected: |
---|
90 | ProcessHandle(ProcessHandleImpl* pImpl); |
---|
91 | |
---|
92 | private: |
---|
93 | ProcessHandle(); |
---|
94 | |
---|
95 | ProcessHandleImpl* _pImpl; |
---|
96 | |
---|
97 | friend class Process; |
---|
98 | }; |
---|
99 | |
---|
100 | |
---|
101 | class Foundation_API Process: public ProcessImpl |
---|
102 | /// This class provides methods for working with processes. |
---|
103 | { |
---|
104 | public: |
---|
105 | typedef PIDImpl PID; |
---|
106 | typedef ArgsImpl Args; |
---|
107 | |
---|
108 | static PID id(); |
---|
109 | /// Returns the process ID of the current process. |
---|
110 | |
---|
111 | static void times(long& userTime, long& kernelTime); |
---|
112 | /// Returns the number of seconds spent by the |
---|
113 | /// current process in user and kernel mode. |
---|
114 | |
---|
115 | static ProcessHandle launch(const std::string& command, const Args& args); |
---|
116 | /// Creates a new process for the given command and returns |
---|
117 | /// a ProcessHandle of the new process. The given arguments are |
---|
118 | /// passed to the command on the command line. |
---|
119 | |
---|
120 | static ProcessHandle launch(const std::string& command, const Args& args, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe); |
---|
121 | /// Creates a new process for the given command and returns |
---|
122 | /// a ProcessHandle of the new process. The given arguments are |
---|
123 | /// passed to the command on the command line. |
---|
124 | /// |
---|
125 | /// If inPipe, outPipe or errPipe is non-null, the corresponding |
---|
126 | /// standard input, standard output or standard error stream |
---|
127 | /// of the launched process is redirected to the Pipe. |
---|
128 | /// PipeInputStream or PipeOutputStream can be used to |
---|
129 | /// send receive data from, or send data to the process. |
---|
130 | /// |
---|
131 | /// Note: the same Pipe can be used for both outPipe and errPipe. |
---|
132 | /// |
---|
133 | /// After a Pipe has been passed as inPipe, only write operations |
---|
134 | /// are valid. After a Pipe has been passed as outPipe or errPipe, |
---|
135 | /// only read operations are valid. |
---|
136 | /// |
---|
137 | /// It is forbidden to pass the same pipe as inPipe and outPipe or errPipe. |
---|
138 | /// |
---|
139 | /// Usage example: |
---|
140 | /// Pipe outPipe; |
---|
141 | /// Process::Args args; |
---|
142 | /// ProcessHandle ph(launch("/bin/ps", args, 0, &outPipe, 0)); |
---|
143 | /// PipeInputStream istr(outPipe); |
---|
144 | /// ... // read output of ps from istr |
---|
145 | /// int rc = ph.wait(); |
---|
146 | |
---|
147 | static int wait(const ProcessHandle& handle); |
---|
148 | /// Waits for the process specified by handle to terminate |
---|
149 | /// and returns the exit code of the process. |
---|
150 | |
---|
151 | static void kill(PID pid); |
---|
152 | /// Kills the process with the given pid. |
---|
153 | |
---|
154 | static void requestTermination(PID pid); |
---|
155 | /// Requests termination of the process with the give PID. |
---|
156 | /// |
---|
157 | /// On Unix platforms, this will send a SIGINT to the |
---|
158 | /// process and thus work with arbitrary processes. |
---|
159 | /// |
---|
160 | /// On other platforms, a global event flag |
---|
161 | /// will be set. Setting the flag will cause |
---|
162 | /// Util::ServerApplication::waitForTerminationRequest() to |
---|
163 | /// return. Therefore this will only work with applications |
---|
164 | /// based on Util::ServerApplication. |
---|
165 | }; |
---|
166 | |
---|
167 | |
---|
168 | // |
---|
169 | // inlines |
---|
170 | // |
---|
171 | inline Process::PID Process::id() |
---|
172 | { |
---|
173 | return ProcessImpl::idImpl(); |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | inline void Process::times(long& userTime, long& kernelTime) |
---|
178 | { |
---|
179 | ProcessImpl::timesImpl(userTime, kernelTime); |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | } // namespace Poco |
---|
184 | |
---|
185 | |
---|
186 | #endif // Foundation_Process_INCLUDED |
---|