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/types.h>
30 #include <sys/socket.h>
32 #include <netinet/in.h>
45 * This test is intended to detect a leak of a file descriptor in the process
46 * following a failed non-blocking accept. It measures an available fd
47 * baseline, then performs 1000 failing accepts, then checks to see what the
48 * next fd is. It relies on sequential fd allocation, and will test for it
49 * briefly before beginning (not 100% reliable, but a good start).
52 main(int argc, char *argv[])
54 struct sockaddr_in sin;
56 int fd1, fd2, fd3, i, s;
61 * Check for sequential fd allocation, and give up early if not.
63 fd1 = dup(STDIN_FILENO);
64 fd2 = dup(STDIN_FILENO);
66 errx(-1, "Non-sequential fd allocation\n");
68 s = socket(PF_INET, SOCK_STREAM, 0);
70 errx(-1, "socket: %s", strerror(errno));
72 bzero(&sin, sizeof(sin));
73 sin.sin_len = sizeof(sin);
74 sin.sin_family = AF_INET;
75 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
76 sin.sin_port = htons(8080);
78 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) != 0)
79 errx(-1, "bind: %s", strerror(errno));
81 if (listen(s, -1) != 0)
82 errx(-1, "listen: %s", strerror(errno));
84 i = fcntl(s, F_GETFL);
86 errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
88 if (fcntl(s, F_SETFL, i) != 0)
89 errx(-1, "ioctl(F_SETFL): %s", strerror(errno));
90 i = fcntl(s, F_GETFL);
92 errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
93 if ((i & O_NONBLOCK) != O_NONBLOCK)
94 errx(-1, "Failed to set O_NONBLOCK (i=0x%x)\n", i);
96 for (i = 0; i < LOOPS; i++) {
98 if (accept(s, (struct sockaddr *)&sin, &size) != -1)
99 errx(-1, "accept succeeded\n");
101 errx(-1, "accept: %s", strerror(errno));
105 * Allocate a file descriptor and make sure it's fd2+2. 2 because
106 * we allocate an fd for the socket.
108 fd3 = dup(STDIN_FILENO);
110 printf("not ok 1 - (%d, %d, %d)\n", fd1, fd2, fd3);