Leosac  0.8.0
Open Source Access Control
signalhandler.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
26 #include "signalhandler.hpp"
27 #include <assert.h>
28 
29 extern "C" {
30 #include <unistd.h>
31 }
32 
34 #include "tools/unixsyscall.hpp"
35 
36 using namespace Leosac::Tools;
37 
38 static std::array<std::function<void(Signal)>, Leosac::Tools::num_signals>
39  sigCallback;
40 
41 static void fesser_e(int signal)
42 {
43  assert(signal > 0 && signal < Leosac::Tools::num_signals);
44  if (sigCallback[signal])
45  sigCallback[signal](static_cast<Signal>(signal));
46 }
47 
49  std::function<void(Signal)> callback)
50 {
51  struct sigaction act;
52 
53  act.sa_handler = &fesser_e;
54  if (sigemptyset(&act.sa_mask) < 0)
55  throw(SignalException(UnixSyscall::getErrorString("sigemptyset", errno)));
56  act.sa_flags = SA_RESTART;
57  if (sigaction(static_cast<int>(signal), &act, 0) < 0)
58  throw(SignalException(UnixSyscall::getErrorString("sigaction", errno)));
59  assert(static_cast<int>(signal) > 0 &&
60  static_cast<int>(signal) < Leosac::Tools::num_signals);
61  sigCallback[static_cast<int>(signal)] = callback;
62 }
unixsyscall.hpp
unix syscall helper functions
signalhandler.hpp
signal handler to provide a C++ interface for UNIX sigaction()
Leosac::Tools::UnixSyscall::getErrorString
static std::string getErrorString(const std::string &func, int errNo)
Definition: unixsyscall.cpp:32
Leosac::Tools::SignalHandler::registerCallback
static void registerCallback(Signal signal, std::function< void(Signal)> callback)
Definition: signalhandler.cpp:48
signalexception.hpp
Exception class for Signal related errors.
SignalException
Definition: signalexception.hpp:33
Leosac::Tools::Signal
Signal
Definition: signalhandler.hpp:39
Leosac::Tools
Definition: DatabaseLogSink.hpp:27