xref: /freebsd/tools/regression/sockets/accept_fd_leak/accept_fd_leak.c (revision 7cc9e6ddbb1a6cd38b2f44d4dd9dfc890d2fc9d0)
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/types.h>
30 #include <sys/socket.h>
31 
32 #include <netinet/in.h>
33 
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #define	LOOPS	500
42 
43 /*
44  * This test is intended to detect a leak of a file descriptor in the process
45  * following a failed non-blocking accept.  It measures an available fd
46  * baseline, then performs 1000 failing accepts, then checks to see what the
47  * next fd is.  It relies on sequential fd allocation, and will test for it
48  * briefly before beginning (not 100% reliable, but a good start).
49  */
50 int
51 main(int argc, char *argv[])
52 {
53 	struct sockaddr_in sin;
54 	socklen_t size;
55 	int fd1, fd2, fd3, i, s;
56 
57 	/*
58 	 * Check for sequential fd allocation, and give up early if not.
59 	 */
60 	fd1 = dup(STDIN_FILENO);
61 	fd2 = dup(STDIN_FILENO);
62 	if (fd2 != fd1 + 1) {
63 		fprintf(stderr, "Non-sequential fd allocation!\n");
64 		exit(-1);
65 	}
66 
67 	s = socket(PF_INET, SOCK_STREAM, 0);
68 	if (s == -1) {
69 		perror("socket");
70 		exit(-1);
71 	}
72 
73 	bzero(&sin, sizeof(sin));
74 	sin.sin_len = sizeof(sin);
75 	sin.sin_family = AF_INET;
76 	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
77 	sin.sin_port = htons(8080);
78 
79 	if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) != 0) {
80 		perror("bind");
81 		exit(-1);
82 	}
83 
84 	if (listen(s, -1) != 0) {
85 		perror("listen");
86 		exit(-1);
87 	}
88 
89 	i = fcntl(s, F_GETFL);
90 	if (i == -1) {
91 		perror("F_GETFL");
92 		exit(-1);
93 	}
94 	i |= O_NONBLOCK;
95 	if (fcntl(s, F_SETFL, i) != 0) {
96 		perror("F_SETFL");
97 		exit(-1);
98 	}
99 	i = fcntl(s, F_GETFL);
100 	if (i == -1) {
101 		perror("F_GETFL");
102 		exit(-1);
103 	}
104 	if ((i & O_NONBLOCK) != O_NONBLOCK) {
105 		fprintf(stderr, "Failed to set O_NONBLOCK (i=%d)\n", i);
106 		exit(-1);
107 	}
108 
109 	for (i = 0; i < LOOPS; i++) {
110 		if (accept(s, (struct sockaddr *)&sin, &size) != -1) {
111 			fprintf(stderr, "accept succeeded!\n");
112 			exit(-1);
113 		}
114 		if (errno != EAGAIN) {
115 			perror("accept");
116 			exit(-1);
117 		}
118 	}
119 
120 	/*
121 	 * Allocate a file descriptor and make sure it's fd2+2.  2 because
122 	 * we allocate an fd for the socket.
123 	 */
124 	fd3 = dup(STDIN_FILENO);
125 	if (fd3 != fd2 + 2) {
126 		fprintf(stderr, "TEST FAILED (%d, %d, %d)\n", fd1, fd2, fd3);
127 		exit(-1);
128 	}
129 
130 	return (0);
131 }
132