]>
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. | |
92f5a8d4 TL |
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"); | |
b32b8144 | 24 | |
11fdf7f2 | 25 | inline bool is_running(int code) |
b32b8144 | 26 | { |
11fdf7f2 | 27 | return !WIFEXITED(code) && !WIFSIGNALED(code); |
b32b8144 FG |
28 | } |
29 | ||
30 | inline bool is_running(const child_handle &p, int & exit_code, std::error_code &ec) noexcept | |
31 | { | |
32 | int status; | |
11fdf7f2 TL |
33 | auto ret = ::waitpid(p.pid, &status, WNOHANG); |
34 | ||
b32b8144 FG |
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(); | |
11fdf7f2 TL |
46 | |
47 | if (!is_running(status)) | |
b32b8144 | 48 | exit_code = status; |
11fdf7f2 | 49 | |
b32b8144 FG |
50 | return false; |
51 | } | |
52 | } | |
53 | ||
11fdf7f2 | 54 | inline bool is_running(const child_handle &p, int & exit_code) |
b32b8144 | 55 | { |
11fdf7f2 TL |
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; | |
b32b8144 FG |
60 | } |
61 | ||
62 | inline int eval_exit_status(int code) | |
63 | { | |
11fdf7f2 TL |
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 | } | |
b32b8144 FG |
76 | } |
77 | ||
78 | }}}} | |
79 | ||
80 | #endif |