summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2022-01-31 19:24:54 +0100
committerLeah Neukirchen <leah@vuxu.org>2022-01-31 19:46:26 +0100
commit6af2be1353faff4a257425620419da3f77327b83 (patch)
tree202c8afb775f1332d67d17a6bbf8199531042395
parente6508c84c88c8b6db9e8c04ec387a70ce79d4a40 (diff)
downloadrvnit-6af2be1353faff4a257425620419da3f77327b83.tar.gz
rvnit-6af2be1353faff4a257425620419da3f77327b83.tar.xz
rvnit-6af2be1353faff4a257425620419da3f77327b83.zip
move want_reboot into want_shutdown, put under services_lock during runtime
-rwxr-xr-xrvnitbin76160 -> 76368 bytes
-rw-r--r--rvnit.c59
2 files changed, 40 insertions, 19 deletions
diff --git a/rvnit b/rvnit
index 730af55..1e0d702 100755
--- a/rvnit
+++ b/rvnit
Binary files differdiff --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