Merge commit 'upstream/0.3.2'
[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 methodes 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-2008 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
36 #include "datatypes.h"
37
38 #include "log.h"
39 #include <signal.h>
40 #include <unistd.h>
41 #include <fcntl.h>
42 #include <sys/select.h>
43 #include <errno.h>
44
45 #include "sig_handler.h"
46
47 #include <stdio.h>
48
49
50 static int sig_pipe_fds[2];
51
52
53 static void sig_handler(int sig)
54 {
55   sigset_t set;
56   int ret = read(sig_pipe_fds[0], &set, sizeof(sigset_t));
57   if(ret != sizeof(sigset_t))
58     sigemptyset(&set);
59
60   sigaddset(&set, sig);
61   ret = write(sig_pipe_fds[1], &set, sizeof(sigset_t));
62 }
63
64
65 int signal_init()
66 {
67   if(pipe(sig_pipe_fds)) {
68     log_printf(ERROR, "signal handling init failed (pipe error: %s)", strerror(errno));
69     return -1;
70   }
71
72   int i;
73   for(i=0; i<2; ++i) {
74     int fd_flags = fcntl(sig_pipe_fds[i], F_GETFL);
75     if(fd_flags == -1) {
76       log_printf(ERROR, "signal handling init failed (pipe fd[%d] read flags error: %s)", i, strerror(errno));
77       return -1;
78     }
79     if(fcntl(sig_pipe_fds[i], F_SETFL, fd_flags | O_NONBLOCK) == -1){
80       log_printf(ERROR, "signal handling init failed (pipe fd[%d] write flags error: %s)", i, strerror(errno));
81       return -1;
82     }
83   }
84
85   struct sigaction act;
86   act.sa_handler = sig_handler;
87   sigfillset(&act.sa_mask);
88   act.sa_flags = 0;
89
90   if((sigaction(SIGINT, &act, NULL) < 0) ||
91      (sigaction(SIGQUIT, &act, NULL) < 0) ||
92      (sigaction(SIGTERM, &act, NULL) < 0) ||
93      (sigaction(SIGHUP, &act, NULL) < 0) ||
94      (sigaction(SIGUSR1, &act, NULL) < 0) ||
95      (sigaction(SIGUSR2, &act, NULL) < 0)) {
96
97     log_printf(ERROR, "signal handling init failed (sigaction error: %s)", strerror(errno));
98     close(sig_pipe_fds[0]);
99     close(sig_pipe_fds[1]);
100   }
101
102   return sig_pipe_fds[0];
103 }
104
105 int signal_handle()
106 {
107   sigset_t set, oldset, tmpset;
108
109   sigemptyset(&tmpset);
110   sigaddset(&tmpset, SIGINT);
111   sigaddset(&tmpset, SIGQUIT);
112   sigaddset(&tmpset, SIGTERM);
113   sigaddset(&tmpset, SIGHUP);
114   sigaddset(&tmpset, SIGUSR1);
115   sigaddset(&tmpset, SIGUSR2);
116   sigprocmask(SIG_BLOCK, &tmpset, &oldset);
117
118   int ret = read(sig_pipe_fds[0], &set, sizeof(sigset_t));
119   if(ret != sizeof(sigset_t))
120     sigemptyset(&set);
121
122   int return_value = 0;
123   int sig;
124   for(sig=1; sig < NSIG; ++sig) {
125     if(sigismember(&set, sig)) {
126       switch(sig) {
127       case SIGINT: log_printf(NOTICE, "SIG-Int caught, exitting"); return_value = 1; break;
128       case SIGQUIT: log_printf(NOTICE, "SIG-Quit caught, exitting"); return_value = 1; break;
129       case SIGTERM: log_printf(NOTICE, "SIG-Term caught, exitting"); return_value = 1; break;
130       case SIGHUP: log_printf(NOTICE, "SIG-Hup caught"); break;
131       case SIGUSR1: log_printf(NOTICE, "SIG-Usr1 caught"); break;
132       case SIGUSR2: log_printf(NOTICE, "SIG-Usr2 caught"); break;
133       default: log_printf(WARNING, "unknown signal %d caught, ignoring", sig); break;
134       }
135       sigdelset(&set, sig);
136     }
137   }
138
139   sigprocmask(SIG_SETMASK, &oldset, NULL);
140   return return_value;
141 }
142
143 void signal_stop()
144 {
145   struct sigaction act;
146   act.sa_handler = SIG_DFL;
147   sigemptyset(&act.sa_mask);
148   act.sa_flags = 0;
149
150   sigaction(SIGINT, &act, NULL);
151   sigaction(SIGQUIT, &act, NULL);
152   sigaction(SIGTERM, &act, NULL);
153   sigaction(SIGHUP, &act, NULL);
154   sigaction(SIGUSR1, &act, NULL);
155   sigaction(SIGUSR2, &act, NULL);
156
157   close(sig_pipe_fds[0]);
158   close(sig_pipe_fds[1]);
159 }