]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
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_GROUP_HPP_ | |
7 | #define BOOST_PROCESS_DETAIL_POSIX_GROUP_HPP_ | |
8 | ||
11fdf7f2 | 9 | #include <boost/process/detail/config.hpp> |
b32b8144 FG |
10 | #include <boost/process/detail/posix/child_handle.hpp> |
11 | #include <system_error> | |
12 | #include <unistd.h> | |
13 | ||
14 | namespace boost { namespace process { namespace detail { namespace posix { | |
15 | ||
b32b8144 FG |
16 | struct group_handle |
17 | { | |
18 | pid_t grp = -1; | |
19 | ||
20 | typedef pid_t handle_t; | |
21 | handle_t handle() const { return grp; } | |
22 | ||
23 | explicit group_handle(handle_t h) : | |
24 | grp(h) | |
25 | { | |
26 | } | |
27 | ||
b32b8144 FG |
28 | group_handle() = default; |
29 | ||
30 | ~group_handle() = default; | |
31 | group_handle(const group_handle & c) = delete; | |
32 | group_handle(group_handle && c) : grp(c.grp) | |
33 | { | |
34 | c.grp = -1; | |
35 | } | |
36 | group_handle &operator=(const group_handle & c) = delete; | |
37 | group_handle &operator=(group_handle && c) | |
38 | { | |
b32b8144 FG |
39 | grp = c.grp; |
40 | c.grp = -1; | |
41 | return *this; | |
42 | } | |
43 | ||
44 | void add(handle_t proc) | |
45 | { | |
46 | if (::setpgid(proc, grp)) | |
47 | throw_last_error(); | |
48 | } | |
49 | void add(handle_t proc, std::error_code & ec) noexcept | |
50 | { | |
51 | if (::setpgid(proc, grp)) | |
52 | ec = get_last_error(); | |
53 | } | |
54 | ||
55 | bool has(handle_t proc) | |
56 | { | |
57 | return ::getpgid(proc) == grp; | |
58 | } | |
92f5a8d4 | 59 | bool has(handle_t proc, std::error_code &) noexcept |
b32b8144 FG |
60 | { |
61 | return ::getpgid(proc) == grp; | |
b32b8144 FG |
62 | } |
63 | ||
64 | bool valid() const | |
65 | { | |
66 | return grp != -1; | |
67 | } | |
b32b8144 FG |
68 | }; |
69 | ||
b32b8144 FG |
70 | inline void terminate(group_handle &p, std::error_code &ec) noexcept |
71 | { | |
72 | if (::killpg(p.grp, SIGKILL) == -1) | |
73 | ec = boost::process::detail::get_last_error(); | |
74 | else | |
75 | ec.clear(); | |
76 | ||
77 | p.grp = -1; | |
78 | } | |
79 | ||
11fdf7f2 TL |
80 | inline void terminate(group_handle &p) |
81 | { | |
82 | std::error_code ec; | |
83 | terminate(p, ec); | |
84 | boost::process::detail::throw_error(ec, "killpg(2) failed in terminate"); | |
85 | } | |
b32b8144 FG |
86 | |
87 | inline bool in_group() | |
88 | { | |
89 | return true; | |
90 | } | |
91 | ||
b32b8144 FG |
92 | }}}} |
93 | ||
b32b8144 | 94 | #endif /* BOOST_PROCESS_DETAIL_WINDOWS_GROUP_HPP_ */ |