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