accept_fd_leak.c revision 137587
1132295Srwatson/*- 2132295Srwatson * Copyright (c) 2004 Robert N. M. Watson 3132295Srwatson * All rights reserved. 4132295Srwatson * 5132295Srwatson * Redistribution and use in source and binary forms, with or without 6132295Srwatson * modification, are permitted provided that the following conditions 7132295Srwatson * are met: 8132295Srwatson * 1. Redistributions of source code must retain the above copyright 9132295Srwatson * notice, this list of conditions and the following disclaimer. 10132295Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11132295Srwatson * notice, this list of conditions and the following disclaimer in the 12132295Srwatson * documentation and/or other materials provided with the distribution. 13132295Srwatson * 14132295Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15132295Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16132295Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17132295Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18132295Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19132295Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20132295Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21132295Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22132295Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23132295Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24132295Srwatson * SUCH DAMAGE. 25132295Srwatson * 26132295Srwatson * $FreeBSD: head/tools/regression/sockets/accept_fd_leak/accept_fd_leak.c 137587 2004-11-11 19:47:55Z nik $ 27132295Srwatson */ 28132295Srwatson 29132295Srwatson#include <sys/types.h> 30132295Srwatson#include <sys/socket.h> 31132295Srwatson 32132295Srwatson#include <netinet/in.h> 33132295Srwatson 34136843Srwatson#include <err.h> 35132295Srwatson#include <errno.h> 36132295Srwatson#include <fcntl.h> 37132295Srwatson#include <stdio.h> 38132295Srwatson#include <stdlib.h> 39132295Srwatson#include <string.h> 40132295Srwatson#include <unistd.h> 41132295Srwatson 42132295Srwatson#define LOOPS 500 43132295Srwatson 44132295Srwatson/* 45132295Srwatson * This test is intended to detect a leak of a file descriptor in the process 46132295Srwatson * following a failed non-blocking accept. It measures an available fd 47132295Srwatson * baseline, then performs 1000 failing accepts, then checks to see what the 48132295Srwatson * next fd is. It relies on sequential fd allocation, and will test for it 49132295Srwatson * briefly before beginning (not 100% reliable, but a good start). 50132295Srwatson */ 51132295Srwatsonint 52132295Srwatsonmain(int argc, char *argv[]) 53132295Srwatson{ 54132295Srwatson struct sockaddr_in sin; 55132295Srwatson socklen_t size; 56132295Srwatson int fd1, fd2, fd3, i, s; 57132295Srwatson 58137587Snik printf("1..1\n"); 59137587Snik 60132295Srwatson /* 61132295Srwatson * Check for sequential fd allocation, and give up early if not. 62132295Srwatson */ 63132295Srwatson fd1 = dup(STDIN_FILENO); 64132295Srwatson fd2 = dup(STDIN_FILENO); 65136843Srwatson if (fd2 != fd1 + 1) 66136843Srwatson errx(-1, "Non-sequential fd allocation\n"); 67132295Srwatson 68132295Srwatson s = socket(PF_INET, SOCK_STREAM, 0); 69136843Srwatson if (s == -1) 70136843Srwatson errx(-1, "socket: %s", strerror(errno)); 71132295Srwatson 72132295Srwatson bzero(&sin, sizeof(sin)); 73132295Srwatson sin.sin_len = sizeof(sin); 74132295Srwatson sin.sin_family = AF_INET; 75132295Srwatson sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 76132295Srwatson sin.sin_port = htons(8080); 77132295Srwatson 78136843Srwatson if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) != 0) 79136843Srwatson errx(-1, "bind: %s", strerror(errno)); 80132295Srwatson 81136843Srwatson if (listen(s, -1) != 0) 82136843Srwatson errx(-1, "listen: %s", strerror(errno)); 83132295Srwatson 84132295Srwatson i = fcntl(s, F_GETFL); 85136843Srwatson if (i == -1) 86136843Srwatson errx(-1, "ioctl(F_GETFL): %s", strerror(errno)); 87132295Srwatson i |= O_NONBLOCK; 88136843Srwatson if (fcntl(s, F_SETFL, i) != 0) 89136843Srwatson errx(-1, "ioctl(F_SETFL): %s", strerror(errno)); 90132295Srwatson i = fcntl(s, F_GETFL); 91136843Srwatson if (i == -1) 92136843Srwatson errx(-1, "ioctl(F_GETFL): %s", strerror(errno)); 93136843Srwatson if ((i & O_NONBLOCK) != O_NONBLOCK) 94136843Srwatson errx(-1, "Failed to set O_NONBLOCK (i=0x%x)\n", i); 95132295Srwatson 96132295Srwatson for (i = 0; i < LOOPS; i++) { 97134238Srwatson size = sizeof(sin); 98136843Srwatson if (accept(s, (struct sockaddr *)&sin, &size) != -1) 99136843Srwatson errx(-1, "accept succeeded\n"); 100136843Srwatson if (errno != EAGAIN) 101136843Srwatson errx(-1, "accept: %s", strerror(errno)); 102132295Srwatson } 103132295Srwatson 104132295Srwatson /* 105132295Srwatson * Allocate a file descriptor and make sure it's fd2+2. 2 because 106132295Srwatson * we allocate an fd for the socket. 107132295Srwatson */ 108132295Srwatson fd3 = dup(STDIN_FILENO); 109136843Srwatson if (fd3 != fd2 + 2) 110137587Snik printf("not ok 1 - (%d, %d, %d)\n", fd1, fd2, fd3); 111136843Srwatson else 112137587Snik printf("ok 1\n"); 113132295Srwatson 114132295Srwatson return (0); 115132295Srwatson} 116