1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2022 Gleb Smirnoff <glebius@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/time.h> 29 #include <sys/socket.h> 30 #include <sys/sysctl.h> 31 #include <sys/un.h> 32 #include <errno.h> 33 #include <fcntl.h> 34 #include <signal.h> 35 #include <stdlib.h> 36 37 #include <atf-c.h> 38 39 static struct itimerval itv = { 40 .it_interval = { 0, 0 }, 41 .it_value = { 1, 0 }, /* one second */ 42 }; 43 static sig_atomic_t timer_done = 0; 44 static void 45 sigalarm(int sig __unused) 46 { 47 48 timer_done = 1; 49 } 50 51 static struct sigaction sigact = { 52 .sa_handler = sigalarm, 53 }; 54 55 /* 56 * Fill socket to a state when next send(len) would fail. 57 */ 58 static void 59 fill(int fd, void *buf, ssize_t len) 60 { 61 unsigned long recvspace; 62 size_t llen = sizeof(unsigned long); 63 ssize_t sent; 64 65 ATF_REQUIRE(sysctlbyname("net.local.dgram.recvspace", &recvspace, 66 &llen, NULL, 0) == 0); 67 for (sent = 0; sent + len < (ssize_t)recvspace; sent += len) 68 ATF_REQUIRE(send(fd, buf, len, 0) == len); 69 } 70 71 ATF_TC_WITHOUT_HEAD(basic); 72 ATF_TC_BODY(basic, tc) 73 { 74 struct msghdr msg; 75 struct iovec iov[1]; 76 unsigned long maxdgram; 77 size_t llen = sizeof(unsigned long); 78 int fd[2]; 79 char *buf; 80 81 /* Allocate and initialize: 82 * - fd[0] to send, fd[1] to receive 83 * - buf[maxdgram] for data 84 */ 85 ATF_REQUIRE(sysctlbyname("net.local.dgram.maxdgram", &maxdgram, 86 &llen, NULL, 0) == 0); 87 ATF_REQUIRE(socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) != -1); 88 buf = malloc(maxdgram + 1); 89 ATF_REQUIRE(buf); 90 msg = (struct msghdr ){ 91 .msg_iov = iov, 92 .msg_iovlen = 1, 93 }; 94 iov[0] = (struct iovec ){ 95 .iov_base = buf, 96 }; 97 98 /* Fail to send > maxdgram. */ 99 ATF_REQUIRE(send(fd[0], buf, maxdgram + 1, 0) == -1); 100 ATF_REQUIRE(errno == EMSGSIZE); 101 102 /* Send maxdgram. */ 103 ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == (ssize_t)maxdgram); 104 105 /* Exercise MSG_PEEK, full and truncated.. */ 106 ATF_REQUIRE(recv(fd[1], buf, maxdgram, MSG_PEEK) == (ssize_t)maxdgram); 107 iov[0].iov_len = 42; 108 ATF_REQUIRE(recvmsg(fd[1], &msg, MSG_PEEK) == 42); 109 ATF_REQUIRE(msg.msg_flags == (MSG_PEEK | MSG_TRUNC)); 110 111 /* Receive maxdgram. */ 112 iov[0].iov_len = maxdgram; 113 ATF_REQUIRE(recvmsg(fd[1], &msg, 0) == (ssize_t)maxdgram); 114 ATF_REQUIRE(msg.msg_flags == 0); 115 116 /* Receive truncated message. */ 117 ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == (ssize_t)maxdgram); 118 iov[0].iov_len = maxdgram / 2; 119 ATF_REQUIRE(recvmsg(fd[1], &msg, 0) == (ssize_t)maxdgram / 2); 120 ATF_REQUIRE(msg.msg_flags == MSG_TRUNC); 121 122 /* Empty: block. */ 123 ATF_REQUIRE(sigaction(SIGALRM, &sigact, NULL) == 0); 124 ATF_REQUIRE(timer_done == 0); 125 ATF_REQUIRE(setitimer(ITIMER_REAL, &itv, NULL) == 0); 126 ATF_REQUIRE(recv(fd[1], buf, maxdgram, 0) == -1); 127 ATF_REQUIRE(errno == EINTR); 128 ATF_REQUIRE(timer_done == 1); 129 130 /* Don't block with MSG_DONTWAIT. */ 131 ATF_REQUIRE(recv(fd[1], buf, maxdgram, MSG_DONTWAIT) == -1); 132 ATF_REQUIRE(errno == EAGAIN); 133 134 /* Don't block with O_NONBLOCK. */ 135 ATF_REQUIRE(fcntl(fd[1], F_SETFL, O_NONBLOCK) != -1); 136 ATF_REQUIRE(recv(fd[1], buf, maxdgram, 0) == -1); 137 ATF_REQUIRE(errno == EAGAIN); 138 139 /* Fail with ENOBUFS on full socket. */ 140 fill(fd[0], buf, maxdgram); 141 ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1); 142 ATF_REQUIRE(errno == ENOBUFS); 143 144 /* Fail with EAGAIN with O_NONBLOCK set. */ 145 ATF_REQUIRE(fcntl(fd[0], F_SETFL, O_NONBLOCK) != -1); 146 ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1); 147 ATF_REQUIRE(errno == EAGAIN); 148 149 /* Remote side closed -> ECONNRESET. */ 150 close(fd[1]); 151 ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1); 152 ATF_REQUIRE(errno == ECONNRESET); 153 } 154 155 ATF_TC_WITHOUT_HEAD(one2many); 156 ATF_TC_BODY(one2many, tc) 157 { 158 struct sockaddr_un sun; 159 const char *path = "unix_dgram_listener"; 160 int one, many[2], two; 161 char buf[1024]; 162 163 /* Establish one to many connection. */ 164 ATF_REQUIRE((one = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0); 165 bzero(&sun, sizeof(sun)); 166 sun.sun_family = AF_LOCAL; 167 sun.sun_len = sizeof(sun); 168 strlcpy(sun.sun_path, path, sizeof(sun.sun_path)); 169 ATF_REQUIRE(bind(one, (struct sockaddr *)&sun, sizeof(sun)) == 0); 170 /* listen(2) shall fail. */ 171 ATF_REQUIRE(listen(one, -1) != 0); 172 for (int i = 0; i < 2; i++) { 173 ATF_REQUIRE((many[i] = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0); 174 ATF_REQUIRE(connect(many[i], (struct sockaddr *)&sun, 175 sizeof(sun)) == 0); 176 } 177 178 /* accept() on UNIX/DGRAM is invalid. */ 179 ATF_REQUIRE(accept(one, NULL, NULL) == -1); 180 ATF_REQUIRE(errno == EINVAL); 181 182 /* 183 * Connecting a bound socket to self: a strange, useless, but 184 * historically existing edge case that is not explicitly described 185 * in SuS, neither is forbidden there. Works on FreeBSD and Linux. 186 */ 187 ATF_REQUIRE(connect(one, (struct sockaddr *)&sun, sizeof(sun)) == 0); 188 ATF_REQUIRE(send(one, buf, 42, 0) == 42); 189 ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == 42); 190 191 /* 192 * Sending from an unconnected socket to a bound socket. Connection is 193 * created for the duration of the syscall. 194 */ 195 ATF_REQUIRE((two = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0); 196 ATF_REQUIRE(sendto(two, buf, 43, 0, (struct sockaddr *)&sun, 197 sizeof(sun)) == 43); 198 ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == 43); 199 200 /* One sender can fill the receive side. 201 * Current behavior which needs improvement. 202 */ 203 fill(many[0], buf, sizeof(buf)); 204 ATF_REQUIRE(send(many[1], buf, sizeof(buf), 0) == -1); 205 ATF_REQUIRE(errno == ENOBUFS); 206 ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf)); 207 ATF_REQUIRE(send(many[1], buf, sizeof(buf), 0) == sizeof(buf)); 208 } 209 210 ATF_TP_ADD_TCS(tp) 211 { 212 213 ATF_TP_ADD_TC(tp, basic); 214 ATF_TP_ADD_TC(tp, one2many); 215 216 return (atf_no_error()); 217 } 218