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.
18 * Copyright (C) 2007-2008 Christian Pointner <equinox@anytun.org>
20 * This file is part of uAnytun.
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
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.
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/>.
36 #include "datatypes.h"
42 #include <sys/select.h>
45 #include "sig_handler.h"
50 static int sig_pipe_fds[2];
53 static void sig_handler(int sig)
56 int ret = read(sig_pipe_fds[0], &set, sizeof(sigset_t));
57 if(ret != sizeof(sigset_t))
61 ret = write(sig_pipe_fds[1], &set, sizeof(sigset_t));
67 if(pipe(sig_pipe_fds)) {
68 log_printf(ERROR, "signal handling init failed (pipe error: %s)", strerror(errno));
74 int fd_flags = fcntl(sig_pipe_fds[i], F_GETFL);
76 log_printf(ERROR, "signal handling init failed (pipe fd[%d] read flags error: %s)", i, strerror(errno));
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));
86 act.sa_handler = sig_handler;
87 sigfillset(&act.sa_mask);
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)) {
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]);
102 return sig_pipe_fds[0];
107 sigset_t set, oldset, tmpset;
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);
118 int ret = read(sig_pipe_fds[0], &set, sizeof(sigset_t));
119 if(ret != sizeof(sigset_t))
122 int return_value = 0;
124 for(sig=1; sig < NSIG; ++sig) {
125 if(sigismember(&set, 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;
135 sigdelset(&set, sig);
139 sigprocmask(SIG_SETMASK, &oldset, NULL);
145 struct sigaction act;
146 act.sa_handler = SIG_DFL;
147 sigemptyset(&act.sa_mask);
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);
157 close(sig_pipe_fds[0]);
158 close(sig_pipe_fds[1]);