1 // Copyright (c) 2019 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_WINDOWS_HANDLES_HPP_
7 #define BOOST_PROCESS_DETAIL_WINDOWS_HANDLES_HPP_
10 #include <system_error>
11 #include <boost/process/detail/windows/handle_workaround.hpp>
12 #include <boost/process/detail/windows/handler.hpp>
13 #include <boost/winapi/get_current_process_id.hpp>
15 namespace boost { namespace process { namespace detail {
18 template<typename Executor, typename Function>
19 void foreach_used_handle(Executor &exec, Function &&func);
25 using native_handle_type = ::boost::winapi::HANDLE_ ;
27 inline std::vector<native_handle_type> get_handles(std::error_code & ec)
29 auto pid = ::boost::winapi::GetCurrentProcessId();
31 std::vector<char> buffer(2048);
32 constexpr static auto STATUS_INFO_LENGTH_MISMATCH_ = static_cast<::boost::winapi::NTSTATUS_>(0xC0000004l);
33 auto info_pointer = reinterpret_cast<workaround::SYSTEM_HANDLE_INFORMATION_*>(buffer.data());
35 ::boost::winapi::NTSTATUS_ nt_status = STATUS_INFO_LENGTH_MISMATCH_;
38 nt_status == STATUS_INFO_LENGTH_MISMATCH_;
39 nt_status = workaround::nt_system_query_information(
40 workaround::SystemHandleInformation_,
41 info_pointer, buffer.size(),
44 buffer.resize(buffer.size() * 2);
45 info_pointer = reinterpret_cast<workaround::SYSTEM_HANDLE_INFORMATION_*>(buffer.data());
49 if (nt_status < 0 || nt_status > 0x7FFFFFFF)
51 ec = ::boost::process::detail::get_last_error();
57 std::vector<native_handle_type> res;
58 for (auto itr = info_pointer->Handle; itr != (info_pointer->Handle + info_pointer->Count); itr++)
60 if (itr->OwnerPid == pid)
61 res.push_back(reinterpret_cast<native_handle_type>(static_cast<std::uintptr_t>(itr->HandleValue)));
67 inline std::vector<native_handle_type> get_handles()
71 auto res = get_handles(ec);
73 boost::process::detail::throw_error(ec, "NtQuerySystemInformation failed");
79 inline bool is_stream_handle(native_handle_type handle, std::error_code & ec)
81 ::boost::winapi::ULONG_ actual_size;
82 auto nt_status = workaround::nt_query_object(
84 workaround::ObjectTypeInformation,
88 std::vector<char> vec;
89 vec.resize(actual_size);
91 workaround::OBJECT_TYPE_INFORMATION_ * type_info_p = reinterpret_cast<workaround::OBJECT_TYPE_INFORMATION_*>(vec.data());
92 nt_status = workaround::nt_query_object(
94 workaround::ObjectTypeInformation,
96 actual_size, &actual_size);
98 if (nt_status < 0 || nt_status > 0x7FFFFFFF)
100 ec = ::boost::process::detail::get_last_error();
106 auto &nm = type_info_p->TypeName.Buffer;
107 return type_info_p->TypeName.Length >= 5 &&
116 inline bool is_stream_handle(native_handle_type handle)
119 auto res = is_stream_handle(handle, ec);
121 boost::process::detail::throw_error(ec, "NtQueryObject failed");
127 struct limit_handles_ : handler_base_ext
129 mutable std::vector<::boost::winapi::HANDLE_> handles_with_inherit_flag;
131 template<typename Executor>
132 void on_setup(Executor & exec) const
134 auto all_handles = get_handles();
135 foreach_used_handle(exec,
136 [&](::boost::winapi::HANDLE_ handle)
138 auto itr = std::find(all_handles.begin(), all_handles .end(), handle);
140 if (itr != all_handles.end())
141 *itr = ::boost::winapi::INVALID_HANDLE_VALUE_;
142 else if ((::boost::winapi::GetHandleInformation(*itr, &flags) != 0)
143 &&((flags & ::boost::winapi::HANDLE_FLAG_INHERIT_) == 0)) //it is NOT inherited anyhow, so ignore too
144 *itr = ::boost::winapi::INVALID_HANDLE_VALUE_;
147 auto part_itr = std::partition(all_handles.begin(), all_handles.end(),
148 [](::boost::winapi::HANDLE_ handle) {return handle != ::boost::winapi::INVALID_HANDLE_VALUE_;});
150 all_handles.erase(part_itr, all_handles.end()); //remove invalid handles
151 handles_with_inherit_flag = std::move(all_handles);
153 for (auto handle : handles_with_inherit_flag)
154 ::boost::winapi::SetHandleInformation(handle, ::boost::winapi::HANDLE_FLAG_INHERIT_, 0);
157 template<typename Executor>
158 void on_error(Executor & exec, const std::error_code & ec) const
160 for (auto handle : handles_with_inherit_flag)
161 ::boost::winapi::SetHandleInformation(handle, ::boost::winapi::HANDLE_FLAG_INHERIT_, ::boost::winapi::HANDLE_FLAG_INHERIT_);
164 template<typename Executor>
165 void on_sucess(Executor & exec) const
167 for (auto handle : handles_with_inherit_flag)
168 ::boost::winapi::SetHandleInformation(handle, ::boost::winapi::HANDLE_FLAG_INHERIT_, ::boost::winapi::HANDLE_FLAG_INHERIT_);
176 #endif //PROCESS_HANDLES_HPP