2 * Copyright (c) 2010 Mikolaj Golub
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
30 * This regression test attempts to trigger a race that occurs when both
31 * endpoints of a connected UNIX domain socket are closed at once. The two
32 * close paths may run concurrently leading to a call to sodisconnect() on an
33 * already-closed socket in kernel. Before it was fixed, this might lead to
34 * ENOTCONN being returned improperly from close().
36 * This race is fairly timing-dependent, so it effectively requires SMP, and
37 * may not even trigger then.
40 #include <sys/types.h>
41 #include <sys/select.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
57 static char socket_path[] = "tmp.XXXXXXXX";
65 struct sockaddr_un servaddr;
66 int listenfd, connfd, pid;
71 if (sysctlbyname("kern.smp.cpus", &ncpus, &len, NULL, 0) < 0)
72 err(1, "kern.smp.cpus");
73 if (len != sizeof(ncpus))
74 errx(1, "kern.smp.cpus: invalid length");
76 warnx("SMP not present, test may be unable to trigger race");
78 if (mkstemp(socket_path) == -1)
79 err(1, "mkstemp failed");
83 * Create a UNIX domain socket that the child will repeatedly
84 * accept() from, and that the parent will repeatedly connect() to.
86 if ((listenfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0)
87 err(1, "parent: socket error");
88 (void)unlink(socket_path);
89 bzero(&servaddr, sizeof(servaddr));
90 servaddr.sun_family = AF_LOCAL;
91 strcpy(servaddr.sun_path, socket_path);
92 if (bind(listenfd, (struct sockaddr *) &servaddr,
93 sizeof(servaddr)) < 0)
94 err(1, "parent: bind error");
95 if (listen(listenfd, 1024) < 0)
96 err(1, "parent: listen error");
103 * In the parent, repeatedly connect and disconnect from the
104 * socket, attempting to induce the race.
108 bzero(&servaddr, sizeof(servaddr));
109 servaddr.sun_family = AF_LOCAL;
110 strcpy(servaddr.sun_path, socket_path);
111 for (counter = 0; counter < LOOPS; counter++) {
112 if ((connfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
113 (void)kill(pid, SIGTERM);
114 err(1, "parent: socket error");
116 if (connect(connfd, (struct sockaddr *)&servaddr,
117 sizeof(servaddr)) < 0) {
118 (void)kill(pid, SIGTERM);
119 err(1, "parent: connect error");
121 if (close(connfd) < 0) {
122 (void)kill(pid, SIGTERM);
123 err(1, "parent: close error");
127 (void)kill(pid, SIGTERM);
130 * In the child, loop accepting and closing. We may pick up
131 * the race here so report errors from close().
134 if ((connfd = accept(listenfd,
135 (struct sockaddr *)NULL, NULL)) < 0)
136 err(1, "child: accept error");
137 if (close(connfd) < 0)
138 err(1, "child: close error");