sigchld_service.hpp 3.3 KB

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