From 6af2be1353faff4a257425620419da3f77327b83 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Mon, 31 Jan 2022 19:24:54 +0100 Subject: move want_reboot into want_shutdown, put under services_lock during runtime --- rvnit | Bin 76160 -> 76368 bytes rvnit.c | 59 ++++++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/rvnit b/rvnit index 730af55..1e0d702 100755 Binary files a/rvnit and b/rvnit differ diff --git a/rvnit.c b/rvnit.c index 0846457..066dcbd 100644 --- a/rvnit.c +++ b/rvnit.c @@ -68,7 +68,6 @@ int real_pid1; int use_global_log; volatile sig_atomic_t want_shutdown; -volatile sig_atomic_t want_reboot; int want_rescan; int want_rescandir; @@ -77,9 +76,10 @@ on_signal(int sig) { switch (sig) { case SIGINT: - if (real_pid1) /* Linux Ctrl-Alt-Delete */ - want_reboot = 1; - want_shutdown = 1; + if (real_pid1) + want_shutdown = 2; /* Linux Ctrl-Alt-Delete */ + else + want_shutdown = 1; break; case SIGTERM: want_shutdown = 1; @@ -337,13 +337,16 @@ socket_loop(void* ignored) if (cmd == 'S') { write(connfd, "ok\n", 3); + pthread_mutex_lock(&services_lock); want_shutdown = 1; + pthread_mutex_unlock(&services_lock); pthread_kill(main_thread, SIGCONT); } if (cmd == 'R') { write(connfd, "ok\n", 3); - want_shutdown = 1; - want_reboot = 1; + pthread_mutex_lock(&services_lock); + want_shutdown = 2; + pthread_mutex_unlock(&services_lock); pthread_kill(main_thread, SIGCONT); } @@ -776,12 +779,17 @@ main(int argc, char *argv[]) } int i; - for (level = 0; level < 100 && !want_shutdown; level++) { + for (level = 0; level < 100; level++) { /* spawn all of level */ int oneshot = 0; pthread_mutex_lock(&services_lock); + if (want_shutdown) { + pthread_mutex_unlock(&services_lock); + break; + } + // spawn loggers first for (i = 0; i < MAX_SV; i++) { if (services[i].level != level) @@ -814,19 +822,20 @@ main(int argc, char *argv[]) pthread_mutex_unlock(&services_lock); - while (oneshot && !want_shutdown) { + while (oneshot) { int status = 0; int pid = wait(&status); - if (pid < 0) { - if (errno == ECHILD) - break; - else - continue; - } + if (pid < 0 && errno == ECHILD) + break; pthread_mutex_lock(&services_lock); + if (want_shutdown) { + pthread_mutex_unlock(&services_lock); + break; + } + int i = reap(pid, status); if (i < 0) goto cont1; @@ -868,9 +877,14 @@ cont1: LOG("system up"); - while (!want_shutdown) { + while (1) { pthread_mutex_lock(&services_lock); + if (want_shutdown) { + pthread_mutex_unlock(&services_lock); + break; + } + if (want_rescandir) { rescandir(); want_rescandir = 0; @@ -915,7 +929,7 @@ cont1: want_rescan = 0; } - pthread_mutex_lock(&services_lock); + pthread_mutex_unlock(&services_lock); int status = 0; errno = 0; @@ -951,13 +965,20 @@ cont2: if (pid1) own_console(); + sigaction(SIGINT, &(struct sigaction){ + .sa_handler=SIG_IGN, .sa_mask=allset }, 0); + sigaction(SIGTERM, &(struct sigaction){ + .sa_handler=SIG_IGN, .sa_mask=allset }, 0); + sigaction(SIGCONT, &(struct sigaction){ + .sa_handler=SIG_IGN, .sa_mask=allset }, 0); + #ifdef __linux__ reboot(RB_ENABLE_CAD); #endif LOG("shutting down"); - if (want_reboot) { + if (want_shutdown == 2) { static char rvnit_want_reboot[] = "RVNIT_WANT_REBOOT=1"; putenv(rvnit_want_reboot); } @@ -1093,7 +1114,7 @@ cont4: } } - if (want_reboot) + if (want_shutdown == 2) dprintf(selflogfd[1], "reboot\n"); else dprintf(selflogfd[1], "shutdown\n"); @@ -1106,7 +1127,7 @@ cont4: #ifdef __linux__ if (pid1) { sync(); - if (want_reboot) { + if (want_shutdown == 2) { reboot(RB_AUTOBOOT); } else { // falls back to RB_HALT_SYSTEM if not possible -- cgit 1.4.1