Imported Upstream version 0.3.2
[anytun.git] / src / signalController.cpp
1 /*
2  *  anytun
3  *
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.
12  *
13  *
14  *  Copyright (C) 2007-2009 Othmar Gsenger, Erwin Nindl, 
15  *                          Christian Pointner <satp@wirdorange.org>
16  *
17  *  This file is part of Anytun.
18  *
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
22  *  any later version.
23  *
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.
28  *
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/>.
31  */
32
33 #include <map>
34 #include <iostream>
35
36 #include "signalController.h"
37 #include "log.h"
38 #include "anytunError.h"
39 #include "threadUtils.hpp"
40
41 #ifndef _MSC_VER
42 #include <csignal>
43 #include <boost/bind.hpp>
44 #else
45 #include <windows.h>
46 #endif
47
48 SignalController* SignalController::inst = NULL;
49 Mutex SignalController::instMutex;
50 SignalController& gSignalController = SignalController::instance();
51
52 SignalController& SignalController::instance()
53 {
54         Lock lock(instMutex);
55         static instanceCleaner c;
56         if(!inst)
57                 inst = new SignalController();
58
59         return *inst;
60 }
61
62 int SigErrorHandler::handle(const std::string& msg)
63 {
64   AnytunError::throwErr() << msg;
65
66   return 0;
67 }
68
69 #ifndef _MSC_VER
70
71 int SigIntHandler::handle()
72 {
73   cLog.msg(Log::PRIO_NOTICE) << "SIG-Int caught, exiting";
74
75   return 1;
76 }
77
78 int SigQuitHandler::handle()
79 {
80   cLog.msg(Log::PRIO_NOTICE) << "SIG-Quit caught, exiting";
81
82   return 1;
83 }
84
85 int SigHupHandler::handle()
86 {
87   cLog.msg(Log::PRIO_NOTICE) << "SIG-Hup caught";
88
89   return 0;
90 }
91
92 int SigTermHandler::handle()
93 {
94   cLog.msg(Log::PRIO_NOTICE) << "SIG-Term caughtm, exiting";
95
96   return 1;
97 }
98
99 int SigUsr1Handler::handle()
100 {
101   cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr1 caught";
102
103   return 0;
104 }
105
106 int SigUsr2Handler::handle()
107 {
108   cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr2 caught";
109
110   return 0;
111 }
112 #else
113 int CtrlCHandler::handle()
114 {
115   cLog.msg(Log::PRIO_NOTICE) << "CTRL-C Event received, exitting";
116
117   return 1;
118 }
119
120 int CtrlBreakHandler::handle()
121 {
122   cLog.msg(Log::PRIO_NOTICE) << "CTRL-Break Event received, ignoring";
123
124   return 0;
125 }
126
127 int CtrlCloseHandler::handle()
128 {
129   cLog.msg(Log::PRIO_NOTICE) << "Close Event received, exitting";
130
131   return 1;
132 }
133
134 int CtrlLogoffHandler::handle()
135 {
136   cLog.msg(Log::PRIO_NOTICE) << "LogOff Event received, exitting";
137
138   return 1;
139 }
140
141 int CtrlShutdownHandler::handle()
142 {
143   cLog.msg(Log::PRIO_NOTICE) << "Shutdown Event received, exitting";
144
145   return 1;
146 }
147 #endif
148
149 SignalController::~SignalController() 
150 {
151   for(HandlerMap::iterator it = handler.begin(); it != handler.end(); ++it)
152     delete it->second;
153 }
154
155 #ifndef _MSC_VER
156 void SignalController::handle()
157 {
158   sigset_t signal_set;
159   int sigNum;
160
161   while(1) 
162   {
163     sigfillset(&signal_set);
164     sigwait(&signal_set, &sigNum);
165     inject(sigNum);
166   }
167 }
168 #else
169 bool SignalController::handle(DWORD ctrlType)
170 {
171   gSignalController.inject(ctrlType);
172   return true;
173 }
174 #endif
175
176 void SignalController::init()
177 {
178 #ifndef _MSC_VER
179   sigset_t signal_set;
180   
181   sigfillset(&signal_set);        
182   sigdelset(&signal_set, SIGCHLD);
183   sigdelset(&signal_set, SIGSEGV);
184   sigdelset(&signal_set, SIGBUS);
185   sigdelset(&signal_set, SIGFPE);
186
187 #if defined(BOOST_HAS_PTHREADS)
188   pthread_sigmask(SIG_BLOCK, &signal_set, NULL);
189 #else
190 #error The signalhandler works only with pthreads
191 #endif
192   
193   boost::thread(boost::bind(&SignalController::handle, this));
194
195   handler[SIGINT] = new SigIntHandler;
196   handler[SIGQUIT] = new SigQuitHandler;
197   handler[SIGHUP] = new SigHupHandler;
198   handler[SIGTERM] = new SigTermHandler;
199   handler[SIGUSR1] = new SigUsr1Handler;
200   handler[SIGUSR2] = new SigUsr2Handler;
201 #else
202   if(!SetConsoleCtrlHandler((PHANDLER_ROUTINE)SignalController::handle, true))
203     AnytunError::throwErr() << "Error on SetConsoleCtrlhandler: " << AnytunErrno(GetLastError());
204
205   handler[CTRL_C_EVENT] = new CtrlCHandler;
206   handler[CTRL_BREAK_EVENT] = new CtrlBreakHandler;
207   handler[CTRL_CLOSE_EVENT] = new CtrlCloseHandler;
208   handler[CTRL_LOGOFF_EVENT] = new CtrlLogoffHandler;
209   handler[CTRL_SHUTDOWN_EVENT] = new CtrlShutdownHandler;
210 #endif
211
212   handler[SIGERROR] = new SigErrorHandler;
213 }
214
215 void SignalController::inject(int sig, const std::string& msg)
216 {
217   {
218     Lock lock(sigQueueMutex);
219     sigQueue.push(SigPair(sig, msg));
220   }
221   sigQueueSem.up();
222 }
223
224 int SignalController::run()
225 {
226   while(1) {
227     sigQueueSem.down();
228     SigPair sig;
229     {
230       Lock lock(sigQueueMutex);
231       sig = sigQueue.front();
232       sigQueue.pop();
233     }
234     
235     HandlerMap::iterator it = handler.find(sig.first);
236     if(it != handler.end())
237     {
238       int ret;
239       if(sig.second == "")
240         ret = it->second->handle();
241       else
242         ret = it->second->handle(sig.second);
243
244       if(ret)
245         return ret;
246     }
247     else
248       cLog.msg(Log::PRIO_NOTICE) << "SIG " << sig.first << " caught with message '" << sig.second << "' - ignoring";
249   }
250   return 0;
251 }
252