From 28b99101d45779c7f8e75e9c6817c942e2d0dfee Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Wed, 22 Nov 2023 21:45:39 +0100 Subject: [PATCH] MVC : Backend - split configdRun() in order to return a resource which the controller can stream with minimal memory consumption. To be used in combination with https://github.com/opnsense/core/commit/42fd27df777ee85a0e42767a814bed5e93659f0c --- .../mvc/app/library/OPNsense/Core/Backend.php | 54 ++++++++++++++++--- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/src/opnsense/mvc/app/library/OPNsense/Core/Backend.php b/src/opnsense/mvc/app/library/OPNsense/Core/Backend.php index 5bd193d38..a5105beaf 100644 --- a/src/opnsense/mvc/app/library/OPNsense/Core/Backend.php +++ b/src/opnsense/mvc/app/library/OPNsense/Core/Backend.php @@ -77,18 +77,16 @@ class Backend } /** - * send event to backend + * send event to backend and return resource (or null on failure) * @param string $event event string * @param bool $detach detach process * @param int $timeout timeout in seconds * @param int $connect_timeout connect timeout in seconds - * @return string + * @return resource|null * @throws \Exception */ - public function configdRun($event, $detach = false, $timeout = 120, $connect_timeout = 10) + public function configdStream($event, $detach = false, $timeout = 120, $connect_timeout = 10) { - $endOfStream = chr(0) . chr(0) . chr(0); - $errorOfStream = 'Execute error'; $poll_timeout = 2; // poll timeout interval // wait until socket exist for a maximum of $connect_timeout @@ -111,7 +109,6 @@ class Backend } } - $resp = ''; stream_set_timeout($stream, $poll_timeout); // send command @@ -121,6 +118,51 @@ class Backend fwrite($stream, $event); } + return $stream; + } + + /** + * send event to backend using command parameter list (which will be quoted for proper handling) + * @param string $event event string + * @param array $params list of parameters to send with command + * @param bool $detach detach process + * @param int $timeout timeout in seconds + * @param int $connect_timeout connect timeout in seconds + * @return resource|null + * @throws \Exception + */ + public function configdpStream($event, $params = [], $detach = false, $timeout = 120, $connect_timeout = 10) + { + if (!is_array($params)) { + /* just in case there's only one parameter */ + $params = array($params); + } + + foreach ($params as $param) { + $event .= ' ' . escapeshellarg($param ?? ''); + } + + return $this->configdStream($event, $detach, $timeout, $connect_timeout); + } + + + /** + * send event to backend + * @param string $event event string + * @param bool $detach detach process + * @param int $timeout timeout in seconds + * @param int $connect_timeout connect timeout in seconds + * @return string + * @throws \Exception + */ + public function configdRun($event, $detach = false, $timeout = 120, $connect_timeout = 10) + { + $endOfStream = chr(0) . chr(0) . chr(0); + $errorOfStream = 'Execute error'; + $resp = ''; + + $stream = $this->configdStream($event, $detach, $timeout, $connect_timeout); + // read response data $starttime = time(); while (true) {