Kea: io_service_signal.cc Source File

| Kea 3.2.0-git

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
9#include <asiolink/io_service_signal.h>
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
17namespace ph = std::placeholders;
18
19namespace isc {
20namespace asiolink {
21
23 class IOSignalSetImpl : public boost::enable_shared_from_this<IOSignalSetImpl>,
24 public boost::noncopyable {
25public:
30 IOSignalSetImpl(const IOServicePtr& io_service, IOSignalHandler handler);
31
33 ~IOSignalSetImpl();
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
68IOSignalSetImpl::IOSignalSetImpl(const IOServicePtr& io_service,
69 IOSignalHandler handler)
70 : io_service_(io_service),
71 signal_set_(io_service_->getInternalIOService()),
72 handler_(handler) {
73}
74
75IOSignalSetImpl::~IOSignalSetImpl() {
76 handler_ = IOSignalHandler();
77}
78
79void
80IOSignalSetImpl::cancel() {
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
99IOSignalSetImpl::install() {
100 signal_set_.async_wait(std::bind(&IOSignalSetImpl::callback,
101 shared_from_this(),
102 ph::_1, ph::_2));
103}
104
105void
106IOSignalSetImpl::add(int signum) {
107 try {
108 signal_set_.add(signum);
109 } catch (const boost::system::system_error& ex) {
110 isc_throw(isc::Unexpected,
111 "Failed to add signal " << signum << ": " << ex.what());
112 }
113}
114
115void
116IOSignalSetImpl::remove(int signum) {
117 try {
118 signal_set_.remove(signum);
119 } catch (const boost::system::system_error& ex) {
120 isc_throw(isc::Unexpected,
121 "Failed to remove signal " << signum << ": " << ex.what());
122 }
123}
124
125IOSignalSet::IOSignalSet(const IOServicePtr& io_service, IOSignalHandler handler) :
126 impl_(new IOSignalSetImpl(io_service, handler)) {
127 // It can throw but the error is fatal...
128 impl_->install();
129}
130
131IOSignalSet::~IOSignalSet() {
132 impl_->cancel();
133}
134
135void
136IOSignalSet::add(int signum) {
137 impl_->add(signum);
138}
139
140void
141IOSignalSet::remove(int signum) {
142 impl_->remove(signum);
143}
144
145} // namespace asiolink
146} // namespace isc
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::Unexpected
A generic exception that is thrown when an unexpected error condition occurs.
Definition exceptions/exceptions.h:153
isc::asiolink::IOSignalSetImpl
Implementation class of IOSignalSet.
Definition io_service_signal.cc:24
isc::asiolink::IOSignalSetImpl::install
void install()
Install the callback on the IO service queue.
Definition io_service_signal.cc:99
isc::asiolink::IOSignalSetImpl::add
void add(int signum)
Add a signal to the ASIO signal set.
Definition io_service_signal.cc:106
isc::asiolink::IOSignalSetImpl::remove
void remove(int signum)
Remove a signal from the ASIO signal set.
Definition io_service_signal.cc:116
isc::asiolink::IOSignalSetImpl::IOSignalSetImpl
IOSignalSetImpl(const IOServicePtr &io_service, IOSignalHandler handler)
Constructor.
Definition io_service_signal.cc:68
isc::asiolink::IOSignalSetImpl::cancel
void cancel()
Cancel the remaining installed signal handler callbacks.
Definition io_service_signal.cc:80
isc::asiolink::IOSignalSetImpl::~IOSignalSetImpl
~IOSignalSetImpl()
Destructor.
Definition io_service_signal.cc:75
isc::asiolink::IOSignalSet::~IOSignalSet
~IOSignalSet()
Destructor.
Definition io_service_signal.cc:131
isc::asiolink::IOSignalSet::add
void add(int signum)
Add a signal to the list of signals to handle.
Definition io_service_signal.cc:136
isc::asiolink::IOSignalSet::remove
void remove(int signum)
Remove a signal from the list of signals to handle.
Definition io_service_signal.cc:141
isc::asiolink::IOSignalSet::IOSignalSet
IOSignalSet(const asiolink::IOServicePtr &io_service, IOSignalHandler handler)
Constructor.
Definition io_service_signal.cc:125
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
io_service_signal.h
isc::asiolink
Definition addr_utilities.cc:170
isc::asiolink::IOServicePtr
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
Definition io_service.h:35
isc::asiolink::IOSignalHandler
std::function< void(int signum)> IOSignalHandler
Defines a handler function for an IOSignal.
Definition io_service_signal.h:18
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18