Kea: io_service_signal.cc Source File

| 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
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(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
65IOSignalSetImpl::IOSignalSetImpl(IOServicePtr io_service,
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
87IOSignalSetImpl::install() {
88 signal_set_.async_wait(std::bind(&IOSignalSetImpl::callback,
89 shared_from_this(), ph::_1, ph::_2));
90}
91
92void
93IOSignalSetImpl::add(int signum) {
94 try {
95 signal_set_.add(signum);
96 } catch (const boost::system::system_error& ex) {
97 isc_throw(isc::Unexpected,
98 "Failed to add signal " << signum << ": " << ex.what());
99 }
100}
101
102void
103IOSignalSetImpl::remove(int signum) {
104 try {
105 signal_set_.remove(signum);
106 } catch (const boost::system::system_error& ex) {
107 isc_throw(isc::Unexpected,
108 "Failed to remove signal " << signum << ": " << ex.what());
109 }
110}
111
112IOSignalSet::IOSignalSet(IOServicePtr io_service, IOSignalHandler handler) :
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
124IOSignalSet::remove(int signum) {
125 impl_->remove(signum);
126}
127
128} // namespace asiolink
129} // 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:87
isc::asiolink::IOSignalSetImpl::add
void add(int signum)
Add a signal to the ASIO signal set.
Definition: io_service_signal.cc:93
isc::asiolink::IOSignalSetImpl::IOSignalSetImpl
IOSignalSetImpl(IOServicePtr io_service, IOSignalHandler handler)
Constructor.
Definition: io_service_signal.cc:65
isc::asiolink::IOSignalSetImpl::~IOSignalSetImpl
~IOSignalSetImpl()=default
Destructor.
isc::asiolink::IOSignalSetImpl::remove
void remove(int signum)
Remove a signal from the ASIO signal set.
Definition: io_service_signal.cc:103
isc::asiolink::IOSignalSet::add
void add(int signum)
Add a signal to the list of signals to handle.
Definition: io_service_signal.cc:119
isc::asiolink::IOSignalSet::remove
void remove(int signum)
Remove a signal from the list of signals to handle.
Definition: io_service_signal.cc:124
isc::asiolink::IOSignalSet::IOSignalSet
IOSignalSet(asiolink::IOServicePtr io_service, IOSignalHandler handler)
Constructor.
Definition: io_service_signal.cc:112
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
asiolink
A wrapper interface for the ASIO library.
isc::asiolink::IOServicePtr
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
Definition: io_service.h:110
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: agent_parser.cc:143