Process_WIN32.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // Process_WIN32.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Process_WIN32.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_WIN32.h"
  16. #include "Poco/Exception.h"
  17. #include "Poco/NumberFormatter.h"
  18. #include "Poco/NamedEvent.h"
  19. #include "Poco/Pipe.h"
  20. namespace Poco {
  21. //
  22. // ProcessHandleImpl
  23. //
  24. ProcessHandleImpl::ProcessHandleImpl(HANDLE hProcess, UInt32 pid):
  25. _hProcess(hProcess),
  26. _pid(pid)
  27. {
  28. }
  29. ProcessHandleImpl::~ProcessHandleImpl()
  30. {
  31. closeHandle();
  32. }
  33. void ProcessHandleImpl::closeHandle()
  34. {
  35. if (_hProcess)
  36. {
  37. CloseHandle(_hProcess);
  38. _hProcess = NULL;
  39. }
  40. }
  41. UInt32 ProcessHandleImpl::id() const
  42. {
  43. return _pid;
  44. }
  45. HANDLE ProcessHandleImpl::process() const
  46. {
  47. return _hProcess;
  48. }
  49. int ProcessHandleImpl::wait() const
  50. {
  51. DWORD rc = WaitForSingleObject(_hProcess, INFINITE);
  52. if (rc != WAIT_OBJECT_0)
  53. throw SystemException("Wait failed for process", NumberFormatter::format(_pid));
  54. DWORD exitCode;
  55. if (GetExitCodeProcess(_hProcess, &exitCode) == 0)
  56. throw SystemException("Cannot get exit code for process", NumberFormatter::format(_pid));
  57. return exitCode;
  58. }
  59. //
  60. // ProcessImpl
  61. //
  62. ProcessImpl::PIDImpl ProcessImpl::idImpl()
  63. {
  64. return GetCurrentProcessId();
  65. }
  66. void ProcessImpl::timesImpl(long& userTime, long& kernelTime)
  67. {
  68. FILETIME ftCreation;
  69. FILETIME ftExit;
  70. FILETIME ftKernel;
  71. FILETIME ftUser;
  72. if (GetProcessTimes(GetCurrentProcess(), &ftCreation, &ftExit, &ftKernel, &ftUser) != 0)
  73. {
  74. ULARGE_INTEGER time;
  75. time.LowPart = ftKernel.dwLowDateTime;
  76. time.HighPart = ftKernel.dwHighDateTime;
  77. kernelTime = long(time.QuadPart/10000000L);
  78. time.LowPart = ftUser.dwLowDateTime;
  79. time.HighPart = ftUser.dwHighDateTime;
  80. userTime = long(time.QuadPart/10000000L);
  81. }
  82. else
  83. {
  84. userTime = kernelTime = -1;
  85. }
  86. }
  87. ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const ArgsImpl& args, const std::string& initialDirectory, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe, const EnvImpl& env)
  88. {
  89. std::string commandLine = command;
  90. for (ArgsImpl::const_iterator it = args.begin(); it != args.end(); ++it)
  91. {
  92. commandLine.append(" ");
  93. commandLine.append(*it);
  94. }
  95. STARTUPINFOA startupInfo;
  96. GetStartupInfoA(&startupInfo); // take defaults from current process
  97. startupInfo.cb = sizeof(STARTUPINFOA);
  98. startupInfo.lpReserved = NULL;
  99. startupInfo.lpDesktop = NULL;
  100. startupInfo.lpTitle = NULL;
  101. startupInfo.dwFlags = STARTF_FORCEOFFFEEDBACK;
  102. startupInfo.cbReserved2 = 0;
  103. startupInfo.lpReserved2 = NULL;
  104. HANDLE hProc = GetCurrentProcess();
  105. bool mustInheritHandles = false;
  106. if (inPipe)
  107. {
  108. DuplicateHandle(hProc, inPipe->readHandle(), hProc, &startupInfo.hStdInput, 0, TRUE, DUPLICATE_SAME_ACCESS);
  109. mustInheritHandles = true;
  110. inPipe->close(Pipe::CLOSE_READ);
  111. }
  112. else if (GetStdHandle(STD_INPUT_HANDLE))
  113. {
  114. DuplicateHandle(hProc, GetStdHandle(STD_INPUT_HANDLE), hProc, &startupInfo.hStdInput, 0, TRUE, DUPLICATE_SAME_ACCESS);
  115. mustInheritHandles = true;
  116. }
  117. else
  118. {
  119. startupInfo.hStdInput = 0;
  120. }
  121. // outPipe may be the same as errPipe, so we duplicate first and close later.
  122. if (outPipe)
  123. {
  124. DuplicateHandle(hProc, outPipe->writeHandle(), hProc, &startupInfo.hStdOutput, 0, TRUE, DUPLICATE_SAME_ACCESS);
  125. mustInheritHandles = true;
  126. }
  127. else if (GetStdHandle(STD_OUTPUT_HANDLE))
  128. {
  129. DuplicateHandle(hProc, GetStdHandle(STD_OUTPUT_HANDLE), hProc, &startupInfo.hStdOutput, 0, TRUE, DUPLICATE_SAME_ACCESS);
  130. mustInheritHandles = true;
  131. }
  132. else
  133. {
  134. startupInfo.hStdOutput = 0;
  135. }
  136. if (errPipe)
  137. {
  138. DuplicateHandle(hProc, errPipe->writeHandle(), hProc, &startupInfo.hStdError, 0, TRUE, DUPLICATE_SAME_ACCESS);
  139. mustInheritHandles = true;
  140. }
  141. else if (GetStdHandle(STD_ERROR_HANDLE))
  142. {
  143. DuplicateHandle(hProc, GetStdHandle(STD_ERROR_HANDLE), hProc, &startupInfo.hStdError, 0, TRUE, DUPLICATE_SAME_ACCESS);
  144. mustInheritHandles = true;
  145. }
  146. else
  147. {
  148. startupInfo.hStdError = 0;
  149. }
  150. if (outPipe) outPipe->close(Pipe::CLOSE_WRITE);
  151. if (errPipe) errPipe->close(Pipe::CLOSE_WRITE);
  152. if (mustInheritHandles)
  153. {
  154. startupInfo.dwFlags |= STARTF_USESTDHANDLES;
  155. }
  156. const char* workingDirectory = initialDirectory.empty() ? 0 : initialDirectory.c_str();
  157. const char* pEnv = 0;
  158. std::vector<char> envChars;
  159. if (!env.empty())
  160. {
  161. envChars = getEnvironmentVariablesBuffer(env);
  162. pEnv = &envChars[0];
  163. }
  164. PROCESS_INFORMATION processInfo;
  165. DWORD creationFlags = GetConsoleWindow() ? 0 : CREATE_NO_WINDOW;
  166. BOOL rc = CreateProcessA(
  167. NULL,
  168. const_cast<char*>(commandLine.c_str()),
  169. NULL, // processAttributes
  170. NULL, // threadAttributes
  171. mustInheritHandles,
  172. creationFlags,
  173. (LPVOID) pEnv,
  174. workingDirectory,
  175. &startupInfo,
  176. &processInfo
  177. );
  178. if (startupInfo.hStdInput) CloseHandle(startupInfo.hStdInput);
  179. if (startupInfo.hStdOutput) CloseHandle(startupInfo.hStdOutput);
  180. if (startupInfo.hStdError) CloseHandle(startupInfo.hStdError);
  181. if (rc)
  182. {
  183. CloseHandle(processInfo.hThread);
  184. return new ProcessHandleImpl(processInfo.hProcess, processInfo.dwProcessId);
  185. }
  186. else throw SystemException("Cannot launch process", command);
  187. }
  188. void ProcessImpl::killImpl(ProcessHandleImpl& handle)
  189. {
  190. if (handle.process())
  191. {
  192. if (TerminateProcess(handle.process(), 0) == 0)
  193. {
  194. handle.closeHandle();
  195. throw SystemException("cannot kill process");
  196. }
  197. handle.closeHandle();
  198. }
  199. }
  200. void ProcessImpl::killImpl(PIDImpl pid)
  201. {
  202. HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
  203. if (hProc)
  204. {
  205. if (TerminateProcess(hProc, 0) == 0)
  206. {
  207. CloseHandle(hProc);
  208. throw SystemException("cannot kill process");
  209. }
  210. CloseHandle(hProc);
  211. }
  212. else
  213. {
  214. switch (GetLastError())
  215. {
  216. case ERROR_ACCESS_DENIED:
  217. throw NoPermissionException("cannot kill process");
  218. case ERROR_NOT_FOUND:
  219. throw NotFoundException("cannot kill process");
  220. default:
  221. throw SystemException("cannot kill process");
  222. }
  223. }
  224. }
  225. bool ProcessImpl::isRunningImpl(const ProcessHandleImpl& handle)
  226. {
  227. bool result = true;
  228. DWORD exitCode;
  229. BOOL rc = GetExitCodeProcess(handle.process(), &exitCode);
  230. if (!rc || exitCode != STILL_ACTIVE) result = false;
  231. return result;
  232. }
  233. bool ProcessImpl::isRunningImpl(PIDImpl pid)
  234. {
  235. HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
  236. bool result = true;
  237. DWORD exitCode;
  238. BOOL rc = GetExitCodeProcess(hProc, &exitCode);
  239. if (!rc || exitCode != STILL_ACTIVE) result = false;
  240. return result;
  241. }
  242. void ProcessImpl::requestTerminationImpl(PIDImpl pid)
  243. {
  244. NamedEvent ev(terminationEventName(pid));
  245. ev.set();
  246. }
  247. std::string ProcessImpl::terminationEventName(PIDImpl pid)
  248. {
  249. std::string evName("POCOTRM");
  250. NumberFormatter::appendHex(evName, pid, 8);
  251. return evName;
  252. }
  253. } // namespace Poco