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