From: pcarana Date: Tue, 2 Apr 2019 20:55:32 +0000 (-0600) Subject: Handle SIGINT signal to terminate client threads X-Git-Tag: v0.0.2~52^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=872b234da0c59f2617d4ceb471487af49f451981;p=thirdparty%2FFORT-validator.git Handle SIGINT signal to terminate client threads --- diff --git a/src/rtr/rtr.c b/src/rtr/rtr.c index 2189f27a..863e4272 100644 --- a/src/rtr/rtr.c +++ b/src/rtr/rtr.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -243,6 +244,31 @@ handle_client_connections(int server_fd) return 0; /* Unreachable. */ } +static void +signal_handler(int signal, siginfo_t *info, void *param) +{ + /* Empty handler */ +} + +static int +init_signal_handler(void) +{ + struct sigaction act; + int error; + + memset(&act, 0, sizeof act); + sigemptyset(&act.sa_mask); + act.sa_flags = SA_SIGINFO; + act.sa_sigaction = signal_handler; + + error = sigaction(SIGINT, &act, NULL); + if (error) { + warn("Error initializing signal handler"); + error = -errno; + } + return error; +} + /* * Starts the server, using the current thread to listen for RTR client * requests. @@ -268,6 +294,10 @@ rtr_listen(void) loop = true; SLIST_INIT(&threads); + error = init_signal_handler(); + if (error) + return error; + return handle_client_connections(server_fd); } @@ -283,6 +313,7 @@ rtr_cleanup(void) while (!SLIST_EMPTY(&threads)) { ptr = SLIST_FIRST(&threads); SLIST_REMOVE_HEAD(&threads, next); + pthread_kill(ptr->tid, SIGINT); pthread_join(ptr->tid, NULL); free(ptr); }