group_handle.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_POSIX_GROUP_HPP_
  6. #define BOOST_PROCESS_DETAIL_POSIX_GROUP_HPP_
  7. #include <boost/process/detail/config.hpp>
  8. #include <boost/process/detail/posix/child_handle.hpp>
  9. #include <system_error>
  10. #include <unistd.h>
  11. namespace boost { namespace process { namespace detail { namespace posix {
  12. struct group_handle
  13. {
  14. pid_t grp = -1;
  15. typedef pid_t handle_t;
  16. handle_t handle() const { return grp; }
  17. explicit group_handle(handle_t h) :
  18. grp(h)
  19. {
  20. }
  21. group_handle() = default;
  22. ~group_handle() = default;
  23. group_handle(const group_handle & c) = delete;
  24. group_handle(group_handle && c) : grp(c.grp)
  25. {
  26. c.grp = -1;
  27. }
  28. group_handle &operator=(const group_handle & c) = delete;
  29. group_handle &operator=(group_handle && c)
  30. {
  31. grp = c.grp;
  32. c.grp = -1;
  33. return *this;
  34. }
  35. void add(handle_t proc)
  36. {
  37. if (::setpgid(proc, grp))
  38. throw_last_error();
  39. }
  40. void add(handle_t proc, std::error_code & ec) noexcept
  41. {
  42. if (::setpgid(proc, grp))
  43. ec = get_last_error();
  44. }
  45. bool has(handle_t proc)
  46. {
  47. return ::getpgid(proc) == grp;
  48. }
  49. bool has(handle_t proc, std::error_code &) noexcept
  50. {
  51. return ::getpgid(proc) == grp;
  52. }
  53. bool valid() const
  54. {
  55. return grp != -1;
  56. }
  57. };
  58. inline void terminate(group_handle &p, std::error_code &ec) noexcept
  59. {
  60. if (::killpg(p.grp, SIGKILL) == -1)
  61. ec = boost::process::detail::get_last_error();
  62. else
  63. ec.clear();
  64. p.grp = -1;
  65. }
  66. inline void terminate(group_handle &p)
  67. {
  68. std::error_code ec;
  69. terminate(p, ec);
  70. boost::process::detail::throw_error(ec, "killpg(2) failed in terminate");
  71. }
  72. inline bool in_group()
  73. {
  74. return true;
  75. }
  76. }}}}
  77. #endif /* BOOST_PROCESS_DETAIL_WINDOWS_GROUP_HPP_ */