xref: /freebsd/tests/sys/kern/unix_dgram.c (revision 1f1e2261)
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/event.h>
30 #include <sys/ioctl.h>
31 #include <sys/select.h>
32 #include <sys/socket.h>
33 #include <sys/sysctl.h>
34 #include <sys/un.h>
35 #include <aio.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <signal.h>
39 #include <stdlib.h>
40 
41 #include <atf-c.h>
42 
43 static struct itimerval itv = {
44 	.it_interval = { 0, 0 },
45 	.it_value = { 1, 0 },	/* one second */
46 };
47 static sig_atomic_t timer_done = 0;
48 static void
49 sigalarm(int sig __unused)
50 {
51 
52 	timer_done = 1;
53 }
54 
55 static struct sigaction sigact = {
56 	.sa_handler = sigalarm,
57 };
58 
59 static struct sockaddr_un sun = {
60 	.sun_family = AF_LOCAL,
61 	.sun_len = sizeof(sun),
62 	.sun_path = "unix_dgram_listener",
63 };
64 
65 /*
66  * Fill socket to a state when next send(len) would fail.
67  *
68  * Note that every datagram is prepended with sender address,
69  * size of struct sockaddr.
70  */
71 static void
72 fill(int fd, void *buf, ssize_t len)
73 {
74 	unsigned long recvspace;
75 	size_t llen = sizeof(unsigned long);
76 	ssize_t sent;
77 
78 	ATF_REQUIRE(sysctlbyname("net.local.dgram.recvspace", &recvspace,
79 	    &llen, NULL, 0) == 0);
80 	for (sent = 0;
81 	    sent + len + sizeof(struct sockaddr) < recvspace;
82 	    sent += len + sizeof(struct sockaddr))
83 		ATF_REQUIRE(send(fd, buf, len, 0) == len);
84 }
85 
86 ATF_TC_WITHOUT_HEAD(basic);
87 ATF_TC_BODY(basic, tc)
88 {
89 	struct msghdr msg;
90 	struct iovec iov[1];
91 	unsigned long maxdgram;
92 	size_t llen = sizeof(unsigned long);
93 	int fd[2];
94 	char *buf;
95 
96 	/* Allocate and initialize:
97 	 * - fd[0] to send, fd[1] to receive
98 	 * - buf[maxdgram] for data
99 	 */
100 	ATF_REQUIRE(sysctlbyname("net.local.dgram.maxdgram", &maxdgram,
101 	    &llen, NULL, 0) == 0);
102 	ATF_REQUIRE(socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) != -1);
103 	buf = malloc(maxdgram + 1);
104 	ATF_REQUIRE(buf);
105 	msg = (struct msghdr ){
106 		.msg_iov = iov,
107 		.msg_iovlen = 1,
108 	};
109 	iov[0] = (struct iovec ){
110 		.iov_base = buf,
111 	};
112 
113 	/* Fail to send > maxdgram. */
114 	ATF_REQUIRE(send(fd[0], buf, maxdgram + 1, 0) == -1);
115 	ATF_REQUIRE(errno == EMSGSIZE);
116 
117 	/* Send maxdgram. */
118 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == (ssize_t)maxdgram);
119 
120 	/* Exercise MSG_PEEK, full and truncated.. */
121 	ATF_REQUIRE(recv(fd[1], buf, maxdgram, MSG_PEEK) == (ssize_t)maxdgram);
122 	iov[0].iov_len = 42;
123 	ATF_REQUIRE(recvmsg(fd[1], &msg, MSG_PEEK) == 42);
124 	ATF_REQUIRE(msg.msg_flags == (MSG_PEEK | MSG_TRUNC));
125 
126 	/* Receive maxdgram. */
127 	iov[0].iov_len = maxdgram;
128 	ATF_REQUIRE(recvmsg(fd[1], &msg, 0) == (ssize_t)maxdgram);
129 	ATF_REQUIRE(msg.msg_flags == 0);
130 
131 	/* Receive truncated message. */
132 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == (ssize_t)maxdgram);
133 	iov[0].iov_len = maxdgram / 2;
134 	ATF_REQUIRE(recvmsg(fd[1], &msg, 0) == (ssize_t)maxdgram / 2);
135 	ATF_REQUIRE(msg.msg_flags == MSG_TRUNC);
136 
137 	/* Empty: block. */
138 	ATF_REQUIRE(sigaction(SIGALRM, &sigact, NULL) == 0);
139 	ATF_REQUIRE(timer_done == 0);
140 	ATF_REQUIRE(setitimer(ITIMER_REAL, &itv, NULL) == 0);
141 	ATF_REQUIRE(recv(fd[1], buf, maxdgram, 0) == -1);
142 	ATF_REQUIRE(errno == EINTR);
143 	ATF_REQUIRE(timer_done == 1);
144 
145 	/* Don't block with MSG_DONTWAIT. */
146 	ATF_REQUIRE(recv(fd[1], buf, maxdgram, MSG_DONTWAIT) == -1);
147 	ATF_REQUIRE(errno == EAGAIN);
148 
149 	/* Don't block with O_NONBLOCK. */
150 	ATF_REQUIRE(fcntl(fd[1], F_SETFL, O_NONBLOCK) != -1);
151 	ATF_REQUIRE(recv(fd[1], buf, maxdgram, 0) == -1);
152 	ATF_REQUIRE(errno == EAGAIN);
153 
154 	/* Fail with ENOBUFS on full socket. */
155 	fill(fd[0], buf, maxdgram);
156 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1);
157 	ATF_REQUIRE(errno == ENOBUFS);
158 
159 	/* Fail with EAGAIN with O_NONBLOCK set. */
160 	ATF_REQUIRE(fcntl(fd[0], F_SETFL, O_NONBLOCK) != -1);
161 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1);
162 	ATF_REQUIRE(errno == EAGAIN);
163 
164 	/* Remote side closed -> ECONNRESET. */
165 	close(fd[1]);
166 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1);
167 	ATF_REQUIRE(errno == ECONNRESET);
168 }
169 
170 ATF_TC_WITHOUT_HEAD(one2many);
171 ATF_TC_BODY(one2many, tc)
172 {
173 	int one, many[2], two;
174 	char buf[1024];
175 
176 	/* Establish one to many connection. */
177 	ATF_REQUIRE((one = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
178 	ATF_REQUIRE(bind(one, (struct sockaddr *)&sun, sizeof(sun)) == 0);
179 	/* listen(2) shall fail. */
180 	ATF_REQUIRE(listen(one, -1) != 0);
181 	for (int i = 0; i < 2; i++) {
182 		ATF_REQUIRE((many[i] = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
183 		ATF_REQUIRE(connect(many[i], (struct sockaddr *)&sun,
184 		    sizeof(sun)) == 0);
185 	}
186 
187 	/* accept() on UNIX/DGRAM is invalid. */
188 	ATF_REQUIRE(accept(one, NULL, NULL) == -1);
189 	ATF_REQUIRE(errno == EINVAL);
190 
191 	/*
192 	 * Connecting a bound socket to self: a strange, useless, but
193 	 * historically existing edge case that is not explicitly described
194 	 * in SuS, neither is forbidden there. Works on FreeBSD and Linux.
195 	 */
196 	ATF_REQUIRE(connect(one, (struct sockaddr *)&sun, sizeof(sun)) == 0);
197 	ATF_REQUIRE(send(one, buf, 42, 0) == 42);
198 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == 42);
199 
200 	/*
201 	 * Sending from an unconnected socket to a bound socket.  Connection is
202 	 * created for the duration of the syscall.
203 	 */
204 	ATF_REQUIRE((two = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
205 	ATF_REQUIRE(sendto(two, buf, 43, 0, (struct sockaddr *)&sun,
206 	    sizeof(sun)) == 43);
207 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == 43);
208 
209 	/* One sender can fill the receive side.
210 	 * Current behavior which needs improvement.
211 	 */
212 	fill(many[0], buf, sizeof(buf));
213 	ATF_REQUIRE(send(many[1], buf, sizeof(buf), 0) == -1);
214 	ATF_REQUIRE(errno == ENOBUFS);
215 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
216 	ATF_REQUIRE(send(many[1], buf, sizeof(buf), 0) == sizeof(buf));
217 }
218 
219 /*
220  * Check that various mechanism report socket as readable and having
221  * 42 bytes of data.
222  */
223 static void
224 test42(int fd)
225 {
226 
227 	/* ioctl(FIONREAD) */
228 	int data;
229 
230 	ATF_REQUIRE(ioctl(fd, FIONREAD, &data) != -1);
231 	ATF_REQUIRE(data == 42);
232 
233 	/* select(2) */
234 	fd_set rfds;
235 
236 	FD_ZERO(&rfds);
237 	FD_SET(fd, &rfds);
238 	ATF_REQUIRE(select(fd + 1, &rfds, NULL, NULL, NULL) == 1);
239 	ATF_REQUIRE(FD_ISSET(fd, &rfds));
240 
241 	/* kevent(2) */
242 	struct kevent ev;
243 	int kq;
244 
245 	ATF_REQUIRE((kq = kqueue()) != -1);
246 	EV_SET(&ev, fd, EVFILT_READ, EV_ADD, NOTE_LOWAT, 41, NULL);
247 	ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == 0);
248 	ATF_REQUIRE(kevent(kq, NULL, 0, &ev, 1, NULL) == 1);
249 	ATF_REQUIRE(ev.filter == EVFILT_READ);
250 	ATF_REQUIRE(ev.data == 42);
251 
252 	/* aio(4) */
253 	char buf[50];
254 	struct aiocb aio = {
255 		.aio_nbytes = 50,
256 		.aio_fildes = fd,
257 		.aio_buf = buf,
258 	}, *aiop;
259 
260 	ATF_REQUIRE(aio_read(&aio) == 0);
261 	ATF_REQUIRE(aio_waitcomplete(&aiop, NULL) == 42);
262 	ATF_REQUIRE(aiop == &aio);
263 }
264 
265 /*
266  * Send data and control in connected & unconnected mode and check that
267  * various event mechanisms see the data, but don't count control bytes.
268  */
269 ATF_TC_WITHOUT_HEAD(event);
270 ATF_TC_BODY(event, tc)
271 {
272 	int fd[2];
273 	char buf[50];
274 	struct iovec iov = {
275 		.iov_base = buf,
276 		.iov_len = 42,
277 	};
278 	struct cmsghdr cmsg = {
279 		.cmsg_len = CMSG_LEN(0),
280 		.cmsg_level = SOL_SOCKET,
281 		.cmsg_type = SCM_TIMESTAMP,
282 	};
283 	struct msghdr msghdr = {
284 		.msg_iov = &iov,
285 		.msg_iovlen = 1,
286 		.msg_control = &cmsg,
287 		.msg_controllen = CMSG_LEN(0),
288 	};
289 
290 	/* Connected socket */
291 	ATF_REQUIRE(socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) != -1);
292 	ATF_REQUIRE(sendmsg(fd[0], &msghdr, 0) == 42);
293 	test42(fd[1]);
294 	close(fd[0]);
295 	close(fd[1]);
296 
297 	/* Not-connected send */
298 	ATF_REQUIRE((fd[0] = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
299 	ATF_REQUIRE((fd[1] = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
300 	ATF_REQUIRE(bind(fd[0], (struct sockaddr *)&sun, sizeof(sun)) == 0);
301 	ATF_REQUIRE(sendto(fd[1], buf, 42, 0, (struct sockaddr *)&sun,
302 	    sizeof(sun)) == 42);
303 	test42(fd[0]);
304 }
305 
306 ATF_TP_ADD_TCS(tp)
307 {
308 
309 	ATF_TP_ADD_TC(tp, basic);
310 	ATF_TP_ADD_TC(tp, one2many);
311 	ATF_TP_ADD_TC(tp, event);
312 
313 	return (atf_no_error());
314 }
315