wait_for_exit.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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_FOR_EXIT_HPP
  10. #define BOOST_PROCESS_DETAIL_POSIX_WAIT_FOR_EXIT_HPP
  11. #include <boost/process/detail/config.hpp>
  12. #include <boost/process/detail/posix/child_handle.hpp>
  13. #include <system_error>
  14. #include <sys/types.h>
  15. #include <sys/wait.h>
  16. #include <unistd.h>
  17. namespace boost { namespace process { namespace detail { namespace posix {
  18. inline void wait(const child_handle &p, int & exit_code, std::error_code &ec) noexcept
  19. {
  20. pid_t ret;
  21. int status;
  22. do
  23. {
  24. ret = ::waitpid(p.pid, &status, 0);
  25. }
  26. while (((ret == -1) && (errno == EINTR)) ||
  27. (ret != -1 && !WIFEXITED(status) && !WIFSIGNALED(status)));
  28. if (ret == -1)
  29. ec = boost::process::detail::get_last_error();
  30. else
  31. {
  32. ec.clear();
  33. exit_code = status;
  34. }
  35. }
  36. inline void wait(const child_handle &p, int & exit_code) noexcept
  37. {
  38. std::error_code ec;
  39. wait(p, exit_code, ec);
  40. boost::process::detail::throw_error(ec, "waitpid(2) failed in wait");
  41. }
  42. template< class Clock, class Duration >
  43. inline bool wait_until(
  44. const child_handle &p,
  45. int & exit_code,
  46. const std::chrono::time_point<Clock, Duration>& time_out,
  47. std::error_code & ec) noexcept
  48. {
  49. ::sigset_t sigset;
  50. //I need to set the signal, because it might be ignore / default, in which case sigwait might not work.
  51. using _signal_t = void(*)(int);
  52. static thread_local _signal_t sigchld_handler = SIG_DFL;
  53. struct signal_interceptor_t
  54. {
  55. static void handler_func(int val)
  56. {
  57. if ((sigchld_handler != SIG_DFL) && (sigchld_handler != SIG_IGN))
  58. sigchld_handler(val);
  59. }
  60. signal_interceptor_t() { sigchld_handler = ::signal(SIGCHLD, &handler_func); }
  61. ~signal_interceptor_t() { ::signal(SIGCHLD, sigchld_handler); sigchld_handler = SIG_DFL;}
  62. } signal_interceptor{};
  63. if (sigemptyset(&sigset) != 0)
  64. {
  65. ec = get_last_error();
  66. return false;
  67. }
  68. if (sigaddset(&sigset, SIGCHLD) != 0)
  69. {
  70. ec = get_last_error();
  71. return false;
  72. }
  73. auto get_timespec =
  74. [](const Duration & dur)
  75. {
  76. ::timespec ts;
  77. ts.tv_sec = std::chrono::duration_cast<std::chrono::seconds>(dur).count();
  78. ts.tv_nsec = std::chrono::duration_cast<std::chrono::nanoseconds>(dur).count() % 1000000000;
  79. return ts;
  80. };
  81. int ret;
  82. int status{0};
  83. struct ::sigaction old_sig;
  84. if (-1 == ::sigaction(SIGCHLD, nullptr, &old_sig))
  85. {
  86. ec = get_last_error();
  87. return false;
  88. }
  89. bool timed_out;
  90. #if defined(BOOST_POSIX_HAS_SIGTIMEDWAIT)
  91. do
  92. {
  93. auto ts = get_timespec(time_out - Clock::now());
  94. auto ret_sig = ::sigtimedwait(&sigset, nullptr, &ts);
  95. errno = 0;
  96. ret = ::waitpid(p.pid, &status, WNOHANG);
  97. if ((ret_sig == SIGCHLD) && (old_sig.sa_handler != SIG_DFL) && (old_sig.sa_handler != SIG_IGN))
  98. old_sig.sa_handler(ret);
  99. if (ret == 0)
  100. {
  101. timed_out = Clock::now() >= time_out;
  102. if (timed_out)
  103. return false;
  104. }
  105. }
  106. while ((ret == 0) ||
  107. (((ret == -1) && errno == EINTR) ||
  108. ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status))));
  109. #else
  110. //if we do not have sigtimedwait, we fork off a child process to get the signal in time
  111. pid_t timeout_pid = ::fork();
  112. if (timeout_pid == -1)
  113. {
  114. ec = boost::process::detail::get_last_error();
  115. return true;
  116. }
  117. else if (timeout_pid == 0)
  118. {
  119. auto ts = get_timespec(time_out - Clock::now());
  120. ::timespec rem;
  121. while (ts.tv_sec > 0 || ts.tv_nsec > 0)
  122. {
  123. if (::nanosleep(&ts, &rem) != 0)
  124. {
  125. auto err = errno;
  126. if ((err == EINVAL) || (err == EFAULT))
  127. break;
  128. }
  129. ts = get_timespec(time_out - Clock::now());
  130. }
  131. ::exit(0);
  132. }
  133. struct child_cleaner_t
  134. {
  135. pid_t pid;
  136. ~child_cleaner_t()
  137. {
  138. int res;
  139. ::kill(pid, SIGKILL);
  140. ::waitpid(pid, &res, WNOHANG);
  141. }
  142. };
  143. child_cleaner_t child_cleaner{timeout_pid};
  144. do
  145. {
  146. int sig_{0};
  147. if ((::waitpid(timeout_pid, &status, WNOHANG) != 0)
  148. && (WIFEXITED(status) || WIFSIGNALED(status)))
  149. return false;
  150. ret = ::sigwait(&sigset, &sig_);
  151. errno = 0;
  152. if ((sig_ == SIGCHLD) &&
  153. (old_sig.sa_handler != SIG_DFL) && (old_sig.sa_handler != SIG_IGN))
  154. old_sig.sa_handler(ret);
  155. ret = ::waitpid(p.pid, &status, WNOHANG);
  156. if (ret == 0) // == > is running
  157. {
  158. timed_out = Clock::now() >= time_out;
  159. if (timed_out)
  160. return false;
  161. }
  162. }
  163. while ((ret == 0) ||
  164. (((ret == -1) && errno == EINTR) ||
  165. ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status))));
  166. #endif
  167. if (ret == -1)
  168. ec = boost::process::detail::get_last_error();
  169. else
  170. {
  171. ec.clear();
  172. exit_code = status;
  173. }
  174. return true;
  175. }
  176. template< class Clock, class Duration >
  177. inline bool wait_until(
  178. const child_handle &p,
  179. int & exit_code,
  180. const std::chrono::time_point<Clock, Duration>& time_out)
  181. {
  182. std::error_code ec;
  183. bool b = wait_until(p, exit_code, time_out, ec);
  184. boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_until");
  185. return b;
  186. }
  187. template< class Rep, class Period >
  188. inline bool wait_for(
  189. const child_handle &p,
  190. int & exit_code,
  191. const std::chrono::duration<Rep, Period>& rel_time,
  192. std::error_code & ec) noexcept
  193. {
  194. return wait_until(p, exit_code, std::chrono::steady_clock::now() + rel_time, ec);
  195. }
  196. template< class Rep, class Period >
  197. inline bool wait_for(
  198. const child_handle &p,
  199. int & exit_code,
  200. const std::chrono::duration<Rep, Period>& rel_time)
  201. {
  202. std::error_code ec;
  203. bool b = wait_for(p, exit_code, rel_time, ec);
  204. boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_for");
  205. return b;
  206. }
  207. }}}}
  208. #endif