Imported Upstream version 0.3.4
[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 #include <boost/bind.hpp>
36
37 #include "signalController.h"
38 #include "log.h"
39 #include "anytunError.h"
40 #include "threadUtils.hpp"
41
42 SignalController* SignalController::inst = NULL;
43 Mutex SignalController::instMutex;
44 SignalController& gSignalController = SignalController::instance();
45
46 SignalController& SignalController::instance()
47 {
48   Lock lock(instMutex);
49   static instanceCleaner c;
50   if(!inst) {
51     inst = new SignalController();
52   }
53
54   return *inst;
55 }
56
57 int SigErrorHandler(int /*sig*/, const std::string& msg)
58 {
59   AnytunError::throwErr() << msg;
60
61   return 0;
62 }
63
64 //use system specific signal handler
65 #ifndef _MSC_VER
66 #include "signalHandler.hpp"
67 #else
68 #ifdef WIN_SERVICE
69 #include "win32/signalServiceHandler.hpp"
70 #else
71 #include "win32/signalHandler.hpp"
72 #endif
73 #endif
74
75 void SignalController::init(DaemonService& service)
76 {
77   registerSignalHandler(*this, service);
78   handler[SIGERROR] = boost::bind(SigErrorHandler, _1, _2);
79 }
80
81 void SignalController::inject(int sig, const std::string& msg)
82 {
83   {
84     Lock lock(sigQueueMutex);
85     sigQueue.push(SigPair(sig, msg));
86   }
87   sigQueueSem.up();
88 }
89
90 int SignalController::run()
91 {
92   for(CallbackMap::iterator it = callbacks.begin(); it != callbacks.end(); ++it)
93     if(it->first == CALLB_RUNNING) {
94       it->second();
95     }
96
97   int ret = 0;
98   while(1) {
99     sigQueueSem.down();
100     SigPair sig;
101     {
102       Lock lock(sigQueueMutex);
103       sig = sigQueue.front();
104       sigQueue.pop();
105     }
106
107     HandlerMap::iterator it = handler.find(sig.first);
108     if(it != handler.end()) {
109       ret = it->second(sig.first, sig.second);
110
111       if(ret) {
112         break;
113       }
114     } else {
115       it = handler.find(SIGUNKNOWN);
116       if(it != handler.end()) {
117         it->second(sig.first, sig.second);
118       } else {
119         cLog.msg(Log::PRIO_NOTICE) << "SIG " << sig.first << " caught with message '" << sig.second << "' - ignoring";
120       }
121     }
122   }
123
124   for(CallbackMap::iterator it = callbacks.begin(); it != callbacks.end(); ++it)
125     if(it->first == CALLB_STOPPING) {
126       it->second();
127     }
128
129   return ret;
130 }
131