supervise-daemon: fix busy loop

This fixes #264.
This commit is contained in:
William Hubbs 2018-12-05 18:11:40 -06:00
parent 9dae4f2e38
commit bebc604438

View File

@ -184,13 +184,22 @@ static void re_exec_supervisor(void)
static void handle_signal(int sig) static void handle_signal(int sig)
{ {
int serrno = errno; int serrno = errno;
pid_t pid;
switch (sig) { switch (sig) {
case SIGALRM: case SIGALRM:
do_healthcheck = 1; do_healthcheck = 1;
break; break;
case SIGCHLD: case SIGCHLD:
if (exiting)
while (waitpid((pid_t)(-1), NULL, WNOHANG) > 0) {} while (waitpid((pid_t)(-1), NULL, WNOHANG) > 0) {}
else {
while ((pid = waitpid((pid_t)(-1), NULL, WNOHANG|WNOWAIT)) > 0) {
if (pid == child_pid)
break;
pid = waitpid(pid, NULL, WNOHANG);
}
}
break; break;
case SIGTERM: case SIGTERM:
exiting = 1; exiting = 1;
@ -451,6 +460,7 @@ static void supervisor(char *exec, char **argv)
int healthcheck_respawn; int healthcheck_respawn;
int i; int i;
int nkilled; int nkilled;
int ready;
int sig_send; int sig_send;
pid_t health_pid; pid_t health_pid;
pid_t wait_pid; pid_t wait_pid;
@ -498,18 +508,18 @@ static void supervisor(char *exec, char **argv)
alarm(healthcheckdelay); alarm(healthcheckdelay);
else if (healthchecktimer) else if (healthchecktimer)
alarm(healthchecktimer); alarm(healthchecktimer);
fifo_fd = open(fifopath, O_RDONLY |O_NONBLOCK);
failing = 0; failing = 0;
while (!exiting) { while (!exiting) {
healthcheck_respawn = 0; healthcheck_respawn = 0;
wait_pid = waitpid(child_pid, &i, WNOHANG); fifo_fd = open(fifopath, O_RDONLY);
if (fifo_fd > 0) {
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
if (fifo_fd >= 0) {
count = read(fifo_fd, buf, sizeof(buf) - 1); count = read(fifo_fd, buf, sizeof(buf) - 1);
close(fifo_fd);
if (count != -1) if (count != -1)
buf[count] = 0; buf[count] = 0;
} if (count == 0)
if (strlen(buf) > 0) { continue;
syslog(LOG_DEBUG, "Received %s from fifo", buf); syslog(LOG_DEBUG, "Received %s from fifo", buf);
if (strncasecmp(buf, "sig", 3) == 0) { if (strncasecmp(buf, "sig", 3) == 0) {
if ((sscanf(buf, "%s %d", cmd, &sig_send) == 2) if ((sscanf(buf, "%s %d", cmd, &sig_send) == 2)
@ -521,6 +531,7 @@ static void supervisor(char *exec, char **argv)
sig_send, child_pid); sig_send, child_pid);
} }
} }
continue;
} }
if (do_healthcheck) { if (do_healthcheck) {
do_healthcheck = 0; do_healthcheck = 0;
@ -553,6 +564,7 @@ static void supervisor(char *exec, char **argv)
syslog(LOG_INFO, "killed %d processes", nkilled); syslog(LOG_INFO, "killed %d processes", nkilled);
continue; continue;
} }
wait_pid = waitpid(child_pid, &i, WNOHANG);
if (wait_pid == child_pid) { if (wait_pid == child_pid) {
if (WIFEXITED(i)) if (WIFEXITED(i))
syslog(LOG_WARNING, "%s, pid %d, exited with return code %d", syslog(LOG_WARNING, "%s, pid %d, exited with return code %d",