handles.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright (c) 2019 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. #ifndef BOOST_PROCESS_DETAIL_WINDOWS_HANDLES_HPP_
  6. #define BOOST_PROCESS_DETAIL_WINDOWS_HANDLES_HPP_
  7. #include <vector>
  8. #include <system_error>
  9. #include <boost/process/detail/windows/handle_workaround.hpp>
  10. #include <boost/process/detail/windows/handler.hpp>
  11. #include <boost/winapi/get_current_process_id.hpp>
  12. namespace boost { namespace process { namespace detail {
  13. template<typename Executor, typename Function>
  14. void foreach_used_handle(Executor &exec, Function &&func);
  15. namespace windows {
  16. using native_handle_type = ::boost::winapi::HANDLE_ ;
  17. inline std::vector<native_handle_type> get_handles(std::error_code & ec)
  18. {
  19. auto pid = ::boost::winapi::GetCurrentProcessId();
  20. std::vector<char> buffer(2048);
  21. constexpr static auto STATUS_INFO_LENGTH_MISMATCH_ = static_cast<::boost::winapi::NTSTATUS_>(0xC0000004l);
  22. auto info_pointer = reinterpret_cast<workaround::SYSTEM_HANDLE_INFORMATION_*>(buffer.data());
  23. ::boost::winapi::NTSTATUS_ nt_status = STATUS_INFO_LENGTH_MISMATCH_;
  24. for (;
  25. nt_status == STATUS_INFO_LENGTH_MISMATCH_;
  26. nt_status = workaround::nt_system_query_information(
  27. workaround::SystemHandleInformation_,
  28. info_pointer, static_cast<::boost::winapi::ULONG_>(buffer.size()),
  29. nullptr))
  30. {
  31. buffer.resize(buffer.size() * 2);
  32. info_pointer = reinterpret_cast<workaround::SYSTEM_HANDLE_INFORMATION_*>(buffer.data());
  33. }
  34. if (nt_status < 0 || nt_status > 0x7FFFFFFF)
  35. {
  36. ec = ::boost::process::detail::get_last_error();
  37. return {};
  38. }
  39. else
  40. ec.clear();
  41. std::vector<native_handle_type> res;
  42. for (auto itr = info_pointer->Handle; itr != (info_pointer->Handle + info_pointer->Count); itr++)
  43. {
  44. if (itr->OwnerPid == pid)
  45. res.push_back(reinterpret_cast<native_handle_type>(static_cast<std::uintptr_t>(itr->HandleValue)));
  46. }
  47. return res;
  48. }
  49. inline std::vector<native_handle_type> get_handles()
  50. {
  51. std::error_code ec;
  52. auto res = get_handles(ec);
  53. if (ec)
  54. boost::process::detail::throw_error(ec, "NtQuerySystemInformation failed");
  55. return res;
  56. }
  57. inline bool is_stream_handle(native_handle_type handle, std::error_code & ec)
  58. {
  59. ::boost::winapi::ULONG_ actual_size;
  60. auto nt_status = workaround::nt_query_object(
  61. handle,
  62. workaround::ObjectTypeInformation,
  63. NULL,
  64. 0, &actual_size);
  65. std::vector<char> vec;
  66. vec.resize(actual_size);
  67. workaround::OBJECT_TYPE_INFORMATION_ * type_info_p = reinterpret_cast<workaround::OBJECT_TYPE_INFORMATION_*>(vec.data());
  68. nt_status = workaround::nt_query_object(
  69. handle,
  70. workaround::ObjectTypeInformation,
  71. type_info_p,
  72. actual_size, &actual_size);
  73. if (nt_status < 0 || nt_status > 0x7FFFFFFF)
  74. {
  75. ec = ::boost::process::detail::get_last_error();
  76. return false;
  77. }
  78. else
  79. ec.clear();
  80. auto &nm = type_info_p->TypeName.Buffer;
  81. return type_info_p->TypeName.Length >= 5 &&
  82. nm[0] == L'F' &&
  83. nm[1] == L'i' &&
  84. nm[2] == L'l' &&
  85. nm[3] == L'e' &&
  86. nm[4] == L'\0';
  87. }
  88. inline bool is_stream_handle(native_handle_type handle)
  89. {
  90. std::error_code ec;
  91. auto res = is_stream_handle(handle, ec);
  92. if (ec)
  93. boost::process::detail::throw_error(ec, "NtQueryObject failed");
  94. return res;
  95. }
  96. struct limit_handles_ : handler_base_ext
  97. {
  98. mutable std::vector<::boost::winapi::HANDLE_> handles_with_inherit_flag;
  99. template<typename Executor>
  100. void on_setup(Executor & exec) const
  101. {
  102. auto all_handles = get_handles();
  103. foreach_used_handle(exec,
  104. [&](::boost::winapi::HANDLE_ handle)
  105. {
  106. auto itr = std::find(all_handles.begin(), all_handles .end(), handle);
  107. DWORD flags = 0u;
  108. if (itr != all_handles.end())
  109. *itr = ::boost::winapi::INVALID_HANDLE_VALUE_;
  110. else if ((::boost::winapi::GetHandleInformation(*itr, &flags) != 0)
  111. &&((flags & ::boost::winapi::HANDLE_FLAG_INHERIT_) == 0)) //it is NOT inherited anyhow, so ignore too
  112. *itr = ::boost::winapi::INVALID_HANDLE_VALUE_;
  113. });
  114. auto part_itr = std::partition(all_handles.begin(), all_handles.end(),
  115. [](::boost::winapi::HANDLE_ handle) {return handle != ::boost::winapi::INVALID_HANDLE_VALUE_;});
  116. all_handles.erase(part_itr, all_handles.end()); //remove invalid handles
  117. handles_with_inherit_flag = std::move(all_handles);
  118. for (auto handle : handles_with_inherit_flag)
  119. ::boost::winapi::SetHandleInformation(handle, ::boost::winapi::HANDLE_FLAG_INHERIT_, 0);
  120. }
  121. template<typename Executor>
  122. void on_error(Executor & exec, const std::error_code & ec) const
  123. {
  124. for (auto handle : handles_with_inherit_flag)
  125. ::boost::winapi::SetHandleInformation(handle, ::boost::winapi::HANDLE_FLAG_INHERIT_, ::boost::winapi::HANDLE_FLAG_INHERIT_);
  126. }
  127. template<typename Executor>
  128. void on_sucess(Executor & exec) const
  129. {
  130. for (auto handle : handles_with_inherit_flag)
  131. ::boost::winapi::SetHandleInformation(handle, ::boost::winapi::HANDLE_FLAG_INHERIT_, ::boost::winapi::HANDLE_FLAG_INHERIT_);
  132. }
  133. };
  134. }}}}
  135. #endif //PROCESS_HANDLES_HPP