From 286b86d62c350a06f3486abd7a9e949b684e4e0c Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Thu, 24 Feb 2022 05:06:12 -0500 Subject: [PATCH] Preserve errno across signals. --- ndhc.c | 2 ++ scriptd.c | 11 +++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/ndhc.c b/ndhc.c index e4851e8..27d488d 100644 --- a/ndhc.c +++ b/ndhc.c @@ -149,6 +149,7 @@ void show_usage(void) static void signal_handler(int signo) { + int serrno = errno; switch (signo) { case SIGCHLD: { static const char errstr[] = "ndhc-master: Subprocess terminated unexpectedly. Exiting.\n"; @@ -161,6 +162,7 @@ static void signal_handler(int signo) case SIGUSR2: l_signal_release = 1; break; default: break; } + errno = serrno; } void signal_exit(int status) diff --git a/scriptd.c b/scriptd.c index 6512322..148c588 100644 --- a/scriptd.c +++ b/scriptd.c @@ -123,14 +123,13 @@ static void do_scriptd_work(void) static void signal_handler(int signo) { - switch (signo) { - case SIGCHLD: + int serrno = errno; + if (signo == SIGCHLD) { while (waitpid(-1, NULL, WNOHANG) > 0); - break; - case SIGINT: - case SIGTERM: _exit(EXIT_FAILURE); break; - default: break; + } else if (signo == SIGINT || signo == SIGTERM) { + _exit(EXIT_FAILURE); } + errno = serrno; } static void setup_signals_scriptd(void)