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>
15 #include <system_error>
16 #include <sys/types.h>
19 namespace boost { namespace process { namespace detail { namespace posix {
21 inline void wait(const group_handle &p)
27 ret = ::waitpid(-p.grp, &status, 0);
28 } while (((ret == -1) && (errno == EINTR)) || (ret != -1 && !WIFEXITED(status) && !WIFSIGNALED(status)));
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");
35 inline void wait(const group_handle &p, std::error_code &ec) noexcept
42 ret = ::waitpid(-p.grp, &status, 0);
44 while (((ret == -1) && (errno == EINTR)) || (ret != -1 && !WIFEXITED(status) && !WIFSIGNALED(status)));
47 ec = boost::process::detail::get_last_error();
48 else if (WIFSIGNALED(status))
49 ec = std::make_error_code(std::errc::no_such_process);
55 template< class Rep, class Period >
57 const group_handle &p,
58 const std::chrono::duration<Rep, Period>& rel_time)
64 auto start = std::chrono::system_clock::now();
65 auto time_out = start + rel_time;
67 bool time_out_occured = false;
70 ret = ::waitpid(-p.grp, &status, WUNTRACED | WNOHANG);
71 if (std::chrono::system_clock::now() >= time_out)
73 time_out_occured = true;
77 while (((ret == -1) && errno == EINTR) ||
78 ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status)));
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");
86 return !time_out_occured;
90 template< class Rep, class Period >
92 const group_handle &p,
93 const std::chrono::duration<Rep, Period>& rel_time,
94 std::error_code & ec) noexcept
100 auto start = std::chrono::system_clock::now();
101 auto time_out = start + rel_time;
103 bool time_out_occured = false;
106 ret = ::waitpid(-p.grp, &status, WUNTRACED | WNOHANG);
107 if (std::chrono::system_clock::now() >= time_out)
109 time_out_occured = true;
113 while (((ret == -1) && errno == EINTR) ||
114 ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status)));
118 ec = boost::process::detail::get_last_error();
119 else if (WIFSIGNALED(status))
120 ec = std::make_error_code(std::errc::no_such_process);
124 return !time_out_occured;
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)
138 bool time_out_occured = false;
141 ret = ::waitpid(-p.grp, &status, WUNTRACED | WNOHANG);
142 if (std::chrono::system_clock::now() >= time_out)
144 time_out_occured = true;
148 while (((ret == -1) && errno == EINTR) ||
149 ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status)));
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");
158 return !time_out_occured;
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
172 bool time_out_occured = false;
175 ret = ::waitpid(-p.grp, &status, WUNTRACED | WNOHANG);
176 if (std::chrono::system_clock::now() >= time_out)
178 time_out_occured = true;
182 while (((ret == -1) && errno == EINTR) ||
183 ((ret != -1) && !WIFEXITED(status) && !WIFSIGNALED(status)));
187 ec = boost::process::detail::get_last_error();
188 else if (WIFSIGNALED(status))
189 ec = std::make_error_code(std::errc::no_such_process);
193 return !time_out_occured;