Project

General

Profile

Bug #2712 » daemonize_startup_behaviour_v2.diff

pasdVn, 2016-02-10 17:56

View differences:

src/server.c (working copy)
#endif
#ifdef HAVE_FORK
static void daemonize(void) {
static int daemonize() {
int pipefd[2];
pid_t pid;
#ifdef SIGTTOU
signal(SIGTTOU, SIG_IGN);
#endif
......
#ifdef SIGTSTP
signal(SIGTSTP, SIG_IGN);
#endif
if (0 != fork()) exit(0);
if (pipe(pipefd) < 0) exit(-1);
if (0 > (pid = fork())) exit(-1);
else if (0 < pid)
{
char buf;
ssize_t bytes;
close(pipefd[1]);
/* parent waits for grandchild to be ready */
bytes = read(pipefd[0], &buf, sizeof(buf));
close(pipefd[0]);
/* closed fd (without writing) == failure in grandchild */
exit((bytes > 0) ? 0 : -1);
return 0; /* never reached */
}
close(pipefd[0]);
/* better to return -1 in the following error cases? */
if (-1 == setsid()) exit(0);
signal(SIGHUP, SIG_IGN);
......
if (0 != fork()) exit(0);
if (0 != chdir("/")) exit(0);
return pipefd[1];
}
#endif
......
interval.it_value.tv_usec = 0;
#endif
#ifdef HAVE_FORK
int parent_pipe_fd = 0;
#endif
/* for nice %b handling in strfime() */
setlocale(LC_TIME, "C");
......
#ifdef HAVE_FORK
/* network is up, let's deamonize ourself */
if (srv->srvconf.dont_daemonize == 0) daemonize();
if (srv->srvconf.dont_daemonize == 0) {
parent_pipe_fd = daemonize();
}
#endif
......
return -1;
}
#ifdef HAVE_FORK
/**
* notify daemonize-grandparent of successfull startup
* do this before any further forking is done (workers)
*/
if (srv->srvconf.dont_daemonize == 0) {
if (0 > write(parent_pipe_fd, "\x0", 1)) return -1;
close(parent_pipe_fd);
}
/* start watcher and workers */
num_childs = srv->srvconf.max_worker;
if (num_childs > 0) {
(2-2/2)