pid_t child_pid = -1;
static int child_fds[2];
-static unsigned child_should_run;
+static enum {
+ CH_STOPPED = 0,
+ CH_STARTING = 1,
+ CH_RUNNING = 2,
+ CH_STOPPING = 3,
+ CH_DIED = 4
+} child_state = CH_STOPPED;
struct evbase *mgt_evb;
struct ev *ev_poker;
return (1);
}
buf[i] = '\0';
- printf("Child said: <<%s>>\n", buf);
+ printf("Child said (%d, %d): <<%s>>\n", child_state, child_pid, buf);
return (0);
}
(void)e;
(void)what;
- if (!child_should_run)
+ if (child_state != CH_RUNNING)
return (1);
if (child_pid > 0 && mgt_cli_askchild(NULL, NULL, "ping\n"))
kill(child_pid, SIGKILL);
char *p;
struct ev *e;
- if (child_pid >= 0)
+ if (child_state != CH_STOPPED && child_state != CH_DIED)
return;
- child_should_run = 1;
+ child_state = CH_STARTING;
AZ(pipe(&heritage.fds[0]));
AZ(pipe(&heritage.fds[2]));
free(p);
exit (2);
}
+ child_state = CH_RUNNING;
}
/*--------------------------------------------------------------------*/
stop_child(void)
{
- if (child_pid < 0)
+ if (child_state != CH_RUNNING)
return;
+ child_state = CH_STOPPING;
+
if (ev_poker != NULL)
ev_del(mgt_evb, ev_poker);
ev_poker = NULL;
- child_should_run = 0;
-
printf("Clean child\n");
mgt_cli_stop_child();
AZ(close(heritage.fds[3]));
heritage.fds[3] = -1;
- printf("Child stopped\n");
+ printf("Child stopping\n");
}
/*--------------------------------------------------------------------*/
printf("Cache child died pid=%d status=0x%x\n", r, status);
child_pid = -1;
- if (child_should_run) {
+ if (child_state == CH_RUNNING) {
+ child_state = CH_DIED;
printf("Clean child\n");
mgt_cli_stop_child();
child_fds[0] = -1;
printf("Child cleaned\n");
- if (child_should_run)
+ if (child_state == CH_DIED)
start_child();
+ else if (child_state == CH_STOPPING)
+ child_state = CH_STOPPED;
return (0);
}
sac.sa_handler = SIG_IGN;
sac.sa_flags = SA_RESTART;
+
AZ(sigaction(SIGPIPE, &sac, NULL));
AZ(sigaction(SIGHUP, &sac, NULL));