1 // Copyright (c) 2016 Klemens D. Morgenstern
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef BOOST_PROCESS_DETAIL_POSIX_IS_RUNNING_HPP
7 #define BOOST_PROCESS_DETAIL_POSIX_IS_RUNNING_HPP
8 
9 #include <boost/process/detail/config.hpp>
10 #include <boost/process/detail/posix/child_handle.hpp>
11 #include <system_error>
12 #include <sys/wait.h>
13 
14 namespace boost { namespace process { namespace detail { namespace posix {
15 
16 // Use the "stopped" state (WIFSTOPPED) to indicate "not terminated".
17 // This bit arrangement of status codes is not guaranteed by POSIX, but (according to comments in
18 // the glibc <bits/waitstatus.h> header) is the same across systems in practice.
19 constexpr int still_active = 0x017f;
20 static_assert(WIFSTOPPED(still_active), "Expected still_active to indicate WIFSTOPPED");
21 static_assert(!WIFEXITED(still_active), "Expected still_active to not indicate WIFEXITED");
22 static_assert(!WIFSIGNALED(still_active), "Expected still_active to not indicate WIFSIGNALED");
23 static_assert(!WIFCONTINUED(still_active), "Expected still_active to not indicate WIFCONTINUED");
24 
is_running(int code)25 inline bool is_running(int code)
26 {
27     return !WIFEXITED(code) && !WIFSIGNALED(code);
28 }
29 
is_running(const child_handle & p,int & exit_code,std::error_code & ec)30 inline bool is_running(const child_handle &p, int & exit_code, std::error_code &ec) noexcept
31 {
32     int status;
33     auto ret = ::waitpid(p.pid, &status, WNOHANG);
34 
35     if (ret == -1)
36     {
37         if (errno != ECHILD) //because it no child is running, than this one isn't either, obviously.
38             ec = ::boost::process::detail::get_last_error();
39         return false;
40     }
41     else if (ret == 0)
42         return true;
43     else
44     {
45         ec.clear();
46 
47         if (!is_running(status))
48             exit_code = status;
49 
50         return false;
51     }
52 }
53 
is_running(const child_handle & p,int & exit_code)54 inline bool is_running(const child_handle &p, int & exit_code)
55 {
56     std::error_code ec;
57     bool b = is_running(p, exit_code, ec);
58     boost::process::detail::throw_error(ec, "waitpid(2) failed in is_running");
59     return b;
60 }
61 
eval_exit_status(int code)62 inline int eval_exit_status(int code)
63 {
64     if (WIFEXITED(code))
65     {
66         return WEXITSTATUS(code);
67     }
68     else if (WIFSIGNALED(code))
69     {
70         return WTERMSIG(code);
71     }
72     else
73     {
74         return code;
75     }
76 }
77 
78 }}}}
79 
80 #endif
81