From 1da9e52fe56f0ee34a4ee9eb0f981a87e8e1c984 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Thu, 6 Jan 2022 19:36:27 +0100 Subject: revamp signal handling, use SIGCONT to wake up main thread support reboot via command --- rvnit.c | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/rvnit.c b/rvnit.c index 4c9505c..74af39b 100644 --- a/rvnit.c +++ b/rvnit.c @@ -50,27 +50,29 @@ pthread_t logger_thread; int selflogfd[2]; -sig_atomic_t got_intr; -sig_atomic_t got_sigusr1; +sig_atomic_t want_shutdown; +sig_atomic_t want_rescan; + +int want_reboot = 0; void on_sigint(int sig) { (void)sig; - got_intr = 1; + want_shutdown = 1; } void -on_sigusr1(int sig) +on_sigusr2(int sig) { (void)sig; - got_sigusr1 = 1; } void -on_sigusr2(int sig) +on_sigcont(int sig) { (void)sig; + /* do nothing, but interrupt the system call */ } void @@ -249,12 +251,14 @@ socket_loop(void* ignored) printf("setting %s down\n", services[i].name); services[i].state = DOWN; - pthread_kill(main_thread, SIGUSR1); + want_rescan = 1; + pthread_kill(main_thread, SIGCONT); } else if (cmd == 'u') { printf("setting %s up\n", services[i].name); services[i].state = UP; - pthread_kill(main_thread, SIGUSR1); + want_rescan = 1; + pthread_kill(main_thread, SIGCONT); } else if (charsig(cmd) && services[i].pid > 0) { printf("sending signal %d to %s\n", charsig(cmd), services[i].name); kill(services[i].pid, charsig(cmd)); @@ -264,7 +268,13 @@ socket_loop(void* ignored) } if (cmd == 'X') { - pthread_kill(main_thread, SIGPWR); + want_shutdown = 1; + pthread_kill(main_thread, SIGCONT); + } + if (cmd == 'R') { + want_shutdown = 1; + want_reboot = 1; + pthread_kill(main_thread, SIGCONT); } close(connfd); @@ -574,16 +584,15 @@ main() } sigaction(SIGINT, &(struct sigaction){.sa_handler=on_sigint}, 0); - sigaction(SIGPWR, &(struct sigaction){.sa_handler=on_sigint}, 0); - sigaction(SIGUSR1, &(struct sigaction){.sa_handler=on_sigusr1}, 0); + sigaction(SIGCONT, &(struct sigaction){.sa_handler=on_sigcont}, 0); LOG("system up"); while (1) { - if (got_intr) + if (want_shutdown) break; - if (got_sigusr1) { + if (want_rescan) { printf("rescanning state\n"); for (i = 0; i < MAX_SV; i++) { if (services[i].name[2] == 'D' || @@ -597,7 +606,7 @@ main() } } - got_sigusr1 = 0; + want_rescan = 0; } int status = 0; @@ -743,7 +752,11 @@ fatal: ; // arrives with level < 99 } } - dprintf(selflogfd[1], "shutdown\n"); + if (want_reboot) + dprintf(selflogfd[1], "reboot\n"); + else + dprintf(selflogfd[1], "shutdown\n"); + close(selflogfd[1]); pthread_join(logger_thread, 0); } -- cgit 1.4.1