process_handle_signal.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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_SIGNAL_HPP
  6. #define BOOST_PROCESS_V2_DETAIL_PROCESS_HANDLE_SIGNAL_HPP
  7. #include <boost/process/v2/detail/config.hpp>
  8. #include <sys/types.h>
  9. #include <sys/wait.h>
  10. #include <boost/process/v2/detail/last_error.hpp>
  11. #include <boost/process/v2/detail/throw_error.hpp>
  12. #include <boost/process/v2/exit_code.hpp>
  13. #include <boost/process/v2/pid.hpp>
  14. #if defined(BOOST_PROCESS_V2_STANDALONE)
  15. #include <asio/any_io_executor.hpp>
  16. #include <asio/append.hpp>
  17. #include <asio/associated_immediate_executor.hpp>
  18. #include <asio/compose.hpp>
  19. #include <asio/dispatch.hpp>
  20. #include <asio/post.hpp>
  21. #if !defined(BOOST_PROCESS_V2_DISABLE_SIGNALSET)
  22. #include <asio/signal_set.hpp>
  23. #endif
  24. #else
  25. #include <boost/asio/any_io_executor.hpp>
  26. #include <boost/asio/append.hpp>
  27. #include <boost/asio/associated_immediate_executor.hpp>
  28. #include <boost/asio/compose.hpp>
  29. #include <boost/asio/dispatch.hpp>
  30. #include <boost/asio/post.hpp>
  31. #if !defined(BOOST_PROCESS_V2_DISABLE_SIGNALSET)
  32. #include <boost/asio/signal_set.hpp>
  33. #endif
  34. #endif
  35. BOOST_PROCESS_V2_BEGIN_NAMESPACE
  36. namespace detail
  37. {
  38. template<typename Executor = net::any_io_executor>
  39. struct basic_process_handle_signal
  40. {
  41. struct native_handle_type
  42. {
  43. native_handle_type() = delete;
  44. native_handle_type(const native_handle_type & ) = delete;
  45. ~native_handle_type() = default;
  46. };
  47. typedef Executor executor_type;
  48. executor_type get_executor()
  49. { return signal_set_.get_executor(); }
  50. /// Rebinds the process_handle to another executor.
  51. template<typename Executor1>
  52. struct rebind_executor
  53. {
  54. /// The socket type when rebound to the specified executor.
  55. typedef basic_process_handle_signal<Executor1> other;
  56. };
  57. template<typename ExecutionContext>
  58. basic_process_handle_signal(ExecutionContext &context,
  59. typename std::enable_if<
  60. std::is_convertible<ExecutionContext &,
  61. net::execution_context &>::value
  62. >::type * = nullptr)
  63. : pid_(-1), signal_set_(context, SIGCHLD)
  64. {
  65. }
  66. basic_process_handle_signal(Executor executor)
  67. : pid_(-1), signal_set_(executor, SIGCHLD)
  68. {
  69. }
  70. basic_process_handle_signal(Executor executor, pid_type pid)
  71. : pid_(pid), signal_set_(executor, SIGCHLD)
  72. {
  73. }
  74. basic_process_handle_signal(basic_process_handle_signal && handle)
  75. : pid_(handle.pid_), signal_set_(handle.signal_set_.get_executor(), SIGCHLD)
  76. {
  77. handle.pid_ = -1;
  78. }
  79. basic_process_handle_signal& operator=(basic_process_handle_signal && handle)
  80. {
  81. pid_ = handle.id();
  82. #if !defined(BOOST_PROCESS_V2_DISABLE_SIGNALSET)
  83. signal_set_.~basic_signal_set();
  84. using ss = net::basic_signal_set<Executor>;
  85. new (&signal_set_) ss(handle.get_executor(), SIGCHLD);
  86. #else
  87. signal_set_.executor = handle.signal_set_.executor;
  88. #endif
  89. handle.pid_ = -1;
  90. return *this;
  91. }
  92. template<typename Executor1>
  93. basic_process_handle_signal(basic_process_handle_signal<Executor1> && handle)
  94. : pid_(handle.pid_), signal_set_(Executor1(handle.signal_set_.get_executor()), SIGCHLD)
  95. {
  96. handle.pid_ = -1;
  97. }
  98. pid_type id() const { return pid_; }
  99. native_handle_type native_handle() {return {};}
  100. void terminate_if_running(error_code &)
  101. {
  102. terminate_if_running();
  103. }
  104. void terminate_if_running()
  105. {
  106. if (pid_ <= 0)
  107. return;
  108. if (::waitpid(pid_, nullptr, WNOHANG) == 0)
  109. {
  110. ::kill(pid_, SIGKILL);
  111. ::waitpid(pid_, nullptr, 0);
  112. }
  113. }
  114. void wait(native_exit_code_type &exit_status, error_code &ec)
  115. {
  116. if (pid_ <= 0)
  117. return;
  118. while (::waitpid(pid_, &exit_status, 0) < 0)
  119. {
  120. if (errno != EINTR)
  121. {
  122. ec = get_last_error();
  123. break;
  124. }
  125. }
  126. }
  127. void wait(native_exit_code_type &exit_status)
  128. {
  129. if (pid_ <= 0)
  130. return;
  131. error_code ec;
  132. wait(exit_status, ec);
  133. if (ec)
  134. detail::throw_error(ec, "wait(pid)");
  135. }
  136. void interrupt(error_code &ec)
  137. {
  138. if (pid_ <= 0)
  139. return;
  140. if (::kill(pid_, SIGINT) == -1)
  141. ec = get_last_error();
  142. }
  143. void interrupt()
  144. {
  145. if (pid_ <= 0)
  146. return;
  147. error_code ec;
  148. interrupt(ec);
  149. if (ec)
  150. detail::throw_error(ec, "interrupt");
  151. }
  152. void request_exit(error_code &ec)
  153. {
  154. if (pid_ <= 0)
  155. return;
  156. if (::kill(pid_, SIGTERM) == -1)
  157. ec = get_last_error();
  158. }
  159. void request_exit()
  160. {
  161. if (pid_ <= 0)
  162. return;
  163. error_code ec;
  164. request_exit(ec);
  165. if (ec)
  166. detail::throw_error(ec, "request_exit");
  167. }
  168. void suspend()
  169. {
  170. if (pid_ <= 0)
  171. return;
  172. error_code ec;
  173. suspend(ec);
  174. if (ec)
  175. detail::throw_error(ec, "suspend");
  176. }
  177. void suspend(error_code &ec)
  178. {
  179. if (pid_ <= 0)
  180. return;
  181. if (::kill(pid_, SIGSTOP) == -1)
  182. ec = get_last_error();
  183. }
  184. void resume()
  185. {
  186. if (pid_ <= 0)
  187. return;
  188. error_code ec;
  189. resume(ec);
  190. if (ec)
  191. detail::throw_error(ec, "resume");
  192. }
  193. void resume(error_code &ec)
  194. {
  195. if (pid_ <= 0)
  196. return;
  197. if (::kill(pid_, SIGCONT) == -1)
  198. ec = get_last_error();
  199. }
  200. void terminate(native_exit_code_type &exit_status, error_code &ec)
  201. {
  202. if (pid_ <= 0)
  203. return;
  204. if (::kill(pid_, SIGKILL) == -1)
  205. ec = get_last_error();
  206. else
  207. wait(exit_status, ec);
  208. }
  209. void terminate(native_exit_code_type &exit_status)
  210. {
  211. if (pid_ <= 0)
  212. return;
  213. error_code ec;
  214. terminate(exit_status, ec);
  215. if (ec)
  216. detail::throw_error(ec, "terminate");
  217. }
  218. bool running(native_exit_code_type &exit_code, error_code & ec)
  219. {
  220. if (pid_ <= 0)
  221. return false;
  222. int code = 0;
  223. int res = ::waitpid(pid_, &code, WNOHANG);
  224. if (res == -1)
  225. ec = get_last_error();
  226. else if (res == 0)
  227. return true;
  228. else
  229. {
  230. ec.clear();
  231. exit_code = code;
  232. }
  233. return false;
  234. }
  235. bool running(native_exit_code_type &exit_code)
  236. {
  237. if (pid_ <= 0)
  238. return false;
  239. error_code ec;
  240. bool res = running(exit_code, ec);
  241. if (ec)
  242. detail::throw_error(ec, "is_running");
  243. return res;
  244. }
  245. bool is_open() const
  246. {
  247. return pid_ != -1;
  248. }
  249. private:
  250. template<typename>
  251. friend struct basic_process_handle_signal;
  252. pid_type pid_ = -1;
  253. #if !defined(BOOST_PROCESS_V2_DISABLE_SIGNALSET)
  254. net::basic_signal_set<Executor> signal_set_;
  255. #else
  256. struct signal_set_dummy_
  257. {
  258. signal_set_dummy_(signal_set_dummy_ &&) = default;
  259. signal_set_dummy_(const signal_set_dummy_ &) = default;
  260. Executor executor;
  261. using executor_type = Executor;
  262. executor_type get_executor() {return executor;}
  263. signal_set_dummy_(Executor executor, int) : executor(std::move(executor)) {}
  264. };
  265. signal_set_dummy_ signal_set_;
  266. #endif
  267. struct async_wait_op_
  268. {
  269. #if !defined(BOOST_PROCESS_V2_DISABLE_SIGNALSET)
  270. net::basic_signal_set<Executor> &handle;
  271. pid_type pid_;
  272. native_exit_code_type & exit_code;
  273. template<typename Self>
  274. void operator()(Self &&self)
  275. {
  276. self.reset_cancellation_state(asio::enable_total_cancellation());
  277. handle.async_wait(std::move(self));
  278. handle.cancel();
  279. // we cancel so we end up on the signal-sets executor
  280. }
  281. template<typename Self>
  282. void operator()(Self &&self, error_code ec, int /*sig*/)
  283. {
  284. if (ec == net::error::operation_aborted &&
  285. self.get_cancellation_state().cancelled()
  286. == net::cancellation_type::none)
  287. ec.clear();
  288. int wait_res = -1;
  289. if (pid_ <= 0) // error, complete early
  290. ec = net::error::bad_descriptor;
  291. else if (!ec && process_is_running(exit_code))
  292. {
  293. wait_res = ::waitpid(pid_, &exit_code, WNOHANG);
  294. if (wait_res == -1)
  295. ec = get_last_error();
  296. }
  297. if (!ec && (wait_res == 0))
  298. {
  299. handle.async_wait(std::move(self));
  300. return;
  301. }
  302. const auto exec = self.get_executor();
  303. net::dispatch(exec, net::append(std::move(self), ec));
  304. }
  305. #else
  306. signal_set_dummy_ dummy_;
  307. pid_t pid;
  308. template<typename Self>
  309. void operator()(Self &&self)
  310. {
  311. auto exec = net::get_associated_immediate_executor(self, dummy_.get_executor());
  312. error_code ec;
  313. BOOST_PROCESS_V2_ASSIGN_EC(ec, net::error::operation_not_supported);
  314. net::dispatch(exec, net::append(std::move(self), native_exit_code_type(), ec));
  315. }
  316. #endif
  317. template<typename Self>
  318. void operator()(Self &&self, error_code ec)
  319. {
  320. self.complete(ec);
  321. }
  322. };
  323. public:
  324. template<BOOST_PROCESS_V2_COMPLETION_TOKEN_FOR(void(error_code))
  325. WaitHandler = net::default_completion_token_t<executor_type>>
  326. auto async_wait(native_exit_code_type & exit_code,
  327. WaitHandler &&handler = net::default_completion_token_t<executor_type>())
  328. -> decltype(net::async_compose<WaitHandler, void(error_code)>(
  329. async_wait_op_{signal_set_, pid_, exit_code}, handler, signal_set_))
  330. {
  331. return net::async_compose<WaitHandler, void(error_code)>(
  332. async_wait_op_{signal_set_, pid_, exit_code}, handler, signal_set_);
  333. }
  334. };
  335. }
  336. BOOST_PROCESS_V2_END_NAMESPACE
  337. #endif //BOOST_PROCESS_V2_DETAIL_PROCESS_HANDLE_SIGNAL_HPP