bugfix with strerror_r - still not working but at least not using uninitialized data
[anytun.git] / src / signalController.cpp
blobf77abf6d967cbfa324c471aad443c36b771b3648
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 anytun.org <satp@wirdorange.org>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program (see the file COPYING included with this
27 * distribution); if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <csignal>
32 #include <map>
34 #include <iostream>
36 #include "threadUtils.hpp"
37 #include "signalController.h"
38 #include "log.h"
41 int SigIntHandler::handle()
43 cLog.msg(Log::PRIO_NOTICE) << "SIG-Int caught, exiting";
45 return 1;
48 int SigQuitHandler::handle()
50 cLog.msg(Log::PRIO_NOTICE) << "SIG-Quit caught, exiting";
52 return 1;
55 int SigHupHandler::handle()
57 cLog.msg(Log::PRIO_NOTICE) << "SIG-Hup caught";
59 return 0;
62 int SigTermHandler::handle()
64 cLog.msg(Log::PRIO_NOTICE) << "SIG-Term caughtm, exiting";
66 return 1;
69 int SigUsr1Handler::handle()
71 cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr1 caught";
73 return 0;
76 int SigUsr2Handler::handle()
78 cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr2 caught";
80 return 0;
83 SignalController::~SignalController()
85 for(HandlerMap::iterator it = handler.begin(); it != handler.end(); ++it)
86 delete it->second;
89 void* SignalController::handle(void *s)
91 SignalController* self = reinterpret_cast<SignalController*>(s);
92 sigset_t signal_set;
93 int sigNum;
95 while(1)
97 sigfillset(&signal_set);
98 sigwait(&signal_set, &sigNum);
100 Lock(self->sigQueueMutex);
101 self->sigQueue.push(sigNum);
103 self->sigQueueSem.up();
105 pthread_exit(NULL);
108 void SignalController::init()
110 sigset_t signal_set;
112 sigfillset(&signal_set);
113 sigdelset(&signal_set, SIGCHLD);
114 sigdelset(&signal_set, SIGSEGV);
115 sigdelset(&signal_set, SIGBUS);
116 sigdelset(&signal_set, SIGFPE);
117 pthread_sigmask(SIG_BLOCK, &signal_set, NULL);
119 pthread_create(&thread, NULL, handle, this);
120 pthread_detach(thread);
122 handler[SIGINT] = new SigIntHandler;
123 handler[SIGQUIT] = new SigQuitHandler;
124 handler[SIGHUP] = new SigHupHandler;
125 handler[SIGTERM] = new SigTermHandler;
126 handler[SIGUSR1] = new SigUsr1Handler;
127 handler[SIGUSR2] = new SigUsr2Handler;
130 int SignalController::run()
132 while(1)
134 sigQueueSem.down();
135 int sigNum;
137 Lock lock(sigQueueMutex);
138 sigNum = sigQueue.front();
139 sigQueue.pop();
142 HandlerMap::iterator it = handler.find(sigNum);
143 if(it != handler.end())
145 int ret = it->second->handle();
146 if(ret)
147 return ret;
149 else
150 cLog.msg(Log::PRIO_NOTICE) << "SIG " << sigNum << " caught - ignoring";
152 return 0;