1 1.6 andvar /* $NetBSD: t_fifo.c,v 1.6 2025/05/19 06:16:24 andvar 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.6 andvar __RCSID("$NetBSD: t_fifo.c,v 1.6 2025/05/19 06:16:24 andvar 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.1 jmmv #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.1 jmmv atf_tc_set_md_var(tc, "descr", "Checks EVFILT_READ 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], rfd, EVFILT_READ, 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 not be readable. */ 99 1.5 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &to) == 0); 100 1.5 thorpej 101 1.5 thorpej /* Write a single byte of data into the FIFO. */ 102 1.5 thorpej ATF_REQUIRE(write(wfd, buf, 1) == 1); 103 1.5 thorpej 104 1.5 thorpej /* We expect the FIFO to be readable. */ 105 1.5 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &to) == 1); 106 1.5 thorpej ATF_REQUIRE(event[0].ident == (uintptr_t)rfd); 107 1.5 thorpej ATF_REQUIRE(event[0].filter == EVFILT_READ); 108 1.5 thorpej ATF_REQUIRE((event[0].flags & EV_EOF) == 0); 109 1.5 thorpej 110 1.5 thorpej /* Read that single byte back out. */ 111 1.5 thorpej ATF_REQUIRE(read(rfd, buf, 1) == 1); 112 1.5 thorpej 113 1.5 thorpej /* We expect the FIFO to not be readable. */ 114 1.5 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &to) == 0); 115 1.5 thorpej 116 1.5 thorpej /* Close the writer. We expect to get EV_EOF. */ 117 1.5 thorpej (void)close(wfd); 118 1.5 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &to) == 1); 119 1.5 thorpej ATF_REQUIRE(event[0].ident == (uintptr_t)rfd); 120 1.5 thorpej ATF_REQUIRE(event[0].filter == EVFILT_READ); 121 1.5 thorpej ATF_REQUIRE((event[0].flags & EV_EOF) != 0); 122 1.5 thorpej 123 1.5 thorpej /* 124 1.6 andvar * Reconnect the writer. We expect EV_EOF to be cleared and 125 1.5 thorpej * for the FIFO to no longer be readable once again. 126 1.5 thorpej */ 127 1.5 thorpej ATF_REQUIRE((wfd = open(fifo_path, O_WRONLY | O_NONBLOCK)) >= 0); 128 1.5 thorpej ATF_REQUIRE(kevent(kq, NULL, 0, event, 1, &to) == 0); 129 1.5 thorpej 130 1.5 thorpej (void)close(wfd); 131 1.5 thorpej (void)close(rfd); 132 1.5 thorpej (void)close(kq); 133 1.5 thorpej } 134 1.1 jmmv 135 1.5 thorpej ATF_TC_CLEANUP(fifo, tc) 136 1.5 thorpej { 137 1.5 thorpej (void)unlink(fifo_path); 138 1.1 jmmv } 139 1.1 jmmv 140 1.1 jmmv ATF_TP_ADD_TCS(tp) 141 1.1 jmmv { 142 1.1 jmmv ATF_TP_ADD_TC(tp, fifo); 143 1.1 jmmv 144 1.1 jmmv return atf_no_error(); 145 1.1 jmmv } 146