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>
41 * UNIX domain sockets allow file descriptors to be passed via "ancillary
42 * data", or control messages. This regression test is intended to exercise
43 * this facility, both performing some basic tests that it operates, and also
44 * causing some kernel edge cases to execute, such as garbage collection when
45 * there are cyclic file descriptor references. Right now we test only with
46 * stream sockets, but ideally we'd also test with datagram sockets.
50 domainsocketpair(const char *test, int *fdp)
53 if (socketpair(PF_UNIX, SOCK_STREAM, 0, fdp) < 0)
54 err(-1, "%s: socketpair(PF_UNIX, SOCK_STREAM)", test);
58 closesocketpair(int *fdp)
66 devnull(const char *test, int *fdp)
70 fd = open("/dev/null", O_RDONLY);
72 err(-1, "%s: open(/dev/null)", test);
77 tempfile(const char *test, int *fdp)
82 snprintf(path, PATH_MAX, "/tmp/unix_passfd.XXXXXXXXXXXXXXX");
85 err(-1, "%s: mkstemp(%s)", test, path);
91 dofstat(const char *test, int fd, struct stat *sb)
94 if (fstat(fd, sb) < 0)
95 err(-1, "%s: fstat", test);
99 samefile(const char *test, struct stat *sb1, struct stat *sb2)
102 if (sb1->st_dev != sb2->st_dev)
103 errx(-1, "%s: samefile: different device", test);
104 if (sb1->st_ino != sb2->st_ino)
105 errx(-1, "%s: samefile: different inode", test);
109 sendfd(const char *test, int sockfd, int sendfd)
114 char message[CMSG_SPACE(sizeof(int))];
115 struct cmsghdr *cmsghdr;
116 struct msghdr msghdr;
119 bzero(&msghdr, sizeof(msghdr));
120 bzero(&message, sizeof(message));
123 msghdr.msg_control = message;
124 msghdr.msg_controllen = sizeof(message);
126 iovec.iov_base = &ch;
127 iovec.iov_len = sizeof(ch);
129 msghdr.msg_iov = &iovec;
130 msghdr.msg_iovlen = 1;
132 cmsghdr = (struct cmsghdr *)message;
133 cmsghdr->cmsg_len = CMSG_LEN(sizeof(int));
134 cmsghdr->cmsg_level = SOL_SOCKET;
135 cmsghdr->cmsg_type = SCM_RIGHTS;
136 *(int *)CMSG_DATA(cmsghdr) = sendfd;
138 len = sendmsg(sockfd, &msghdr, 0);
140 err(-1, "%s: sendmsg", test);
141 if (len != sizeof(ch))
142 errx(-1, "%s: sendmsg: %zd bytes sent", test, len);
146 recvfd(const char *test, int sockfd, int *recvfd)
148 struct cmsghdr *cmsghdr;
149 char message[CMSG_SPACE(sizeof(int))];
150 struct msghdr msghdr;
155 bzero(&msghdr, sizeof(msghdr));
158 msghdr.msg_control = message;
159 msghdr.msg_controllen = sizeof(message);
161 iovec.iov_base = &ch;
162 iovec.iov_len = sizeof(ch);
164 msghdr.msg_iov = &iovec;
165 msghdr.msg_iovlen = 1;
167 iovec.iov_len = sizeof(ch);
169 msghdr.msg_iov = &iovec;
170 msghdr.msg_iovlen = 1;
172 len = recvmsg(sockfd, &msghdr, 0);
174 err(-1, "%s: recvmsg", test);
175 if (len != sizeof(ch))
176 errx(-1, "%s: recvmsg: %zd bytes received", test, len);
177 cmsghdr = CMSG_FIRSTHDR(&msghdr);
179 errx(-1, "%s: recvmsg: did not receive control message", test);
180 if (cmsghdr->cmsg_len != CMSG_LEN(sizeof(int)) ||
181 cmsghdr->cmsg_level != SOL_SOCKET ||
182 cmsghdr->cmsg_type != SCM_RIGHTS)
183 errx(-1, "%s: recvmsg: did not receive single-fd message",
185 *recvfd = *(int *)CMSG_DATA(cmsghdr);
187 errx(-1, "%s: recvmsg: received fd -1", test);
191 main(int argc, char *argv[])
193 struct stat putfd_1_stat, putfd_2_stat, getfd_1_stat, getfd_2_stat;
194 int fd[2], putfd_1, putfd_2, getfd_1, getfd_2;
198 * First test: put a temporary file into a UNIX domain socket, then
199 * take it out and make sure it's the same file. First time around,
200 * don't close the reference after sending.
202 test = "test1-simplesendfd";
203 printf("beginning %s\n", test);
205 domainsocketpair(test, fd);
206 tempfile(test, &putfd_1);
207 dofstat(test, putfd_1, &putfd_1_stat);
208 sendfd(test, fd[0], putfd_1);
209 recvfd(test, fd[1], &getfd_1);
210 dofstat(test, getfd_1, &getfd_1_stat);
211 samefile(test, &putfd_1_stat, &getfd_1_stat);
216 printf("%s passed\n", test);
219 * Second test: same as first, only close the file reference after
220 * sending, so that the only reference is the descriptor in the UNIX
221 * domain socket buffer.
223 test = "test2-sendandclose";
224 printf("beginning %s\n", test);
226 domainsocketpair(test, fd);
227 tempfile(test, &putfd_1);
228 dofstat(test, putfd_1, &putfd_1_stat);
229 sendfd(test, fd[0], putfd_1);
231 recvfd(test, fd[1], &getfd_1);
232 dofstat(test, getfd_1, &getfd_1_stat);
233 samefile(test, &putfd_1_stat, &getfd_1_stat);
237 printf("%s passed\n", test);
240 * Third test: put a temporary file into a UNIX domain socket, then
241 * close both endpoints causing garbage collection to kick off.
243 test = "test3-sendandcancel";
244 printf("beginning %s\n", test);
246 domainsocketpair(test, fd);
247 tempfile(test, &putfd_1);
248 sendfd(test, fd[0], putfd_1);
252 printf("%s passed\n", test);
255 * Send two files. Then receive them. Make sure they are returned
256 * in the right order, and both get there.
259 test = "test4-twofile";
260 printf("beginning %s\n", test);
262 domainsocketpair(test, fd);
263 tempfile(test, &putfd_1);
264 tempfile(test, &putfd_2);
265 dofstat(test, putfd_1, &putfd_1_stat);
266 dofstat(test, putfd_2, &putfd_2_stat);
267 sendfd(test, fd[0], putfd_1);
268 sendfd(test, fd[0], putfd_2);
271 recvfd(test, fd[1], &getfd_1);
272 recvfd(test, fd[1], &getfd_2);
273 dofstat(test, getfd_1, &getfd_1_stat);
274 dofstat(test, getfd_2, &getfd_2_stat);
275 samefile(test, &putfd_1_stat, &getfd_1_stat);
276 samefile(test, &putfd_2_stat, &getfd_2_stat);
281 printf("%s passed\n", test);
284 * Big bundling test. Send an endpoint of the UNIX domain socket
285 * over itself, closing the door behind it.
288 test = "test5-bundle";
289 printf("beginning %s\n", test);
291 domainsocketpair(test, fd);
293 sendfd(test, fd[0], fd[0]);
295 recvfd(test, fd[1], &getfd_1);
299 printf("%s passed\n", test);
302 * Big bundling test part two: Send an endpoint of the UNIX domain
303 * socket over itself, close the door behind it, and never remove it
304 * from the other end.
307 test = "test6-bundlecancel";
308 printf("beginning %s\n", test);
310 domainsocketpair(test, fd);
311 sendfd(test, fd[0], fd[0]);
312 sendfd(test, fd[1], fd[0]);
315 printf("%s passed\n", test);
318 * Test for PR 151758: Send an character device over the UNIX
319 * domain socket and then close both sockets to orphan the
323 test = "test7-devfsorphan";
324 printf("beginning %s\n", test);
326 domainsocketpair(test, fd);
327 devnull(test, &putfd_1);
328 sendfd(test, fd[0], putfd_1);
332 printf("%s passed\n", test);