]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - tools/regression/sockets/unix_close_race/unix_close_race.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / tools / regression / sockets / unix_close_race / unix_close_race.c
1 /*-
2  * Copyright (c) 2010 Mikolaj Golub
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 /*
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().
35  *
36  * This race is fairly timing-dependent, so it effectively requires SMP, and
37  * may not even trigger then.
38  */
39
40 #include <sys/types.h>
41 #include <sys/select.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
44 #include <sys/un.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <signal.h>
50 #include <stdlib.h>
51 #include <stdio.h>
52 #include <strings.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <err.h>
56
57 static char socket_path[] = "tmp.XXXXXXXX";
58
59 #define USLEEP  100
60 #define LOOPS   100000
61
62 int
63 main(void)
64 {
65         struct sockaddr_un servaddr;
66         int listenfd, connfd, pid;
67         u_int counter, ncpus;
68         size_t len;
69
70         len = sizeof(ncpus);
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");
75         if (ncpus < 2)
76                 warnx("SMP not present, test may be unable to trigger race");
77
78         if (mkstemp(socket_path) == -1)
79                 err(1, "mkstemp failed");
80         unlink(socket_path);
81
82         /*
83          * Create a UNIX domain socket that the child will repeatedly
84          * accept() from, and that the parent will repeatedly connect() to.
85          */
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");
97
98         pid = fork();
99         if (pid == -1)
100                 err(1, "fork()");
101         if (pid != 0) {
102                 /*
103                  * In the parent, repeatedly connect and disconnect from the
104                  * socket, attempting to induce the race.
105                  */
106                 close(listenfd);
107                 sleep(1);
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");
115                         }
116                         if (connect(connfd, (struct sockaddr *)&servaddr,
117                             sizeof(servaddr)) < 0) {
118                                 (void)kill(pid, SIGTERM);
119                                 err(1, "parent: connect error");
120                         }
121                         if (close(connfd) < 0) {
122                                 (void)kill(pid, SIGTERM);
123                                 err(1, "parent: close error");
124                         }
125                         usleep(USLEEP);
126                 }
127                 (void)kill(pid, SIGTERM);
128         } else {
129                 /*
130                  * In the child, loop accepting and closing.  We may pick up
131                  * the race here so report errors from close().
132                  */
133                 for ( ; ; ) {
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");
139                 }
140         }
141         printf("OK\n");
142         exit(0);
143 }