From ebf79db79eaa2df0ceb62b4edbef94e68a28b612 Mon Sep 17 00:00:00 2001
From: William Hubbs <w.d.hubbs@gmail.com>
Date: Fri, 30 Nov 2018 18:07:16 -0600
Subject: supervise-daemon: mark a service failed if it respawns too many times

---
 src/rc/supervise-daemon.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

(limited to 'src')

diff --git a/src/rc/supervise-daemon.c b/src/rc/supervise-daemon.c
index d93d31f8..c2082100 100644
--- a/src/rc/supervise-daemon.c
+++ b/src/rc/supervise-daemon.c
@@ -446,6 +446,7 @@ static void supervisor(char *exec, char **argv)
 	char buf[2048];
 	char cmd[2048];
 	int count;
+	int failing;
 	int health_status;
 	int healthcheck_respawn;
 	int i;
@@ -498,6 +499,7 @@ static void supervisor(char *exec, char **argv)
 	else if (healthchecktimer)
 		alarm(healthchecktimer);
  fifo_fd = open(fifopath, O_RDONLY |O_NONBLOCK);
+ failing = 0;
 	while (!exiting) {
 		healthcheck_respawn = 0;
 		wait_pid = waitpid(child_pid, &i, WNOHANG);
@@ -576,6 +578,7 @@ static void supervisor(char *exec, char **argv)
 				syslog(LOG_WARNING, "respawned \"%s\" too many times, exiting",
 						exec);
 				exiting = 1;
+				failing = 1;
 				continue;
 			}
 			ts.tv_sec = respawn_delay;
@@ -602,16 +605,18 @@ static void supervisor(char *exec, char **argv)
 		}
 	}
 
-	if (pidfile && exists(pidfile))
-		unlink(pidfile);
-	if (fifopath && exists(fifopath))
-		unlink(fifopath);
 	if (svcname) {
 		rc_service_daemon_set(svcname, exec, (const char *const *)argv,
 				pidfile, false);
-		rc_service_mark(svcname, RC_SERVICE_STOPPED);
 		rc_service_value_set(svcname, "child_pid", NULL);
+		rc_service_mark(svcname, RC_SERVICE_STOPPED);
+		if (failing)
+			rc_service_mark(svcname, RC_SERVICE_FAILED);
 	}
+	if (pidfile && exists(pidfile))
+		unlink(pidfile);
+	if (fifopath && exists(fifopath))
+		unlink(fifopath);
 	exit(EXIT_SUCCESS);
 }
 
-- 
cgit v1.2.3