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
wait(const group_handle & p,std::error_code & ec)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
wait(const group_handle & p)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 >
wait_until(const group_handle & p,const std::chrono::time_point<Clock,Duration> & time_out,std::error_code & ec)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 ::timespec sleep_interval;
68 sleep_interval.tv_sec = 0;
69 sleep_interval.tv_nsec = 100000000;
70
71
72 while (!(timed_out = (Clock::now() > time_out)))
73 {
74 ret = ::waitid(P_PGID, p.grp, &siginfo, WEXITED | WSTOPPED | WNOHANG);
75 if (ret == -1)
76 {
77 if ((errno == ECHILD) || (errno == ESRCH))
78 {
79 ec.clear();
80 return true;
81 }
82 ec = boost::process::detail::get_last_error();
83 return false;
84 }
85 //we can wait, because unlike in the wait_for_exit, we have no race condition regarding eh exit code.
86 ::nanosleep(&sleep_interval, nullptr);
87 }
88 return !timed_out;
89 }
90
91 template< class Clock, class Duration >
wait_until(const group_handle & p,const std::chrono::time_point<Clock,Duration> & time_out)92 inline bool wait_until(
93 const group_handle &p,
94 const std::chrono::time_point<Clock, Duration>& time_out) noexcept
95 {
96 std::error_code ec;
97 bool b = wait_until(p, time_out, ec);
98 boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_until");
99 return b;
100 }
101
102 template< class Rep, class Period >
wait_for(const group_handle & p,const std::chrono::duration<Rep,Period> & rel_time,std::error_code & ec)103 inline bool wait_for(
104 const group_handle &p,
105 const std::chrono::duration<Rep, Period>& rel_time,
106 std::error_code & ec) noexcept
107 {
108 return wait_until(p, std::chrono::steady_clock::now() + rel_time, ec);
109 }
110
111 template< class Rep, class Period >
wait_for(const group_handle & p,const std::chrono::duration<Rep,Period> & rel_time)112 inline bool wait_for(
113 const group_handle &p,
114 const std::chrono::duration<Rep, Period>& rel_time) noexcept
115 {
116 std::error_code ec;
117 bool b = wait_for(p, rel_time, ec);
118 boost::process::detail::throw_error(ec, "waitpid(2) failed in wait_for");
119 return b;
120 }
121
122 }}}}
123
124 #endif
125