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
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)
10 #ifndef BOOST_PROCESS_DETAIL_POSIX_WAIT_GROUP_HPP
11 #define BOOST_PROCESS_DETAIL_POSIX_WAIT_GROUP_HPP
13 #include <boost/process/detail/config.hpp>
14 #include <boost/process/detail/posix/group_handle.hpp>
16 #include <system_error>
17 #include <sys/types.h>
21 namespace boost { namespace process { namespace detail { namespace posix {
23 inline void wait(const group_handle &p, std::error_code &ec) noexcept
30 ret = ::waitpid(-p.grp, &status.si_status, 0);
33 ec = get_last_error();
37 //ECHILD --> no child processes left.
38 ret = ::waitid(P_PGID, p.grp, &status, WEXITED | WNOHANG);
40 while ((ret != -1) || (errno != ECHILD));
43 ec = boost::process::detail::get_last_error();
48 inline void wait(const group_handle &p) noexcept
52 boost::process::detail::throw_error(ec, "waitpid(2) failed in wait");
55 template< class Clock, class Duration >
56 inline bool wait_until(
57 const group_handle &p,
58 const std::chrono::time_point<Clock, Duration>& time_out,
59 std::error_code & ec) noexcept
64 bool timed_out = false;
67 #if defined(BOOST_POSIX_HAS_SIGTIMEDWAIT)
69 +[](const Duration & dur)
72 ts.tv_sec = std::chrono::duration_cast<std::chrono::seconds>(dur).count();
73 ts.tv_nsec = std::chrono::duration_cast<std::chrono::nanoseconds>(dur).count() % 1000000000;
79 if (sigemptyset(&sigset) != 0)
81 ec = get_last_error();
84 if (sigaddset(&sigset, SIGCHLD) != 0)
86 ec = get_last_error();
90 struct ::sigaction old_sig;
91 if (-1 == ::sigaction(SIGCHLD, nullptr, &old_sig))
93 ec = get_last_error();
99 auto ts = get_timespec(time_out - Clock::now());
100 ret = ::sigtimedwait(&sigset, nullptr, &ts);
102 if ((ret == SIGCHLD) && (old_sig.sa_handler != SIG_DFL) && (old_sig.sa_handler != SIG_IGN))
103 old_sig.sa_handler(ret);
105 ret = ::waitpid(-p.grp, &siginfo.si_status, 0); //so in case it exited, we wanna reap it first
108 if ((errno == ECHILD) || (errno == ESRCH))
115 ec = get_last_error();
120 //check if we're done ->
121 ret = ::waitid(P_PGID, p.grp, &siginfo, WEXITED | WNOHANG);
123 while (((ret != -1) || ((errno != ECHILD) && (errno != ESRCH))) && !(timed_out = (Clock::now() > time_out)));
127 ec = boost::process::detail::get_last_error();
133 return true; //even if timed out, there are no child proccessess left
137 ::timespec sleep_interval;
138 sleep_interval.tv_sec = 0;
139 sleep_interval.tv_nsec = 1000000;
142 while (!(timed_out = (Clock::now() > time_out)))
144 ret = ::waitid(P_PGID, p.grp, &siginfo, WEXITED | WSTOPPED | WNOHANG);
147 if ((errno == ECHILD) || (errno == ESRCH))
152 ec = boost::process::detail::get_last_error();
155 //we can wait, because unlike in the wait_for_exit, we have no race condition regarding eh exit code.
156 ::nanosleep(&sleep_interval, nullptr);
162 template< class Clock, class Duration >
163 inline bool wait_until(
164 const group_handle &p,
165 const std::chrono::time_point<Clock, Duration>& time_out) noexcept
168 bool b = wait_until(p, time_out, ec);
169 boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_until");
173 template< class Rep, class Period >
174 inline bool wait_for(
175 const group_handle &p,
176 const std::chrono::duration<Rep, Period>& rel_time,
177 std::error_code & ec) noexcept
179 return wait_until(p, std::chrono::steady_clock::now() + rel_time, ec);
182 template< class Rep, class Period >
183 inline bool wait_for(
184 const group_handle &p,
185 const std::chrono::duration<Rep, Period>& rel_time) noexcept
188 bool b = wait_for(p, rel_time, ec);
189 boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_for");