2 * Copyright (c) 2005 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
30 #include <sys/socket.h>
32 #include <sys/sysctl.h>
44 * UNIX domain sockets allow file descriptors to be passed via "ancillary
45 * data", or control messages. This regression test is intended to exercise
46 * this facility, both performing some basic tests that it operates, and also
47 * causing some kernel edge cases to execute, such as garbage collection when
48 * there are cyclic file descriptor references. Right now we test only with
49 * stream sockets, but ideally we'd also test with datagram sockets.
53 domainsocketpair(const char *test, int *fdp)
56 if (socketpair(PF_UNIX, SOCK_STREAM, 0, fdp) < 0)
57 err(-1, "%s: socketpair(PF_UNIX, SOCK_STREAM)", test);
61 closesocketpair(int *fdp)
69 devnull(const char *test, int *fdp)
73 fd = open("/dev/null", O_RDONLY);
75 err(-1, "%s: open(/dev/null)", test);
80 tempfile(const char *test, int *fdp)
85 snprintf(path, PATH_MAX, "/tmp/unix_passfd.XXXXXXXXXXXXXXX");
88 err(-1, "%s: mkstemp(%s)", test, path);
94 dofstat(const char *test, int fd, struct stat *sb)
97 if (fstat(fd, sb) < 0)
98 err(-1, "%s: fstat", test);
102 samefile(const char *test, struct stat *sb1, struct stat *sb2)
105 if (sb1->st_dev != sb2->st_dev)
106 errx(-1, "%s: samefile: different device", test);
107 if (sb1->st_ino != sb2->st_ino)
108 errx(-1, "%s: samefile: different inode", test);
112 sendfd_payload(const char *test, int sockfd, int sendfd,
113 void *payload, size_t paylen)
116 char message[CMSG_SPACE(sizeof(int))];
117 struct cmsghdr *cmsghdr;
118 struct msghdr msghdr;
121 bzero(&msghdr, sizeof(msghdr));
122 bzero(&message, sizeof(message));
124 msghdr.msg_control = message;
125 msghdr.msg_controllen = sizeof(message);
127 iovec.iov_base = payload;
128 iovec.iov_len = paylen;
130 msghdr.msg_iov = &iovec;
131 msghdr.msg_iovlen = 1;
133 cmsghdr = (struct cmsghdr *)message;
134 cmsghdr->cmsg_len = CMSG_LEN(sizeof(int));
135 cmsghdr->cmsg_level = SOL_SOCKET;
136 cmsghdr->cmsg_type = SCM_RIGHTS;
137 *(int *)CMSG_DATA(cmsghdr) = sendfd;
139 len = sendmsg(sockfd, &msghdr, 0);
141 err(-1, "%s: sendmsg", test);
142 if ((size_t)len != paylen)
143 errx(-1, "%s: sendmsg: %zd bytes sent", test, len);
147 sendfd(const char *test, int sockfd, int sendfd)
151 return (sendfd_payload(test, sockfd, sendfd, &ch, sizeof(ch)));
155 recvfd_payload(const char *test, int sockfd, int *recvfd,
156 void *buf, size_t buflen)
158 struct cmsghdr *cmsghdr;
159 char message[CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + sizeof(int)];
160 struct msghdr msghdr;
164 bzero(&msghdr, sizeof(msghdr));
166 msghdr.msg_control = message;
167 msghdr.msg_controllen = sizeof(message);
169 iovec.iov_base = buf;
170 iovec.iov_len = buflen;
172 msghdr.msg_iov = &iovec;
173 msghdr.msg_iovlen = 1;
175 len = recvmsg(sockfd, &msghdr, 0);
177 err(-1, "%s: recvmsg", test);
178 if ((size_t)len != buflen)
179 errx(-1, "%s: recvmsg: %zd bytes received", test, len);
181 cmsghdr = CMSG_FIRSTHDR(&msghdr);
183 errx(-1, "%s: recvmsg: did not receive control message", test);
185 for (; cmsghdr != NULL; cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) {
186 if (cmsghdr->cmsg_level == SOL_SOCKET &&
187 cmsghdr->cmsg_type == SCM_RIGHTS &&
188 cmsghdr->cmsg_len == CMSG_LEN(sizeof(int))) {
189 *recvfd = *(int *)CMSG_DATA(cmsghdr);
191 errx(-1, "%s: recvmsg: received fd -1", test);
195 errx(-1, "%s: recvmsg: did not receive single-fd message",
200 recvfd(const char *test, int sockfd, int *recvfd)
204 return (recvfd_payload(test, sockfd, recvfd, &ch, sizeof(ch)));
210 struct stat putfd_1_stat, putfd_2_stat, getfd_1_stat, getfd_2_stat;
211 int fd[2], putfd_1, putfd_2, getfd_1, getfd_2;
215 * First test: put a temporary file into a UNIX domain socket, then
216 * take it out and make sure it's the same file. First time around,
217 * don't close the reference after sending.
219 test = "test1-simplesendfd";
220 printf("beginning %s\n", test);
222 domainsocketpair(test, fd);
223 tempfile(test, &putfd_1);
224 dofstat(test, putfd_1, &putfd_1_stat);
225 sendfd(test, fd[0], putfd_1);
226 recvfd(test, fd[1], &getfd_1);
227 dofstat(test, getfd_1, &getfd_1_stat);
228 samefile(test, &putfd_1_stat, &getfd_1_stat);
233 printf("%s passed\n", test);
236 * Second test: same as first, only close the file reference after
237 * sending, so that the only reference is the descriptor in the UNIX
238 * domain socket buffer.
240 test = "test2-sendandclose";
241 printf("beginning %s\n", test);
243 domainsocketpair(test, fd);
244 tempfile(test, &putfd_1);
245 dofstat(test, putfd_1, &putfd_1_stat);
246 sendfd(test, fd[0], putfd_1);
248 recvfd(test, fd[1], &getfd_1);
249 dofstat(test, getfd_1, &getfd_1_stat);
250 samefile(test, &putfd_1_stat, &getfd_1_stat);
254 printf("%s passed\n", test);
257 * Third test: put a temporary file into a UNIX domain socket, then
258 * close both endpoints causing garbage collection to kick off.
260 test = "test3-sendandcancel";
261 printf("beginning %s\n", test);
263 domainsocketpair(test, fd);
264 tempfile(test, &putfd_1);
265 sendfd(test, fd[0], putfd_1);
269 printf("%s passed\n", test);
272 * Send two files. Then receive them. Make sure they are returned
273 * in the right order, and both get there.
276 test = "test4-twofile";
277 printf("beginning %s\n", test);
279 domainsocketpair(test, fd);
280 tempfile(test, &putfd_1);
281 tempfile(test, &putfd_2);
282 dofstat(test, putfd_1, &putfd_1_stat);
283 dofstat(test, putfd_2, &putfd_2_stat);
284 sendfd(test, fd[0], putfd_1);
285 sendfd(test, fd[0], putfd_2);
288 recvfd(test, fd[1], &getfd_1);
289 recvfd(test, fd[1], &getfd_2);
290 dofstat(test, getfd_1, &getfd_1_stat);
291 dofstat(test, getfd_2, &getfd_2_stat);
292 samefile(test, &putfd_1_stat, &getfd_1_stat);
293 samefile(test, &putfd_2_stat, &getfd_2_stat);
298 printf("%s passed\n", test);
301 * Big bundling test. Send an endpoint of the UNIX domain socket
302 * over itself, closing the door behind it.
305 test = "test5-bundle";
306 printf("beginning %s\n", test);
308 domainsocketpair(test, fd);
310 sendfd(test, fd[0], fd[0]);
312 recvfd(test, fd[1], &getfd_1);
316 printf("%s passed\n", test);
319 * Big bundling test part two: Send an endpoint of the UNIX domain
320 * socket over itself, close the door behind it, and never remove it
321 * from the other end.
324 test = "test6-bundlecancel";
325 printf("beginning %s\n", test);
327 domainsocketpair(test, fd);
328 sendfd(test, fd[0], fd[0]);
329 sendfd(test, fd[1], fd[0]);
332 printf("%s passed\n", test);
335 * Test for PR 151758: Send an character device over the UNIX
336 * domain socket and then close both sockets to orphan the
340 test = "test7-devfsorphan";
341 printf("beginning %s\n", test);
343 domainsocketpair(test, fd);
344 devnull(test, &putfd_1);
345 sendfd(test, fd[0], putfd_1);
349 printf("%s passed\n", test);
352 * Test for PR 181741. Receiver sets LOCAL_CREDS, and kernel
353 * prepends a control message to the data. Sender sends large
354 * payload. Payload + SCM_RIGHTS + LOCAL_CREDS hit socket buffer
355 * limit, and receiver receives truncated data.
357 test = "test8-rights+creds+payload";
358 printf("beginning %s\n", test);
366 len = sizeof(sendspace);
367 if (sysctlbyname("net.local.stream.sendspace", &sendspace,
369 err(-1, "%s: sysctlbyname(net.local.stream.sendspace)",
372 if ((buf = malloc(sendspace)) == NULL)
373 err(-1, "%s: malloc", test);
375 domainsocketpair(test, fd);
376 if (setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on)) < 0)
377 err(-1, "%s: setsockopt(LOCAL_CREDS)", test);
378 tempfile(test, &putfd_1);
379 sendfd_payload(test, fd[0], putfd_1, buf, sendspace);
380 recvfd_payload(test, fd[1], &getfd_1, buf, sendspace);
386 printf("%s passed\n", test);