]> granicus.if.org Git - php/commitdiff
Switch to using shell-less proc_open() in various server tests
authorNikita Popov <nikita.ppv@gmail.com>
Mon, 1 Jul 2019 11:07:30 +0000 (13:07 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Thu, 11 Jul 2019 13:48:10 +0000 (15:48 +0200)
ext/curl/tests/server.inc
ext/opcache/tests/php_cli_server.inc
ext/soap/tests/bug73037.phpt
ext/soap/tests/custom_content_type.phpt
sapi/cli/tests/php_cli_server.inc
sapi/cli/tests/upload_2G.phpt
sapi/fpm/tests/main-global-prefix.phpt
sapi/fpm/tests/tester.inc
tests/basic/bug67198.phpt

index 68a5bde8a801d73f46fb82ac77432cbe0d12cfe4..00eeb5ff766833b612302ee764ce9e6c332df64a 100644 (file)
@@ -12,30 +12,13 @@ function curl_cli_server_start() {
     $php_executable = getenv('TEST_PHP_EXECUTABLE');
     $doc_root = __DIR__;
     $router = "responder/get.inc";
-
-    if (substr(PHP_OS, 0, 3) == 'WIN') {
-        $descriptorspec = array(
-            0 => STDIN,
-            1 => STDOUT,
-            2 => array("pipe", "w"),
-        );
-
-        $cmd = "{$php_executable} -t {$doc_root} -n -S " . PHP_CURL_SERVER_ADDRESS;
-        $cmd .= " {$router}";
-        $handle = proc_open(addslashes($cmd), $descriptorspec, $pipes, $doc_root, NULL, array("bypass_shell" => true,  "suppress_errors" => true));
-    } else {
-        $descriptorspec = array(
-            0 => STDIN,
-            1 => STDOUT,
-            2 => STDERR,
-        );
-
-        $cmd = "exec {$php_executable} -t {$doc_root} -n -S " . PHP_CURL_SERVER_ADDRESS;
-        $cmd .= " {$router}";
-        $cmd .= " 2>/dev/null";
-
-        $handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
-    }
+    $cmd = [$php_executable, '-t', $doc_root, '-n', '-S', PHP_CURL_SERVER_ADDRESS, $router];
+    $descriptorspec = array(
+        0 => STDIN,
+        1 => STDOUT,
+        2 => array("null"),
+    );
+    $handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root, null, array("suppress_errors" => true));
 
     // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
     //       it might not be listening yet...need to wait until fsockopen() call returns
index 70ef14e585877fa058811dc52c119c16048e4d62..8367715728e129b2c412d408ec366574110cdd39 100644 (file)
@@ -7,25 +7,14 @@ function php_cli_server_start($ini = "") {
        $php_executable = getenv('TEST_PHP_EXECUTABLE');
        $doc_root = __DIR__;
 
-       if (substr(PHP_OS, 0, 3) == 'WIN') {
-               $descriptorspec = array(
-                       0 => STDIN,
-                       1 => STDOUT,
-                       2 => array("pipe", "w"),
-               );
-
-               $cmd = "{$php_executable} -t {$doc_root} $ini -S " . PHP_CLI_SERVER_ADDRESS;
-               $handle = proc_open(addslashes($cmd), $descriptorspec, $pipes, $doc_root, NULL, array("bypass_shell" => true,  "suppress_errors" => true));
-       } else {
-               $descriptorspec = array(
-                       0 => STDIN,
-                       1 => STDOUT,
-                       2 => STDERR,
-               );
-
-               $cmd = "exec {$php_executable} -t {$doc_root} $ini -S " . PHP_CLI_SERVER_ADDRESS . " 2>/dev/null";
-               $handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
-       }
+       $ini_array = preg_split('/\s+/', trim($ini));
+       $cmd = [$php_executable, '-t', $doc_root, '-n', ...$ini_array, '-S', PHP_CLI_SERVER_ADDRESS];
+       $descriptorspec = array(
+               0 => STDIN,
+               1 => STDOUT,
+               2 => array("null"),
+       );
+       $handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root, null, array("suppress_errors" => true));
 
        // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
        //       it might not be listening yet...need to wait until fsockopen() call returns
index 7f49a843f5289e56c4dc96e11781215b34b7341e..897aec89d0517895ec59af9aa720e80de12fd131 100644 (file)
@@ -63,7 +63,8 @@ function get_data($max)
 }
 
 $router = "bug73037_server.php";
-$args = substr(PHP_OS, 0, 3) == 'WIN' ? "-d extension_dir=" . ini_get("extension_dir") . " -d extension=php_soap.dll" : "";
+$args = substr(PHP_OS, 0, 3) == 'WIN'
+    ? ["-d", "extension_dir=" . ini_get("extension_dir"), "-d", "extension=php_soap.dll"] : [];
 $code = <<<'PHP'
 $s = new SoapServer(NULL, array('uri' => 'http://here'));
 $s->setObject(new stdclass());
index a0ee164053a1601195d3641d46889dd551de468a..ef95be4ffc554d6b83130114140a4974682f73d9 100644 (file)
@@ -15,13 +15,14 @@ server
 
 include __DIR__ . "/../../../sapi/cli/tests/php_cli_server.inc";
 
-$args = substr(PHP_OS, 0, 3) == 'WIN' ? "-d extension_dir=" . ini_get("extension_dir") . " -d extension=php_soap.dll" : "";
+$args = substr(PHP_OS, 0, 3) == 'WIN'
+    ? ["-d", "extension_dir=" . ini_get("extension_dir"), "-d", "extension=php_soap.dll"] : [];
 $code = <<<'PHP'
 /* Receive */
 $content = trim(file_get_contents("php://input")) . PHP_EOL;
 PHP;
 
-php_cli_server_start($code, false, $args);
+php_cli_server_start($code, null, $args);
 
 $client = new soapclient(NULL, [
   'location' => 'http://' . PHP_CLI_SERVER_ADDRESS,
index 6421978a37400f8b66513a86251db0d851cadd24..679a55eed4c02181d19ff46857f4f252ee103e77 100644 (file)
@@ -3,7 +3,11 @@ define ("PHP_CLI_SERVER_HOSTNAME", "localhost");
 define ("PHP_CLI_SERVER_PORT", 8964);
 define ("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_PORT);
 
-function php_cli_server_start($code = 'echo "Hello world";', $router = 'index.php', $cmd_args = null) {
+function php_cli_server_start(
+       ?string $code = 'echo "Hello world";',
+       ?string $router = 'index.php',
+       array $cmd_args = []
+) {
        $php_executable = getenv('TEST_PHP_EXECUTABLE');
        $doc_root = __DIR__;
     $error = null;
@@ -12,35 +16,18 @@ function php_cli_server_start($code = 'echo "Hello world";', $router = 'index.ph
                file_put_contents($doc_root . '/' . ($router ?: 'index.php'), '<?php ' . $code . ' ?>');
        }
 
-       if (substr(PHP_OS, 0, 3) == 'WIN') {
-               $descriptorspec = array(
-                       0 => STDIN,
-                       1 => STDOUT,
-                       2 => array("pipe", "w"),
-               );
-
-               $cmd = "{$php_executable} -t {$doc_root} -n {$cmd_args} -S " . PHP_CLI_SERVER_ADDRESS;
-               if (!is_null($router)) {
-                       $cmd .= " {$router}";
-               }
-
-               $handle = proc_open(addslashes($cmd), $descriptorspec, $pipes, $doc_root, NULL, array("bypass_shell" => true,  "suppress_errors" => true));
-       } else {
-               $descriptorspec = array(
-                       0 => STDIN,
-                       1 => STDOUT,
-                       2 => STDERR,
-               );
-
-               $cmd = "exec {$php_executable} -t {$doc_root} -n {$cmd_args} -S " . PHP_CLI_SERVER_ADDRESS;
-               if (!is_null($router)) {
-                       $cmd .= " {$router}";
-               }
-               $cmd .= " 2>/dev/null";
-
-               $handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
+       $cmd = [$php_executable, '-t', $doc_root, '-n', ...$cmd_args, '-S', PHP_CLI_SERVER_ADDRESS];
+       if (!is_null($router)) {
+               $cmd[] = $router;
        }
 
+       $descriptorspec = array(
+               0 => STDIN,
+               1 => STDOUT,
+               2 => array("null"),
+       );
+       $handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root, null, array("suppress_errors" => true));
+
     // note: here we check the process is running
     for ($i=0; $i < 120; $i++) {
         $status = proc_get_status($handle);
index a4bcc6860f716ed29ab4cf6fa5536299fe527c1d..44d2da4a81fe66a43c6e58c6a71e11e08c50af95 100644 (file)
@@ -37,8 +37,8 @@ echo "Test\n";
 
 include "php_cli_server.inc";
 
-php_cli_server_start("var_dump(\$_FILES);", false,
-       "-d post_max_size=3G -d upload_max_filesize=3G");
+php_cli_server_start("var_dump(\$_FILES);", null,
+       ["-d", "post_max_size=3G", "-d", "upload_max_filesize=3G"]);
 
 list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
 $port = intval($port)?:80;
index 710e688c40864916ef0ea5d3392a77871e61aff7..44486e4ed3288ae57c739c1cbf4056cb0b22f2b1 100644 (file)
@@ -27,7 +27,7 @@ EOT;
 
 $prefix = __DIR__;
 $tester = new FPM\Tester($cfg);
-$tester->start('--prefix ' . $prefix);
+$tester->start(['--prefix', $prefix]);
 $tester->expectLogStartNotices();
 $tester->expectFile(FPM\Tester::FILE_EXT_LOG_ACC, $prefix);
 $tester->expectFile(FPM\Tester::FILE_EXT_LOG_ERR, $prefix);
index 70c03ad70f1c52b545271d30dd115d346a23416f..12fe3dc2de8a87d69d8857617c76522240cc0688 100644 (file)
@@ -320,25 +320,21 @@ class Tester
     /**
      * Start PHP-FPM master process
      *
-     * @param string $extraArgs
+     * @param array $extraArgs
      * @return bool
      * @throws \Exception
      */
-    public function start(string $extraArgs = '')
+    public function start(array $extraArgs = [])
     {
         $configFile = $this->createConfig();
-        $desc = $this->outDesc ? [] : [1 => array('pipe', 'w')];
-        $asRoot = getenv('TEST_FPM_RUN_AS_ROOT') ? '--allow-to-run-as-root' : '';
-        $cmd = self::findExecutable() . " $asRoot -F -O -y $configFile $extraArgs";
-        /* Since it's not possible to spawn a process under linux without using a
-         * shell in php (why?!?) we need a little shell trickery, so that we can
-         * actually kill php-fpm */
-        $this->masterProcess = proc_open(
-            "killit () { kill \$child 2> /dev/null; }; " .
-                "trap killit TERM; $cmd 2>&1 & child=\$!; wait",
-            $desc,
-            $pipes
-        );
+        $desc = $this->outDesc ? [] : [1 => array('pipe', 'w'), 2 => array('redirect', 1)];
+        $cmd = [self::findExecutable(), '-F', '-O', '-y', $configFile];
+        if (getenv('TEST_FPM_RUN_AS_ROOT')) {
+            $cmd[] = '--allow-to-run-as-root';
+        }
+        $cmd = array_merge($cmd, $extraArgs);
+
+        $this->masterProcess = proc_open($cmd, $desc, $pipes);
         register_shutdown_function(
             function($masterProcess) use($configFile) {
                 @unlink($configFile);
index 184916197b878caa73c85887c6148f2f496da72b..3f5e2a348c91c83cc2ec1abb7eb32648edb027c8 100644 (file)
@@ -35,7 +35,9 @@ $opts = array('http' =>
 
 $context  = stream_context_create($opts);
 
-php_cli_server_start("exit(file_get_contents('php://input'));", false, "-d enable_post_data_reading=Off");
+php_cli_server_start(
+    "exit(file_get_contents('php://input'));", null,
+    ["-d", "enable_post_data_reading=Off"]);
 
 var_dump(file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS, false, $context));
 var_dump(file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS, false, $context));