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