aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorturret <turret@duck.com>2024-01-14 20:54:28 -0600
committerturret <turret@duck.com>2024-01-14 20:54:28 -0600
commitd4ba645474580e1da5a1297ceab40d5544bf53f0 (patch)
tree2c1116437c215d2cfa926ddea6a09874478f7b2c
parentbb357be604b64d489b6015f4c7466be51a748929 (diff)
downloaddiscord-bot-skeleton-d4ba645474580e1da5a1297ceab40d5544bf53f0.tar.gz
discord-bot-skeleton-d4ba645474580e1da5a1297ceab40d5544bf53f0.tar.bz2
discord-bot-skeleton-d4ba645474580e1da5a1297ceab40d5544bf53f0.zip
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
-rw-r--r--init/init.c18
1 files changed, 12 insertions, 6 deletions
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;