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