1 #include <darwintest.h>
2 #include <darwintest_utils.h>
4 #include <sys/select.h>
7 T_META_RUN_CONCURRENTLY(true),
8 T_META_LTEPHASE(LTE_POSTINIT
)
12 fd_select_close_helper(void *ctx
)
16 // wait for the thread to enter select
23 T_DECL(fd_select_close
, "Test for 54795873: make sure close breaks out of select")
29 rc
= socketpair(PF_LOCAL
, SOCK_STREAM
, 0, pair
);
30 T_ASSERT_POSIX_SUCCESS(rc
, "socketpair");
32 pthread_create(&th
, NULL
, fd_select_close_helper
, pair
);
35 FD_SET(pair
[0], &read_fd
);
37 rc
= select(pair
[0] + 1, &read_fd
, NULL
, NULL
, NULL
);
38 T_EXPECT_POSIX_FAILURE(rc
, EBADF
, "select broke out with EBADF");
42 fd_stress_dup2_close_fun(void *ctx
)
44 int thno
= (int)(long)ctx
;
45 int count
= 10000, rc
;
47 for (int i
= 1; i
<= count
; i
++) {
48 rc
= dup2(STDIN_FILENO
, 42);
49 T_QUIET
; T_EXPECT_POSIX_SUCCESS(rc
, "dup2(%d, 42)", STDIN_FILENO
);
53 T_QUIET
; T_EXPECT_POSIX_FAILURE(rc
, EBADF
, "close(42)");
57 T_LOG("thread %d: %d/%d dups\n", thno
, i
, count
);
64 T_DECL(fd_stress_dup2_close
, "Stress test races between dup2 and close")
69 for (int i
= 0; i
< 4; i
++) {
70 rc
= pthread_create(&th
[i
], NULL
,
71 fd_stress_dup2_close_fun
, (void *)(long)i
);
72 T_ASSERT_POSIX_ZERO(rc
, "pthread_create");
75 for (int i
= 0; i
< 4; i
++) {
76 pthread_join(th
[i
], NULL
);
80 T_DECL(fd_dup2_erase_clofork_58446996
,
81 "Make sure dup2() doesn't inherit flags from an old fd")
85 fd1
= open("/dev/null", O_RDONLY
| O_CLOEXEC
);
86 T_ASSERT_POSIX_SUCCESS(fd1
, "open(/dev/null)");
88 fd2
= open("/dev/null", O_RDONLY
| O_CLOEXEC
);
89 T_ASSERT_POSIX_SUCCESS(fd2
, "open(/dev/null)");
91 T_ASSERT_POSIX_SUCCESS(dup2(fd1
, fd2
), "dup2(fd1, fd2)");
92 T_EXPECT_EQ(fcntl(fd2
, F_GETFD
, 0), 0,
93 "neither FD_CLOEXEC nor FD_CLOFORK should be set");