From 3b31e082970210459fffac36781c829890ea8f11 Mon Sep 17 00:00:00 2001 From: Josef Schlehofer Date: Mon, 10 Aug 2026 10:09:13 +0200 Subject: [PATCH] Exit with the intended status before the signal handler is installed olsr_exit() ends the process with raise(SIGTERM) so that the orderly shutdown in olsr_shutdown() runs, and falls through to exit(val) "in case the signal handler was not setup yet". That fall-through is unreachable: raise() with the default disposition terminates the process, so exit(val) never executes. main() calls olsr_exit() twenty times before signal(SIGTERM, olsr_shutdown) is installed, so all of those paths report 143 instead of the value they pass: $ olsrd -v; echo $? *** olsr.org - pre-0.9.9-git_3653c47-hash_426688c *** Terminated: 15 143 $ olsrd -f /nonexistent.conf; echo $? Terminated: 15 143 The second one matters beyond cosmetics: a configuration error is indistinguishable from a successful run for anything that checks the exit status, and EXIT_FAILURE never reaches the caller. Record whether the handler has been installed and only take the signal path once it has. With this, -v and -h exit 0 and the early error paths exit with EXIT_FAILURE; behaviour after the handler is installed is unchanged. Signed-off-by: Josef Schlehofer --- src/main.c | 2 ++ src/olsr.c | 10 +++++++++- src/olsr.h | 2 ++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index 6e6635956..8141f8de6 100644 --- a/src/main.c +++ b/src/main.c @@ -771,6 +771,8 @@ int main(int argc, char *argv[]) { signal(SIGUSR2, SIG_IGN); #endif /* _WIN32 */ + olsr_shutdown_registered = true; + /* Starting scheduler */ olsr_scheduler(); diff --git a/src/olsr.c b/src/olsr.c index 216996228..986044c1b 100644 --- a/src/olsr.c +++ b/src/olsr.c @@ -81,6 +81,8 @@ bool changes_neighborhood; bool changes_hna; bool changes_force; +bool olsr_shutdown_registered = false; + /*COLLECT startup sleeps caused by warnings*/ #ifdef OLSR_COLLECT_STARTUP_SLEEP @@ -576,7 +578,13 @@ olsr_exit(const char *msg, int val) olsr_cnf->exit_value = val; } - raise(SIGTERM); + /* Only take the orderly shutdown path once olsr_shutdown() is + * installed. Before that, SIGTERM still has its default disposition + * and raise() would kill us outright, so the exit() below would never + * run and the process would report 143 instead of val. */ + if (olsr_shutdown_registered) { + raise(SIGTERM); + } /* in case the signal handler was not setup yet */ exit(val); diff --git a/src/olsr.h b/src/olsr.h index 505231691..755228432 100644 --- a/src/olsr.h +++ b/src/olsr.h @@ -54,6 +54,8 @@ extern bool changes_neighborhood; extern bool changes_hna; extern bool changes_force; +extern bool olsr_shutdown_registered; + extern union olsr_ip_addr all_zero; void get_argc_argv(int *argc, char **argv[]);