2 * Copyright (c) 2004 Robert N. M. Watson
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
29 #include <sys/param.h>
30 #include <sys/socket.h>
33 #include <netinet/in.h>
44 #define BIND_ATTEMPTS 10
46 #define NUM_ATTEMPTS 1000
48 static volatile int quit;
51 child_died(int sig __unused)
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).
67 struct sockaddr_in sin;
70 int fd1, fd2, fd3, i, listen_port, s, status;
75 * Check for sequential fd allocation, and give up early if not.
77 fd1 = dup(STDIN_FILENO);
78 fd2 = dup(STDIN_FILENO);
80 errx(-1, "Non-sequential fd allocation\n");
82 s = socket(PF_INET, SOCK_STREAM, 0);
84 errx(-1, "socket: %s", strerror(errno));
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);
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)
99 warn("bind with %d failed", listen_port);
102 if (i >= BIND_ATTEMPTS) {
103 printf("Bail out!\n");
107 if (listen(s, -1) != 0)
108 errx(-1, "listen: %s", strerror(errno));
110 i = fcntl(s, F_GETFL);
112 errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
114 if (fcntl(s, F_SETFL, i) != 0)
115 errx(-1, "ioctl(F_SETFL): %s", strerror(errno));
116 i = fcntl(s, F_GETFL);
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);
122 for (i = 0; i < LOOPS; i++) {
124 if (accept(s, (struct sockaddr *)&sin, &size) != -1)
125 errx(-1, "accept succeeded\n");
127 errx(-1, "accept: %s", strerror(errno));
131 * Allocate a file descriptor and make sure it's fd2+2. 2 because
132 * we allocate an fd for the socket.
134 fd3 = dup(STDIN_FILENO);
136 printf("not ok 1 - (%d, %d, %d)\n", fd1, fd2, fd3);
141 * Try failing accept's w/o non-blocking where the destination
142 * address pointer is invalid.
145 signal(SIGCHLD, child_died);
148 errx(-1, "fork: %s", strerror(errno));
151 * Child process does `NUM_ATTEMPTS` connects.
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);
164 for (i = 0; i < NUM_ATTEMPTS; i++) {
165 s = socket(PF_INET, SOCK_STREAM, 0);
167 errx(-1, "socket: %s", strerror(errno));
168 if (connect(s, (struct sockaddr *)&sin,
170 errx(-1, "connect: %s", strerror(errno));
176 /* Reset back to a blocking socket. */
177 i = fcntl(s, F_GETFL);
179 errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
181 if (fcntl(s, F_SETFL, i) != 0)
182 errx(-1, "ioctl(F_SETFL): %s", strerror(errno));
183 i = fcntl(s, F_GETFL);
185 errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
187 errx(-1, "Failed to clear O_NONBLOCK (i=0x%x)\n", i);
189 /* Do `NUM_ATTEMPTS` accepts with an invalid pointer. */
190 for (i = 0; !quit && i < NUM_ATTEMPTS; i++) {
192 if (accept(s, (struct sockaddr *)(uintptr_t)(0x100),
194 errx(-1, "accept succeeded\n");
196 errx(-1, "accept: %s", strerror(errno));
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");
205 * Allocate a file descriptor and make sure it's fd2+2. 2 because
206 * we allocate an fd for the socket.
208 fd3 = dup(STDIN_FILENO);
210 printf("not ok 2 - (%d, %d, %d)\n", fd1, fd2, fd3);