From fc01a20bea97468fb481d45d57f28f500d1c3ac5 Mon Sep 17 00:00:00 2001 From: Alexander Bluhm Date: Sat, 18 Jul 2026 11:04:53 +0200 Subject: [PATCH] Signal handlers must preserve errno If a signal handler executes a system call, the errno may be modified. After return to the main program, this new errno does not reflect the error of the previously failed system call in the main context. Common pattern for signal handlers is to save and restore the errno. Signed-off-by: Alexander Bluhm --- src/script.c | 2 ++ src/timer.c | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/script.c b/src/script.c index fced3ac..acd4468 100644 --- a/src/script.c +++ b/src/script.c @@ -35,6 +35,7 @@ static pid_t script = 0; static void handler(int signo) { + int save_errno = errno; int status; pid_t pid = 1; @@ -55,6 +56,7 @@ static void handler(int signo) smclog(LOG_WARNING, "Script %s returned error: %d", exec, status); } } + errno = save_errno; } int script_init(char *script) diff --git a/src/timer.c b/src/timer.c index 56e0d27..b0d8e48 100644 --- a/src/timer.c +++ b/src/timer.c @@ -164,9 +164,12 @@ static void run(int sd, void *arg) /* write to pipe to create an event for select() on SIGALRM */ static void handler(int signo) { + int save_errno = errno; + (void)signo; if (write(timerfd[1], "!", 1) < 0) smclog(LOG_DEBUG, "Failed write(pipe): %s", strerror(errno)); + errno = save_errno; } /*