1 // Copyright (c) 2016 Klemens D. Morgenstern
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)
6 #ifndef BOOST_PROCESS_DETAIL_POSIX_IS_RUNNING_HPP
7 #define BOOST_PROCESS_DETAIL_POSIX_IS_RUNNING_HPP
9 #include <boost/process/detail/config.hpp>
10 #include <boost/process/detail/posix/child_handle.hpp>
11 #include <system_error>
14 namespace boost { namespace process { namespace detail { namespace posix {
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");
25 inline bool is_running(int code)
27 return !WIFEXITED(code) && !WIFSIGNALED(code);
30 inline bool is_running(const child_handle &p, int & exit_code, std::error_code &ec) noexcept
33 auto ret = ::waitpid(p.pid, &status, WNOHANG);
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();
47 if (!is_running(status))
54 inline bool is_running(const child_handle &p, int & exit_code)
57 bool b = is_running(p, exit_code, ec);
58 boost::process::detail::throw_error(ec, "waitpid(2) failed in is_running");
62 inline int eval_exit_status(int code)
66 return WEXITSTATUS(code);
68 else if (WIFSIGNALED(code))
70 return WTERMSIG(code);