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