1 /* $NetBSD: t_empty.c,v 1.2 2024/08/23 07:13:50 rin Exp $ */ 2 3 /*- 4 * Copyright (c) 2021 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __RCSID("$NetBSD: t_empty.c,v 1.2 2024/08/23 07:13:50 rin Exp $"); 31 32 #include <sys/event.h> 33 #include <sys/socket.h> 34 #include <sys/time.h> 35 #include <sys/types.h> 36 37 #include <netinet/in.h> 38 39 #include <err.h> 40 #include <errno.h> 41 #include <fcntl.h> 42 #include <stdbool.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <unistd.h> 46 47 #include <atf-c.h> 48 49 static void 50 test_empty(int readfd, int writefd, bool is_tcp) 51 { 52 struct timespec ts = { 0, 0 }; 53 struct kevent event; 54 int kq, error, sndbufsize; 55 char buf[1024] = { 0 }; 56 ssize_t rv; 57 58 ATF_REQUIRE((kq = kqueue()) >= 0); 59 60 EV_SET(&event, writefd, EVFILT_EMPTY, EV_ADD, 0, 0, NULL); 61 ATF_REQUIRE(kevent(kq, &event, 1, NULL, 0, NULL) == 0); 62 63 /* Check that EMPTY is true. */ 64 memset(&event, 0, sizeof(event)); 65 ATF_REQUIRE(kevent(kq, NULL, 0, &event, 1, &ts) == 1); 66 ATF_REQUIRE(event.ident == (uintptr_t)writefd); 67 ATF_REQUIRE(event.filter == EVFILT_EMPTY); 68 69 if (is_tcp) { 70 /* 71 * Get the write socket buffer size so that we can set 72 * the read socket buffer size to something larger 73 * later on. 74 */ 75 socklen_t slen = sizeof(sndbufsize); 76 ATF_REQUIRE(getsockopt(writefd, SOL_SOCKET, 77 SO_SNDBUF, &sndbufsize, &slen) == 0); 78 79 /* 80 * Set the receive buffer size to 1, slamming shut 81 * the TCP receive window, thus trapping all of the 82 * data in the sender's queue. 83 */ 84 int val = 1; 85 ATF_REQUIRE(setsockopt(readfd, SOL_SOCKET, 86 SO_RCVBUF, &val, sizeof(val)) == 0); 87 } 88 89 /* Write until the write buffer is full. */ 90 for (rv = 0; rv != -1;) { 91 rv = write(writefd, buf, sizeof(buf)); 92 error = errno; 93 ATF_REQUIRE(rv > 0 || (rv == -1 && error == EAGAIN)); 94 } 95 96 /* Check that EMPTY is false. */ 97 ATF_REQUIRE(kevent(kq, NULL, 0, &event, 1, &ts) == 0); 98 99 if (is_tcp) { 100 /* 101 * Set the receive buffer size to something larger than 102 * the sender's send buffer. 103 */ 104 int val = sndbufsize + 128; 105 ATF_REQUIRE(setsockopt(readfd, SOL_SOCKET, 106 SO_RCVBUF, &val, sizeof(val)) == 0); 107 } 108 109 /* Read all of the data that's available. */ 110 for (rv = 0; rv != -1;) { 111 rv = read(readfd, buf, sizeof(buf)); 112 error = errno; 113 ATF_REQUIRE(rv > 0 || (rv == -1 && error == EAGAIN)); 114 } 115 116 /* 117 * Check that EMPTY is true. Check a few times (TCP might 118 * not drain immediately). 119 */ 120 if (is_tcp) { 121 for (rv = 0; rv < 5; rv++) { 122 if (kevent(kq, NULL, 0, &event, 1, &ts) == 1) { 123 break; 124 } 125 } 126 sleep(1); 127 } 128 memset(&event, 0, sizeof(event)); 129 ATF_REQUIRE(kevent(kq, NULL, 0, &event, 1, &ts) == 1); 130 ATF_REQUIRE(event.ident == (uintptr_t)writefd); 131 ATF_REQUIRE(event.filter == EVFILT_EMPTY); 132 } 133 134 ATF_TC(sock_tcp); 135 ATF_TC_HEAD(sock_tcp, tc) 136 { 137 atf_tc_set_md_var(tc, "descr", 138 "Test EVFILT_EMPTY with TCP sockets."); 139 } 140 141 ATF_TC_BODY(sock_tcp, tc) 142 { 143 int readsock, writesock; 144 socklen_t slen; 145 146 ATF_REQUIRE((readsock = 147 socket(PF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP)) != -1); 148 ATF_REQUIRE((writesock = 149 socket(PF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP)) != -1); 150 151 struct sockaddr_in sin = { 152 .sin_len = sizeof(sin), 153 .sin_family = AF_INET, 154 .sin_port = 0, /* no need to swap 0 */ 155 .sin_addr = { .s_addr = htonl(INADDR_LOOPBACK) }, 156 }; 157 ATF_REQUIRE(bind(readsock, (struct sockaddr *)&sin, 158 sizeof(sin)) == 0); 159 ATF_REQUIRE(listen(readsock, 1) == 0); 160 slen = sizeof(sin); 161 ATF_REQUIRE(getsockname(readsock, (struct sockaddr *)&sin, &slen) == 0); 162 163 ATF_REQUIRE_ERRNO(EINPROGRESS, 164 connect(writesock, (struct sockaddr *)&sin, sizeof(sin)) == -1); 165 166 /* XXX Avoid race between connect(2) and accept(2). */ 167 sleep(1); 168 169 slen = sizeof(sin); 170 ATF_REQUIRE((readsock = accept(readsock, (struct sockaddr *)&sin, 171 &slen)) != -1); 172 173 test_empty(readsock, writesock, true); 174 } 175 176 ATF_TP_ADD_TCS(tp) 177 { 178 ATF_TP_ADD_TC(tp, sock_tcp); 179 180 return atf_no_error(); 181 } 182