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