Deleted Added
full compact
newfileops_on_fork_test.c (281432) newfileops_on_fork_test.c (282067)
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 *
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 * $FreeBSD: user/ngie/more-tests/tests/sys/file/newfileops_on_fork_test.c 281432 2015-04-11 10:14:59Z ngie $
29 * $FreeBSD: head/tests/sys/file/newfileops_on_fork_test.c 281432 2015-04-11 10:14:59Z ngie $
30 */
31
32/*
33 * When a multi-threaded application calls fork(2) from one thread while
34 * another thread is blocked in accept(2), we prefer that the file descriptor
35 * to be returned by accept(2) not appear in the child process. Test this by
36 * creating a thread blocked in accept(2), then forking a child and seeing if
37 * the fd it would have returned is defined in the child or not.
38 */
39
40#include <sys/socket.h>
41#include <sys/wait.h>
42
43#include <netinet/in.h>
44
45#include <err.h>
46#include <errno.h>
47#include <pthread.h>
48#include <signal.h>
49#include <stdlib.h>
50#include <string.h>
51#include <unistd.h>
52
53#define PORT 9000
54
55static int listen_fd;
56
57static void *
58do_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 return (NULL);
67}
68
69static void
70do_fork(void)
71{
72 int pid;
73
74 pid = fork();
75 if (pid < 0)
76 err(-1, "fork");
77 if (pid > 0) {
78 waitpid(pid, NULL, 0);
79 exit(0);
80 }
81
82 /*
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 */
87 if (ftruncate(listen_fd + 1, 0 < 0)) {
88 if (errno == EBADF)
89 exit(0);
90 else if (errno == EINVAL)
91 errx(-1, "file descriptor still open in child");
92 else
93 err(-1, "unexpected error");
94 } else
95 errx(-1, "ftruncate succeeded");
96}
97
98int
99main(__unused int argc, __unused char *argv[])
100{
101 struct sockaddr_in sin;
102 pthread_t accept_thread;
103
104 listen_fd = socket(PF_INET, SOCK_STREAM, 0);
105 if (listen_fd < 0)
106 err(-1, "socket");
107 bzero(&sin, sizeof(sin));
108 sin.sin_family = AF_INET;
109 sin.sin_len = sizeof(sin);
110 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
111 sin.sin_port = htons(PORT);
112 if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
113 err(-1, "bind");
114 if (listen(listen_fd, -1) <0)
115 err(-1, "listen");
116 if (pthread_create(&accept_thread, NULL, do_accept, NULL) != 0)
117 err(-1, "pthread_create");
118 sleep(1); /* Easier than using a CV. */;
119 do_fork();
120 exit(0);
121}
30 */
31
32/*
33 * When a multi-threaded application calls fork(2) from one thread while
34 * another thread is blocked in accept(2), we prefer that the file descriptor
35 * to be returned by accept(2) not appear in the child process. Test this by
36 * creating a thread blocked in accept(2), then forking a child and seeing if
37 * the fd it would have returned is defined in the child or not.
38 */
39
40#include <sys/socket.h>
41#include <sys/wait.h>
42
43#include <netinet/in.h>
44
45#include <err.h>
46#include <errno.h>
47#include <pthread.h>
48#include <signal.h>
49#include <stdlib.h>
50#include <string.h>
51#include <unistd.h>
52
53#define PORT 9000
54
55static int listen_fd;
56
57static void *
58do_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 return (NULL);
67}
68
69static void
70do_fork(void)
71{
72 int pid;
73
74 pid = fork();
75 if (pid < 0)
76 err(-1, "fork");
77 if (pid > 0) {
78 waitpid(pid, NULL, 0);
79 exit(0);
80 }
81
82 /*
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 */
87 if (ftruncate(listen_fd + 1, 0 < 0)) {
88 if (errno == EBADF)
89 exit(0);
90 else if (errno == EINVAL)
91 errx(-1, "file descriptor still open in child");
92 else
93 err(-1, "unexpected error");
94 } else
95 errx(-1, "ftruncate succeeded");
96}
97
98int
99main(__unused int argc, __unused char *argv[])
100{
101 struct sockaddr_in sin;
102 pthread_t accept_thread;
103
104 listen_fd = socket(PF_INET, SOCK_STREAM, 0);
105 if (listen_fd < 0)
106 err(-1, "socket");
107 bzero(&sin, sizeof(sin));
108 sin.sin_family = AF_INET;
109 sin.sin_len = sizeof(sin);
110 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
111 sin.sin_port = htons(PORT);
112 if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
113 err(-1, "bind");
114 if (listen(listen_fd, -1) <0)
115 err(-1, "listen");
116 if (pthread_create(&accept_thread, NULL, do_accept, NULL) != 0)
117 err(-1, "pthread_create");
118 sleep(1); /* Easier than using a CV. */;
119 do_fork();
120 exit(0);
121}