From 708825e4b229a4a1e182e682e2d1021fa2765396 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sat, 15 Jan 2011 22:15:39 +0100 Subject: [PATCH] Ignore signals in all but the main thread. --- src/net.c | 20 +------------------- src/net.h | 1 + src/process.c | 12 ++++++++++++ src/threads.h | 9 ++++++++- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/net.c b/src/net.c index 2ffe03a..3eec9a6 100644 --- a/src/net.c +++ b/src/net.c @@ -38,6 +38,7 @@ int contradicting_add_edge = 0; int contradicting_del_edge = 0; +bool running = true; /* Purge edges and subnets of unreachable nodes. Use carefully. */ @@ -244,16 +245,6 @@ void handle_meta_connection_data(void *data) { } } -static void sigterm_handler(int signal, short events, void *data) { - logger(LOG_NOTICE, "Got %s signal", strsignal(signal)); - exit(0); -} - -static void sighup_handler(int signal, short events, void *data) { - logger(LOG_NOTICE, "Got %s signal", strsignal(signal)); - reload_configuration(); -} - int reload_configuration(void) { connection_t *c; splay_node_t *node, *next; @@ -361,15 +352,6 @@ int main_loop(void) { event_add(&timeout_event); -#ifdef SIGHUP - signal(SIGHUP, sighup_handler); -#endif -#ifdef SIGTERM - signal(SIGTERM, sigterm_handler); -#endif -#ifdef SIGQUIT - signal(SIGQUIT, sigterm_handler); -#endif while(true) { mutex_unlock(&mutex); diff --git a/src/net.h b/src/net.h index 2be797d..d39dd93 100644 --- a/src/net.h +++ b/src/net.h @@ -158,5 +158,6 @@ extern void load_all_subnets(); #endif extern mutex_t mutex; +extern bool running; #endif /* __TINC_NET_H__ */ diff --git a/src/process.c b/src/process.c index c1ad81f..e6b108f 100644 --- a/src/process.c +++ b/src/process.c @@ -342,6 +342,14 @@ bool execute_script(const char *name, char **envp) { */ #ifndef HAVE_MINGW +static RETSIGTYPE sigterm_handler(int a) { + logger(LOG_NOTICE, "Got %s signal", "TERM"); + if(running) + running = false; + else + exit(1); +} + static RETSIGTYPE fatal_signal_square(int a) { logger(LOG_ERR, "Got another fatal signal %d (%s): not restarting.", a, strsignal(a)); @@ -382,11 +390,15 @@ static struct { int signal; void (*handler)(int); } sighandlers[] = { + {SIGTERM, sigterm_handler}, + {SIGQUIT, sigterm_handler}, + {SIGINT, sigterm_handler}, {SIGSEGV, fatal_signal_handler}, {SIGBUS, fatal_signal_handler}, {SIGILL, fatal_signal_handler}, {SIGPIPE, ignore_signal_handler}, {SIGCHLD, ignore_signal_handler}, + {SIGALRM, ignore_signal_handler}, {0, NULL} }; #endif diff --git a/src/threads.h b/src/threads.h index 85aaced..3ec8c68 100644 --- a/src/threads.h +++ b/src/threads.h @@ -29,7 +29,14 @@ typedef pthread_t thread_t; typedef pthread_mutex_t mutex_t; static inline bool thread_create(thread_t *tid, void (*func)(void *), void *arg) { - return !pthread_create(tid, NULL, (void *(*)(void *))func, arg); + bool result; + + sigset_t old, block; + sigfillset(&block); + pthread_sigmask(SIG_SETMASK, &block, &old); + result = pthread_create(tid, NULL, (void *(*)(void *))func, arg); + pthread_sigmask(SIG_SETMASK, &old, NULL); + return !result; } static inline void thread_destroy(thread_t *tid) { pthread_cancel(*tid); -- 2.25.1