Kea  1.9.9-git
io_service_signal.cc
Go to the documentation of this file.
1 // Copyright (C) 2020-2021 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
10 #include <exceptions/exceptions.h>
11 
12 #include <boost/enable_shared_from_this.hpp>
13 #include <boost/noncopyable.hpp>
14 #include <boost/asio/signal_set.hpp>
15 #include <functional>
16 
17 namespace ph = std::placeholders;
18 
19 namespace isc {
20 namespace asiolink {
21 
23 class IOSignalSetImpl : public boost::enable_shared_from_this<IOSignalSetImpl>,
24  public boost::noncopyable {
25 public:
30  IOSignalSetImpl(IOServicePtr io_service, IOSignalHandler handler);
31 
33  ~IOSignalSetImpl() = default;
34 
36  void install();
37 
41  void add(int signum);
42 
46  void remove(int signum);
47 
48 private:
50  boost::asio::signal_set signal_set_;
51 
53  IOSignalHandler handler_;
54 
59  void callback(const boost::system::error_code& ec, int signum);
60 };
61 
63  IOSignalHandler handler)
64  : signal_set_(io_service->get_io_service()), handler_(handler) {
65 }
66 
67 void
68 IOSignalSetImpl::callback(const boost::system::error_code& ec, int signum) {
69  if (ec && ec.value() == boost::asio::error::operation_aborted) {
70  return;
71  }
72  install();
73  if (!ec && (signum > 0)) {
74  try {
75  handler_(signum);
76  } catch (const std::exception& ex) {
77  }
78  }
79 }
80 
81 void
83  signal_set_.async_wait(std::bind(&IOSignalSetImpl::callback,
84  shared_from_this(), ph::_1, ph::_2));
85 }
86 
87 void
88 IOSignalSetImpl::add(int signum) {
89  try {
90  signal_set_.add(signum);
91  } catch (const boost::system::system_error& ex) {
93  "Failed to add signal " << signum << ": " << ex.what());
94  }
95 }
96 
97 void
99  try {
100  signal_set_.remove(signum);
101  } catch (const boost::system::system_error& ex) {
103  "Failed to remove signal " << signum << ": " << ex.what());
104  }
105 }
106 
108  impl_(new IOSignalSetImpl(io_service, handler)) {
109  // It can throw but the error is fatal...
110  impl_->install();
111 }
112 
113 void
114 IOSignalSet::add(int signum) {
115  impl_->add(signum);
116 }
117 
118 void
119 IOSignalSet::remove(int signum) {
120  impl_->remove(signum);
121 }
122 
123 } // namespace asiolink
124 } // namespace isc
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown when an unexpected error condition occurs.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Defines the logger used by the top-level component of kea-dhcp-ddns.