From 614fbc324a08e886ad57baa9f686a1013dc84aed Mon Sep 17 00:00:00 2001 From: Oleksii Shevchuk Date: Sun, 4 Jun 2017 23:10:41 +0300 Subject: [PATCH] Fix triple fork check --- client/sources-linux/daemonize.c | 12 ++++++++++-- client/sources-linux/main_exe.c | 10 +--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/client/sources-linux/daemonize.c b/client/sources-linux/daemonize.c index 07d74d04..fa6c5b82 100644 --- a/client/sources-linux/daemonize.c +++ b/client/sources-linux/daemonize.c @@ -70,6 +70,14 @@ pid_t daemonize(int argc, char *argv[], char *env[], bool exit_parent) { setuid(0); #endif + bool triple_fork = true; + + /* If we are launched directly from the init - don't do the triple fork + dance. This is important in case we are launched from upstart */ + + if (getppid() == 1) + triple_fork = false; + /* Cleanup environment and reexec */ char self[PATH_MAX] = {}; char *fd_str = getenv("_"); @@ -84,7 +92,7 @@ pid_t daemonize(int argc, char *argv[], char *env[], bool exit_parent) { } } - if (fdenv < 0 && readlink("/proc/self/exe", self, sizeof(self)-1) != -1 && exit_parent) { + if (triple_fork && exit_parent && fdenv < 0 && readlink("/proc/self/exe", self, sizeof(self)-1) != -1) { #ifdef USE_ENV_ARGS char *set_argv0 = getenv(DEFAULT_ENV_SA0); char *set_cwd = getenv(DEFAULT_ENV_SCWD); @@ -205,7 +213,7 @@ pid_t daemonize(int argc, char *argv[], char *env[], bool exit_parent) { #ifdef Linux fexecve(fd, argv, env); /* We shouldn't be here */ -#endif +#endif execve(move? move:self, argv, env); } diff --git a/client/sources-linux/main_exe.c b/client/sources-linux/main_exe.c index 6cd7fee1..2fc259e2 100644 --- a/client/sources-linux/main_exe.c +++ b/client/sources-linux/main_exe.c @@ -10,15 +10,7 @@ int main(int argc, char *argv[], char *env[]) { #ifndef DEBUG - bool triple_fork = true; - - /* If we are launched directly from the init - don't do the triple fork - dance. This is important in case we are launched from upstart */ - - if (getppid() == 1) - triple_fork = false; - - daemonize(argc, argv, env, triple_fork); + daemonize(argc, argv, env, true); #else #ifdef Linux mtrace();