xref: /freebsd/tools/regression/sockets/unix_close_race/unix_close_race.c (revision 5ae59dec60e3815b621ae87f74a377cf3449ca55)
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 #define	UNIXSTR_PATH	"/tmp/mytest.socket"
58 #define	USLEEP	100
59 #define	LOOPS	100000
60 
61 int
62 main(int argc, char **argv)
63 {
64 	struct sockaddr_un servaddr;
65 	int listenfd, connfd, pid;
66 	u_int counter, ncpus;
67 	size_t len;
68 
69 	len = sizeof(ncpus);
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");
74 	if (ncpus < 2)
75 		warnx("SMP not present, test may be unable to trigger race");
76 
77 	/*
78 	 * Create a UNIX domain socket that the child will repeatedly
79 	 * accept() from, and that the parent will repeatedly connect() to.
80 	 */
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");
92 
93 	pid = fork();
94 	if (pid == -1)
95 		err(1, "fork()");
96 	if (pid != 0) {
97 		/*
98 		 * In the parent, repeatedly connect and disconnect from the
99 		 * socket, attempting to induce the race.
100 		 */
101 		close(listenfd);
102 		sleep(1);
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");
110 			}
111 			if (connect(connfd, (struct sockaddr *)&servaddr,
112 			    sizeof(servaddr)) < 0) {
113 			    	(void)kill(pid, SIGTERM);
114 				err(1, "parent: connect error");
115 			}
116 			if (close(connfd) < 0) {
117 				(void)kill(pid, SIGTERM);
118 				err(1, "parent: close error");
119 			}
120 			usleep(USLEEP);
121 		}
122 		(void)kill(pid, SIGTERM);
123 	} else {
124 		/*
125 		 * In the child, loop accepting and closing.  We may pick up
126 		 * the race here so report errors from close().
127 		 */
128 		for ( ; ; ) {
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");
134 		}
135 	}
136 	printf("OK\n");
137 	exit(0);
138 }
139