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