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_FOR_EXIT_HPP
11 #define BOOST_PROCESS_DETAIL_POSIX_WAIT_FOR_EXIT_HPP
13 #include <boost/process/detail/config.hpp>
14 #include <boost/process/detail/posix/child_handle.hpp>
15 #include <system_error>
16 #include <sys/types.h>
20 namespace boost { namespace process { namespace detail { namespace posix {
22 inline void wait(const child_handle &p, int & exit_code, std::error_code &ec) noexcept
29 ret = ::waitpid(p.pid, &status, 0);
31 while (((ret == -1) && (errno == EINTR)) ||
32 (ret != -1 && !WIFEXITED(status) && !WIFSIGNALED(status)));
35 ec = boost::process::detail::get_last_error();
43 inline void wait(const child_handle &p, int & exit_code) noexcept
46 wait(p, exit_code, ec);
47 boost::process::detail::throw_error(ec, "waitpid(2) failed in wait");
50 template< class Clock, class Duration >
51 inline bool wait_until(
52 const child_handle &p,
54 const std::chrono::time_point<Clock, Duration>& time_out,
55 std::error_code & ec) noexcept
59 //I need to set the signal, because it might be ignore / default, in which case sigwait might not work.
61 using _signal_t = void(*)(int);
62 static thread_local _signal_t sigchld_handler = SIG_DFL;
64 struct signal_interceptor_t
66 static void handler_func(int val)
68 if ((sigchld_handler != SIG_DFL) && (sigchld_handler != SIG_IGN))
71 signal_interceptor_t() { sigchld_handler = ::signal(SIGCHLD, &handler_func); }
72 ~signal_interceptor_t() { ::signal(SIGCHLD, sigchld_handler); sigchld_handler = SIG_DFL;}
74 } signal_interceptor{};
76 if (sigemptyset(&sigset) != 0)
78 ec = get_last_error();
81 if (sigaddset(&sigset, SIGCHLD) != 0)
83 ec = get_last_error();
88 [](const Duration & dur)
91 ts.tv_sec = std::chrono::duration_cast<std::chrono::seconds>(dur).count();
92 ts.tv_nsec = std::chrono::duration_cast<std::chrono::nanoseconds>(dur).count() % 1000000000;
99 struct ::sigaction old_sig;
100 if (-1 == ::sigaction(SIGCHLD, nullptr, &old_sig))
102 ec = get_last_error();
108 #if defined(BOOST_POSIX_HAS_SIGTIMEDWAIT)
111 auto ts = get_timespec(time_out - Clock::now());
112 auto ret_sig = ::sigtimedwait(&sigset, nullptr, &ts);
114 ret = ::waitpid(p.pid, &status, WNOHANG);
116 if ((ret_sig == SIGCHLD) && (old_sig.sa_handler != SIG_DFL) && (old_sig.sa_handler != SIG_IGN))
117 old_sig.sa_handler(ret);
121 timed_out = Clock::now() >= time_out;
127 (((ret == -1) && errno == EINTR) ||
128 ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status))));
130 //if we do not have sigtimedwait, we fork off a child process to get the signal in time
131 pid_t timeout_pid = ::fork();
132 if (timeout_pid == -1)
134 ec = boost::process::detail::get_last_error();
137 else if (timeout_pid == 0)
139 auto ts = get_timespec(time_out - Clock::now());
141 while (ts.tv_sec > 0 || ts.tv_nsec > 0)
143 if (::nanosleep(&ts, &rem) != 0)
146 if ((err == EINVAL) || (err == EFAULT))
149 ts = get_timespec(time_out - Clock::now());
154 struct child_cleaner_t
160 ::kill(pid, SIGKILL);
161 ::waitpid(pid, &res, WNOHANG);
164 child_cleaner_t child_cleaner{timeout_pid};
169 if ((::waitpid(timeout_pid, &status, WNOHANG) != 0)
170 && (WIFEXITED(status) || WIFSIGNALED(status)))
174 ret = ::sigwait(&sigset, &sig_);
177 if ((sig_ == SIGCHLD) &&
178 (old_sig.sa_handler != SIG_DFL) && (old_sig.sa_handler != SIG_IGN))
179 old_sig.sa_handler(ret);
181 ret = ::waitpid(p.pid, &status, WNOHANG);
182 if (ret == 0) // == > is running
184 timed_out = Clock::now() >= time_out;
190 (((ret == -1) && errno == EINTR) ||
191 ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status))));
195 ec = boost::process::detail::get_last_error();
205 template< class Clock, class Duration >
206 inline bool wait_until(
207 const child_handle &p,
209 const std::chrono::time_point<Clock, Duration>& time_out)
212 bool b = wait_until(p, exit_code, time_out, ec);
213 boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_until");
217 template< class Rep, class Period >
218 inline bool wait_for(
219 const child_handle &p,
221 const std::chrono::duration<Rep, Period>& rel_time,
222 std::error_code & ec) noexcept
224 return wait_until(p, exit_code, std::chrono::steady_clock::now() + rel_time, ec);
227 template< class Rep, class Period >
228 inline bool wait_for(
229 const child_handle &p,
231 const std::chrono::duration<Rep, Period>& rel_time)
234 bool b = wait_for(p, exit_code, rel_time, ec);
235 boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_for");