xref: /freebsd/tests/sys/kern/pipe/pipe_kqueue_test.c (revision 9bf12bb91d729d827ca0dddf6885d4b3e94e07d3)
1344d411cSMark Johnston /*-
2344d411cSMark Johnston  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
29344d411cSMark Johnston __FBSDID("$FreeBSD$");
30344d411cSMark Johnston 
31344d411cSMark Johnston #include <sys/param.h>
32344d411cSMark Johnston #include <sys/event.h>
33344d411cSMark Johnston #include <sys/stat.h>
34344d411cSMark Johnston 
35344d411cSMark Johnston #include <errno.h>
36344d411cSMark Johnston #include <fcntl.h>
37344d411cSMark Johnston #include <limits.h>
38344d411cSMark Johnston #include <poll.h>
39344d411cSMark Johnston #include <stdio.h>
40344d411cSMark Johnston #include <stdlib.h>
41344d411cSMark Johnston #include <time.h>
42344d411cSMark Johnston #include <unistd.h>
43344d411cSMark Johnston 
44344d411cSMark Johnston #include <atf-c.h>
45344d411cSMark Johnston 
46344d411cSMark Johnston ATF_TC_WITHOUT_HEAD(pipe_kqueue__write_end);
47344d411cSMark Johnston ATF_TC_BODY(pipe_kqueue__write_end, tc)
48344d411cSMark Johnston {
49344d411cSMark Johnston 	int p[2] = { -1, -1 };
50344d411cSMark Johnston 
51344d411cSMark Johnston 	ATF_REQUIRE(pipe2(p, O_CLOEXEC | O_NONBLOCK) == 0);
52344d411cSMark Johnston 	ATF_REQUIRE(p[0] >= 0);
53344d411cSMark Johnston 	ATF_REQUIRE(p[1] >= 0);
54344d411cSMark Johnston 
55344d411cSMark Johnston 	int kq = kqueue();
56344d411cSMark Johnston 	ATF_REQUIRE(kq >= 0);
57344d411cSMark Johnston 
58344d411cSMark Johnston 	struct kevent kev[32];
59344d411cSMark Johnston 	EV_SET(&kev[0], p[1], EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, 0);
60344d411cSMark Johnston 
61344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, kev, 1, NULL, 0, NULL) == 0);
62344d411cSMark Johnston 
63344d411cSMark Johnston 	/* Test that EVFILT_WRITE behaves sensibly on the write end. */
64344d411cSMark Johnston 
65344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
66344d411cSMark Johnston 	    &(struct timespec) { 0, 0 }) == 1);
67344d411cSMark Johnston 	ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]);
68344d411cSMark Johnston 	ATF_REQUIRE(kev[0].filter == EVFILT_WRITE);
69344d411cSMark Johnston 	ATF_REQUIRE(kev[0].flags == EV_CLEAR);
70344d411cSMark Johnston 	ATF_REQUIRE(kev[0].fflags == 0);
71344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == 16384);
72344d411cSMark Johnston 	ATF_REQUIRE(kev[0].udata == 0);
73344d411cSMark Johnston 
74344d411cSMark Johnston 	/* Filling up the pipe should make the EVFILT_WRITE disappear. */
75344d411cSMark Johnston 
76344d411cSMark Johnston 	char c = 0;
77344d411cSMark Johnston 	ssize_t r;
78344d411cSMark Johnston 	while ((r = write(p[1], &c, 1)) == 1) {
79344d411cSMark Johnston 	}
80344d411cSMark Johnston 	ATF_REQUIRE(r < 0);
81344d411cSMark Johnston 	ATF_REQUIRE(errno == EAGAIN || errno == EWOULDBLOCK);
82344d411cSMark Johnston 
83344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
84344d411cSMark Johnston 	    &(struct timespec) { 0, 0 }) == 0);
85344d411cSMark Johnston 
86344d411cSMark Johnston 	/* Reading (PIPE_BUF - 1) bytes will not trigger a EVFILT_WRITE yet. */
87344d411cSMark Johnston 
88344d411cSMark Johnston 	for (int i = 0; i < PIPE_BUF - 1; ++i) {
89344d411cSMark Johnston 		ATF_REQUIRE(read(p[0], &c, 1) == 1);
90344d411cSMark Johnston 	}
91344d411cSMark Johnston 
92344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
93344d411cSMark Johnston 	    &(struct timespec) { 0, 0 }) == 0);
94344d411cSMark Johnston 
95344d411cSMark Johnston 	/* Reading one additional byte triggers the EVFILT_WRITE. */
96344d411cSMark Johnston 
97344d411cSMark Johnston 	ATF_REQUIRE(read(p[0], &c, 1) == 1);
98344d411cSMark Johnston 
99344d411cSMark Johnston 	r = kevent(kq, NULL, 0, kev, nitems(kev), &(struct timespec) { 0, 0 });
100344d411cSMark Johnston 	ATF_REQUIRE(r == 1);
101344d411cSMark Johnston 	ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]);
102344d411cSMark Johnston 	ATF_REQUIRE(kev[0].filter == EVFILT_WRITE);
103344d411cSMark Johnston 	ATF_REQUIRE(kev[0].flags == EV_CLEAR);
104344d411cSMark Johnston 	ATF_REQUIRE(kev[0].fflags == 0);
105344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == PIPE_BUF);
106344d411cSMark Johnston 	ATF_REQUIRE(kev[0].udata == 0);
107344d411cSMark Johnston 
108344d411cSMark Johnston 	/*
109344d411cSMark Johnston 	 * Reading another byte triggers the EVFILT_WRITE again with a changed
110344d411cSMark Johnston 	 * 'data' field.
111344d411cSMark Johnston 	 */
112344d411cSMark Johnston 
113344d411cSMark Johnston 	ATF_REQUIRE(read(p[0], &c, 1) == 1);
114344d411cSMark Johnston 
115344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
116344d411cSMark Johnston 	    &(struct timespec) { 0, 0 }) == 1);
117344d411cSMark Johnston 	ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]);
118344d411cSMark Johnston 	ATF_REQUIRE(kev[0].filter == EVFILT_WRITE);
119344d411cSMark Johnston 	ATF_REQUIRE(kev[0].flags == EV_CLEAR);
120344d411cSMark Johnston 	ATF_REQUIRE(kev[0].fflags == 0);
121344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == PIPE_BUF + 1);
122344d411cSMark Johnston 	ATF_REQUIRE(kev[0].udata == 0);
123344d411cSMark Johnston 
124344d411cSMark Johnston 	/*
125344d411cSMark Johnston 	 * Closing the read end should make a EV_EOF appear but leave the 'data'
126344d411cSMark Johnston 	 * field unchanged.
127344d411cSMark Johnston 	 */
128344d411cSMark Johnston 
129344d411cSMark Johnston 	ATF_REQUIRE(close(p[0]) == 0);
130344d411cSMark Johnston 
131344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
132344d411cSMark Johnston 	    &(struct timespec) { 0, 0 }) == 1);
133344d411cSMark Johnston 	ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]);
134344d411cSMark Johnston 	ATF_REQUIRE(kev[0].filter == EVFILT_WRITE);
135344d411cSMark Johnston 	ATF_REQUIRE(kev[0].flags == (EV_CLEAR | EV_EOF | EV_ONESHOT));
136344d411cSMark Johnston 	ATF_REQUIRE(kev[0].fflags == 0);
137344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == PIPE_BUF + 1);
138344d411cSMark Johnston 	ATF_REQUIRE(kev[0].udata == 0);
139344d411cSMark Johnston 
140344d411cSMark Johnston 	ATF_REQUIRE(close(kq) == 0);
141344d411cSMark Johnston 	ATF_REQUIRE(close(p[1]) == 0);
142344d411cSMark Johnston }
143344d411cSMark Johnston 
144344d411cSMark Johnston ATF_TC_WITHOUT_HEAD(pipe_kqueue__closed_read_end);
145344d411cSMark Johnston ATF_TC_BODY(pipe_kqueue__closed_read_end, tc)
146344d411cSMark Johnston {
147344d411cSMark Johnston 	int p[2] = { -1, -1 };
148344d411cSMark Johnston 
149344d411cSMark Johnston 	ATF_REQUIRE(pipe2(p, O_CLOEXEC | O_NONBLOCK) == 0);
150344d411cSMark Johnston 	ATF_REQUIRE(p[0] >= 0);
151344d411cSMark Johnston 	ATF_REQUIRE(p[1] >= 0);
152344d411cSMark Johnston 
153344d411cSMark Johnston 	ATF_REQUIRE(close(p[0]) == 0);
154344d411cSMark Johnston 
155344d411cSMark Johnston 	int kq = kqueue();
156344d411cSMark Johnston 	ATF_REQUIRE(kq >= 0);
157344d411cSMark Johnston 
158344d411cSMark Johnston 	struct kevent kev[32];
159344d411cSMark Johnston 	EV_SET(&kev[0], p[1], EVFILT_READ, EV_ADD | EV_CLEAR | EV_RECEIPT, /**/
160344d411cSMark Johnston 	    0, 0, 0);
161344d411cSMark Johnston 	EV_SET(&kev[1], p[1], EVFILT_WRITE, EV_ADD | EV_CLEAR | EV_RECEIPT, /**/
162344d411cSMark Johnston 	    0, 0, 0);
163344d411cSMark Johnston 
164344d411cSMark Johnston 	/*
165344d411cSMark Johnston 	 * Trying to register EVFILT_WRITE when the pipe is closed leads to an
166344d411cSMark Johnston 	 * EPIPE error.
167344d411cSMark Johnston 	 */
168344d411cSMark Johnston 
169344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, kev, 2, kev, 2, NULL) == 2);
170344d411cSMark Johnston 	ATF_REQUIRE((kev[0].flags & EV_ERROR) != 0);
171344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == 0);
172344d411cSMark Johnston 	ATF_REQUIRE((kev[1].flags & EV_ERROR) != 0);
173344d411cSMark Johnston 	ATF_REQUIRE(kev[1].data == EPIPE);
174344d411cSMark Johnston 
175344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
176344d411cSMark Johnston 	    &(struct timespec) { 0, 0 }) == 1);
177344d411cSMark Johnston 	ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]);
178344d411cSMark Johnston 	ATF_REQUIRE(kev[0].filter == EVFILT_READ);
179344d411cSMark Johnston 	ATF_REQUIRE(kev[0].flags == (EV_EOF | EV_CLEAR | EV_RECEIPT));
180344d411cSMark Johnston 	ATF_REQUIRE(kev[0].fflags == 0);
181344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == 0);
182344d411cSMark Johnston 	ATF_REQUIRE(kev[0].udata == 0);
183344d411cSMark Johnston 
184344d411cSMark Johnston 	ATF_REQUIRE(close(kq) == 0);
185344d411cSMark Johnston 	ATF_REQUIRE(close(p[1]) == 0);
186344d411cSMark Johnston }
187344d411cSMark Johnston 
188344d411cSMark Johnston ATF_TC_WITHOUT_HEAD(pipe_kqueue__closed_read_end_register_before_close);
189344d411cSMark Johnston ATF_TC_BODY(pipe_kqueue__closed_read_end_register_before_close, tc)
190344d411cSMark Johnston {
191344d411cSMark Johnston 	int p[2] = { -1, -1 };
192344d411cSMark Johnston 
193344d411cSMark Johnston 	ATF_REQUIRE(pipe2(p, O_CLOEXEC | O_NONBLOCK) == 0);
194344d411cSMark Johnston 	ATF_REQUIRE(p[0] >= 0);
195344d411cSMark Johnston 	ATF_REQUIRE(p[1] >= 0);
196344d411cSMark Johnston 
197344d411cSMark Johnston 	int kq = kqueue();
198344d411cSMark Johnston 	ATF_REQUIRE(kq >= 0);
199344d411cSMark Johnston 
200344d411cSMark Johnston 	struct kevent kev[32];
201344d411cSMark Johnston 	EV_SET(&kev[0], p[1], EVFILT_READ, EV_ADD | EV_CLEAR | EV_RECEIPT, /**/
202344d411cSMark Johnston 	    0, 0, 0);
203344d411cSMark Johnston 	EV_SET(&kev[1], p[1], EVFILT_WRITE, EV_ADD | EV_CLEAR | EV_RECEIPT, /**/
204344d411cSMark Johnston 	    0, 0, 0);
205344d411cSMark Johnston 
206344d411cSMark Johnston 	/*
207344d411cSMark Johnston 	 * Registering EVFILT_WRITE before the pipe is closed leads to a
208344d411cSMark Johnston 	 * EVFILT_WRITE event with EV_EOF set.
209344d411cSMark Johnston 	 */
210344d411cSMark Johnston 
211344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, kev, 2, kev, 2, NULL) == 2);
212344d411cSMark Johnston 	ATF_REQUIRE((kev[0].flags & EV_ERROR) != 0);
213344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == 0);
214344d411cSMark Johnston 	ATF_REQUIRE((kev[1].flags & EV_ERROR) != 0);
215344d411cSMark Johnston 	ATF_REQUIRE(kev[1].data == 0);
216344d411cSMark Johnston 
217344d411cSMark Johnston 	ATF_REQUIRE(close(p[0]) == 0);
218344d411cSMark Johnston 
219344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
220344d411cSMark Johnston 	    &(struct timespec) { 0, 0 }) == 2);
221344d411cSMark Johnston 	{
222344d411cSMark Johnston 		ATF_REQUIRE(kev[0].ident == (uintptr_t)p[1]);
223344d411cSMark Johnston 		ATF_REQUIRE(kev[0].filter == EVFILT_WRITE);
224344d411cSMark Johnston 		ATF_REQUIRE(kev[0].flags ==
225344d411cSMark Johnston 		    (EV_EOF | EV_CLEAR | EV_ONESHOT | EV_RECEIPT));
226344d411cSMark Johnston 		ATF_REQUIRE(kev[0].fflags == 0);
227344d411cSMark Johnston 		ATF_REQUIRE(kev[0].data == 16384);
228344d411cSMark Johnston 		ATF_REQUIRE(kev[0].udata == 0);
229344d411cSMark Johnston 	}
230344d411cSMark Johnston 	{
231344d411cSMark Johnston 		ATF_REQUIRE(kev[1].ident == (uintptr_t)p[1]);
232344d411cSMark Johnston 		ATF_REQUIRE(kev[1].filter == EVFILT_READ);
233344d411cSMark Johnston 		ATF_REQUIRE(kev[1].flags == (EV_EOF | EV_CLEAR | EV_RECEIPT));
234344d411cSMark Johnston 		ATF_REQUIRE(kev[1].fflags == 0);
235344d411cSMark Johnston 		ATF_REQUIRE(kev[1].data == 0);
236344d411cSMark Johnston 		ATF_REQUIRE(kev[1].udata == 0);
237344d411cSMark Johnston 	}
238344d411cSMark Johnston 
239344d411cSMark Johnston 	ATF_REQUIRE(close(kq) == 0);
240344d411cSMark Johnston 	ATF_REQUIRE(close(p[1]) == 0);
241344d411cSMark Johnston }
242344d411cSMark Johnston 
243344d411cSMark Johnston ATF_TC_WITHOUT_HEAD(pipe_kqueue__closed_write_end);
244344d411cSMark Johnston ATF_TC_BODY(pipe_kqueue__closed_write_end, tc)
245344d411cSMark Johnston {
246*9bf12bb9SMark Johnston 	struct kevent kev[32];
247*9bf12bb9SMark Johnston 	ssize_t bytes, n;
248*9bf12bb9SMark Johnston 	int kq, p[2];
249*9bf12bb9SMark Johnston 	char c;
250344d411cSMark Johnston 
251344d411cSMark Johnston 	ATF_REQUIRE(pipe2(p, O_CLOEXEC | O_NONBLOCK) == 0);
252344d411cSMark Johnston 	ATF_REQUIRE(p[0] >= 0);
253344d411cSMark Johnston 	ATF_REQUIRE(p[1] >= 0);
254344d411cSMark Johnston 
255*9bf12bb9SMark Johnston 	bytes = 0;
256*9bf12bb9SMark Johnston 	c = 0;
257*9bf12bb9SMark Johnston 	while ((n = write(p[1], &c, 1)) == 1)
258*9bf12bb9SMark Johnston 		bytes++;
259*9bf12bb9SMark Johnston 	ATF_REQUIRE(n < 0);
260344d411cSMark Johnston 	ATF_REQUIRE(errno == EAGAIN || errno == EWOULDBLOCK);
261344d411cSMark Johnston 
262344d411cSMark Johnston 	ATF_REQUIRE(close(p[1]) == 0);
263344d411cSMark Johnston 
264*9bf12bb9SMark Johnston 	kq = kqueue();
265344d411cSMark Johnston 	ATF_REQUIRE(kq >= 0);
266344d411cSMark Johnston 
267*9bf12bb9SMark Johnston 	EV_SET(&kev[0], p[0], EVFILT_READ, EV_ADD | EV_CLEAR | EV_RECEIPT,
268344d411cSMark Johnston 	    0, 0, 0);
269*9bf12bb9SMark Johnston 	EV_SET(&kev[1], p[0], EVFILT_WRITE, EV_ADD | EV_CLEAR | EV_RECEIPT,
270344d411cSMark Johnston 	    0, 0, 0);
271344d411cSMark Johnston 
272344d411cSMark Johnston 	/*
273344d411cSMark Johnston 	 * Trying to register EVFILT_WRITE when the pipe is closed leads to an
274344d411cSMark Johnston 	 * EPIPE error.
275344d411cSMark Johnston 	 */
276344d411cSMark Johnston 
277344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, kev, 2, kev, 2, NULL) == 2);
278344d411cSMark Johnston 	ATF_REQUIRE((kev[0].flags & EV_ERROR) != 0);
279344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == 0);
280344d411cSMark Johnston 	ATF_REQUIRE((kev[1].flags & EV_ERROR) != 0);
281344d411cSMark Johnston 	ATF_REQUIRE(kev[1].data == EPIPE);
282344d411cSMark Johnston 
283344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
284344d411cSMark Johnston 	    &(struct timespec) { 0, 0 }) == 1);
285344d411cSMark Johnston 	ATF_REQUIRE(kev[0].ident == (uintptr_t)p[0]);
286344d411cSMark Johnston 	ATF_REQUIRE(kev[0].filter == EVFILT_READ);
287344d411cSMark Johnston 	ATF_REQUIRE(kev[0].flags == (EV_EOF | EV_CLEAR | EV_RECEIPT));
288344d411cSMark Johnston 	ATF_REQUIRE(kev[0].fflags == 0);
289*9bf12bb9SMark Johnston 	ATF_REQUIRE(kev[0].data == bytes);
290344d411cSMark Johnston 	ATF_REQUIRE(kev[0].udata == 0);
291344d411cSMark Johnston 
292344d411cSMark Johnston 	ATF_REQUIRE(close(kq) == 0);
293344d411cSMark Johnston 	ATF_REQUIRE(close(p[0]) == 0);
294344d411cSMark Johnston }
295344d411cSMark Johnston 
296344d411cSMark Johnston ATF_TC_WITHOUT_HEAD(pipe_kqueue__closed_write_end_register_before_close);
297344d411cSMark Johnston ATF_TC_BODY(pipe_kqueue__closed_write_end_register_before_close, tc)
298344d411cSMark Johnston {
299*9bf12bb9SMark Johnston 	struct kevent kev[32];
300*9bf12bb9SMark Johnston 	ssize_t bytes, n;
301*9bf12bb9SMark Johnston 	int kq, p[2];
302*9bf12bb9SMark Johnston 	char c;
303344d411cSMark Johnston 
304344d411cSMark Johnston 	ATF_REQUIRE(pipe2(p, O_CLOEXEC | O_NONBLOCK) == 0);
305344d411cSMark Johnston 	ATF_REQUIRE(p[0] >= 0);
306344d411cSMark Johnston 	ATF_REQUIRE(p[1] >= 0);
307344d411cSMark Johnston 
308*9bf12bb9SMark Johnston 	kq = kqueue();
309344d411cSMark Johnston 	ATF_REQUIRE(kq >= 0);
310344d411cSMark Johnston 
311*9bf12bb9SMark Johnston 	EV_SET(&kev[0], p[0], EVFILT_READ, EV_ADD | EV_CLEAR | EV_RECEIPT,
312344d411cSMark Johnston 	    0, 0, 0);
313*9bf12bb9SMark Johnston 	EV_SET(&kev[1], p[0], EVFILT_WRITE, EV_ADD | EV_CLEAR | EV_RECEIPT,
314344d411cSMark Johnston 	    0, 0, 0);
315344d411cSMark Johnston 
316344d411cSMark Johnston 	/*
317344d411cSMark Johnston 	 * Registering EVFILT_WRITE before the pipe is closed leads to a
318344d411cSMark Johnston 	 * EVFILT_WRITE event with EV_EOF set.
319344d411cSMark Johnston 	 */
320344d411cSMark Johnston 
321344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, kev, 2, kev, 2, NULL) == 2);
322344d411cSMark Johnston 	ATF_REQUIRE((kev[0].flags & EV_ERROR) != 0);
323344d411cSMark Johnston 	ATF_REQUIRE(kev[0].data == 0);
324344d411cSMark Johnston 	ATF_REQUIRE((kev[1].flags & EV_ERROR) != 0);
325344d411cSMark Johnston 	ATF_REQUIRE(kev[1].data == 0);
326344d411cSMark Johnston 
327*9bf12bb9SMark Johnston 	bytes = 0;
328*9bf12bb9SMark Johnston 	c = 0;
329*9bf12bb9SMark Johnston 	while ((n = write(p[1], &c, 1)) == 1)
330*9bf12bb9SMark Johnston 		bytes++;
331*9bf12bb9SMark Johnston 	ATF_REQUIRE(n < 0);
332344d411cSMark Johnston 	ATF_REQUIRE(errno == EAGAIN || errno == EWOULDBLOCK);
333344d411cSMark Johnston 
334344d411cSMark Johnston 	ATF_REQUIRE(close(p[1]) == 0);
335344d411cSMark Johnston 
336344d411cSMark Johnston 	ATF_REQUIRE(kevent(kq, NULL, 0, kev, nitems(kev),
337344d411cSMark Johnston 	    &(struct timespec){ 0, 0 }) == 2);
338*9bf12bb9SMark Johnston 
339344d411cSMark Johnston 	ATF_REQUIRE(kev[0].ident == (uintptr_t)p[0]);
340344d411cSMark Johnston 	ATF_REQUIRE(kev[0].filter == EVFILT_WRITE);
341344d411cSMark Johnston 	ATF_REQUIRE(kev[0].flags ==
342344d411cSMark Johnston 	    (EV_EOF | EV_CLEAR | EV_ONESHOT | EV_RECEIPT));
343344d411cSMark Johnston 	ATF_REQUIRE(kev[0].fflags == 0);
344*9bf12bb9SMark Johnston 	ATF_REQUIRE(kev[0].data > 0);
345344d411cSMark Johnston 	ATF_REQUIRE(kev[0].udata == 0);
346*9bf12bb9SMark Johnston 
347344d411cSMark Johnston 	ATF_REQUIRE(kev[1].ident == (uintptr_t)p[0]);
348344d411cSMark Johnston 	ATF_REQUIRE(kev[1].filter == EVFILT_READ);
349344d411cSMark Johnston 	ATF_REQUIRE(kev[1].flags == (EV_EOF | EV_CLEAR | EV_RECEIPT));
350344d411cSMark Johnston 	ATF_REQUIRE(kev[1].fflags == 0);
351*9bf12bb9SMark Johnston 	ATF_REQUIRE(kev[1].data == bytes);
352344d411cSMark Johnston 	ATF_REQUIRE(kev[1].udata == 0);
353344d411cSMark Johnston 
354344d411cSMark Johnston 	ATF_REQUIRE(close(kq) == 0);
355344d411cSMark Johnston 	ATF_REQUIRE(close(p[0]) == 0);
356344d411cSMark Johnston }
357344d411cSMark Johnston 
358344d411cSMark Johnston ATF_TP_ADD_TCS(tp)
359344d411cSMark Johnston {
360344d411cSMark Johnston 	ATF_TP_ADD_TC(tp, pipe_kqueue__write_end);
361344d411cSMark Johnston 	ATF_TP_ADD_TC(tp, pipe_kqueue__closed_read_end);
362344d411cSMark Johnston 	ATF_TP_ADD_TC(tp, pipe_kqueue__closed_read_end_register_before_close);
363344d411cSMark Johnston 	ATF_TP_ADD_TC(tp, pipe_kqueue__closed_write_end);
364344d411cSMark Johnston 	ATF_TP_ADD_TC(tp, pipe_kqueue__closed_write_end_register_before_close);
365344d411cSMark Johnston 
366344d411cSMark Johnston 	return atf_no_error();
367344d411cSMark Johnston }
368