xref: /freebsd/tools/regression/sockets/accept_fd_leak/accept_fd_leak.c (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 /*-
2  * Copyright (c) 2004 Robert N. M. Watson
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 #include <sys/param.h>
30 #include <sys/socket.h>
31 #include <sys/wait.h>
32 
33 #include <netinet/in.h>
34 
35 #include <err.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #define	BIND_ATTEMPTS	10
45 #define	LOOPS	500
46 #define	NUM_ATTEMPTS	1000
47 
48 static volatile int quit;
49 
50 static void
51 child_died(int sig __unused)
52 {
53 
54 	quit = 1;
55 }
56 
57 /*
58  * This test is intended to detect a leak of a file descriptor in the process
59  * following a failed non-blocking accept.  It measures an available fd
60  * baseline, then performs 1000 failing accepts, then checks to see what the
61  * next fd is.  It relies on sequential fd allocation, and will test for it
62  * briefly before beginning (not 100% reliable, but a good start).
63  */
64 int
65 main(void)
66 {
67 	struct sockaddr_in sin;
68 	socklen_t size;
69 	pid_t child;
70 	int fd1, fd2, fd3, i, listen_port, s, status;
71 
72 	printf("1..2\n");
73 
74 	/*
75 	 * Check for sequential fd allocation, and give up early if not.
76 	 */
77 	fd1 = dup(STDIN_FILENO);
78 	fd2 = dup(STDIN_FILENO);
79 	if (fd2 != fd1 + 1)
80 		errx(-1, "Non-sequential fd allocation\n");
81 
82 	s = socket(PF_INET, SOCK_STREAM, 0);
83 	if (s == -1)
84 		errx(-1, "socket: %s", strerror(errno));
85 
86 	bzero(&sin, sizeof(sin));
87 	sin.sin_len = sizeof(sin);
88 	sin.sin_family = AF_INET;
89 	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
90 
91 	srandomdev();
92 
93 	for (i = 0; i < BIND_ATTEMPTS; i++) {
94 		/* Pick a random unprivileged port 1025-65535 */
95 		listen_port = MAX((int)random() % 65535, 1025);
96 		sin.sin_port = htons(listen_port);
97 		if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0)
98 			break;
99 		warn("bind with %d failed", listen_port);
100 		usleep(1000);
101 	}
102 	if (i >= BIND_ATTEMPTS) {
103 		printf("Bail out!\n");
104 		exit(1);
105 	}
106 
107 	if (listen(s, -1) != 0)
108 		errx(-1, "listen: %s", strerror(errno));
109 
110 	i = fcntl(s, F_GETFL);
111 	if (i == -1)
112 		errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
113 	i |= O_NONBLOCK;
114 	if (fcntl(s, F_SETFL, i) != 0)
115 		errx(-1, "ioctl(F_SETFL): %s", strerror(errno));
116 	i = fcntl(s, F_GETFL);
117 	if (i == -1)
118 		errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
119 	if ((i & O_NONBLOCK) != O_NONBLOCK)
120 		errx(-1, "Failed to set O_NONBLOCK (i=0x%x)\n", i);
121 
122 	for (i = 0; i < LOOPS; i++) {
123 		size = sizeof(sin);
124 		if (accept(s, (struct sockaddr *)&sin, &size) != -1)
125 			errx(-1, "accept succeeded\n");
126 		if (errno != EAGAIN)
127 			errx(-1, "accept: %s", strerror(errno));
128 	}
129 
130 	/*
131 	 * Allocate a file descriptor and make sure it's fd2+2.  2 because
132 	 * we allocate an fd for the socket.
133 	 */
134 	fd3 = dup(STDIN_FILENO);
135 	if (fd3 != fd2 + 2)
136 		printf("not ok 1 - (%d, %d, %d)\n", fd1, fd2, fd3);
137 	else
138 		printf("ok 1\n");
139 
140 	/*
141 	 * Try failing accept's w/o non-blocking where the destination
142 	 * address pointer is invalid.
143 	 */
144 	close(fd3);
145 	signal(SIGCHLD, child_died);
146 	child = fork();
147 	if (child < 0)
148 		errx(-1, "fork: %s", strerror(errno));
149 
150 	/*
151 	 * Child process does `NUM_ATTEMPTS` connects.
152 	 */
153 	if (child == 0) {
154 		close(fd1);
155 		close(fd2);
156 		close(s);
157 
158 		bzero(&sin, sizeof(sin));
159 		sin.sin_len = sizeof(sin);
160 		sin.sin_family = AF_INET;
161 		sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
162 		sin.sin_port = htons(listen_port);
163 
164 		for (i = 0; i < NUM_ATTEMPTS; i++) {
165 			s = socket(PF_INET, SOCK_STREAM, 0);
166 			if (s == -1)
167 				errx(-1, "socket: %s", strerror(errno));
168 			if (connect(s, (struct sockaddr *)&sin,
169 			    sizeof(sin)) < 0)
170 				errx(-1, "connect: %s", strerror(errno));
171 			close(s);
172 		}
173 		_exit(0);
174 	}
175 
176 	/* Reset back to a blocking socket. */
177 	i = fcntl(s, F_GETFL);
178 	if (i == -1)
179 		errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
180 	i &= ~O_NONBLOCK;
181 	if (fcntl(s, F_SETFL, i) != 0)
182 		errx(-1, "ioctl(F_SETFL): %s", strerror(errno));
183 	i = fcntl(s, F_GETFL);
184 	if (i == -1)
185 		errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
186 	if (i & O_NONBLOCK)
187 		errx(-1, "Failed to clear O_NONBLOCK (i=0x%x)\n", i);
188 
189 	/* Do `NUM_ATTEMPTS` accepts with an invalid pointer. */
190 	for (i = 0; !quit && i < NUM_ATTEMPTS; i++) {
191 		size = sizeof(sin);
192 		if (accept(s, (struct sockaddr *)(uintptr_t)(0x100),
193 		    &size) != -1)
194 			errx(-1, "accept succeeded\n");
195 		if (errno != EFAULT)
196 			errx(-1, "accept: %s", strerror(errno));
197 	}
198 
199 	if (waitpid(child, &status, 0) < 0)
200 		errx(-1, "waitpid: %s", strerror(errno));
201 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
202 		warnx("child process died");
203 
204 	/*
205 	 * Allocate a file descriptor and make sure it's fd2+2.  2 because
206 	 * we allocate an fd for the socket.
207 	 */
208 	fd3 = dup(STDIN_FILENO);
209 	if (fd3 != fd2 + 2)
210 		printf("not ok 2 - (%d, %d, %d)\n", fd1, fd2, fd3);
211 	else
212 		printf("ok 2\n");
213 
214 	return (0);
215 }
216