diff options
author | Christian Pointner <equinox@anytun.org> | 2008-12-29 06:13:48 +0000 |
---|---|---|
committer | Christian Pointner <equinox@anytun.org> | 2008-12-29 06:13:48 +0000 |
commit | d759331ef32bbf0908bce4d8753293ef08bbbfed (patch) | |
tree | 67e33b5fe11e14d154beaa6bf609aebac760645c /src | |
parent | added -O2 to CCFLAGS (diff) |
improved signal handling
Diffstat (limited to 'src')
-rw-r--r-- | src/signal.c | 30 | ||||
-rw-r--r-- | src/signal.h | 5 | ||||
-rw-r--r-- | src/uanytun.c | 14 |
3 files changed, 23 insertions, 26 deletions
diff --git a/src/signal.c b/src/signal.c index 1086969..122577d 100644 --- a/src/signal.c +++ b/src/signal.c @@ -42,10 +42,9 @@ volatile sig_atomic_t signal_exit = 0; void signal_init() { - signal(SIGINT, handle_signal_exit); - signal(SIGQUIT, handle_signal_exit); - signal(SIGTERM, handle_signal_exit); - + signal(SIGINT, handle_signal); + signal(SIGQUIT, handle_signal); + signal(SIGTERM, handle_signal); signal(SIGHUP, handle_signal); signal(SIGUSR1, handle_signal); signal(SIGUSR2, handle_signal); @@ -54,29 +53,12 @@ void signal_init() void handle_signal(int sig) { switch(sig) { + case SIGINT: log_printf(NOTICE, "SIG-Int caught, exitting"); signal_exit = 1; break; + case SIGQUIT: log_printf(NOTICE, "SIG-Quit caught, exitting"); signal_exit = 1; break; + case SIGTERM: log_printf(NOTICE, "SIG-Term caught, exitting"); signal_exit = 1; break; case SIGHUP: log_printf(NOTICE, "SIG-Hup caught"); break; case SIGUSR1: log_printf(NOTICE, "SIG-Usr1 caught"); break; case SIGUSR2: log_printf(NOTICE, "SIG-Usr2 caught"); break; default: log_printf(NOTICE, "Signal %d caught, ignoring", sig); break; } } - -void handle_signal_exit(int sig) -{ - switch(sig) { - case SIGINT: log_printf(NOTICE, "SIG-Int caught, exitting"); break; - case SIGQUIT: log_printf(NOTICE, "SIG-Quit caught, exitting"); break; - case SIGTERM: log_printf(NOTICE, "SIG-Term caught, exitting"); break; - default: log_printf(NOTICE, "Signal %d caught, ignoring", sig); return; - } - - if (signal_exit) - raise (sig); - signal_exit = 1; - - // do cleanup here - - signal (sig, SIG_DFL); - raise (sig); -} - diff --git a/src/signal.h b/src/signal.h index 3dd0440..328f849 100644 --- a/src/signal.h +++ b/src/signal.h @@ -35,8 +35,11 @@ #ifndef _SIGNAL_H_ #define _SIGNAL_H_ +#include <signal.h> + +extern volatile sig_atomic_t signal_exit; + void signal_init(); void handle_signal(int sig); -void handle_signal_exit(int sig); #endif diff --git a/src/uanytun.c b/src/uanytun.c index d76c468..0d47e8f 100644 --- a/src/uanytun.c +++ b/src/uanytun.c @@ -37,6 +37,7 @@ #include <stdlib.h> #include <stdio.h> #include <string.h> +#include <errno.h> #include "log.h" #include "signal.h" @@ -83,13 +84,19 @@ int main_loop(tun_device_t* dev, udp_socket_t* sock, options_t* opt) int nfds = dev->fd_ > sock->fd_ ? dev->fd_+1 : sock->fd_+1; int ret = select(nfds, &readfds, NULL, NULL, NULL); - if(ret == -1) { + if(ret == -1 && errno != EINTR) { log_printf(ERR, "select returned with error: %m"); + cipher_close(&c); return -1; } if(!ret) continue; + if(signal_exit) { + cipher_close(&c); + return 1; + } + if(FD_ISSET(dev->fd_, &readfds)) { len = tun_read(dev, plain_packet_get_payload(&plain_packet), plain_packet_get_payload_length(&plain_packet)); plain_packet_set_payload_length(&plain_packet, len); @@ -231,6 +238,11 @@ int main(int argc, char* argv[]) if(!ret) log_printf(NOTICE, "normal shutdown"); + else if(ret < 0) + log_printf(NOTICE, "shutdown after error"); + else + log_printf(NOTICE, "shutdown after signal"); + return ret; } |