From 6ef360978933655dd67aef9e4b8369aa8431fd74 Mon Sep 17 00:00:00 2001 From: Matheus Date: Thu, 5 Sep 2024 18:25:08 -0300 Subject: [PATCH] main: kill other processes --- tools/init/main.c | 48 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/tools/init/main.c b/tools/init/main.c index 265df002..aa1cd595 100644 --- a/tools/init/main.c +++ b/tools/init/main.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "common.h" @@ -34,14 +35,49 @@ static void sendacpicommand(char c) { sleep(1); } +static bool alarmfired; +static void timeout(int signal) { + alarmfired = true; +} + +static void waitforall(bool timeout) { + alarmfired = false; + if (timeout) { + alarm(5); + } + + int err = 0; + errno = 0; + do { + pid_t pid = waitpid(-1, NULL, WNOHANG); + int err = errno; + if (pid == 0) + sleep(1); + } while (errno != ECHILD && alarmfired == false); +} + static void preparetodie(void) { printf("init: bringing system down\n"); - //printf("init: sending SIGTERM to all processes\n"); - // TODO kill(-1, SIGTERM); - // TODO wait 5 seconds or until all children have died - //printf("init: sending SIGKILL to all processes\n"); - // TODO kill(-1, SIGKILL); - // TODO wait until all children have died + + // preapre alarm + struct sigaction sa; + memset(&sa, 0, sizeof(sigaction)); + sa.sa_handler = timeout; + sigaction(SIGALRM, &sa, NULL); + + // unblock sigalarm for timeout + sigset_t signals; + sigemptyset(&signals); + sigaddset(&signals, SIGALRM); + sigprocmask(SIG_UNBLOCK, &signals, NULL); + + printf("init: sending SIGTERM to all processes\n"); + kill(-1, SIGTERM); + waitforall(true); + + printf("init: sending SIGKILL to all processes\n"); + kill(-1, SIGKILL); + waitforall(false); printf("init: syncing disks\n"); sync();