process_handle_windows.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 = BOOST_PROCESS_V2_ASIO_NAMESPACE::any_io_executor>
  35. struct basic_process_handle_win
  36. {
  37. typedef BOOST_PROCESS_V2_ASIO_NAMESPACE::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. BOOST_PROCESS_V2_ASIO_NAMESPACE::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> && handle)
  72. : pid_(handle.pid_), handle_(std::move(handle.handle_))
  73. {
  74. }
  75. basic_process_handle_win(basic_process_handle_win && handle)
  76. : pid_(handle.id()), handle_(std::move(handle.handle_))
  77. {
  78. handle.pid_ = static_cast<DWORD>(-1);
  79. }
  80. basic_process_handle_win& operator=(basic_process_handle_win && handle)
  81. {
  82. pid_ = handle.pid_;
  83. handle_ = std::move(handle.handle_);
  84. handle.pid_ = static_cast<DWORD>(-1);
  85. return *this;
  86. }
  87. ~basic_process_handle_win()
  88. {
  89. if (handle_.is_open())
  90. {
  91. error_code ec;
  92. handle_.close(ec);
  93. }
  94. }
  95. native_handle_type native_handle()
  96. { return handle_.native_handle(); }
  97. pid_type id() const
  98. { return pid_; }
  99. void terminate_if_running(error_code &)
  100. {
  101. detail::terminate_if_running_(handle_.native_handle());
  102. }
  103. void terminate_if_running()
  104. {
  105. detail::terminate_if_running_(handle_.native_handle());
  106. }
  107. void wait(native_exit_code_type &exit_status, error_code &ec)
  108. {
  109. if (!detail::check_handle_(handle_.native_handle(), ec))
  110. return;
  111. handle_.wait(ec);
  112. if (!ec)
  113. detail::get_exit_code_(handle_.native_handle(), exit_status, ec);
  114. }
  115. void wait(native_exit_code_type &exit_status)
  116. {
  117. error_code ec;
  118. wait(exit_status, ec);
  119. if (ec)
  120. detail::throw_error(ec, "wait(pid)");
  121. }
  122. void interrupt(error_code &ec)
  123. {
  124. if (!detail::check_pid_(pid_, ec))
  125. return;
  126. detail::interrupt_(pid_, ec);
  127. }
  128. void interrupt()
  129. {
  130. error_code ec;
  131. interrupt(ec);
  132. if (ec)
  133. detail::throw_error(ec, "interrupt");
  134. }
  135. void request_exit(error_code &ec)
  136. {
  137. if (!detail::check_pid_(pid_, ec))
  138. return;
  139. detail::request_exit_(pid_, ec);
  140. }
  141. void request_exit()
  142. {
  143. error_code ec;
  144. request_exit(ec);
  145. if (ec)
  146. detail::throw_error(ec, "request_exit");
  147. }
  148. void suspend(error_code &ec)
  149. {
  150. detail::suspend_(handle_.native_handle(), ec);
  151. }
  152. void suspend()
  153. {
  154. error_code ec;
  155. suspend(ec);
  156. if (ec)
  157. detail::throw_error(ec, "suspend");
  158. }
  159. void resume(error_code &ec)
  160. {
  161. detail::resume_(handle_.native_handle(), ec);
  162. }
  163. void resume()
  164. {
  165. error_code ec;
  166. suspend(ec);
  167. if (ec)
  168. detail::throw_error(ec, "resume");
  169. }
  170. void terminate(native_exit_code_type &exit_status, error_code &ec)
  171. {
  172. if (!detail::check_handle_(handle_.native_handle(), ec))
  173. return;
  174. detail::terminate_(handle_.native_handle(), ec, exit_status);
  175. if (!ec)
  176. wait(exit_status, ec);
  177. }
  178. void terminate(native_exit_code_type &exit_status)
  179. {
  180. error_code ec;
  181. terminate(exit_status, ec);
  182. if (ec)
  183. detail::throw_error(ec, "terminate");
  184. }
  185. bool running(native_exit_code_type &exit_code, error_code & ec)
  186. {
  187. if (!detail::check_handle_(handle_.native_handle(), ec))
  188. return false;
  189. native_exit_code_type code;
  190. //single value, not needed in the winapi.
  191. detail::check_running_(handle_.native_handle(), ec, code);
  192. if (ec)
  193. return false;
  194. if (process_is_running(code))
  195. return true;
  196. else
  197. {
  198. exit_code = code;
  199. return false;
  200. }
  201. }
  202. bool running(native_exit_code_type &exit_code)
  203. {
  204. error_code ec;
  205. bool res = running(exit_code, ec);
  206. if (ec)
  207. detail::throw_error(ec, "is_running");
  208. return res;
  209. }
  210. bool is_open() const
  211. {
  212. return handle_.is_open();
  213. }
  214. template<BOOST_PROCESS_V2_COMPLETION_TOKEN_FOR(void(error_code, native_exit_code_type))
  215. WaitHandler BOOST_PROCESS_V2_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  216. BOOST_PROCESS_V2_INITFN_AUTO_RESULT_TYPE(WaitHandler, void (error_code, native_exit_code_type))
  217. async_wait(WaitHandler &&handler BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  218. {
  219. return BOOST_PROCESS_V2_ASIO_NAMESPACE::async_compose<WaitHandler, void(error_code, native_exit_code_type)>(
  220. async_wait_op_{handle_}, handler, handle_
  221. );
  222. }
  223. template<typename>
  224. friend struct basic_process_handle_win;
  225. private:
  226. pid_type pid_;
  227. handle_type handle_;
  228. struct async_wait_op_
  229. {
  230. handle_type &handle;
  231. template<typename Self>
  232. void operator()(Self &&self)
  233. {
  234. handle.async_wait(std::move(self));
  235. }
  236. template<typename Self>
  237. void operator()(Self &&self, error_code ec)
  238. {
  239. native_exit_code_type exit_code{};
  240. if (!ec)
  241. detail::get_exit_code_(handle.native_handle(), exit_code, ec);
  242. std::move(self).complete(ec, exit_code);
  243. }
  244. };
  245. };
  246. #if !defined(BOOST_PROCESS_V2_HEADER_ONLY)
  247. extern template struct basic_process_handle_win<>;
  248. #endif
  249. }
  250. BOOST_PROCESS_V2_END_NAMESPACE
  251. #endif //BOOST_PROCESS_V2_DETAIL_PROCESS_HANDLE_WINDOWS_HPP