xref: /freebsd/tests/sys/kern/pipe/pipe_kqueue_test.c (revision 6dced2c6358e467ac1dccd99f6f648d4f71957a8)
1344d411cSMark Johnston /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3344d411cSMark Johnston  *
4344d411cSMark Johnston  * Copyright (c) 2020 Jan Kokemüller
5344d411cSMark Johnston  *
6344d411cSMark Johnston  * Redistribution and use in source and binary forms, with or without
7344d411cSMark Johnston  * modification, are permitted provided that the following conditions
8344d411cSMark Johnston  * are met:
9344d411cSMark Johnston  * 1. Redistributions of source code must retain the above copyright
10344d411cSMark Johnston  *    notice, this list of conditions and the following disclaimer.
11344d411cSMark Johnston  * 2. Redistributions in binary form must reproduce the above copyright
12344d411cSMark Johnston  *    notice, this list of conditions and the following disclaimer in the
13344d411cSMark Johnston  *    documentation and/or other materials provided with the distribution.
14344d411cSMark Johnston  *
15344d411cSMark Johnston  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16344d411cSMark Johnston  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17344d411cSMark Johnston  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18344d411cSMark Johnston  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19344d411cSMark Johnston  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20344d411cSMark Johnston  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21344d411cSMark Johnston  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22344d411cSMark Johnston  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23344d411cSMark Johnston  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24344d411cSMark Johnston  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25344d411cSMark Johnston  * SUCH DAMAGE.
26344d411cSMark Johnston  */
27344d411cSMark Johnston 
28344d411cSMark Johnston #include <sys/param.h>
29344d411cSMark Johnston #include <sys/event.h>
30344d411cSMark Johnston #include <sys/stat.h>
31344d411cSMark Johnston 
32344d411cSMark Johnston #include <errno.h>
33344d411cSMark Johnston #include <fcntl.h>
34344d411cSMark Johnston #include <limits.h>
35344d411cSMark Johnston #include <poll.h>
36344d411cSMark Johnston #include <stdio.h>
37344d411cSMark Johnston #include <stdlib.h>
38344d411cSMark Johnston #include <time.h>
39344d411cSMark Johnston #include <unistd.h>
40344d411cSMark Johnston 
41344d411cSMark Johnston #include <atf-c.h>
42344d411cSMark Johnston 
43344d411cSMark Johnston ATF_TC_WITHOUT_HEAD(pipe_kqueue__write_end);
ATF_TC_BODY(pipe_kqueue__write_end,tc)44344d411cSMark Johnston ATF_TC_BODY(pipe_kqueue__write_end, tc)
45344d411cSMark Johnston {
46344d411cSMark Johnston 	int p[2] = { -1, -1 };
47344d411cSMark Johnston 
48344d411cSMark Johnston 	ATF_REQUIRE(pipe2(p, O_CLOEXEC | O_NONBLOCK) == 0);
49344d411cSMark Johnston 	ATF_REQUIRE(p[0] >= 0);
50344d411cSMark Johnston 	ATF_REQUIRE(p[1] >= 0);
51344d411cSMark Johnston 
52344d411cSMark Johnston 	int kq = kqueue();
53344d411cSMark Johnston 	ATF_REQUIRE(kq >= 0);
54344d411cSMark Johnston 
55344d411cSMark Johnston 	struct kevent kev[32];
56344d411cSMark Johnston 	EV_SET(&kev[0], p[1], EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, 0);
57344d411cSMark Johnston 
58344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, NULL) == 0);
59344d411cSMark Johnston 
60344d411cSMark Johnston 	/* Test that EVFILT_WRITE behaves sensibly on the write end. */
61344d411cSMark Johnston 
62344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
63344d411cSMark Johnston 	    &(struct timespec) { 0, 0 }) == 1);
64344d411cSMark Johnston 	ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]);
65344d411cSMark Johnston 	ATF_REQUIRE(kev[0].filter == EVFILT_WRITE);
66344d411cSMark Johnston 	ATF_REQUIRE(kev[0].flags == EV_CLEAR);
67344d411cSMark Johnston 	ATF_REQUIRE(kev[0].fflags == 0);
68344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == 16384);
69344d411cSMark Johnston 	ATF_REQUIRE(kev[0].udata == 0);
70344d411cSMark Johnston 
71344d411cSMark Johnston 	/* Filling up the pipe should make the EVFILT_WRITE disappear. */
72344d411cSMark Johnston 
73344d411cSMark Johnston 	char c = 0;
74344d411cSMark Johnston 	ssize_t r;
75344d411cSMark Johnston 	while ((r = write(p[1], &c, 1)) == 1) {
76344d411cSMark Johnston 	}
77344d411cSMark Johnston 	ATF_REQUIRE(r < 0);
78344d411cSMark Johnston 	ATF_REQUIRE(errno == EAGAIN || errno == EWOULDBLOCK);
79344d411cSMark Johnston 
80344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
81344d411cSMark Johnston 	    &(struct timespec) { 0, 0 }) == 0);
82344d411cSMark Johnston 
83344d411cSMark Johnston 	/* Reading (PIPE_BUF - 1) bytes will not trigger a EVFILT_WRITE yet. */
84344d411cSMark Johnston 
85344d411cSMark Johnston 	for (int i = 0; i < PIPE_BUF - 1; ++i) {
86344d411cSMark Johnston 		ATF_REQUIRE(read(p[0], &c, 1) == 1);
87344d411cSMark Johnston 	}
88344d411cSMark Johnston 
89344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
90344d411cSMark Johnston 	    &(struct timespec) { 0, 0 }) == 0);
91344d411cSMark Johnston 
92344d411cSMark Johnston 	/* Reading one additional byte triggers the EVFILT_WRITE. */
93344d411cSMark Johnston 
94344d411cSMark Johnston 	ATF_REQUIRE(read(p[0], &c, 1) == 1);
95344d411cSMark Johnston 
96344d411cSMark Johnston 	r = kevent(kq, NULL, 0, kev, nitems(kev), &(struct timespec) { 0, 0 });
97344d411cSMark Johnston 	ATF_REQUIRE(r == 1);
98344d411cSMark Johnston 	ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]);
99344d411cSMark Johnston 	ATF_REQUIRE(kev[0].filter == EVFILT_WRITE);
100344d411cSMark Johnston 	ATF_REQUIRE(kev[0].flags == EV_CLEAR);
101344d411cSMark Johnston 	ATF_REQUIRE(kev[0].fflags == 0);
102344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == PIPE_BUF);
103344d411cSMark Johnston 	ATF_REQUIRE(kev[0].udata == 0);
104344d411cSMark Johnston 
105344d411cSMark Johnston 	/*
106344d411cSMark Johnston 	 * Reading another byte triggers the EVFILT_WRITE again with a changed
107344d411cSMark Johnston 	 * 'data' field.
108344d411cSMark Johnston 	 */
109344d411cSMark Johnston 
110344d411cSMark Johnston 	ATF_REQUIRE(read(p[0], &c, 1) == 1);
111344d411cSMark Johnston 
112344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
113344d411cSMark Johnston 	    &(struct timespec) { 0, 0 }) == 1);
114344d411cSMark Johnston 	ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]);
115344d411cSMark Johnston 	ATF_REQUIRE(kev[0].filter == EVFILT_WRITE);
116344d411cSMark Johnston 	ATF_REQUIRE(kev[0].flags == EV_CLEAR);
117344d411cSMark Johnston 	ATF_REQUIRE(kev[0].fflags == 0);
118344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == PIPE_BUF + 1);
119344d411cSMark Johnston 	ATF_REQUIRE(kev[0].udata == 0);
120344d411cSMark Johnston 
121344d411cSMark Johnston 	/*
122344d411cSMark Johnston 	 * Closing the read end should make a EV_EOF appear but leave the 'data'
123344d411cSMark Johnston 	 * field unchanged.
124344d411cSMark Johnston 	 */
125344d411cSMark Johnston 
126344d411cSMark Johnston 	ATF_REQUIRE(close(p[0]) == 0);
127344d411cSMark Johnston 
128344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
129344d411cSMark Johnston 	    &(struct timespec) { 0, 0 }) == 1);
130344d411cSMark Johnston 	ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]);
131344d411cSMark Johnston 	ATF_REQUIRE(kev[0].filter == EVFILT_WRITE);
132344d411cSMark Johnston 	ATF_REQUIRE(kev[0].flags == (EV_CLEAR | EV_EOF | EV_ONESHOT));
133344d411cSMark Johnston 	ATF_REQUIRE(kev[0].fflags == 0);
134344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == PIPE_BUF + 1);
135344d411cSMark Johnston 	ATF_REQUIRE(kev[0].udata == 0);
136344d411cSMark Johnston 
137344d411cSMark Johnston 	ATF_REQUIRE(close(kq) == 0);
138344d411cSMark Johnston 	ATF_REQUIRE(close(p[1]) == 0);
139344d411cSMark Johnston }
140344d411cSMark Johnston 
141344d411cSMark Johnston ATF_TC_WITHOUT_HEAD(pipe_kqueue__closed_read_end);
ATF_TC_BODY(pipe_kqueue__closed_read_end,tc)142344d411cSMark Johnston ATF_TC_BODY(pipe_kqueue__closed_read_end, tc)
143344d411cSMark Johnston {
144344d411cSMark Johnston 	int p[2] = { -1, -1 };
145344d411cSMark Johnston 
146344d411cSMark Johnston 	ATF_REQUIRE(pipe2(p, O_CLOEXEC | O_NONBLOCK) == 0);
147344d411cSMark Johnston 	ATF_REQUIRE(p[0] >= 0);
148344d411cSMark Johnston 	ATF_REQUIRE(p[1] >= 0);
149344d411cSMark Johnston 
150344d411cSMark Johnston 	ATF_REQUIRE(close(p[0]) == 0);
151344d411cSMark Johnston 
152344d411cSMark Johnston 	int kq = kqueue();
153344d411cSMark Johnston 	ATF_REQUIRE(kq >= 0);
154344d411cSMark Johnston 
155344d411cSMark Johnston 	struct kevent kev[32];
156344d411cSMark Johnston 	EV_SET(&kev[0], p[1], EVFILT_READ, EV_ADD | EV_CLEAR | EV_RECEIPT, /**/
157344d411cSMark Johnston 	    0, 0, 0);
158344d411cSMark Johnston 	EV_SET(&kev[1], p[1], EVFILT_WRITE, EV_ADD | EV_CLEAR | EV_RECEIPT, /**/
159344d411cSMark Johnston 	    0, 0, 0);
160344d411cSMark Johnston 
161344d411cSMark Johnston 	/*
162344d411cSMark Johnston 	 * Trying to register EVFILT_WRITE when the pipe is closed leads to an
163344d411cSMark Johnston 	 * EPIPE error.
164344d411cSMark Johnston 	 */
165344d411cSMark Johnston 
166344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, kev, 2, kev, 2, NULL) == 2);
167344d411cSMark Johnston 	ATF_REQUIRE((kev[0].flags & EV_ERROR) != 0);
168344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == 0);
169344d411cSMark Johnston 	ATF_REQUIRE((kev[1].flags & EV_ERROR) != 0);
170344d411cSMark Johnston 	ATF_REQUIRE(kev[1].data == EPIPE);
171344d411cSMark Johnston 
172344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
173344d411cSMark Johnston 	    &(struct timespec) { 0, 0 }) == 1);
174344d411cSMark Johnston 	ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]);
175344d411cSMark Johnston 	ATF_REQUIRE(kev[0].filter == EVFILT_READ);
176344d411cSMark Johnston 	ATF_REQUIRE(kev[0].flags == (EV_EOF | EV_CLEAR | EV_RECEIPT));
177344d411cSMark Johnston 	ATF_REQUIRE(kev[0].fflags == 0);
178344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == 0);
179344d411cSMark Johnston 	ATF_REQUIRE(kev[0].udata == 0);
180344d411cSMark Johnston 
181344d411cSMark Johnston 	ATF_REQUIRE(close(kq) == 0);
182344d411cSMark Johnston 	ATF_REQUIRE(close(p[1]) == 0);
183344d411cSMark Johnston }
184344d411cSMark Johnston 
185344d411cSMark Johnston ATF_TC_WITHOUT_HEAD(pipe_kqueue__closed_read_end_register_before_close);
ATF_TC_BODY(pipe_kqueue__closed_read_end_register_before_close,tc)186344d411cSMark Johnston ATF_TC_BODY(pipe_kqueue__closed_read_end_register_before_close, tc)
187344d411cSMark Johnston {
188344d411cSMark Johnston 	int p[2] = { -1, -1 };
189344d411cSMark Johnston 
190344d411cSMark Johnston 	ATF_REQUIRE(pipe2(p, O_CLOEXEC | O_NONBLOCK) == 0);
191344d411cSMark Johnston 	ATF_REQUIRE(p[0] >= 0);
192344d411cSMark Johnston 	ATF_REQUIRE(p[1] >= 0);
193344d411cSMark Johnston 
194344d411cSMark Johnston 	int kq = kqueue();
195344d411cSMark Johnston 	ATF_REQUIRE(kq >= 0);
196344d411cSMark Johnston 
197344d411cSMark Johnston 	struct kevent kev[32];
198344d411cSMark Johnston 	EV_SET(&kev[0], p[1], EVFILT_READ, EV_ADD | EV_CLEAR | EV_RECEIPT, /**/
199344d411cSMark Johnston 	    0, 0, 0);
200344d411cSMark Johnston 	EV_SET(&kev[1], p[1], EVFILT_WRITE, EV_ADD | EV_CLEAR | EV_RECEIPT, /**/
201344d411cSMark Johnston 	    0, 0, 0);
202344d411cSMark Johnston 
203344d411cSMark Johnston 	/*
204344d411cSMark Johnston 	 * Registering EVFILT_WRITE before the pipe is closed leads to a
205344d411cSMark Johnston 	 * EVFILT_WRITE event with EV_EOF set.
206344d411cSMark Johnston 	 */
207344d411cSMark Johnston 
208344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, kev, 2, kev, 2, NULL) == 2);
209344d411cSMark Johnston 	ATF_REQUIRE((kev[0].flags & EV_ERROR) != 0);
210344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == 0);
211344d411cSMark Johnston 	ATF_REQUIRE((kev[1].flags & EV_ERROR) != 0);
212344d411cSMark Johnston 	ATF_REQUIRE(kev[1].data == 0);
213344d411cSMark Johnston 
214344d411cSMark Johnston 	ATF_REQUIRE(close(p[0]) == 0);
215344d411cSMark Johnston 
216344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
217344d411cSMark Johnston 	    &(struct timespec) { 0, 0 }) == 2);
218344d411cSMark Johnston 	{
219344d411cSMark Johnston 		ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]);
220344d411cSMark Johnston 		ATF_REQUIRE(kev[0].filter == EVFILT_WRITE);
221344d411cSMark Johnston 		ATF_REQUIRE(kev[0].flags ==
222344d411cSMark Johnston 		    (EV_EOF | EV_CLEAR | EV_ONESHOT | EV_RECEIPT));
223344d411cSMark Johnston 		ATF_REQUIRE(kev[0].fflags == 0);
224344d411cSMark Johnston 		ATF_REQUIRE(kev[0].data == 16384);
225344d411cSMark Johnston 		ATF_REQUIRE(kev[0].udata == 0);
226344d411cSMark Johnston 	}
227344d411cSMark Johnston 	{
228344d411cSMark Johnston 		ATF_REQUIRE(kev[1].ident == (uintptr_t)p[1]);
229344d411cSMark Johnston 		ATF_REQUIRE(kev[1].filter == EVFILT_READ);
230344d411cSMark Johnston 		ATF_REQUIRE(kev[1].flags == (EV_EOF | EV_CLEAR | EV_RECEIPT));
231344d411cSMark Johnston 		ATF_REQUIRE(kev[1].fflags == 0);
232344d411cSMark Johnston 		ATF_REQUIRE(kev[1].data == 0);
233344d411cSMark Johnston 		ATF_REQUIRE(kev[1].udata == 0);
234344d411cSMark Johnston 	}
235344d411cSMark Johnston 
236344d411cSMark Johnston 	ATF_REQUIRE(close(kq) == 0);
237344d411cSMark Johnston 	ATF_REQUIRE(close(p[1]) == 0);
238344d411cSMark Johnston }
239344d411cSMark Johnston 
240344d411cSMark Johnston ATF_TC_WITHOUT_HEAD(pipe_kqueue__closed_write_end);
ATF_TC_BODY(pipe_kqueue__closed_write_end,tc)241344d411cSMark Johnston ATF_TC_BODY(pipe_kqueue__closed_write_end, tc)
242344d411cSMark Johnston {
2439bf12bb9SMark Johnston 	struct kevent kev[32];
2449bf12bb9SMark Johnston 	ssize_t bytes, n;
2459bf12bb9SMark Johnston 	int kq, p[2];
2469bf12bb9SMark Johnston 	char c;
247344d411cSMark Johnston 
248344d411cSMark Johnston 	ATF_REQUIRE(pipe2(p, O_CLOEXEC | O_NONBLOCK) == 0);
249344d411cSMark Johnston 	ATF_REQUIRE(p[0] >= 0);
250344d411cSMark Johnston 	ATF_REQUIRE(p[1] >= 0);
251344d411cSMark Johnston 
2529bf12bb9SMark Johnston 	bytes = 0;
2539bf12bb9SMark Johnston 	c = 0;
2549bf12bb9SMark Johnston 	while ((n = write(p[1], &c, 1)) == 1)
2559bf12bb9SMark Johnston 		bytes++;
2569bf12bb9SMark Johnston 	ATF_REQUIRE(n < 0);
257344d411cSMark Johnston 	ATF_REQUIRE(errno == EAGAIN || errno == EWOULDBLOCK);
258344d411cSMark Johnston 
259344d411cSMark Johnston 	ATF_REQUIRE(close(p[1]) == 0);
260344d411cSMark Johnston 
2619bf12bb9SMark Johnston 	kq = kqueue();
262344d411cSMark Johnston 	ATF_REQUIRE(kq >= 0);
263344d411cSMark Johnston 
2649bf12bb9SMark Johnston 	EV_SET(&kev[0], p[0], EVFILT_READ, EV_ADD | EV_CLEAR | EV_RECEIPT,
265344d411cSMark Johnston 	    0, 0, 0);
2669bf12bb9SMark Johnston 	EV_SET(&kev[1], p[0], EVFILT_WRITE, EV_ADD | EV_CLEAR | EV_RECEIPT,
267344d411cSMark Johnston 	    0, 0, 0);
268344d411cSMark Johnston 
269344d411cSMark Johnston 	/*
270344d411cSMark Johnston 	 * Trying to register EVFILT_WRITE when the pipe is closed leads to an
271344d411cSMark Johnston 	 * EPIPE error.
272344d411cSMark Johnston 	 */
273344d411cSMark Johnston 
274344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, kev, 2, kev, 2, NULL) == 2);
275344d411cSMark Johnston 	ATF_REQUIRE((kev[0].flags & EV_ERROR) != 0);
276344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == 0);
277344d411cSMark Johnston 	ATF_REQUIRE((kev[1].flags & EV_ERROR) != 0);
278344d411cSMark Johnston 	ATF_REQUIRE(kev[1].data == EPIPE);
279344d411cSMark Johnston 
280344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
281344d411cSMark Johnston 	    &(struct timespec) { 0, 0 }) == 1);
282344d411cSMark Johnston 	ATF_REQUIRE(kev[0].ident == (uintptr_t)p[0]);
283344d411cSMark Johnston 	ATF_REQUIRE(kev[0].filter == EVFILT_READ);
284344d411cSMark Johnston 	ATF_REQUIRE(kev[0].flags == (EV_EOF | EV_CLEAR | EV_RECEIPT));
285344d411cSMark Johnston 	ATF_REQUIRE(kev[0].fflags == 0);
2869bf12bb9SMark Johnston 	ATF_REQUIRE(kev[0].data == bytes);
287344d411cSMark Johnston 	ATF_REQUIRE(kev[0].udata == 0);
288344d411cSMark Johnston 
289344d411cSMark Johnston 	ATF_REQUIRE(close(kq) == 0);
290344d411cSMark Johnston 	ATF_REQUIRE(close(p[0]) == 0);
291344d411cSMark Johnston }
292344d411cSMark Johnston 
293344d411cSMark Johnston ATF_TC_WITHOUT_HEAD(pipe_kqueue__closed_write_end_register_before_close);
ATF_TC_BODY(pipe_kqueue__closed_write_end_register_before_close,tc)294344d411cSMark Johnston ATF_TC_BODY(pipe_kqueue__closed_write_end_register_before_close, tc)
295344d411cSMark Johnston {
2969bf12bb9SMark Johnston 	struct kevent kev[32];
2979bf12bb9SMark Johnston 	ssize_t bytes, n;
2989bf12bb9SMark Johnston 	int kq, p[2];
2999bf12bb9SMark Johnston 	char c;
300344d411cSMark Johnston 
301344d411cSMark Johnston 	ATF_REQUIRE(pipe2(p, O_CLOEXEC | O_NONBLOCK) == 0);
302344d411cSMark Johnston 	ATF_REQUIRE(p[0] >= 0);
303344d411cSMark Johnston 	ATF_REQUIRE(p[1] >= 0);
304344d411cSMark Johnston 
3059bf12bb9SMark Johnston 	kq = kqueue();
306344d411cSMark Johnston 	ATF_REQUIRE(kq >= 0);
307344d411cSMark Johnston 
3089bf12bb9SMark Johnston 	EV_SET(&kev[0], p[0], EVFILT_READ, EV_ADD | EV_CLEAR | EV_RECEIPT,
309344d411cSMark Johnston 	    0, 0, 0);
3109bf12bb9SMark Johnston 	EV_SET(&kev[1], p[0], EVFILT_WRITE, EV_ADD | EV_CLEAR | EV_RECEIPT,
311344d411cSMark Johnston 	    0, 0, 0);
312344d411cSMark Johnston 
313344d411cSMark Johnston 	/*
314344d411cSMark Johnston 	 * Registering EVFILT_WRITE before the pipe is closed leads to a
315344d411cSMark Johnston 	 * EVFILT_WRITE event with EV_EOF set.
316344d411cSMark Johnston 	 */
317344d411cSMark Johnston 
318344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, kev, 2, kev, 2, NULL) == 2);
319344d411cSMark Johnston 	ATF_REQUIRE((kev[0].flags & EV_ERROR) != 0);
320344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == 0);
321344d411cSMark Johnston 	ATF_REQUIRE((kev[1].flags & EV_ERROR) != 0);
322344d411cSMark Johnston 	ATF_REQUIRE(kev[1].data == 0);
323344d411cSMark Johnston 
3249bf12bb9SMark Johnston 	bytes = 0;
3259bf12bb9SMark Johnston 	c = 0;
3269bf12bb9SMark Johnston 	while ((n = write(p[1], &c, 1)) == 1)
3279bf12bb9SMark Johnston 		bytes++;
3289bf12bb9SMark Johnston 	ATF_REQUIRE(n < 0);
329344d411cSMark Johnston 	ATF_REQUIRE(errno == EAGAIN || errno == EWOULDBLOCK);
330344d411cSMark Johnston 
331344d411cSMark Johnston 	ATF_REQUIRE(close(p[1]) == 0);
332344d411cSMark Johnston 
333344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
334344d411cSMark Johnston 	    &(struct timespec){ 0, 0 }) == 2);
3359bf12bb9SMark Johnston 
336344d411cSMark Johnston 	ATF_REQUIRE(kev[0].ident == (uintptr_t)p[0]);
337344d411cSMark Johnston 	ATF_REQUIRE(kev[0].filter == EVFILT_WRITE);
338344d411cSMark Johnston 	ATF_REQUIRE(kev[0].flags ==
339344d411cSMark Johnston 	    (EV_EOF | EV_CLEAR | EV_ONESHOT | EV_RECEIPT));
340344d411cSMark Johnston 	ATF_REQUIRE(kev[0].fflags == 0);
3419bf12bb9SMark Johnston 	ATF_REQUIRE(kev[0].data > 0);
342344d411cSMark Johnston 	ATF_REQUIRE(kev[0].udata == 0);
3439bf12bb9SMark Johnston 
344344d411cSMark Johnston 	ATF_REQUIRE(kev[1].ident == (uintptr_t)p[0]);
345344d411cSMark Johnston 	ATF_REQUIRE(kev[1].filter == EVFILT_READ);
346344d411cSMark Johnston 	ATF_REQUIRE(kev[1].flags == (EV_EOF | EV_CLEAR | EV_RECEIPT));
347344d411cSMark Johnston 	ATF_REQUIRE(kev[1].fflags == 0);
3489bf12bb9SMark Johnston 	ATF_REQUIRE(kev[1].data == bytes);
349344d411cSMark Johnston 	ATF_REQUIRE(kev[1].udata == 0);
350344d411cSMark Johnston 
351344d411cSMark Johnston 	ATF_REQUIRE(close(kq) == 0);
352344d411cSMark Johnston 	ATF_REQUIRE(close(p[0]) == 0);
353344d411cSMark Johnston }
354344d411cSMark Johnston 
ATF_TP_ADD_TCS(tp)355344d411cSMark Johnston ATF_TP_ADD_TCS(tp)
356344d411cSMark Johnston {
357344d411cSMark Johnston 	ATF_TP_ADD_TC(tp, pipe_kqueue__write_end);
358344d411cSMark Johnston 	ATF_TP_ADD_TC(tp, pipe_kqueue__closed_read_end);
359344d411cSMark Johnston 	ATF_TP_ADD_TC(tp, pipe_kqueue__closed_read_end_register_before_close);
360344d411cSMark Johnston 	ATF_TP_ADD_TC(tp, pipe_kqueue__closed_write_end);
361344d411cSMark Johnston 	ATF_TP_ADD_TC(tp, pipe_kqueue__closed_write_end_register_before_close);
362344d411cSMark Johnston 
363344d411cSMark Johnston 	return atf_no_error();
364344d411cSMark Johnston }
365