process_handle_fd.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. }
  197. void terminate(native_exit_code_type &exit_status)
  198. {
  199. if (pid_ <= 0)
  200. return;
  201. error_code ec;
  202. terminate(exit_status, ec);
  203. if (ec)
  204. detail::throw_error(ec, "terminate");
  205. }
  206. bool running(native_exit_code_type &exit_code, error_code & ec)
  207. {
  208. if (pid_ <= 0)
  209. return false;
  210. int code = 0;
  211. int res = ::waitpid(pid_, &code, WNOHANG);
  212. if (res == -1)
  213. ec = get_last_error();
  214. else if (res == 0)
  215. return true;
  216. else
  217. ec.clear();
  218. if (res == 0)
  219. return true;
  220. else
  221. {
  222. exit_code = code;
  223. return false;
  224. }
  225. }
  226. bool running(native_exit_code_type &exit_code)
  227. {
  228. if (pid_ <= 0)
  229. return false;
  230. error_code ec;
  231. bool res = running(exit_code, ec);
  232. if (ec)
  233. detail::throw_error(ec, "is_running");
  234. return res;
  235. }
  236. bool is_open() const
  237. {
  238. return pid_ != -1;
  239. }
  240. template<BOOST_PROCESS_V2_COMPLETION_TOKEN_FOR(void(error_code, native_exit_code_type))
  241. WaitHandler BOOST_PROCESS_V2_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  242. BOOST_PROCESS_V2_INITFN_AUTO_RESULT_TYPE(WaitHandler, void (error_code, native_exit_code_type))
  243. async_wait(WaitHandler &&handler BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  244. {
  245. return BOOST_PROCESS_V2_ASIO_NAMESPACE::async_compose<WaitHandler, void(error_code, native_exit_code_type)>(
  246. async_wait_op_{descriptor_, pid_}, handler, descriptor_);
  247. }
  248. private:
  249. template<typename>
  250. friend
  251. struct basic_process_handle_fd;
  252. pid_type pid_ = -1;
  253. BOOST_PROCESS_V2_ASIO_NAMESPACE::posix::basic_stream_descriptor<Executor> descriptor_;
  254. struct async_wait_op_
  255. {
  256. BOOST_PROCESS_V2_ASIO_NAMESPACE::posix::basic_descriptor<Executor> &descriptor;
  257. pid_type pid_;
  258. template<typename Self>
  259. void operator()(Self &&self)
  260. {
  261. error_code ec;
  262. native_exit_code_type exit_code{};
  263. int wait_res = -1;
  264. if (pid_ <= 0) // error, complete early
  265. ec = BOOST_PROCESS_V2_ASIO_NAMESPACE::error::bad_descriptor;
  266. else
  267. {
  268. wait_res = ::waitpid(pid_, &exit_code, WNOHANG);
  269. if (wait_res == -1)
  270. ec = get_last_error();
  271. }
  272. if (!ec && (wait_res == 0))
  273. {
  274. descriptor.async_wait(
  275. BOOST_PROCESS_V2_ASIO_NAMESPACE::posix::descriptor_base::wait_read, std::move(self));
  276. return;
  277. }
  278. struct completer
  279. {
  280. error_code ec;
  281. native_exit_code_type code;
  282. typename std::decay<Self>::type self;
  283. void operator()()
  284. {
  285. self.complete(ec, code);
  286. }
  287. };
  288. BOOST_PROCESS_V2_ASIO_NAMESPACE::post(descriptor.get_executor(),
  289. completer{ec, exit_code, std::move(self)});
  290. }
  291. template<typename Self>
  292. void operator()(Self &&self, error_code ec, int = 0)
  293. {
  294. native_exit_code_type exit_code{};
  295. if (!ec)
  296. if (::waitpid(pid_, &exit_code, 0) == -1)
  297. ec = get_last_error();
  298. std::move(self).complete(ec, exit_code);
  299. }
  300. };
  301. };
  302. }
  303. BOOST_PROCESS_V2_END_NAMESPACE
  304. #endif //BOOST_PROCESS_HANDLE_FD_OR_SIGNAL_HPP