summaryrefslogtreecommitdiff
path: root/ext/standard/tests/streams/proc_open_bug60120.phpt
blob: 053de2ebe0588f854de9b13817b1264f671da177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
--TEST--
Bug #60120 proc_open hangs with stdin/out with >2048 bytes
--FILE--
<?php
error_reporting(E_ALL);

$file = preg_replace(
    "~\.phpt?$~", ".io.php", __FILE__);

file_put_contents($file, <<<TMPFILE
<?php
\$input = stream_get_contents(STDIN);

if (\$input) {
    fwrite(STDOUT, \$input);
    fwrite(STDERR, \$input);
}
?>
TMPFILE
);

$command = sprintf("%s -n %s", PHP_BINARY, $file);

$process = proc_open(
    $command,
    [
        ['pipe', 'r'],
        ['pipe', 'w'],
        ['pipe', 'w']
    ],
    $pipes,
    getcwd(),
    [],
    [
        'suppress_errors' => true, 
        'bypass_shell' => false
    ]
);

if (!is_resource($process)) {
    die(sprintf(
        "could not open process \"%s\"", 
        $command));
}

fwrite($pipes[0], str_repeat('*', 10000));
fclose($pipes[0]);

stream_set_blocking($pipes[1], false);
stream_set_blocking($pipes[2], false);

$buffers = [
    1 => "",
    2 => ""
];

do {
    $r = [$pipes[1], $pipes[2]];
    $w = [];
    $e = [];
    $s = stream_select($r, $w, $e, 60);

    if (!$s) {
        if ($s === false) {
            proc_terminate($process);
        }
        break;
    }

    foreach ($r as $ready) {
        $buffers[
            array_search($ready, $pipes)
        ] .= fread($ready, 8192);
    }

    if (strlen($buffers[1]) === 10000 &&
        strlen($buffers[2]) === 10000) {
        break;
    }
} while (1);

var_dump(
    $buffers[1],
    $buffers[2],
    fread($pipes[1], 1),
    fread($pipes[2], 1));

fclose($pipes[1]);
fclose($pipes[2]);
?>
--EXPECTF--
string(10000) "%s"
string(10000) "%s"
string(0) ""
string(0) ""
--CLEAN--
unlink($file);