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