From: Alberto Leiva Popper Date: Fri, 1 May 2026 23:31:04 +0000 (-0600) Subject: Add custom SIGTERM handler X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1c3286ed4ee58b14973a868402db475f8759ffc8;p=thirdparty%2FFORT-validator.git Add custom SIGTERM handler Allows clean termination when killed via SIGTERM. It's a temporal hack; not safe because of the multithreading. It's meant to be enabled during the controlled environment of Rapport tests, and nowhere else. Compile with -DOVERRIDE_SIGTERM to enable. --- diff --git a/src/log.c b/src/log.c index 0b12ce19..a67c0ca2 100644 --- a/src/log.c +++ b/src/log.c @@ -156,6 +156,18 @@ sigusr1_handler(int signum) */ } +volatile bool fort_end = false; + +#ifdef OVERRIDE_SIGTERM + +static void +sigterm_handler(int signum) +{ + fort_end = true; +} + +#endif + /* * Register better handlers for some signals. * @@ -190,6 +202,16 @@ register_signal_handlers(void) } #endif +#ifdef OVERRIDE_SIGTERM + /* SIGTERM handler */ + memset(&action, 0, sizeof(action)); + action.sa_handler = sigterm_handler; + sigemptyset(&action.sa_mask); + if (sigaction(SIGTERM, &action, NULL) == -1) + pr_op_err("SIGTERM handler registration failure: %s", + strerror(errno)); +#endif + /* SIGUSR1 handler */ memset(&action, 0, sizeof(action)); action.sa_handler = sigusr1_handler; diff --git a/src/log.h b/src/log.h index a8ac304d..57b4b0fa 100644 --- a/src/log.h +++ b/src/log.h @@ -39,6 +39,8 @@ #define CHECK_FORMAT(str, args) /* Nothing */ #endif +extern volatile bool fort_end; + /* * Only call this group of functions when you know there's only one thread. * diff --git a/src/main.c b/src/main.c index 84cc2d58..e4f458c3 100644 --- a/src/main.c +++ b/src/main.c @@ -42,9 +42,11 @@ fort_server(void) return error; error = vrps_update(NULL); + if (fort_end) + goto end; if (error) { pr_op_err("Main loop: Validation unsuccessful; results unusable."); - return error; + goto end; } rtr_notify(); @@ -56,10 +58,12 @@ fort_server(void) do { pr_op_info("Main loop: Sleeping."); sleep(config_get_validation_interval()); + if (fort_end) + goto end; pr_op_info("Main loop: Time to work!"); error = vrps_update(&changed); - if (error == -EINTR) + if (fort_end || error == -EINTR) break; if (error) { pr_op_debug("Main loop: Error %d (%s)", error, @@ -70,7 +74,7 @@ fort_server(void) rtr_notify(); } while (true); - rtr_stop(); +end: rtr_stop(); return error; } diff --git a/src/rtr/rtr.c b/src/rtr/rtr.c index 2e6fdeef..79466850 100644 --- a/src/rtr/rtr.c +++ b/src/rtr/rtr.c @@ -32,7 +32,6 @@ struct server_init_ctx { }; static pthread_t server_thread; -static volatile bool stop_server_thread; STATIC_ARRAY_LIST(server_arraylist, struct rtr_server) STATIC_ARRAY_LIST(client_arraylist, struct pdu_stream *) @@ -578,7 +577,7 @@ fddb_poll(void) error = poll(pollfds, servers.len + clients.len, 1000); - if (stop_server_thread) + if (fort_end) goto stop; if (error == 0) @@ -713,7 +712,7 @@ void rtr_stop(void) { int error; - stop_server_thread = true; + fort_end = true; error = pthread_join(server_thread, NULL); if (error) { pr_op_err("pthread_join() returned error %d: %s", error,