1 /*-
2 * Copyright (c) 2009 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * This software was developed at the University of Cambridge Computer
6 * Laboratory with support from a grant from Google, Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * When a multi-threaded application calls fork(2) from one thread while
32 * another thread is blocked in accept(2), we prefer that the file descriptor
33 * to be returned by accept(2) not appear in the child process. Test this by
34 * creating a thread blocked in accept(2), then forking a child and seeing if
35 * the fd it would have returned is defined in the child or not.
36 */
37
38 #include <sys/socket.h>
39 #include <sys/wait.h>
40
41 #include <netinet/in.h>
42
43 #include <err.h>
44 #include <errno.h>
45 #include <pthread.h>
46 #include <signal.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #ifndef PORT
52 #define PORT 9000
53 #endif
54
55 static int listen_fd;
56
57 static void *
do_accept(__unused void * arg)58 do_accept(__unused void *arg)
59 {
60 int accept_fd;
61
62 accept_fd = accept(listen_fd, NULL, NULL);
63 if (accept_fd < 0)
64 err(1, "accept");
65
66 close(accept_fd);
67 return (NULL);
68 }
69
70 static void
do_fork(void)71 do_fork(void)
72 {
73 int pid;
74
75 pid = fork();
76 if (pid < 0)
77 err(1, "fork");
78 if (pid > 0) {
79 waitpid(pid, NULL, 0);
80 exit(0);
81 }
82
83 /*
84 * We will call ftruncate(2) on the next available file descriptor,
85 * listen_fd+1, and get back EBADF if it's not a valid descriptor,
86 * and EINVAL if it is. This (currently) works fine in practice.
87 */
88 if (ftruncate(listen_fd + 1, 0) < 0) {
89 if (errno == EBADF)
90 exit(0);
91 else if (errno == EINVAL)
92 errx(1, "file descriptor still open in child");
93 else
94 err(1, "unexpected error");
95 } else
96 errx(1, "ftruncate succeeded");
97 }
98
99 int
main(void)100 main(void)
101 {
102 struct sockaddr_in sin;
103 pthread_t accept_thread;
104
105 listen_fd = socket(PF_INET, SOCK_STREAM, 0);
106 if (listen_fd < 0)
107 err(1, "socket");
108 bzero(&sin, sizeof(sin));
109 sin.sin_family = AF_INET;
110 sin.sin_len = sizeof(sin);
111 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
112 sin.sin_port = htons(PORT);
113 if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
114 err(1, "bind");
115 if (listen(listen_fd, -1) <0)
116 err(1, "listen");
117 if (pthread_create(&accept_thread, NULL, do_accept, NULL) != 0)
118 err(1, "pthread_create");
119 sleep(1); /* Easier than using a CV. */
120 do_fork();
121 exit(0);
122 }
123