]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/process/detail/posix/wait_group.hpp
d12cce1064224456ff2a61fa8caaa5a2559a86e6
[ceph.git] / ceph / src / boost / boost / process / detail / posix / wait_group.hpp
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_GROUP_HPP
11 #define BOOST_PROCESS_DETAIL_POSIX_WAIT_GROUP_HPP
12
13 #include <boost/process/detail/config.hpp>
14 #include <boost/process/detail/posix/group_handle.hpp>
15 #include <chrono>
16 #include <system_error>
17 #include <sys/types.h>
18 #include <sys/wait.h>
19 #include <unistd.h>
20
21 namespace boost { namespace process { namespace detail { namespace posix {
22
23 inline void wait(const group_handle &p, std::error_code &ec) noexcept
24 {
25 pid_t ret;
26 siginfo_t status;
27
28 do
29 {
30 ret = ::waitpid(-p.grp, &status.si_status, 0);
31 if (ret == -1)
32 {
33 ec = get_last_error();
34 return;
35 }
36
37 //ECHILD --> no child processes left.
38 ret = ::waitid(P_PGID, p.grp, &status, WEXITED | WNOHANG);
39 }
40 while ((ret != -1) || (errno != ECHILD));
41
42 if (errno != ECHILD)
43 ec = boost::process::detail::get_last_error();
44 else
45 ec.clear();
46 }
47
48 inline void wait(const group_handle &p) noexcept
49 {
50 std::error_code ec;
51 wait(p, ec);
52 boost::process::detail::throw_error(ec, "waitpid(2) failed in wait");
53 }
54
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
60 {
61
62 ::siginfo_t siginfo;
63
64 bool timed_out = false;
65 int ret;
66
67 #if defined(BOOST_POSIX_HAS_SIGTIMEDWAIT)
68 auto get_timespec =
69 +[](const Duration & dur)
70 {
71 ::timespec ts;
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;
74 return ts;
75 };
76
77 ::sigset_t sigset;
78
79 if (sigemptyset(&sigset) != 0)
80 {
81 ec = get_last_error();
82 return false;
83 }
84 if (sigaddset(&sigset, SIGCHLD) != 0)
85 {
86 ec = get_last_error();
87 return false;
88 }
89
90 struct ::sigaction old_sig;
91 if (-1 == ::sigaction(SIGCHLD, nullptr, &old_sig))
92 {
93 ec = get_last_error();
94 return false;
95 }
96
97 do
98 {
99 auto ts = get_timespec(time_out - Clock::now());
100 ret = ::sigtimedwait(&sigset, nullptr, &ts);
101 errno = 0;
102 if ((ret == SIGCHLD) && (old_sig.sa_handler != SIG_DFL) && (old_sig.sa_handler != SIG_IGN))
103 old_sig.sa_handler(ret);
104
105 ret = ::waitpid(-p.grp, &siginfo.si_status, 0); //so in case it exited, we wanna reap it first
106 if (ret == -1)
107 {
108 if ((errno == ECHILD) || (errno == ESRCH))
109 {
110 ec.clear();
111 return true;
112 }
113 else
114 {
115 ec = get_last_error();
116 return false;
117 }
118 }
119
120 //check if we're done ->
121 ret = ::waitid(P_PGID, p.grp, &siginfo, WEXITED | WNOHANG);
122 }
123 while (((ret != -1) || ((errno != ECHILD) && (errno != ESRCH))) && !(timed_out = (Clock::now() > time_out)));
124
125 if (errno != ECHILD)
126 {
127 ec = boost::process::detail::get_last_error();
128 return !timed_out;
129 }
130 else
131 {
132 ec.clear();
133 return true; //even if timed out, there are no child proccessess left
134 }
135
136 #else
137 ::timespec sleep_interval;
138 sleep_interval.tv_sec = 0;
139 sleep_interval.tv_nsec = 1000000;
140
141
142 while (!(timed_out = (Clock::now() > time_out)))
143 {
144 ret = ::waitid(P_PGID, p.grp, &siginfo, WEXITED | WSTOPPED | WNOHANG);
145 if (ret == -1)
146 {
147 if ((errno == ECHILD) || (errno == ESRCH))
148 {
149 ec.clear();
150 return true;
151 }
152 ec = boost::process::detail::get_last_error();
153 return false;
154 }
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);
157 }
158 return !timed_out;
159 #endif
160 }
161
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
166 {
167 std::error_code ec;
168 bool b = wait_until(p, time_out, ec);
169 boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_until");
170 return b;
171 }
172
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
178 {
179 return wait_until(p, std::chrono::steady_clock::now() + rel_time, ec);
180 }
181
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
186 {
187 std::error_code ec;
188 bool b = wait_for(p, rel_time, ec);
189 boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_for");
190 return b;
191 }
192
193 }}}}
194
195 #endif