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