Process.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // Process.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/Process.h#4 $
  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. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_Process_INCLUDED
  18. #define Foundation_Process_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #if defined(POCO_OS_FAMILY_WINDOWS) && defined(POCO_WIN32_UTF8)
  21. #if defined(_WIN32_WCE)
  22. #include "Process_WINCE.h"
  23. #else
  24. #include "Poco/Process_WIN32U.h"
  25. #endif
  26. #elif defined(POCO_OS_FAMILY_WINDOWS)
  27. #include "Poco/Process_WIN32.h"
  28. #elif defined(POCO_VXWORKS)
  29. #include "Poco/Process_VX.h"
  30. #elif defined(POCO_OS_FAMILY_UNIX)
  31. #include "Poco/Process_UNIX.h"
  32. #else
  33. #include "Poco/Process_VMS.h"
  34. #endif
  35. namespace Poco {
  36. class Pipe;
  37. class Foundation_API ProcessHandle
  38. /// A handle for a process created with Process::launch().
  39. ///
  40. /// This handle can be used to determine the process ID of
  41. /// the newly created process and it can be used to wait for
  42. /// the completion of a process.
  43. {
  44. public:
  45. typedef ProcessImpl::PIDImpl PID;
  46. ProcessHandle(const ProcessHandle& handle);
  47. /// Creates a ProcessHandle by copying another one.
  48. ~ProcessHandle();
  49. /// Destroys the ProcessHandle.
  50. ProcessHandle& operator = (const ProcessHandle& handle);
  51. /// Assigns another handle.
  52. PID id() const;
  53. /// Returns the process ID.
  54. int wait() const;
  55. /// Waits for the process to terminate
  56. /// and returns the exit code of the process.
  57. protected:
  58. ProcessHandle(ProcessHandleImpl* pImpl);
  59. private:
  60. ProcessHandle();
  61. ProcessHandleImpl* _pImpl;
  62. friend class Process;
  63. };
  64. class Foundation_API Process: public ProcessImpl
  65. /// This class provides methods for working with processes.
  66. {
  67. public:
  68. typedef PIDImpl PID;
  69. typedef ArgsImpl Args;
  70. typedef EnvImpl Env;
  71. static PID id();
  72. /// Returns the process ID of the current process.
  73. static void times(long& userTime, long& kernelTime);
  74. /// Returns the number of seconds spent by the
  75. /// current process in user and kernel mode.
  76. static ProcessHandle launch(const std::string& command, const Args& args);
  77. /// Creates a new process for the given command and returns
  78. /// a ProcessHandle of the new process. The given arguments are
  79. /// passed to the command on the command line.
  80. static ProcessHandle launch(
  81. const std::string& command,
  82. const Args& args,
  83. const std::string& initialDirectory);
  84. /// Creates a new process for the given command and returns
  85. /// a ProcessHandle of the new process. The given arguments are
  86. /// passed to the command on the command line.
  87. /// The process starts executing in the specified initial directory.
  88. static ProcessHandle launch(
  89. const std::string& command,
  90. const Args& args,
  91. Pipe* inPipe,
  92. Pipe* outPipe,
  93. Pipe* errPipe);
  94. /// Creates a new process for the given command and returns
  95. /// a ProcessHandle of the new process. The given arguments are
  96. /// passed to the command on the command line.
  97. ///
  98. /// If inPipe, outPipe or errPipe is non-null, the corresponding
  99. /// standard input, standard output or standard error stream
  100. /// of the launched process is redirected to the Pipe.
  101. /// PipeInputStream or PipeOutputStream can be used to
  102. /// send receive data from, or send data to the process.
  103. ///
  104. /// Note: the same Pipe can be used for both outPipe and errPipe.
  105. ///
  106. /// After a Pipe has been passed as inPipe, only write operations
  107. /// are valid. After a Pipe has been passed as outPipe or errPipe,
  108. /// only read operations are valid.
  109. ///
  110. /// It is forbidden to pass the same pipe as inPipe and outPipe or errPipe.
  111. ///
  112. /// Usage example:
  113. /// Pipe outPipe;
  114. /// Process::Args args;
  115. /// ProcessHandle ph(launch("/bin/ps", args, 0, &outPipe, 0));
  116. /// PipeInputStream istr(outPipe);
  117. /// ... // read output of ps from istr
  118. /// int rc = ph.wait();
  119. static ProcessHandle launch(
  120. const std::string& command,
  121. const Args& args,
  122. const std::string& initialDirectory,
  123. Pipe* inPipe,
  124. Pipe* outPipe,
  125. Pipe* errPipe);
  126. /// Creates a new process for the given command and returns
  127. /// a ProcessHandle of the new process. The given arguments are
  128. /// passed to the command on the command line.
  129. /// The process starts executing in the specified initial directory.
  130. ///
  131. /// If inPipe, outPipe or errPipe is non-null, the corresponding
  132. /// standard input, standard output or standard error stream
  133. /// of the launched process is redirected to the Pipe.
  134. /// PipeInputStream or PipeOutputStream can be used to
  135. /// send receive data from, or send data to the process.
  136. ///
  137. /// Note: the same Pipe can be used for both outPipe and errPipe.
  138. ///
  139. /// After a Pipe has been passed as inPipe, only write operations
  140. /// are valid. After a Pipe has been passed as outPipe or errPipe,
  141. /// only read operations are valid.
  142. ///
  143. /// It is forbidden to pass the same pipe as inPipe and outPipe or errPipe.
  144. ///
  145. /// Usage example:
  146. /// Pipe outPipe;
  147. /// Process::Args args;
  148. /// ProcessHandle ph(launch("/bin/ps", args, 0, &outPipe, 0));
  149. /// PipeInputStream istr(outPipe);
  150. /// ... // read output of ps from istr
  151. /// int rc = ph.wait();
  152. static ProcessHandle launch(
  153. const std::string& command,
  154. const Args& args,
  155. Pipe* inPipe,
  156. Pipe* outPipe,
  157. Pipe* errPipe,
  158. const Env& env);
  159. /// Creates a new process for the given command and returns
  160. /// a ProcessHandle of the new process. The given arguments are
  161. /// passed to the command on the command line.
  162. ///
  163. /// If inPipe, outPipe or errPipe is non-null, the corresponding
  164. /// standard input, standard output or standard error stream
  165. /// of the launched process is redirected to the Pipe.
  166. ///
  167. /// The launched process is given the specified environment variables.
  168. static ProcessHandle launch(
  169. const std::string& command,
  170. const Args& args,
  171. const std::string& initialDirectory,
  172. Pipe* inPipe,
  173. Pipe* outPipe,
  174. Pipe* errPipe,
  175. const Env& env);
  176. /// Creates a new process for the given command and returns
  177. /// a ProcessHandle of the new process. The given arguments are
  178. /// passed to the command on the command line.
  179. /// The process starts executing in the specified initial directory.
  180. /// If inPipe, outPipe or errPipe is non-null, the corresponding
  181. /// standard input, standard output or standard error stream
  182. /// of the launched process is redirected to the Pipe.
  183. /// The launched process is given the specified environment variables.
  184. static int wait(const ProcessHandle& handle);
  185. /// Waits for the process specified by handle to terminate
  186. /// and returns the exit code of the process.
  187. static bool isRunning(const ProcessHandle& handle);
  188. /// check if the process specified by handle is running or not
  189. ///
  190. /// This is preferable on Windows where process IDs
  191. /// may be reused.
  192. static bool isRunning(PID pid);
  193. /// Check if the process specified by given pid is running or not.
  194. static void kill(ProcessHandle& handle);
  195. /// Kills the process specified by handle.
  196. ///
  197. /// This is preferable on Windows where process IDs
  198. /// may be reused.
  199. static void kill(PID pid);
  200. /// Kills the process with the given pid.
  201. static void requestTermination(PID pid);
  202. /// Requests termination of the process with the give PID.
  203. ///
  204. /// On Unix platforms, this will send a SIGINT to the
  205. /// process and thus work with arbitrary processes.
  206. ///
  207. /// On other platforms, a global event flag
  208. /// will be set. Setting the flag will cause
  209. /// Util::ServerApplication::waitForTerminationRequest() to
  210. /// return. Therefore this will only work with applications
  211. /// based on Util::ServerApplication.
  212. };
  213. //
  214. // inlines
  215. //
  216. inline Process::PID Process::id()
  217. {
  218. return ProcessImpl::idImpl();
  219. }
  220. inline void Process::times(long& userTime, long& kernelTime)
  221. {
  222. ProcessImpl::timesImpl(userTime, kernelTime);
  223. }
  224. } // namespace Poco
  225. #endif // Foundation_Process_INCLUDED