diff options
-rw-r--r-- | src/server.c | 4 | ||||
-rw-r--r-- | src/signal.c | 43 | ||||
-rw-r--r-- | src/signal.h | 20 | ||||
-rw-r--r-- | src/worker.c | 4 | ||||
-rw-r--r-- | src/worker_main.c | 1 |
5 files changed, 52 insertions, 20 deletions
diff --git a/src/server.c b/src/server.c index 58833a1..33610f3 100644 --- a/src/server.c +++ b/src/server.c @@ -385,6 +385,10 @@ int server_main(struct server *server) { int ret = 0; + ret = signal_install_global_handler(); + if (ret < 0) + return ret; + while (!global_stop_flag) { log("Waiting for new connections\n"); diff --git a/src/signal.c b/src/signal.c index b5c27ec..d721f67 100644 --- a/src/signal.c +++ b/src/signal.c @@ -6,12 +6,55 @@ */ #include "signal.h" +#include "compiler.h" #include "log.h" #include <signal.h> +#include <string.h> volatile sig_atomic_t global_stop_flag = 0; +static void set_global_stop_flag(UNUSED int signum) +{ + global_stop_flag = 1; +} + +static int my_sigaction(int signo, const struct sigaction *act) +{ + int ret = 0; + + ret = sigaction(signo, act, NULL); + if (ret < 0) { + log_errno("sigaction"); + return ret; + } + + return ret; +} + +int signal_install_global_handler() +{ + int ret = 0; + + struct sigaction sa; + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = set_global_stop_flag; + + /* Don't care about proper cleanup here; we exit the program if this + * fails anyway. */ + ret = my_sigaction(SIGINT, &sa); + if (ret < 0) + return ret; + ret = my_sigaction(SIGQUIT, &sa); + if (ret < 0) + return ret; + ret = my_sigaction(SIGTERM, &sa); + if (ret < 0) + return ret; + + return ret; +} + int signal_set(const sigset_t *new, sigset_t *old) { int ret = 0; diff --git a/src/signal.h b/src/signal.h index 22305a9..3509a92 100644 --- a/src/signal.h +++ b/src/signal.h @@ -8,28 +8,10 @@ #ifndef __SIGNAL_H__ #define __SIGNAL_H__ -#include "compiler.h" - #include <signal.h> -#include <string.h> extern volatile sig_atomic_t global_stop_flag; - -static void signal_handler(UNUSED int signum) -{ - global_stop_flag = 1; -} - -static __attribute__((constructor)) void signal_handler_install() -{ - struct sigaction sa; - memset(&sa, 0, sizeof(sa)); - sa.sa_handler = signal_handler; - - sigaction(SIGINT, &sa, NULL); - sigaction(SIGQUIT, &sa, NULL); - sigaction(SIGTERM, &sa, NULL); -} +int signal_install_global_handler(); int signal_set(const sigset_t *new, sigset_t *old); diff --git a/src/worker.c b/src/worker.c index cf57ad6..05243ac 100644 --- a/src/worker.c +++ b/src/worker.c @@ -181,6 +181,10 @@ int worker_main(struct worker *worker, UNUSED int argc, UNUSED char *argv[]) { int ret = 0; + ret = signal_install_global_handler(); + if (ret < 0) + return ret; + ret = msg_send_new_worker(worker); if (ret < 0) return ret; diff --git a/src/worker_main.c b/src/worker_main.c index 540eea1..fc3d0a2 100644 --- a/src/worker_main.c +++ b/src/worker_main.c @@ -7,7 +7,6 @@ #include "cmd_line.h" #include "const.h" -#include "signal.h" #include "worker.h" #include <getopt.h> |