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