From 413ef2082f5e872447d5ec05682d1211c1663c87 Mon Sep 17 00:00:00 2001 From: laf Date: Tue, 4 Mar 2014 12:52:36 +0000 Subject: [PATCH] Updated external_exec to use proc_open --- includes/common.php | 37 ++++++++++++++++++++++++++-- includes/discovery/functions.inc.php | 2 +- includes/polling/functions.inc.php | 2 +- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/includes/common.php b/includes/common.php index 2a7129db37..747316f795 100644 --- a/includes/common.php +++ b/includes/common.php @@ -21,10 +21,43 @@ function format_number_short($number, $sf) function external_exec($command) { - global $debug; + global $debug , $exec_response; + + $exec_response = array('command' => $command); if ($debug) { echo($command."\n"); } - $output = shell_exec($command); + + $descriptorspec = array( + 0 => array('pipe', 'r'), // stdin + 1 => array('pipe', 'w'), // stdout + 2 => array('pipe', 'w') // stderr + ); + + $process = proc_open($command, $descriptorspec, $pipes); + stream_set_blocking($pipes[2], 0); + if (is_resource($process)) + { + $exec_response['error'] = stream_get_contents($pipes[2]); + if ($exec_response['error']) + { + $output = FALSE; + } else { + $output = stream_get_contents($pipes[1]); + } + fclose($pipes[0]); + fclose($pipes[1]); + fclose($pipes[2]); + $exec_response['status'] = proc_close($process); + } else { + fclose($pipes[0]); + fclose($pipes[1]); + fclose($pipes[2]); + proc_terminate($process); + $output = FALSE; + $exec_error['error'] = ''; + $exec_response['status'] = -1; + } + if ($debug) { echo($output."\n"); } return $output; diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index f2b1427e05..3b4a0cd3e1 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -47,7 +47,7 @@ function discover_new_device($hostname) function discover_device($device, $options = NULL) { - global $config, $valid; + global $config, $valid, $exec_response; $valid = array(); // Reset $valid array diff --git a/includes/polling/functions.inc.php b/includes/polling/functions.inc.php index cca6720791..c95a1c5aae 100644 --- a/includes/polling/functions.inc.php +++ b/includes/polling/functions.inc.php @@ -89,7 +89,7 @@ function poll_sensor($device, $class, $unit) function poll_device($device, $options) { - global $config, $device, $polled_devices, $db_stats, $memcache; + global $config, $device, $polled_devices, $db_stats, $memcache, $exec_response; $attribs = get_dev_attribs($device['device_id']);