process_handle_windows.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Copyright (c) 2022 Klemens D. Morgenstern
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PROCESS_V2_DETAIL_PROCESS_HANDLE_WINDOWS_HPP
  6. #define BOOST_PROCESS_V2_DETAIL_PROCESS_HANDLE_WINDOWS_HPP
  7. #include <boost/process/v2/detail/config.hpp>
  8. #include <boost/process/v2/exit_code.hpp>
  9. #include <boost/process/v2/pid.hpp>
  10. #include <boost/process/v2/detail/throw_error.hpp>
  11. #if defined(BOOST_PROCESS_V2_STANDALONE)
  12. #include <asio/any_io_executor.hpp>
  13. #include <asio/compose.hpp>
  14. #include <asio/windows/basic_object_handle.hpp>
  15. #else
  16. #include <boost/asio/any_io_executor.hpp>
  17. #include <boost/asio/compose.hpp>
  18. #include <boost/asio/windows/basic_object_handle.hpp>
  19. #endif
  20. BOOST_PROCESS_V2_BEGIN_NAMESPACE
  21. namespace detail
  22. {
  23. BOOST_PROCESS_V2_DECL void get_exit_code_( void * handle, native_exit_code_type & exit_code, error_code & ec);
  24. BOOST_PROCESS_V2_DECL void * open_process_(pid_type pid);
  25. BOOST_PROCESS_V2_DECL void terminate_if_running_(void * handle);
  26. BOOST_PROCESS_V2_DECL bool check_handle_(void* handle, error_code & ec);
  27. BOOST_PROCESS_V2_DECL bool check_pid_(pid_type pid_, error_code & ec);
  28. BOOST_PROCESS_V2_DECL void interrupt_(pid_type pid_, error_code & ec);
  29. BOOST_PROCESS_V2_DECL void suspend_(void * handle, error_code & ec);
  30. BOOST_PROCESS_V2_DECL void resume_(void * handle, error_code & ec);
  31. BOOST_PROCESS_V2_DECL void terminate_(void * handle, error_code & ec, native_exit_code_type & exit_code);
  32. BOOST_PROCESS_V2_DECL void request_exit_(pid_type pid_, error_code & ec);
  33. BOOST_PROCESS_V2_DECL void check_running_(void* handle, error_code & ec, native_exit_code_type & exit_status);
  34. template<typename Executor = net::any_io_executor>
  35. struct basic_process_handle_win
  36. {
  37. typedef net::windows::basic_object_handle<Executor> handle_type;
  38. typedef typename handle_type::native_handle_type native_handle_type;
  39. typedef Executor executor_type;
  40. executor_type get_executor()
  41. { return handle_.get_executor(); }
  42. /// Rebinds the process_handle to another executor.
  43. template<typename Executor1>
  44. struct rebind_executor
  45. {
  46. /// The socket type when rebound to the specified executor.
  47. typedef basic_process_handle_win<Executor1> other;
  48. };
  49. template<typename ExecutionContext>
  50. basic_process_handle_win(ExecutionContext &context,
  51. typename std::enable_if<
  52. std::is_convertible<ExecutionContext &,
  53. net::execution_context &>::value
  54. >::type = 0)
  55. : pid_(0), handle_(context)
  56. {
  57. }
  58. basic_process_handle_win(Executor executor)
  59. : pid_(0), handle_(executor)
  60. {
  61. }
  62. basic_process_handle_win(Executor executor, pid_type pid)
  63. : pid_(pid), handle_(executor, detail::open_process_(pid))
  64. {
  65. }
  66. basic_process_handle_win(Executor executor, pid_type pid, native_handle_type process_handle)
  67. : pid_(pid), handle_(executor, process_handle)
  68. {
  69. }
  70. template<typename Executor1>
  71. basic_process_handle_win(basic_process_handle_win<Executor1> && other)
  72. : pid_(other.pid_), handle_(std::move(other.handle_))
  73. {
  74. other.pid_ = static_cast<DWORD>(-1);
  75. }
  76. basic_process_handle_win(basic_process_handle_win && other)
  77. : pid_(other.pid_), handle_(std::move(other.handle_))
  78. {
  79. other.pid_ = static_cast<DWORD>(-1);
  80. }
  81. basic_process_handle_win& operator=(basic_process_handle_win && other)
  82. {
  83. pid_ = other.pid_;
  84. handle_ = std::move(other.handle_);
  85. other.pid_ = static_cast<DWORD>(-1);
  86. return *this;
  87. }
  88. template<typename Executor1>
  89. basic_process_handle_win& operator=(basic_process_handle_win<Executor1> && other)
  90. {
  91. pid_ = other.pid_;
  92. handle_ = std::move(other.handle_);
  93. other.pid_ = static_cast<DWORD>(-1);
  94. return *this;
  95. }
  96. ~basic_process_handle_win()
  97. {
  98. if (handle_.is_open())
  99. {
  100. error_code ec;
  101. handle_.close(ec);
  102. }
  103. }
  104. native_handle_type native_handle()
  105. { return handle_.native_handle(); }
  106. pid_type id() const
  107. { return pid_; }
  108. void terminate_if_running(error_code &)
  109. {
  110. detail::terminate_if_running_(handle_.native_handle());
  111. }
  112. void terminate_if_running()
  113. {
  114. detail::terminate_if_running_(handle_.native_handle());
  115. }
  116. void wait(native_exit_code_type &exit_status, error_code &ec)
  117. {
  118. if (!detail::check_handle_(handle_.native_handle(), ec))
  119. return;
  120. handle_.wait(ec);
  121. if (!ec)
  122. detail::get_exit_code_(handle_.native_handle(), exit_status, ec);
  123. }
  124. void wait(native_exit_code_type &exit_status)
  125. {
  126. error_code ec;
  127. wait(exit_status, ec);
  128. if (ec)
  129. detail::throw_error(ec, "wait(pid)");
  130. }
  131. void interrupt(error_code &ec)
  132. {
  133. if (!detail::check_pid_(pid_, ec))
  134. return;
  135. detail::interrupt_(pid_, ec);
  136. }
  137. void interrupt()
  138. {
  139. error_code ec;
  140. interrupt(ec);
  141. if (ec)
  142. detail::throw_error(ec, "interrupt");
  143. }
  144. void request_exit(error_code &ec)
  145. {
  146. if (!detail::check_pid_(pid_, ec))
  147. return;
  148. detail::request_exit_(pid_, ec);
  149. }
  150. void request_exit()
  151. {
  152. error_code ec;
  153. request_exit(ec);
  154. if (ec)
  155. detail::throw_error(ec, "request_exit");
  156. }
  157. void suspend(error_code &ec)
  158. {
  159. detail::suspend_(handle_.native_handle(), ec);
  160. }
  161. void suspend()
  162. {
  163. error_code ec;
  164. suspend(ec);
  165. if (ec)
  166. detail::throw_error(ec, "suspend");
  167. }
  168. void resume(error_code &ec)
  169. {
  170. detail::resume_(handle_.native_handle(), ec);
  171. }
  172. void resume()
  173. {
  174. error_code ec;
  175. resume(ec);
  176. if (ec)
  177. detail::throw_error(ec, "resume");
  178. }
  179. void terminate(native_exit_code_type &exit_status, error_code &ec)
  180. {
  181. if (!detail::check_handle_(handle_.native_handle(), ec))
  182. return;
  183. detail::terminate_(handle_.native_handle(), ec, exit_status);
  184. if (!ec)
  185. wait(exit_status, ec);
  186. }
  187. void terminate(native_exit_code_type &exit_status)
  188. {
  189. error_code ec;
  190. terminate(exit_status, ec);
  191. if (ec)
  192. detail::throw_error(ec, "terminate");
  193. }
  194. bool running(native_exit_code_type &exit_code, error_code & ec)
  195. {
  196. if (!detail::check_handle_(handle_.native_handle(), ec))
  197. return false;
  198. native_exit_code_type code;
  199. //single value, not needed in the winapi.
  200. detail::check_running_(handle_.native_handle(), ec, code);
  201. if (ec)
  202. return false;
  203. if (process_is_running(code))
  204. return true;
  205. else
  206. exit_code = code;
  207. return false;
  208. }
  209. bool running(native_exit_code_type &exit_code)
  210. {
  211. error_code ec;
  212. bool res = running(exit_code, ec);
  213. if (ec)
  214. detail::throw_error(ec, "is_running");
  215. return res;
  216. }
  217. bool is_open() const
  218. {
  219. return handle_.is_open();
  220. }
  221. template<typename>
  222. friend struct basic_process_handle_win;
  223. private:
  224. pid_type pid_;
  225. handle_type handle_;
  226. struct async_wait_op_
  227. {
  228. handle_type &handle;
  229. native_exit_code_type & exit_code;
  230. template<typename Self>
  231. void operator()(Self &&self)
  232. {
  233. self.reset_cancellation_state(asio::enable_total_cancellation());
  234. auto sl = self.get_cancellation_state().slot();
  235. auto & h = handle;
  236. if (sl.is_connected())
  237. sl.assign(
  238. [&h](asio::cancellation_type ct)
  239. {
  240. error_code ec;
  241. h.cancel(ec);
  242. });
  243. handle.async_wait(std::move(self));
  244. }
  245. template<typename Self>
  246. void operator()(Self &&self, error_code ec)
  247. {
  248. if (ec == asio::error::operation_aborted && !self.get_cancellation_state().cancelled())
  249. return handle.async_wait(std::move(self));
  250. if (!ec && process_is_running(exit_code)) // exit_code could be set by another call to wait.
  251. detail::get_exit_code_(handle.native_handle(), exit_code, ec);
  252. std::move(self).complete(ec);
  253. }
  254. };
  255. public:
  256. template<BOOST_PROCESS_V2_COMPLETION_TOKEN_FOR(void(error_code))
  257. WaitHandler = net::default_completion_token_t<executor_type>>
  258. auto async_wait(native_exit_code_type & exit_code,
  259. WaitHandler &&handler = net::default_completion_token_t<executor_type>())
  260. -> decltype(net::async_compose<WaitHandler, void(error_code)>(
  261. async_wait_op_{handle_, exit_code}, handler, handle_))
  262. {
  263. return net::async_compose<WaitHandler, void(error_code)>(
  264. async_wait_op_{handle_, exit_code}, handler, handle_
  265. );
  266. }
  267. };
  268. }
  269. BOOST_PROCESS_V2_END_NAMESPACE
  270. #endif //BOOST_PROCESS_V2_DETAIL_PROCESS_HANDLE_WINDOWS_HPP