xref: /xnu-8792.41.9/tests/kqueue_in_select.c (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributions #include <unistd.h>
2*5c2921b0SApple OSS Distributions #include <errno.h>
3*5c2921b0SApple OSS Distributions #include <sys/event.h>
4*5c2921b0SApple OSS Distributions #include <sys/select.h>
5*5c2921b0SApple OSS Distributions #include <Block.h>
6*5c2921b0SApple OSS Distributions #include <darwintest.h>
7*5c2921b0SApple OSS Distributions 
8*5c2921b0SApple OSS Distributions T_GLOBAL_META(
9*5c2921b0SApple OSS Distributions 	T_META_NAMESPACE("xnu.kevent"),
10*5c2921b0SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
11*5c2921b0SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("kevent"),
12*5c2921b0SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true)
13*5c2921b0SApple OSS Distributions 	);
14*5c2921b0SApple OSS Distributions 
15*5c2921b0SApple OSS Distributions #define TV(s) (struct timeval){ .tv_sec = s }
16*5c2921b0SApple OSS Distributions 
17*5c2921b0SApple OSS Distributions static void *
pthread_async_do(void * arg)18*5c2921b0SApple OSS Distributions pthread_async_do(void *arg)
19*5c2921b0SApple OSS Distributions {
20*5c2921b0SApple OSS Distributions 	void (^block)(void) = arg;
21*5c2921b0SApple OSS Distributions 	block();
22*5c2921b0SApple OSS Distributions 	Block_release(block);
23*5c2921b0SApple OSS Distributions 	pthread_detach(pthread_self());
24*5c2921b0SApple OSS Distributions 	return NULL;
25*5c2921b0SApple OSS Distributions }
26*5c2921b0SApple OSS Distributions 
27*5c2921b0SApple OSS Distributions static void
28*5c2921b0SApple OSS Distributions pthread_async(void (^block)(void))
29*5c2921b0SApple OSS Distributions {
30*5c2921b0SApple OSS Distributions 	pthread_t th;
31*5c2921b0SApple OSS Distributions 	int rc;
32*5c2921b0SApple OSS Distributions 
33*5c2921b0SApple OSS Distributions 	rc = pthread_create(&th, NULL, pthread_async_do, Block_copy(block));
34*5c2921b0SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(rc, "pthread_create");
35*5c2921b0SApple OSS Distributions }
36*5c2921b0SApple OSS Distributions 
37*5c2921b0SApple OSS Distributions T_DECL(kqueue_in_select, "make sure kqueue in select works")
38*5c2921b0SApple OSS Distributions {
39*5c2921b0SApple OSS Distributions 	fd_set rd_set;
40*5c2921b0SApple OSS Distributions 	int kq_fd, ret, nfd;
41*5c2921b0SApple OSS Distributions 	struct kevent ret_kev;
42*5c2921b0SApple OSS Distributions 	const struct kevent kev = {
43*5c2921b0SApple OSS Distributions 		.ident = 1,
44*5c2921b0SApple OSS Distributions 		.filter = EVFILT_USER,
45*5c2921b0SApple OSS Distributions 		.flags = EV_ADD | EV_CLEAR,
46*5c2921b0SApple OSS Distributions 	};
47*5c2921b0SApple OSS Distributions 
48*5c2921b0SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS((kq_fd = kqueue()), NULL);
49*5c2921b0SApple OSS Distributions 	ret = kevent(kq_fd, &kev, 1, NULL, 0, NULL);
50*5c2921b0SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "kevent");
51*5c2921b0SApple OSS Distributions 
52*5c2921b0SApple OSS Distributions 	FD_ZERO(&rd_set);
53*5c2921b0SApple OSS Distributions 	FD_SET(kq_fd, &rd_set);
54*5c2921b0SApple OSS Distributions 	nfd = select(kq_fd + 1, &rd_set, NULL, NULL, &TV(1));
55*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(nfd, 0, "no trigger");
56*5c2921b0SApple OSS Distributions 
57*5c2921b0SApple OSS Distributions 	pthread_async(^{
58*5c2921b0SApple OSS Distributions 		sleep(1);
59*5c2921b0SApple OSS Distributions 		const struct kevent k = {
60*5c2921b0SApple OSS Distributions 		        .ident = 1,
61*5c2921b0SApple OSS Distributions 		        .filter = EVFILT_USER,
62*5c2921b0SApple OSS Distributions 		        .flags = EV_ADD | EV_CLEAR,
63*5c2921b0SApple OSS Distributions 		        .fflags = NOTE_TRIGGER,
64*5c2921b0SApple OSS Distributions 		};
65*5c2921b0SApple OSS Distributions 
66*5c2921b0SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(kevent(kq_fd, &k, 1, NULL, 0, NULL), "trigger");
67*5c2921b0SApple OSS Distributions 	});
68*5c2921b0SApple OSS Distributions 
69*5c2921b0SApple OSS Distributions 	FD_ZERO(&rd_set);
70*5c2921b0SApple OSS Distributions 	FD_SET(kq_fd, &rd_set);
71*5c2921b0SApple OSS Distributions 	nfd = select(kq_fd + 1, &rd_set, NULL, NULL, &TV(5));
72*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(nfd, 1, "kqueue triggered");
73*5c2921b0SApple OSS Distributions 
74*5c2921b0SApple OSS Distributions 	FD_ZERO(&rd_set);
75*5c2921b0SApple OSS Distributions 	FD_SET(kq_fd, &rd_set);
76*5c2921b0SApple OSS Distributions 	nfd = select(kq_fd + 1, &rd_set, NULL, NULL, &TV(1));
77*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(nfd, 1, "kqueue is still triggered");
78*5c2921b0SApple OSS Distributions 
79*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(kevent(kq_fd, NULL, 0, &ret_kev, 1, NULL), 1, "pump event");
80*5c2921b0SApple OSS Distributions 
81*5c2921b0SApple OSS Distributions 	FD_ZERO(&rd_set);
82*5c2921b0SApple OSS Distributions 	FD_SET(kq_fd, &rd_set);
83*5c2921b0SApple OSS Distributions 	nfd = select(kq_fd + 1, &rd_set, NULL, NULL, &TV(1));
84*5c2921b0SApple OSS Distributions 	T_EXPECT_EQ(nfd, 0, "no trigger");
85*5c2921b0SApple OSS Distributions }
86