xref: /xnu-11417.121.6/tests/tty_hang.c (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
1*a1e26a70SApple OSS Distributions /*
2*a1e26a70SApple OSS Distributions  * Copyright (c) 2015-2018 Apple Inc. All rights reserved.
3*a1e26a70SApple OSS Distributions  *
4*a1e26a70SApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*a1e26a70SApple OSS Distributions  *
6*a1e26a70SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*a1e26a70SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*a1e26a70SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*a1e26a70SApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*a1e26a70SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*a1e26a70SApple OSS Distributions  * file.
12*a1e26a70SApple OSS Distributions  *
13*a1e26a70SApple OSS Distributions  * The Original Code and all software distributed under the License are
14*a1e26a70SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*a1e26a70SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*a1e26a70SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*a1e26a70SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*a1e26a70SApple OSS Distributions  * Please see the License for the specific language governing rights and
19*a1e26a70SApple OSS Distributions  * limitations under the License.
20*a1e26a70SApple OSS Distributions  *
21*a1e26a70SApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*a1e26a70SApple OSS Distributions  */
23*a1e26a70SApple OSS Distributions 
24*a1e26a70SApple OSS Distributions 
25*a1e26a70SApple OSS Distributions #include <unistd.h>
26*a1e26a70SApple OSS Distributions #include <sys/ioctl.h>
27*a1e26a70SApple OSS Distributions #include <sys/types.h>
28*a1e26a70SApple OSS Distributions #include <stdlib.h>
29*a1e26a70SApple OSS Distributions #include <stdio.h>
30*a1e26a70SApple OSS Distributions #include <string.h>
31*a1e26a70SApple OSS Distributions #include <util.h>
32*a1e26a70SApple OSS Distributions #include <syslog.h>
33*a1e26a70SApple OSS Distributions #include <termios.h>
34*a1e26a70SApple OSS Distributions #include <errno.h>
35*a1e26a70SApple OSS Distributions #include <fcntl.h>
36*a1e26a70SApple OSS Distributions #include <sys/socket.h>
37*a1e26a70SApple OSS Distributions #include <sys/stat.h>
38*a1e26a70SApple OSS Distributions 
39*a1e26a70SApple OSS Distributions #include <darwintest.h>
40*a1e26a70SApple OSS Distributions #include <darwintest_utils.h>
41*a1e26a70SApple OSS Distributions #include <darwintest_multiprocess.h>
42*a1e26a70SApple OSS Distributions 
43*a1e26a70SApple OSS Distributions #define TEST_TIMEOUT    10
44*a1e26a70SApple OSS Distributions 
45*a1e26a70SApple OSS Distributions /*
46*a1e26a70SApple OSS Distributions  * Receiving SIGTTIN (from the blocked read) is the passing condition, we just
47*a1e26a70SApple OSS Distributions  * catch it so that we don't get terminated when we receive this.
48*a1e26a70SApple OSS Distributions  */
49*a1e26a70SApple OSS Distributions void
handle_sigttin(int signal)50*a1e26a70SApple OSS Distributions handle_sigttin(int signal)
51*a1e26a70SApple OSS Distributions {
52*a1e26a70SApple OSS Distributions 	return;
53*a1e26a70SApple OSS Distributions }
54*a1e26a70SApple OSS Distributions 
55*a1e26a70SApple OSS Distributions /*
56*a1e26a70SApple OSS Distributions  * Because of the way dt_fork_helpers work, we have to ensure any children
57*a1e26a70SApple OSS Distributions  * created by this function calls exit instead of getting the fork handlers exit
58*a1e26a70SApple OSS Distributions  * handling
59*a1e26a70SApple OSS Distributions  */
60*a1e26a70SApple OSS Distributions int
get_new_session_and_terminal_and_fork_child_to_read(char * pty_name)61*a1e26a70SApple OSS Distributions get_new_session_and_terminal_and_fork_child_to_read(char *pty_name)
62*a1e26a70SApple OSS Distributions {
63*a1e26a70SApple OSS Distributions 	int sock_fd[2];
64*a1e26a70SApple OSS Distributions 	int pty_fd;
65*a1e26a70SApple OSS Distributions 	pid_t pid;
66*a1e26a70SApple OSS Distributions 	char buf[10];
67*a1e26a70SApple OSS Distributions 
68*a1e26a70SApple OSS Distributions 	/*
69*a1e26a70SApple OSS Distributions 	 * We use this to handshake certain actions between this process and its
70*a1e26a70SApple OSS Distributions 	 * child.
71*a1e26a70SApple OSS Distributions 	 */
72*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(socketpair(AF_UNIX, SOCK_STREAM, 0, sock_fd),
73*a1e26a70SApple OSS Distributions 	    NULL);
74*a1e26a70SApple OSS Distributions 
75*a1e26a70SApple OSS Distributions 	/*
76*a1e26a70SApple OSS Distributions 	 * New session, lose any existing controlling terminal and become
77*a1e26a70SApple OSS Distributions 	 * session leader.
78*a1e26a70SApple OSS Distributions 	 */
79*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setsid(), NULL);
80*a1e26a70SApple OSS Distributions 
81*a1e26a70SApple OSS Distributions 	/* now open pty, become controlling terminal of new session */
82*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(pty_fd = open(pty_name, O_RDWR), NULL);
83*a1e26a70SApple OSS Distributions 
84*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(pid = fork(), NULL);
85*a1e26a70SApple OSS Distributions 
86*a1e26a70SApple OSS Distributions 	if (pid == 0) { /* child */
87*a1e26a70SApple OSS Distributions 		int pty_fd_child;
88*a1e26a70SApple OSS Distributions 		char buf[10];
89*a1e26a70SApple OSS Distributions 
90*a1e26a70SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(close(sock_fd[0]), NULL);
91*a1e26a70SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(close(pty_fd), NULL);
92*a1e26a70SApple OSS Distributions 
93*a1e26a70SApple OSS Distributions 		/* Make a new process group for ourselves */
94*a1e26a70SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(setpgid(0, 0), NULL);
95*a1e26a70SApple OSS Distributions 
96*a1e26a70SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(pty_fd_child = open(pty_name, O_RDWR),
97*a1e26a70SApple OSS Distributions 		    NULL);
98*a1e26a70SApple OSS Distributions 
99*a1e26a70SApple OSS Distributions 		/* now let parent know we've done open and setpgid */
100*a1e26a70SApple OSS Distributions 		write(sock_fd[1], "done", sizeof("done"));
101*a1e26a70SApple OSS Distributions 
102*a1e26a70SApple OSS Distributions 		/* wait for parent to set us to the foreground process group */
103*a1e26a70SApple OSS Distributions 		read(sock_fd[1], buf, sizeof(buf));
104*a1e26a70SApple OSS Distributions 
105*a1e26a70SApple OSS Distributions 		/*
106*a1e26a70SApple OSS Distributions 		 * We are the foreground process group now so we can read
107*a1e26a70SApple OSS Distributions 		 * without getting a SIGTTIN.
108*a1e26a70SApple OSS Distributions 		 *
109*a1e26a70SApple OSS Distributions 		 * Once we are blocked though (we have a crude 1 second sleep on
110*a1e26a70SApple OSS Distributions 		 * the parent to "detect" this), our parent is going to change
111*a1e26a70SApple OSS Distributions 		 * us to be in the background.
112*a1e26a70SApple OSS Distributions 		 *
113*a1e26a70SApple OSS Distributions 		 * We'll be blocked until we get a signal and if that is signal
114*a1e26a70SApple OSS Distributions 		 * is SIGTTIN, then the test has passed otherwise the test has
115*a1e26a70SApple OSS Distributions 		 * failed.
116*a1e26a70SApple OSS Distributions 		 */
117*a1e26a70SApple OSS Distributions 		signal(SIGTTIN, handle_sigttin);
118*a1e26a70SApple OSS Distributions 		(void)read(pty_fd_child, buf, sizeof(buf));
119*a1e26a70SApple OSS Distributions 		/*
120*a1e26a70SApple OSS Distributions 		 * If we get here, we passed, if we get any other signal than
121*a1e26a70SApple OSS Distributions 		 * SIGTTIN, we will not reach here.
122*a1e26a70SApple OSS Distributions 		 */
123*a1e26a70SApple OSS Distributions 		exit(0);
124*a1e26a70SApple OSS Distributions 	}
125*a1e26a70SApple OSS Distributions 
126*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(close(sock_fd[1]), NULL);
127*a1e26a70SApple OSS Distributions 
128*a1e26a70SApple OSS Distributions 	/* wait for child to open slave side and set its pgid to its pid */
129*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(read(sock_fd[0], buf, sizeof(buf)), NULL);
130*a1e26a70SApple OSS Distributions 
131*a1e26a70SApple OSS Distributions 	/*
132*a1e26a70SApple OSS Distributions 	 * We need this to happen and in the order shown
133*a1e26a70SApple OSS Distributions 	 *
134*a1e26a70SApple OSS Distributions 	 * parent (pgid = pid)                  child (child_pgid = child_pid)
135*a1e26a70SApple OSS Distributions 	 *
136*a1e26a70SApple OSS Distributions 	 * 1 - tcsetpgrp(child_pgid)
137*a1e26a70SApple OSS Distributions 	 * 2 -                                      block in read()
138*a1e26a70SApple OSS Distributions 	 * 3 - tcsetpgrp(pgid)
139*a1e26a70SApple OSS Distributions 	 *
140*a1e26a70SApple OSS Distributions 	 * making sure 2 happens after 1 is easy, we use a sleep(1) in the
141*a1e26a70SApple OSS Distributions 	 * parent to try and ensure 3 happens after 2.
142*a1e26a70SApple OSS Distributions 	 */
143*a1e26a70SApple OSS Distributions 
144*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(tcsetpgrp(pty_fd, pid), NULL);
145*a1e26a70SApple OSS Distributions 
146*a1e26a70SApple OSS Distributions 	/* let child know you have set it to be the foreground process group */
147*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(write(sock_fd[0], "done", sizeof("done")), NULL);
148*a1e26a70SApple OSS Distributions 
149*a1e26a70SApple OSS Distributions 	/*
150*a1e26a70SApple OSS Distributions 	 * give it a second to do the read of the terminal in response.
151*a1e26a70SApple OSS Distributions 	 *
152*a1e26a70SApple OSS Distributions 	 * XXX : Find a way to detect that the child is blocked in read(2).
153*a1e26a70SApple OSS Distributions 	 */
154*a1e26a70SApple OSS Distributions 	sleep(1);
155*a1e26a70SApple OSS Distributions 
156*a1e26a70SApple OSS Distributions 	/*
157*a1e26a70SApple OSS Distributions 	 * now change the foreground process group to ourselves -
158*a1e26a70SApple OSS Distributions 	 * Note we are now in the background process group and we need to ignore
159*a1e26a70SApple OSS Distributions 	 * SIGTTOU for this call to succeed.
160*a1e26a70SApple OSS Distributions 	 *
161*a1e26a70SApple OSS Distributions 	 * Hopefully the child has gotten to run and blocked for read on the
162*a1e26a70SApple OSS Distributions 	 * terminal in the 1 second we slept.
163*a1e26a70SApple OSS Distributions 	 */
164*a1e26a70SApple OSS Distributions 	signal(SIGTTOU, SIG_IGN);
165*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(tcsetpgrp(pty_fd, getpid()), NULL);
166*a1e26a70SApple OSS Distributions 
167*a1e26a70SApple OSS Distributions 	return 0;
168*a1e26a70SApple OSS Distributions }
169*a1e26a70SApple OSS Distributions 
170*a1e26a70SApple OSS Distributions /*
171*a1e26a70SApple OSS Distributions  * We're running in a "fork helper", we can't do a waitpid on the child because
172*a1e26a70SApple OSS Distributions  * the fork helper unhelpfully hides the pid of the child and in it kills itself.
173*a1e26a70SApple OSS Distributions  * We will instead fork first and wait on the child. If it is
174*a1e26a70SApple OSS Distributions  * able to emerge from the read of the terminal, the test passes and if it
175*a1e26a70SApple OSS Distributions  * doesn't, the test fails.
176*a1e26a70SApple OSS Distributions  * Since the test is testing for a deadlock in proc_exit of the child (caused
177*a1e26a70SApple OSS Distributions  * by a background read in the "grandchild".
178*a1e26a70SApple OSS Distributions  */
179*a1e26a70SApple OSS Distributions void
run_test(int do_revoke)180*a1e26a70SApple OSS Distributions run_test(int do_revoke)
181*a1e26a70SApple OSS Distributions {
182*a1e26a70SApple OSS Distributions 	int master_fd;
183*a1e26a70SApple OSS Distributions 	char *slave_pty;
184*a1e26a70SApple OSS Distributions 	pid_t pid;
185*a1e26a70SApple OSS Distributions 
186*a1e26a70SApple OSS Distributions 	T_WITH_ERRNO;
187*a1e26a70SApple OSS Distributions 	T_QUIET;
188*a1e26a70SApple OSS Distributions 
189*a1e26a70SApple OSS Distributions 	T_SETUPBEGIN;
190*a1e26a70SApple OSS Distributions 
191*a1e26a70SApple OSS Distributions 	slave_pty = NULL;
192*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(master_fd = posix_openpt(O_RDWR | O_NOCTTY),
193*a1e26a70SApple OSS Distributions 	    NULL);
194*a1e26a70SApple OSS Distributions 	(void)fcntl(master_fd, F_SETFL, O_NONBLOCK);
195*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(grantpt(master_fd), NULL);
196*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(unlockpt(master_fd), NULL);
197*a1e26a70SApple OSS Distributions 	slave_pty = ptsname(master_fd);
198*a1e26a70SApple OSS Distributions 	T_ASSERT_NOTNULL(slave_pty, NULL);
199*a1e26a70SApple OSS Distributions 	T_LOG("slave pty is %s\n", slave_pty);
200*a1e26a70SApple OSS Distributions 
201*a1e26a70SApple OSS Distributions 	T_SETUPEND;
202*a1e26a70SApple OSS Distributions 
203*a1e26a70SApple OSS Distributions 	/*
204*a1e26a70SApple OSS Distributions 	 * We get the stdin and stdout redirection but we don't have visibility
205*a1e26a70SApple OSS Distributions 	 * into the child (nor can we wait for it). To get around that, we fork
206*a1e26a70SApple OSS Distributions 	 * and only let the parent to the caller and the child exits before
207*a1e26a70SApple OSS Distributions 	 * returning to the caller.
208*a1e26a70SApple OSS Distributions 	 */
209*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(pid = fork(), NULL);
210*a1e26a70SApple OSS Distributions 
211*a1e26a70SApple OSS Distributions 	if (pid == 0) { /* child */
212*a1e26a70SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(close(master_fd), NULL);
213*a1e26a70SApple OSS Distributions 		get_new_session_and_terminal_and_fork_child_to_read(slave_pty);
214*a1e26a70SApple OSS Distributions 
215*a1e26a70SApple OSS Distributions 		/*
216*a1e26a70SApple OSS Distributions 		 * These tests are for testing revoke and read hangs. This
217*a1e26a70SApple OSS Distributions 		 * revoke can be explicit by a revoke(2) system call (test 2)
218*a1e26a70SApple OSS Distributions 		 * or as part of exit(2) of the session leader (test 1).
219*a1e26a70SApple OSS Distributions 		 * The exit hang is the common hang and can be fixed
220*a1e26a70SApple OSS Distributions 		 * independently but fixing the revoke(2) hang requires us make
221*a1e26a70SApple OSS Distributions 		 * changes in the tcsetpgrp path ( which also fixes the exit
222*a1e26a70SApple OSS Distributions 		 * hang). In essence, we have 2 fixes. One which only addresses
223*a1e26a70SApple OSS Distributions 		 * the exit hang and one which fixes both.
224*a1e26a70SApple OSS Distributions 		 */
225*a1e26a70SApple OSS Distributions 		if (do_revoke) {
226*a1e26a70SApple OSS Distributions 			/* This should not hang for the test to pass .. */
227*a1e26a70SApple OSS Distributions 			T_ASSERT_POSIX_SUCCESS(revoke(slave_pty), NULL);
228*a1e26a70SApple OSS Distributions 		}
229*a1e26a70SApple OSS Distributions 		/*
230*a1e26a70SApple OSS Distributions 		 * This child has the same dt_helper variables as its parent
231*a1e26a70SApple OSS Distributions 		 * The way dt_fork_helpers work if we don't exit() from here,
232*a1e26a70SApple OSS Distributions 		 * we will be killing the parent. So we have to exit() and not
233*a1e26a70SApple OSS Distributions 		 * let the dt_fork_helpers continue.
234*a1e26a70SApple OSS Distributions 		 * If we didn't do the revoke(2), This test passes if this exit
235*a1e26a70SApple OSS Distributions 		 * doesn't hang waiting for its child to finish reading.
236*a1e26a70SApple OSS Distributions 		 */
237*a1e26a70SApple OSS Distributions 		exit(0);
238*a1e26a70SApple OSS Distributions 	}
239*a1e26a70SApple OSS Distributions 
240*a1e26a70SApple OSS Distributions 	int status;
241*a1e26a70SApple OSS Distributions 	int sig;
242*a1e26a70SApple OSS Distributions 
243*a1e26a70SApple OSS Distributions 	dt_waitpid(pid, &status, &sig, 0);
244*a1e26a70SApple OSS Distributions 	if (sig) {
245*a1e26a70SApple OSS Distributions 		T_FAIL("Test failed because child received signal %s\n",
246*a1e26a70SApple OSS Distributions 		    strsignal(sig));
247*a1e26a70SApple OSS Distributions 	} else if (status) {
248*a1e26a70SApple OSS Distributions 		T_FAIL("Test failed because child exited with status %d\n",
249*a1e26a70SApple OSS Distributions 		    status);
250*a1e26a70SApple OSS Distributions 	} else {
251*a1e26a70SApple OSS Distributions 		T_PASS("test_passed\n");
252*a1e26a70SApple OSS Distributions 	}
253*a1e26a70SApple OSS Distributions 	/*
254*a1e26a70SApple OSS Distributions 	 * we can let this process proceed with the regular darwintest process
255*a1e26a70SApple OSS Distributions 	 * termination and cleanup.
256*a1e26a70SApple OSS Distributions 	 */
257*a1e26a70SApple OSS Distributions }
258*a1e26a70SApple OSS Distributions 
259*a1e26a70SApple OSS Distributions 
260*a1e26a70SApple OSS Distributions /*************************** TEST 1 ********************************/
261*a1e26a70SApple OSS Distributions T_HELPER_DECL(create_new_session_and_exit, "create_new_session_and_exit") {
262*a1e26a70SApple OSS Distributions 	run_test(0);
263*a1e26a70SApple OSS Distributions }
264*a1e26a70SApple OSS Distributions 
265*a1e26a70SApple OSS Distributions T_DECL(tty_exit_bgread_hang_test, "test for background read hang on ttys with proc exit", T_META_TAG_VM_PREFERRED)
266*a1e26a70SApple OSS Distributions {
267*a1e26a70SApple OSS Distributions 	dt_helper_t helpers[1];
268*a1e26a70SApple OSS Distributions 
269*a1e26a70SApple OSS Distributions 	helpers[0] = dt_fork_helper("create_new_session_and_exit");
270*a1e26a70SApple OSS Distributions 	dt_run_helpers(helpers, 1, TEST_TIMEOUT);
271*a1e26a70SApple OSS Distributions }
272*a1e26a70SApple OSS Distributions /***********************  END TEST 1  ********************************/
273*a1e26a70SApple OSS Distributions 
274*a1e26a70SApple OSS Distributions /************************** TEST 2 ***********************************/
275*a1e26a70SApple OSS Distributions T_HELPER_DECL(create_new_session_and_revoke_terminal, "create_new_session_and_revoke_terminal") {
276*a1e26a70SApple OSS Distributions 	run_test(1);
277*a1e26a70SApple OSS Distributions }
278*a1e26a70SApple OSS Distributions 
279*a1e26a70SApple OSS Distributions T_DECL(tty_revoke_bgread_hang_test, "test for background read hang on ttys with revoke", T_META_TAG_VM_PREFERRED)
280*a1e26a70SApple OSS Distributions {
281*a1e26a70SApple OSS Distributions 	dt_helper_t helpers[1];
282*a1e26a70SApple OSS Distributions 
283*a1e26a70SApple OSS Distributions 	helpers[0] = dt_fork_helper("create_new_session_and_revoke_terminal");
284*a1e26a70SApple OSS Distributions 	dt_run_helpers(helpers, 1, TEST_TIMEOUT);
285*a1e26a70SApple OSS Distributions }
286*a1e26a70SApple OSS Distributions /***********************  END TEST 2 *********************************/
287