sigchld_service.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (c) 2017 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_DETAIL_POSIX_SIGCHLD_SERVICE_HPP_
  6. #define BOOST_PROCESS_DETAIL_POSIX_SIGCHLD_SERVICE_HPP_
  7. #include <boost/asio/dispatch.hpp>
  8. #include <boost/asio/post.hpp>
  9. #include <boost/asio/signal_set.hpp>
  10. #include <boost/asio/strand.hpp>
  11. #include <boost/optional.hpp>
  12. #include <signal.h>
  13. #include <functional>
  14. #include <sys/wait.h>
  15. namespace boost { namespace process { namespace detail { namespace posix {
  16. class sigchld_service : public boost::asio::detail::service_base<sigchld_service>
  17. {
  18. boost::asio::io_context::strand _strand{get_io_context()};
  19. boost::asio::signal_set _signal_set{get_io_context(), SIGCHLD};
  20. std::vector<std::pair<::pid_t, std::function<void(int, std::error_code)>>> _receivers;
  21. inline void _handle_signal(const boost::system::error_code & ec);
  22. public:
  23. sigchld_service(boost::asio::io_context & io_context)
  24. : boost::asio::detail::service_base<sigchld_service>(io_context)
  25. {
  26. }
  27. template <typename SignalHandler>
  28. BOOST_ASIO_INITFN_RESULT_TYPE(SignalHandler,
  29. void (int, std::error_code))
  30. async_wait(::pid_t pid, SignalHandler && handler)
  31. {
  32. boost::asio::async_completion<
  33. SignalHandler, void(boost::system::error_code)> init{handler};
  34. auto & h = init.completion_handler;
  35. boost::asio::dispatch(
  36. _strand,
  37. [this, pid, h]
  38. {
  39. //check if the child actually is running first
  40. int status;
  41. auto pid_res = ::waitpid(pid, &status, WNOHANG);
  42. if (pid_res < 0)
  43. h(-1, get_last_error());
  44. else if ((pid_res == pid) && (WIFEXITED(status) || WIFSIGNALED(status)))
  45. h(status, {}); //successfully exited already
  46. else //still running
  47. {
  48. if (_receivers.empty())
  49. _signal_set.async_wait(
  50. [this](const boost::system::error_code &ec, int)
  51. {
  52. boost::asio::dispatch(_strand, [this, ec]{this->_handle_signal(ec);});
  53. });
  54. _receivers.emplace_back(pid, h);
  55. }
  56. });
  57. return init.result.get();
  58. }
  59. void shutdown() override
  60. {
  61. _receivers.clear();
  62. }
  63. void cancel()
  64. {
  65. _signal_set.cancel();
  66. }
  67. void cancel(boost::system::error_code & ec)
  68. {
  69. _signal_set.cancel(ec);
  70. }
  71. };
  72. void sigchld_service::_handle_signal(const boost::system::error_code & ec)
  73. {
  74. std::error_code ec_{ec.value(), std::system_category()};
  75. if (ec_)
  76. {
  77. for (auto & r : _receivers)
  78. r.second(-1, ec_);
  79. return;
  80. }
  81. for (auto & r : _receivers) {
  82. int status;
  83. int pid = ::waitpid(r.first, &status, WNOHANG);
  84. if (pid < 0) {
  85. // error (eg: the process no longer exists)
  86. r.second(-1, get_last_error());
  87. r.first = 0; // mark for deletion
  88. } else if (pid == r.first) {
  89. r.second(status, ec_);
  90. r.first = 0; // mark for deletion
  91. }
  92. // otherwise the process is still around
  93. }
  94. _receivers.erase(std::remove_if(_receivers.begin(), _receivers.end(),
  95. [](const std::pair<::pid_t, std::function<void(int, std::error_code)>> & p)
  96. {
  97. return p.first == 0;
  98. }),
  99. _receivers.end());
  100. if (!_receivers.empty())
  101. {
  102. _signal_set.async_wait(
  103. [this](const boost::system::error_code & ec, int)
  104. {
  105. boost::asio::post(_strand, [this, ec]{this->_handle_signal(ec);});
  106. });
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. #endif