Skip to content

Commit 17f7fb7

Browse files
committed
Switch to using shell-less proc_open() in various server tests
1 parent 6285bb5 commit 17f7fb7

File tree

9 files changed

+51
-92
lines changed

9 files changed

+51
-92
lines changed

ext/curl/tests/server.inc

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,13 @@ function curl_cli_server_start() {
1212
$php_executable = getenv('TEST_PHP_EXECUTABLE');
1313
$doc_root = __DIR__;
1414
$router = "responder/get.inc";
15-
16-
if (substr(PHP_OS, 0, 3) == 'WIN') {
17-
$descriptorspec = array(
18-
0 => STDIN,
19-
1 => STDOUT,
20-
2 => array("pipe", "w"),
21-
);
22-
23-
$cmd = "{$php_executable} -t {$doc_root} -n -S " . PHP_CURL_SERVER_ADDRESS;
24-
$cmd .= " {$router}";
25-
$handle = proc_open(addslashes($cmd), $descriptorspec, $pipes, $doc_root, NULL, array("bypass_shell" => true, "suppress_errors" => true));
26-
} else {
27-
$descriptorspec = array(
28-
0 => STDIN,
29-
1 => STDOUT,
30-
2 => STDERR,
31-
);
32-
33-
$cmd = "exec {$php_executable} -t {$doc_root} -n -S " . PHP_CURL_SERVER_ADDRESS;
34-
$cmd .= " {$router}";
35-
$cmd .= " 2>/dev/null";
36-
37-
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
38-
}
15+
$cmd = [$php_executable, '-t', $doc_root, '-n', '-S', PHP_CURL_SERVER_ADDRESS, $router];
16+
$descriptorspec = array(
17+
0 => STDIN,
18+
1 => STDOUT,
19+
2 => array("null"),
20+
);
21+
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root, null, array("suppress_errors" => true));
3922

4023
// note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
4124
// it might not be listening yet...need to wait until fsockopen() call returns

ext/opcache/tests/php_cli_server.inc

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,14 @@ function php_cli_server_start($ini = "") {
77
$php_executable = getenv('TEST_PHP_EXECUTABLE');
88
$doc_root = __DIR__;
99

10-
if (substr(PHP_OS, 0, 3) == 'WIN') {
11-
$descriptorspec = array(
12-
0 => STDIN,
13-
1 => STDOUT,
14-
2 => array("pipe", "w"),
15-
);
16-
17-
$cmd = "{$php_executable} -t {$doc_root} $ini -S " . PHP_CLI_SERVER_ADDRESS;
18-
$handle = proc_open(addslashes($cmd), $descriptorspec, $pipes, $doc_root, NULL, array("bypass_shell" => true, "suppress_errors" => true));
19-
} else {
20-
$descriptorspec = array(
21-
0 => STDIN,
22-
1 => STDOUT,
23-
2 => STDERR,
24-
);
25-
26-
$cmd = "exec {$php_executable} -t {$doc_root} $ini -S " . PHP_CLI_SERVER_ADDRESS . " 2>/dev/null";
27-
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
28-
}
10+
$ini_array = preg_split('/\s+/', trim($ini));
11+
$cmd = [$php_executable, '-t', $doc_root, '-n', ...$ini_array, '-S', PHP_CLI_SERVER_ADDRESS];
12+
$descriptorspec = array(
13+
0 => STDIN,
14+
1 => STDOUT,
15+
2 => array("null"),
16+
);
17+
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root, null, array("suppress_errors" => true));
2918

3019
// note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
3120
// it might not be listening yet...need to wait until fsockopen() call returns

ext/soap/tests/bug73037.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ function get_data($max)
6363
}
6464

6565
$router = "bug73037_server.php";
66-
$args = substr(PHP_OS, 0, 3) == 'WIN' ? "-d extension_dir=" . ini_get("extension_dir") . " -d extension=php_soap.dll" : "";
66+
$args = substr(PHP_OS, 0, 3) == 'WIN'
67+
? ["-d", "extension_dir=" . ini_get("extension_dir"), "-d", "extension=php_soap.dll"] : [];
6768
$code = <<<'PHP'
6869
$s = new SoapServer(NULL, array('uri' => 'http://here'));
6970
$s->setObject(new stdclass());

ext/soap/tests/custom_content_type.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ server
1515

1616
include __DIR__ . "/../../../sapi/cli/tests/php_cli_server.inc";
1717

18-
$args = substr(PHP_OS, 0, 3) == 'WIN' ? "-d extension_dir=" . ini_get("extension_dir") . " -d extension=php_soap.dll" : "";
18+
$args = substr(PHP_OS, 0, 3) == 'WIN'
19+
? ["-d", "extension_dir=" . ini_get("extension_dir"), "-d", "extension=php_soap.dll"] : [];
1920
$code = <<<'PHP'
2021
/* Receive */
2122
$content = trim(file_get_contents("php://input")) . PHP_EOL;
2223
PHP;
2324

24-
php_cli_server_start($code, false, $args);
25+
php_cli_server_start($code, null, $args);
2526

2627
$client = new soapclient(NULL, [
2728
'location' => 'http://' . PHP_CLI_SERVER_ADDRESS,

sapi/cli/tests/php_cli_server.inc

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ define ("PHP_CLI_SERVER_HOSTNAME", "localhost");
33
define ("PHP_CLI_SERVER_PORT", 8964);
44
define ("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_PORT);
55

6-
function php_cli_server_start($code = 'echo "Hello world";', $router = 'index.php', $cmd_args = null) {
6+
function php_cli_server_start(
7+
?string $code = 'echo "Hello world";',
8+
?string $router = 'index.php',
9+
array $cmd_args = []
10+
) {
711
$php_executable = getenv('TEST_PHP_EXECUTABLE');
812
$doc_root = __DIR__;
913
$error = null;
@@ -12,35 +16,18 @@ function php_cli_server_start($code = 'echo "Hello world";', $router = 'index.ph
1216
file_put_contents($doc_root . '/' . ($router ?: 'index.php'), '<?php ' . $code . ' ?>');
1317
}
1418

15-
if (substr(PHP_OS, 0, 3) == 'WIN') {
16-
$descriptorspec = array(
17-
0 => STDIN,
18-
1 => STDOUT,
19-
2 => array("pipe", "w"),
20-
);
21-
22-
$cmd = "{$php_executable} -t {$doc_root} -n {$cmd_args} -S " . PHP_CLI_SERVER_ADDRESS;
23-
if (!is_null($router)) {
24-
$cmd .= " {$router}";
25-
}
26-
27-
$handle = proc_open(addslashes($cmd), $descriptorspec, $pipes, $doc_root, NULL, array("bypass_shell" => true, "suppress_errors" => true));
28-
} else {
29-
$descriptorspec = array(
30-
0 => STDIN,
31-
1 => STDOUT,
32-
2 => STDERR,
33-
);
34-
35-
$cmd = "exec {$php_executable} -t {$doc_root} -n {$cmd_args} -S " . PHP_CLI_SERVER_ADDRESS;
36-
if (!is_null($router)) {
37-
$cmd .= " {$router}";
38-
}
39-
$cmd .= " 2>/dev/null";
40-
41-
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
19+
$cmd = [$php_executable, '-t', $doc_root, '-n', ...$cmd_args, '-S', PHP_CLI_SERVER_ADDRESS];
20+
if (!is_null($router)) {
21+
$cmd[] = $router;
4222
}
4323

24+
$descriptorspec = array(
25+
0 => STDIN,
26+
1 => STDOUT,
27+
2 => array("null"),
28+
);
29+
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root, null, array("suppress_errors" => true));
30+
4431
// note: here we check the process is running
4532
for ($i=0; $i < 120; $i++) {
4633
$status = proc_get_status($handle);

sapi/cli/tests/upload_2G.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ echo "Test\n";
3737

3838
include "php_cli_server.inc";
3939

40-
php_cli_server_start("var_dump(\$_FILES);", false,
41-
"-d post_max_size=3G -d upload_max_filesize=3G");
40+
php_cli_server_start("var_dump(\$_FILES);", null,
41+
["-d", "post_max_size=3G", "-d", "upload_max_filesize=3G"]);
4242

4343
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
4444
$port = intval($port)?:80;

sapi/fpm/tests/main-global-prefix.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ EOT;
2727

2828
$prefix = __DIR__;
2929
$tester = new FPM\Tester($cfg);
30-
$tester->start('--prefix ' . $prefix);
30+
$tester->start(['--prefix', $prefix]);
3131
$tester->expectLogStartNotices();
3232
$tester->expectFile(FPM\Tester::FILE_EXT_LOG_ACC, $prefix);
3333
$tester->expectFile(FPM\Tester::FILE_EXT_LOG_ERR, $prefix);

sapi/fpm/tests/tester.inc

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -320,25 +320,21 @@ class Tester
320320
/**
321321
* Start PHP-FPM master process
322322
*
323-
* @param string $extraArgs
323+
* @param array $extraArgs
324324
* @return bool
325325
* @throws \Exception
326326
*/
327-
public function start(string $extraArgs = '')
327+
public function start(array $extraArgs = [])
328328
{
329329
$configFile = $this->createConfig();
330-
$desc = $this->outDesc ? [] : [1 => array('pipe', 'w')];
331-
$asRoot = getenv('TEST_FPM_RUN_AS_ROOT') ? '--allow-to-run-as-root' : '';
332-
$cmd = self::findExecutable() . " $asRoot -F -O -y $configFile $extraArgs";
333-
/* Since it's not possible to spawn a process under linux without using a
334-
* shell in php (why?!?) we need a little shell trickery, so that we can
335-
* actually kill php-fpm */
336-
$this->masterProcess = proc_open(
337-
"killit () { kill \$child 2> /dev/null; }; " .
338-
"trap killit TERM; $cmd 2>&1 & child=\$!; wait",
339-
$desc,
340-
$pipes
341-
);
330+
$desc = $this->outDesc ? [] : [1 => array('pipe', 'w'), 2 => array('redirect', 1)];
331+
$cmd = [self::findExecutable(), '-F', '-O', '-y', $configFile];
332+
if (getenv('TEST_FPM_RUN_AS_ROOT')) {
333+
$cmd[] = '--allow-to-run-as-root';
334+
}
335+
$cmd = array_merge($cmd, $extraArgs);
336+
337+
$this->masterProcess = proc_open($cmd, $desc, $pipes);
342338
register_shutdown_function(
343339
function($masterProcess) use($configFile) {
344340
@unlink($configFile);

tests/basic/bug67198.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ $opts = array('http' =>
3535

3636
$context = stream_context_create($opts);
3737

38-
php_cli_server_start("exit(file_get_contents('php://input'));", false, "-d enable_post_data_reading=Off");
38+
php_cli_server_start(
39+
"exit(file_get_contents('php://input'));", null,
40+
["-d", "enable_post_data_reading=Off"]);
3941

4042
var_dump(file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS, false, $context));
4143
var_dump(file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS, false, $context));

0 commit comments

Comments
 (0)