1 /* $NetBSD: t_fifo.c,v 1.5 2021/10/02 18:39:15 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:39:15 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 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); 69 ATF_TC_HEAD(fifo, tc) 70 { 71 atf_tc_set_md_var(tc, "descr", "Checks EVFILT_WRITE on fifo"); 72 } 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], wfd, EVFILT_WRITE, 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 be writable. */ 99 ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &to) == 1); 100 ATF_REQUIRE(event[0].ident == (uintptr_t)wfd); 101 ATF_REQUIRE(event[0].filter == EVFILT_WRITE); 102 103 /* 104 * Write data into the FIFO until it is full, which is 105 * defined as insufficient buffer space to hold the 106 * maximum atomic pipe write size. 107 */ 108 while (write(wfd, buf, pipe_buf) != -1) { 109 continue; 110 } 111 ATF_REQUIRE(errno == EAGAIN); 112 113 /* We expect the FIFO to no longer be writable. */ 114 ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &to) == 0); 115 116 /* Read a single byte of data from the FIFO. */ 117 ATF_REQUIRE(read(rfd, buf, 1) == 1); 118 119 /* 120 * Because we have read only a single byte out, there will 121 * be insufficient space for a pipe_buf-sized message, so 122 * the FIFO should still not be writable. 123 */ 124 ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &to) == 0); 125 126 /* 127 * Now read enough so that exactly pipe_buf space should be 128 * available. The FIFO should be writable after that. 129 */ 130 ATF_REQUIRE(read(rfd, buf, pipe_buf - 1) == pipe_buf - 1); 131 ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &to) == 1); 132 ATF_REQUIRE(event[0].ident == (uintptr_t)wfd); 133 ATF_REQUIRE(event[0].filter == EVFILT_WRITE); 134 135 (void)close(wfd); 136 (void)close(rfd); 137 (void)close(kq); 138 } 139 140 ATF_TC_CLEANUP(fifo, tc) 141 { 142 (void)unlink(fifo_path); 143 } 144 145 ATF_TP_ADD_TCS(tp) 146 { 147 ATF_TP_ADD_TC(tp, fifo); 148 149 return atf_no_error(); 150 } 151