child_handle.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef BOOST_PROCESS_WINDOWS_CHILD_HPP
  10. #define BOOST_PROCESS_WINDOWS_CHILD_HPP
  11. #include <boost/process/v1/detail/config.hpp>
  12. #include <boost/move/move.hpp>
  13. #include <boost/winapi/handles.hpp>
  14. #include <boost/winapi/process.hpp>
  15. #include <boost/winapi/jobs.hpp>
  16. namespace boost { namespace process { BOOST_PROCESS_V1_INLINE namespace v1 { namespace detail { namespace windows {
  17. typedef ::boost::winapi::DWORD_ pid_t;
  18. struct child_handle
  19. {
  20. ::boost::winapi::PROCESS_INFORMATION_ proc_info{nullptr, nullptr, 0,0};
  21. explicit child_handle(const ::boost::winapi::PROCESS_INFORMATION_ &pi) :
  22. proc_info(pi)
  23. {}
  24. explicit child_handle(pid_t pid) :
  25. proc_info{nullptr, nullptr, 0,0}
  26. {
  27. auto h = ::boost::winapi::OpenProcess(
  28. ::boost::winapi::PROCESS_ALL_ACCESS_,
  29. static_cast<::boost::winapi::BOOL_>(0),
  30. pid);
  31. if (h == nullptr)
  32. throw_last_error("OpenProcess() failed");
  33. proc_info.hProcess = h;
  34. proc_info.dwProcessId = pid;
  35. }
  36. child_handle() = default;
  37. ~child_handle()
  38. {
  39. ::boost::winapi::CloseHandle(proc_info.hProcess);
  40. ::boost::winapi::CloseHandle(proc_info.hThread);
  41. }
  42. child_handle(const child_handle & c) = delete;
  43. child_handle(child_handle && c) : proc_info(c.proc_info)
  44. {
  45. c.proc_info.hProcess = ::boost::winapi::invalid_handle_value;
  46. c.proc_info.hThread = ::boost::winapi::invalid_handle_value;
  47. }
  48. child_handle &operator=(const child_handle & c) = delete;
  49. child_handle &operator=(child_handle && c)
  50. {
  51. ::boost::winapi::CloseHandle(proc_info.hProcess);
  52. ::boost::winapi::CloseHandle(proc_info.hThread);
  53. proc_info = c.proc_info;
  54. c.proc_info.hProcess = ::boost::winapi::invalid_handle_value;
  55. c.proc_info.hThread = ::boost::winapi::invalid_handle_value;
  56. return *this;
  57. }
  58. pid_t id() const
  59. {
  60. return static_cast<pid_t>(proc_info.dwProcessId);
  61. }
  62. typedef ::boost::winapi::HANDLE_ process_handle_t;
  63. process_handle_t process_handle() const { return proc_info.hProcess; }
  64. bool valid() const
  65. {
  66. return (proc_info.hProcess != nullptr) &&
  67. (proc_info.hProcess != ::boost::winapi::INVALID_HANDLE_VALUE_);
  68. }
  69. bool in_group() const
  70. {
  71. ::boost::winapi::BOOL_ value;
  72. if (!::boost::winapi::IsProcessInJob(proc_info.hProcess, nullptr, &value))
  73. throw_last_error("IsProcessInJob Failed");
  74. return value!=0;
  75. }
  76. bool in_group(std::error_code &ec) const noexcept
  77. {
  78. ::boost::winapi::BOOL_ value;
  79. if (!::boost::winapi::IsProcessInJob(proc_info.hProcess, nullptr, &value))
  80. ec = get_last_error();
  81. return value!=0;
  82. }
  83. };
  84. }}}}}
  85. #endif