diff --git a/config.h b/config.h index 5725340..91f8486 100644 --- a/config.h +++ b/config.h @@ -31,8 +31,8 @@ /* Init scripts */ #ifdef INIT -char *INIT_POWEROFF[] = {"/etc/rc.poweroff"}; -char *INIT_START[] = {"/etc/rc.init"}; +char *INIT_POWEROFF[] = {"/etc/rc.poweroff", NULL}; +char *INIT_START[] = {"/etc/rc.init", NULL}; #define INIT_MSG "Starting micro-init..." #endif diff --git a/configs/rc.poweroff b/configs/rc.poweroff index 68adbf3..b5f747e 100644 --- a/configs/rc.poweroff +++ b/configs/rc.poweroff @@ -1,12 +1,12 @@ #!/bin/sh -echo "Closing processes..." -kill -a -s TERM -sleep 5 - echo "Umounting..." umount /proc umount /sys umount /dev umount /tmp +echo "Closing processes..." +kill -a -s TERM +sleep 5 + echo "Poweroff..." diff --git a/src/init/init/init.c b/src/init/init/init.c index e84b947..7c19ea1 100644 --- a/src/init/init/init.c +++ b/src/init/init/init.c @@ -11,31 +11,24 @@ void poweroff(int sig) { printf("Syncing...\n"); + sync(); + sleep(5); if (sig == SIGUSR1) reboot(RB_POWER_OFF); - else if (sig == SIGKILL) + else if (sig == SIGUSR2) reboot(RB_AUTOBOOT); } -void execute(char *argv[], sigset_t *set) { +void execute(char *argv[]) { pid_t pid; if ((pid = fork()) == 0) { - sigprocmask(SIG_UNBLOCK, set, NULL); setsid(); execvp(argv[0], argv); - fprintf(stderr, "init: %s\n", strerror(errno)); - exit(1); - } - - int status = 0; - waitpid(pid, &status, 0); - - if (status != 0) { - fprintf(stderr, "init: Init script returned %d\nRebooting...\n", status); + fprintf(stderr, "init: Failed to run %s\nRebooting...\n", argv[0]); poweroff(SIGKILL); } } @@ -49,17 +42,21 @@ int main(void) { chdir("/"); printf("%s\n", INIT_MSG); - /* Start main service */ sigset_t set; + sigemptyset(&set); + sigaddset(&set, SIGUSR2); + sigaddset(&set, SIGUSR1); + + /* Start main service */ sigprocmask(SIG_BLOCK, &set, NULL); - execute(INIT_START, &set); + execute(INIT_START); /* Sig handler */ int sig = 0; while (1) { if (sigwait(&set, &sig) == 0) - if (sig == SIGKILL || sig == SIGUSR1) { - execute(INIT_POWEROFF, &set); + if (sig == SIGUSR2 || sig == SIGUSR1) { + execute(INIT_POWEROFF); poweroff(sig); } } diff --git a/src/init/init/reboot b/src/init/init/reboot index c6b922b..3612dd2 100644 --- a/src/init/init/reboot +++ b/src/init/init/reboot @@ -1,2 +1,2 @@ #!/bin/sh -kill -s KILL 1 +kill -s USR2 1