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