1*5e3eaea3SApple OSS Distributions #ifdef T_NAMESPACE
2*5e3eaea3SApple OSS Distributions #undef T_NAMESPACE
3*5e3eaea3SApple OSS Distributions #endif /* T_NAMESPACE */
4*5e3eaea3SApple OSS Distributions
5*5e3eaea3SApple OSS Distributions #include <Block.h>
6*5e3eaea3SApple OSS Distributions #include <darwintest.h>
7*5e3eaea3SApple OSS Distributions #include <dispatch/dispatch.h>
8*5e3eaea3SApple OSS Distributions #include <err.h>
9*5e3eaea3SApple OSS Distributions #include <fcntl.h>
10*5e3eaea3SApple OSS Distributions #include <limits.h>
11*5e3eaea3SApple OSS Distributions #include <signal.h>
12*5e3eaea3SApple OSS Distributions #include <stdbool.h>
13*5e3eaea3SApple OSS Distributions #include <stdlib.h>
14*5e3eaea3SApple OSS Distributions #include <stdint.h>
15*5e3eaea3SApple OSS Distributions #include <unistd.h>
16*5e3eaea3SApple OSS Distributions #include <util.h>
17*5e3eaea3SApple OSS Distributions
18*5e3eaea3SApple OSS Distributions T_GLOBAL_META(
19*5e3eaea3SApple OSS Distributions T_META_NAMESPACE("xnu.kevent"),
20*5e3eaea3SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
21*5e3eaea3SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("kevent"),
22*5e3eaea3SApple OSS Distributions T_META_CHECK_LEAKS(false),
23*5e3eaea3SApple OSS Distributions T_META_RUN_CONCURRENTLY(true));
24*5e3eaea3SApple OSS Distributions
25*5e3eaea3SApple OSS Distributions #define TIMEOUT_SECS 10
26*5e3eaea3SApple OSS Distributions
27*5e3eaea3SApple OSS Distributions static int child_ready[2];
28*5e3eaea3SApple OSS Distributions
29*5e3eaea3SApple OSS Distributions static void
child_tty_client(void)30*5e3eaea3SApple OSS Distributions child_tty_client(void)
31*5e3eaea3SApple OSS Distributions {
32*5e3eaea3SApple OSS Distributions dispatch_source_t src;
33*5e3eaea3SApple OSS Distributions char buf[16] = "";
34*5e3eaea3SApple OSS Distributions ssize_t bytes_wr;
35*5e3eaea3SApple OSS Distributions
36*5e3eaea3SApple OSS Distributions src = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ,
37*5e3eaea3SApple OSS Distributions (uintptr_t)STDIN_FILENO, 0, NULL);
38*5e3eaea3SApple OSS Distributions if (!src) {
39*5e3eaea3SApple OSS Distributions exit(1);
40*5e3eaea3SApple OSS Distributions }
41*5e3eaea3SApple OSS Distributions dispatch_source_set_event_handler(src, ^{});
42*5e3eaea3SApple OSS Distributions
43*5e3eaea3SApple OSS Distributions dispatch_activate(src);
44*5e3eaea3SApple OSS Distributions
45*5e3eaea3SApple OSS Distributions close(child_ready[0]);
46*5e3eaea3SApple OSS Distributions snprintf(buf, sizeof(buf), "%ds", getpid());
47*5e3eaea3SApple OSS Distributions bytes_wr = write(child_ready[1], buf, strlen(buf));
48*5e3eaea3SApple OSS Distributions if (bytes_wr < 0) {
49*5e3eaea3SApple OSS Distributions err(1, "failed to write on child ready pipe");
50*5e3eaea3SApple OSS Distributions }
51*5e3eaea3SApple OSS Distributions
52*5e3eaea3SApple OSS Distributions dispatch_main();
53*5e3eaea3SApple OSS Distributions }
54*5e3eaea3SApple OSS Distributions
55*5e3eaea3SApple OSS Distributions static void
pty_master(void)56*5e3eaea3SApple OSS Distributions pty_master(void)
57*5e3eaea3SApple OSS Distributions {
58*5e3eaea3SApple OSS Distributions pid_t child_pid;
59*5e3eaea3SApple OSS Distributions int ret;
60*5e3eaea3SApple OSS Distributions
61*5e3eaea3SApple OSS Distributions child_pid = fork();
62*5e3eaea3SApple OSS Distributions if (child_pid == 0) {
63*5e3eaea3SApple OSS Distributions child_tty_client();
64*5e3eaea3SApple OSS Distributions }
65*5e3eaea3SApple OSS Distributions ret = setpgid(child_pid, child_pid);
66*5e3eaea3SApple OSS Distributions if (ret < 0) {
67*5e3eaea3SApple OSS Distributions exit(1);
68*5e3eaea3SApple OSS Distributions }
69*5e3eaea3SApple OSS Distributions ret = tcsetpgrp(STDIN_FILENO, child_pid);
70*5e3eaea3SApple OSS Distributions if (ret < 0) {
71*5e3eaea3SApple OSS Distributions exit(1);
72*5e3eaea3SApple OSS Distributions }
73*5e3eaea3SApple OSS Distributions
74*5e3eaea3SApple OSS Distributions sleep(TIMEOUT_SECS);
75*5e3eaea3SApple OSS Distributions exit(1);
76*5e3eaea3SApple OSS Distributions }
77*5e3eaea3SApple OSS Distributions
78*5e3eaea3SApple OSS Distributions T_DECL(pty_master_teardown,
79*5e3eaea3SApple OSS Distributions "try removing a TTY master out from under a PTY slave holding a kevent",
80*5e3eaea3SApple OSS Distributions T_META_ASROOT(true))
81*5e3eaea3SApple OSS Distributions {
82*5e3eaea3SApple OSS Distributions __block pid_t master_pid;
83*5e3eaea3SApple OSS Distributions char buf[16] = "";
84*5e3eaea3SApple OSS Distributions char *end;
85*5e3eaea3SApple OSS Distributions ssize_t bytes_rd;
86*5e3eaea3SApple OSS Distributions size_t buf_len = 0;
87*5e3eaea3SApple OSS Distributions unsigned long slave_pid;
88*5e3eaea3SApple OSS Distributions int master_fd;
89*5e3eaea3SApple OSS Distributions char pty_filename[PATH_MAX];
90*5e3eaea3SApple OSS Distributions int status;
91*5e3eaea3SApple OSS Distributions
92*5e3eaea3SApple OSS Distributions T_SETUPBEGIN;
93*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(pipe(child_ready), NULL);
94*5e3eaea3SApple OSS Distributions
95*5e3eaea3SApple OSS Distributions master_pid = forkpty(&master_fd, pty_filename, NULL, NULL);
96*5e3eaea3SApple OSS Distributions if (master_pid == 0) {
97*5e3eaea3SApple OSS Distributions pty_master();
98*5e3eaea3SApple OSS Distributions __builtin_unreachable();
99*5e3eaea3SApple OSS Distributions }
100*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(master_pid,
101*5e3eaea3SApple OSS Distributions "forked child master PTY with pid %d, at pty %s", master_pid,
102*5e3eaea3SApple OSS Distributions pty_filename);
103*5e3eaea3SApple OSS Distributions
104*5e3eaea3SApple OSS Distributions close(child_ready[1]);
105*5e3eaea3SApple OSS Distributions
106*5e3eaea3SApple OSS Distributions end = buf;
107*5e3eaea3SApple OSS Distributions do {
108*5e3eaea3SApple OSS Distributions bytes_rd = read(child_ready[0], end, sizeof(buf) - buf_len);
109*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(bytes_rd, "read on pipe between master and runner");
110*5e3eaea3SApple OSS Distributions buf_len += (size_t)bytes_rd;
111*5e3eaea3SApple OSS Distributions T_LOG("runner read %zd bytes", bytes_rd);
112*5e3eaea3SApple OSS Distributions end += bytes_rd;
113*5e3eaea3SApple OSS Distributions } while (bytes_rd != 0 && *(end - 1) != 's');
114*5e3eaea3SApple OSS Distributions
115*5e3eaea3SApple OSS Distributions slave_pid = strtoul(buf, &end, 0);
116*5e3eaea3SApple OSS Distributions if (buf == end) {
117*5e3eaea3SApple OSS Distributions T_ASSERT_FAIL("could not parse child PID from master pipe");
118*5e3eaea3SApple OSS Distributions }
119*5e3eaea3SApple OSS Distributions
120*5e3eaea3SApple OSS Distributions T_LOG("got pid %lu for slave process from master", slave_pid);
121*5e3eaea3SApple OSS Distributions T_SETUPEND;
122*5e3eaea3SApple OSS Distributions
123*5e3eaea3SApple OSS Distributions T_LOG("sending fatal signal to master");
124*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(kill(master_pid, SIGKILL), NULL);
125*5e3eaea3SApple OSS Distributions
126*5e3eaea3SApple OSS Distributions T_LOG("sending fatal signal to slave");
127*5e3eaea3SApple OSS Distributions (void)kill((int)slave_pid, SIGKILL);
128*5e3eaea3SApple OSS Distributions
129*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid(master_pid, &status, 0), NULL);
130*5e3eaea3SApple OSS Distributions T_ASSERT_TRUE(WIFSIGNALED(status), "master PID was signaled");
131*5e3eaea3SApple OSS Distributions (void)waitpid((int)slave_pid, &status, 0);
132*5e3eaea3SApple OSS Distributions }
133*5e3eaea3SApple OSS Distributions
134*5e3eaea3SApple OSS Distributions volatile static bool writing = true;
135*5e3eaea3SApple OSS Distributions
136*5e3eaea3SApple OSS Distributions static void *
reader_thread(void * arg)137*5e3eaea3SApple OSS Distributions reader_thread(void *arg)
138*5e3eaea3SApple OSS Distributions {
139*5e3eaea3SApple OSS Distributions int fd = (int)arg;
140*5e3eaea3SApple OSS Distributions char c;
141*5e3eaea3SApple OSS Distributions
142*5e3eaea3SApple OSS Distributions T_SETUPBEGIN;
143*5e3eaea3SApple OSS Distributions T_QUIET;
144*5e3eaea3SApple OSS Distributions T_ASSERT_GT(fd, 0, "reader thread received valid fd");
145*5e3eaea3SApple OSS Distributions T_SETUPEND;
146*5e3eaea3SApple OSS Distributions
147*5e3eaea3SApple OSS Distributions for (;;) {
148*5e3eaea3SApple OSS Distributions ssize_t rdsize = read(fd, &c, sizeof(c));
149*5e3eaea3SApple OSS Distributions if (rdsize == -1) {
150*5e3eaea3SApple OSS Distributions if (errno == EINTR) {
151*5e3eaea3SApple OSS Distributions continue;
152*5e3eaea3SApple OSS Distributions } else if (errno == EBADF) {
153*5e3eaea3SApple OSS Distributions T_LOG("reader got an error (%s), shutting down",
154*5e3eaea3SApple OSS Distributions strerror(errno));
155*5e3eaea3SApple OSS Distributions return NULL;
156*5e3eaea3SApple OSS Distributions } else {
157*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(rdsize, "read on PTY");
158*5e3eaea3SApple OSS Distributions }
159*5e3eaea3SApple OSS Distributions } else if (rdsize == 0) {
160*5e3eaea3SApple OSS Distributions return NULL;
161*5e3eaea3SApple OSS Distributions }
162*5e3eaea3SApple OSS Distributions }
163*5e3eaea3SApple OSS Distributions
164*5e3eaea3SApple OSS Distributions return NULL;
165*5e3eaea3SApple OSS Distributions }
166*5e3eaea3SApple OSS Distributions
167*5e3eaea3SApple OSS Distributions static void *
writer_thread(void * arg)168*5e3eaea3SApple OSS Distributions writer_thread(void *arg)
169*5e3eaea3SApple OSS Distributions {
170*5e3eaea3SApple OSS Distributions int fd = (int)arg;
171*5e3eaea3SApple OSS Distributions char c[4096];
172*5e3eaea3SApple OSS Distributions memset(c, 'a', sizeof(c));
173*5e3eaea3SApple OSS Distributions
174*5e3eaea3SApple OSS Distributions T_SETUPBEGIN;
175*5e3eaea3SApple OSS Distributions T_QUIET;
176*5e3eaea3SApple OSS Distributions T_ASSERT_GT(fd, 0, "writer thread received valid fd");
177*5e3eaea3SApple OSS Distributions T_SETUPEND;
178*5e3eaea3SApple OSS Distributions
179*5e3eaea3SApple OSS Distributions while (writing) {
180*5e3eaea3SApple OSS Distributions ssize_t wrsize = write(fd, c, sizeof(c));
181*5e3eaea3SApple OSS Distributions if (wrsize == -1) {
182*5e3eaea3SApple OSS Distributions if (errno == EINTR) {
183*5e3eaea3SApple OSS Distributions continue;
184*5e3eaea3SApple OSS Distributions } else {
185*5e3eaea3SApple OSS Distributions T_LOG("writer got an error (%s), shutting down",
186*5e3eaea3SApple OSS Distributions strerror(errno));
187*5e3eaea3SApple OSS Distributions return NULL;
188*5e3eaea3SApple OSS Distributions }
189*5e3eaea3SApple OSS Distributions }
190*5e3eaea3SApple OSS Distributions }
191*5e3eaea3SApple OSS Distributions
192*5e3eaea3SApple OSS Distributions return NULL;
193*5e3eaea3SApple OSS Distributions }
194*5e3eaea3SApple OSS Distributions
195*5e3eaea3SApple OSS Distributions #define ATTACH_ITERATIONS 10000
196*5e3eaea3SApple OSS Distributions
197*5e3eaea3SApple OSS Distributions static int attach_master, attach_slave;
198*5e3eaea3SApple OSS Distributions static pthread_t reader, writer;
199*5e3eaea3SApple OSS Distributions
200*5e3eaea3SApple OSS Distributions static void
redispatch(dispatch_group_t grp,dispatch_source_type_t type,int fd)201*5e3eaea3SApple OSS Distributions redispatch(dispatch_group_t grp, dispatch_source_type_t type, int fd)
202*5e3eaea3SApple OSS Distributions {
203*5e3eaea3SApple OSS Distributions __block int iters = 0;
204*5e3eaea3SApple OSS Distributions
205*5e3eaea3SApple OSS Distributions __block void (^redispatch_blk)(void) = Block_copy(^{
206*5e3eaea3SApple OSS Distributions if (iters++ > ATTACH_ITERATIONS) {
207*5e3eaea3SApple OSS Distributions return;
208*5e3eaea3SApple OSS Distributions } else if (iters == ATTACH_ITERATIONS) {
209*5e3eaea3SApple OSS Distributions dispatch_group_leave(grp);
210*5e3eaea3SApple OSS Distributions T_PASS("created %d %s sources on busy PTY", iters,
211*5e3eaea3SApple OSS Distributions type == DISPATCH_SOURCE_TYPE_READ ? "read" : "write");
212*5e3eaea3SApple OSS Distributions }
213*5e3eaea3SApple OSS Distributions
214*5e3eaea3SApple OSS Distributions dispatch_source_t src = dispatch_source_create(
215*5e3eaea3SApple OSS Distributions type, (uintptr_t)fd, 0,
216*5e3eaea3SApple OSS Distributions dispatch_get_main_queue());
217*5e3eaea3SApple OSS Distributions
218*5e3eaea3SApple OSS Distributions dispatch_source_set_event_handler(src, ^{
219*5e3eaea3SApple OSS Distributions dispatch_cancel(src);
220*5e3eaea3SApple OSS Distributions });
221*5e3eaea3SApple OSS Distributions
222*5e3eaea3SApple OSS Distributions dispatch_source_set_cancel_handler(src, redispatch_blk);
223*5e3eaea3SApple OSS Distributions
224*5e3eaea3SApple OSS Distributions dispatch_activate(src);
225*5e3eaea3SApple OSS Distributions });
226*5e3eaea3SApple OSS Distributions
227*5e3eaea3SApple OSS Distributions dispatch_group_enter(grp);
228*5e3eaea3SApple OSS Distributions dispatch_async(dispatch_get_main_queue(), redispatch_blk);
229*5e3eaea3SApple OSS Distributions }
230*5e3eaea3SApple OSS Distributions
231*5e3eaea3SApple OSS Distributions T_DECL(attach_while_tty_wakeups,
232*5e3eaea3SApple OSS Distributions "try to attach knotes while a TTY is getting wakeups")
233*5e3eaea3SApple OSS Distributions {
234*5e3eaea3SApple OSS Distributions dispatch_group_t grp = dispatch_group_create();
235*5e3eaea3SApple OSS Distributions
236*5e3eaea3SApple OSS Distributions T_SETUPBEGIN;
237*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(openpty(&attach_master, &attach_slave, NULL, NULL,
238*5e3eaea3SApple OSS Distributions NULL), NULL);
239*5e3eaea3SApple OSS Distributions
240*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_create(&reader, NULL, reader_thread,
241*5e3eaea3SApple OSS Distributions (void *)(uintptr_t)attach_master), NULL);
242*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_create(&writer, NULL, writer_thread,
243*5e3eaea3SApple OSS Distributions (void *)(uintptr_t)attach_slave), NULL);
244*5e3eaea3SApple OSS Distributions T_SETUPEND;
245*5e3eaea3SApple OSS Distributions
246*5e3eaea3SApple OSS Distributions redispatch(grp, DISPATCH_SOURCE_TYPE_READ, attach_master);
247*5e3eaea3SApple OSS Distributions redispatch(grp, DISPATCH_SOURCE_TYPE_WRITE, attach_slave);
248*5e3eaea3SApple OSS Distributions
249*5e3eaea3SApple OSS Distributions dispatch_group_notify(grp, dispatch_get_main_queue(), ^{
250*5e3eaea3SApple OSS Distributions T_LOG("both reader and writer sources cleaned up");
251*5e3eaea3SApple OSS Distributions T_END;
252*5e3eaea3SApple OSS Distributions });
253*5e3eaea3SApple OSS Distributions
254*5e3eaea3SApple OSS Distributions dispatch_main();
255*5e3eaea3SApple OSS Distributions }
256*5e3eaea3SApple OSS Distributions
257*5e3eaea3SApple OSS Distributions T_DECL(master_read_data_set,
258*5e3eaea3SApple OSS Distributions "check that the data is set on read sources of master fds")
259*5e3eaea3SApple OSS Distributions {
260*5e3eaea3SApple OSS Distributions int master = -1, slave = -1;
261*5e3eaea3SApple OSS Distributions
262*5e3eaea3SApple OSS Distributions T_SETUPBEGIN;
263*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(openpty(&master, &slave, NULL, NULL, NULL), NULL);
264*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_GE(master, 0, "master fd is valid");
265*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_GE(slave, 0, "slave fd is valid");
266*5e3eaea3SApple OSS Distributions
267*5e3eaea3SApple OSS Distributions dispatch_source_t src = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ,
268*5e3eaea3SApple OSS Distributions (uintptr_t)master, 0, dispatch_get_main_queue());
269*5e3eaea3SApple OSS Distributions
270*5e3eaea3SApple OSS Distributions dispatch_source_set_event_handler(src, ^{
271*5e3eaea3SApple OSS Distributions unsigned long len = dispatch_source_get_data(src);
272*5e3eaea3SApple OSS Distributions T_EXPECT_GT(len, (unsigned long)0,
273*5e3eaea3SApple OSS Distributions "the amount of data to read was set for the master source");
274*5e3eaea3SApple OSS Distributions dispatch_cancel(src);
275*5e3eaea3SApple OSS Distributions });
276*5e3eaea3SApple OSS Distributions
277*5e3eaea3SApple OSS Distributions dispatch_source_set_cancel_handler(src, ^{
278*5e3eaea3SApple OSS Distributions dispatch_release(src);
279*5e3eaea3SApple OSS Distributions T_END;
280*5e3eaea3SApple OSS Distributions });
281*5e3eaea3SApple OSS Distributions
282*5e3eaea3SApple OSS Distributions dispatch_activate(src);
283*5e3eaea3SApple OSS Distributions T_SETUPEND;
284*5e3eaea3SApple OSS Distributions
285*5e3eaea3SApple OSS Distributions // Let's not fill up the TTY's buffer, otherwise write(2) will block.
286*5e3eaea3SApple OSS Distributions char buf[512] = "";
287*5e3eaea3SApple OSS Distributions
288*5e3eaea3SApple OSS Distributions int ret = 0;
289*5e3eaea3SApple OSS Distributions while ((ret = write(slave, buf, sizeof(buf)) == -1 && errno == EAGAIN)) {
290*5e3eaea3SApple OSS Distributions ;
291*5e3eaea3SApple OSS Distributions }
292*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "slave wrote data");
293*5e3eaea3SApple OSS Distributions
294*5e3eaea3SApple OSS Distributions dispatch_main();
295*5e3eaea3SApple OSS Distributions }
296