wait_group.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. ::siginfo_t siginfo;
  53. bool timed_out = false;
  54. int ret;
  55. #if defined(BOOST_POSIX_HAS_SIGTIMEDWAIT)
  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. ::sigset_t sigset;
  65. if (sigemptyset(&sigset) != 0)
  66. {
  67. ec = get_last_error();
  68. return false;
  69. }
  70. if (sigaddset(&sigset, SIGCHLD) != 0)
  71. {
  72. ec = get_last_error();
  73. return false;
  74. }
  75. struct ::sigaction old_sig;
  76. if (-1 == ::sigaction(SIGCHLD, nullptr, &old_sig))
  77. {
  78. ec = get_last_error();
  79. return false;
  80. }
  81. do
  82. {
  83. auto ts = get_timespec(time_out - Clock::now());
  84. ret = ::sigtimedwait(&sigset, nullptr, &ts);
  85. errno = 0;
  86. if ((ret == SIGCHLD) && (old_sig.sa_handler != SIG_DFL) && (old_sig.sa_handler != SIG_IGN))
  87. old_sig.sa_handler(ret);
  88. ret = ::waitpid(-p.grp, &siginfo.si_status, 0); //so in case it exited, we wanna reap it first
  89. if (ret == -1)
  90. {
  91. if ((errno == ECHILD) || (errno == ESRCH))
  92. {
  93. ec.clear();
  94. return true;
  95. }
  96. else
  97. {
  98. ec = get_last_error();
  99. return false;
  100. }
  101. }
  102. //check if we're done ->
  103. ret = ::waitid(P_PGID, p.grp, &siginfo, WEXITED | WNOHANG);
  104. }
  105. while (((ret != -1) || ((errno != ECHILD) && (errno != ESRCH))) && !(timed_out = (Clock::now() > time_out)));
  106. if (errno != ECHILD)
  107. {
  108. ec = boost::process::detail::get_last_error();
  109. return !timed_out;
  110. }
  111. else
  112. {
  113. ec.clear();
  114. return true; //even if timed out, there are no child proccessess left
  115. }
  116. #else
  117. ::timespec sleep_interval;
  118. sleep_interval.tv_sec = 0;
  119. sleep_interval.tv_nsec = 1000000;
  120. while (!(timed_out = (Clock::now() > time_out)))
  121. {
  122. ret = ::waitid(P_PGID, p.grp, &siginfo, WEXITED | WSTOPPED | WNOHANG);
  123. if (ret == -1)
  124. {
  125. if ((errno == ECHILD) || (errno == ESRCH))
  126. {
  127. ec.clear();
  128. return true;
  129. }
  130. ec = boost::process::detail::get_last_error();
  131. return false;
  132. }
  133. //we can wait, because unlike in the wait_for_exit, we have no race condition regarding eh exit code.
  134. ::nanosleep(&sleep_interval, nullptr);
  135. }
  136. return !timed_out;
  137. #endif
  138. }
  139. template< class Clock, class Duration >
  140. inline bool wait_until(
  141. const group_handle &p,
  142. const std::chrono::time_point<Clock, Duration>& time_out) noexcept
  143. {
  144. std::error_code ec;
  145. bool b = wait_until(p, time_out, ec);
  146. boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_until");
  147. return b;
  148. }
  149. template< class Rep, class Period >
  150. inline bool wait_for(
  151. const group_handle &p,
  152. const std::chrono::duration<Rep, Period>& rel_time,
  153. std::error_code & ec) noexcept
  154. {
  155. return wait_until(p, std::chrono::steady_clock::now() + rel_time, ec);
  156. }
  157. template< class Rep, class Period >
  158. inline bool wait_for(
  159. const group_handle &p,
  160. const std::chrono::duration<Rep, Period>& rel_time) noexcept
  161. {
  162. std::error_code ec;
  163. bool b = wait_for(p, rel_time, ec);
  164. boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_for");
  165. return b;
  166. }
  167. }}}}
  168. #endif