Process_UNIX.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // Process_UNIX.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Process_UNIX.cpp#3 $
  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_UNIX.h"
  16. #include "Poco/Exception.h"
  17. #include "Poco/NumberFormatter.h"
  18. #include "Poco/Pipe.h"
  19. #include <errno.h>
  20. #include <signal.h>
  21. #include <stdlib.h>
  22. #include <sys/time.h>
  23. #include <sys/types.h>
  24. #include <sys/resource.h>
  25. #include <sys/wait.h>
  26. #if defined(__QNX__)
  27. #include <process.h>
  28. #include <spawn.h>
  29. #include <cstring>
  30. #endif
  31. namespace Poco {
  32. //
  33. // ProcessHandleImpl
  34. //
  35. ProcessHandleImpl::ProcessHandleImpl(pid_t pid):
  36. _pid(pid)
  37. {
  38. }
  39. ProcessHandleImpl::~ProcessHandleImpl()
  40. {
  41. }
  42. pid_t ProcessHandleImpl::id() const
  43. {
  44. return _pid;
  45. }
  46. int ProcessHandleImpl::wait() const
  47. {
  48. int status;
  49. int rc;
  50. do
  51. {
  52. rc = waitpid(_pid, &status, 0);
  53. }
  54. while (rc < 0 && errno == EINTR);
  55. if (rc != _pid)
  56. throw SystemException("Cannot wait for process", NumberFormatter::format(_pid));
  57. return WEXITSTATUS(status);
  58. }
  59. //
  60. // ProcessImpl
  61. //
  62. ProcessImpl::PIDImpl ProcessImpl::idImpl()
  63. {
  64. return getpid();
  65. }
  66. void ProcessImpl::timesImpl(long& userTime, long& kernelTime)
  67. {
  68. struct rusage usage;
  69. getrusage(RUSAGE_SELF, &usage);
  70. userTime = usage.ru_utime.tv_sec;
  71. kernelTime = usage.ru_stime.tv_sec;
  72. }
  73. ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const ArgsImpl& args, const std::string& initialDirectory, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe, const EnvImpl& env)
  74. {
  75. #if defined(__QNX__)
  76. if (initialDirectory.empty())
  77. {
  78. /// use QNX's spawn system call which is more efficient than fork/exec.
  79. char** argv = new char*[args.size() + 2];
  80. int i = 0;
  81. argv[i++] = const_cast<char*>(command.c_str());
  82. for (ArgsImpl::const_iterator it = args.begin(); it != args.end(); ++it)
  83. argv[i++] = const_cast<char*>(it->c_str());
  84. argv[i] = NULL;
  85. struct inheritance inherit;
  86. std::memset(&inherit, 0, sizeof(inherit));
  87. inherit.flags = SPAWN_ALIGN_DEFAULT | SPAWN_CHECK_SCRIPT | SPAWN_SEARCH_PATH;
  88. int fdmap[3];
  89. fdmap[0] = inPipe ? inPipe->readHandle() : 0;
  90. fdmap[1] = outPipe ? outPipe->writeHandle() : 1;
  91. fdmap[2] = errPipe ? errPipe->writeHandle() : 2;
  92. char** envPtr = 0;
  93. std::vector<char> envChars;
  94. std::vector<char*> envPtrs;
  95. if (!env.empty())
  96. {
  97. envChars = getEnvironmentVariablesBuffer(env);
  98. envPtrs.reserve(env.size() + 1);
  99. char* p = &envChars[0];
  100. while (*p)
  101. {
  102. envPtrs.push_back(p);
  103. while (*p) ++p;
  104. ++p;
  105. }
  106. envPtrs.push_back(0);
  107. envPtr = &envPtrs[0];
  108. }
  109. int pid = spawn(command.c_str(), 3, fdmap, &inherit, argv, envPtr);
  110. delete [] argv;
  111. if (pid == -1)
  112. throw SystemException("cannot spawn", command);
  113. if (inPipe) inPipe->close(Pipe::CLOSE_READ);
  114. if (outPipe) outPipe->close(Pipe::CLOSE_WRITE);
  115. if (errPipe) errPipe->close(Pipe::CLOSE_WRITE);
  116. return new ProcessHandleImpl(pid);
  117. }
  118. else
  119. {
  120. return launchByForkExecImpl(command, args, initialDirectory, inPipe, outPipe, errPipe, env);
  121. }
  122. #else
  123. return launchByForkExecImpl(command, args, initialDirectory, inPipe, outPipe, errPipe, env);
  124. #endif
  125. }
  126. ProcessHandleImpl* ProcessImpl::launchByForkExecImpl(const std::string& command, const ArgsImpl& args, const std::string& initialDirectory, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe, const EnvImpl& env)
  127. {
  128. // We must not allocated memory after fork(),
  129. // therefore allocate all required buffers first.
  130. std::vector<char> envChars = getEnvironmentVariablesBuffer(env);
  131. std::vector<char*> argv(args.size() + 2);
  132. int i = 0;
  133. argv[i++] = const_cast<char*>(command.c_str());
  134. for (ArgsImpl::const_iterator it = args.begin(); it != args.end(); ++it)
  135. {
  136. argv[i++] = const_cast<char*>(it->c_str());
  137. }
  138. argv[i] = NULL;
  139. const char* pInitialDirectory = initialDirectory.empty() ? 0 : initialDirectory.c_str();
  140. int pid = fork();
  141. if (pid < 0)
  142. {
  143. throw SystemException("Cannot fork process for", command);
  144. }
  145. else if (pid == 0)
  146. {
  147. if (pInitialDirectory)
  148. {
  149. if (chdir(pInitialDirectory) != 0)
  150. {
  151. _exit(72);
  152. }
  153. }
  154. // set environment variables
  155. char* p = &envChars[0];
  156. while (*p)
  157. {
  158. putenv(p);
  159. while (*p) ++p;
  160. ++p;
  161. }
  162. // setup redirection
  163. if (inPipe)
  164. {
  165. dup2(inPipe->readHandle(), STDIN_FILENO);
  166. inPipe->close(Pipe::CLOSE_BOTH);
  167. }
  168. // outPipe and errPipe may be the same, so we dup first and close later
  169. if (outPipe) dup2(outPipe->writeHandle(), STDOUT_FILENO);
  170. if (errPipe) dup2(errPipe->writeHandle(), STDERR_FILENO);
  171. if (outPipe) outPipe->close(Pipe::CLOSE_BOTH);
  172. if (errPipe) errPipe->close(Pipe::CLOSE_BOTH);
  173. // close all open file descriptors other than stdin, stdout, stderr
  174. for (int i = 3; i < getdtablesize(); ++i)
  175. {
  176. close(i);
  177. }
  178. execvp(argv[0], &argv[0]);
  179. _exit(72);
  180. }
  181. if (inPipe) inPipe->close(Pipe::CLOSE_READ);
  182. if (outPipe) outPipe->close(Pipe::CLOSE_WRITE);
  183. if (errPipe) errPipe->close(Pipe::CLOSE_WRITE);
  184. return new ProcessHandleImpl(pid);
  185. }
  186. void ProcessImpl::killImpl(ProcessHandleImpl& handle)
  187. {
  188. killImpl(handle.id());
  189. }
  190. void ProcessImpl::killImpl(PIDImpl pid)
  191. {
  192. if (kill(pid, SIGKILL) != 0)
  193. {
  194. switch (errno)
  195. {
  196. case ESRCH:
  197. throw NotFoundException("cannot kill process");
  198. case EPERM:
  199. throw NoPermissionException("cannot kill process");
  200. default:
  201. throw SystemException("cannot kill process");
  202. }
  203. }
  204. }
  205. bool ProcessImpl::isRunningImpl(const ProcessHandleImpl& handle)
  206. {
  207. return isRunningImpl(handle.id());
  208. }
  209. bool ProcessImpl::isRunningImpl(PIDImpl pid)
  210. {
  211. if (kill(pid, 0) == 0)
  212. {
  213. return true;
  214. }
  215. else
  216. {
  217. return false;
  218. }
  219. }
  220. void ProcessImpl::requestTerminationImpl(PIDImpl pid)
  221. {
  222. if (kill(pid, SIGINT) != 0)
  223. {
  224. switch (errno)
  225. {
  226. case ESRCH:
  227. throw NotFoundException("cannot terminate process");
  228. case EPERM:
  229. throw NoPermissionException("cannot terminate process");
  230. default:
  231. throw SystemException("cannot terminate process");
  232. }
  233. }
  234. }
  235. } // namespace Poco