Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / process / detail / windows / child_handle.hpp
1 // Copyright (c) 2006, 2007 Julio M. Merino Vidal
2 // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
3 // Copyright (c) 2009 Boris Schaeling
4 // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
5 // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9
10 #ifndef BOOST_PROCESS_WINDOWS_CHILD_HPP
11 #define BOOST_PROCESS_WINDOWS_CHILD_HPP
12
13 #include <boost/move/move.hpp>
14 #include <boost/detail/winapi/handles.hpp>
15 #include <boost/detail/winapi/process.hpp>
16 #include <boost/detail/winapi/jobs.hpp>
17
18 namespace boost { namespace process { namespace detail { namespace windows {
19
20 typedef int pid_t;
21
22 struct child_handle
23 {
24     ::boost::detail::winapi::PROCESS_INFORMATION_ proc_info{nullptr, nullptr, 0,0};
25
26     explicit child_handle(const ::boost::detail::winapi::PROCESS_INFORMATION_ &pi) :
27                                   proc_info(pi)
28     {}
29
30     explicit child_handle(pid_t pid) :
31                                   proc_info{nullptr, nullptr, 0,0}
32     {
33         auto h = ::boost::detail::winapi::OpenProcess(
34                 ::boost::detail::winapi::PROCESS_ALL_ACCESS_,
35                 static_cast<::boost::detail::winapi::BOOL_>(0),
36                  pid);
37
38         if (h == nullptr)
39             throw_last_error("OpenProcess() failed");
40         proc_info.hProcess = h;
41         proc_info.dwProcessId = pid;
42     }
43
44     child_handle() = default;
45     ~child_handle()
46     {
47         ::boost::detail::winapi::CloseHandle(proc_info.hProcess);
48         ::boost::detail::winapi::CloseHandle(proc_info.hThread);
49     }
50     child_handle(const child_handle & c) = delete;
51     child_handle(child_handle && c) : proc_info(c.proc_info)
52     {
53         c.proc_info.hProcess = ::boost::detail::winapi::invalid_handle_value;
54         c.proc_info.hThread  = ::boost::detail::winapi::invalid_handle_value;
55     }
56     child_handle &operator=(const child_handle & c) = delete;
57     child_handle &operator=(child_handle && c)
58     {
59         ::boost::detail::winapi::CloseHandle(proc_info.hProcess);
60         ::boost::detail::winapi::CloseHandle(proc_info.hThread);
61         proc_info = c.proc_info;
62         c.proc_info.hProcess = ::boost::detail::winapi::invalid_handle_value;
63         c.proc_info.hThread  = ::boost::detail::winapi::invalid_handle_value;
64         return *this;
65     }
66
67     pid_t id() const
68     {
69         return static_cast<int>(proc_info.dwProcessId);
70     }
71
72     typedef ::boost::detail::winapi::HANDLE_ process_handle_t;
73     process_handle_t process_handle() const { return proc_info.hProcess; }
74
75     bool valid() const
76     {
77         return (proc_info.hProcess != nullptr) &&
78                (proc_info.hProcess != ::boost::detail::winapi::INVALID_HANDLE_VALUE_);
79     }
80     bool in_group() const
81     {
82         ::boost::detail::winapi::BOOL_ value;
83         if (!::boost::detail::winapi::IsProcessInJob(proc_info.hProcess, nullptr, &value))
84             throw_last_error("IsProcessinJob Failed");
85         return value!=0;
86     }
87     bool in_group(std::error_code &ec) const noexcept
88     {
89         ::boost::detail::winapi::BOOL_ value;
90         if (!::boost::detail::winapi::IsProcessInJob(proc_info.hProcess, nullptr, &value))
91             ec = get_last_error();
92         return value!=0;
93     }
94 };
95
96 }}}}
97
98 #endif