summaryrefslogtreecommitdiff
path: root/boost/process/detail/posix/is_running.hpp
blob: e8a009dd22abb66eac65af8edd24a3cd7a5ce893 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) 2106 Klemens D. Morgenstern
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_PROCESS_DETAIL_POSIX_IS_RUNNING_HPP
#define BOOST_PROCESS_DETAIL_POSIX_IS_RUNNING_HPP

#include <boost/process/detail/config.hpp>
#include <boost/process/detail/posix/child_handle.hpp>
#include <system_error>
#include <sys/wait.h>

namespace boost { namespace process { namespace detail { namespace posix {


constexpr int still_active = 0x7F;
static_assert(!WIFEXITED(still_active), "Internal Error");

inline bool is_running(const child_handle &p, int & exit_code)
{
    int status; 
    auto ret = ::waitpid(p.pid, &status, WNOHANG|WUNTRACED);
    
    if (ret == -1)
    {
        if (errno != ECHILD) //because it no child is running, than this one isn't either, obviously.
            ::boost::process::detail::throw_last_error("is_running error");

        return false;
    }
    else if (ret == 0)
        return true;
    else //exited
    {
        if (WIFEXITED(status))
            exit_code = status;
        return false;
    }
}

inline bool is_running(const child_handle &p, int & exit_code, std::error_code &ec) noexcept
{
    int status;
    auto ret = ::waitpid(p.pid, &status, WNOHANG|WUNTRACED); 
    
    if (ret == -1)
    {
        if (errno != ECHILD) //because it no child is running, than this one isn't either, obviously.
            ec = ::boost::process::detail::get_last_error();
        return false;
    }
    else if (ret == 0)
        return true;
    else
    {
        ec.clear();
        
        if (WIFEXITED(status))
            exit_code = status;
        
        return false;
    }
}

inline bool is_running(int code)
{
    return !WIFEXITED(code);
}

inline int eval_exit_status(int code)
{
    return WEXITSTATUS(code);
}

}}}}

#endif