1 /*
2 * Copyright (c) 2024 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdlib.h>
27 #include <sys/ioctl.h>
28 #include <unistd.h>
29
30 #include <darwintest.h>
31
32
33 T_GLOBAL_META(
34 T_META_NAMESPACE("xnu.tty"),
35 T_META_RADAR_COMPONENT_NAME("xnu"),
36 T_META_RADAR_COMPONENT_VERSION("file descriptors"),
37 T_META_OWNER("souvik_b"),
38 T_META_RUN_CONCURRENTLY(true));
39
40 static void
tty_ioctl_tioccons(bool privileged)41 tty_ioctl_tioccons(bool privileged)
42 {
43 int primary;
44 const char *name;
45 int replica;
46 int on = 1;
47 int off = 0;
48
49 // open primary tty
50 T_ASSERT_POSIX_SUCCESS(primary = posix_openpt(O_RDWR | O_NOCTTY), "open primary");
51
52 // allow opening a replica from the primary
53 T_ASSERT_POSIX_SUCCESS(grantpt(primary), "grantpt");
54 T_ASSERT_POSIX_SUCCESS(unlockpt(primary), "unlockpt");
55
56 // get the name of the primary tty
57 T_ASSERT_NOTNULL((name = ptsname(primary)), "ptsname");
58
59 // open the replica
60 T_ASSERT_POSIX_SUCCESS(replica = open(name, O_RDWR | O_NOCTTY), "open replica");
61
62 // try calling the TIOCCONS ioctl
63 if (privileged) {
64 T_ASSERT_POSIX_SUCCESS(ioctl(primary, TIOCCONS, (char *)&on), "ioctl TIOCCONS on");
65 } else {
66 T_ASSERT_POSIX_ERROR(ioctl(primary, TIOCCONS, (char *)&on), -EPERM, "ioctl TIOCCONS on");
67 }
68 T_ASSERT_POSIX_SUCCESS(ioctl(primary, TIOCCONS, (char *)&off), "ioctl TIOCCONS off");
69
70 // close primary and replica
71 T_ASSERT_POSIX_SUCCESS(close(primary), "close primary");
72 T_ASSERT_POSIX_SUCCESS(close(replica), "close replica");
73 }
74
75 T_DECL(tty_ioctl_tioccons_privileged,
76 "call the TIOCCONS ioctl as root",
77 T_META_ASROOT(true),
78 T_META_TAG_VM_PREFERRED)
79 {
80 tty_ioctl_tioccons(true);
81 }
82
83
84 T_DECL(tty_ioctl_tioccons_unprivileged,
85 "call the TIOCCONS ioctl without root",
86 T_META_ASROOT(false),
87 T_META_TAG_VM_PREFERRED)
88 {
89 tty_ioctl_tioccons(false);
90 }
91