1--TEST--
2Bug #60120 proc_open hangs with stdin/out with 2048+ bytes
3--FILE--
4<?php
5error_reporting(E_ALL);
6
7if (substr(PHP_OS, 0, 3) == 'WIN') {
8	$cmd = PHP_BINARY . ' -n -r "fwrite(STDOUT, $in = file_get_contents(\'php://stdin\')); fwrite(STDERR, $in);"';
9} else {
10	$cmd = PHP_BINARY . ' -n -r \'fwrite(STDOUT, $in = file_get_contents("php://stdin")); fwrite(STDERR, $in);\'';
11}
12$descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));
13$stdin = str_repeat('*', 1024 * 16) . '!';
14$stdin = str_repeat('*', 2049 );
15
16$options = array_merge(array('suppress_errors' => true, 'bypass_shell' => false));
17$process = proc_open($cmd, $descriptors, $pipes, getcwd(), array(), $options);
18
19foreach ($pipes as $pipe) {
20    stream_set_blocking($pipe, false);
21}
22$writePipes = array($pipes[0]);
23$stdinLen = strlen($stdin);
24$stdinOffset = 0;
25
26unset($pipes[0]);
27
28while ($pipes || $writePipes) {
29    $r = $pipes;
30    $w = $writePipes;
31    $e = null;
32    $n = stream_select($r, $w, $e, 60);
33
34    if (false === $n) {
35        break;
36    } elseif ($n === 0) {
37        proc_terminate($process);
38
39    }
40    if ($w) {
41        $written = fwrite($writePipes[0], substr($stdin, $stdinOffset), 8192);
42        if (false !== $written) {
43            $stdinOffset += $written;
44        }
45        if ($stdinOffset >= $stdinLen) {
46            fclose($writePipes[0]);
47            $writePipes = null;
48        }
49    }
50
51    foreach ($r as $pipe) {
52        $type = array_search($pipe, $pipes);
53        $data = fread($pipe, 8192);
54        var_dump($data);
55        if (false === $data || feof($pipe)) {
56            fclose($pipe);
57            unset($pipes[$type]);
58        }
59    }
60}
61
62
63?>
64===DONE===
65--EXPECTF--
66string(2049) "%s"
67string(2049) "%s"
68string(0) ""
69string(0) ""
70===DONE===
71