Imported Upstream version 0.3.3
[anytun.git] / src / posix / signalHandler.hpp
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 #ifndef ANYTUN_signalHandler_hpp_INCLUDED
34 #define ANYTUN_signalHandler_hpp_INCLUDED
35
36 #include <csignal>
37 #include <boost/thread.hpp>
38 #include <boost/bind.hpp>
39
40 int SigIntHandler(int /*sig*/, const std::string& /*msg*/)
41 {
42   cLog.msg(Log::PRIO_NOTICE) << "SIG-Int caught, exiting";
43   return 1;
44 }
45
46 int SigQuitHandler(int /*sig*/, const std::string& /*msg*/)
47 {
48   cLog.msg(Log::PRIO_NOTICE) << "SIG-Quit caught, exiting";
49   return 1;
50 }
51
52 int SigHupHandler(int /*sig*/, const std::string& /*msg*/)
53 {
54   cLog.msg(Log::PRIO_NOTICE) << "SIG-Hup caught"; 
55   return 0;
56 }
57
58 int SigTermHandler(int /*sig*/, const std::string& /*msg*/)
59 {
60   cLog.msg(Log::PRIO_NOTICE) << "SIG-Term caught, exiting";
61   return 1;
62 }
63
64 int SigUsr1Handler(int /*sig*/, const std::string& /*msg*/)
65 {
66   cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr1 caught";
67   return 0;
68 }
69
70 int SigUsr2Handler(int /*sig*/, const std::string& /*msg*/)
71 {
72   cLog.msg(Log::PRIO_NOTICE) << "SIG-Usr2 caught";
73   return 0;
74 }
75
76 /// TODO: this outstandignly ugly please and i really can't stress the please fix it asap!!!!!!!
77 extern std::ofstream pidFile;
78
79 void handleSignal()
80 {
81   if(pidFile.is_open()) {
82     pid_t pid = getpid();
83     pidFile << pid;
84     pidFile.close();
85   }
86
87   struct timespec timeout;
88   sigset_t signal_set;
89   int sigNum;
90   while(1) {
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);
98     timeout.tv_sec = 1;
99     timeout.tv_nsec = 0;
100     sigNum = sigtimedwait(&signal_set, NULL, &timeout);
101     if (sigNum == -1) {
102       if (errno != EINTR && errno != EAGAIN) {
103         cLog.msg(Log::PRIO_ERROR) << "sigwait failed with error: \"" << AnytunErrno(errno) << "\" SignalHandling will be disabled";
104         break;
105       }
106     } else {
107       gSignalController.inject(sigNum);
108     }
109   }
110 }
111
112 void registerSignalHandler(SignalController& ctrl, DaemonService& /*service*/)
113 {
114   sigset_t signal_set;
115   
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);
123   
124 #if defined(BOOST_HAS_PTHREADS)
125   pthread_sigmask(SIG_BLOCK, &signal_set, NULL);
126 #else
127 #error The signalhandler works only with pthreads
128 #endif
129   
130   boost::thread(boost::bind(handleSignal));
131
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);
138
139   cLog.msg(Log::PRIO_DEBUG) << "signal handlers are now registered";
140 }
141
142 #endif