16d317723SGleb Smirnoff /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
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
2870d07b20SGleb Smirnoff #include <sys/event.h>
2970d07b20SGleb Smirnoff #include <sys/ioctl.h>
3070d07b20SGleb Smirnoff #include <sys/select.h>
316d317723SGleb Smirnoff #include <sys/socket.h>
32*bfd03046SMark Johnston #include <sys/stat.h>
336d317723SGleb Smirnoff #include <sys/sysctl.h>
34*bfd03046SMark Johnston #include <sys/time.h>
356d317723SGleb Smirnoff #include <sys/un.h>
36*bfd03046SMark Johnston
3770d07b20SGleb Smirnoff #include <aio.h>
386d317723SGleb Smirnoff #include <errno.h>
396d317723SGleb Smirnoff #include <fcntl.h>
406d317723SGleb Smirnoff #include <signal.h>
41383d51d5SMark Johnston #include <stdio.h>
426d317723SGleb Smirnoff #include <stdlib.h>
436d317723SGleb Smirnoff
446d317723SGleb Smirnoff #include <atf-c.h>
456d317723SGleb Smirnoff
466d317723SGleb Smirnoff static struct itimerval itv = {
476d317723SGleb Smirnoff .it_interval = { 0, 0 },
486d317723SGleb Smirnoff .it_value = { 1, 0 }, /* one second */
496d317723SGleb Smirnoff };
506d317723SGleb Smirnoff static sig_atomic_t timer_done = 0;
516d317723SGleb Smirnoff static void
sigalarm(int sig __unused)526d317723SGleb Smirnoff sigalarm(int sig __unused)
536d317723SGleb Smirnoff {
546d317723SGleb Smirnoff
556d317723SGleb Smirnoff timer_done = 1;
566d317723SGleb Smirnoff }
576d317723SGleb Smirnoff
586d317723SGleb Smirnoff static struct sigaction sigact = {
596d317723SGleb Smirnoff .sa_handler = sigalarm,
606d317723SGleb Smirnoff };
616d317723SGleb Smirnoff
6270d07b20SGleb Smirnoff static struct sockaddr_un sun = {
6370d07b20SGleb Smirnoff .sun_family = AF_LOCAL,
6470d07b20SGleb Smirnoff .sun_len = sizeof(sun),
6570d07b20SGleb Smirnoff .sun_path = "unix_dgram_listener",
6670d07b20SGleb Smirnoff };
6770d07b20SGleb Smirnoff
686d317723SGleb Smirnoff /*
696d317723SGleb Smirnoff * Fill socket to a state when next send(len) would fail.
70aa9f97afSGleb Smirnoff *
71aa9f97afSGleb Smirnoff * Note that every datagram is prepended with sender address,
72aa9f97afSGleb Smirnoff * size of struct sockaddr.
736d317723SGleb Smirnoff */
746d317723SGleb Smirnoff static void
fill(int fd,void * buf,ssize_t len)756d317723SGleb Smirnoff fill(int fd, void *buf, ssize_t len)
766d317723SGleb Smirnoff {
776d317723SGleb Smirnoff unsigned long recvspace;
786d317723SGleb Smirnoff size_t llen = sizeof(unsigned long);
796d317723SGleb Smirnoff ssize_t sent;
806d317723SGleb Smirnoff
816d317723SGleb Smirnoff ATF_REQUIRE(sysctlbyname("net.local.dgram.recvspace", &recvspace,
826d317723SGleb Smirnoff &llen, NULL, 0) == 0);
83aa9f97afSGleb Smirnoff for (sent = 0;
84aa9f97afSGleb Smirnoff sent + len + sizeof(struct sockaddr) < recvspace;
85aa9f97afSGleb Smirnoff sent += len + sizeof(struct sockaddr))
866d317723SGleb Smirnoff ATF_REQUIRE(send(fd, buf, len, 0) == len);
876d317723SGleb Smirnoff }
886d317723SGleb Smirnoff
896d317723SGleb Smirnoff ATF_TC_WITHOUT_HEAD(basic);
ATF_TC_BODY(basic,tc)906d317723SGleb Smirnoff ATF_TC_BODY(basic, tc)
916d317723SGleb Smirnoff {
926d317723SGleb Smirnoff struct msghdr msg;
936d317723SGleb Smirnoff struct iovec iov[1];
946d317723SGleb Smirnoff unsigned long maxdgram;
956d317723SGleb Smirnoff size_t llen = sizeof(unsigned long);
966d317723SGleb Smirnoff int fd[2];
976d317723SGleb Smirnoff char *buf;
986d317723SGleb Smirnoff
996d317723SGleb Smirnoff /* Allocate and initialize:
1006d317723SGleb Smirnoff * - fd[0] to send, fd[1] to receive
1016d317723SGleb Smirnoff * - buf[maxdgram] for data
1026d317723SGleb Smirnoff */
1036d317723SGleb Smirnoff ATF_REQUIRE(sysctlbyname("net.local.dgram.maxdgram", &maxdgram,
1046d317723SGleb Smirnoff &llen, NULL, 0) == 0);
1056d317723SGleb Smirnoff ATF_REQUIRE(socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) != -1);
1066d317723SGleb Smirnoff buf = malloc(maxdgram + 1);
1076d317723SGleb Smirnoff ATF_REQUIRE(buf);
1086d317723SGleb Smirnoff msg = (struct msghdr ){
1096d317723SGleb Smirnoff .msg_iov = iov,
1106d317723SGleb Smirnoff .msg_iovlen = 1,
1116d317723SGleb Smirnoff };
1126d317723SGleb Smirnoff iov[0] = (struct iovec ){
1136d317723SGleb Smirnoff .iov_base = buf,
1146d317723SGleb Smirnoff };
1156d317723SGleb Smirnoff
1166d317723SGleb Smirnoff /* Fail to send > maxdgram. */
1176d317723SGleb Smirnoff ATF_REQUIRE(send(fd[0], buf, maxdgram + 1, 0) == -1);
1186d317723SGleb Smirnoff ATF_REQUIRE(errno == EMSGSIZE);
1196d317723SGleb Smirnoff
1206d317723SGleb Smirnoff /* Send maxdgram. */
1216d317723SGleb Smirnoff ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == (ssize_t)maxdgram);
1226d317723SGleb Smirnoff
1236d317723SGleb Smirnoff /* Exercise MSG_PEEK, full and truncated.. */
1246d317723SGleb Smirnoff ATF_REQUIRE(recv(fd[1], buf, maxdgram, MSG_PEEK) == (ssize_t)maxdgram);
1256d317723SGleb Smirnoff iov[0].iov_len = 42;
1266d317723SGleb Smirnoff ATF_REQUIRE(recvmsg(fd[1], &msg, MSG_PEEK) == 42);
1276d317723SGleb Smirnoff ATF_REQUIRE(msg.msg_flags == (MSG_PEEK | MSG_TRUNC));
1286d317723SGleb Smirnoff
1296d317723SGleb Smirnoff /* Receive maxdgram. */
1306d317723SGleb Smirnoff iov[0].iov_len = maxdgram;
1316d317723SGleb Smirnoff ATF_REQUIRE(recvmsg(fd[1], &msg, 0) == (ssize_t)maxdgram);
1326d317723SGleb Smirnoff ATF_REQUIRE(msg.msg_flags == 0);
1336d317723SGleb Smirnoff
1346d317723SGleb Smirnoff /* Receive truncated message. */
1356d317723SGleb Smirnoff ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == (ssize_t)maxdgram);
1366d317723SGleb Smirnoff iov[0].iov_len = maxdgram / 2;
1376d317723SGleb Smirnoff ATF_REQUIRE(recvmsg(fd[1], &msg, 0) == (ssize_t)maxdgram / 2);
1386d317723SGleb Smirnoff ATF_REQUIRE(msg.msg_flags == MSG_TRUNC);
1396d317723SGleb Smirnoff
1406d317723SGleb Smirnoff /* Empty: block. */
1416d317723SGleb Smirnoff ATF_REQUIRE(sigaction(SIGALRM, &sigact, NULL) == 0);
1426d317723SGleb Smirnoff ATF_REQUIRE(timer_done == 0);
1436d317723SGleb Smirnoff ATF_REQUIRE(setitimer(ITIMER_REAL, &itv, NULL) == 0);
1446d317723SGleb Smirnoff ATF_REQUIRE(recv(fd[1], buf, maxdgram, 0) == -1);
1456d317723SGleb Smirnoff ATF_REQUIRE(errno == EINTR);
1466d317723SGleb Smirnoff ATF_REQUIRE(timer_done == 1);
1476d317723SGleb Smirnoff
1486d317723SGleb Smirnoff /* Don't block with MSG_DONTWAIT. */
1496d317723SGleb Smirnoff ATF_REQUIRE(recv(fd[1], buf, maxdgram, MSG_DONTWAIT) == -1);
1506d317723SGleb Smirnoff ATF_REQUIRE(errno == EAGAIN);
1516d317723SGleb Smirnoff
1526d317723SGleb Smirnoff /* Don't block with O_NONBLOCK. */
1536d317723SGleb Smirnoff ATF_REQUIRE(fcntl(fd[1], F_SETFL, O_NONBLOCK) != -1);
1546d317723SGleb Smirnoff ATF_REQUIRE(recv(fd[1], buf, maxdgram, 0) == -1);
1556d317723SGleb Smirnoff ATF_REQUIRE(errno == EAGAIN);
1566d317723SGleb Smirnoff
1576d317723SGleb Smirnoff /* Fail with ENOBUFS on full socket. */
1586d317723SGleb Smirnoff fill(fd[0], buf, maxdgram);
1596d317723SGleb Smirnoff ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1);
1606d317723SGleb Smirnoff ATF_REQUIRE(errno == ENOBUFS);
1616d317723SGleb Smirnoff
162a1706571SGleb Smirnoff /*
163a1706571SGleb Smirnoff * Fail with ENOBUFS with O_NONBLOCK set, too. See 71e70c25c00
164a1706571SGleb Smirnoff * for explanation why this behavior needs to be preserved.
165a1706571SGleb Smirnoff */
1666d317723SGleb Smirnoff ATF_REQUIRE(fcntl(fd[0], F_SETFL, O_NONBLOCK) != -1);
1676d317723SGleb Smirnoff ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1);
168a1706571SGleb Smirnoff ATF_REQUIRE(errno == ENOBUFS);
1696d317723SGleb Smirnoff
1706d317723SGleb Smirnoff /* Remote side closed -> ECONNRESET. */
1716d317723SGleb Smirnoff close(fd[1]);
1726d317723SGleb Smirnoff ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1);
1736d317723SGleb Smirnoff ATF_REQUIRE(errno == ECONNRESET);
1746d317723SGleb Smirnoff }
1756d317723SGleb Smirnoff
1766d317723SGleb Smirnoff ATF_TC_WITHOUT_HEAD(one2many);
ATF_TC_BODY(one2many,tc)1776d317723SGleb Smirnoff ATF_TC_BODY(one2many, tc)
1786d317723SGleb Smirnoff {
179458f475dSGleb Smirnoff int one, many[3], two;
180458f475dSGleb Smirnoff #define BUFSIZE 1024
181458f475dSGleb Smirnoff char buf[BUFSIZE], goodboy[BUFSIZE], flooder[BUFSIZE], notconn[BUFSIZE];
1826d317723SGleb Smirnoff
1836d317723SGleb Smirnoff /* Establish one to many connection. */
1846d317723SGleb Smirnoff ATF_REQUIRE((one = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
1856d317723SGleb Smirnoff ATF_REQUIRE(bind(one, (struct sockaddr *)&sun, sizeof(sun)) == 0);
1866d317723SGleb Smirnoff /* listen(2) shall fail. */
1876d317723SGleb Smirnoff ATF_REQUIRE(listen(one, -1) != 0);
188458f475dSGleb Smirnoff for (int i = 0; i < 3; i++) {
1896d317723SGleb Smirnoff ATF_REQUIRE((many[i] = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
1906d317723SGleb Smirnoff ATF_REQUIRE(connect(many[i], (struct sockaddr *)&sun,
1916d317723SGleb Smirnoff sizeof(sun)) == 0);
1926d317723SGleb Smirnoff }
1936d317723SGleb Smirnoff
1946d317723SGleb Smirnoff /* accept() on UNIX/DGRAM is invalid. */
1956d317723SGleb Smirnoff ATF_REQUIRE(accept(one, NULL, NULL) == -1);
1966d317723SGleb Smirnoff ATF_REQUIRE(errno == EINVAL);
1976d317723SGleb Smirnoff
1986d317723SGleb Smirnoff /*
1996d317723SGleb Smirnoff * Connecting a bound socket to self: a strange, useless, but
2006d317723SGleb Smirnoff * historically existing edge case that is not explicitly described
2016d317723SGleb Smirnoff * in SuS, neither is forbidden there. Works on FreeBSD and Linux.
2026d317723SGleb Smirnoff */
2036d317723SGleb Smirnoff ATF_REQUIRE(connect(one, (struct sockaddr *)&sun, sizeof(sun)) == 0);
2046d317723SGleb Smirnoff ATF_REQUIRE(send(one, buf, 42, 0) == 42);
2056d317723SGleb Smirnoff ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == 42);
2066d317723SGleb Smirnoff
2076d317723SGleb Smirnoff /*
208458f475dSGleb Smirnoff * Interaction between concurrent senders. New feature in FreeBSD 14.
209458f475dSGleb Smirnoff *
210458f475dSGleb Smirnoff * One sender can not fill the receive side. Other senders can
211458f475dSGleb Smirnoff * continue operation. Senders who don't fill their buffers are
212458f475dSGleb Smirnoff * prioritized over flooders. Connected senders are prioritized over
213458f475dSGleb Smirnoff * unconnected.
214458f475dSGleb Smirnoff *
215458f475dSGleb Smirnoff * Disconnecting a sender that has queued data optionally preserves
216458f475dSGleb Smirnoff * the data. Allow the data to migrate to peers buffer only if the
217458f475dSGleb Smirnoff * latter is empty. Otherwise discard it, to prevent against
218458f475dSGleb Smirnoff * connect-fill-close attack.
2196d317723SGleb Smirnoff */
220458f475dSGleb Smirnoff #define FLOODER 13 /* for connected flooder on many[0] */
221458f475dSGleb Smirnoff #define GOODBOY 42 /* for a good boy on many[1] */
222458f475dSGleb Smirnoff #define NOTCONN 66 /* for sendto(2) via two */
223458f475dSGleb Smirnoff goodboy[0] = GOODBOY;
224458f475dSGleb Smirnoff flooder[0] = FLOODER;
225458f475dSGleb Smirnoff notconn[0] = NOTCONN;
2266d317723SGleb Smirnoff
227458f475dSGleb Smirnoff /* Connected priority over sendto(2). */
228458f475dSGleb Smirnoff ATF_REQUIRE((two = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
229458f475dSGleb Smirnoff ATF_REQUIRE(sendto(two, notconn, BUFSIZE, 0, (struct sockaddr *)&sun,
230458f475dSGleb Smirnoff sizeof(sun)) == BUFSIZE);
231458f475dSGleb Smirnoff ATF_REQUIRE(send(many[1], goodboy, BUFSIZE, 0) == BUFSIZE);
2326d317723SGleb Smirnoff ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
233458f475dSGleb Smirnoff ATF_REQUIRE(buf[0] == GOODBOY); /* message from good boy comes first */
234458f475dSGleb Smirnoff ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
235458f475dSGleb Smirnoff ATF_REQUIRE(buf[0] == NOTCONN); /* only then message from sendto(2) */
236458f475dSGleb Smirnoff
237458f475dSGleb Smirnoff /* Casual sender priority over a flooder. */
238458f475dSGleb Smirnoff fill(many[0], flooder, sizeof(flooder));
239458f475dSGleb Smirnoff ATF_REQUIRE(send(many[0], flooder, BUFSIZE, 0) == -1);
240458f475dSGleb Smirnoff ATF_REQUIRE(errno == ENOBUFS);
241458f475dSGleb Smirnoff ATF_REQUIRE(send(many[1], goodboy, BUFSIZE, 0) == BUFSIZE);
242458f475dSGleb Smirnoff ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
243458f475dSGleb Smirnoff ATF_REQUIRE(buf[0] == GOODBOY); /* message from good boy comes first */
244458f475dSGleb Smirnoff ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
245458f475dSGleb Smirnoff ATF_REQUIRE(buf[0] == FLOODER); /* only then message from flooder */
246458f475dSGleb Smirnoff
247458f475dSGleb Smirnoff /* Once seen, a message can't be deprioritized by any other message. */
248458f475dSGleb Smirnoff ATF_REQUIRE(recv(one, buf, sizeof(buf), MSG_PEEK) == sizeof(buf));
249458f475dSGleb Smirnoff ATF_REQUIRE(buf[0] == FLOODER); /* message from the flooder seen */
250458f475dSGleb Smirnoff ATF_REQUIRE(send(many[1], goodboy, BUFSIZE, 0) == BUFSIZE);
251458f475dSGleb Smirnoff ATF_REQUIRE(recv(one, buf, sizeof(buf), MSG_PEEK) == sizeof(buf));
252458f475dSGleb Smirnoff ATF_REQUIRE(buf[0] == FLOODER); /* should be the same message */
253458f475dSGleb Smirnoff ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
254458f475dSGleb Smirnoff ATF_REQUIRE(buf[0] == FLOODER); /* now we read it out... */
255458f475dSGleb Smirnoff ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
256458f475dSGleb Smirnoff ATF_REQUIRE(buf[0] == GOODBOY); /* ... and next one is the good boy */
257458f475dSGleb Smirnoff
258458f475dSGleb Smirnoff /* Disconnect in presence of data from not connected. */
259458f475dSGleb Smirnoff ATF_REQUIRE(sendto(two, notconn, BUFSIZE, 0, (struct sockaddr *)&sun,
260458f475dSGleb Smirnoff sizeof(sun)) == BUFSIZE);
261458f475dSGleb Smirnoff close(many[0]);
262458f475dSGleb Smirnoff ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
263458f475dSGleb Smirnoff ATF_REQUIRE(buf[0] == NOTCONN); /* message from sendto() */
264458f475dSGleb Smirnoff ATF_REQUIRE(recv(one, buf, sizeof(buf), MSG_DONTWAIT) == -1);
265458f475dSGleb Smirnoff ATF_REQUIRE(errno == EAGAIN); /* data from many[0] discarded */
266458f475dSGleb Smirnoff
267458f475dSGleb Smirnoff /* Disconnect in absence of data from not connected. */
268458f475dSGleb Smirnoff ATF_REQUIRE(send(many[1], goodboy, BUFSIZE, 0) == BUFSIZE);
269458f475dSGleb Smirnoff close(many[1]);
270458f475dSGleb Smirnoff ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
271458f475dSGleb Smirnoff ATF_REQUIRE(buf[0] == GOODBOY); /* message from many[1] preserved */
272458f475dSGleb Smirnoff
273458f475dSGleb Smirnoff /* Check that nothing leaks on close(2). */
274458f475dSGleb Smirnoff ATF_REQUIRE(send(many[2], buf, 42, 0) == 42);
275458f475dSGleb Smirnoff ATF_REQUIRE(send(many[2], buf, 42, 0) == 42);
276458f475dSGleb Smirnoff ATF_REQUIRE(recv(one, buf, sizeof(buf), MSG_PEEK) == 42);
277458f475dSGleb Smirnoff ATF_REQUIRE(sendto(two, notconn, 42, 0, (struct sockaddr *)&sun,
278458f475dSGleb Smirnoff sizeof(sun)) == 42);
279458f475dSGleb Smirnoff close(one);
2806d317723SGleb Smirnoff }
2816d317723SGleb Smirnoff
28270d07b20SGleb Smirnoff /*
28370d07b20SGleb Smirnoff * Check that various mechanism report socket as readable and having
28470d07b20SGleb Smirnoff * 42 bytes of data.
28570d07b20SGleb Smirnoff */
28670d07b20SGleb Smirnoff static void
test42(int fd)28770d07b20SGleb Smirnoff test42(int fd)
28870d07b20SGleb Smirnoff {
28970d07b20SGleb Smirnoff
29070d07b20SGleb Smirnoff /* ioctl(FIONREAD) */
29170d07b20SGleb Smirnoff int data;
29270d07b20SGleb Smirnoff
29370d07b20SGleb Smirnoff ATF_REQUIRE(ioctl(fd, FIONREAD, &data) != -1);
29470d07b20SGleb Smirnoff ATF_REQUIRE(data == 42);
29570d07b20SGleb Smirnoff
29670d07b20SGleb Smirnoff /* select(2) */
29770d07b20SGleb Smirnoff fd_set rfds;
29870d07b20SGleb Smirnoff
29970d07b20SGleb Smirnoff FD_ZERO(&rfds);
30070d07b20SGleb Smirnoff FD_SET(fd, &rfds);
30170d07b20SGleb Smirnoff ATF_REQUIRE(select(fd + 1, &rfds, NULL, NULL, NULL) == 1);
30270d07b20SGleb Smirnoff ATF_REQUIRE(FD_ISSET(fd, &rfds));
30370d07b20SGleb Smirnoff
30470d07b20SGleb Smirnoff /* kevent(2) */
30570d07b20SGleb Smirnoff struct kevent ev;
30670d07b20SGleb Smirnoff int kq;
30770d07b20SGleb Smirnoff
30870d07b20SGleb Smirnoff ATF_REQUIRE((kq = kqueue()) != -1);
30970d07b20SGleb Smirnoff EV_SET(&ev, fd, EVFILT_READ, EV_ADD, NOTE_LOWAT, 41, NULL);
31070d07b20SGleb Smirnoff ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == 0);
31170d07b20SGleb Smirnoff ATF_REQUIRE(kevent(kq, NULL, 0, &ev, 1, NULL) == 1);
31270d07b20SGleb Smirnoff ATF_REQUIRE(ev.filter == EVFILT_READ);
31370d07b20SGleb Smirnoff ATF_REQUIRE(ev.data == 42);
31470d07b20SGleb Smirnoff
31570d07b20SGleb Smirnoff /* aio(4) */
31670d07b20SGleb Smirnoff char buf[50];
31770d07b20SGleb Smirnoff struct aiocb aio = {
31870d07b20SGleb Smirnoff .aio_nbytes = 50,
31970d07b20SGleb Smirnoff .aio_fildes = fd,
32070d07b20SGleb Smirnoff .aio_buf = buf,
32170d07b20SGleb Smirnoff }, *aiop;
32270d07b20SGleb Smirnoff
32370d07b20SGleb Smirnoff ATF_REQUIRE(aio_read(&aio) == 0);
32470d07b20SGleb Smirnoff ATF_REQUIRE(aio_waitcomplete(&aiop, NULL) == 42);
32570d07b20SGleb Smirnoff ATF_REQUIRE(aiop == &aio);
32670d07b20SGleb Smirnoff }
32770d07b20SGleb Smirnoff
32870d07b20SGleb Smirnoff /*
32970d07b20SGleb Smirnoff * Send data and control in connected & unconnected mode and check that
33070d07b20SGleb Smirnoff * various event mechanisms see the data, but don't count control bytes.
33170d07b20SGleb Smirnoff */
33270d07b20SGleb Smirnoff ATF_TC_WITHOUT_HEAD(event);
ATF_TC_BODY(event,tc)33370d07b20SGleb Smirnoff ATF_TC_BODY(event, tc)
33470d07b20SGleb Smirnoff {
33570d07b20SGleb Smirnoff int fd[2];
33670d07b20SGleb Smirnoff char buf[50];
33770d07b20SGleb Smirnoff struct iovec iov = {
33870d07b20SGleb Smirnoff .iov_base = buf,
33970d07b20SGleb Smirnoff .iov_len = 42,
34070d07b20SGleb Smirnoff };
34170d07b20SGleb Smirnoff struct cmsghdr cmsg = {
34270d07b20SGleb Smirnoff .cmsg_len = CMSG_LEN(0),
34370d07b20SGleb Smirnoff .cmsg_level = SOL_SOCKET,
34470d07b20SGleb Smirnoff .cmsg_type = SCM_TIMESTAMP,
34570d07b20SGleb Smirnoff };
34670d07b20SGleb Smirnoff struct msghdr msghdr = {
34770d07b20SGleb Smirnoff .msg_iov = &iov,
34870d07b20SGleb Smirnoff .msg_iovlen = 1,
34970d07b20SGleb Smirnoff .msg_control = &cmsg,
35070d07b20SGleb Smirnoff .msg_controllen = CMSG_LEN(0),
35170d07b20SGleb Smirnoff };
35270d07b20SGleb Smirnoff
35370d07b20SGleb Smirnoff /* Connected socket */
35470d07b20SGleb Smirnoff ATF_REQUIRE(socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) != -1);
35570d07b20SGleb Smirnoff ATF_REQUIRE(sendmsg(fd[0], &msghdr, 0) == 42);
35670d07b20SGleb Smirnoff test42(fd[1]);
35770d07b20SGleb Smirnoff close(fd[0]);
35870d07b20SGleb Smirnoff close(fd[1]);
35970d07b20SGleb Smirnoff
36070d07b20SGleb Smirnoff /* Not-connected send */
36170d07b20SGleb Smirnoff ATF_REQUIRE((fd[0] = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
36270d07b20SGleb Smirnoff ATF_REQUIRE((fd[1] = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
36370d07b20SGleb Smirnoff ATF_REQUIRE(bind(fd[0], (struct sockaddr *)&sun, sizeof(sun)) == 0);
36470d07b20SGleb Smirnoff ATF_REQUIRE(sendto(fd[1], buf, 42, 0, (struct sockaddr *)&sun,
36570d07b20SGleb Smirnoff sizeof(sun)) == 42);
36670d07b20SGleb Smirnoff test42(fd[0]);
36770d07b20SGleb Smirnoff }
36870d07b20SGleb Smirnoff
369383d51d5SMark Johnston ATF_TC_WITHOUT_HEAD(selfgetpeername);
ATF_TC_BODY(selfgetpeername,tc)370383d51d5SMark Johnston ATF_TC_BODY(selfgetpeername, tc)
371383d51d5SMark Johnston {
372383d51d5SMark Johnston struct sockaddr_un sun;
373383d51d5SMark Johnston const char *name;
374383d51d5SMark Johnston socklen_t len;
375383d51d5SMark Johnston int sd;
376383d51d5SMark Johnston
377383d51d5SMark Johnston name = "selfgetpeername";
378383d51d5SMark Johnston
379383d51d5SMark Johnston sd = socket(PF_UNIX, SOCK_DGRAM, 0);
380383d51d5SMark Johnston ATF_REQUIRE(sd != -1);
381383d51d5SMark Johnston
382383d51d5SMark Johnston memset(&sun, 0, sizeof(sun));
383383d51d5SMark Johnston sun.sun_len = sizeof(sun);
384383d51d5SMark Johnston sun.sun_family = AF_UNIX;
385383d51d5SMark Johnston snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", name);
386383d51d5SMark Johnston ATF_REQUIRE(bind(sd, (struct sockaddr *)&sun, sizeof(sun)) == 0);
387383d51d5SMark Johnston ATF_REQUIRE(connect(sd, (struct sockaddr *)&sun, sizeof(sun)) == 0);
388383d51d5SMark Johnston
389383d51d5SMark Johnston len = sizeof(sun);
390383d51d5SMark Johnston ATF_REQUIRE(getpeername(sd, (struct sockaddr *)&sun, &len) == 0);
391383d51d5SMark Johnston ATF_REQUIRE(strcmp(sun.sun_path, name) == 0);
392383d51d5SMark Johnston
393383d51d5SMark Johnston ATF_REQUIRE(close(sd) == 0);
394383d51d5SMark Johnston }
395383d51d5SMark Johnston
396*bfd03046SMark Johnston ATF_TC_WITHOUT_HEAD(fchmod);
ATF_TC_BODY(fchmod,tc)397*bfd03046SMark Johnston ATF_TC_BODY(fchmod, tc)
398*bfd03046SMark Johnston {
399*bfd03046SMark Johnston struct stat sb;
400*bfd03046SMark Johnston struct sockaddr_un sun;
401*bfd03046SMark Johnston int error, sd;
402*bfd03046SMark Johnston
403*bfd03046SMark Johnston memset(&sun, 0, sizeof(sun));
404*bfd03046SMark Johnston sun.sun_len = sizeof(sun);
405*bfd03046SMark Johnston sun.sun_family = AF_UNIX;
406*bfd03046SMark Johnston strlcpy(sun.sun_path, "sock", sizeof(sun.sun_path));
407*bfd03046SMark Johnston
408*bfd03046SMark Johnston sd = socket(PF_UNIX, SOCK_DGRAM, 0);
409*bfd03046SMark Johnston ATF_REQUIRE(sd != -1);
410*bfd03046SMark Johnston
411*bfd03046SMark Johnston error = fchmod(sd, 0600 | S_ISUID);
412*bfd03046SMark Johnston ATF_REQUIRE_ERRNO(EINVAL, error == -1);
413*bfd03046SMark Johnston
414*bfd03046SMark Johnston umask(0022);
415*bfd03046SMark Johnston error = fchmod(sd, 0766);
416*bfd03046SMark Johnston ATF_REQUIRE(error == 0);
417*bfd03046SMark Johnston
418*bfd03046SMark Johnston error = bind(sd, (struct sockaddr *)&sun, sizeof(sun));
419*bfd03046SMark Johnston ATF_REQUIRE(error == 0);
420*bfd03046SMark Johnston
421*bfd03046SMark Johnston error = stat(sun.sun_path, &sb);
422*bfd03046SMark Johnston ATF_REQUIRE(error == 0);
423*bfd03046SMark Johnston ATF_REQUIRE_MSG((sb.st_mode & 0777) == 0744,
424*bfd03046SMark Johnston "sb.st_mode = %o", sb.st_mode);
425*bfd03046SMark Johnston
426*bfd03046SMark Johnston error = fchmod(sd, 0666);
427*bfd03046SMark Johnston ATF_REQUIRE_ERRNO(EINVAL, error == -1);
428*bfd03046SMark Johnston
429*bfd03046SMark Johnston ATF_REQUIRE(close(sd) == 0);
430*bfd03046SMark Johnston }
431*bfd03046SMark Johnston
ATF_TP_ADD_TCS(tp)4326d317723SGleb Smirnoff ATF_TP_ADD_TCS(tp)
4336d317723SGleb Smirnoff {
4346d317723SGleb Smirnoff ATF_TP_ADD_TC(tp, basic);
4356d317723SGleb Smirnoff ATF_TP_ADD_TC(tp, one2many);
43670d07b20SGleb Smirnoff ATF_TP_ADD_TC(tp, event);
437383d51d5SMark Johnston ATF_TP_ADD_TC(tp, selfgetpeername);
438*bfd03046SMark Johnston ATF_TP_ADD_TC(tp, fchmod);
4396d317723SGleb Smirnoff
4406d317723SGleb Smirnoff return (atf_no_error());
4416d317723SGleb Smirnoff }
442