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