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