child_handle.hpp 3.1 KB

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