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 #define UNIXSTR_PATH "/tmp/mytest.socket"
62 main(int argc, char **argv)
64 struct sockaddr_un servaddr;
65 int listenfd, connfd, pid;
70 if (sysctlbyname("kern.smp.cpus", &ncpus, &len, NULL, 0) < 0)
71 err(1, "kern.smp.cpus");
72 if (len != sizeof(ncpus))
73 errx(1, "kern.smp.cpus: invalid length");
75 warnx("SMP not present, test may be unable to trigger race");
78 * Create a UNIX domain socket that the child will repeatedly
79 * accept() from, and that the parent will repeatedly connect() to.
81 if ((listenfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0)
82 err(1, "parent: socket error");
83 (void)unlink(UNIXSTR_PATH);
84 bzero(&servaddr, sizeof(servaddr));
85 servaddr.sun_family = AF_LOCAL;
86 strcpy(servaddr.sun_path, UNIXSTR_PATH);
87 if (bind(listenfd, (struct sockaddr *) &servaddr,
88 sizeof(servaddr)) < 0)
89 err(1, "parent: bind error");
90 if (listen(listenfd, 1024) < 0)
91 err(1, "parent: listen error");
98 * In the parent, repeatedly connect and disconnect from the
99 * socket, attempting to induce the race.
103 bzero(&servaddr, sizeof(servaddr));
104 servaddr.sun_family = AF_LOCAL;
105 strcpy(servaddr.sun_path, UNIXSTR_PATH);
106 for (counter = 0; counter < LOOPS; counter++) {
107 if ((connfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
108 (void)kill(pid, SIGTERM);
109 err(1, "parent: socket error");
111 if (connect(connfd, (struct sockaddr *)&servaddr,
112 sizeof(servaddr)) < 0) {
113 (void)kill(pid, SIGTERM);
114 err(1, "parent: connect error");
116 if (close(connfd) < 0) {
117 (void)kill(pid, SIGTERM);
118 err(1, "parent: close error");
122 (void)kill(pid, SIGTERM);
125 * In the child, loop accepting and closing. We may pick up
126 * the race here so report errors from close().
129 if ((connfd = accept(listenfd,
130 (struct sockaddr *)NULL, NULL)) < 0)
131 err(1, "child: accept error");
132 if (close(connfd) < 0)
133 err(1, "child: close error");