xref: /freebsd/tests/sys/kern/unix_dgram.c (revision 62ff619dcc3540659a319be71c9a489f1659e14a)
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  * Note that every datagram is prepended with sender address,
59  * size of struct sockaddr.
60  */
61 static void
62 fill(int fd, void *buf, ssize_t len)
63 {
64 	unsigned long recvspace;
65 	size_t llen = sizeof(unsigned long);
66 	ssize_t sent;
67 
68 	ATF_REQUIRE(sysctlbyname("net.local.dgram.recvspace", &recvspace,
69 	    &llen, NULL, 0) == 0);
70 	for (sent = 0;
71 	    sent + len + sizeof(struct sockaddr) < recvspace;
72 	    sent += len + sizeof(struct sockaddr))
73 		ATF_REQUIRE(send(fd, buf, len, 0) == len);
74 }
75 
76 ATF_TC_WITHOUT_HEAD(basic);
77 ATF_TC_BODY(basic, tc)
78 {
79 	struct msghdr msg;
80 	struct iovec iov[1];
81 	unsigned long maxdgram;
82 	size_t llen = sizeof(unsigned long);
83 	int fd[2];
84 	char *buf;
85 
86 	/* Allocate and initialize:
87 	 * - fd[0] to send, fd[1] to receive
88 	 * - buf[maxdgram] for data
89 	 */
90 	ATF_REQUIRE(sysctlbyname("net.local.dgram.maxdgram", &maxdgram,
91 	    &llen, NULL, 0) == 0);
92 	ATF_REQUIRE(socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) != -1);
93 	buf = malloc(maxdgram + 1);
94 	ATF_REQUIRE(buf);
95 	msg = (struct msghdr ){
96 		.msg_iov = iov,
97 		.msg_iovlen = 1,
98 	};
99 	iov[0] = (struct iovec ){
100 		.iov_base = buf,
101 	};
102 
103 	/* Fail to send > maxdgram. */
104 	ATF_REQUIRE(send(fd[0], buf, maxdgram + 1, 0) == -1);
105 	ATF_REQUIRE(errno == EMSGSIZE);
106 
107 	/* Send maxdgram. */
108 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == (ssize_t)maxdgram);
109 
110 	/* Exercise MSG_PEEK, full and truncated.. */
111 	ATF_REQUIRE(recv(fd[1], buf, maxdgram, MSG_PEEK) == (ssize_t)maxdgram);
112 	iov[0].iov_len = 42;
113 	ATF_REQUIRE(recvmsg(fd[1], &msg, MSG_PEEK) == 42);
114 	ATF_REQUIRE(msg.msg_flags == (MSG_PEEK | MSG_TRUNC));
115 
116 	/* Receive maxdgram. */
117 	iov[0].iov_len = maxdgram;
118 	ATF_REQUIRE(recvmsg(fd[1], &msg, 0) == (ssize_t)maxdgram);
119 	ATF_REQUIRE(msg.msg_flags == 0);
120 
121 	/* Receive truncated message. */
122 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == (ssize_t)maxdgram);
123 	iov[0].iov_len = maxdgram / 2;
124 	ATF_REQUIRE(recvmsg(fd[1], &msg, 0) == (ssize_t)maxdgram / 2);
125 	ATF_REQUIRE(msg.msg_flags == MSG_TRUNC);
126 
127 	/* Empty: block. */
128 	ATF_REQUIRE(sigaction(SIGALRM, &sigact, NULL) == 0);
129 	ATF_REQUIRE(timer_done == 0);
130 	ATF_REQUIRE(setitimer(ITIMER_REAL, &itv, NULL) == 0);
131 	ATF_REQUIRE(recv(fd[1], buf, maxdgram, 0) == -1);
132 	ATF_REQUIRE(errno == EINTR);
133 	ATF_REQUIRE(timer_done == 1);
134 
135 	/* Don't block with MSG_DONTWAIT. */
136 	ATF_REQUIRE(recv(fd[1], buf, maxdgram, MSG_DONTWAIT) == -1);
137 	ATF_REQUIRE(errno == EAGAIN);
138 
139 	/* Don't block with O_NONBLOCK. */
140 	ATF_REQUIRE(fcntl(fd[1], F_SETFL, O_NONBLOCK) != -1);
141 	ATF_REQUIRE(recv(fd[1], buf, maxdgram, 0) == -1);
142 	ATF_REQUIRE(errno == EAGAIN);
143 
144 	/* Fail with ENOBUFS on full socket. */
145 	fill(fd[0], buf, maxdgram);
146 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1);
147 	ATF_REQUIRE(errno == ENOBUFS);
148 
149 	/* Fail with EAGAIN with O_NONBLOCK set. */
150 	ATF_REQUIRE(fcntl(fd[0], F_SETFL, O_NONBLOCK) != -1);
151 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1);
152 	ATF_REQUIRE(errno == EAGAIN);
153 
154 	/* Remote side closed -> ECONNRESET. */
155 	close(fd[1]);
156 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1);
157 	ATF_REQUIRE(errno == ECONNRESET);
158 }
159 
160 ATF_TC_WITHOUT_HEAD(one2many);
161 ATF_TC_BODY(one2many, tc)
162 {
163 	struct sockaddr_un sun;
164 	const char *path = "unix_dgram_listener";
165 	int one, many[2], two;
166 	char buf[1024];
167 
168 	/* Establish one to many connection. */
169 	ATF_REQUIRE((one = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
170 	bzero(&sun, sizeof(sun));
171 	sun.sun_family = AF_LOCAL;
172 	sun.sun_len = sizeof(sun);
173 	strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
174 	ATF_REQUIRE(bind(one, (struct sockaddr *)&sun, sizeof(sun)) == 0);
175 	/* listen(2) shall fail. */
176 	ATF_REQUIRE(listen(one, -1) != 0);
177 	for (int i = 0; i < 2; i++) {
178 		ATF_REQUIRE((many[i] = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
179 		ATF_REQUIRE(connect(many[i], (struct sockaddr *)&sun,
180 		    sizeof(sun)) == 0);
181 	}
182 
183 	/* accept() on UNIX/DGRAM is invalid. */
184 	ATF_REQUIRE(accept(one, NULL, NULL) == -1);
185 	ATF_REQUIRE(errno == EINVAL);
186 
187 	/*
188 	 * Connecting a bound socket to self: a strange, useless, but
189 	 * historically existing edge case that is not explicitly described
190 	 * in SuS, neither is forbidden there. Works on FreeBSD and Linux.
191 	 */
192 	ATF_REQUIRE(connect(one, (struct sockaddr *)&sun, sizeof(sun)) == 0);
193 	ATF_REQUIRE(send(one, buf, 42, 0) == 42);
194 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == 42);
195 
196 	/*
197 	 * Sending from an unconnected socket to a bound socket.  Connection is
198 	 * created for the duration of the syscall.
199 	 */
200 	ATF_REQUIRE((two = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
201 	ATF_REQUIRE(sendto(two, buf, 43, 0, (struct sockaddr *)&sun,
202 	    sizeof(sun)) == 43);
203 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == 43);
204 
205 	/* One sender can fill the receive side.
206 	 * Current behavior which needs improvement.
207 	 */
208 	fill(many[0], buf, sizeof(buf));
209 	ATF_REQUIRE(send(many[1], buf, sizeof(buf), 0) == -1);
210 	ATF_REQUIRE(errno == ENOBUFS);
211 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
212 	ATF_REQUIRE(send(many[1], buf, sizeof(buf), 0) == sizeof(buf));
213 }
214 
215 ATF_TP_ADD_TCS(tp)
216 {
217 
218 	ATF_TP_ADD_TC(tp, basic);
219 	ATF_TP_ADD_TC(tp, one2many);
220 
221 	return (atf_no_error());
222 }
223