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-2010 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"
39 #include "sig_handler.h"
47 static int sig_pipe_fds[2];
50 static void sig_handler(int sig)
53 int ret = read(sig_pipe_fds[0], &set, sizeof(sigset_t));
54 if(ret != sizeof(sigset_t))
58 ret = write(sig_pipe_fds[1], &set, sizeof(sigset_t));
64 if(pipe(sig_pipe_fds)) {
65 log_printf(ERROR, "signal handling init failed (pipe error: %s)", strerror(errno));
71 int fd_flags = fcntl(sig_pipe_fds[i], F_GETFL);
73 log_printf(ERROR, "signal handling init failed (pipe fd[%d] read flags error: %s)", i, strerror(errno));
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));
83 act.sa_handler = sig_handler;
84 sigfillset(&act.sa_mask);
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)) {
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]);
99 return sig_pipe_fds[0];
104 sigset_t set, oldset, tmpset;
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);
115 int ret = read(sig_pipe_fds[0], &set, sizeof(sigset_t));
116 if(ret != sizeof(sigset_t))
119 int return_value = 0;
121 for(sig=1; sig < NSIG; ++sig) {
122 if(sigismember(&set, 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;
132 sigdelset(&set, sig);
136 sigprocmask(SIG_SETMASK, &oldset, NULL);
142 struct sigaction act;
143 act.sa_handler = SIG_DFL;
144 sigemptyset(&act.sa_mask);
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);
154 close(sig_pipe_fds[0]);
155 close(sig_pipe_fds[1]);