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