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