From 0786e5e8a79f814b6cf047ef21ad75ad2cc9ccb4 Mon Sep 17 00:00:00 2001 From: Oleksii Shevchuk Date: Fri, 9 Feb 2018 16:08:57 +0200 Subject: [PATCH] For PID=1 do just one fork --- client/sources-linux/daemonize.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/client/sources-linux/daemonize.c b/client/sources-linux/daemonize.c index fa6c5b82..24461e11 100644 --- a/client/sources-linux/daemonize.c +++ b/client/sources-linux/daemonize.c @@ -268,7 +268,7 @@ pid_t daemonize(int argc, char *argv[], char *env[], bool exit_parent) { } /* create new process */ - pid = fork ( ); + pid = fork(); if (pid == -1) return -1; @@ -285,18 +285,20 @@ pid_t daemonize(int argc, char *argv[], char *env[], bool exit_parent) { } } - /* Fork once again */ - pid = fork ( ); - if (pid == -1) { - if (!exit_parent) { - close(pipes[1]); + if (triple_fork) { + /* Fork once again */ + pid = fork(); + if (pid == -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);