From 8e84bdf4f5ceda3df1777a682e5b3b9398714c56 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Fri, 16 Nov 2018 17:23:33 +0100 Subject: [PATCH 1/2] icingacli web serve: preserve console colors and avoid redundand process --- application/clicommands/WebCommand.php | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/application/clicommands/WebCommand.php b/application/clicommands/WebCommand.php index 7277a1fd4..8e4306ce8 100644 --- a/application/clicommands/WebCommand.php +++ b/application/clicommands/WebCommand.php @@ -55,20 +55,12 @@ class WebCommand extends Command $this->forkAndExit(); } echo "Serving Icinga Web 2 from directory $documentRoot and listening on $socket\n"; - $cmd = sprintf( - '%s -S %s -t %s %s', - readlink('/proc/self/exe'), - $socket, - $documentRoot, - Icinga::app()->getLibraryDir('/Icinga/Application/webrouter.php') - ); // TODO: Store webserver log, switch uid, log index.php includes, pid file - if ($fork) { - exec($cmd); - } else { - passthru($cmd); - } + pcntl_exec( + readlink('/proc/self/exe'), + ['-S', $socket, '-t', $documentRoot, Icinga::app()->getLibraryDir('/Icinga/Application/webrouter.php')] + ); } public function stopAction() From 59708bb0f71c72bd06e1e30ed68e8bea6759f65d Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Fri, 10 May 2019 15:19:53 +0200 Subject: [PATCH 2/2] icingacli web serve: redirect standard I/O to /dev/null after fork(2) --- application/clicommands/WebCommand.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/application/clicommands/WebCommand.php b/application/clicommands/WebCommand.php index 8e4306ce8..67d50a343 100644 --- a/application/clicommands/WebCommand.php +++ b/application/clicommands/WebCommand.php @@ -85,6 +85,17 @@ class WebCommand extends Command // pcntl_wait($status); } else { // child + + // Replace console with /dev/null by first freeing the (lowest possible) FDs 0, 1 and 2 + // and then opening /dev/null once for every one of them (open(2) chooses the lowest free FD). + + fclose(STDIN); + fclose(STDOUT); + fclose(STDERR); + + fopen('/dev/null', 'rb'); + fopen('/dev/null', 'wb'); + fopen('/dev/null', 'wb'); } } }