xref: /freebsd/tests/sys/kern/unix_dgram.c (revision a170657108623cd48e3d695ca74c428a564912ee)
16d317723SGleb Smirnoff /*-
26d317723SGleb Smirnoff  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
36d317723SGleb Smirnoff  *
46d317723SGleb Smirnoff  * Copyright (c) 2022 Gleb Smirnoff <glebius@FreeBSD.org>
56d317723SGleb Smirnoff  *
66d317723SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
76d317723SGleb Smirnoff  * modification, are permitted provided that the following conditions
86d317723SGleb Smirnoff  * are met:
96d317723SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
106d317723SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
116d317723SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
126d317723SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
136d317723SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
146d317723SGleb Smirnoff  *
156d317723SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
166d317723SGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
176d317723SGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
186d317723SGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
196d317723SGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
206d317723SGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
216d317723SGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
226d317723SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
236d317723SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
246d317723SGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
256d317723SGleb Smirnoff  * SUCH DAMAGE.
266d317723SGleb Smirnoff  */
276d317723SGleb Smirnoff 
286d317723SGleb Smirnoff #include <sys/time.h>
2970d07b20SGleb Smirnoff #include <sys/event.h>
3070d07b20SGleb Smirnoff #include <sys/ioctl.h>
3170d07b20SGleb Smirnoff #include <sys/select.h>
326d317723SGleb Smirnoff #include <sys/socket.h>
336d317723SGleb Smirnoff #include <sys/sysctl.h>
346d317723SGleb Smirnoff #include <sys/un.h>
3570d07b20SGleb Smirnoff #include <aio.h>
366d317723SGleb Smirnoff #include <errno.h>
376d317723SGleb Smirnoff #include <fcntl.h>
386d317723SGleb Smirnoff #include <signal.h>
396d317723SGleb Smirnoff #include <stdlib.h>
406d317723SGleb Smirnoff 
416d317723SGleb Smirnoff #include <atf-c.h>
426d317723SGleb Smirnoff 
436d317723SGleb Smirnoff static struct itimerval itv = {
446d317723SGleb Smirnoff 	.it_interval = { 0, 0 },
456d317723SGleb Smirnoff 	.it_value = { 1, 0 },	/* one second */
466d317723SGleb Smirnoff };
476d317723SGleb Smirnoff static sig_atomic_t timer_done = 0;
486d317723SGleb Smirnoff static void
496d317723SGleb Smirnoff sigalarm(int sig __unused)
506d317723SGleb Smirnoff {
516d317723SGleb Smirnoff 
526d317723SGleb Smirnoff 	timer_done = 1;
536d317723SGleb Smirnoff }
546d317723SGleb Smirnoff 
556d317723SGleb Smirnoff static struct sigaction sigact = {
566d317723SGleb Smirnoff 	.sa_handler = sigalarm,
576d317723SGleb Smirnoff };
586d317723SGleb Smirnoff 
5970d07b20SGleb Smirnoff static struct sockaddr_un sun = {
6070d07b20SGleb Smirnoff 	.sun_family = AF_LOCAL,
6170d07b20SGleb Smirnoff 	.sun_len = sizeof(sun),
6270d07b20SGleb Smirnoff 	.sun_path = "unix_dgram_listener",
6370d07b20SGleb Smirnoff };
6470d07b20SGleb Smirnoff 
656d317723SGleb Smirnoff /*
666d317723SGleb Smirnoff  * Fill socket to a state when next send(len) would fail.
67aa9f97afSGleb Smirnoff  *
68aa9f97afSGleb Smirnoff  * Note that every datagram is prepended with sender address,
69aa9f97afSGleb Smirnoff  * size of struct sockaddr.
706d317723SGleb Smirnoff  */
716d317723SGleb Smirnoff static void
726d317723SGleb Smirnoff fill(int fd, void *buf, ssize_t len)
736d317723SGleb Smirnoff {
746d317723SGleb Smirnoff 	unsigned long recvspace;
756d317723SGleb Smirnoff 	size_t llen = sizeof(unsigned long);
766d317723SGleb Smirnoff 	ssize_t sent;
776d317723SGleb Smirnoff 
786d317723SGleb Smirnoff 	ATF_REQUIRE(sysctlbyname("net.local.dgram.recvspace", &recvspace,
796d317723SGleb Smirnoff 	    &llen, NULL, 0) == 0);
80aa9f97afSGleb Smirnoff 	for (sent = 0;
81aa9f97afSGleb Smirnoff 	    sent + len + sizeof(struct sockaddr) < recvspace;
82aa9f97afSGleb Smirnoff 	    sent += len + sizeof(struct sockaddr))
836d317723SGleb Smirnoff 		ATF_REQUIRE(send(fd, buf, len, 0) == len);
846d317723SGleb Smirnoff }
856d317723SGleb Smirnoff 
866d317723SGleb Smirnoff ATF_TC_WITHOUT_HEAD(basic);
876d317723SGleb Smirnoff ATF_TC_BODY(basic, tc)
886d317723SGleb Smirnoff {
896d317723SGleb Smirnoff 	struct msghdr msg;
906d317723SGleb Smirnoff 	struct iovec iov[1];
916d317723SGleb Smirnoff 	unsigned long maxdgram;
926d317723SGleb Smirnoff 	size_t llen = sizeof(unsigned long);
936d317723SGleb Smirnoff 	int fd[2];
946d317723SGleb Smirnoff 	char *buf;
956d317723SGleb Smirnoff 
966d317723SGleb Smirnoff 	/* Allocate and initialize:
976d317723SGleb Smirnoff 	 * - fd[0] to send, fd[1] to receive
986d317723SGleb Smirnoff 	 * - buf[maxdgram] for data
996d317723SGleb Smirnoff 	 */
1006d317723SGleb Smirnoff 	ATF_REQUIRE(sysctlbyname("net.local.dgram.maxdgram", &maxdgram,
1016d317723SGleb Smirnoff 	    &llen, NULL, 0) == 0);
1026d317723SGleb Smirnoff 	ATF_REQUIRE(socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) != -1);
1036d317723SGleb Smirnoff 	buf = malloc(maxdgram + 1);
1046d317723SGleb Smirnoff 	ATF_REQUIRE(buf);
1056d317723SGleb Smirnoff 	msg = (struct msghdr ){
1066d317723SGleb Smirnoff 		.msg_iov = iov,
1076d317723SGleb Smirnoff 		.msg_iovlen = 1,
1086d317723SGleb Smirnoff 	};
1096d317723SGleb Smirnoff 	iov[0] = (struct iovec ){
1106d317723SGleb Smirnoff 		.iov_base = buf,
1116d317723SGleb Smirnoff 	};
1126d317723SGleb Smirnoff 
1136d317723SGleb Smirnoff 	/* Fail to send > maxdgram. */
1146d317723SGleb Smirnoff 	ATF_REQUIRE(send(fd[0], buf, maxdgram + 1, 0) == -1);
1156d317723SGleb Smirnoff 	ATF_REQUIRE(errno == EMSGSIZE);
1166d317723SGleb Smirnoff 
1176d317723SGleb Smirnoff 	/* Send maxdgram. */
1186d317723SGleb Smirnoff 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == (ssize_t)maxdgram);
1196d317723SGleb Smirnoff 
1206d317723SGleb Smirnoff 	/* Exercise MSG_PEEK, full and truncated.. */
1216d317723SGleb Smirnoff 	ATF_REQUIRE(recv(fd[1], buf, maxdgram, MSG_PEEK) == (ssize_t)maxdgram);
1226d317723SGleb Smirnoff 	iov[0].iov_len = 42;
1236d317723SGleb Smirnoff 	ATF_REQUIRE(recvmsg(fd[1], &msg, MSG_PEEK) == 42);
1246d317723SGleb Smirnoff 	ATF_REQUIRE(msg.msg_flags == (MSG_PEEK | MSG_TRUNC));
1256d317723SGleb Smirnoff 
1266d317723SGleb Smirnoff 	/* Receive maxdgram. */
1276d317723SGleb Smirnoff 	iov[0].iov_len = maxdgram;
1286d317723SGleb Smirnoff 	ATF_REQUIRE(recvmsg(fd[1], &msg, 0) == (ssize_t)maxdgram);
1296d317723SGleb Smirnoff 	ATF_REQUIRE(msg.msg_flags == 0);
1306d317723SGleb Smirnoff 
1316d317723SGleb Smirnoff 	/* Receive truncated message. */
1326d317723SGleb Smirnoff 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == (ssize_t)maxdgram);
1336d317723SGleb Smirnoff 	iov[0].iov_len = maxdgram / 2;
1346d317723SGleb Smirnoff 	ATF_REQUIRE(recvmsg(fd[1], &msg, 0) == (ssize_t)maxdgram / 2);
1356d317723SGleb Smirnoff 	ATF_REQUIRE(msg.msg_flags == MSG_TRUNC);
1366d317723SGleb Smirnoff 
1376d317723SGleb Smirnoff 	/* Empty: block. */
1386d317723SGleb Smirnoff 	ATF_REQUIRE(sigaction(SIGALRM, &sigact, NULL) == 0);
1396d317723SGleb Smirnoff 	ATF_REQUIRE(timer_done == 0);
1406d317723SGleb Smirnoff 	ATF_REQUIRE(setitimer(ITIMER_REAL, &itv, NULL) == 0);
1416d317723SGleb Smirnoff 	ATF_REQUIRE(recv(fd[1], buf, maxdgram, 0) == -1);
1426d317723SGleb Smirnoff 	ATF_REQUIRE(errno == EINTR);
1436d317723SGleb Smirnoff 	ATF_REQUIRE(timer_done == 1);
1446d317723SGleb Smirnoff 
1456d317723SGleb Smirnoff 	/* Don't block with MSG_DONTWAIT. */
1466d317723SGleb Smirnoff 	ATF_REQUIRE(recv(fd[1], buf, maxdgram, MSG_DONTWAIT) == -1);
1476d317723SGleb Smirnoff 	ATF_REQUIRE(errno == EAGAIN);
1486d317723SGleb Smirnoff 
1496d317723SGleb Smirnoff 	/* Don't block with O_NONBLOCK. */
1506d317723SGleb Smirnoff 	ATF_REQUIRE(fcntl(fd[1], F_SETFL, O_NONBLOCK) != -1);
1516d317723SGleb Smirnoff 	ATF_REQUIRE(recv(fd[1], buf, maxdgram, 0) == -1);
1526d317723SGleb Smirnoff 	ATF_REQUIRE(errno == EAGAIN);
1536d317723SGleb Smirnoff 
1546d317723SGleb Smirnoff 	/* Fail with ENOBUFS on full socket. */
1556d317723SGleb Smirnoff 	fill(fd[0], buf, maxdgram);
1566d317723SGleb Smirnoff 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1);
1576d317723SGleb Smirnoff 	ATF_REQUIRE(errno == ENOBUFS);
1586d317723SGleb Smirnoff 
159*a1706571SGleb Smirnoff 	/*
160*a1706571SGleb Smirnoff 	 * Fail with ENOBUFS with O_NONBLOCK set, too. See 71e70c25c00
161*a1706571SGleb Smirnoff 	 * for explanation why this behavior needs to be preserved.
162*a1706571SGleb Smirnoff 	 */
1636d317723SGleb Smirnoff 	ATF_REQUIRE(fcntl(fd[0], F_SETFL, O_NONBLOCK) != -1);
1646d317723SGleb Smirnoff 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1);
165*a1706571SGleb Smirnoff 	ATF_REQUIRE(errno == ENOBUFS);
1666d317723SGleb Smirnoff 
1676d317723SGleb Smirnoff 	/* Remote side closed -> ECONNRESET. */
1686d317723SGleb Smirnoff 	close(fd[1]);
1696d317723SGleb Smirnoff 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1);
1706d317723SGleb Smirnoff 	ATF_REQUIRE(errno == ECONNRESET);
1716d317723SGleb Smirnoff }
1726d317723SGleb Smirnoff 
1736d317723SGleb Smirnoff ATF_TC_WITHOUT_HEAD(one2many);
1746d317723SGleb Smirnoff ATF_TC_BODY(one2many, tc)
1756d317723SGleb Smirnoff {
176458f475dSGleb Smirnoff 	int one, many[3], two;
177458f475dSGleb Smirnoff #define	BUFSIZE	1024
178458f475dSGleb Smirnoff 	char buf[BUFSIZE], goodboy[BUFSIZE], flooder[BUFSIZE], notconn[BUFSIZE];
1796d317723SGleb Smirnoff 
1806d317723SGleb Smirnoff 	/* Establish one to many connection. */
1816d317723SGleb Smirnoff 	ATF_REQUIRE((one = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
1826d317723SGleb Smirnoff 	ATF_REQUIRE(bind(one, (struct sockaddr *)&sun, sizeof(sun)) == 0);
1836d317723SGleb Smirnoff 	/* listen(2) shall fail. */
1846d317723SGleb Smirnoff 	ATF_REQUIRE(listen(one, -1) != 0);
185458f475dSGleb Smirnoff 	for (int i = 0; i < 3; i++) {
1866d317723SGleb Smirnoff 		ATF_REQUIRE((many[i] = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
1876d317723SGleb Smirnoff 		ATF_REQUIRE(connect(many[i], (struct sockaddr *)&sun,
1886d317723SGleb Smirnoff 		    sizeof(sun)) == 0);
1896d317723SGleb Smirnoff 	}
1906d317723SGleb Smirnoff 
1916d317723SGleb Smirnoff 	/* accept() on UNIX/DGRAM is invalid. */
1926d317723SGleb Smirnoff 	ATF_REQUIRE(accept(one, NULL, NULL) == -1);
1936d317723SGleb Smirnoff 	ATF_REQUIRE(errno == EINVAL);
1946d317723SGleb Smirnoff 
1956d317723SGleb Smirnoff 	/*
1966d317723SGleb Smirnoff 	 * Connecting a bound socket to self: a strange, useless, but
1976d317723SGleb Smirnoff 	 * historically existing edge case that is not explicitly described
1986d317723SGleb Smirnoff 	 * in SuS, neither is forbidden there. Works on FreeBSD and Linux.
1996d317723SGleb Smirnoff 	 */
2006d317723SGleb Smirnoff 	ATF_REQUIRE(connect(one, (struct sockaddr *)&sun, sizeof(sun)) == 0);
2016d317723SGleb Smirnoff 	ATF_REQUIRE(send(one, buf, 42, 0) == 42);
2026d317723SGleb Smirnoff 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == 42);
2036d317723SGleb Smirnoff 
2046d317723SGleb Smirnoff 	/*
205458f475dSGleb Smirnoff 	 * Interaction between concurrent senders. New feature in FreeBSD 14.
206458f475dSGleb Smirnoff 	 *
207458f475dSGleb Smirnoff 	 * One sender can not fill the receive side.  Other senders can
208458f475dSGleb Smirnoff 	 * continue operation.  Senders who don't fill their buffers are
209458f475dSGleb Smirnoff 	 * prioritized over flooders.  Connected senders are prioritized over
210458f475dSGleb Smirnoff 	 * unconnected.
211458f475dSGleb Smirnoff 	 *
212458f475dSGleb Smirnoff 	 * Disconnecting a sender that has queued data optionally preserves
213458f475dSGleb Smirnoff 	 * the data.  Allow the data to migrate to peers buffer only if the
214458f475dSGleb Smirnoff 	 * latter is empty.  Otherwise discard it, to prevent against
215458f475dSGleb Smirnoff 	 * connect-fill-close attack.
2166d317723SGleb Smirnoff 	 */
217458f475dSGleb Smirnoff #define	FLOODER	13	/* for connected flooder on many[0] */
218458f475dSGleb Smirnoff #define	GOODBOY	42	/* for a good boy on many[1] */
219458f475dSGleb Smirnoff #define	NOTCONN	66	/* for sendto(2) via two */
220458f475dSGleb Smirnoff 	goodboy[0] = GOODBOY;
221458f475dSGleb Smirnoff 	flooder[0] = FLOODER;
222458f475dSGleb Smirnoff 	notconn[0] = NOTCONN;
2236d317723SGleb Smirnoff 
224458f475dSGleb Smirnoff 	/* Connected priority over sendto(2). */
225458f475dSGleb Smirnoff 	ATF_REQUIRE((two = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
226458f475dSGleb Smirnoff 	ATF_REQUIRE(sendto(two, notconn, BUFSIZE, 0, (struct sockaddr *)&sun,
227458f475dSGleb Smirnoff 	    sizeof(sun)) == BUFSIZE);
228458f475dSGleb Smirnoff 	ATF_REQUIRE(send(many[1], goodboy, BUFSIZE, 0) == BUFSIZE);
2296d317723SGleb Smirnoff 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
230458f475dSGleb Smirnoff 	ATF_REQUIRE(buf[0] == GOODBOY);	/* message from good boy comes first */
231458f475dSGleb Smirnoff 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
232458f475dSGleb Smirnoff 	ATF_REQUIRE(buf[0] == NOTCONN);	/* only then message from sendto(2) */
233458f475dSGleb Smirnoff 
234458f475dSGleb Smirnoff 	/* Casual sender priority over a flooder. */
235458f475dSGleb Smirnoff 	fill(many[0], flooder, sizeof(flooder));
236458f475dSGleb Smirnoff 	ATF_REQUIRE(send(many[0], flooder, BUFSIZE, 0) == -1);
237458f475dSGleb Smirnoff 	ATF_REQUIRE(errno == ENOBUFS);
238458f475dSGleb Smirnoff 	ATF_REQUIRE(send(many[1], goodboy, BUFSIZE, 0) == BUFSIZE);
239458f475dSGleb Smirnoff 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
240458f475dSGleb Smirnoff 	ATF_REQUIRE(buf[0] == GOODBOY);	/* message from good boy comes first */
241458f475dSGleb Smirnoff 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
242458f475dSGleb Smirnoff 	ATF_REQUIRE(buf[0] == FLOODER);	/* only then message from flooder */
243458f475dSGleb Smirnoff 
244458f475dSGleb Smirnoff 	/* Once seen, a message can't be deprioritized by any other message. */
245458f475dSGleb Smirnoff 	ATF_REQUIRE(recv(one, buf, sizeof(buf), MSG_PEEK) == sizeof(buf));
246458f475dSGleb Smirnoff 	ATF_REQUIRE(buf[0] == FLOODER); /* message from the flooder seen */
247458f475dSGleb Smirnoff 	ATF_REQUIRE(send(many[1], goodboy, BUFSIZE, 0) == BUFSIZE);
248458f475dSGleb Smirnoff 	ATF_REQUIRE(recv(one, buf, sizeof(buf), MSG_PEEK) == sizeof(buf));
249458f475dSGleb Smirnoff 	ATF_REQUIRE(buf[0] == FLOODER); /* should be the same message */
250458f475dSGleb Smirnoff 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
251458f475dSGleb Smirnoff 	ATF_REQUIRE(buf[0] == FLOODER); /* now we read it out... */
252458f475dSGleb Smirnoff 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
253458f475dSGleb Smirnoff 	ATF_REQUIRE(buf[0] == GOODBOY); /* ... and next one is the good boy */
254458f475dSGleb Smirnoff 
255458f475dSGleb Smirnoff 	/* Disconnect in presence of data from not connected. */
256458f475dSGleb Smirnoff 	ATF_REQUIRE(sendto(two, notconn, BUFSIZE, 0, (struct sockaddr *)&sun,
257458f475dSGleb Smirnoff 	    sizeof(sun)) == BUFSIZE);
258458f475dSGleb Smirnoff 	close(many[0]);
259458f475dSGleb Smirnoff 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
260458f475dSGleb Smirnoff 	ATF_REQUIRE(buf[0] == NOTCONN);	/* message from sendto() */
261458f475dSGleb Smirnoff 	ATF_REQUIRE(recv(one, buf, sizeof(buf), MSG_DONTWAIT) == -1);
262458f475dSGleb Smirnoff 	ATF_REQUIRE(errno == EAGAIN);	/* data from many[0] discarded */
263458f475dSGleb Smirnoff 
264458f475dSGleb Smirnoff 	/* Disconnect in absence of data from not connected. */
265458f475dSGleb Smirnoff 	ATF_REQUIRE(send(many[1], goodboy, BUFSIZE, 0) == BUFSIZE);
266458f475dSGleb Smirnoff 	close(many[1]);
267458f475dSGleb Smirnoff 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
268458f475dSGleb Smirnoff 	ATF_REQUIRE(buf[0] == GOODBOY);	/* message from many[1] preserved */
269458f475dSGleb Smirnoff 
270458f475dSGleb Smirnoff 	/* Check that nothing leaks on close(2). */
271458f475dSGleb Smirnoff 	ATF_REQUIRE(send(many[2], buf, 42, 0) == 42);
272458f475dSGleb Smirnoff 	ATF_REQUIRE(send(many[2], buf, 42, 0) == 42);
273458f475dSGleb Smirnoff 	ATF_REQUIRE(recv(one, buf, sizeof(buf), MSG_PEEK) == 42);
274458f475dSGleb Smirnoff 	ATF_REQUIRE(sendto(two, notconn, 42, 0, (struct sockaddr *)&sun,
275458f475dSGleb Smirnoff 	    sizeof(sun)) == 42);
276458f475dSGleb Smirnoff 	close(one);
2776d317723SGleb Smirnoff }
2786d317723SGleb Smirnoff 
27970d07b20SGleb Smirnoff /*
28070d07b20SGleb Smirnoff  * Check that various mechanism report socket as readable and having
28170d07b20SGleb Smirnoff  * 42 bytes of data.
28270d07b20SGleb Smirnoff  */
28370d07b20SGleb Smirnoff static void
28470d07b20SGleb Smirnoff test42(int fd)
28570d07b20SGleb Smirnoff {
28670d07b20SGleb Smirnoff 
28770d07b20SGleb Smirnoff 	/* ioctl(FIONREAD) */
28870d07b20SGleb Smirnoff 	int data;
28970d07b20SGleb Smirnoff 
29070d07b20SGleb Smirnoff 	ATF_REQUIRE(ioctl(fd, FIONREAD, &data) != -1);
29170d07b20SGleb Smirnoff 	ATF_REQUIRE(data == 42);
29270d07b20SGleb Smirnoff 
29370d07b20SGleb Smirnoff 	/* select(2) */
29470d07b20SGleb Smirnoff 	fd_set rfds;
29570d07b20SGleb Smirnoff 
29670d07b20SGleb Smirnoff 	FD_ZERO(&rfds);
29770d07b20SGleb Smirnoff 	FD_SET(fd, &rfds);
29870d07b20SGleb Smirnoff 	ATF_REQUIRE(select(fd + 1, &rfds, NULL, NULL, NULL) == 1);
29970d07b20SGleb Smirnoff 	ATF_REQUIRE(FD_ISSET(fd, &rfds));
30070d07b20SGleb Smirnoff 
30170d07b20SGleb Smirnoff 	/* kevent(2) */
30270d07b20SGleb Smirnoff 	struct kevent ev;
30370d07b20SGleb Smirnoff 	int kq;
30470d07b20SGleb Smirnoff 
30570d07b20SGleb Smirnoff 	ATF_REQUIRE((kq = kqueue()) != -1);
30670d07b20SGleb Smirnoff 	EV_SET(&ev, fd, EVFILT_READ, EV_ADD, NOTE_LOWAT, 41, NULL);
30770d07b20SGleb Smirnoff 	ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == 0);
30870d07b20SGleb Smirnoff 	ATF_REQUIRE(kevent(kq, NULL, 0, &ev, 1, NULL) == 1);
30970d07b20SGleb Smirnoff 	ATF_REQUIRE(ev.filter == EVFILT_READ);
31070d07b20SGleb Smirnoff 	ATF_REQUIRE(ev.data == 42);
31170d07b20SGleb Smirnoff 
31270d07b20SGleb Smirnoff 	/* aio(4) */
31370d07b20SGleb Smirnoff 	char buf[50];
31470d07b20SGleb Smirnoff 	struct aiocb aio = {
31570d07b20SGleb Smirnoff 		.aio_nbytes = 50,
31670d07b20SGleb Smirnoff 		.aio_fildes = fd,
31770d07b20SGleb Smirnoff 		.aio_buf = buf,
31870d07b20SGleb Smirnoff 	}, *aiop;
31970d07b20SGleb Smirnoff 
32070d07b20SGleb Smirnoff 	ATF_REQUIRE(aio_read(&aio) == 0);
32170d07b20SGleb Smirnoff 	ATF_REQUIRE(aio_waitcomplete(&aiop, NULL) == 42);
32270d07b20SGleb Smirnoff 	ATF_REQUIRE(aiop == &aio);
32370d07b20SGleb Smirnoff }
32470d07b20SGleb Smirnoff 
32570d07b20SGleb Smirnoff /*
32670d07b20SGleb Smirnoff  * Send data and control in connected & unconnected mode and check that
32770d07b20SGleb Smirnoff  * various event mechanisms see the data, but don't count control bytes.
32870d07b20SGleb Smirnoff  */
32970d07b20SGleb Smirnoff ATF_TC_WITHOUT_HEAD(event);
33070d07b20SGleb Smirnoff ATF_TC_BODY(event, tc)
33170d07b20SGleb Smirnoff {
33270d07b20SGleb Smirnoff 	int fd[2];
33370d07b20SGleb Smirnoff 	char buf[50];
33470d07b20SGleb Smirnoff 	struct iovec iov = {
33570d07b20SGleb Smirnoff 		.iov_base = buf,
33670d07b20SGleb Smirnoff 		.iov_len = 42,
33770d07b20SGleb Smirnoff 	};
33870d07b20SGleb Smirnoff 	struct cmsghdr cmsg = {
33970d07b20SGleb Smirnoff 		.cmsg_len = CMSG_LEN(0),
34070d07b20SGleb Smirnoff 		.cmsg_level = SOL_SOCKET,
34170d07b20SGleb Smirnoff 		.cmsg_type = SCM_TIMESTAMP,
34270d07b20SGleb Smirnoff 	};
34370d07b20SGleb Smirnoff 	struct msghdr msghdr = {
34470d07b20SGleb Smirnoff 		.msg_iov = &iov,
34570d07b20SGleb Smirnoff 		.msg_iovlen = 1,
34670d07b20SGleb Smirnoff 		.msg_control = &cmsg,
34770d07b20SGleb Smirnoff 		.msg_controllen = CMSG_LEN(0),
34870d07b20SGleb Smirnoff 	};
34970d07b20SGleb Smirnoff 
35070d07b20SGleb Smirnoff 	/* Connected socket */
35170d07b20SGleb Smirnoff 	ATF_REQUIRE(socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) != -1);
35270d07b20SGleb Smirnoff 	ATF_REQUIRE(sendmsg(fd[0], &msghdr, 0) == 42);
35370d07b20SGleb Smirnoff 	test42(fd[1]);
35470d07b20SGleb Smirnoff 	close(fd[0]);
35570d07b20SGleb Smirnoff 	close(fd[1]);
35670d07b20SGleb Smirnoff 
35770d07b20SGleb Smirnoff 	/* Not-connected send */
35870d07b20SGleb Smirnoff 	ATF_REQUIRE((fd[0] = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
35970d07b20SGleb Smirnoff 	ATF_REQUIRE((fd[1] = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
36070d07b20SGleb Smirnoff 	ATF_REQUIRE(bind(fd[0], (struct sockaddr *)&sun, sizeof(sun)) == 0);
36170d07b20SGleb Smirnoff 	ATF_REQUIRE(sendto(fd[1], buf, 42, 0, (struct sockaddr *)&sun,
36270d07b20SGleb Smirnoff 	    sizeof(sun)) == 42);
36370d07b20SGleb Smirnoff 	test42(fd[0]);
36470d07b20SGleb Smirnoff }
36570d07b20SGleb Smirnoff 
3666d317723SGleb Smirnoff ATF_TP_ADD_TCS(tp)
3676d317723SGleb Smirnoff {
3686d317723SGleb Smirnoff 
3696d317723SGleb Smirnoff 	ATF_TP_ADD_TC(tp, basic);
3706d317723SGleb Smirnoff 	ATF_TP_ADD_TC(tp, one2many);
37170d07b20SGleb Smirnoff 	ATF_TP_ADD_TC(tp, event);
3726d317723SGleb Smirnoff 
3736d317723SGleb Smirnoff 	return (atf_no_error());
3746d317723SGleb Smirnoff }
375