| 1 | // Copyright (c) 2009 Dmitry Goncharov
|
|---|
| 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 |
|
|---|
| 6 | #ifndef BOOST_ASIO_POSIX_SIGNALFD_HPP
|
|---|
| 7 | #define BOOST_ASIO_POSIX_SIGNALFD_HPP
|
|---|
| 8 |
|
|---|
| 9 | #if (!defined(BOOST_WINDOWS) && !defined(__CYGWIN__)) \
|
|---|
| 10 | || defined(GENERATING_DOCUMENTATION)
|
|---|
| 11 |
|
|---|
| 12 | #include <signal.h>
|
|---|
| 13 | #include <unistd.h>
|
|---|
| 14 |
|
|---|
| 15 | #include <cerrno>
|
|---|
| 16 | #include <boost/system/system_error.hpp>
|
|---|
| 17 | #include <boost/throw_exception.hpp>
|
|---|
| 18 | #include <boost/asio/error.hpp>
|
|---|
| 19 |
|
|---|
| 20 | namespace boost { namespace asio { namespace posix {
|
|---|
| 21 |
|
|---|
| 22 | template <int Signo>
|
|---|
| 23 | class signalfd
|
|---|
| 24 | {
|
|---|
| 25 | public:
|
|---|
| 26 | typedef char buf_type;
|
|---|
| 27 |
|
|---|
| 28 | signalfd()
|
|---|
| 29 | {
|
|---|
| 30 | int s = pipe(m_pipe);
|
|---|
| 31 | if (s < 0)
|
|---|
| 32 | {
|
|---|
| 33 | boost::system::error_code const ec(errno, boost::asio::error::get_system_category());
|
|---|
| 34 | boost::system::system_error const e(ec, "pipe");
|
|---|
| 35 | boost::throw_exception(e);
|
|---|
| 36 | }
|
|---|
| 37 | struct sigaction act;
|
|---|
| 38 | std::memset(&act, 0, sizeof act);
|
|---|
| 39 | std::memset(&m_oldact, 0, sizeof m_oldact);
|
|---|
| 40 | act.sa_handler = signalfd<Signo>::on_signal;
|
|---|
| 41 |
|
|---|
| 42 | s = sigaction(Signo, &act, &m_oldact);
|
|---|
| 43 | if (s < 0)
|
|---|
| 44 | {
|
|---|
| 45 | boost::system::error_code const ec(errno, boost::asio::error::get_system_category());
|
|---|
| 46 | boost::system::system_error const e(ec, "sigaction");
|
|---|
| 47 | boost::throw_exception(e);
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | ~signalfd()
|
|---|
| 52 | {
|
|---|
| 53 | sigaction(Signo, &m_oldact, 0);
|
|---|
| 54 | close(m_pipe[0]);
|
|---|
| 55 | close(m_pipe[1]);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | int fd() const
|
|---|
| 59 | {
|
|---|
| 60 | return m_pipe[0];
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | private:
|
|---|
| 64 | signalfd(signalfd const&);
|
|---|
| 65 | signalfd const& operator=(signalfd const&);
|
|---|
| 66 |
|
|---|
| 67 | private:
|
|---|
| 68 | static void on_signal(int signo)
|
|---|
| 69 | {
|
|---|
| 70 | try
|
|---|
| 71 | {
|
|---|
| 72 | buf_type const c = 0;
|
|---|
| 73 | // The return value is ignored, because
|
|---|
| 74 | // if write() fails there is, probably, nothing useful that can be done.
|
|---|
| 75 | while (write(m_pipe[1], &c, sizeof c) < 0 && EINTR == errno);
|
|---|
| 76 | }
|
|---|
| 77 | catch (...) {}
|
|---|
| 78 | }
|
|---|
| 79 | static int m_pipe[2];
|
|---|
| 80 |
|
|---|
| 81 | private:
|
|---|
| 82 | struct sigaction m_oldact;
|
|---|
| 83 | };
|
|---|
| 84 |
|
|---|
| 85 | template<int Signo>
|
|---|
| 86 | int signalfd<Signo>::m_pipe[2];
|
|---|
| 87 |
|
|---|
| 88 | }}}
|
|---|
| 89 |
|
|---|
| 90 | #endif // (!defined(BOOST_WINDOWS) && !defined(__CYGWIN__)) \
|
|---|
| 91 | // || defined(GENERATING_DOCUMENTATION)
|
|---|
| 92 | #endif // BOOST_ASIO_POSIX_SIGNALFD_HPP
|
|---|
| 93 |
|
|---|