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