accept_fd_leak.c revision 136843
1254721Semaste/*-
2254721Semaste * Copyright (c) 2004 Robert N. M. Watson
3254721Semaste * All rights reserved.
4254721Semaste *
5254721Semaste * Redistribution and use in source and binary forms, with or without
6254721Semaste * modification, are permitted provided that the following conditions
7254721Semaste * are met:
8254721Semaste * 1. Redistributions of source code must retain the above copyright
9254721Semaste *    notice, this list of conditions and the following disclaimer.
10254721Semaste * 2. Redistributions in binary form must reproduce the above copyright
11254721Semaste *    notice, this list of conditions and the following disclaimer in the
12254721Semaste *    documentation and/or other materials provided with the distribution.
13254721Semaste *
14254721Semaste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15254721Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16254721Semaste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17254721Semaste * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18254721Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19254721Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20254721Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21254721Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22254721Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23254721Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24254721Semaste * SUCH DAMAGE.
25254721Semaste *
26296417Sdim * $FreeBSD: head/tools/regression/sockets/accept_fd_leak/accept_fd_leak.c 136843 2004-10-23 22:11:35Z rwatson $
27254721Semaste */
28296417Sdim
29296417Sdim#include <sys/types.h>
30254721Semaste#include <sys/socket.h>
31296417Sdim
32296417Sdim#include <netinet/in.h>
33254721Semaste
34296417Sdim#include <err.h>
35296417Sdim#include <errno.h>
36254721Semaste#include <fcntl.h>
37296417Sdim#include <stdio.h>
38296417Sdim#include <stdlib.h>
39254721Semaste#include <string.h>
40296417Sdim#include <unistd.h>
41296417Sdim
42254721Semaste#define	LOOPS	500
43296417Sdim
44296417Sdim/*
45296417Sdim * This test is intended to detect a leak of a file descriptor in the process
46254721Semaste * following a failed non-blocking accept.  It measures an available fd
47296417Sdim * baseline, then performs 1000 failing accepts, then checks to see what the
48296417Sdim * next fd is.  It relies on sequential fd allocation, and will test for it
49296417Sdim * briefly before beginning (not 100% reliable, but a good start).
50254721Semaste */
51296417Sdimint
52296417Sdimmain(int argc, char *argv[])
53254721Semaste{
54296417Sdim	struct sockaddr_in sin;
55296417Sdim	socklen_t size;
56254721Semaste	int fd1, fd2, fd3, i, s;
57296417Sdim
58296417Sdim	/*
59254721Semaste	 * Check for sequential fd allocation, and give up early if not.
60296417Sdim	 */
61296417Sdim	fd1 = dup(STDIN_FILENO);
62254721Semaste	fd2 = dup(STDIN_FILENO);
63254721Semaste	if (fd2 != fd1 + 1)
64254721Semaste		errx(-1, "Non-sequential fd allocation\n");
65254721Semaste
66254721Semaste	s = socket(PF_INET, SOCK_STREAM, 0);
67254721Semaste	if (s == -1)
68254721Semaste		errx(-1, "socket: %s", strerror(errno));
69254721Semaste
70254721Semaste	bzero(&sin, sizeof(sin));
71254721Semaste	sin.sin_len = sizeof(sin);
72254721Semaste	sin.sin_family = AF_INET;
73254721Semaste	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
74254721Semaste	sin.sin_port = htons(8080);
75254721Semaste
76254721Semaste	if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) != 0)
77254721Semaste		errx(-1, "bind: %s", strerror(errno));
78254721Semaste
79254721Semaste	if (listen(s, -1) != 0)
80254721Semaste		errx(-1, "listen: %s", strerror(errno));
81254721Semaste
82254721Semaste	i = fcntl(s, F_GETFL);
83254721Semaste	if (i == -1)
84254721Semaste		errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
85254721Semaste	i |= O_NONBLOCK;
86254721Semaste	if (fcntl(s, F_SETFL, i) != 0)
87254721Semaste		errx(-1, "ioctl(F_SETFL): %s", strerror(errno));
88254721Semaste	i = fcntl(s, F_GETFL);
89254721Semaste	if (i == -1)
90254721Semaste		errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
91254721Semaste	if ((i & O_NONBLOCK) != O_NONBLOCK)
92254721Semaste		errx(-1, "Failed to set O_NONBLOCK (i=0x%x)\n", i);
93254721Semaste
94254721Semaste	for (i = 0; i < LOOPS; i++) {
95254721Semaste		size = sizeof(sin);
96254721Semaste		if (accept(s, (struct sockaddr *)&sin, &size) != -1)
97254721Semaste			errx(-1, "accept succeeded\n");
98254721Semaste		if (errno != EAGAIN)
99254721Semaste			errx(-1, "accept: %s", strerror(errno));
100254721Semaste	}
101254721Semaste
102254721Semaste	/*
103254721Semaste	 * Allocate a file descriptor and make sure it's fd2+2.  2 because
104254721Semaste	 * we allocate an fd for the socket.
105254721Semaste	 */
106254721Semaste	fd3 = dup(STDIN_FILENO);
107254721Semaste	if (fd3 != fd2 + 2)
108254721Semaste		errx(-1, "FAIL (%d, %d, %d)\n", fd1, fd2, fd3);
109254721Semaste	else
110254721Semaste		fprintf(stderr, "PASS\n");
111254721Semaste
112254721Semaste	return (0);
113254721Semaste}
114254721Semaste