Process.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // Process.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Process.cpp#4 $
  5. //
  6. // Library: Foundation
  7. // Package: Processes
  8. // Module: Process
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Process.h"
  16. #include "Poco/Environment.h"
  17. namespace
  18. {
  19. std::vector<char> getEnvironmentVariablesBuffer(const Poco::Process::Env& env)
  20. {
  21. std::vector<char> envbuf;
  22. std::size_t pos = 0;
  23. for (Poco::Process::Env::const_iterator it = env.begin(); it != env.end(); ++it)
  24. {
  25. std::size_t envlen = it->first.length() + it->second.length() + 1;
  26. envbuf.resize(pos + envlen + 1);
  27. std::copy(it->first.begin(), it->first.end(), &envbuf[pos]);
  28. pos += it->first.length();
  29. envbuf[pos] = '=';
  30. ++pos;
  31. std::copy(it->second.begin(), it->second.end(), &envbuf[pos]);
  32. pos += it->second.length();
  33. envbuf[pos] = '\0';
  34. ++pos;
  35. }
  36. envbuf.resize(pos + 1);
  37. envbuf[pos] = '\0';
  38. return envbuf;
  39. }
  40. void setEnvironmentVariables(const Poco::Process::Env& env)
  41. {
  42. for (Poco::Process::Env::const_iterator it = env.begin(); it != env.end(); ++it)
  43. {
  44. Poco::Environment::set(it->first, it->second);
  45. }
  46. }
  47. }
  48. #if defined(POCO_OS_FAMILY_WINDOWS) && defined(POCO_WIN32_UTF8)
  49. #if defined(_WIN32_WCE)
  50. #include "Process_WINCE.cpp"
  51. #else
  52. #include "Process_WIN32U.cpp"
  53. #endif
  54. #elif defined(POCO_OS_FAMILY_WINDOWS)
  55. #include "Process_WIN32.cpp"
  56. #elif defined(POCO_VXWORKS)
  57. #include "Process_VX.cpp"
  58. #elif defined(POCO_OS_FAMILY_UNIX)
  59. #include "Process_UNIX.cpp"
  60. #else
  61. #include "Process_VMS.cpp"
  62. #endif
  63. namespace Poco {
  64. //
  65. // ProcessHandle
  66. //
  67. ProcessHandle::ProcessHandle(const ProcessHandle& handle):
  68. _pImpl(handle._pImpl)
  69. {
  70. _pImpl->duplicate();
  71. }
  72. ProcessHandle::~ProcessHandle()
  73. {
  74. _pImpl->release();
  75. }
  76. ProcessHandle::ProcessHandle(ProcessHandleImpl* pImpl):
  77. _pImpl(pImpl)
  78. {
  79. poco_check_ptr (_pImpl);
  80. }
  81. ProcessHandle& ProcessHandle::operator = (const ProcessHandle& handle)
  82. {
  83. if (&handle != this)
  84. {
  85. _pImpl->release();
  86. _pImpl = handle._pImpl;
  87. _pImpl->duplicate();
  88. }
  89. return *this;
  90. }
  91. ProcessHandle::PID ProcessHandle::id() const
  92. {
  93. return _pImpl->id();
  94. }
  95. int ProcessHandle::wait() const
  96. {
  97. return _pImpl->wait();
  98. }
  99. //
  100. // Process
  101. //
  102. ProcessHandle Process::launch(const std::string& command, const Args& args)
  103. {
  104. std::string initialDirectory;
  105. Env env;
  106. return ProcessHandle(launchImpl(command, args, initialDirectory, 0, 0, 0, env));
  107. }
  108. ProcessHandle Process::launch(const std::string& command, const Args& args, const std::string& initialDirectory)
  109. {
  110. Env env;
  111. return ProcessHandle(launchImpl(command, args, initialDirectory, 0, 0, 0, env));
  112. }
  113. ProcessHandle Process::launch(const std::string& command, const Args& args, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe)
  114. {
  115. poco_assert (inPipe == 0 || (inPipe != outPipe && inPipe != errPipe));
  116. std::string initialDirectory;
  117. Env env;
  118. return ProcessHandle(launchImpl(command, args, initialDirectory, inPipe, outPipe, errPipe, env));
  119. }
  120. ProcessHandle Process::launch(const std::string& command, const Args& args, const std::string& initialDirectory, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe)
  121. {
  122. poco_assert (inPipe == 0 || (inPipe != outPipe && inPipe != errPipe));
  123. Env env;
  124. return ProcessHandle(launchImpl(command, args, initialDirectory, inPipe, outPipe, errPipe, env));
  125. }
  126. ProcessHandle Process::launch(const std::string& command, const Args& args, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe, const Env& env)
  127. {
  128. poco_assert (inPipe == 0 || (inPipe != outPipe && inPipe != errPipe));
  129. std::string initialDirectory;
  130. return ProcessHandle(launchImpl(command, args, initialDirectory, inPipe, outPipe, errPipe, env));
  131. }
  132. ProcessHandle Process::launch(const std::string& command, const Args& args, const std::string& initialDirectory, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe, const Env& env)
  133. {
  134. poco_assert (inPipe == 0 || (inPipe != outPipe && inPipe != errPipe));
  135. return ProcessHandle(launchImpl(command, args, initialDirectory, inPipe, outPipe, errPipe, env));
  136. }
  137. int Process::wait(const ProcessHandle& handle)
  138. {
  139. return handle.wait();
  140. }
  141. void Process::kill(ProcessHandle& handle)
  142. {
  143. killImpl(*handle._pImpl);
  144. }
  145. void Process::kill(PID pid)
  146. {
  147. killImpl(pid);
  148. }
  149. bool Process::isRunning(const ProcessHandle& handle)
  150. {
  151. return isRunningImpl(*handle._pImpl);
  152. }
  153. bool Process::isRunning(PID pid)
  154. {
  155. return isRunningImpl(pid);
  156. }
  157. void Process::requestTermination(PID pid)
  158. {
  159. requestTerminationImpl(pid);
  160. }
  161. } // namespace Poco