Kea 2.2.0
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
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
17namespace ph = std::placeholders;
18
19namespace isc {
20namespace asiolink {
21
23class IOSignalSetImpl : public boost::enable_shared_from_this<IOSignalSetImpl>,
24 public boost::noncopyable {
25public:
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
48private:
50 IOServicePtr io_service_;
51
53 boost::asio::signal_set signal_set_;
54
56 IOSignalHandler handler_;
57
62 void callback(const boost::system::error_code& ec, int signum);
63};
64
66 IOSignalHandler handler)
67 : io_service_(io_service),
68 signal_set_(io_service_->get_io_service()),
69 handler_(handler) {
70}
71
72void
73IOSignalSetImpl::callback(const boost::system::error_code& ec, int signum) {
74 if (ec && ec.value() == boost::asio::error::operation_aborted) {
75 return;
76 }
77 install();
78 if (!ec && (signum > 0)) {
79 try {
80 handler_(signum);
81 } catch (const std::exception& ex) {
82 }
83 }
84}
85
86void
88 signal_set_.async_wait(std::bind(&IOSignalSetImpl::callback,
89 shared_from_this(), ph::_1, ph::_2));
90}
91
92void
94 try {
95 signal_set_.add(signum);
96 } catch (const boost::system::system_error& ex) {
98 "Failed to add signal " << signum << ": " << ex.what());
99 }
100}
101
102void
104 try {
105 signal_set_.remove(signum);
106 } catch (const boost::system::system_error& ex) {
108 "Failed to remove signal " << signum << ": " << ex.what());
109 }
110}
111
113 impl_(new IOSignalSetImpl(io_service, handler)) {
114 // It can throw but the error is fatal...
115 impl_->install();
116}
117
118void
119IOSignalSet::add(int signum) {
120 impl_->add(signum);
121}
122
123void
125 impl_->remove(signum);
126}
127
128} // namespace asiolink
129} // namespace isc
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown when an unexpected error condition occurs.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Defines the logger used by the top-level component of kea-lfc.