]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/process/detail/posix/wait_group.hpp
update sources to v12.2.3
[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 <system_error>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18
19 namespace boost { namespace process { namespace detail { namespace posix {
20
21 inline void wait(const group_handle &p)
22 {
23 pid_t ret;
24 int status;
25 do
26 {
27 ret = ::waitpid(-p.grp, &status, 0);
28 } while (((ret == -1) && (errno == EINTR)) || (ret != -1 && !WIFEXITED(status) && !WIFSIGNALED(status)));
29 if (ret == -1)
30 boost::process::detail::throw_last_error("waitpid(2) failed");
31 if (WIFSIGNALED(status))
32 throw process_error(std::error_code(), "process group terminated due to receipt of a signal");
33 }
34
35 inline void wait(const group_handle &p, std::error_code &ec) noexcept
36 {
37 pid_t ret;
38 int status;
39
40 do
41 {
42 ret = ::waitpid(-p.grp, &status, 0);
43 }
44 while (((ret == -1) && (errno == EINTR)) || (ret != -1 && !WIFEXITED(status) && !WIFSIGNALED(status)));
45
46 if (ret == -1)
47 ec = boost::process::detail::get_last_error();
48 else if (WIFSIGNALED(status))
49 ec = std::make_error_code(std::errc::no_such_process);
50 else
51 ec.clear();
52
53 }
54
55 template< class Rep, class Period >
56 inline bool wait_for(
57 const group_handle &p,
58 const std::chrono::duration<Rep, Period>& rel_time)
59 {
60
61 pid_t ret;
62 int status;
63
64 auto start = std::chrono::system_clock::now();
65 auto time_out = start + rel_time;
66
67 bool time_out_occured = false;
68 do
69 {
70 ret = ::waitpid(-p.grp, &status, WUNTRACED | WNOHANG);
71 if (std::chrono::system_clock::now() >= time_out)
72 {
73 time_out_occured = true;
74 break;
75 }
76 }
77 while (((ret == -1) && errno == EINTR) ||
78 ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status)));
79
80
81 if (ret == -1)
82 boost::process::detail::throw_last_error("waitpid(2) failed");
83 if (WIFSIGNALED(status))
84 throw process_error(std::error_code(), "process group terminated due to receipt of a signal");
85
86 return !time_out_occured;
87 }
88
89
90 template< class Rep, class Period >
91 inline bool wait_for(
92 const group_handle &p,
93 const std::chrono::duration<Rep, Period>& rel_time,
94 std::error_code & ec) noexcept
95 {
96
97 pid_t ret;
98 int status;
99
100 auto start = std::chrono::system_clock::now();
101 auto time_out = start + rel_time;
102
103 bool time_out_occured = false;
104 do
105 {
106 ret = ::waitpid(-p.grp, &status, WUNTRACED | WNOHANG);
107 if (std::chrono::system_clock::now() >= time_out)
108 {
109 time_out_occured = true;
110 break;
111 }
112 }
113 while (((ret == -1) && errno == EINTR) ||
114 ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status)));
115
116
117 if (ret == -1)
118 ec = boost::process::detail::get_last_error();
119 else if (WIFSIGNALED(status))
120 ec = std::make_error_code(std::errc::no_such_process);
121 else
122 ec.clear();
123
124 return !time_out_occured;
125 }
126
127
128
129 template< class Rep, class Period >
130 inline bool wait_until(
131 const group_handle &p,
132 const std::chrono::duration<Rep, Period>& time_out)
133 {
134
135 pid_t ret;
136 int status;
137
138 bool time_out_occured = false;
139 do
140 {
141 ret = ::waitpid(-p.grp, &status, WUNTRACED | WNOHANG);
142 if (std::chrono::system_clock::now() >= time_out)
143 {
144 time_out_occured = true;
145 break;
146 }
147 }
148 while (((ret == -1) && errno == EINTR) ||
149 ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status)));
150
151
152 if (ret == -1)
153 boost::process::detail::throw_last_error("waitpid(2) failed");
154 if (WIFSIGNALED(status))
155 throw process_error(std::error_code(), "process group terminated due to receipt of a signal");
156
157
158 return !time_out_occured;
159 }
160
161
162 template< class Rep, class Period >
163 inline bool wait_until(
164 const group_handle &p,
165 const std::chrono::duration<Rep, Period>& time_out,
166 std::error_code & ec) noexcept
167 {
168
169 pid_t ret;
170 int status;
171
172 bool time_out_occured = false;
173 do
174 {
175 ret = ::waitpid(-p.grp, &status, WUNTRACED | WNOHANG);
176 if (std::chrono::system_clock::now() >= time_out)
177 {
178 time_out_occured = true;
179 break;
180 }
181 }
182 while (((ret == -1) && errno == EINTR) ||
183 ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status)));
184
185
186 if (ret == -1)
187 ec = boost::process::detail::get_last_error();
188 else if (WIFSIGNALED(status))
189 ec = std::make_error_code(std::errc::no_such_process);
190 else
191 ec.clear();
192
193 return !time_out_occured;
194 }
195
196 }}}}
197
198 #endif