Skip to content

Commit ad1a7af

Browse files
committed
exec -> proc_open
1 parent e824a75 commit ad1a7af

12 files changed

Lines changed: 116 additions & 66 deletions

‎ext/user_cache/tests/user_cache_apache2handler_dynamic_boundary.phpt‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ if ($httpd === false || $module === false) {
1111
if (!is_file($httpd) || !is_executable($httpd)) die('skip TEST_PHP_APACHE2HANDLER_HTTPD is not executable');
1212
if (!is_file($module)) die('skip TEST_PHP_APACHE2HANDLER_MODULE is not a file');
1313

14-
$builtIn = shell_exec(escapeshellarg($httpd) . ' -l 2>/dev/null') ?: '';
14+
$list = proc_open([$httpd, '-l'], [1 => ['pipe', 'w'], 2 => ['null']], $listPipes);
15+
$builtIn = stream_get_contents($listPipes[1]) ?: '';
16+
fclose($listPipes[1]);
17+
proc_close($list);
1518
if (str_contains($builtIn, 'mod_vhost_alias.c')) return;
1619

1720
$candidates = [
@@ -120,7 +123,10 @@ $module = getenv('TEST_PHP_APACHE2HANDLER_MODULE');
120123
$moduleName = getenv('TEST_PHP_APACHE2HANDLER_MODULE_NAME') ?: 'php_module';
121124
$extraConfig = getenv('TEST_PHP_APACHE2HANDLER_EXTRA_CONFIG') ?: '';
122125
$ldPreload = getenv('TEST_PHP_APACHE2HANDLER_LD_PRELOAD');
123-
$builtIn = shell_exec(escapeshellarg($httpd) . ' -l 2>/dev/null') ?: '';
126+
$list = proc_open([$httpd, '-l'], [1 => ['pipe', 'w'], 2 => ['null']], $listPipes);
127+
$builtIn = stream_get_contents($listPipes[1]) ?: '';
128+
fclose($listPipes[1]);
129+
proc_close($list);
124130
$vhostAliasLoad = '';
125131
if (!str_contains($builtIn, 'mod_vhost_alias.c')) {
126132
$candidates = [

‎ext/user_cache/tests/user_cache_eviction_policies.phpt‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
--TEST--
22
UserCache\Cache: user_cache.eviction_policy selects lru, clear or none behavior
3+
--SKIPIF--
4+
<?php
5+
if (!function_exists('proc_open')) die('skip proc_open() not available');
6+
?>
37
--INI--
48
user_cache.enable=1
59
user_cache.enable_cli=1
610
user_cache.shm_size=4M
711
--FILE--
812
<?php
9-
$php = escapeshellarg(getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY);
10-
$args = '-n -d user_cache.enable=1 -d user_cache.enable_cli=1 -d user_cache.shm_size=4M';
11-
/* Run the child from a file: escapeshellarg() on Windows strips the double
12-
* quotes and percent signs the inline code would need. */
13-
$child = __DIR__ . '/user_cache_eviction_policies_child.php';
14-
file_put_contents($child, <<<'CODE'
15-
<?php
13+
$php = getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY;
14+
$args = ['-n', '-d', 'user_cache.enable=1', '-d', 'user_cache.enable_cli=1', '-d', 'user_cache.shm_size=4M'];
15+
$code = <<<'CODE'
1616
$cache = UserCache\Cache::getPool('p');
1717
$blob = str_repeat('x', 8192);
1818
$ok = 0;
@@ -39,16 +39,14 @@ printf(
3939
(int) ($status->getStoreFailureCount() > 0),
4040
(int) $intact
4141
);
42-
CODE);
42+
CODE;
4343

4444
foreach (['lru', 'clear', 'none'] as $policy) {
45-
echo $policy, ': ', shell_exec(
46-
"$php $args -d user_cache.eviction_policy=$policy " . escapeshellarg($child)
47-
);
45+
$process = proc_open([$php, ...$args, '-d', "user_cache.eviction_policy=$policy", '-r', $code], [1 => ['pipe', 'w']], $pipes);
46+
echo $policy, ': ', stream_get_contents($pipes[1]);
47+
fclose($pipes[1]);
48+
proc_close($process);
4849
}
49-
50-
/* Unlink here instead of --CLEAN-- so --repeat runs keep this test. */
51-
unlink($child);
5250
?>
5351
--EXPECTF--
5452
lru: stored=600 full=1 evicted=1 wiped=0 failed=0 intact=1

‎ext/user_cache/tests/user_cache_ini_shm_size_floor.phpt‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22
UserCache\Cache: the minimum shm_size reported by the startup warning does not depend on shm_size and is exactly the smallest segment that initializes with room for one block
33
--SKIPIF--
44
<?php
5-
if (!function_exists('shell_exec')) die('skip shell_exec() not available');
5+
if (!function_exists('proc_open')) die('skip proc_open() not available');
66
?>
77
--INI--
88
user_cache.enable=1
99
user_cache.enable_cli=1
1010
--FILE--
1111
<?php
12-
$php = escapeshellarg(getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY);
13-
$args = '-n -d display_errors=1 -d display_startup_errors=1 -d error_reporting=E_ALL -d user_cache.enable=1 -d user_cache.enable_cli=1';
14-
$probe = escapeshellarg('echo UserCache\\Cache::getStatus()->getAvailability()->name;');
12+
$php = getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY;
13+
$args = ['-n', '-d', 'display_errors=1', '-d', 'display_startup_errors=1', '-d', 'error_reporting=E_ALL', '-d', 'user_cache.enable=1', '-d', 'user_cache.enable_cli=1'];
14+
$probe = 'echo UserCache\\Cache::getStatus()->getAvailability()->name;';
1515
$run = function (int $size) use ($php, $args, $probe): string {
16-
return trim(shell_exec("$php $args -d user_cache.shm_size=$size -r $probe 2>&1"));
16+
$process = proc_open([$php, ...$args, '-d', "user_cache.shm_size=$size", '-r', $probe], [1 => ['pipe', 'w'], 2 => ['redirect', 1]], $pipes);
17+
$output = stream_get_contents($pipes[1]);
18+
fclose($pipes[1]);
19+
proc_close($process);
20+
21+
return trim($output);
1722
};
1823

1924
preg_match('/minimum cache layout \((\d+) bytes\)/', $run(16), $m);

‎ext/user_cache/tests/user_cache_ini_startup.phpt‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ UserCache\Cache: startup sizing follows entries_hint and shm_size, invalid direc
33
--SKIPIF--
44
<?php
55
if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only");
6-
if (!function_exists('shell_exec')) die('skip shell_exec() not available');
6+
if (!function_exists('proc_open')) die('skip proc_open() not available');
77
?>
88
--INI--
99
user_cache.enable=1
@@ -12,10 +12,15 @@ user_cache.shm_size=16M
1212
user_cache.entries_hint=1000
1313
--FILE--
1414
<?php
15-
$php = escapeshellarg(getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY);
16-
$args = '-n -d display_errors=1 -d display_startup_errors=1 -d error_reporting=E_ALL -d user_cache.enable=1 -d user_cache.enable_cli=1';
15+
$php = getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY;
16+
$args = ['-n', '-d', 'display_errors=1', '-d', 'display_startup_errors=1', '-d', 'error_reporting=E_ALL', '-d', 'user_cache.enable=1', '-d', 'user_cache.enable_cli=1'];
1717
$run = function (string $ini, string $code) use ($php, $args): string {
18-
return shell_exec("$php $args $ini -r " . escapeshellarg($code) . ' 2>&1');
18+
$process = proc_open([$php, ...$args, ...explode(' ', $ini), '-r', $code], [1 => ['pipe', 'w'], 2 => ['redirect', 1]], $pipes);
19+
$output = stream_get_contents($pipes[1]);
20+
fclose($pipes[1]);
21+
proc_close($process);
22+
23+
return $output;
1924
};
2025
$capacity = 'echo UserCache\\Cache::getStatus()->getEntryCapacity();';
2126
$probe = '$status = UserCache\\Cache::getStatus();'

‎ext/user_cache/tests/user_cache_ini_validation.phpt‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ UserCache\Cache: a preferred_memory_model that is not available on the platform
33
--SKIPIF--
44
<?php
55
if (PHP_OS_FAMILY === 'Windows') die('skip user_cache.lockfile_path is only used and validated on POSIX systems');
6+
if (!function_exists('proc_open')) die('skip proc_open() not available');
67
?>
78
--INI--
89
user_cache.enable=1
@@ -14,20 +15,25 @@ user_cache.lockfile_path=relative/dir
1415
var_dump(ini_get('user_cache.preferred_memory_model'), ini_get('user_cache.lockfile_path'));
1516
var_dump(UserCache\Cache::getPool('ini-validation')->store('k', 1), UserCache\Cache::getPool('ini-validation')->fetch('k'));
1617

18+
$run = function (array $args): string {
19+
$process = proc_open([PHP_BINARY, '-n', ...$args], [1 => ['pipe', 'w'], 2 => ['redirect', 1]], $pipes);
20+
$output = stream_get_contents($pipes[1]);
21+
fclose($pipes[1]);
22+
proc_close($process);
23+
24+
return $output;
25+
};
26+
1727
/* "cgi" is accepted as an alias of "shm", which only builds with SysV shared memory provide (never macOS) */
18-
$shm = shell_exec(sprintf(
19-
'%s -n -d user_cache.preferred_memory_model=shm -r %s 2>&1',
20-
escapeshellarg(PHP_BINARY),
21-
escapeshellarg('echo ini_get("user_cache.preferred_memory_model");')
22-
));
28+
$shm = $run(['-d', 'user_cache.preferred_memory_model=shm', '-r', 'echo ini_get("user_cache.preferred_memory_model");']);
2329
$model = $shm === 'shm' ? 'cgi' : 'mmap';
24-
$command = sprintf(
25-
'%s -n -d user_cache.enable=1 -d user_cache.enable_cli=1 -d user_cache.preferred_memory_model=%s -d user_cache.lockfile_path= -r %s 2>&1',
26-
escapeshellarg(PHP_BINARY),
27-
$model,
28-
escapeshellarg('var_dump(ini_get("user_cache.preferred_memory_model"), ini_get("user_cache.lockfile_path"), UserCache\Cache::getPool("x")->store("k", 1));')
29-
);
30-
echo shell_exec($command);
30+
echo $run([
31+
'-d', 'user_cache.enable=1',
32+
'-d', 'user_cache.enable_cli=1',
33+
'-d', "user_cache.preferred_memory_model=$model",
34+
'-d', 'user_cache.lockfile_path=',
35+
'-r', 'var_dump(ini_get("user_cache.preferred_memory_model"), ini_get("user_cache.lockfile_path"), UserCache\Cache::getPool("x")->store("k", 1));',
36+
]);
3137
?>
3238
--EXPECTF--
3339
Warning: user_cache.lockfile_path must be an absolute path, "relative/dir" given in Unknown on line 0

‎ext/user_cache/tests/user_cache_memory_model_lazy_table.phpt‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ if (PHP_OS_FAMILY !== 'Linux') die('skip needs the RssShmem line of /proc/self/s
66
if (!preg_match('/^RssShmem:/m', (string) @file_get_contents('/proc/self/status'))) die('skip kernel does not report RssShmem');
77
if (!function_exists('proc_open')) die('skip proc_open() not available');
88
if (@disk_free_space('/dev/shm') < 256 * 1024 * 1024) die('skip the posix memory model needs 256M free in /dev/shm');
9-
$shm = shell_exec(escapeshellarg(PHP_BINARY) . ' -n -d user_cache.preferred_memory_model=shm -r ' . escapeshellarg('echo ini_get("user_cache.preferred_memory_model");') . ' 2>&1');
9+
$process = proc_open([PHP_BINARY, '-n', '-d', 'user_cache.preferred_memory_model=shm', '-r', 'echo ini_get("user_cache.preferred_memory_model");'], [1 => ['pipe', 'w'], 2 => ['redirect', 1]], $pipes);
10+
$shm = stream_get_contents($pipes[1]);
11+
fclose($pipes[1]);
12+
proc_close($process);
1013
if ($shm !== 'shm') die('skip the shm memory model is not built in (no SysV shared memory)');
1114
?>
1215
--FILE--

‎ext/user_cache/tests/user_cache_memory_model_mmap.phpt‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@ user_cache.shm_size=16M
99
user_cache.preferred_memory_model=mmap
1010
--SKIPIF--
1111
<?php
12+
if (!function_exists('proc_open')) die('skip proc_open() not available');
1213
$php = getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY;
1314
$probe = 'UserCache\Cache::getPool("probe")->store("k", 1);'
1415
. 'ob_start(); phpinfo(INFO_MODULES);'
1516
. 'preg_match("/^Active memory model => (.+)$/m", ob_get_clean(), $m);'
1617
. 'echo trim($m[1] ?? "none");';
17-
$cmd = escapeshellarg($php)
18-
. ' -n -d user_cache.enable=1 -d user_cache.enable_cli=1 -d user_cache.shm_size=16M'
19-
. ' -d user_cache.preferred_memory_model=mmap'
20-
. ' -r ' . escapeshellarg($probe) . ' 2>/dev/null';
21-
$active = trim((string) shell_exec($cmd));
18+
$process = proc_open(
19+
[$php, '-n', '-d', 'user_cache.enable=1', '-d', 'user_cache.enable_cli=1', '-d', 'user_cache.shm_size=16M', '-d', 'user_cache.preferred_memory_model=mmap', '-r', $probe],
20+
[1 => ['pipe', 'w'], 2 => ['null']],
21+
$pipes
22+
);
23+
$active = trim(stream_get_contents($pipes[1]));
24+
fclose($pipes[1]);
25+
proc_close($process);
2226
if ($active !== 'mmap') {
2327
die("skip mmap memory model unavailable (active: $active)");
2428
}

‎ext/user_cache/tests/user_cache_memory_model_posix.phpt‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@ user_cache.shm_size=16M
99
user_cache.preferred_memory_model=posix
1010
--SKIPIF--
1111
<?php
12+
if (!function_exists('proc_open')) die('skip proc_open() not available');
1213
$php = getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY;
1314
$probe = 'UserCache\Cache::getPool("probe")->store("k", 1);'
1415
. 'ob_start(); phpinfo(INFO_MODULES);'
1516
. 'preg_match("/^Active memory model => (.+)$/m", ob_get_clean(), $m);'
1617
. 'echo trim($m[1] ?? "none");';
17-
$cmd = escapeshellarg($php)
18-
. ' -n -d user_cache.enable=1 -d user_cache.enable_cli=1 -d user_cache.shm_size=16M'
19-
. ' -d user_cache.preferred_memory_model=posix'
20-
. ' -r ' . escapeshellarg($probe) . ' 2>/dev/null';
21-
$active = trim((string) shell_exec($cmd));
18+
$process = proc_open(
19+
[$php, '-n', '-d', 'user_cache.enable=1', '-d', 'user_cache.enable_cli=1', '-d', 'user_cache.shm_size=16M', '-d', 'user_cache.preferred_memory_model=posix', '-r', $probe],
20+
[1 => ['pipe', 'w'], 2 => ['null']],
21+
$pipes
22+
);
23+
$active = trim(stream_get_contents($pipes[1]));
24+
fclose($pipes[1]);
25+
proc_close($process);
2226
if ($active !== 'posix') {
2327
die("skip posix memory model unavailable (active: $active)");
2428
}

‎ext/user_cache/tests/user_cache_memory_model_shm.phpt‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@ user_cache.shm_size=16M
99
user_cache.preferred_memory_model=shm
1010
--SKIPIF--
1111
<?php
12+
if (!function_exists('proc_open')) die('skip proc_open() not available');
1213
$php = getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY;
1314
$probe = 'UserCache\Cache::getPool("probe")->store("k", 1);'
1415
. 'ob_start(); phpinfo(INFO_MODULES);'
1516
. 'preg_match("/^Active memory model => (.+)$/m", ob_get_clean(), $m);'
1617
. 'echo trim($m[1] ?? "none");';
17-
$cmd = escapeshellarg($php)
18-
. ' -n -d user_cache.enable=1 -d user_cache.enable_cli=1 -d user_cache.shm_size=16M'
19-
. ' -d user_cache.preferred_memory_model=shm'
20-
. ' -r ' . escapeshellarg($probe) . ' 2>/dev/null';
21-
$active = trim((string) shell_exec($cmd));
18+
$process = proc_open(
19+
[$php, '-n', '-d', 'user_cache.enable=1', '-d', 'user_cache.enable_cli=1', '-d', 'user_cache.shm_size=16M', '-d', 'user_cache.preferred_memory_model=shm', '-r', $probe],
20+
[1 => ['pipe', 'w'], 2 => ['null']],
21+
$pipes
22+
);
23+
$active = trim(stream_get_contents($pipes[1]));
24+
fclose($pipes[1]);
25+
proc_close($process);
2226
if ($active !== 'shm') {
2327
die("skip shm memory model unavailable (active: $active)");
2428
}

‎ext/user_cache/tests/user_cache_pcre_cache_keys.phpt‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
--TEST--
22
UserCache\Cache: fetched strings used as regex patterns are not kept as PCRE cache keys
3+
--SKIPIF--
4+
<?php
5+
if (!function_exists('proc_open')) die('skip proc_open() not available');
6+
?>
37
--FILE--
48
<?php
5-
$php = escapeshellarg(getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY);
6-
$args = '-n -d user_cache.enable=1 -d user_cache.enable_cli=1';
9+
$php = getenv('TEST_PHP_EXECUTABLE') ?: PHP_BINARY;
10+
$args = ['-n', '-d', 'user_cache.enable=1', '-d', 'user_cache.enable_cli=1'];
711
$code = <<<'PHP'
812
$cache = UserCache\Cache::getPool('pcre_cache_keys');
913
$cache->store('routes', ['user' => '#^/user/(\d+)$#', 'post' => '#^/post/(\w+)$#']);
@@ -18,9 +22,10 @@ PHP;
1822
/* The PCRE cache releases its keys at process shutdown, after the cache
1923
* segment has been unmapped; the exit status is checked in a child because
2024
* the crash happens after stdout is closed. */
21-
exec("$php $args -r " . escapeshellarg($code) . ' 2>&1', $output, $status);
22-
echo implode("\n", $output), "\n";
23-
var_dump($status);
25+
$process = proc_open([$php, ...$args, '-r', $code], [1 => ['pipe', 'w'], 2 => ['redirect', 1]], $pipes);
26+
echo stream_get_contents($pipes[1]);
27+
fclose($pipes[1]);
28+
var_dump(proc_close($process));
2429
?>
2530
--EXPECT--
2631
int(1)

0 commit comments

Comments
 (0)