[PATCH stalld 43/52] stalld: remove async-signal-unsafe calls from signal handler

Wander Lairson Costa <[email protected]> Mon, 8 Jun 2026 15:31:53 -0300
Newsgroups org.kernel.vger.linux-rt-users
Message-ID <[email protected]>
The inthandler signal handler calls log_msg(), which internally uses
fprintf, vsnprintf, and syslog. None of these functions are
async-signal-safe, so if a signal arrives while the main program holds
a libc internal lock the handler deadlocks trying to acquire the same
lock.

Remove the log_msg() call and let the handler only set the running
flag. Change running from int to volatile sig_atomic_t as required by
POSIX for variables shared between signal handlers and normal program
flow. Move the shutdown message to main() so it is emitted from a
safe context after the main loop exits.

Signed-off-by: Wander Lairson Costa <[email protected]>
---
 src/stalld.c | 4 +++-
 src/stalld.h | 3 ++-
 src/utils.c  | 1 -
 3 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/stalld.c b/src/stalld.c
index feb0099..a9e98ff 100644
--- a/src/stalld.c
+++ b/src/stalld.c
@@ -109,7 +109,7 @@ int boost_policy;
 /*
  * Variable to indicate if stalld is running or shutting down.
  */
-int running = 1;
+volatile sig_atomic_t running = 1;
 
 /*
  * Config single threaded: uses less CPU, but has a lower precision.
@@ -1247,6 +1247,8 @@ int main(int argc, char **argv)
 	else
 		single_threaded_main(cpus, config_nr_cpus);
 
+	log_msg("stalld shutting down\n");
+
 	cleanup_regex(&nr_thread_ignore, &compiled_regex_thread);
 	cleanup_regex(&nr_process_ignore, &compiled_regex_process);
 	if (config_log_syslog)
diff --git a/src/stalld.h b/src/stalld.h
index 03a4ca1..8b6ab8b 100644
--- a/src/stalld.h
+++ b/src/stalld.h
@@ -12,6 +12,7 @@
 #include <regex.h>
 #include <sched.h>
 #include <stdatomic.h>
+#include <signal.h>
 
 #define BUFFER_PAGES		10
 #define MAX_WAITING_PIDS	30
@@ -213,7 +214,7 @@ int check_dl_server_dir_exists(void);
 /*
  * Shared variables.
  */
-extern int running;
+extern volatile sig_atomic_t running;
 extern const char *version;
 extern int config_verbose;
 extern int config_write_kmesg;
diff --git a/src/utils.c b/src/utils.c
index 738d059..b3b17dd 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -169,7 +169,6 @@ long get_variable_long_value(char *buffer, const char *variable)
  */
 static void inthandler(int signo, siginfo_t *info, void *extra)
 {
-	log_msg("received signal %d, starting shutdown\n", signo);
 	running = 0;
 }
 
-- 
2.54.0