Path: blob/main/tests/sys/file/newfileops_on_fork_test.c
39536 views
/*-1* Copyright (c) 2009 Robert N. M. Watson2* All rights reserved.3*4* This software was developed at the University of Cambridge Computer5* Laboratory with support from a grant from Google, Inc.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*30* When a multi-threaded application calls fork(2) from one thread while31* another thread is blocked in accept(2), we prefer that the file descriptor32* to be returned by accept(2) not appear in the child process. Test this by33* creating a thread blocked in accept(2), then forking a child and seeing if34* the fd it would have returned is defined in the child or not.35*/3637#include <sys/socket.h>38#include <sys/wait.h>3940#include <netinet/in.h>4142#include <err.h>43#include <errno.h>44#include <pthread.h>45#include <signal.h>46#include <stdlib.h>47#include <string.h>48#include <unistd.h>4950#ifndef PORT51#define PORT 900052#endif5354static int listen_fd;5556static void *57do_accept(__unused void *arg)58{59int accept_fd;6061accept_fd = accept(listen_fd, NULL, NULL);62if (accept_fd < 0)63err(1, "accept");6465close(accept_fd);66return (NULL);67}6869static void70do_fork(void)71{72int pid;7374pid = fork();75if (pid < 0)76err(1, "fork");77if (pid > 0) {78waitpid(pid, NULL, 0);79exit(0);80}8182/*83* We will call ftruncate(2) on the next available file descriptor,84* listen_fd+1, and get back EBADF if it's not a valid descriptor,85* and EINVAL if it is. This (currently) works fine in practice.86*/87if (ftruncate(listen_fd + 1, 0) < 0) {88if (errno == EBADF)89exit(0);90else if (errno == EINVAL)91errx(1, "file descriptor still open in child");92else93err(1, "unexpected error");94} else95errx(1, "ftruncate succeeded");96}9798int99main(void)100{101struct sockaddr_in sin;102pthread_t accept_thread;103104listen_fd = socket(PF_INET, SOCK_STREAM, 0);105if (listen_fd < 0)106err(1, "socket");107bzero(&sin, sizeof(sin));108sin.sin_family = AF_INET;109sin.sin_len = sizeof(sin);110sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);111sin.sin_port = htons(PORT);112if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)113err(1, "bind");114if (listen(listen_fd, -1) <0)115err(1, "listen");116if (pthread_create(&accept_thread, NULL, do_accept, NULL) != 0)117err(1, "pthread_create");118sleep(1); /* Easier than using a CV. */119do_fork();120exit(0);121}122123124