]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | // Copyright (c) 2106 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 | ||
11fdf7f2 TL |
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. | |
b32b8144 | 19 | constexpr int still_active = 0x7F; |
11fdf7f2 | 20 | static_assert(!WIFEXITED(still_active) && !WIFSIGNALED(still_active), "Internal Error"); |
b32b8144 | 21 | |
11fdf7f2 | 22 | inline bool is_running(int code) |
b32b8144 | 23 | { |
11fdf7f2 | 24 | return !WIFEXITED(code) && !WIFSIGNALED(code); |
b32b8144 FG |
25 | } |
26 | ||
27 | inline bool is_running(const child_handle &p, int & exit_code, std::error_code &ec) noexcept | |
28 | { | |
29 | int status; | |
11fdf7f2 TL |
30 | auto ret = ::waitpid(p.pid, &status, WNOHANG); |
31 | ||
b32b8144 FG |
32 | if (ret == -1) |
33 | { | |
34 | if (errno != ECHILD) //because it no child is running, than this one isn't either, obviously. | |
35 | ec = ::boost::process::detail::get_last_error(); | |
36 | return false; | |
37 | } | |
38 | else if (ret == 0) | |
39 | return true; | |
40 | else | |
41 | { | |
42 | ec.clear(); | |
11fdf7f2 TL |
43 | |
44 | if (!is_running(status)) | |
b32b8144 | 45 | exit_code = status; |
11fdf7f2 | 46 | |
b32b8144 FG |
47 | return false; |
48 | } | |
49 | } | |
50 | ||
11fdf7f2 | 51 | inline bool is_running(const child_handle &p, int & exit_code) |
b32b8144 | 52 | { |
11fdf7f2 TL |
53 | std::error_code ec; |
54 | bool b = is_running(p, exit_code, ec); | |
55 | boost::process::detail::throw_error(ec, "waitpid(2) failed in is_running"); | |
56 | return b; | |
b32b8144 FG |
57 | } |
58 | ||
59 | inline int eval_exit_status(int code) | |
60 | { | |
11fdf7f2 TL |
61 | if (WIFEXITED(code)) |
62 | { | |
63 | return WEXITSTATUS(code); | |
64 | } | |
65 | else if (WIFSIGNALED(code)) | |
66 | { | |
67 | return WTERMSIG(code); | |
68 | } | |
69 | else | |
70 | { | |
71 | return code; | |
72 | } | |
b32b8144 FG |
73 | } |
74 | ||
75 | }}}} | |
76 | ||
77 | #endif |