Process.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // Process.h
  3. //
  4. // $Id: //poco/svn/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. #ifndef Foundation_Process_INCLUDED
  38. #define Foundation_Process_INCLUDED
  39. #include "Poco/Foundation.h"
  40. #if defined(POCO_OS_FAMILY_WINDOWS) && defined(POCO_WIN32_UTF8)
  41. #include "Poco/Process_WIN32U.h"
  42. #elif defined(POCO_OS_FAMILY_WINDOWS)
  43. #include "Poco/Process_WIN32.h"
  44. #elif defined(POCO_OS_FAMILY_UNIX)
  45. #include "Poco/Process_UNIX.h"
  46. #else
  47. #include "Poco/Process_VMS.h"
  48. #endif
  49. namespace Poco {
  50. class Pipe;
  51. class Foundation_API ProcessHandle
  52. /// A handle for a process created with Process::launch().
  53. ///
  54. /// This handle can be used to determine the process ID of
  55. /// the newly created process and it can be used to wait for
  56. /// the completion of a process.
  57. {
  58. public:
  59. typedef ProcessImpl::PIDImpl PID;
  60. ProcessHandle(const ProcessHandle& handle);
  61. /// Creates a ProcessHandle by copying another one.
  62. ~ProcessHandle();
  63. /// Destroys the ProcessHandle.
  64. ProcessHandle& operator = (const ProcessHandle& handle);
  65. /// Assigns another handle.
  66. PID id() const;
  67. /// Returns the process ID.
  68. int wait() const;
  69. /// Waits for the process to terminate
  70. /// and returns the exit code of the process.
  71. protected:
  72. ProcessHandle(ProcessHandleImpl* pImpl);
  73. private:
  74. ProcessHandle();
  75. ProcessHandleImpl* _pImpl;
  76. friend class Process;
  77. };
  78. class Foundation_API Process: public ProcessImpl
  79. /// This class provides methods for working with processes.
  80. {
  81. public:
  82. typedef PIDImpl PID;
  83. typedef ArgsImpl Args;
  84. static PID id();
  85. /// Returns the process ID of the current process.
  86. static void times(long& userTime, long& kernelTime);
  87. /// Returns the number of seconds spent by the
  88. /// current process in user and kernel mode.
  89. static ProcessHandle launch(const std::string& command, const Args& args);
  90. /// Creates a new process for the given command and returns
  91. /// a ProcessHandle of the new process. The given arguments are
  92. /// passed to the command on the command line.
  93. static ProcessHandle launch(const std::string& command, const Args& args, Pipe* inPipe, Pipe* outPipe, 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, &outPipe, 0, 0));
  116. /// PipeInputStream istr(outPipe);
  117. /// ... // read output of ps from istr
  118. /// int rc = ph.wait();
  119. static int wait(const ProcessHandle& handle);
  120. /// Waits for the process specified by handle to terminate
  121. /// and returns the exit code of the process.
  122. static void kill(PID pid);
  123. /// Kills the process with the given pid.
  124. static void requestTermination(PID pid);
  125. /// Requests termination of the process with the give PID.
  126. ///
  127. /// On Unix platforms, this will send a SIGINT to the
  128. /// process and thus work with arbitrary processes.
  129. ///
  130. /// On other platforms, a global event flag
  131. /// will be set. Setting the flag will cause
  132. /// Util::ServerApplication::waitForTerminationRequest() to
  133. /// return. Therefore this will only work with applications
  134. /// based on Util::ServerApplication.
  135. };
  136. //
  137. // inlines
  138. //
  139. inline Process::PID Process::id()
  140. {
  141. return ProcessImpl::idImpl();
  142. }
  143. inline void Process::times(long& userTime, long& kernelTime)
  144. {
  145. ProcessImpl::timesImpl(userTime, kernelTime);
  146. }
  147. } // namespace Poco
  148. #endif // Foundation_Process_INCLUDED