Kea  1.5.0
signal_set.h
Go to the documentation of this file.
1 // Copyright (C) 2014-2015 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 #ifndef SIGNAL_SET_H
8 #define SIGNAL_SET_H
9 
10 #include <exceptions/exceptions.h>
11 #include <boost/function.hpp>
12 #include <boost/noncopyable.hpp>
13 #include <boost/shared_ptr.hpp>
14 #include <set>
15 #include <list>
16 #include <pthread.h>
17 #include <signal.h>
18 
19 namespace isc {
20 namespace util {
21 
24 class SignalSetError : public Exception {
25 public:
26  SignalSetError(const char* file, size_t line, const char* what) :
27  isc::Exception(file, line, what) { };
28 };
29 
30 
32 typedef std::set<int> SigIntSet;
34 typedef boost::shared_ptr<SigIntSet> SigIntSetPtr;
35 
37 typedef std::list<int> SigIntList;
39 typedef boost::shared_ptr<SigIntList> SigIntListPtr;
40 
41 
43 class SignalSet;
45 typedef boost::shared_ptr<SignalSet> SignalSetPtr;
47 typedef boost::function<void(int signum)> SignalHandler;
48 
54 typedef boost::function<bool(int signum)> BoolSignalHandler;
55 
87 class SignalSet : public boost::noncopyable {
88 public:
94  SignalSet(const int sig0);
95 
102  SignalSet(const int sig0, const int sig1);
103 
111  SignalSet(const int sig0, const int sig1, const int sig2);
112 
116  ~SignalSet();
117 
127  void add(const int sig);
128 
133  void clear();
134 
139  int getNext() const;
140 
149  void handleNext(SignalHandler signal_handler);
150 
154  void remove(const int sig);
155 
166  static void setOnReceiptHandler(BoolSignalHandler handler);
167 
169  static void clearOnReceiptHandler();
170 
186  static bool invokeOnReceiptHandler(int sig);
187 
188 private:
189 
194  void block() const;
195 
203  void erase(const int sig);
204 
210  void insert(const int sig);
211 
218  void maskSignals(const int mask) const;
219 
225  void popNext();
226 
230  void unblock() const;
231 
233  std::set<int> local_signals_;
234 
238  SigIntSetPtr registered_signals_;
239 
243  SigIntListPtr signal_states_;
244 };
245 
246 }
247 }
248 
249 #endif // SIGNAL_SET_H
250 
int getNext() const
Returns a code of the next received signal.
Definition: signal_set.cc:201
static void setOnReceiptHandler(BoolSignalHandler handler)
Registers a handler as the onreceipt signal handler.
Definition: signal_set.cc:307
std::set< int > SigIntSet
Defines a set of integer signal identifiers: SIGHUP, SIGTERM...
Definition: signal_set.h:32
boost::shared_ptr< SignalSet > SignalSetPtr
Pointer to the isc::util::SignalSet.
Definition: signal_set.h:43
boost::function< bool(int signum)> BoolSignalHandler
Pointer to a signal handling function which returns bool result.
Definition: signal_set.h:54
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static bool invokeOnReceiptHandler(int sig)
Invokes the onreceipt handler if it exists.
Definition: signal_set.cc:88
void handleNext(SignalHandler signal_handler)
Calls a handler for the next received signal.
Definition: signal_set.cc:233
void clear()
Uninstalls all signals.
Definition: signal_set.cc:188
~SignalSet()
Destructor.
Definition: signal_set.cc:156
Exception thrown when the isc::util::SignalSet class experiences an error.
Definition: signal_set.h:24
Represents a collection of signals handled in a customized way.
Definition: signal_set.h:87
boost::shared_ptr< SigIntList > SigIntListPtr
Pointer to a list of signal identifiers.
Definition: signal_set.h:39
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-dhcp-ddns.
std::list< int > SigIntList
Defines a list of integer signal identifiers: SIGHUP, SIGTERM...
Definition: signal_set.h:37
boost::function< void(int signum)> SignalHandler
Pointer to the signal handling function.
Definition: signal_set.h:47
boost::shared_ptr< SigIntSet > SigIntSetPtr
Pointer to a set of signal identifiers.
Definition: signal_set.h:34
static void clearOnReceiptHandler()
Unregisters the onreceipt signal handler.
Definition: signal_set.cc:312
SignalSetError(const char *file, size_t line, const char *what)
Definition: signal_set.h:26
SignalSet(const int sig0)
Constructor installing one signal.
Definition: signal_set.cc:134
void remove(const int sig)
Uninstalls signal handler for a specified signal.
Definition: signal_set.cc:282
void add(const int sig)
Installs the handler for the specified signal.
Definition: signal_set.cc:168