1*2c2f96dcSApple OSS Distributions #ifdef T_NAMESPACE
2*2c2f96dcSApple OSS Distributions #undef T_NAMESPACE
3*2c2f96dcSApple OSS Distributions #endif
4*2c2f96dcSApple OSS Distributions
5*2c2f96dcSApple OSS Distributions #include <darwintest.h>
6*2c2f96dcSApple OSS Distributions #include <mach/mach.h>
7*2c2f96dcSApple OSS Distributions #include <darwintest_multiprocess.h>
8*2c2f96dcSApple OSS Distributions
9*2c2f96dcSApple OSS Distributions #include <assert.h>
10*2c2f96dcSApple OSS Distributions #include <dispatch/dispatch.h>
11*2c2f96dcSApple OSS Distributions #include <dispatch/private.h>
12*2c2f96dcSApple OSS Distributions #include <err.h>
13*2c2f96dcSApple OSS Distributions #include <errno.h>
14*2c2f96dcSApple OSS Distributions #include <fcntl.h>
15*2c2f96dcSApple OSS Distributions #include <poll.h>
16*2c2f96dcSApple OSS Distributions #include <pthread.h>
17*2c2f96dcSApple OSS Distributions #include <pthread/workqueue_private.h>
18*2c2f96dcSApple OSS Distributions #include <stdio.h>
19*2c2f96dcSApple OSS Distributions #include <stdlib.h>
20*2c2f96dcSApple OSS Distributions #include <string.h>
21*2c2f96dcSApple OSS Distributions #include <sys/event.h>
22*2c2f96dcSApple OSS Distributions #include <sys/socket.h>
23*2c2f96dcSApple OSS Distributions #include <sys/stat.h>
24*2c2f96dcSApple OSS Distributions #include <sys/time.h>
25*2c2f96dcSApple OSS Distributions #include <sys/types.h>
26*2c2f96dcSApple OSS Distributions #include <sys/wait.h>
27*2c2f96dcSApple OSS Distributions #include <sysexits.h>
28*2c2f96dcSApple OSS Distributions #include <unistd.h>
29*2c2f96dcSApple OSS Distributions #include <util.h>
30*2c2f96dcSApple OSS Distributions #include <System/sys/event.h> /* kevent_qos */
31*2c2f96dcSApple OSS Distributions
32*2c2f96dcSApple OSS Distributions T_GLOBAL_META(
33*2c2f96dcSApple OSS Distributions T_META_NAMESPACE("xnu.kevent"),
34*2c2f96dcSApple OSS Distributions T_META_CHECK_LEAKS(false),
35*2c2f96dcSApple OSS Distributions T_META_LTEPHASE(LTE_POSTINIT));
36*2c2f96dcSApple OSS Distributions
37*2c2f96dcSApple OSS Distributions /*
38*2c2f96dcSApple OSS Distributions * Test to validate that monitoring a PTY device, FIFO, pipe, or socket pair in
39*2c2f96dcSApple OSS Distributions * a dispatch source, kqueue, poll, or select delivers read events within and
40*2c2f96dcSApple OSS Distributions * between processes as expected.
41*2c2f96dcSApple OSS Distributions *
42*2c2f96dcSApple OSS Distributions * This test catches issues with watching special devices in kqueue(),
43*2c2f96dcSApple OSS Distributions * which has tricky special cases for character devices like PTYs.
44*2c2f96dcSApple OSS Distributions *
45*2c2f96dcSApple OSS Distributions * It also exercises the path to wake up a dispatch worker thread from the
46*2c2f96dcSApple OSS Distributions * special device kqueue event, which is also a special case in kqueue().
47*2c2f96dcSApple OSS Distributions *
48*2c2f96dcSApple OSS Distributions * See rdar://problem/26240299&26220074&26226862&28625427 for examples and
49*2c2f96dcSApple OSS Distributions * history.
50*2c2f96dcSApple OSS Distributions */
51*2c2f96dcSApple OSS Distributions
52*2c2f96dcSApple OSS Distributions #define EXPECTED_STRING "abcdefghijklmnopqrstuvwxyz. ABCDEFGHIJKLMNOPQRSTUVWXYZ. 1234567890"
53*2c2f96dcSApple OSS Distributions #define EXPECTED_LEN strlen(EXPECTED_STRING)
54*2c2f96dcSApple OSS Distributions
55*2c2f96dcSApple OSS Distributions #define READ_SETUP_TIMEOUT_SECS 2
56*2c2f96dcSApple OSS Distributions #define WRITE_TIMEOUT_SECS 4
57*2c2f96dcSApple OSS Distributions #define READ_TIMEOUT_SECS 4
58*2c2f96dcSApple OSS Distributions #define INCREMENTAL_WRITE_SLEEP_USECS 50
59*2c2f96dcSApple OSS Distributions
60*2c2f96dcSApple OSS Distributions static mach_timespec_t READ_SETUP_timeout = {.tv_sec = READ_SETUP_TIMEOUT_SECS, .tv_nsec = 0};
61*2c2f96dcSApple OSS Distributions static mach_timespec_t READ_timeout = {.tv_sec = READ_TIMEOUT_SECS, .tv_nsec = 0};
62*2c2f96dcSApple OSS Distributions static mach_timespec_t WRITE_timeout = {.tv_sec = WRITE_TIMEOUT_SECS, .tv_nsec = 0};
63*2c2f96dcSApple OSS Distributions
64*2c2f96dcSApple OSS Distributions enum fd_pair {
65*2c2f96dcSApple OSS Distributions PTY_PAIR,
66*2c2f96dcSApple OSS Distributions FIFO_PAIR,
67*2c2f96dcSApple OSS Distributions PIPE_PAIR,
68*2c2f96dcSApple OSS Distributions SOCKET_PAIR
69*2c2f96dcSApple OSS Distributions };
70*2c2f96dcSApple OSS Distributions
71*2c2f96dcSApple OSS Distributions enum write_mode {
72*2c2f96dcSApple OSS Distributions FULL_WRITE,
73*2c2f96dcSApple OSS Distributions INCREMENTAL_WRITE,
74*2c2f96dcSApple OSS Distributions KEVENT_INCREMENTAL_WRITE,
75*2c2f96dcSApple OSS Distributions KEVENT64_INCREMENTAL_WRITE,
76*2c2f96dcSApple OSS Distributions KEVENT_QOS_INCREMENTAL_WRITE,
77*2c2f96dcSApple OSS Distributions WORKQ_INCREMENTAL_WRITE,
78*2c2f96dcSApple OSS Distributions DISPATCH_INCREMENTAL_WRITE
79*2c2f96dcSApple OSS Distributions };
80*2c2f96dcSApple OSS Distributions
81*2c2f96dcSApple OSS Distributions enum read_mode {
82*2c2f96dcSApple OSS Distributions POLL_READ,
83*2c2f96dcSApple OSS Distributions SELECT_READ,
84*2c2f96dcSApple OSS Distributions KEVENT_READ,
85*2c2f96dcSApple OSS Distributions KEVENT64_READ,
86*2c2f96dcSApple OSS Distributions KEVENT_QOS_READ,
87*2c2f96dcSApple OSS Distributions WORKQ_READ,
88*2c2f96dcSApple OSS Distributions DISPATCH_READ
89*2c2f96dcSApple OSS Distributions };
90*2c2f96dcSApple OSS Distributions
91*2c2f96dcSApple OSS Distributions union mode {
92*2c2f96dcSApple OSS Distributions enum read_mode rd;
93*2c2f96dcSApple OSS Distributions enum write_mode wr;
94*2c2f96dcSApple OSS Distributions };
95*2c2f96dcSApple OSS Distributions
96*2c2f96dcSApple OSS Distributions static struct {
97*2c2f96dcSApple OSS Distributions enum fd_pair fd_pair;
98*2c2f96dcSApple OSS Distributions enum write_mode wr_mode;
99*2c2f96dcSApple OSS Distributions int wr_fd;
100*2c2f96dcSApple OSS Distributions enum read_mode rd_mode;
101*2c2f96dcSApple OSS Distributions int rd_fd;
102*2c2f96dcSApple OSS Distributions
103*2c2f96dcSApple OSS Distributions enum writer_kind {
104*2c2f96dcSApple OSS Distributions THREAD_WRITER, /* sem */
105*2c2f96dcSApple OSS Distributions PROCESS_WRITER /* fd */
106*2c2f96dcSApple OSS Distributions } wr_kind;
107*2c2f96dcSApple OSS Distributions union {
108*2c2f96dcSApple OSS Distributions semaphore_t sem;
109*2c2f96dcSApple OSS Distributions struct {
110*2c2f96dcSApple OSS Distributions int in_fd;
111*2c2f96dcSApple OSS Distributions int out_fd;
112*2c2f96dcSApple OSS Distributions };
113*2c2f96dcSApple OSS Distributions } wr_wait;
114*2c2f96dcSApple OSS Distributions semaphore_t wr_finished;
115*2c2f96dcSApple OSS Distributions semaphore_t rd_finished;
116*2c2f96dcSApple OSS Distributions } shared;
117*2c2f96dcSApple OSS Distributions
118*2c2f96dcSApple OSS Distributions static bool handle_reading(enum fd_pair fd_pair, int fd);
119*2c2f96dcSApple OSS Distributions static bool handle_writing(enum fd_pair fd_pair, int fd);
120*2c2f96dcSApple OSS Distributions static void drive_kq(bool reading, union mode mode, enum fd_pair fd_pair,
121*2c2f96dcSApple OSS Distributions int fd);
122*2c2f96dcSApple OSS Distributions
123*2c2f96dcSApple OSS Distributions #pragma mark writing
124*2c2f96dcSApple OSS Distributions
125*2c2f96dcSApple OSS Distributions static void
wake_writer(void)126*2c2f96dcSApple OSS Distributions wake_writer(void)
127*2c2f96dcSApple OSS Distributions {
128*2c2f96dcSApple OSS Distributions T_LOG("waking writer");
129*2c2f96dcSApple OSS Distributions
130*2c2f96dcSApple OSS Distributions switch (shared.wr_kind) {
131*2c2f96dcSApple OSS Distributions case THREAD_WRITER:
132*2c2f96dcSApple OSS Distributions T_LOG("signal shared.wr_wait.sem");
133*2c2f96dcSApple OSS Distributions semaphore_signal(shared.wr_wait.sem);
134*2c2f96dcSApple OSS Distributions break;
135*2c2f96dcSApple OSS Distributions case PROCESS_WRITER: {
136*2c2f96dcSApple OSS Distributions char tmp = 'a';
137*2c2f96dcSApple OSS Distributions close(shared.wr_wait.out_fd);
138*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(write(
139*2c2f96dcSApple OSS Distributions shared.wr_wait.in_fd, &tmp, 1), NULL);
140*2c2f96dcSApple OSS Distributions break;
141*2c2f96dcSApple OSS Distributions }
142*2c2f96dcSApple OSS Distributions }
143*2c2f96dcSApple OSS Distributions }
144*2c2f96dcSApple OSS Distributions
145*2c2f96dcSApple OSS Distributions static void
writer_wait(void)146*2c2f96dcSApple OSS Distributions writer_wait(void)
147*2c2f96dcSApple OSS Distributions {
148*2c2f96dcSApple OSS Distributions switch (shared.wr_kind) {
149*2c2f96dcSApple OSS Distributions case THREAD_WRITER:
150*2c2f96dcSApple OSS Distributions T_LOG("wait shared.wr_wait.sem");
151*2c2f96dcSApple OSS Distributions kern_return_t kret = semaphore_timedwait(shared.wr_wait.sem, READ_SETUP_timeout);
152*2c2f96dcSApple OSS Distributions
153*2c2f96dcSApple OSS Distributions if (kret == KERN_OPERATION_TIMED_OUT) {
154*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("THREAD_WRITER semaphore timedout after %d seconds", READ_SETUP_timeout.tv_sec);
155*2c2f96dcSApple OSS Distributions }
156*2c2f96dcSApple OSS Distributions T_QUIET;
157*2c2f96dcSApple OSS Distributions T_ASSERT_MACH_SUCCESS(kret, "semaphore_timedwait shared.wr_wait.sem");
158*2c2f96dcSApple OSS Distributions break;
159*2c2f96dcSApple OSS Distributions
160*2c2f96dcSApple OSS Distributions case PROCESS_WRITER: {
161*2c2f96dcSApple OSS Distributions char tmp;
162*2c2f96dcSApple OSS Distributions close(shared.wr_wait.in_fd);
163*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(read(
164*2c2f96dcSApple OSS Distributions shared.wr_wait.out_fd, &tmp, 1), NULL);
165*2c2f96dcSApple OSS Distributions break;
166*2c2f96dcSApple OSS Distributions }
167*2c2f96dcSApple OSS Distributions }
168*2c2f96dcSApple OSS Distributions
169*2c2f96dcSApple OSS Distributions T_LOG("writer woken up, starting to write");
170*2c2f96dcSApple OSS Distributions }
171*2c2f96dcSApple OSS Distributions
172*2c2f96dcSApple OSS Distributions static bool
handle_writing(enum fd_pair __unused fd_pair,int fd)173*2c2f96dcSApple OSS Distributions handle_writing(enum fd_pair __unused fd_pair, int fd)
174*2c2f96dcSApple OSS Distributions {
175*2c2f96dcSApple OSS Distributions static unsigned int cur_char = 0;
176*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(write(fd,
177*2c2f96dcSApple OSS Distributions &(EXPECTED_STRING[cur_char]), 1), NULL);
178*2c2f96dcSApple OSS Distributions cur_char++;
179*2c2f96dcSApple OSS Distributions
180*2c2f96dcSApple OSS Distributions return cur_char < EXPECTED_LEN;
181*2c2f96dcSApple OSS Distributions }
182*2c2f96dcSApple OSS Distributions
183*2c2f96dcSApple OSS Distributions #define EXPECTED_QOS QOS_CLASS_USER_INITIATED
184*2c2f96dcSApple OSS Distributions
185*2c2f96dcSApple OSS Distributions static void
reenable_workq(int fd,int16_t filt)186*2c2f96dcSApple OSS Distributions reenable_workq(int fd, int16_t filt)
187*2c2f96dcSApple OSS Distributions {
188*2c2f96dcSApple OSS Distributions struct kevent_qos_s events[] = {{
189*2c2f96dcSApple OSS Distributions .ident = (uint64_t)fd,
190*2c2f96dcSApple OSS Distributions .filter = filt,
191*2c2f96dcSApple OSS Distributions .flags = EV_ENABLE | EV_UDATA_SPECIFIC | EV_DISPATCH,
192*2c2f96dcSApple OSS Distributions .qos = (int32_t)_pthread_qos_class_encode(EXPECTED_QOS,
193*2c2f96dcSApple OSS Distributions 0, 0),
194*2c2f96dcSApple OSS Distributions .fflags = NOTE_LOWAT,
195*2c2f96dcSApple OSS Distributions .data = 1
196*2c2f96dcSApple OSS Distributions }};
197*2c2f96dcSApple OSS Distributions
198*2c2f96dcSApple OSS Distributions int kev = kevent_qos(-1, events, 1, events, 1, NULL, NULL,
199*2c2f96dcSApple OSS Distributions KEVENT_FLAG_WORKQ | KEVENT_FLAG_ERROR_EVENTS);
200*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(kev, "reenable workq in kevent_qos");
201*2c2f96dcSApple OSS Distributions }
202*2c2f96dcSApple OSS Distributions
203*2c2f96dcSApple OSS Distributions static void
workqueue_write_fn(void ** __unused buf,int * __unused count)204*2c2f96dcSApple OSS Distributions workqueue_write_fn(void ** __unused buf, int * __unused count)
205*2c2f96dcSApple OSS Distributions {
206*2c2f96dcSApple OSS Distributions // T_MAYFAIL;
207*2c2f96dcSApple OSS Distributions // T_QUIET; T_ASSERT_EFFECTIVE_QOS_EQ(EXPECTED_QOS,
208*2c2f96dcSApple OSS Distributions // "writer thread should be woken up at correct QoS");
209*2c2f96dcSApple OSS Distributions if (!handle_writing(shared.fd_pair, shared.wr_fd)) {
210*2c2f96dcSApple OSS Distributions /* finished handling the fd, tear down the source */
211*2c2f96dcSApple OSS Distributions T_LOG("signal shared.wr_finished");
212*2c2f96dcSApple OSS Distributions semaphore_signal(shared.wr_finished);
213*2c2f96dcSApple OSS Distributions return;
214*2c2f96dcSApple OSS Distributions }
215*2c2f96dcSApple OSS Distributions
216*2c2f96dcSApple OSS Distributions reenable_workq(shared.wr_fd, EVFILT_WRITE);
217*2c2f96dcSApple OSS Distributions }
218*2c2f96dcSApple OSS Distributions
219*2c2f96dcSApple OSS Distributions static void
workqueue_fn(pthread_priority_t __unused priority)220*2c2f96dcSApple OSS Distributions workqueue_fn(pthread_priority_t __unused priority)
221*2c2f96dcSApple OSS Distributions {
222*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("workqueue function callback was called");
223*2c2f96dcSApple OSS Distributions }
224*2c2f96dcSApple OSS Distributions
225*2c2f96dcSApple OSS Distributions static void
drive_kq(bool reading,union mode mode,enum fd_pair fd_pair,int fd)226*2c2f96dcSApple OSS Distributions drive_kq(bool reading, union mode mode, enum fd_pair fd_pair, int fd)
227*2c2f96dcSApple OSS Distributions {
228*2c2f96dcSApple OSS Distributions struct timespec timeout = { .tv_sec = READ_TIMEOUT_SECS };
229*2c2f96dcSApple OSS Distributions int kev = -1;
230*2c2f96dcSApple OSS Distributions
231*2c2f96dcSApple OSS Distributions struct kevent events;
232*2c2f96dcSApple OSS Distributions EV_SET(&events, fd, reading ? EVFILT_READ : EVFILT_WRITE, EV_ADD,
233*2c2f96dcSApple OSS Distributions NOTE_LOWAT, 1, NULL);
234*2c2f96dcSApple OSS Distributions struct kevent64_s events64;
235*2c2f96dcSApple OSS Distributions EV_SET64(&events64, fd, reading ? EVFILT_READ : EVFILT_WRITE, EV_ADD,
236*2c2f96dcSApple OSS Distributions NOTE_LOWAT, 1, 0, 0, 0);
237*2c2f96dcSApple OSS Distributions struct kevent_qos_s events_qos[] = {{
238*2c2f96dcSApple OSS Distributions .ident = (uint64_t)fd,
239*2c2f96dcSApple OSS Distributions .filter = reading ? EVFILT_READ : EVFILT_WRITE,
240*2c2f96dcSApple OSS Distributions .flags = EV_ADD,
241*2c2f96dcSApple OSS Distributions .fflags = NOTE_LOWAT,
242*2c2f96dcSApple OSS Distributions .data = 1
243*2c2f96dcSApple OSS Distributions }, {
244*2c2f96dcSApple OSS Distributions .ident = 0,
245*2c2f96dcSApple OSS Distributions .filter = EVFILT_TIMER,
246*2c2f96dcSApple OSS Distributions .flags = EV_ADD,
247*2c2f96dcSApple OSS Distributions .fflags = NOTE_SECONDS,
248*2c2f96dcSApple OSS Distributions .data = READ_TIMEOUT_SECS
249*2c2f96dcSApple OSS Distributions }};
250*2c2f96dcSApple OSS Distributions
251*2c2f96dcSApple OSS Distributions /* determine which variant of kevent to use */
252*2c2f96dcSApple OSS Distributions enum read_mode which_kevent;
253*2c2f96dcSApple OSS Distributions if (reading) {
254*2c2f96dcSApple OSS Distributions which_kevent = mode.rd;
255*2c2f96dcSApple OSS Distributions } else {
256*2c2f96dcSApple OSS Distributions if (mode.wr == KEVENT_INCREMENTAL_WRITE) {
257*2c2f96dcSApple OSS Distributions which_kevent = KEVENT_READ;
258*2c2f96dcSApple OSS Distributions } else if (mode.wr == KEVENT64_INCREMENTAL_WRITE) {
259*2c2f96dcSApple OSS Distributions which_kevent = KEVENT64_READ;
260*2c2f96dcSApple OSS Distributions } else if (mode.wr == KEVENT_QOS_INCREMENTAL_WRITE) {
261*2c2f96dcSApple OSS Distributions which_kevent = KEVENT_QOS_READ;
262*2c2f96dcSApple OSS Distributions } else {
263*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("unexpected mode: %d", mode.wr);
264*2c2f96dcSApple OSS Distributions __builtin_unreachable();
265*2c2f96dcSApple OSS Distributions }
266*2c2f96dcSApple OSS Distributions }
267*2c2f96dcSApple OSS Distributions
268*2c2f96dcSApple OSS Distributions int kq_fd = kqueue();
269*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(kq_fd, "kqueue");
270*2c2f96dcSApple OSS Distributions
271*2c2f96dcSApple OSS Distributions switch (which_kevent) {
272*2c2f96dcSApple OSS Distributions case KEVENT_READ:
273*2c2f96dcSApple OSS Distributions kev = kevent(kq_fd, &events, 1, NULL, 0, NULL);
274*2c2f96dcSApple OSS Distributions break;
275*2c2f96dcSApple OSS Distributions case KEVENT64_READ:
276*2c2f96dcSApple OSS Distributions kev = kevent64(kq_fd, &events64, 1, NULL, 0, 0, NULL);
277*2c2f96dcSApple OSS Distributions break;
278*2c2f96dcSApple OSS Distributions case KEVENT_QOS_READ:
279*2c2f96dcSApple OSS Distributions kev = kevent_qos(kq_fd, events_qos, 2, NULL, 0, NULL, NULL, 0);
280*2c2f96dcSApple OSS Distributions break;
281*2c2f96dcSApple OSS Distributions case POLL_READ: /* FALLTHROUGH */
282*2c2f96dcSApple OSS Distributions case SELECT_READ: /* FALLTHROUGH */
283*2c2f96dcSApple OSS Distributions case DISPATCH_READ: /* FALLTHROUGH */
284*2c2f96dcSApple OSS Distributions case WORKQ_READ: /* FALLTHROUGH */
285*2c2f96dcSApple OSS Distributions default:
286*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("unexpected mode: %d", reading ? mode.rd : mode.wr);
287*2c2f96dcSApple OSS Distributions break;
288*2c2f96dcSApple OSS Distributions }
289*2c2f96dcSApple OSS Distributions
290*2c2f96dcSApple OSS Distributions if (reading) {
291*2c2f96dcSApple OSS Distributions wake_writer();
292*2c2f96dcSApple OSS Distributions } else {
293*2c2f96dcSApple OSS Distributions writer_wait();
294*2c2f96dcSApple OSS Distributions }
295*2c2f96dcSApple OSS Distributions
296*2c2f96dcSApple OSS Distributions for (;;) {
297*2c2f96dcSApple OSS Distributions switch (which_kevent) {
298*2c2f96dcSApple OSS Distributions case KEVENT_READ:
299*2c2f96dcSApple OSS Distributions kev = kevent(kq_fd, NULL, 0, &events, 1, &timeout);
300*2c2f96dcSApple OSS Distributions break;
301*2c2f96dcSApple OSS Distributions case KEVENT64_READ:
302*2c2f96dcSApple OSS Distributions kev = kevent64(kq_fd, NULL, 0, &events64, 1, 0, &timeout);
303*2c2f96dcSApple OSS Distributions break;
304*2c2f96dcSApple OSS Distributions case KEVENT_QOS_READ:
305*2c2f96dcSApple OSS Distributions kev = kevent_qos(kq_fd, NULL, 0, events_qos, 2, NULL, NULL, 0);
306*2c2f96dcSApple OSS Distributions
307*2c2f96dcSApple OSS Distributions /* check for a timeout */
308*2c2f96dcSApple OSS Distributions for (int i = 0; i < kev; i++) {
309*2c2f96dcSApple OSS Distributions if (events_qos[i].filter == EVFILT_TIMER) {
310*2c2f96dcSApple OSS Distributions kev = 0;
311*2c2f96dcSApple OSS Distributions }
312*2c2f96dcSApple OSS Distributions }
313*2c2f96dcSApple OSS Distributions break;
314*2c2f96dcSApple OSS Distributions case POLL_READ: /* FALLTHROUGH */
315*2c2f96dcSApple OSS Distributions case SELECT_READ: /* FALLTHROUGH */
316*2c2f96dcSApple OSS Distributions case DISPATCH_READ: /* FALLTHROUGH */
317*2c2f96dcSApple OSS Distributions case WORKQ_READ: /* FALLTHROUGH */
318*2c2f96dcSApple OSS Distributions default:
319*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("unexpected mode: %d", reading ? mode.rd : mode.wr);
320*2c2f96dcSApple OSS Distributions break;
321*2c2f96dcSApple OSS Distributions }
322*2c2f96dcSApple OSS Distributions
323*2c2f96dcSApple OSS Distributions if (kev == -1 && errno == EINTR) {
324*2c2f96dcSApple OSS Distributions T_LOG("kevent was interrupted");
325*2c2f96dcSApple OSS Distributions continue;
326*2c2f96dcSApple OSS Distributions }
327*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(kev, "kevent");
328*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_NE(kev, 0, "kevent timed out");
329*2c2f96dcSApple OSS Distributions
330*2c2f96dcSApple OSS Distributions if (reading) {
331*2c2f96dcSApple OSS Distributions if (!handle_reading(fd_pair, fd)) {
332*2c2f96dcSApple OSS Distributions break;
333*2c2f96dcSApple OSS Distributions }
334*2c2f96dcSApple OSS Distributions } else {
335*2c2f96dcSApple OSS Distributions if (!handle_writing(fd_pair, fd)) {
336*2c2f96dcSApple OSS Distributions break;
337*2c2f96dcSApple OSS Distributions }
338*2c2f96dcSApple OSS Distributions }
339*2c2f96dcSApple OSS Distributions }
340*2c2f96dcSApple OSS Distributions
341*2c2f96dcSApple OSS Distributions close(kq_fd);
342*2c2f96dcSApple OSS Distributions }
343*2c2f96dcSApple OSS Distributions
344*2c2f96dcSApple OSS Distributions static void *
write_to_fd(void * __unused ctx)345*2c2f96dcSApple OSS Distributions write_to_fd(void * __unused ctx)
346*2c2f96dcSApple OSS Distributions {
347*2c2f96dcSApple OSS Distributions ssize_t bytes_wr = 0;
348*2c2f96dcSApple OSS Distributions
349*2c2f96dcSApple OSS Distributions writer_wait();
350*2c2f96dcSApple OSS Distributions
351*2c2f96dcSApple OSS Distributions switch (shared.wr_mode) {
352*2c2f96dcSApple OSS Distributions case FULL_WRITE:
353*2c2f96dcSApple OSS Distributions do {
354*2c2f96dcSApple OSS Distributions if (bytes_wr == -1) {
355*2c2f96dcSApple OSS Distributions T_LOG("write from child was interrupted");
356*2c2f96dcSApple OSS Distributions }
357*2c2f96dcSApple OSS Distributions bytes_wr = write(shared.wr_fd, EXPECTED_STRING,
358*2c2f96dcSApple OSS Distributions EXPECTED_LEN);
359*2c2f96dcSApple OSS Distributions } while (bytes_wr == -1 && errno == EINTR);
360*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(bytes_wr, "write");
361*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_EQ(bytes_wr, (ssize_t)EXPECTED_LEN,
362*2c2f96dcSApple OSS Distributions "wrote enough bytes");
363*2c2f96dcSApple OSS Distributions break;
364*2c2f96dcSApple OSS Distributions
365*2c2f96dcSApple OSS Distributions case INCREMENTAL_WRITE:
366*2c2f96dcSApple OSS Distributions for (unsigned int i = 0; i < EXPECTED_LEN; i++) {
367*2c2f96dcSApple OSS Distributions T_QUIET;
368*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(write(shared.wr_fd,
369*2c2f96dcSApple OSS Distributions &(EXPECTED_STRING[i]), 1), NULL);
370*2c2f96dcSApple OSS Distributions usleep(INCREMENTAL_WRITE_SLEEP_USECS);
371*2c2f96dcSApple OSS Distributions }
372*2c2f96dcSApple OSS Distributions break;
373*2c2f96dcSApple OSS Distributions
374*2c2f96dcSApple OSS Distributions case KEVENT_INCREMENTAL_WRITE: /* FALLTHROUGH */
375*2c2f96dcSApple OSS Distributions case KEVENT64_INCREMENTAL_WRITE: /* FALLTHROUGH */
376*2c2f96dcSApple OSS Distributions case KEVENT_QOS_INCREMENTAL_WRITE: {
377*2c2f96dcSApple OSS Distributions union mode mode = { .wr = shared.wr_mode };
378*2c2f96dcSApple OSS Distributions drive_kq(false, mode, shared.fd_pair, shared.wr_fd);
379*2c2f96dcSApple OSS Distributions break;
380*2c2f96dcSApple OSS Distributions }
381*2c2f96dcSApple OSS Distributions
382*2c2f96dcSApple OSS Distributions case WORKQ_INCREMENTAL_WRITE: {
383*2c2f96dcSApple OSS Distributions // prohibit ourselves from going multi-threaded see:rdar://33296008
384*2c2f96dcSApple OSS Distributions _dispatch_prohibit_transition_to_multithreaded(true);
385*2c2f96dcSApple OSS Distributions int changes = 1;
386*2c2f96dcSApple OSS Distributions
387*2c2f96dcSApple OSS Distributions T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &shared.wr_finished, SYNC_POLICY_FIFO, 0),
388*2c2f96dcSApple OSS Distributions "semaphore_create shared.wr_finished");
389*2c2f96dcSApple OSS Distributions
390*2c2f96dcSApple OSS Distributions T_QUIET;
391*2c2f96dcSApple OSS Distributions T_ASSERT_NE_UINT(shared.wr_finished, (unsigned)MACH_PORT_NULL, "wr_finished semaphore_create");
392*2c2f96dcSApple OSS Distributions
393*2c2f96dcSApple OSS Distributions T_QUIET;
394*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_kevent(workqueue_fn, workqueue_write_fn, 0, 0), NULL);
395*2c2f96dcSApple OSS Distributions
396*2c2f96dcSApple OSS Distributions struct kevent_qos_s events[] = {{
397*2c2f96dcSApple OSS Distributions .ident = (uint64_t)shared.wr_fd,
398*2c2f96dcSApple OSS Distributions .filter = EVFILT_WRITE,
399*2c2f96dcSApple OSS Distributions .flags = EV_ADD | EV_UDATA_SPECIFIC | EV_DISPATCH | EV_VANISHED,
400*2c2f96dcSApple OSS Distributions .fflags = NOTE_LOWAT,
401*2c2f96dcSApple OSS Distributions .data = 1,
402*2c2f96dcSApple OSS Distributions .qos = (int32_t)_pthread_qos_class_encode(EXPECTED_QOS,
403*2c2f96dcSApple OSS Distributions 0, 0)
404*2c2f96dcSApple OSS Distributions }};
405*2c2f96dcSApple OSS Distributions
406*2c2f96dcSApple OSS Distributions for (;;) {
407*2c2f96dcSApple OSS Distributions int kev = kevent_qos(-1, changes == 0 ? NULL : events, changes,
408*2c2f96dcSApple OSS Distributions events, 1, NULL, NULL,
409*2c2f96dcSApple OSS Distributions KEVENT_FLAG_WORKQ | KEVENT_FLAG_ERROR_EVENTS);
410*2c2f96dcSApple OSS Distributions if (kev == -1 && errno == EINTR) {
411*2c2f96dcSApple OSS Distributions changes = 0;
412*2c2f96dcSApple OSS Distributions T_LOG("kevent_qos was interrupted");
413*2c2f96dcSApple OSS Distributions continue;
414*2c2f96dcSApple OSS Distributions }
415*2c2f96dcSApple OSS Distributions
416*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(kev, "kevent_qos");
417*2c2f96dcSApple OSS Distributions break;
418*2c2f96dcSApple OSS Distributions }
419*2c2f96dcSApple OSS Distributions break;
420*2c2f96dcSApple OSS Distributions }
421*2c2f96dcSApple OSS Distributions
422*2c2f96dcSApple OSS Distributions case DISPATCH_INCREMENTAL_WRITE: {
423*2c2f96dcSApple OSS Distributions dispatch_source_t write_src;
424*2c2f96dcSApple OSS Distributions
425*2c2f96dcSApple OSS Distributions T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &shared.wr_finished, SYNC_POLICY_FIFO, 0),
426*2c2f96dcSApple OSS Distributions "semaphore_create shared.wr_finished");
427*2c2f96dcSApple OSS Distributions
428*2c2f96dcSApple OSS Distributions T_QUIET;
429*2c2f96dcSApple OSS Distributions T_ASSERT_NE_UINT(shared.wr_finished, (unsigned)MACH_PORT_NULL, "semaphore_create");
430*2c2f96dcSApple OSS Distributions
431*2c2f96dcSApple OSS Distributions write_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_WRITE,
432*2c2f96dcSApple OSS Distributions (uintptr_t)shared.wr_fd, 0, NULL);
433*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(write_src,
434*2c2f96dcSApple OSS Distributions "dispatch_source_create(DISPATCH_SOURCE_TYPE_WRITE ...)");
435*2c2f96dcSApple OSS Distributions
436*2c2f96dcSApple OSS Distributions dispatch_block_t handler = dispatch_block_create_with_qos_class(
437*2c2f96dcSApple OSS Distributions DISPATCH_BLOCK_ENFORCE_QOS_CLASS, EXPECTED_QOS, 0, ^{
438*2c2f96dcSApple OSS Distributions // T_MAYFAIL;
439*2c2f96dcSApple OSS Distributions // T_QUIET; T_ASSERT_EFFECTIVE_QOS_EQ(EXPECTED_QOS,
440*2c2f96dcSApple OSS Distributions // "write handler block should run at correct QoS");
441*2c2f96dcSApple OSS Distributions if (!handle_writing(shared.fd_pair, shared.wr_fd)) {
442*2c2f96dcSApple OSS Distributions /* finished handling the fd, tear down the source */
443*2c2f96dcSApple OSS Distributions dispatch_source_cancel(write_src);
444*2c2f96dcSApple OSS Distributions dispatch_release(write_src);
445*2c2f96dcSApple OSS Distributions T_LOG("signal shared.wr_finished");
446*2c2f96dcSApple OSS Distributions semaphore_signal(shared.wr_finished);
447*2c2f96dcSApple OSS Distributions }
448*2c2f96dcSApple OSS Distributions });
449*2c2f96dcSApple OSS Distributions
450*2c2f96dcSApple OSS Distributions dispatch_source_set_event_handler(write_src, handler);
451*2c2f96dcSApple OSS Distributions dispatch_activate(write_src);
452*2c2f96dcSApple OSS Distributions
453*2c2f96dcSApple OSS Distributions break;
454*2c2f96dcSApple OSS Distributions }
455*2c2f96dcSApple OSS Distributions
456*2c2f96dcSApple OSS Distributions default:
457*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("unrecognized write mode: %d", shared.wr_mode);
458*2c2f96dcSApple OSS Distributions break;
459*2c2f96dcSApple OSS Distributions }
460*2c2f96dcSApple OSS Distributions
461*2c2f96dcSApple OSS Distributions if (shared.wr_finished) {
462*2c2f96dcSApple OSS Distributions T_LOG("wait shared.wr_finished");
463*2c2f96dcSApple OSS Distributions kern_return_t kret = semaphore_timedwait(shared.wr_finished, WRITE_timeout);
464*2c2f96dcSApple OSS Distributions if (kret == KERN_OPERATION_TIMED_OUT) {
465*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("write side semaphore timedout after %d seconds", WRITE_timeout.tv_sec);
466*2c2f96dcSApple OSS Distributions }
467*2c2f96dcSApple OSS Distributions T_QUIET;
468*2c2f96dcSApple OSS Distributions T_ASSERT_MACH_SUCCESS(kret, "semaphore_timedwait shared.wr_finished");
469*2c2f96dcSApple OSS Distributions semaphore_destroy(mach_task_self(), shared.wr_finished);
470*2c2f96dcSApple OSS Distributions }
471*2c2f96dcSApple OSS Distributions
472*2c2f96dcSApple OSS Distributions T_LOG("writer finished, closing fd");
473*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(close(shared.wr_fd), NULL);
474*2c2f96dcSApple OSS Distributions return NULL;
475*2c2f96dcSApple OSS Distributions }
476*2c2f96dcSApple OSS Distributions
477*2c2f96dcSApple OSS Distributions #pragma mark reading
478*2c2f96dcSApple OSS Distributions
479*2c2f96dcSApple OSS Distributions #define BUF_LEN 1024
480*2c2f96dcSApple OSS Distributions static char final_string[BUF_LEN];
481*2c2f96dcSApple OSS Distributions static size_t final_length;
482*2c2f96dcSApple OSS Distributions
483*2c2f96dcSApple OSS Distributions /*
484*2c2f96dcSApple OSS Distributions * Read from the master PTY descriptor.
485*2c2f96dcSApple OSS Distributions *
486*2c2f96dcSApple OSS Distributions * Returns false if EOF is encountered, and true otherwise.
487*2c2f96dcSApple OSS Distributions */
488*2c2f96dcSApple OSS Distributions static bool
handle_reading(enum fd_pair fd_pair,int fd)489*2c2f96dcSApple OSS Distributions handle_reading(enum fd_pair fd_pair, int fd)
490*2c2f96dcSApple OSS Distributions {
491*2c2f96dcSApple OSS Distributions char read_buf[BUF_LEN] = { 0 };
492*2c2f96dcSApple OSS Distributions ssize_t bytes_rd = 0;
493*2c2f96dcSApple OSS Distributions
494*2c2f96dcSApple OSS Distributions do {
495*2c2f96dcSApple OSS Distributions if (bytes_rd == -1) {
496*2c2f96dcSApple OSS Distributions T_LOG("read was interrupted, retrying");
497*2c2f96dcSApple OSS Distributions }
498*2c2f96dcSApple OSS Distributions bytes_rd = read(fd, read_buf, sizeof(read_buf) - 1);
499*2c2f96dcSApple OSS Distributions } while (bytes_rd == -1 && errno == EINTR);
500*2c2f96dcSApple OSS Distributions
501*2c2f96dcSApple OSS Distributions // T_LOG("read %zd bytes: '%s'", bytes_rd, read_buf);
502*2c2f96dcSApple OSS Distributions
503*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(bytes_rd, "reading from file");
504*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_LE(bytes_rd, (ssize_t)EXPECTED_LEN,
505*2c2f96dcSApple OSS Distributions "read too much from file");
506*2c2f96dcSApple OSS Distributions
507*2c2f96dcSApple OSS Distributions if (bytes_rd == 0) {
508*2c2f96dcSApple OSS Distributions T_LOG("read EOF from file");
509*2c2f96dcSApple OSS Distributions return false;
510*2c2f96dcSApple OSS Distributions }
511*2c2f96dcSApple OSS Distributions
512*2c2f96dcSApple OSS Distributions read_buf[bytes_rd] = '\0';
513*2c2f96dcSApple OSS Distributions strlcpy(&(final_string[final_length]), read_buf,
514*2c2f96dcSApple OSS Distributions sizeof(final_string) - final_length);
515*2c2f96dcSApple OSS Distributions final_length += (size_t)bytes_rd;
516*2c2f96dcSApple OSS Distributions
517*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_LE(final_length, EXPECTED_LEN,
518*2c2f96dcSApple OSS Distributions "should not read more from file than what can be sent");
519*2c2f96dcSApple OSS Distributions
520*2c2f96dcSApple OSS Distributions /* FIFOs don't send EOF when the write side closes */
521*2c2f96dcSApple OSS Distributions if (final_length == strlen(EXPECTED_STRING) &&
522*2c2f96dcSApple OSS Distributions (fd_pair == FIFO_PAIR)) {
523*2c2f96dcSApple OSS Distributions T_LOG("read all expected bytes from FIFO");
524*2c2f96dcSApple OSS Distributions return false;
525*2c2f96dcSApple OSS Distributions }
526*2c2f96dcSApple OSS Distributions return true;
527*2c2f96dcSApple OSS Distributions }
528*2c2f96dcSApple OSS Distributions
529*2c2f96dcSApple OSS Distributions static void
workqueue_read_fn(void ** __unused buf,int * __unused count)530*2c2f96dcSApple OSS Distributions workqueue_read_fn(void ** __unused buf, int * __unused count)
531*2c2f96dcSApple OSS Distributions {
532*2c2f96dcSApple OSS Distributions // T_MAYFAIL;
533*2c2f96dcSApple OSS Distributions // T_QUIET; T_ASSERT_EFFECTIVE_QOS_EQ(EXPECTED_QOS,
534*2c2f96dcSApple OSS Distributions // "reader thread should be requested at correct QoS");
535*2c2f96dcSApple OSS Distributions if (!handle_reading(shared.fd_pair, shared.rd_fd)) {
536*2c2f96dcSApple OSS Distributions T_LOG("signal shared.rd_finished");
537*2c2f96dcSApple OSS Distributions semaphore_signal(shared.rd_finished);
538*2c2f96dcSApple OSS Distributions }
539*2c2f96dcSApple OSS Distributions
540*2c2f96dcSApple OSS Distributions reenable_workq(shared.rd_fd, EVFILT_READ);
541*2c2f96dcSApple OSS Distributions }
542*2c2f96dcSApple OSS Distributions
543*2c2f96dcSApple OSS Distributions static void
read_from_fd(int fd,enum fd_pair fd_pair,enum read_mode mode)544*2c2f96dcSApple OSS Distributions read_from_fd(int fd, enum fd_pair fd_pair, enum read_mode mode)
545*2c2f96dcSApple OSS Distributions {
546*2c2f96dcSApple OSS Distributions int fd_flags;
547*2c2f96dcSApple OSS Distributions
548*2c2f96dcSApple OSS Distributions T_LOG("reader setting up");
549*2c2f96dcSApple OSS Distributions
550*2c2f96dcSApple OSS Distributions bzero(final_string, sizeof(final_string));
551*2c2f96dcSApple OSS Distributions
552*2c2f96dcSApple OSS Distributions fd_flags = fcntl(fd, F_GETFL, 0);
553*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(fd_flags, "fcntl(F_GETFL)");
554*2c2f96dcSApple OSS Distributions
555*2c2f96dcSApple OSS Distributions if (!(fd_flags & O_NONBLOCK)) {
556*2c2f96dcSApple OSS Distributions T_QUIET;
557*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fcntl(fd, F_SETFL,
558*2c2f96dcSApple OSS Distributions fd_flags | O_NONBLOCK), NULL);
559*2c2f96dcSApple OSS Distributions }
560*2c2f96dcSApple OSS Distributions
561*2c2f96dcSApple OSS Distributions switch (mode) {
562*2c2f96dcSApple OSS Distributions case POLL_READ: {
563*2c2f96dcSApple OSS Distributions struct pollfd fds[] = { { .fd = fd, .events = POLLIN } };
564*2c2f96dcSApple OSS Distributions wake_writer();
565*2c2f96dcSApple OSS Distributions
566*2c2f96dcSApple OSS Distributions for (;;) {
567*2c2f96dcSApple OSS Distributions fds[0].revents = 0;
568*2c2f96dcSApple OSS Distributions int pol = poll(fds, 1, READ_TIMEOUT_SECS * 1000);
569*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(pol, "poll");
570*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_NE(pol, 0,
571*2c2f96dcSApple OSS Distributions "poll should not time out after %d seconds, read %zd out "
572*2c2f96dcSApple OSS Distributions "of %zu bytes",
573*2c2f96dcSApple OSS Distributions READ_TIMEOUT_SECS, final_length, strlen(EXPECTED_STRING));
574*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_FALSE(fds[0].revents & POLLERR,
575*2c2f96dcSApple OSS Distributions "should not see an error on the device");
576*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_FALSE(fds[0].revents & POLLNVAL,
577*2c2f96dcSApple OSS Distributions "should not set up an invalid poll");
578*2c2f96dcSApple OSS Distributions
579*2c2f96dcSApple OSS Distributions if (!handle_reading(fd_pair, fd)) {
580*2c2f96dcSApple OSS Distributions break;
581*2c2f96dcSApple OSS Distributions }
582*2c2f96dcSApple OSS Distributions }
583*2c2f96dcSApple OSS Distributions break;
584*2c2f96dcSApple OSS Distributions }
585*2c2f96dcSApple OSS Distributions
586*2c2f96dcSApple OSS Distributions case SELECT_READ:
587*2c2f96dcSApple OSS Distributions wake_writer();
588*2c2f96dcSApple OSS Distributions
589*2c2f96dcSApple OSS Distributions for (;;) {
590*2c2f96dcSApple OSS Distributions struct timeval tv = { .tv_sec = READ_TIMEOUT_SECS };
591*2c2f96dcSApple OSS Distributions
592*2c2f96dcSApple OSS Distributions fd_set read_fd;
593*2c2f96dcSApple OSS Distributions FD_ZERO(&read_fd);
594*2c2f96dcSApple OSS Distributions FD_SET(fd, &read_fd);
595*2c2f96dcSApple OSS Distributions fd_set err_fd;
596*2c2f96dcSApple OSS Distributions FD_ZERO(&err_fd);
597*2c2f96dcSApple OSS Distributions FD_SET(fd, &err_fd);
598*2c2f96dcSApple OSS Distributions
599*2c2f96dcSApple OSS Distributions int sel = select(fd + 1, &read_fd, NULL, NULL /*&err_fd*/, &tv);
600*2c2f96dcSApple OSS Distributions if (sel == -1 && errno == EINTR) {
601*2c2f96dcSApple OSS Distributions T_LOG("select interrupted");
602*2c2f96dcSApple OSS Distributions continue;
603*2c2f96dcSApple OSS Distributions }
604*2c2f96dcSApple OSS Distributions (void)fd_pair;
605*2c2f96dcSApple OSS Distributions
606*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(sel, "select");
607*2c2f96dcSApple OSS Distributions
608*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_NE(sel, 0,
609*2c2f96dcSApple OSS Distributions "select waited for %d seconds and timed out",
610*2c2f96dcSApple OSS Distributions READ_TIMEOUT_SECS);
611*2c2f96dcSApple OSS Distributions
612*2c2f96dcSApple OSS Distributions /* didn't fail or time out, therefore data is ready */
613*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_NE(FD_ISSET(fd, &read_fd), 0,
614*2c2f96dcSApple OSS Distributions "select should show reading fd as readable");
615*2c2f96dcSApple OSS Distributions
616*2c2f96dcSApple OSS Distributions if (!handle_reading(fd_pair, fd)) {
617*2c2f96dcSApple OSS Distributions break;
618*2c2f96dcSApple OSS Distributions }
619*2c2f96dcSApple OSS Distributions }
620*2c2f96dcSApple OSS Distributions break;
621*2c2f96dcSApple OSS Distributions
622*2c2f96dcSApple OSS Distributions case KEVENT_READ: /* FALLTHROUGH */
623*2c2f96dcSApple OSS Distributions case KEVENT64_READ: /* FALLTHROUGH */
624*2c2f96dcSApple OSS Distributions case KEVENT_QOS_READ: {
625*2c2f96dcSApple OSS Distributions union mode rd_mode = { .rd = shared.rd_mode };
626*2c2f96dcSApple OSS Distributions drive_kq(true, rd_mode, fd_pair, shared.rd_fd);
627*2c2f96dcSApple OSS Distributions break;
628*2c2f96dcSApple OSS Distributions }
629*2c2f96dcSApple OSS Distributions
630*2c2f96dcSApple OSS Distributions case WORKQ_READ: {
631*2c2f96dcSApple OSS Distributions // prohibit ourselves from going multi-threaded see:rdar://33296008
632*2c2f96dcSApple OSS Distributions _dispatch_prohibit_transition_to_multithreaded(true);
633*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_kevent(
634*2c2f96dcSApple OSS Distributions workqueue_fn, workqueue_read_fn, 0, 0), NULL);
635*2c2f96dcSApple OSS Distributions
636*2c2f96dcSApple OSS Distributions T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &shared.rd_finished, SYNC_POLICY_FIFO, 0),
637*2c2f96dcSApple OSS Distributions "semaphore_create shared.rd_finished");
638*2c2f96dcSApple OSS Distributions
639*2c2f96dcSApple OSS Distributions T_QUIET;
640*2c2f96dcSApple OSS Distributions T_ASSERT_NE_UINT(shared.rd_finished, (unsigned)MACH_PORT_NULL, "semaphore_create");
641*2c2f96dcSApple OSS Distributions
642*2c2f96dcSApple OSS Distributions int changes = 1;
643*2c2f96dcSApple OSS Distributions struct kevent_qos_s events[] = {{
644*2c2f96dcSApple OSS Distributions .ident = (uint64_t)shared.rd_fd,
645*2c2f96dcSApple OSS Distributions .filter = EVFILT_READ,
646*2c2f96dcSApple OSS Distributions .flags = EV_ADD | EV_UDATA_SPECIFIC | EV_DISPATCH | EV_VANISHED,
647*2c2f96dcSApple OSS Distributions .fflags = NOTE_LOWAT,
648*2c2f96dcSApple OSS Distributions .data = 1,
649*2c2f96dcSApple OSS Distributions .qos = (int32_t)_pthread_qos_class_encode(EXPECTED_QOS,
650*2c2f96dcSApple OSS Distributions 0, 0)
651*2c2f96dcSApple OSS Distributions }};
652*2c2f96dcSApple OSS Distributions
653*2c2f96dcSApple OSS Distributions for (;;) {
654*2c2f96dcSApple OSS Distributions int kev = kevent_qos(-1, changes == 0 ? NULL : events, changes,
655*2c2f96dcSApple OSS Distributions events, 1, NULL, NULL,
656*2c2f96dcSApple OSS Distributions KEVENT_FLAG_WORKQ | KEVENT_FLAG_ERROR_EVENTS);
657*2c2f96dcSApple OSS Distributions if (kev == -1 && errno == EINTR) {
658*2c2f96dcSApple OSS Distributions changes = 0;
659*2c2f96dcSApple OSS Distributions T_LOG("kevent_qos was interrupted");
660*2c2f96dcSApple OSS Distributions continue;
661*2c2f96dcSApple OSS Distributions }
662*2c2f96dcSApple OSS Distributions
663*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(kev, "kevent_qos");
664*2c2f96dcSApple OSS Distributions break;
665*2c2f96dcSApple OSS Distributions }
666*2c2f96dcSApple OSS Distributions
667*2c2f96dcSApple OSS Distributions wake_writer();
668*2c2f96dcSApple OSS Distributions break;
669*2c2f96dcSApple OSS Distributions }
670*2c2f96dcSApple OSS Distributions
671*2c2f96dcSApple OSS Distributions case DISPATCH_READ: {
672*2c2f96dcSApple OSS Distributions dispatch_source_t read_src;
673*2c2f96dcSApple OSS Distributions
674*2c2f96dcSApple OSS Distributions T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &shared.rd_finished, SYNC_POLICY_FIFO, 0),
675*2c2f96dcSApple OSS Distributions "semaphore_create shared.rd_finished");
676*2c2f96dcSApple OSS Distributions
677*2c2f96dcSApple OSS Distributions T_QUIET;
678*2c2f96dcSApple OSS Distributions T_ASSERT_NE_UINT(shared.rd_finished, (unsigned)MACH_PORT_NULL, "semaphore_create");
679*2c2f96dcSApple OSS Distributions
680*2c2f96dcSApple OSS Distributions read_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ,
681*2c2f96dcSApple OSS Distributions (uintptr_t)fd, 0, NULL);
682*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(read_src,
683*2c2f96dcSApple OSS Distributions "dispatch_source_create(DISPATCH_SOURCE_TYPE_READ)");
684*2c2f96dcSApple OSS Distributions
685*2c2f96dcSApple OSS Distributions dispatch_block_t handler = dispatch_block_create_with_qos_class(
686*2c2f96dcSApple OSS Distributions DISPATCH_BLOCK_ENFORCE_QOS_CLASS, EXPECTED_QOS, 0, ^{
687*2c2f96dcSApple OSS Distributions // T_MAYFAIL;
688*2c2f96dcSApple OSS Distributions // T_QUIET; T_ASSERT_EFFECTIVE_QOS_EQ(EXPECTED_QOS,
689*2c2f96dcSApple OSS Distributions // "read handler block should run at correct QoS");
690*2c2f96dcSApple OSS Distributions
691*2c2f96dcSApple OSS Distributions if (!handle_reading(fd_pair, fd)) {
692*2c2f96dcSApple OSS Distributions /* finished handling the fd, tear down the source */
693*2c2f96dcSApple OSS Distributions dispatch_source_cancel(read_src);
694*2c2f96dcSApple OSS Distributions dispatch_release(read_src);
695*2c2f96dcSApple OSS Distributions T_LOG("signal shared.rd_finished");
696*2c2f96dcSApple OSS Distributions semaphore_signal(shared.rd_finished);
697*2c2f96dcSApple OSS Distributions }
698*2c2f96dcSApple OSS Distributions });
699*2c2f96dcSApple OSS Distributions
700*2c2f96dcSApple OSS Distributions dispatch_source_set_event_handler(read_src, handler);
701*2c2f96dcSApple OSS Distributions dispatch_activate(read_src);
702*2c2f96dcSApple OSS Distributions
703*2c2f96dcSApple OSS Distributions wake_writer();
704*2c2f96dcSApple OSS Distributions break;
705*2c2f96dcSApple OSS Distributions }
706*2c2f96dcSApple OSS Distributions
707*2c2f96dcSApple OSS Distributions default:
708*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("unrecognized read mode: %d", mode);
709*2c2f96dcSApple OSS Distributions break;
710*2c2f96dcSApple OSS Distributions }
711*2c2f96dcSApple OSS Distributions
712*2c2f96dcSApple OSS Distributions if (shared.rd_finished) {
713*2c2f96dcSApple OSS Distributions T_LOG("wait shared.rd_finished");
714*2c2f96dcSApple OSS Distributions kern_return_t kret = semaphore_timedwait(shared.rd_finished, READ_timeout);
715*2c2f96dcSApple OSS Distributions if (kret == KERN_OPERATION_TIMED_OUT) {
716*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("reading timed out after %d seconds", READ_timeout.tv_sec);
717*2c2f96dcSApple OSS Distributions }
718*2c2f96dcSApple OSS Distributions T_QUIET;
719*2c2f96dcSApple OSS Distributions T_ASSERT_MACH_SUCCESS(kret, "semaphore_timedwait shared.rd_finished");
720*2c2f96dcSApple OSS Distributions }
721*2c2f96dcSApple OSS Distributions
722*2c2f96dcSApple OSS Distributions T_EXPECT_EQ_STR(final_string, EXPECTED_STRING,
723*2c2f96dcSApple OSS Distributions "reader should receive valid string");
724*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(close(fd), NULL);
725*2c2f96dcSApple OSS Distributions }
726*2c2f96dcSApple OSS Distributions
727*2c2f96dcSApple OSS Distributions #pragma mark file setup
728*2c2f96dcSApple OSS Distributions
729*2c2f96dcSApple OSS Distributions static void
fd_pair_init(enum fd_pair fd_pair,int * rd_fd,int * wr_fd)730*2c2f96dcSApple OSS Distributions fd_pair_init(enum fd_pair fd_pair, int *rd_fd, int *wr_fd)
731*2c2f96dcSApple OSS Distributions {
732*2c2f96dcSApple OSS Distributions switch (fd_pair) {
733*2c2f96dcSApple OSS Distributions case PTY_PAIR:
734*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(openpty(rd_fd, wr_fd, NULL, NULL, NULL),
735*2c2f96dcSApple OSS Distributions NULL);
736*2c2f96dcSApple OSS Distributions break;
737*2c2f96dcSApple OSS Distributions
738*2c2f96dcSApple OSS Distributions case FIFO_PAIR: {
739*2c2f96dcSApple OSS Distributions char fifo_path[] = "/tmp/async-io-fifo.XXXXXX";
740*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(mktemp(fifo_path), NULL);
741*2c2f96dcSApple OSS Distributions
742*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(mkfifo(fifo_path, 0700), "mkfifo(%s, 0700)",
743*2c2f96dcSApple OSS Distributions fifo_path);
744*2c2f96dcSApple OSS Distributions /*
745*2c2f96dcSApple OSS Distributions * Opening the read side of a pipe will block until the write
746*2c2f96dcSApple OSS Distributions * side opens -- use O_NONBLOCK.
747*2c2f96dcSApple OSS Distributions */
748*2c2f96dcSApple OSS Distributions *rd_fd = open(fifo_path, O_RDONLY | O_NONBLOCK);
749*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(*rd_fd, "open(... O_RDONLY)");
750*2c2f96dcSApple OSS Distributions *wr_fd = open(fifo_path, O_WRONLY | O_NONBLOCK);
751*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(*wr_fd, "open(... O_WRONLY)");
752*2c2f96dcSApple OSS Distributions break;
753*2c2f96dcSApple OSS Distributions }
754*2c2f96dcSApple OSS Distributions
755*2c2f96dcSApple OSS Distributions case PIPE_PAIR: {
756*2c2f96dcSApple OSS Distributions int pipe_fds[2];
757*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(pipe(pipe_fds), NULL);
758*2c2f96dcSApple OSS Distributions *rd_fd = pipe_fds[0];
759*2c2f96dcSApple OSS Distributions *wr_fd = pipe_fds[1];
760*2c2f96dcSApple OSS Distributions break;
761*2c2f96dcSApple OSS Distributions }
762*2c2f96dcSApple OSS Distributions
763*2c2f96dcSApple OSS Distributions case SOCKET_PAIR: {
764*2c2f96dcSApple OSS Distributions int sock_fds[2];
765*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(socketpair(AF_UNIX, SOCK_STREAM, 0, sock_fds),
766*2c2f96dcSApple OSS Distributions NULL);
767*2c2f96dcSApple OSS Distributions *rd_fd = sock_fds[0];
768*2c2f96dcSApple OSS Distributions *wr_fd = sock_fds[1];
769*2c2f96dcSApple OSS Distributions break;
770*2c2f96dcSApple OSS Distributions }
771*2c2f96dcSApple OSS Distributions
772*2c2f96dcSApple OSS Distributions default:
773*2c2f96dcSApple OSS Distributions T_ASSERT_FAIL("unknown descriptor pair type: %d", fd_pair);
774*2c2f96dcSApple OSS Distributions break;
775*2c2f96dcSApple OSS Distributions }
776*2c2f96dcSApple OSS Distributions
777*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_NE(*rd_fd, -1, "reading descriptor");
778*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_NE(*wr_fd, -1, "writing descriptor");
779*2c2f96dcSApple OSS Distributions }
780*2c2f96dcSApple OSS Distributions
781*2c2f96dcSApple OSS Distributions #pragma mark single process
782*2c2f96dcSApple OSS Distributions
783*2c2f96dcSApple OSS Distributions static void
drive_threads(enum fd_pair fd_pair,enum read_mode rd_mode,enum write_mode wr_mode)784*2c2f96dcSApple OSS Distributions drive_threads(enum fd_pair fd_pair, enum read_mode rd_mode,
785*2c2f96dcSApple OSS Distributions enum write_mode wr_mode)
786*2c2f96dcSApple OSS Distributions {
787*2c2f96dcSApple OSS Distributions pthread_t thread;
788*2c2f96dcSApple OSS Distributions
789*2c2f96dcSApple OSS Distributions shared.fd_pair = fd_pair;
790*2c2f96dcSApple OSS Distributions shared.rd_mode = rd_mode;
791*2c2f96dcSApple OSS Distributions shared.wr_mode = wr_mode;
792*2c2f96dcSApple OSS Distributions fd_pair_init(fd_pair, &(shared.rd_fd), &(shared.wr_fd));
793*2c2f96dcSApple OSS Distributions
794*2c2f96dcSApple OSS Distributions shared.wr_kind = THREAD_WRITER;
795*2c2f96dcSApple OSS Distributions T_ASSERT_MACH_SUCCESS(semaphore_create(mach_task_self(), &shared.wr_wait.sem, SYNC_POLICY_FIFO, 0),
796*2c2f96dcSApple OSS Distributions "semaphore_create shared.wr_wait.sem");
797*2c2f96dcSApple OSS Distributions
798*2c2f96dcSApple OSS Distributions T_QUIET;
799*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_create(&thread, NULL, write_to_fd, NULL),
800*2c2f96dcSApple OSS Distributions NULL);
801*2c2f96dcSApple OSS Distributions T_LOG("created writer thread");
802*2c2f96dcSApple OSS Distributions
803*2c2f96dcSApple OSS Distributions read_from_fd(shared.rd_fd, fd_pair, rd_mode);
804*2c2f96dcSApple OSS Distributions
805*2c2f96dcSApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_join(thread, NULL), NULL);
806*2c2f96dcSApple OSS Distributions
807*2c2f96dcSApple OSS Distributions T_END;
808*2c2f96dcSApple OSS Distributions }
809*2c2f96dcSApple OSS Distributions
810*2c2f96dcSApple OSS Distributions #pragma mark multiple processes
811*2c2f96dcSApple OSS Distributions
812*2c2f96dcSApple OSS Distributions static void __attribute__((noreturn))
drive_processes(enum fd_pair fd_pair,enum read_mode rd_mode,enum write_mode wr_mode)813*2c2f96dcSApple OSS Distributions drive_processes(enum fd_pair fd_pair, enum read_mode rd_mode, enum write_mode wr_mode)
814*2c2f96dcSApple OSS Distributions {
815*2c2f96dcSApple OSS Distributions shared.fd_pair = fd_pair;
816*2c2f96dcSApple OSS Distributions shared.rd_mode = rd_mode;
817*2c2f96dcSApple OSS Distributions shared.wr_mode = wr_mode;
818*2c2f96dcSApple OSS Distributions fd_pair_init(fd_pair, &(shared.rd_fd), &(shared.wr_fd));
819*2c2f96dcSApple OSS Distributions
820*2c2f96dcSApple OSS Distributions shared.wr_kind = PROCESS_WRITER;
821*2c2f96dcSApple OSS Distributions int fds[2];
822*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(pipe(fds), NULL);
823*2c2f96dcSApple OSS Distributions shared.wr_wait.out_fd = fds[0];
824*2c2f96dcSApple OSS Distributions shared.wr_wait.in_fd = fds[1];
825*2c2f96dcSApple OSS Distributions
826*2c2f96dcSApple OSS Distributions T_LOG("starting subprocesses");
827*2c2f96dcSApple OSS Distributions dt_helper_t helpers[2] = {
828*2c2f96dcSApple OSS Distributions dt_fork_helper("reader_helper"),
829*2c2f96dcSApple OSS Distributions dt_fork_helper("writer_helper")
830*2c2f96dcSApple OSS Distributions };
831*2c2f96dcSApple OSS Distributions
832*2c2f96dcSApple OSS Distributions close(shared.rd_fd);
833*2c2f96dcSApple OSS Distributions close(shared.wr_fd);
834*2c2f96dcSApple OSS Distributions
835*2c2f96dcSApple OSS Distributions dt_run_helpers(helpers, 2, 50000);
836*2c2f96dcSApple OSS Distributions }
837*2c2f96dcSApple OSS Distributions
838*2c2f96dcSApple OSS Distributions T_HELPER_DECL(reader_helper, "Read asynchronously")
839*2c2f96dcSApple OSS Distributions {
840*2c2f96dcSApple OSS Distributions close(shared.wr_fd);
841*2c2f96dcSApple OSS Distributions read_from_fd(shared.rd_fd, shared.fd_pair, shared.rd_mode);
842*2c2f96dcSApple OSS Distributions T_END;
843*2c2f96dcSApple OSS Distributions }
844*2c2f96dcSApple OSS Distributions
845*2c2f96dcSApple OSS Distributions T_HELPER_DECL(writer_helper, "Write asynchronously")
846*2c2f96dcSApple OSS Distributions {
847*2c2f96dcSApple OSS Distributions close(shared.rd_fd);
848*2c2f96dcSApple OSS Distributions write_to_fd(NULL);
849*2c2f96dcSApple OSS Distributions }
850*2c2f96dcSApple OSS Distributions
851*2c2f96dcSApple OSS Distributions #pragma mark tests
852*2c2f96dcSApple OSS Distributions
853*2c2f96dcSApple OSS Distributions #define WR_DECL_PROCESSES(desc_name, fd_pair, write_name, write_str, \
854*2c2f96dcSApple OSS Distributions write_mode, read_name, read_mode) \
855*2c2f96dcSApple OSS Distributions T_DECL(desc_name##_r##read_name##_w##write_name##_procs, "read changes to a " \
856*2c2f96dcSApple OSS Distributions #desc_name " with " #read_name " and writing " #write_str \
857*2c2f96dcSApple OSS Distributions " across two processes") \
858*2c2f96dcSApple OSS Distributions { \
859*2c2f96dcSApple OSS Distributions drive_processes(fd_pair, read_mode, write_mode); \
860*2c2f96dcSApple OSS Distributions }
861*2c2f96dcSApple OSS Distributions #define WR_DECL_THREADS(desc_name, fd_pair, write_name, write_str, \
862*2c2f96dcSApple OSS Distributions write_mode, read_name, read_mode) \
863*2c2f96dcSApple OSS Distributions T_DECL(desc_name##_r##read_name##_w##write_name##_thds, "read changes to a " \
864*2c2f96dcSApple OSS Distributions #desc_name " with " #read_name " and writing " #write_str) \
865*2c2f96dcSApple OSS Distributions { \
866*2c2f96dcSApple OSS Distributions drive_threads(fd_pair, read_mode, write_mode); \
867*2c2f96dcSApple OSS Distributions }
868*2c2f96dcSApple OSS Distributions
869*2c2f96dcSApple OSS Distributions #define WR_DECL(desc_name, fd_pair, write_name, write_str, write_mode, \
870*2c2f96dcSApple OSS Distributions read_name, read_mode) \
871*2c2f96dcSApple OSS Distributions WR_DECL_PROCESSES(desc_name, fd_pair, write_name, write_str, \
872*2c2f96dcSApple OSS Distributions write_mode, read_name, read_mode) \
873*2c2f96dcSApple OSS Distributions WR_DECL_THREADS(desc_name, fd_pair, write_name, write_str, \
874*2c2f96dcSApple OSS Distributions write_mode, read_name, read_mode)
875*2c2f96dcSApple OSS Distributions
876*2c2f96dcSApple OSS Distributions #define RD_DECL_SAFE(desc_name, fd_pair, read_name, read_mode) \
877*2c2f96dcSApple OSS Distributions WR_DECL(desc_name, fd_pair, full, "the full string", FULL_WRITE, \
878*2c2f96dcSApple OSS Distributions read_name, read_mode) \
879*2c2f96dcSApple OSS Distributions WR_DECL(desc_name, fd_pair, inc, "incrementally", \
880*2c2f96dcSApple OSS Distributions INCREMENTAL_WRITE, read_name, read_mode)
881*2c2f96dcSApple OSS Distributions
882*2c2f96dcSApple OSS Distributions #define RD_DECL_DISPATCH_ONLY(suffix, desc_name, fd_pair, read_name, \
883*2c2f96dcSApple OSS Distributions read_mode) \
884*2c2f96dcSApple OSS Distributions WR_DECL##suffix(desc_name, fd_pair, inc_dispatch, \
885*2c2f96dcSApple OSS Distributions "incrementally with a dispatch source", \
886*2c2f96dcSApple OSS Distributions DISPATCH_INCREMENTAL_WRITE, read_name, read_mode)
887*2c2f96dcSApple OSS Distributions #define RD_DECL_WORKQ_ONLY(suffix, desc_name, fd_pair, read_name, \
888*2c2f96dcSApple OSS Distributions read_mode) \
889*2c2f96dcSApple OSS Distributions WR_DECL##suffix(desc_name, fd_pair, inc_workq, \
890*2c2f96dcSApple OSS Distributions "incrementally with the workqueue", \
891*2c2f96dcSApple OSS Distributions WORKQ_INCREMENTAL_WRITE, read_name, read_mode)
892*2c2f96dcSApple OSS Distributions
893*2c2f96dcSApple OSS Distributions #define RD_DECL(desc_name, fd_pair, read_name, read_mode) \
894*2c2f96dcSApple OSS Distributions RD_DECL_SAFE(desc_name, fd_pair, read_name, read_mode) \
895*2c2f96dcSApple OSS Distributions RD_DECL_DISPATCH_ONLY(, desc_name, fd_pair, read_name, read_mode)
896*2c2f96dcSApple OSS Distributions // RD_DECL_WORKQ_ONLY(, desc_name, fd_pair, read_name, read_mode)
897*2c2f96dcSApple OSS Distributions
898*2c2f96dcSApple OSS Distributions /*
899*2c2f96dcSApple OSS Distributions * dispatch_source tests cannot share the same process as other workqueue
900*2c2f96dcSApple OSS Distributions * tests.
901*2c2f96dcSApple OSS Distributions */
902*2c2f96dcSApple OSS Distributions #define RD_DECL_DISPATCH(desc_name, fd_pair, read_name, read_mode) \
903*2c2f96dcSApple OSS Distributions RD_DECL_SAFE(desc_name, fd_pair, read_name, read_mode) \
904*2c2f96dcSApple OSS Distributions RD_DECL_DISPATCH_ONLY(, desc_name, fd_pair, read_name, read_mode) \
905*2c2f96dcSApple OSS Distributions RD_DECL_WORKQ_ONLY(_PROCESSES, desc_name, fd_pair, read_name, \
906*2c2f96dcSApple OSS Distributions read_mode)
907*2c2f96dcSApple OSS Distributions
908*2c2f96dcSApple OSS Distributions /*
909*2c2f96dcSApple OSS Distributions * Workqueue tests cannot share the same process as other workqueue or
910*2c2f96dcSApple OSS Distributions * dispatch_source tests.
911*2c2f96dcSApple OSS Distributions #define RD_DECL_WORKQ(desc_name, fd_pair, read_name, read_mode) \
912*2c2f96dcSApple OSS Distributions * RD_DECL_SAFE(desc_name, fd_pair, read_name, read_mode) \
913*2c2f96dcSApple OSS Distributions * RD_DECL_DISPATCH_ONLY(_PROCESSES, desc_name, fd_pair, read_name, \
914*2c2f96dcSApple OSS Distributions * read_mode) \
915*2c2f96dcSApple OSS Distributions * RD_DECL_WORKQ_ONLY(_PROCESSES, desc_name, fd_pair, read_name, \
916*2c2f96dcSApple OSS Distributions * read_mode)
917*2c2f96dcSApple OSS Distributions */
918*2c2f96dcSApple OSS Distributions
919*2c2f96dcSApple OSS Distributions #define PAIR_DECL(desc_name, fd_pair) \
920*2c2f96dcSApple OSS Distributions RD_DECL(desc_name, fd_pair, poll, POLL_READ) \
921*2c2f96dcSApple OSS Distributions RD_DECL(desc_name, fd_pair, select, SELECT_READ) \
922*2c2f96dcSApple OSS Distributions RD_DECL(desc_name, fd_pair, kevent, KEVENT_READ) \
923*2c2f96dcSApple OSS Distributions RD_DECL(desc_name, fd_pair, kevent64, KEVENT64_READ) \
924*2c2f96dcSApple OSS Distributions RD_DECL(desc_name, fd_pair, kevent_qos, KEVENT_QOS_READ) \
925*2c2f96dcSApple OSS Distributions RD_DECL_DISPATCH(desc_name, fd_pair, dispatch_source, DISPATCH_READ)
926*2c2f96dcSApple OSS Distributions // RD_DECL_WORKQ(desc_name, fd_pair, workq, WORKQ_READ)
927*2c2f96dcSApple OSS Distributions
928*2c2f96dcSApple OSS Distributions PAIR_DECL(tty, PTY_PAIR)
929*2c2f96dcSApple OSS Distributions PAIR_DECL(pipe, PIPE_PAIR)
930*2c2f96dcSApple OSS Distributions PAIR_DECL(fifo, FIFO_PAIR)
931*2c2f96dcSApple OSS Distributions PAIR_DECL(socket, SOCKET_PAIR)
932