1--TEST--
2proc_open() with IO socketpairs
3--FILE--
4<?php
5
6function poll($pipe, $read = true)
7{
8    $r = ($read == true) ? [$pipe] : null;
9    $w = ($read == false) ? [$pipe] : null;
10    $e = null;
11
12    if (!stream_select($r, $w, $e, null)) {
13        throw new \Error("Select failed");
14    }
15}
16
17function read_pipe($pipe): string
18{
19    poll($pipe);
20
21    if (false === ($chunk = @fread($pipe, 8192))) {
22        throw new Error("Failed to read: " . (error_get_last()['message'] ?? 'N/A'));
23    }
24
25    return $chunk;
26}
27
28function write_pipe($pipe, $data)
29{
30    poll($pipe, false);
31
32    if (false == @fwrite($pipe, $data)) {
33        throw new Error("Failed to write: " . (error_get_last()['message'] ?? 'N/A'));
34    }
35}
36
37$cmd = [
38    getenv("TEST_PHP_EXECUTABLE"),
39    __DIR__ . '/proc_open_sockets2.inc'
40];
41
42$spec = [
43    ['socket'],
44    ['socket']
45];
46
47$proc = proc_open($cmd, $spec, $pipes);
48
49foreach ($pipes as $pipe) {
50    var_dump(stream_set_blocking($pipe, false));
51}
52
53printf("STDOUT << %s\n", read_pipe($pipes[1]));
54printf("STDOUT << %s\n", read_pipe($pipes[1]));
55
56write_pipe($pipes[0], 'done');
57fclose($pipes[0]);
58
59printf("STDOUT << %s\n", read_pipe($pipes[1]));
60
61?>
62--EXPECT--
63bool(true)
64bool(true)
65STDOUT << hello
66STDOUT << world
67STDOUT << DONE
68