From 1b66209e53239daef2b470b522906f42d9e1929b Mon Sep 17 00:00:00 2001 From: Alexei Sorokin Date: Tue, 10 Sep 2019 21:14:27 +0300 Subject: [PATCH] [DAEMONS] Switch daemons to double-forking for daemonisation --- lib/util/daemon.php | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/util/daemon.php b/lib/util/daemon.php index 25097b0ea8..fa1e47cfcd 100644 --- a/lib/util/daemon.php +++ b/lib/util/daemon.php @@ -46,16 +46,26 @@ class Daemon // Database connection will likely get lost after forking $this->resetDb(); - $pid = pcntl_fork(); - if ($pid < 0) { // error - common_log(LOG_ERR, "Could not fork."); - return false; - } elseif ($pid > 0) { // parent - common_log(LOG_INFO, "Successfully forked."); - exit(0); - } else { // child - return true; + // Double-forking. + foreach (['single', 'double'] as $v) { + switch ($pid = pcntl_fork()) { + case -1: // error + common_log(LOG_ERR, 'Could not fork.'); + return false; + case 0: // child + if ($v === 'single') { + posix_setsid(); + } + break; + default: // parent + if ($v === 'double') { + common_log(LOG_INFO, 'Successfully forked.'); + } + die(); + } } + + return true; } public function alreadyRunning()