xref: /netbsd-src/tests/kernel/kqueue/read/t_fifo.c (revision 1432c4dcd5dc91a576aa0aecaa90ec3c8606feab)
1 /* $NetBSD: t_fifo.c,v 1.5 2021/10/02 18:21:05 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2021 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 2021\
34  The NetBSD Foundation, inc. All rights reserved.");
35 __RCSID("$NetBSD: t_fifo.c,v 1.5 2021/10/02 18:21:05 thorpej Exp $");
36 
37 #include <sys/event.h>
38 #include <sys/stat.h>
39 
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <inttypes.h>
47 
48 #include <atf-c.h>
49 
50 static const char	fifo_path[] = "fifo";
51 
52 static void
fifo_support(void)53 fifo_support(void)
54 {
55 	errno = 0;
56 	if (mkfifo(fifo_path, 0600) == 0) {
57 		ATF_REQUIRE(unlink(fifo_path) == 0);
58 		return;
59 	}
60 
61 	if (errno == EOPNOTSUPP) {
62 		atf_tc_skip("the kernel does not support FIFOs");
63 	} else {
64 		atf_tc_fail("mkfifo(2) failed");
65 	}
66 }
67 
68 ATF_TC_WITH_CLEANUP(fifo);
ATF_TC_HEAD(fifo,tc)69 ATF_TC_HEAD(fifo, tc)
70 {
71 	atf_tc_set_md_var(tc, "descr", "Checks EVFILT_READ on fifo");
72 }
ATF_TC_BODY(fifo,tc)73 ATF_TC_BODY(fifo, tc)
74 {
75 	const struct timespec to = { 0, 0 };
76 	struct kevent event[1];
77 	char *buf;
78 	int rfd, wfd, kq;
79 	long pipe_buf;
80 
81 	fifo_support();
82 
83 	ATF_REQUIRE(mkfifo(fifo_path, 0600) == 0);
84 	ATF_REQUIRE((rfd = open(fifo_path, O_RDONLY | O_NONBLOCK)) >= 0);
85 	ATF_REQUIRE((wfd = open(fifo_path, O_WRONLY | O_NONBLOCK)) >= 0);
86 	ATF_REQUIRE((kq = kqueue()) >= 0);
87 
88 	/* Get the maximum atomic pipe write size. */
89 	pipe_buf = fpathconf(wfd, _PC_PIPE_BUF);
90 	ATF_REQUIRE(pipe_buf > 1);
91 
92 	buf = malloc(pipe_buf);
93 	ATF_REQUIRE(buf != NULL);
94 
95 	EV_SET(&event[0], rfd, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
96 	ATF_REQUIRE(kevent(kq, event, 1, NULL, 0, NULL) == 0);
97 
98 	/* We expect the FIFO to not be readable. */
99 	ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &to) == 0);
100 
101 	/* Write a single byte of data into the FIFO. */
102 	ATF_REQUIRE(write(wfd, buf, 1) == 1);
103 
104 	/* We expect the FIFO to be readable. */
105 	ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &to) == 1);
106 	ATF_REQUIRE(event[0].ident == (uintptr_t)rfd);
107 	ATF_REQUIRE(event[0].filter == EVFILT_READ);
108 	ATF_REQUIRE((event[0].flags & EV_EOF) == 0);
109 
110 	/* Read that single byte back out. */
111 	ATF_REQUIRE(read(rfd, buf, 1) == 1);
112 
113 	/* We expect the FIFO to not be readable. */
114 	ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &to) == 0);
115 
116 	/* Close the writer.  We expect to get EV_EOF. */
117 	(void)close(wfd);
118 	ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &to) == 1);
119 	ATF_REQUIRE(event[0].ident == (uintptr_t)rfd);
120 	ATF_REQUIRE(event[0].filter == EVFILT_READ);
121 	ATF_REQUIRE((event[0].flags & EV_EOF) != 0);
122 
123 	/*
124 	 * Reconect the writer.  We expect EV_EOF to be cleared and
125 	 * for the FIFO to no longer be readable once again.
126 	 */
127 	ATF_REQUIRE((wfd = open(fifo_path, O_WRONLY | O_NONBLOCK)) >= 0);
128 	ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &to) == 0);
129 
130 	(void)close(wfd);
131 	(void)close(rfd);
132 	(void)close(kq);
133 }
134 
ATF_TC_CLEANUP(fifo,tc)135 ATF_TC_CLEANUP(fifo, tc)
136 {
137 	(void)unlink(fifo_path);
138 }
139 
ATF_TP_ADD_TCS(tp)140 ATF_TP_ADD_TCS(tp)
141 {
142 	ATF_TP_ADD_TC(tp, fifo);
143 
144 	return atf_no_error();
145 }
146