process_handle_fd.hpp 9.1 KB

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