Kea 2.7.6
io_service_signal.cc
Go to the documentation of this file.
1// Copyright (C) 2020-2024 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(const IOServicePtr& io_service, IOSignalHandler handler);
31
34
36 void install();
37
41 void add(int signum);
42
46 void remove(int signum);
47
49 void cancel();
50
51private:
53 IOServicePtr io_service_;
54
56 boost::asio::signal_set signal_set_;
57
59 IOSignalHandler handler_;
60
65 void callback(const boost::system::error_code& ec, int signum);
66};
67
69 IOSignalHandler handler)
70 : io_service_(io_service),
71 signal_set_(io_service_->getInternalIOService()),
72 handler_(handler) {
73}
74
78
79void
81 signal_set_.cancel();
82}
83
84void
85IOSignalSetImpl::callback(const boost::system::error_code& ec, int signum) {
86 if (ec && ec.value() == boost::asio::error::operation_aborted) {
87 return;
88 }
89 install();
90 if (!ec && (signum > 0)) {
91 try {
92 handler_(signum);
93 } catch (const std::exception& ex) {
94 }
95 }
96}
97
98void
100 signal_set_.async_wait(std::bind(&IOSignalSetImpl::callback,
101 shared_from_this(),
102 ph::_1, ph::_2));
103}
104
105void
107 try {
108 signal_set_.add(signum);
109 } catch (const boost::system::system_error& ex) {
111 "Failed to add signal " << signum << ": " << ex.what());
112 }
113}
114
115void
117 try {
118 signal_set_.remove(signum);
119 } catch (const boost::system::system_error& ex) {
121 "Failed to remove signal " << signum << ": " << ex.what());
122 }
123}
124
126 impl_(new IOSignalSetImpl(io_service, handler)) {
127 // It can throw but the error is fatal...
128 impl_->install();
129}
130
132 impl_->cancel();
133}
134
135void
136IOSignalSet::add(int signum) {
137 impl_->add(signum);
138}
139
140void
142 impl_->remove(signum);
143}
144
145} // namespace asiolink
146} // 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.