diff options
author | stilez <stilez@users.noreply.github.com> | 2015-07-14 09:08:54 +0100 |
---|---|---|
committer | stilez <stilez@users.noreply.github.com> | 2015-07-14 09:08:54 +0100 |
commit | f0b415481d7dc35176ab5bd90497ce51d6d10faa (patch) | |
tree | fb1277d7e6afb9a9d7785307f38909a237d0610e | |
parent | 82921c72fa346730140aeb774ad56908f1df8f5c (diff) | |
download | pfsense-f0b415481d7dc35176ab5bd90497ce51d6d10faa.zip pfsense-f0b415481d7dc35176ab5bd90497ce51d6d10faa.tar.gz |
mwexec_bg() and mwexec() - transparent change
Slight cleanup with two effects:
1) a bit easier to follow
2) background execution returns PID of started process, which may be very helpful to the calling code if it has to track and control it (eg started processes or "wait....." screens displayed in the graphical UI).
No external effects (totally transparent change with no effect on wider platform code)
This is prompted by (and needed for) work I'm doing on the packet capture page, where it becomes important to track the PID of the background tcpdump (as "ps -axx | grep" used at present won't be sufficient). Since background processes started with mwexec_bg() aren't currently expected to return a value, this doesn't have any wider impact or change anything else, but this change will enable the started process pid to be tracked by calling code in future, when it's useful to do so.
Also cleans up some artifacts: explicit unset() of local variables (usually) isn't needed prior to return, and it doesn't need the redundant $_gb = exec(...) or $garbage = exec(...) as exec() can be run as a statement without using its return value.
-rw-r--r-- | etc/inc/util.inc | 53 |
1 files changed, 19 insertions, 34 deletions
diff --git a/etc/inc/util.inc b/etc/inc/util.inc index 169f784..fd75281 100644 --- a/etc/inc/util.inc +++ b/etc/inc/util.inc @@ -1420,58 +1420,43 @@ function exec_command($command) { return(implode("\n", $output)); } -/* wrapper for exec() */ -function mwexec($command, $mute = false, $clearsigmask = false) { +/* wrapper for exec() + Executes in background or foreground. + For background execution, returns PID of background process to allow calling code control */ +function mwexec($command, $nologentry = false, $clearsigmask = false, $background = false) { global $g; - + $outputarray = array(); + $retval = 0; + if ($g['debug']) { if (!$_SERVER['REMOTE_ADDR']) { - echo "mwexec(): $command\n"; + echo "mwexec(): $command" . ($background ? " [BG]":"") . "\n"; } } - $oarr = array(); - $retval = 0; - if ($clearsigmask) { $oldset = array(); pcntl_sigprocmask(SIG_SETMASK, array(), $oldset); } - $garbage = exec("$command 2>&1", $oarr, $retval); + + if ($background) + $retval = exec("/usr/bin/nohup $command > /dev/null 2>&1 & echo $!"); // start background process and return PID + else { + exec("$command 2>&1", $outputarray, $retval); + $output = implode(" ", $outputarray); + if (($retval <> 0) && (!$nologentry || isset($config['system']['developerspew']))) + log_error(sprintf(gettext("The command '%1\$s' returned exit code '%2\$d', the output was '%3\$s' "), $command, $retval, $output)); + } + if ($clearsigmask) { pcntl_sigprocmask(SIG_SETMASK, $oldset); } - if (isset($config['system']['developerspew'])) { - $mute = false; - } - if (($retval <> 0) && ($mute === false)) { - $output = implode(" ", $oarr); - log_error(sprintf(gettext("The command '%1\$s' returned exit code '%2\$d', the output was '%3\$s' "), $command, $retval, $output)); - unset($output); - } - unset($oarr); return $retval; } /* wrapper for exec() in background */ function mwexec_bg($command, $clearsigmask = false) { - global $g; - - if ($g['debug']) { - if (!$_SERVER['REMOTE_ADDR']) { - echo "mwexec(): $command\n"; - } - } - - if ($clearsigmask) { - $oldset = array(); - pcntl_sigprocmask(SIG_SETMASK, array(), $oldset); - } - $_gb = exec("/usr/bin/nohup $command > /dev/null 2>&1 &"); - if ($clearsigmask) { - pcntl_sigprocmask(SIG_SETMASK, $oldset); - } - unset($_gb); + return mwexec($command, false, $clearsigmask, true); } /* unlink a file, if it exists */ |