From d4ba645474580e1da5a1297ceab40d5544bf53f0 Mon Sep 17 00:00:00 2001 From: turret Date: Sun, 14 Jan 2024 20:54:28 -0600 Subject: init: register signal blockers earlier prevents potential race condition where a subsystem can terminate before we fully block SIGCHLD, causing SIGCHLD to be sent to init and having our entire process terminated ungracefully. prevents bash and family from sending "Terminated" due to our process receiving SIGTERM during a panic, since we already exit in there and it'd be queued up if we do receive one --- init/init.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'init/init.c') diff --git a/init/init.c b/init/init.c index 5387043..3fe370f 100644 --- a/init/init.c +++ b/init/init.c @@ -62,6 +62,8 @@ static void do_initcalls(void) } } + + int main(void) { print("init: Hello world! Running " NAME " v" VERSION "!"); @@ -78,6 +80,15 @@ int main(void) } free(stack_rlimit); + /* configure signal handlers early to prevent race condition where subsystems + can terminate main process on accident, and disable Terminated output during + early-mode panic */ + static sigset_t set; + sigaddset(&set, SIGCHLD); + sigaddset(&set, SIGINT); + sigaddset(&set, SIGTERM); + sigprocmask(SIG_BLOCK, &set, NULL); + /* fetch token */ char *token_base = getenv("TOKEN"); if(!token_base) @@ -99,13 +110,8 @@ int main(void) do_initcalls(); /* Reaper. Much like init. */ - siginfo_t siginfo; - static sigset_t set; - sigaddset(&set, SIGCHLD); - sigaddset(&set, SIGINT); - sigaddset(&set, SIGTERM); - sigprocmask(SIG_BLOCK, &set, NULL); + siginfo_t siginfo; while(subsystem_count > 0) { sigwaitinfo(&set, &siginfo); int sig = siginfo.si_signo; -- cgit v1.2.3