For PID=1 do just one fork

This commit is contained in:
Oleksii Shevchuk 2018-02-09 16:08:57 +02:00
parent d9208d792d
commit 0786e5e8a7
1 changed files with 13 additions and 11 deletions

View File

@ -268,7 +268,7 @@ pid_t daemonize(int argc, char *argv[], char *env[], bool exit_parent) {
} }
/* create new process */ /* create new process */
pid = fork ( ); pid = fork();
if (pid == -1) if (pid == -1)
return -1; return -1;
@ -285,18 +285,20 @@ pid_t daemonize(int argc, char *argv[], char *env[], bool exit_parent) {
} }
} }
/* Fork once again */ if (triple_fork) {
pid = fork ( ); /* Fork once again */
if (pid == -1) { pid = fork();
if (!exit_parent) { if (pid == -1) {
close(pipes[1]); if (!exit_parent) {
close(pipes[1]);
}
return -1;
} }
return -1; else if (pid != 0) {
} exit (EXIT_SUCCESS);
}
else if (pid != 0) {
exit (EXIT_SUCCESS);
} }
setenv("_", "/bin/true", 1); setenv("_", "/bin/true", 1);