fixed windows endian include
[anytun.git] / src / signalController.cpp
blob8d9c78f0100b8efa4acd2397127beacc0d2acdba
1 /*
2 * anytun
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-2008 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 version 3 as
21 * published by the Free Software Foundation.
23 * Anytun is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
32 #include <csignal>
33 #include <map>
35 #include <iostream>
37 #include <boost/bind.hpp>
38 #include "threadUtils.hpp"
39 #include "signalController.h"
40 #include "log.h"
43 int SigIntHandler::handle()
45 cLog.msg(Log::PRIO_NOTICE) << "SIG-Int caught, exiting";
47 return 1;
50 int SigQuitHandler::handle()
52 cLog.msg(Log::PRIO_NOTICE) << "SIG-Quit caught, exiting";
54 return 1;
57 int SigHupHandler::handle()
59 cLog.msg(Log::PRIO_NOTICE) << "SIG-Hup caught";
61 return 0;
64 int SigTermHandler::handle()
66 cLog.msg(Log::PRIO_NOTICE) << "SIG-Term caughtm, exiting";
68 return 1;
71 int SigUsr1Handler::handle()
73 cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr1 caught";
75 return 0;
78 int SigUsr2Handler::handle()
80 cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr2 caught";
82 return 0;
85 SignalController::~SignalController()
87 for(HandlerMap::iterator it = handler.begin(); it != handler.end(); ++it)
88 delete it->second;
89 if(thread) delete thread;
92 void SignalController::handle(void *s)
94 SignalController* self = reinterpret_cast<SignalController*>(s);
95 sigset_t signal_set;
96 int sigNum;
98 while(1)
100 sigfillset(&signal_set);
101 sigwait(&signal_set, &sigNum);
103 Lock(self->sigQueueMutex);
104 self->sigQueue.push(sigNum);
106 self->sigQueueSem.up();
110 void SignalController::init()
112 sigset_t signal_set;
114 sigfillset(&signal_set);
115 sigdelset(&signal_set, SIGCHLD);
116 sigdelset(&signal_set, SIGSEGV);
117 sigdelset(&signal_set, SIGBUS);
118 sigdelset(&signal_set, SIGFPE);
120 #if defined(BOOST_HAS_PTHREADS)
121 pthread_sigmask(SIG_BLOCK, &signal_set, NULL);
122 #else
123 #error The signalhandler works only with pthreads
124 #endif
126 thread = new boost::thread(boost::bind(handle, this));
128 handler[SIGINT] = new SigIntHandler;
129 handler[SIGQUIT] = new SigQuitHandler;
130 handler[SIGHUP] = new SigHupHandler;
131 handler[SIGTERM] = new SigTermHandler;
132 handler[SIGUSR1] = new SigUsr1Handler;
133 handler[SIGUSR2] = new SigUsr2Handler;
136 int SignalController::run()
138 while(1)
140 sigQueueSem.down();
141 int sigNum;
143 Lock lock(sigQueueMutex);
144 sigNum = sigQueue.front();
145 sigQueue.pop();
148 HandlerMap::iterator it = handler.find(sigNum);
149 if(it != handler.end())
151 int ret = it->second->handle();
152 if(ret)
153 return ret;
155 else
156 cLog.msg(Log::PRIO_NOTICE) << "SIG " << sigNum << " caught - ignoring";
158 return 0;