signal.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. // Copyright (c) 2016 Klemens D. Morgenstern
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_PROCESS_POSIX_SIGNAL_HPP
  11. #define BOOST_PROCESS_POSIX_SIGNAL_HPP
  12. #include <boost/process/detail/posix/handler.hpp>
  13. #include <signal.h>
  14. namespace boost { namespace process { namespace detail { namespace posix {
  15. #if defined(__GLIBC__)
  16. using sighandler_t = ::sighandler_t;
  17. #else
  18. using sighandler_t = void(*)(int);
  19. #endif
  20. struct sig_init_ : handler_base_ext
  21. {
  22. sig_init_ (sighandler_t handler) : _handler(handler) {}
  23. template <class PosixExecutor>
  24. void on_exec_setup(PosixExecutor&)
  25. {
  26. _old = ::signal(SIGCHLD, _handler);
  27. }
  28. template <class Executor>
  29. void on_error(Executor&, const std::error_code &)
  30. {
  31. if (!_reset)
  32. {
  33. ::signal(SIGCHLD, _old);
  34. _reset = true;
  35. }
  36. }
  37. template <class Executor>
  38. void on_success(Executor&)
  39. {
  40. if (!_reset)
  41. {
  42. ::signal(SIGCHLD, _old);
  43. _reset = true;
  44. }
  45. }
  46. private:
  47. bool _reset = false;
  48. ::boost::process::detail::posix::sighandler_t _old{0};
  49. ::boost::process::detail::posix::sighandler_t _handler{0};
  50. };
  51. struct sig_
  52. {
  53. constexpr sig_() {}
  54. sig_init_ operator()(::boost::process::detail::posix::sighandler_t h) const {return h;}
  55. sig_init_ operator= (::boost::process::detail::posix::sighandler_t h) const {return h;}
  56. sig_init_ dfl() const {return SIG_DFL;}
  57. sig_init_ ign() const {return SIG_IGN;}
  58. };
  59. }}}}
  60. #endif