f23d070fb80c217cce186b480add793fd2ba0a97
[debian/uanytun.git] / src / sig_handler.c
1 /*
2  *  uAnytun
3  *
4  *  uAnytun is a tiny implementation of SATP. Unlike Anytun which is a full
5  *  featured implementation uAnytun has no support for multiple connections
6  *  or synchronisation. It is a small single threaded implementation intended
7  *  to act as a client on small platforms.
8  *  The secure anycast tunneling protocol (satp) defines a protocol used
9  *  for communication between any combination of unicast and anycast
10  *  tunnel endpoints.  It has less protocol overhead than IPSec in Tunnel
11  *  mode and allows tunneling of every ETHER TYPE protocol (e.g.
12  *  ethernet, ip, arp ...). satp directly includes cryptography and
13  *  message authentication based on the methods used by SRTP.  It is
14  *  intended to deliver a generic, scaleable and secure solution for
15  *  tunneling and relaying of packets of any protocol.
16  *
17  *
18  *  Copyright (C) 2007-2014 Christian Pointner <equinox@anytun.org>
19  *
20  *  This file is part of uAnytun.
21  *
22  *  uAnytun is free software: you can redistribute it and/or modify
23  *  it under the terms of the GNU General Public License as published by
24  *  the Free Software Foundation, either version 3 of the License, or
25  *  any later version.
26  *
27  *  uAnytun is distributed in the hope that it will be useful,
28  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
29  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  *  GNU General Public License for more details.
31  *
32  *  You should have received a copy of the GNU General Public License
33  *  along with uAnytun. If not, see <http://www.gnu.org/licenses/>.
34  *
35  *  In addition, as a special exception, the copyright holders give
36  *  permission to link the code of portions of this program with the
37  *  OpenSSL library under certain conditions as described in each
38  *  individual source file, and distribute linked combinations
39  *  including the two.
40  *  You must obey the GNU General Public License in all respects
41  *  for all of the code used other than OpenSSL.  If you modify
42  *  file(s) with this exception, you may extend this exception to your
43  *  version of the file(s), but you are not obligated to do so.  If you
44  *  do not wish to do so, delete this exception statement from your
45  *  version.  If you delete this exception statement from all source
46  *  files in the program, then also delete it here.
47  */
48
49 #include "datatypes.h"
50
51 #include "log.h"
52 #include "sig_handler.h"
53
54 #include <signal.h>
55 #include <unistd.h>
56 #include <fcntl.h>
57 #include <errno.h>
58 #include <string.h>
59
60 static int sig_pipe_fds[2];
61
62
63 static void sig_handler(int sig)
64 {
65   sigset_t set;
66   int ret = read(sig_pipe_fds[0], &set, sizeof(sigset_t));
67   if(ret != sizeof(sigset_t))
68     sigemptyset(&set);
69
70   sigaddset(&set, sig);
71   ret = write(sig_pipe_fds[1], &set, sizeof(sigset_t));
72 }
73
74
75 int signal_init()
76 {
77   if(pipe(sig_pipe_fds)) {
78     log_printf(ERROR, "signal handling init failed (pipe error: %s)", strerror(errno));
79     return -1;
80   }
81
82   int i;
83   for(i=0; i<2; ++i) {
84     int fd_flags = fcntl(sig_pipe_fds[i], F_GETFL);
85     if(fd_flags == -1) {
86       log_printf(ERROR, "signal handling init failed (pipe fd[%d] read flags error: %s)", i, strerror(errno));
87       return -1;
88     }
89     if(fcntl(sig_pipe_fds[i], F_SETFL, fd_flags | O_NONBLOCK) == -1){
90       log_printf(ERROR, "signal handling init failed (pipe fd[%d] write flags error: %s)", i, strerror(errno));
91       return -1;
92     }
93   }
94
95   struct sigaction act, ign;
96   act.sa_handler = sig_handler;
97   sigfillset(&act.sa_mask);
98   act.sa_flags = 0;
99   ign.sa_handler = SIG_IGN;
100   sigfillset(&ign.sa_mask);
101   ign.sa_flags = 0;
102
103   if((sigaction(SIGINT, &act, NULL) < 0) ||
104      (sigaction(SIGQUIT, &act, NULL) < 0) ||
105      (sigaction(SIGTERM, &act, NULL) < 0) ||
106      (sigaction(SIGHUP, &act, NULL) < 0) ||
107      (sigaction(SIGUSR1, &act, NULL) < 0) ||
108      (sigaction(SIGUSR2, &act, NULL) < 0) ||
109      (sigaction(SIGCHLD, &ign, NULL) < 0) ||
110      (sigaction(SIGPIPE, &ign, NULL) < 0)) {
111
112     log_printf(ERROR, "signal handling init failed (sigaction error: %s)", strerror(errno));
113     close(sig_pipe_fds[0]);
114     close(sig_pipe_fds[1]);
115   }
116
117   return sig_pipe_fds[0];
118 }
119
120 int signal_handle()
121 {
122   sigset_t set, oldset, tmpset;
123
124   sigemptyset(&tmpset);
125   sigaddset(&tmpset, SIGINT);
126   sigaddset(&tmpset, SIGQUIT);
127   sigaddset(&tmpset, SIGTERM);
128   sigaddset(&tmpset, SIGHUP);
129   sigaddset(&tmpset, SIGUSR1);
130   sigaddset(&tmpset, SIGUSR2);
131   sigprocmask(SIG_BLOCK, &tmpset, &oldset);
132
133   int ret = read(sig_pipe_fds[0], &set, sizeof(sigset_t));
134   if(ret != sizeof(sigset_t))
135     sigemptyset(&set);
136
137   int return_value = 0;
138   int sig;
139   for(sig=1; sig < NSIG; ++sig) {
140     if(sigismember(&set, sig)) {
141       switch(sig) {
142       case SIGINT: log_printf(NOTICE, "SIG-Int caught, exitting"); return_value = SIGINT; break;
143       case SIGQUIT: log_printf(NOTICE, "SIG-Quit caught, exitting"); return_value = SIGQUIT; break;
144       case SIGTERM: log_printf(NOTICE, "SIG-Term caught, exitting"); return_value = SIGTERM; break;
145       case SIGHUP: log_printf(NOTICE, "SIG-Hup caught"); return_value = SIGHUP; break;
146       case SIGUSR1: log_printf(NOTICE, "SIG-Usr1 caught"); break;
147       case SIGUSR2: log_printf(NOTICE, "SIG-Usr2 caught"); break;
148       default: log_printf(WARNING, "unknown signal %d caught, ignoring", sig); break;
149       }
150       sigdelset(&set, sig);
151     }
152   }
153
154   sigprocmask(SIG_SETMASK, &oldset, NULL);
155   return return_value;
156 }
157
158 void signal_stop()
159 {
160   struct sigaction act;
161   act.sa_handler = SIG_DFL;
162   sigemptyset(&act.sa_mask);
163   act.sa_flags = 0;
164
165   sigaction(SIGINT, &act, NULL);
166   sigaction(SIGQUIT, &act, NULL);
167   sigaction(SIGTERM, &act, NULL);
168   sigaction(SIGHUP, &act, NULL);
169   sigaction(SIGUSR1, &act, NULL);
170   sigaction(SIGUSR2, &act, NULL);
171   sigaction(SIGPIPE, &act, NULL);
172   sigaction(SIGCHLD, &act, NULL);
173
174   close(sig_pipe_fds[0]);
175   close(sig_pipe_fds[1]);
176 }