Path: blob/main/tools/regression/sockets/accept_fd_leak/accept_fd_leak.c
48255 views
/*-1* Copyright (c) 2004 Robert N. M. Watson2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/param.h>27#include <sys/socket.h>28#include <sys/wait.h>2930#include <netinet/in.h>3132#include <err.h>33#include <errno.h>34#include <fcntl.h>35#include <signal.h>36#include <stdio.h>37#include <stdlib.h>38#include <string.h>39#include <unistd.h>4041#define BIND_ATTEMPTS 1042#define LOOPS 50043#define NUM_ATTEMPTS 10004445static volatile int quit;4647static void48child_died(int sig __unused)49{5051quit = 1;52}5354/*55* This test is intended to detect a leak of a file descriptor in the process56* following a failed non-blocking accept. It measures an available fd57* baseline, then performs 1000 failing accepts, then checks to see what the58* next fd is. It relies on sequential fd allocation, and will test for it59* briefly before beginning (not 100% reliable, but a good start).60*/61int62main(void)63{64struct sockaddr_in sin;65socklen_t size;66pid_t child;67int fd1, fd2, fd3, i, listen_port, s, status;6869printf("1..2\n");7071/*72* Check for sequential fd allocation, and give up early if not.73*/74fd1 = dup(STDIN_FILENO);75fd2 = dup(STDIN_FILENO);76if (fd2 != fd1 + 1)77errx(-1, "Non-sequential fd allocation\n");7879s = socket(PF_INET, SOCK_STREAM, 0);80if (s == -1)81errx(-1, "socket: %s", strerror(errno));8283bzero(&sin, sizeof(sin));84sin.sin_len = sizeof(sin);85sin.sin_family = AF_INET;86sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);8788srandomdev();8990for (i = 0; i < BIND_ATTEMPTS; i++) {91/* Pick a random unprivileged port 1025-65535 */92listen_port = MAX((int)random() % 65535, 1025);93sin.sin_port = htons(listen_port);94if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0)95break;96warn("bind with %d failed", listen_port);97usleep(1000);98}99if (i >= BIND_ATTEMPTS) {100printf("Bail out!\n");101exit(1);102}103104if (listen(s, -1) != 0)105errx(-1, "listen: %s", strerror(errno));106107i = fcntl(s, F_GETFL);108if (i == -1)109errx(-1, "ioctl(F_GETFL): %s", strerror(errno));110i |= O_NONBLOCK;111if (fcntl(s, F_SETFL, i) != 0)112errx(-1, "ioctl(F_SETFL): %s", strerror(errno));113i = fcntl(s, F_GETFL);114if (i == -1)115errx(-1, "ioctl(F_GETFL): %s", strerror(errno));116if ((i & O_NONBLOCK) != O_NONBLOCK)117errx(-1, "Failed to set O_NONBLOCK (i=0x%x)\n", i);118119for (i = 0; i < LOOPS; i++) {120size = sizeof(sin);121if (accept(s, (struct sockaddr *)&sin, &size) != -1)122errx(-1, "accept succeeded\n");123if (errno != EAGAIN)124errx(-1, "accept: %s", strerror(errno));125}126127/*128* Allocate a file descriptor and make sure it's fd2+2. 2 because129* we allocate an fd for the socket.130*/131fd3 = dup(STDIN_FILENO);132if (fd3 != fd2 + 2)133printf("not ok 1 - (%d, %d, %d)\n", fd1, fd2, fd3);134else135printf("ok 1\n");136137/*138* Try failing accept's w/o non-blocking where the destination139* address pointer is invalid.140*/141close(fd3);142signal(SIGCHLD, child_died);143child = fork();144if (child < 0)145errx(-1, "fork: %s", strerror(errno));146147/*148* Child process does `NUM_ATTEMPTS` connects.149*/150if (child == 0) {151close(fd1);152close(fd2);153close(s);154155bzero(&sin, sizeof(sin));156sin.sin_len = sizeof(sin);157sin.sin_family = AF_INET;158sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);159sin.sin_port = htons(listen_port);160161for (i = 0; i < NUM_ATTEMPTS; i++) {162s = socket(PF_INET, SOCK_STREAM, 0);163if (s == -1)164errx(-1, "socket: %s", strerror(errno));165if (connect(s, (struct sockaddr *)&sin,166sizeof(sin)) < 0)167errx(-1, "connect: %s", strerror(errno));168close(s);169}170_exit(0);171}172173/* Reset back to a blocking socket. */174i = fcntl(s, F_GETFL);175if (i == -1)176errx(-1, "ioctl(F_GETFL): %s", strerror(errno));177i &= ~O_NONBLOCK;178if (fcntl(s, F_SETFL, i) != 0)179errx(-1, "ioctl(F_SETFL): %s", strerror(errno));180i = fcntl(s, F_GETFL);181if (i == -1)182errx(-1, "ioctl(F_GETFL): %s", strerror(errno));183if (i & O_NONBLOCK)184errx(-1, "Failed to clear O_NONBLOCK (i=0x%x)\n", i);185186/* Do `NUM_ATTEMPTS` accepts with an invalid pointer. */187for (i = 0; !quit && i < NUM_ATTEMPTS; i++) {188size = sizeof(sin);189if (accept(s, (struct sockaddr *)(uintptr_t)(0x100),190&size) != -1)191errx(-1, "accept succeeded\n");192if (errno != EFAULT)193errx(-1, "accept: %s", strerror(errno));194}195196if (waitpid(child, &status, 0) < 0)197errx(-1, "waitpid: %s", strerror(errno));198if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)199warnx("child process died");200201/*202* Allocate a file descriptor and make sure it's fd2+2. 2 because203* we allocate an fd for the socket.204*/205fd3 = dup(STDIN_FILENO);206if (fd3 != fd2 + 2)207printf("not ok 2 - (%d, %d, %d)\n", fd1, fd2, fd3);208else209printf("ok 2\n");210211return (0);212}213214215