xref: /xnu-12377.81.4/tests/pty_121077498.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions #include <util.h>
2*043036a2SApple OSS Distributions #include <stdlib.h>
3*043036a2SApple OSS Distributions #include <fcntl.h>
4*043036a2SApple OSS Distributions #include <termios.h>
5*043036a2SApple OSS Distributions #include <signal.h>
6*043036a2SApple OSS Distributions #include <darwintest.h>
7*043036a2SApple OSS Distributions #include <darwintest_multiprocess.h>
8*043036a2SApple OSS Distributions 
9*043036a2SApple OSS Distributions #define TEST_TIMEOUT    10
10*043036a2SApple OSS Distributions 
11*043036a2SApple OSS Distributions T_GLOBAL_META(
12*043036a2SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(TRUE));
13*043036a2SApple OSS Distributions 
14*043036a2SApple OSS Distributions static void
sigio_handler(__unused int sig)15*043036a2SApple OSS Distributions sigio_handler(__unused int sig)
16*043036a2SApple OSS Distributions {
17*043036a2SApple OSS Distributions 	/* Do nothing; this should never actually be called. */
18*043036a2SApple OSS Distributions }
19*043036a2SApple OSS Distributions 
20*043036a2SApple OSS Distributions T_HELPER_DECL(pty_121077498_impl, "fork helper")
21*043036a2SApple OSS Distributions {
22*043036a2SApple OSS Distributions 	int primary, replica, flags;
23*043036a2SApple OSS Distributions 	char rname[128], c;
24*043036a2SApple OSS Distributions 	sigset_t sigio_set, received_set;
25*043036a2SApple OSS Distributions 
26*043036a2SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(sigemptyset(&sigio_set), "sigemptyset");
27*043036a2SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(sigaddset(&sigio_set, SIGIO), "sigaddset");
28*043036a2SApple OSS Distributions 
29*043036a2SApple OSS Distributions 	/*
30*043036a2SApple OSS Distributions 	 * New session, lose any existing controlling terminal and become
31*043036a2SApple OSS Distributions 	 * session leader.
32*043036a2SApple OSS Distributions 	 */
33*043036a2SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(setsid(), NULL);
34*043036a2SApple OSS Distributions 
35*043036a2SApple OSS Distributions 	/* Open the primary side, following `openpty`'s implementation. */
36*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(primary = posix_openpt(O_RDWR | O_NOCTTY), NULL);
37*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(grantpt(primary), NULL);
38*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(unlockpt(primary), NULL);
39*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ptsname_r(primary, rname, sizeof(rname)), NULL);
40*043036a2SApple OSS Distributions 
41*043036a2SApple OSS Distributions 	/* Enable both O_NONBLOCK and O_ASYNC before opening the replica. */
42*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(flags = fcntl(primary, F_GETFL, 0), NULL);
43*043036a2SApple OSS Distributions 	flags |= O_NONBLOCK | O_ASYNC;
44*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(flags = fcntl(primary, F_SETFL, flags), NULL);
45*043036a2SApple OSS Distributions 
46*043036a2SApple OSS Distributions 	/* Open the replica, making it our controlling terminal. */
47*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(replica = open(rname, O_RDWR, 0), "open %s", rname);
48*043036a2SApple OSS Distributions 
49*043036a2SApple OSS Distributions 	/* Verify that we are non-blocking. */
50*043036a2SApple OSS Distributions 	T_EXPECT_POSIX_FAILURE(read(primary, &c, 1), EAGAIN, "read with no data returns EAGAIN");
51*043036a2SApple OSS Distributions 
52*043036a2SApple OSS Distributions 	/* Set us up to detect when we get a SIGIO. */
53*043036a2SApple OSS Distributions 	T_EXPECT_TRUE(SIG_ERR != signal(SIGIO, sigio_handler), "set SIGIO handler");
54*043036a2SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(sigprocmask(SIG_BLOCK, &sigio_set, NULL), "block SIGIO");
55*043036a2SApple OSS Distributions 
56*043036a2SApple OSS Distributions 	/* Flush the replica, which should trigger a SIGIO. */
57*043036a2SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(tcflush(replica, TCIOFLUSH), NULL);
58*043036a2SApple OSS Distributions 
59*043036a2SApple OSS Distributions 	/* Verify that we got SIGIO. */
60*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sigpending(&received_set), NULL);
61*043036a2SApple OSS Distributions 	T_ASSERT_TRUE(sigismember(&received_set, SIGIO), "received SIGIO when we flushed");
62*043036a2SApple OSS Distributions 
63*043036a2SApple OSS Distributions 	/* Reset state. */
64*043036a2SApple OSS Distributions 	T_EXPECT_TRUE(SIG_ERR != signal(SIGIO, SIG_IGN), "reset SIGIO handler");
65*043036a2SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(sigprocmask(SIG_UNBLOCK, &sigio_set, NULL), "unblock SIGIO");
66*043036a2SApple OSS Distributions 
67*043036a2SApple OSS Distributions 	/* Close fds. */
68*043036a2SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(close(replica), NULL);
69*043036a2SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(close(primary), NULL);
70*043036a2SApple OSS Distributions }
71*043036a2SApple OSS Distributions 
72*043036a2SApple OSS Distributions T_DECL(pty_121077498, "Ability to use O_NONBLOCK and O_ASYNC on pty primary without replica open.")
73*043036a2SApple OSS Distributions {
74*043036a2SApple OSS Distributions 	/*
75*043036a2SApple OSS Distributions 	 * We need to do the test in a child process because we might have
76*043036a2SApple OSS Distributions 	 * getpgrp() == getpid().
77*043036a2SApple OSS Distributions 	 */
78*043036a2SApple OSS Distributions 	dt_helper_t helper = dt_child_helper("pty_121077498_impl");
79*043036a2SApple OSS Distributions 	dt_run_helpers(&helper, 1, TEST_TIMEOUT);
80*043036a2SApple OSS Distributions }
81