- Improve error handling when daemonizing

This commit is contained in:
Babak Farrokhi 2015-09-16 12:14:02 +04:30
parent 7506f09907
commit 01d25ecb39

View File

@ -197,6 +197,7 @@ daemon_start()
struct sigaction sig_action;
sigset_t sig_set;
pid_t otherpid;
int curPID;
tot = &ift;
@ -218,10 +219,18 @@ daemon_start()
}
warn("Cannot open or create pidfile: %s", pid_filename);
}
/* fork ourselves if not asked otherwise */
if (fork()) {
/* start daemonizing */
curPID = fork();
switch (curPID) {
case 0: /* This process is the child */
break;
case -1: /* fork() failed, should exit */
return (EXIT_FAILURE);
default: /* fork() successful, should exit */
return (EXIT_SUCCESS);
}
/* we are the child, complete the daemonization */
/* Close standard IO */
@ -250,7 +259,8 @@ daemon_start()
sigaction(SIGINT, &sig_action, NULL);
/* create new session and process group */
setsid();
if (setsid() < 0)
return (EXIT_FAILURE);
/* persist pid */
pidfile_write(pfh);