4 * The secure anycast tunneling protocol (satp) defines a protocol used
5 * for communication between any combination of unicast and anycast
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
8 * ethernet, ip, arp ...). satp directly includes cryptography and
9 * message authentication based on the methodes used by SRTP. It is
10 * intended to deliver a generic, scaleable and secure solution for
11 * tunneling and relaying of packets of any protocol.
14 * Copyright (C) 2007-2009 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
19 * Anytun is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
24 * Anytun is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
33 #ifndef ANYTUN_signalHandler_hpp_INCLUDED
34 #define ANYTUN_signalHandler_hpp_INCLUDED
37 #include <boost/thread.hpp>
38 #include <boost/bind.hpp>
40 int SigIntHandler(int /*sig*/, const std::string& /*msg*/)
42 cLog.msg(Log::PRIO_NOTICE) << "SIG-Int caught, exiting";
46 int SigQuitHandler(int /*sig*/, const std::string& /*msg*/)
48 cLog.msg(Log::PRIO_NOTICE) << "SIG-Quit caught, exiting";
52 int SigHupHandler(int /*sig*/, const std::string& /*msg*/)
54 cLog.msg(Log::PRIO_NOTICE) << "SIG-Hup caught";
58 int SigTermHandler(int /*sig*/, const std::string& /*msg*/)
60 cLog.msg(Log::PRIO_NOTICE) << "SIG-Term caught, exiting";
64 int SigUsr1Handler(int /*sig*/, const std::string& /*msg*/)
66 cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr1 caught";
70 int SigUsr2Handler(int /*sig*/, const std::string& /*msg*/)
72 cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr2 caught";
76 /// TODO: this outstandignly ugly please and i really can't stress the please fix it asap!!!!!!!
77 extern std::ofstream pidFile;
81 if(pidFile.is_open()) {
87 struct timespec timeout;
91 sigemptyset(&signal_set);
92 sigaddset(&signal_set, SIGINT);
93 sigaddset(&signal_set, SIGQUIT);
94 sigaddset(&signal_set, SIGHUP);
95 sigaddset(&signal_set, SIGTERM);
96 sigaddset(&signal_set, SIGUSR1);
97 sigaddset(&signal_set, SIGUSR2);
100 sigNum = sigtimedwait(&signal_set, NULL, &timeout);
102 if (errno != EINTR && errno != EAGAIN) {
103 cLog.msg(Log::PRIO_ERROR) << "sigwait failed with error: \"" << AnytunErrno(errno) << "\" SignalHandling will be disabled";
107 gSignalController.inject(sigNum);
112 void registerSignalHandler(SignalController& ctrl, DaemonService& /*service*/)
116 sigemptyset(&signal_set);
117 sigaddset(&signal_set, SIGINT);
118 sigaddset(&signal_set, SIGQUIT);
119 sigaddset(&signal_set, SIGHUP);
120 sigaddset(&signal_set, SIGTERM);
121 sigaddset(&signal_set, SIGUSR1);
122 sigaddset(&signal_set, SIGUSR2);
124 #if defined(BOOST_HAS_PTHREADS)
125 pthread_sigmask(SIG_BLOCK, &signal_set, NULL);
127 #error The signalhandler works only with pthreads
130 boost::thread(boost::bind(handleSignal));
132 ctrl.handler[SIGINT] = boost::bind(SigIntHandler, _1, _2);
133 ctrl.handler[SIGQUIT] = boost::bind(SigQuitHandler, _1, _2);
134 ctrl.handler[SIGHUP] = boost::bind(SigHupHandler, _1, _2);
135 ctrl.handler[SIGTERM] = boost::bind(SigTermHandler, _1, _2);
136 ctrl.handler[SIGUSR1] = boost::bind(SigUsr1Handler, _1, _2);
137 ctrl.handler[SIGUSR2] = boost::bind(SigUsr2Handler, _1, _2);
139 cLog.msg(Log::PRIO_DEBUG) << "signal handlers are now registered";