1 /* Copyright libuv project contributors. All rights reserved.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21
22 #include "uv.h"
23 #include "task.h"
24
25 #ifdef _WIN32
26
TEST_IMPL(iouring_pollhup)27 TEST_IMPL(iouring_pollhup) {
28 RETURN_SKIP("Not on Windows.");
29 }
30
31 #else /* !_WIN32 */
32
33 #include <unistd.h> /* close() */
34
35 static uv_pipe_t p1;
36 static uv_pipe_t p2;
37 static uv_idle_t idle_handle;
38 static int iters;
39 static int duped_fd;
40 static int newpipefds[2];
41
alloc_buffer(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)42 static void alloc_buffer(uv_handle_t* handle,
43 size_t suggested_size,
44 uv_buf_t* buf) {
45 static char slab[32];
46 *buf = uv_buf_init(slab, sizeof(slab));
47 }
48
read_data2(uv_stream_t * stream,ssize_t nread,const uv_buf_t * buf)49 static void read_data2(uv_stream_t* stream,
50 ssize_t nread,
51 const uv_buf_t* buf) {
52 if (nread < 0) {
53 ASSERT_EQ(nread, UV_EOF);
54 ASSERT_OK(close(duped_fd));
55 duped_fd = -1;
56 uv_close((uv_handle_t*) &p2, NULL);
57 uv_close((uv_handle_t*) &idle_handle, NULL);
58 } else {
59 /* If nread == 0 is because of POLLHUP received still from pipefds[0] file
60 * description which is still referenced in duped_fd. It should not happen
61 * if close(p1) was called after EPOLL_CTL_DEL.
62 */
63 ASSERT_GT(nread, 0);
64 }
65 }
66
idle_cb(uv_idle_t * handle)67 static void idle_cb(uv_idle_t* handle) {
68 if (++iters == 1) {
69 ASSERT_OK(uv_pipe_open(&p2, newpipefds[0]));
70 ASSERT_OK(uv_read_start((uv_stream_t*) &p2, alloc_buffer, read_data2));
71 } else {
72 ASSERT_OK(uv_idle_stop(handle));
73 ASSERT_OK(close(newpipefds[1]));
74 newpipefds[1] = -1;
75 }
76 }
77
read_data(uv_stream_t * stream,ssize_t nread,const uv_buf_t * buf)78 static void read_data(uv_stream_t* stream,
79 ssize_t nread,
80 const uv_buf_t* buf) {
81 ASSERT_EQ(nread, UV_EOF);
82 uv_close((uv_handle_t*) stream, NULL);
83 ASSERT_OK(uv_idle_start(&idle_handle, idle_cb));
84 }
85
TEST_IMPL(iouring_pollhup)86 TEST_IMPL(iouring_pollhup) {
87 uv_loop_t* loop;
88 int pipefds[2];
89
90 loop = uv_default_loop();
91 ASSERT_OK(uv_pipe_init(loop, &p1, 0));
92 ASSERT_OK(uv_pipe_init(loop, &p2, 0));
93 ASSERT_OK(uv_idle_init(loop, &idle_handle));
94 ASSERT_OK(pipe(pipefds));
95 ASSERT_OK(pipe(newpipefds));
96
97 ASSERT_OK(uv_pipe_open(&p1, pipefds[0]));
98 duped_fd = dup(pipefds[0]);
99 ASSERT_NE(duped_fd, -1);
100
101 ASSERT_OK(uv_read_start((uv_stream_t*) &p1, alloc_buffer, read_data));
102 ASSERT_OK(close(pipefds[1])); /* Close write end, generate POLLHUP. */
103 pipefds[1] = -1;
104
105 ASSERT_OK(uv_run(loop, UV_RUN_DEFAULT));
106
107 MAKE_VALGRIND_HAPPY(uv_default_loop());
108 return 0;
109 }
110
111 #endif /* !_WIN32 */
112