group_handle.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #ifndef BOOST_PROCESS_DETAIL_WINDOWS_GROUP_HPP_
  6. #define BOOST_PROCESS_DETAIL_WINDOWS_GROUP_HPP_
  7. #include <boost/process/detail/windows/handler.hpp>
  8. #include <boost/winapi/jobs.hpp>
  9. #include <boost/process/detail/windows/child_handle.hpp>
  10. #include <boost/process/detail/windows/job_workaround.hpp>
  11. #include <system_error>
  12. namespace boost { namespace process { namespace detail { namespace windows {
  13. inline bool break_away_enabled(::boost::winapi::HANDLE_ h)
  14. {
  15. workaround::JOBOBJECT_EXTENDED_LIMIT_INFORMATION_ info;
  16. if (!workaround::query_information_job_object(
  17. h,
  18. workaround::JobObjectExtendedLimitInformation_,
  19. static_cast<void*>(&info),
  20. sizeof(info),
  21. nullptr))
  22. throw_last_error("QueryInformationJobObject() failed");
  23. return (info.BasicLimitInformation.LimitFlags & workaround::JOB_OBJECT_LIMIT_BREAKAWAY_OK_) != 0;
  24. }
  25. inline void enable_break_away(::boost::winapi::HANDLE_ h)
  26. {
  27. workaround::JOBOBJECT_EXTENDED_LIMIT_INFORMATION_ info;
  28. if (!workaround::query_information_job_object(
  29. h,
  30. workaround::JobObjectExtendedLimitInformation_,
  31. static_cast<void*>(&info),
  32. sizeof(info),
  33. nullptr))
  34. throw_last_error("QueryInformationJobObject() failed");
  35. if ((info.BasicLimitInformation.LimitFlags & workaround::JOB_OBJECT_LIMIT_BREAKAWAY_OK_) != 0)
  36. return;
  37. info.BasicLimitInformation.LimitFlags |= workaround::JOB_OBJECT_LIMIT_BREAKAWAY_OK_;
  38. if (!workaround::set_information_job_object(
  39. h,
  40. workaround::JobObjectExtendedLimitInformation_,
  41. static_cast<void*>(&info),
  42. sizeof(info)))
  43. throw_last_error("SetInformationJobObject() failed");
  44. }
  45. inline void enable_break_away(::boost::winapi::HANDLE_ h, std::error_code & ec)
  46. {
  47. workaround::JOBOBJECT_EXTENDED_LIMIT_INFORMATION_ info;
  48. if (!workaround::query_information_job_object(
  49. h,
  50. workaround::JobObjectExtendedLimitInformation_,
  51. static_cast<void*>(&info),
  52. sizeof(info),
  53. nullptr))
  54. {
  55. ec = get_last_error();
  56. return;
  57. }
  58. if ((info.BasicLimitInformation.LimitFlags & workaround::JOB_OBJECT_LIMIT_BREAKAWAY_OK_) != 0)
  59. return;
  60. info.BasicLimitInformation.LimitFlags |= workaround::JOB_OBJECT_LIMIT_BREAKAWAY_OK_;
  61. if (!workaround::set_information_job_object(
  62. h,
  63. workaround::JobObjectExtendedLimitInformation_,
  64. static_cast<void*>(&info),
  65. sizeof(info)))
  66. {
  67. ec = get_last_error();
  68. return;
  69. }
  70. }
  71. inline void associate_completion_port(::boost::winapi::HANDLE_ job,
  72. ::boost::winapi::HANDLE_ io_port)
  73. {
  74. workaround::JOBOBJECT_ASSOCIATE_COMPLETION_PORT_ port;
  75. port.CompletionKey = job;
  76. port.CompletionPort = io_port;
  77. if (!workaround::set_information_job_object(
  78. job,
  79. workaround::JobObjectAssociateCompletionPortInformation_,
  80. static_cast<void*>(&port),
  81. sizeof(port)))
  82. throw_last_error("SetInformationJobObject() failed");
  83. }
  84. struct group_handle
  85. {
  86. ::boost::winapi::HANDLE_ _job_object;
  87. ::boost::winapi::HANDLE_ _io_port;
  88. typedef ::boost::winapi::HANDLE_ handle_t;
  89. handle_t handle() const { return _job_object; }
  90. explicit group_handle(handle_t h) :
  91. _job_object(h),
  92. _io_port(::CreateIoCompletionPort(::boost::winapi::INVALID_HANDLE_VALUE_, nullptr, 0, 1))
  93. {
  94. enable_break_away(_job_object);
  95. associate_completion_port(_job_object, _io_port);
  96. }
  97. group_handle() : group_handle(::boost::winapi::CreateJobObjectW(nullptr, nullptr))
  98. {
  99. }
  100. ~group_handle()
  101. {
  102. ::boost::winapi::CloseHandle(_job_object);
  103. ::boost::winapi::CloseHandle(_io_port);
  104. }
  105. group_handle(const group_handle & c) = delete;
  106. group_handle(group_handle && c) : _job_object(c._job_object),
  107. _io_port(c._io_port)
  108. {
  109. c._job_object = ::boost::winapi::invalid_handle_value;
  110. c._io_port = ::boost::winapi::invalid_handle_value;
  111. }
  112. group_handle &operator=(const group_handle & c) = delete;
  113. group_handle &operator=(group_handle && c)
  114. {
  115. ::boost::winapi::CloseHandle(_io_port);
  116. _io_port = c._io_port;
  117. c._io_port = ::boost::winapi::invalid_handle_value;
  118. ::boost::winapi::CloseHandle(_job_object);
  119. _job_object = c._job_object;
  120. c._job_object = ::boost::winapi::invalid_handle_value;
  121. return *this;
  122. }
  123. void add(handle_t proc)
  124. {
  125. if (!::boost::winapi::AssignProcessToJobObject(_job_object, proc))
  126. throw_last_error();
  127. }
  128. void add(handle_t proc, std::error_code & ec) noexcept
  129. {
  130. if (!::boost::winapi::AssignProcessToJobObject(_job_object, proc))
  131. ec = get_last_error();
  132. }
  133. bool has(handle_t proc)
  134. {
  135. ::boost::winapi::BOOL_ is;
  136. if (!::boost::winapi::IsProcessInJob(proc, _job_object, &is))
  137. throw_last_error();
  138. return is!=0;
  139. }
  140. bool has(handle_t proc, std::error_code & ec) noexcept
  141. {
  142. ::boost::winapi::BOOL_ is;
  143. if (!::boost::winapi::IsProcessInJob(proc, _job_object, &is))
  144. ec = get_last_error();
  145. return is!=0;
  146. }
  147. bool valid() const
  148. {
  149. return _job_object != nullptr;
  150. }
  151. };
  152. inline void terminate(const group_handle &p)
  153. {
  154. if (!::boost::winapi::TerminateJobObject(p.handle(), EXIT_FAILURE))
  155. boost::process::detail::throw_last_error("TerminateJobObject() failed");
  156. }
  157. inline void terminate(const group_handle &p, std::error_code &ec) noexcept
  158. {
  159. if (!::boost::winapi::TerminateJobObject(p.handle(), EXIT_FAILURE))
  160. ec = boost::process::detail::get_last_error();
  161. else
  162. ec.clear();
  163. }
  164. inline bool in_group()
  165. {
  166. ::boost::winapi::BOOL_ res;
  167. if (!::boost::winapi::IsProcessInJob(boost::winapi::GetCurrentProcess(), nullptr, &res))
  168. throw_last_error("IsProcessInJob failed");
  169. return res!=0;
  170. }
  171. }}}}
  172. #endif /* BOOST_PROCESS_DETAIL_WINDOWS_GROUP_HPP_ */