Project

General

Profile

Bug #2712 » daemonize_startup_behaviour.diff

pasdVn, 2016-01-31 19:27

View differences:

src/server.c (Arbeitskopie)
}
}
#elif defined(HAVE_SIGNAL) || defined(HAVE_SIGACTION)
static void signal_handler(int sig) {
static int signal_handler(int sig) {
switch (sig) {
case SIGTERM: srv_shutdown = 1; break;
case SIGINT:
......
#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 */
}
/* 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
......
}
}
#ifdef HAVE_FORK
if (srv->srvconf.dont_daemonize == 0) {
/* notify daemonize-grandparent of successfull startup */
if (0 > write(parent_pipe_fd, "\x0", 1)) return -1;
close(parent_pipe_fd);
}
#endif
/* main-loop */
while (!srv_shutdown) {
int n;
(1-1/2)