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 #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	LOOPS	500
45 
46 volatile int quit;
47 
48 static void
49 child_died(int sig)
50 {
51 	quit = 1;
52 }
53 
54 /*
55  * This test is intended to detect a leak of a file descriptor in the process
56  * following a failed non-blocking accept.  It measures an available fd
57  * baseline, then performs 1000 failing accepts, then checks to see what the
58  * next fd is.  It relies on sequential fd allocation, and will test for it
59  * briefly before beginning (not 100% reliable, but a good start).
60  */
61 int
62 main(int argc, char *argv[])
63 {
64 	struct sockaddr_in sin;
65 	socklen_t size;
66 	pid_t child;
67 	int fd1, fd2, fd3, i, s;
68 	int status;
69 
70 	printf("1..2\n");
71 
72 	/*
73 	 * Check for sequential fd allocation, and give up early if not.
74 	 */
75 	fd1 = dup(STDIN_FILENO);
76 	fd2 = dup(STDIN_FILENO);
77 	if (fd2 != fd1 + 1)
78 		errx(-1, "Non-sequential fd allocation\n");
79 
80 	s = socket(PF_INET, SOCK_STREAM, 0);
81 	if (s == -1)
82 		errx(-1, "socket: %s", strerror(errno));
83 
84 	bzero(&sin, sizeof(sin));
85 	sin.sin_len = sizeof(sin);
86 	sin.sin_family = AF_INET;
87 	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
88 	sin.sin_port = htons(8080);
89 
90 	if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) != 0)
91 		errx(-1, "bind: %s", strerror(errno));
92 
93 	if (listen(s, -1) != 0)
94 		errx(-1, "listen: %s", strerror(errno));
95 
96 	i = fcntl(s, F_GETFL);
97 	if (i == -1)
98 		errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
99 	i |= O_NONBLOCK;
100 	if (fcntl(s, F_SETFL, i) != 0)
101 		errx(-1, "ioctl(F_SETFL): %s", strerror(errno));
102 	i = fcntl(s, F_GETFL);
103 	if (i == -1)
104 		errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
105 	if ((i & O_NONBLOCK) != O_NONBLOCK)
106 		errx(-1, "Failed to set O_NONBLOCK (i=0x%x)\n", i);
107 
108 	for (i = 0; i < LOOPS; i++) {
109 		size = sizeof(sin);
110 		if (accept(s, (struct sockaddr *)&sin, &size) != -1)
111 			errx(-1, "accept succeeded\n");
112 		if (errno != EAGAIN)
113 			errx(-1, "accept: %s", strerror(errno));
114 	}
115 
116 	/*
117 	 * Allocate a file descriptor and make sure it's fd2+2.  2 because
118 	 * we allocate an fd for the socket.
119 	 */
120 	fd3 = dup(STDIN_FILENO);
121 	if (fd3 != fd2 + 2)
122 		printf("not ok 1 - (%d, %d, %d)\n", fd1, fd2, fd3);
123 	else
124 		printf("ok 1\n");
125 
126 	/*
127 	 * Try failing accept's w/o non-blocking where the destination
128 	 * address pointer is invalid.
129 	 */
130 	close(fd3);
131 	signal(SIGCHLD, child_died);
132 	child = fork();
133 	if (child < 0)
134 		errx(-1, "fork: %s", strerror(errno));
135 
136 	/*
137 	 * Child process does 1000 connect's.
138 	 */
139 	if (child == 0) {
140 		bzero(&sin, sizeof(sin));
141 		sin.sin_len = sizeof(sin);
142 		sin.sin_family = AF_INET;
143 		sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
144 		sin.sin_port = htons(8080);
145 
146 		for (i = 0; i < 1000; i++) {
147 			s = socket(PF_INET, SOCK_STREAM, 0);
148 			if (s == -1)
149 				errx(-1, "socket: %s", strerror(errno));
150 			if (connect(s, (struct sockaddr *)&sin,
151 			    sizeof(sin)) < 0)
152 				errx(-1, "connect: %s", strerror(errno));
153 			close(s);
154 		}
155 		exit(0);
156 	}
157 
158 	/* Reset back to a blocking socket. */
159 	i = fcntl(s, F_GETFL);
160 	if (i == -1)
161 		errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
162 	i &= ~O_NONBLOCK;
163 	if (fcntl(s, F_SETFL, i) != 0)
164 		errx(-1, "ioctl(F_SETFL): %s", strerror(errno));
165 	i = fcntl(s, F_GETFL);
166 	if (i == -1)
167 		errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
168 	if (i & O_NONBLOCK)
169 		errx(-1, "Failed to clear O_NONBLOCK (i=0x%x)\n", i);
170 
171 	/* Do 1000 accept's with an invalid pointer. */
172 	for (i = 0; !quit && i < 1000; i++) {
173 		size = sizeof(sin);
174 		if (accept(s, (struct sockaddr *)(uintptr_t)(0x100),
175 		    &size) != -1)
176 			errx(-1, "accept succeeded\n");
177 		if (errno != EFAULT)
178 			errx(-1, "accept: %s", strerror(errno));
179 	}
180 
181 	if (waitpid(child, &status, 0) < 0)
182 		errx(-1, "waitpid: %s", strerror(errno));
183 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
184 		warnx("child process died");
185 
186 	/*
187 	 * Allocate a file descriptor and make sure it's fd2+2.  2 because
188 	 * we allocate an fd for the socket.
189 	 */
190 	fd3 = dup(STDIN_FILENO);
191 	if (fd3 != fd2 + 2)
192 		printf("not ok 2 - (%d, %d, %d)\n", fd1, fd2, fd3);
193 	else
194 		printf("ok 2\n");
195 
196 	return (0);
197 }
198