xref: /PHP-8.1/ext/standard/tests/file/bug69442.phpt (revision 7aacc705)
1--TEST--
2proc_open with PTY closes incorrect file descriptor
3--SKIPIF--
4<?php
5
6$code = <<< 'EOC'
7    <?php
8    $descriptors = array(array("pty"), array("pty"), array("pty"), array("pipe", "w"));
9    $pipes = array();
10    $process = proc_open('echo "foo";', $descriptors, $pipes);
11EOC;
12
13    $tmpFile = tempnam(sys_get_temp_dir(), "bug69442");
14    file_put_contents($tmpFile, $code);
15
16    exec($_SERVER['TEST_PHP_EXECUTABLE']." -d display_errors=1 -d error_reporting=E_ALL ".$tmpFile." 2>&1", $output);
17    $output = join("\n", $output);
18    unlink($tmpFile);
19
20    if (strstr($output, "PTY (pseudoterminal) not supported on this system") !== false) {
21        die("skip PTY pseudo terminals are not supported");
22    }
23--FILE--
24<?php
25$cmd = '(echo "foo" ; exit 42;) 3>/dev/null; code=$?; echo $code >&3; exit $code';
26$descriptors = array(array("pty"), array("pty"), array("pty"), array("pipe", "w"));
27$pipes = array();
28
29$process = proc_open($cmd, $descriptors, $pipes);
30
31function read_from_pipe($pipe) {
32    $result = fread($pipe, 1000);
33    /* We can't guarantee that everything written to the pipe will be returned by a single call
34     *   to fread(), even if it was written with a single syscall and the number of bytes written
35     *   was small */
36    $again  = @fread($pipe, 1000);
37    if ($again) {
38        $result .= $again;
39    }
40    return $result;
41}
42
43$data0 = read_from_pipe($pipes[0]);
44echo 'read from pipe 0: ';
45var_dump($data0);
46fclose($pipes[0]);
47
48$data3 = read_from_pipe($pipes[3]);
49echo 'read from pipe 3: ';
50var_dump($data3);
51fclose($pipes[3]);
52
53proc_close($process);
54?>
55--EXPECT--
56read from pipe 0: string(5) "foo
57"
58read from pipe 3: string(3) "42
59"
60