xref: /php-src/ext/standard/tests/file/bug69442.phpt (revision f39b5c4c)
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?>
24--FILE--
25<?php
26$cmd = '(echo "foo" ; exit 42;) 3>/dev/null; code=$?; echo $code >&3; exit $code';
27$descriptors = array(array("pty"), array("pty"), array("pty"), array("pipe", "w"));
28$pipes = array();
29
30$process = proc_open($cmd, $descriptors, $pipes);
31
32function read_from_pipe($pipe) {
33    $result = fread($pipe, 1000);
34    /* We can't guarantee that everything written to the pipe will be returned by a single call
35     *   to fread(), even if it was written with a single syscall and the number of bytes written
36     *   was small */
37    $again  = @fread($pipe, 1000);
38    if ($again) {
39        $result .= $again;
40    }
41    return $result;
42}
43
44$data0 = read_from_pipe($pipes[0]);
45echo 'read from pipe 0: ';
46var_dump($data0);
47fclose($pipes[0]);
48
49$data3 = read_from_pipe($pipes[3]);
50echo 'read from pipe 3: ';
51var_dump($data3);
52fclose($pipes[3]);
53
54proc_close($process);
55?>
56--EXPECT--
57read from pipe 0: string(5) "foo
58"
59read from pipe 3: string(3) "42
60"
61