wait_group.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright (c) 2006, 2007 Julio M. Merino Vidal
  2. // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
  3. // Copyright (c) 2009 Boris Schaeling
  4. // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
  5. // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_PROCESS_DETAIL_POSIX_WAIT_GROUP_HPP
  10. #define BOOST_PROCESS_DETAIL_POSIX_WAIT_GROUP_HPP
  11. #include <boost/process/detail/config.hpp>
  12. #include <boost/process/detail/posix/group_handle.hpp>
  13. #include <chrono>
  14. #include <system_error>
  15. #include <sys/types.h>
  16. #include <sys/wait.h>
  17. #include <unistd.h>
  18. namespace boost { namespace process { namespace detail { namespace posix {
  19. inline void wait(const group_handle &p, std::error_code &ec) noexcept
  20. {
  21. pid_t ret;
  22. siginfo_t status;
  23. do
  24. {
  25. ret = ::waitpid(-p.grp, &status.si_status, 0);
  26. if (ret == -1)
  27. {
  28. ec = get_last_error();
  29. return;
  30. }
  31. //ECHILD --> no child processes left.
  32. ret = ::waitid(P_PGID, p.grp, &status, WEXITED | WNOHANG);
  33. }
  34. while ((ret != -1) || (errno != ECHILD));
  35. if (errno != ECHILD)
  36. ec = boost::process::detail::get_last_error();
  37. else
  38. ec.clear();
  39. }
  40. inline void wait(const group_handle &p) noexcept
  41. {
  42. std::error_code ec;
  43. wait(p, ec);
  44. boost::process::detail::throw_error(ec, "waitpid(2) failed in wait");
  45. }
  46. template< class Clock, class Duration >
  47. inline bool wait_until(
  48. const group_handle &p,
  49. const std::chrono::time_point<Clock, Duration>& time_out,
  50. std::error_code & ec) noexcept
  51. {
  52. ::sigset_t sigset;
  53. ::siginfo_t siginfo;
  54. sigemptyset(&sigset);
  55. sigaddset(&sigset, SIGCHLD);
  56. auto get_timespec =
  57. [](const Duration & dur)
  58. {
  59. ::timespec ts;
  60. ts.tv_sec = std::chrono::duration_cast<std::chrono::seconds>(dur).count();
  61. ts.tv_nsec = std::chrono::duration_cast<std::chrono::nanoseconds>(dur).count() % 1000000000;
  62. return ts;
  63. };
  64. bool timed_out = false;
  65. int ret;
  66. struct ::sigaction old_sig;
  67. if (-1 == ::sigaction(SIGCHLD, nullptr, &old_sig))
  68. {
  69. ec = get_last_error();
  70. return false;
  71. }
  72. #if defined(BOOST_POSIX_HAS_SIGTIMEDWAIT)
  73. do
  74. {
  75. auto ts = get_timespec(time_out - Clock::now());
  76. ret = ::sigtimedwait(&sigset, nullptr, &ts);
  77. errno = 0;
  78. if ((ret == SIGCHLD) && (old_sig.sa_handler != SIG_DFL) && (old_sig.sa_handler != SIG_IGN))
  79. old_sig.sa_handler(ret);
  80. ret = ::waitpid(-p.grp, &siginfo.si_status, 0); //so in case it exited, we wanna reap it first
  81. if (ret == -1)
  82. {
  83. ec = get_last_error();
  84. return false;
  85. }
  86. //check if we're done
  87. ret = ::waitid(P_PGID, p.grp, &siginfo, WEXITED | WNOHANG);
  88. }
  89. while (((ret != -1) || (errno != ECHILD)) && !(timed_out = (Clock::now() > time_out)));
  90. #else
  91. //if we do not have sigtimedwait, we fork off a child process to get the signal in time
  92. pid_t timeout_pid = ::fork();
  93. if (timeout_pid == -1)
  94. {
  95. ec = boost::process::detail::get_last_error();
  96. return true;
  97. }
  98. else if (timeout_pid == 0)
  99. {
  100. auto ts = get_timespec(time_out - Clock::now());
  101. ::setpgid(0, p.grp);
  102. ::nanosleep(&ts, nullptr);
  103. ::exit(0);
  104. }
  105. struct child_cleaner_t
  106. {
  107. pid_t pid;
  108. ~child_cleaner_t()
  109. {
  110. int res;
  111. ::kill(pid, -15);
  112. ::waitpid(pid, &res, WNOHANG);
  113. }
  114. };
  115. child_cleaner_t child_cleaner{timeout_pid};
  116. do
  117. {
  118. int ret_sig = 0;
  119. int status;
  120. if ((::waitpid(timeout_pid, &status, WNOHANG) != 0)
  121. && (WIFEXITED(status) || WIFSIGNALED(status)))
  122. ret = ::sigwait(&sigset, nullptr);
  123. errno = 0;
  124. if ((ret == SIGCHLD) && (old_sig.sa_handler != SIG_DFL) && (old_sig.sa_handler != SIG_IGN))
  125. old_sig.sa_handler(ret);
  126. ret = ::waitpid(-p.grp, &siginfo.si_status, 0); //so in case it exited, we wanna reap it first
  127. if (ret == -1)
  128. {
  129. ec = get_last_error();
  130. return false;
  131. }
  132. //check if we're done
  133. ret = ::waitid(P_PGID, p.grp, &siginfo, WEXITED | WNOHANG);
  134. }
  135. while (((ret != -1) || (errno != ECHILD)) && !(timed_out = (Clock::now() > time_out)));
  136. #endif
  137. if (errno != ECHILD)
  138. {
  139. ec = boost::process::detail::get_last_error();
  140. return !timed_out;
  141. }
  142. else
  143. {
  144. ec.clear();
  145. return true; //even if timed out, there are no child proccessess left
  146. }
  147. }
  148. template< class Clock, class Duration >
  149. inline bool wait_until(
  150. const group_handle &p,
  151. const std::chrono::time_point<Clock, Duration>& time_out) noexcept
  152. {
  153. std::error_code ec;
  154. bool b = wait_until(p, time_out, ec);
  155. boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_until");
  156. return b;
  157. }
  158. template< class Rep, class Period >
  159. inline bool wait_for(
  160. const group_handle &p,
  161. const std::chrono::duration<Rep, Period>& rel_time,
  162. std::error_code & ec) noexcept
  163. {
  164. return wait_until(p, std::chrono::steady_clock::now() + rel_time, ec);
  165. }
  166. template< class Rep, class Period >
  167. inline bool wait_for(
  168. const group_handle &p,
  169. const std::chrono::duration<Rep, Period>& rel_time) noexcept
  170. {
  171. std::error_code ec;
  172. bool b = wait_for(p, rel_time, ec);
  173. boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_for");
  174. return b;
  175. }
  176. }}}}
  177. #endif