1*2c2f96dcSApple OSS Distributions #include <unistd.h>
2*2c2f96dcSApple OSS Distributions #include <pthread.h>
3*2c2f96dcSApple OSS Distributions #include <errno.h>
4*2c2f96dcSApple OSS Distributions
5*2c2f96dcSApple OSS Distributions #include <sys/event.h>
6*2c2f96dcSApple OSS Distributions #include <mach/mach.h>
7*2c2f96dcSApple OSS Distributions #include <mach/mach_port.h>
8*2c2f96dcSApple OSS Distributions
9*2c2f96dcSApple OSS Distributions #include <Block.h>
10*2c2f96dcSApple OSS Distributions #include <darwintest.h>
11*2c2f96dcSApple OSS Distributions
12*2c2f96dcSApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
13*2c2f96dcSApple OSS Distributions
14*2c2f96dcSApple OSS Distributions /*
15*2c2f96dcSApple OSS Distributions * <rdar://problem/30231213> close() of kqueue FD races with kqueue_scan park
16*2c2f96dcSApple OSS Distributions *
17*2c2f96dcSApple OSS Distributions * When close concurrent with poll goes wrong, the close hangs
18*2c2f96dcSApple OSS Distributions * and the kevent never gets any more events.
19*2c2f96dcSApple OSS Distributions */
20*2c2f96dcSApple OSS Distributions
21*2c2f96dcSApple OSS Distributions /* Both events should fire at about the same time */
22*2c2f96dcSApple OSS Distributions static uint32_t timeout_ms = 10;
23*2c2f96dcSApple OSS Distributions
24*2c2f96dcSApple OSS Distributions static void *
poll_kqueue(void * arg)25*2c2f96dcSApple OSS Distributions poll_kqueue(void *arg)
26*2c2f96dcSApple OSS Distributions {
27*2c2f96dcSApple OSS Distributions int fd = (int)arg;
28*2c2f96dcSApple OSS Distributions
29*2c2f96dcSApple OSS Distributions struct kevent kev = {
30*2c2f96dcSApple OSS Distributions .filter = EVFILT_TIMER,
31*2c2f96dcSApple OSS Distributions .flags = EV_ADD,
32*2c2f96dcSApple OSS Distributions .data = timeout_ms,
33*2c2f96dcSApple OSS Distributions };
34*2c2f96dcSApple OSS Distributions
35*2c2f96dcSApple OSS Distributions int rv = kevent(fd, &kev, 1, NULL, 0, NULL);
36*2c2f96dcSApple OSS Distributions
37*2c2f96dcSApple OSS Distributions if (rv == -1 && errno == EBADF) {
38*2c2f96dcSApple OSS Distributions /* The close may race with this thread spawning */
39*2c2f96dcSApple OSS Distributions T_LOG("kqueue already closed?");
40*2c2f96dcSApple OSS Distributions return NULL;
41*2c2f96dcSApple OSS Distributions } else {
42*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "kevent");
43*2c2f96dcSApple OSS Distributions }
44*2c2f96dcSApple OSS Distributions
45*2c2f96dcSApple OSS Distributions while ((rv = kevent(fd, NULL, 0, &kev, 1, NULL)) == 1) {
46*2c2f96dcSApple OSS Distributions T_LOG("poll\n");
47*2c2f96dcSApple OSS Distributions }
48*2c2f96dcSApple OSS Distributions
49*2c2f96dcSApple OSS Distributions if (rv != -1 || errno != EBADF) {
50*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(rv, "fd should be closed");
51*2c2f96dcSApple OSS Distributions }
52*2c2f96dcSApple OSS Distributions
53*2c2f96dcSApple OSS Distributions return NULL;
54*2c2f96dcSApple OSS Distributions }
55*2c2f96dcSApple OSS Distributions
56*2c2f96dcSApple OSS Distributions static void
run_test(void)57*2c2f96dcSApple OSS Distributions run_test(void)
58*2c2f96dcSApple OSS Distributions {
59*2c2f96dcSApple OSS Distributions int fd = kqueue();
60*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(fd, "kqueue");
61*2c2f96dcSApple OSS Distributions
62*2c2f96dcSApple OSS Distributions pthread_t thread;
63*2c2f96dcSApple OSS Distributions int rv = pthread_create(&thread, NULL, poll_kqueue,
64*2c2f96dcSApple OSS Distributions (void *)(uintptr_t)fd);
65*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_create");
66*2c2f96dcSApple OSS Distributions
67*2c2f96dcSApple OSS Distributions usleep(timeout_ms * 1000);
68*2c2f96dcSApple OSS Distributions
69*2c2f96dcSApple OSS Distributions rv = close(fd);
70*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(rv, "close");
71*2c2f96dcSApple OSS Distributions
72*2c2f96dcSApple OSS Distributions rv = pthread_join(thread, NULL);
73*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rv, "pthread_join");
74*2c2f96dcSApple OSS Distributions }
75*2c2f96dcSApple OSS Distributions
76*2c2f96dcSApple OSS Distributions T_DECL(kqueue_close_race, "Races kqueue close with kqueue process",
77*2c2f96dcSApple OSS Distributions T_META_LTEPHASE(LTE_POSTINIT), T_META_TIMEOUT(5))
78*2c2f96dcSApple OSS Distributions {
79*2c2f96dcSApple OSS Distributions for (uint32_t i = 1; i < 100; i++) {
80*2c2f96dcSApple OSS Distributions run_test();
81*2c2f96dcSApple OSS Distributions }
82*2c2f96dcSApple OSS Distributions }
83*2c2f96dcSApple OSS Distributions
84*2c2f96dcSApple OSS Distributions static void *
pthread_async_do(void * arg)85*2c2f96dcSApple OSS Distributions pthread_async_do(void *arg)
86*2c2f96dcSApple OSS Distributions {
87*2c2f96dcSApple OSS Distributions void (^block)(void) = arg;
88*2c2f96dcSApple OSS Distributions block();
89*2c2f96dcSApple OSS Distributions Block_release(block);
90*2c2f96dcSApple OSS Distributions pthread_detach(pthread_self());
91*2c2f96dcSApple OSS Distributions return NULL;
92*2c2f96dcSApple OSS Distributions }
93*2c2f96dcSApple OSS Distributions
94*2c2f96dcSApple OSS Distributions static void
95*2c2f96dcSApple OSS Distributions pthread_async(void (^block)(void))
96*2c2f96dcSApple OSS Distributions {
97*2c2f96dcSApple OSS Distributions pthread_t th;
98*2c2f96dcSApple OSS Distributions int rc;
99*2c2f96dcSApple OSS Distributions
100*2c2f96dcSApple OSS Distributions rc = pthread_create(&th, NULL, pthread_async_do, Block_copy(block));
101*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(rc, "pthread_create");
102*2c2f96dcSApple OSS Distributions }
103*2c2f96dcSApple OSS Distributions
104*2c2f96dcSApple OSS Distributions T_DECL(kqueue_filter_close,
105*2c2f96dcSApple OSS Distributions "Check closing a kqfile with various filters works, rdar://72542450")
106*2c2f96dcSApple OSS Distributions {
107*2c2f96dcSApple OSS Distributions mach_port_t mp, pset;
108*2c2f96dcSApple OSS Distributions kern_return_t kr;
109*2c2f96dcSApple OSS Distributions struct kevent ke;
110*2c2f96dcSApple OSS Distributions int kq, rc;
111*2c2f96dcSApple OSS Distributions int pfd[2];
112*2c2f96dcSApple OSS Distributions
113*2c2f96dcSApple OSS Distributions kq = kqueue();
114*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kq, "kqueue()");
115*2c2f96dcSApple OSS Distributions
116*2c2f96dcSApple OSS Distributions kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp);
117*2c2f96dcSApple OSS Distributions T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "mach_port_allocate(RECEIVE)");
118*2c2f96dcSApple OSS Distributions
119*2c2f96dcSApple OSS Distributions ke = (struct kevent){
120*2c2f96dcSApple OSS Distributions .filter = EVFILT_MACHPORT,
121*2c2f96dcSApple OSS Distributions .flags = EV_ADD,
122*2c2f96dcSApple OSS Distributions .ident = mp,
123*2c2f96dcSApple OSS Distributions };
124*2c2f96dcSApple OSS Distributions rc = kevent(kq, &ke, 1, NULL, 0, NULL);
125*2c2f96dcSApple OSS Distributions T_EXPECT_POSIX_SUCCESS(rc, "kevent(mp)");
126*2c2f96dcSApple OSS Distributions
127*2c2f96dcSApple OSS Distributions kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_PORT_SET, &pset);
128*2c2f96dcSApple OSS Distributions T_QUIET; T_EXPECT_MACH_SUCCESS(kr, "mach_port_allocate(PSET)");
129*2c2f96dcSApple OSS Distributions
130*2c2f96dcSApple OSS Distributions ke = (struct kevent){
131*2c2f96dcSApple OSS Distributions .filter = EVFILT_MACHPORT,
132*2c2f96dcSApple OSS Distributions .flags = EV_ADD,
133*2c2f96dcSApple OSS Distributions .ident = pset,
134*2c2f96dcSApple OSS Distributions };
135*2c2f96dcSApple OSS Distributions rc = kevent(kq, &ke, 1, NULL, 0, NULL);
136*2c2f96dcSApple OSS Distributions T_EXPECT_POSIX_SUCCESS(rc, "kevent(pset)");
137*2c2f96dcSApple OSS Distributions
138*2c2f96dcSApple OSS Distributions rc = pipe(pfd);
139*2c2f96dcSApple OSS Distributions T_EXPECT_POSIX_SUCCESS(rc, "pipe");
140*2c2f96dcSApple OSS Distributions
141*2c2f96dcSApple OSS Distributions ke = (struct kevent){
142*2c2f96dcSApple OSS Distributions .filter = EVFILT_READ,
143*2c2f96dcSApple OSS Distributions .flags = EV_ADD,
144*2c2f96dcSApple OSS Distributions .ident = (unsigned long)pfd[0],
145*2c2f96dcSApple OSS Distributions };
146*2c2f96dcSApple OSS Distributions rc = kevent(kq, &ke, 1, NULL, 0, NULL);
147*2c2f96dcSApple OSS Distributions T_EXPECT_POSIX_SUCCESS(rc, "kevent(fd)");
148*2c2f96dcSApple OSS Distributions
149*2c2f96dcSApple OSS Distributions pthread_async(^{
150*2c2f96dcSApple OSS Distributions sleep(1);
151*2c2f96dcSApple OSS Distributions T_EXPECT_POSIX_SUCCESS(close(kq), "close");
152*2c2f96dcSApple OSS Distributions });
153*2c2f96dcSApple OSS Distributions
154*2c2f96dcSApple OSS Distributions rc = kevent(kq, NULL, 0, &ke, 1, NULL);
155*2c2f96dcSApple OSS Distributions T_EXPECT_POSIX_FAILURE(rc, EBADF, "kevent(closed fd) returns EBADF");
156*2c2f96dcSApple OSS Distributions }
157