From a091370e612b79452ac882e299d0e85154a64b59 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 21 Mar 2025 23:46:04 +0100 Subject: misc stuff MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/signals.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) (limited to 'src/signals.c') diff --git a/src/signals.c b/src/signals.c index 3487d23..390d684 100644 --- a/src/signals.c +++ b/src/signals.c @@ -25,8 +25,8 @@ volatile sig_atomic_t disable = 0; /** - * Signal handlar for exit signals (SIGINT, SIGTERM, SIGQUIT) - * + * Signal handler for exit signals (SIGINT, SIGTERM, SIGQUIT) + * * @param signo The received signal */ static void @@ -41,8 +41,8 @@ sigexit(int signo) /** - * Signal handlar for disable signal (SIGUSR1) - * + * Signal handler for disable signal (SIGUSR1) + * * @param signo The received signal */ #ifndef WINDOWS @@ -55,6 +55,24 @@ sigdisable(int signo) #endif +/** + * Signal handler for forceful exiting; installed by + * `install_forceful_exit_signal_handlers` + * + * @param signo The received signal + */ +#ifndef WINDOWS +static void +sigalrm(int signo) +{ + if (exiting || signo == SIGALRM) + exit(0); + exiting = 1; + alarm(1U); +} +#endif + + void install_signal_handlers(void) { @@ -90,3 +108,28 @@ install_signal_handlers(void) eprintf("sigaction SIGCHLD &{.sa_handler=SIG_IGN, .sa_mask={}, .sa_flags=0} NULL:"); #endif } + + +#ifndef WINDOWS +void +install_forceful_exit_signal_handlers(void) +{ + struct sigaction sigact; + sigset_t sigset; + + exiting = 0; + memset(&sigact, 0, sizeof(sigact)); + sigemptyset(&sigset); + sigact.sa_mask = sigset; + sigact.sa_flags = 0; + sigact.sa_handler = &sigalrm; + if (sigaction(SIGINT, &sigact, NULL)) + eprintf("sigaction SIGINT &{.sa_handler=, .sa_mask={}, .sa_flags=0} NULL:"); + if (sigaction(SIGTERM, &sigact, NULL)) + eprintf("sigaction SIGTERM &{.sa_handler=, .sa_mask={}, .sa_flags=0} NULL:"); + if (sigaction(SIGQUIT, &sigact, NULL)) + eprintf("sigaction SIGQUIT &{.sa_handler=, .sa_mask={}, .sa_flags=0} NULL:"); + if (sigaction(SIGALRM, &sigact, NULL)) + eprintf("sigaction SIGALRM &{.sa_handler=, .sa_mask={}, .sa_flags=0} NULL:"); +} +#endif -- cgit v1.2.3-70-g09d2