1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions * Copyright (c) 2024 Apple Inc. All rights reserved.
3*bbb1b6f9SApple OSS Distributions *
4*bbb1b6f9SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions *
6*bbb1b6f9SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions *
15*bbb1b6f9SApple OSS Distributions * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions *
18*bbb1b6f9SApple OSS Distributions * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions * limitations under the License.
25*bbb1b6f9SApple OSS Distributions *
26*bbb1b6f9SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions */
28*bbb1b6f9SApple OSS Distributions
29*bbb1b6f9SApple OSS Distributions /* test that the header doesn't implicitly depend on others */
30*bbb1b6f9SApple OSS Distributions #include <sys/resource_private.h>
31*bbb1b6f9SApple OSS Distributions #include <sys/resource.h>
32*bbb1b6f9SApple OSS Distributions
33*bbb1b6f9SApple OSS Distributions #include <mach/coalition.h>
34*bbb1b6f9SApple OSS Distributions #include <sys/coalition.h>
35*bbb1b6f9SApple OSS Distributions #include <libproc.h>
36*bbb1b6f9SApple OSS Distributions
37*bbb1b6f9SApple OSS Distributions #include <sys/types.h>
38*bbb1b6f9SApple OSS Distributions #include <unistd.h>
39*bbb1b6f9SApple OSS Distributions
40*bbb1b6f9SApple OSS Distributions #include <darwintest.h>
41*bbb1b6f9SApple OSS Distributions #include <darwintest_utils.h>
42*bbb1b6f9SApple OSS Distributions
43*bbb1b6f9SApple OSS Distributions /* TODO: can this come from the right header? */
44*bbb1b6f9SApple OSS Distributions #define THREAD_GROUP_FLAGS_CARPLAY_MODE 0x1000
45*bbb1b6f9SApple OSS Distributions
46*bbb1b6f9SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.scheduler"),
47*bbb1b6f9SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
48*bbb1b6f9SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("scheduler"),
49*bbb1b6f9SApple OSS Distributions T_META_OWNER("chimene"),
50*bbb1b6f9SApple OSS Distributions T_META_RUN_CONCURRENTLY(false));
51*bbb1b6f9SApple OSS Distributions
52*bbb1b6f9SApple OSS Distributions static void
check_carplay_mode(bool expected_mode)53*bbb1b6f9SApple OSS Distributions check_carplay_mode(bool expected_mode)
54*bbb1b6f9SApple OSS Distributions {
55*bbb1b6f9SApple OSS Distributions int mode = getpriority(PRIO_DARWIN_CARPLAY_MODE, 0);
56*bbb1b6f9SApple OSS Distributions
57*bbb1b6f9SApple OSS Distributions T_QUIET;
58*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(mode, "getpriority(PRIO_DARWIN_CARPLAY_MODE)");
59*bbb1b6f9SApple OSS Distributions
60*bbb1b6f9SApple OSS Distributions T_LOG("pid %d: carplay mode is: %d", getpid(), mode);
61*bbb1b6f9SApple OSS Distributions
62*bbb1b6f9SApple OSS Distributions if (expected_mode) {
63*bbb1b6f9SApple OSS Distributions T_QUIET;
64*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ(mode, PRIO_DARWIN_CARPLAY_MODE_ON, "should be on");
65*bbb1b6f9SApple OSS Distributions } else {
66*bbb1b6f9SApple OSS Distributions T_QUIET;
67*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ(mode, PRIO_DARWIN_CARPLAY_MODE_OFF, "should be off");
68*bbb1b6f9SApple OSS Distributions }
69*bbb1b6f9SApple OSS Distributions }
70*bbb1b6f9SApple OSS Distributions
71*bbb1b6f9SApple OSS Distributions T_DECL(entitled_carplay_mode, "carplay mode bit should be settable while entitled")
72*bbb1b6f9SApple OSS Distributions {
73*bbb1b6f9SApple OSS Distributions T_LOG("uid: %d", getuid());
74*bbb1b6f9SApple OSS Distributions
75*bbb1b6f9SApple OSS Distributions check_carplay_mode(false);
76*bbb1b6f9SApple OSS Distributions
77*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_CARPLAY_MODE, 0, PRIO_DARWIN_CARPLAY_MODE_ON),
78*bbb1b6f9SApple OSS Distributions "setpriority(PRIO_DARWIN_CARPLAY_MODE, 0, PRIO_DARWIN_CARPLAY_MODE_ON)");
79*bbb1b6f9SApple OSS Distributions
80*bbb1b6f9SApple OSS Distributions check_carplay_mode(true);
81*bbb1b6f9SApple OSS Distributions
82*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_CARPLAY_MODE, 0, PRIO_DARWIN_CARPLAY_MODE_OFF),
83*bbb1b6f9SApple OSS Distributions "setpriority(PRIO_DARWIN_CARPLAY_MODE, 0, PRIO_DARWIN_CARPLAY_MODE_OFF)");
84*bbb1b6f9SApple OSS Distributions
85*bbb1b6f9SApple OSS Distributions check_carplay_mode(false);
86*bbb1b6f9SApple OSS Distributions }
87*bbb1b6f9SApple OSS Distributions
88*bbb1b6f9SApple OSS Distributions T_DECL(entitled_carplay_mode_read_root, "carplay mode bit should be readable as root",
89*bbb1b6f9SApple OSS Distributions T_META_ASROOT(true))
90*bbb1b6f9SApple OSS Distributions {
91*bbb1b6f9SApple OSS Distributions T_LOG("uid: %d", getuid());
92*bbb1b6f9SApple OSS Distributions
93*bbb1b6f9SApple OSS Distributions check_carplay_mode(false);
94*bbb1b6f9SApple OSS Distributions }
95*bbb1b6f9SApple OSS Distributions
96*bbb1b6f9SApple OSS Distributions T_DECL(entitled_carplay_mode_read_notroot, "carplay mode bit should be readable as not root but entitled",
97*bbb1b6f9SApple OSS Distributions T_META_ASROOT(false))
98*bbb1b6f9SApple OSS Distributions {
99*bbb1b6f9SApple OSS Distributions T_LOG("uid: %d", getuid());
100*bbb1b6f9SApple OSS Distributions
101*bbb1b6f9SApple OSS Distributions check_carplay_mode(false);
102*bbb1b6f9SApple OSS Distributions }
103*bbb1b6f9SApple OSS Distributions
104*bbb1b6f9SApple OSS Distributions static struct coalinfo_debuginfo
get_coal_debuginfo(uint64_t coal_id,char * prefix)105*bbb1b6f9SApple OSS Distributions get_coal_debuginfo(uint64_t coal_id, char* prefix)
106*bbb1b6f9SApple OSS Distributions {
107*bbb1b6f9SApple OSS Distributions struct coalinfo_debuginfo coaldebuginfo = {};
108*bbb1b6f9SApple OSS Distributions
109*bbb1b6f9SApple OSS Distributions int ret = coalition_info_debug_info(coal_id, &coaldebuginfo, sizeof(coaldebuginfo));
110*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "coalition_info_debug_info");
111*bbb1b6f9SApple OSS Distributions
112*bbb1b6f9SApple OSS Distributions T_LOG("coal(%s): %lld, carplay count %d, flags 0x%x (group 0x%llx, rec %d, focal: %d, nonfocal %d)",
113*bbb1b6f9SApple OSS Distributions prefix, coal_id,
114*bbb1b6f9SApple OSS Distributions coaldebuginfo.carplay_task_count, coaldebuginfo.thread_group_flags,
115*bbb1b6f9SApple OSS Distributions coaldebuginfo.thread_group_id, coaldebuginfo.thread_group_recommendation,
116*bbb1b6f9SApple OSS Distributions coaldebuginfo.focal_task_count, coaldebuginfo.nonfocal_task_count);
117*bbb1b6f9SApple OSS Distributions
118*bbb1b6f9SApple OSS Distributions return coaldebuginfo;
119*bbb1b6f9SApple OSS Distributions }
120*bbb1b6f9SApple OSS Distributions
121*bbb1b6f9SApple OSS Distributions static void
check_carplay_mode_count(uint32_t expected_count)122*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(uint32_t expected_count)
123*bbb1b6f9SApple OSS Distributions {
124*bbb1b6f9SApple OSS Distributions struct proc_pidcoalitioninfo idinfo = {};
125*bbb1b6f9SApple OSS Distributions
126*bbb1b6f9SApple OSS Distributions int ret = proc_pidinfo(getpid(), PROC_PIDCOALITIONINFO, 0,
127*bbb1b6f9SApple OSS Distributions &idinfo, sizeof(idinfo));
128*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "proc_pidinfo(... PROC_PIDCOALITIONINFO ...)");
129*bbb1b6f9SApple OSS Distributions
130*bbb1b6f9SApple OSS Distributions uint64_t res_id = idinfo.coalition_id[COALITION_TYPE_RESOURCE];
131*bbb1b6f9SApple OSS Distributions uint64_t jet_id = idinfo.coalition_id[COALITION_TYPE_JETSAM];
132*bbb1b6f9SApple OSS Distributions
133*bbb1b6f9SApple OSS Distributions struct coalinfo_debuginfo coaldebuginfo_res = get_coal_debuginfo(res_id, "res");
134*bbb1b6f9SApple OSS Distributions struct coalinfo_debuginfo coaldebuginfo_jet = get_coal_debuginfo(jet_id, "jet");
135*bbb1b6f9SApple OSS Distributions
136*bbb1b6f9SApple OSS Distributions if (expected_count) {
137*bbb1b6f9SApple OSS Distributions // see COALITION_FOCAL_TASKS_ACCOUNTING for this difference
138*bbb1b6f9SApple OSS Distributions #if TARGET_OS_OSX
139*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ(coaldebuginfo_res.carplay_task_count, expected_count, "should have carplay in res coalition");
140*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_EQ(coaldebuginfo_jet.carplay_task_count, 0, "should not have carplay in jet coalition");
141*bbb1b6f9SApple OSS Distributions #else
142*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_EQ(coaldebuginfo_res.carplay_task_count, 0, "should not have carplay in res coalition");
143*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ(coaldebuginfo_jet.carplay_task_count, expected_count, "should have carplay in jet coalition");
144*bbb1b6f9SApple OSS Distributions #endif
145*bbb1b6f9SApple OSS Distributions T_ASSERT_BITS_SET(coaldebuginfo_jet.thread_group_flags, THREAD_GROUP_FLAGS_CARPLAY_MODE,
146*bbb1b6f9SApple OSS Distributions "should have carplay mode flag in jet coalition"); \
147*bbb1b6f9SApple OSS Distributions } else {
148*bbb1b6f9SApple OSS Distributions #if TARGET_OS_OSX
149*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ(coaldebuginfo_res.carplay_task_count, 0, "should not have carplay in res coalition");
150*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_EQ(coaldebuginfo_jet.carplay_task_count, 0, "should not have carplay in jet coalition");
151*bbb1b6f9SApple OSS Distributions #else
152*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_EQ(coaldebuginfo_res.carplay_task_count, 0, "should not have carplay in res coalition");
153*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ(coaldebuginfo_jet.carplay_task_count, 0, "should not have carplay in jet coalition");
154*bbb1b6f9SApple OSS Distributions #endif
155*bbb1b6f9SApple OSS Distributions T_ASSERT_BITS_NOTSET(coaldebuginfo_jet.thread_group_flags, THREAD_GROUP_FLAGS_CARPLAY_MODE,
156*bbb1b6f9SApple OSS Distributions "should not have carplay mode flag in jet coalition"); \
157*bbb1b6f9SApple OSS Distributions }
158*bbb1b6f9SApple OSS Distributions
159*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_BITS_NOTSET(coaldebuginfo_res.thread_group_flags, THREAD_GROUP_FLAGS_CARPLAY_MODE,
160*bbb1b6f9SApple OSS Distributions "should never have carplay mode flag in res coalition"); \
161*bbb1b6f9SApple OSS Distributions }
162*bbb1b6f9SApple OSS Distributions
163*bbb1b6f9SApple OSS Distributions static void
skip_if_unsupported(void)164*bbb1b6f9SApple OSS Distributions skip_if_unsupported(void)
165*bbb1b6f9SApple OSS Distributions {
166*bbb1b6f9SApple OSS Distributions int r;
167*bbb1b6f9SApple OSS Distributions int supported = 0;
168*bbb1b6f9SApple OSS Distributions size_t supported_size = sizeof(supported);
169*bbb1b6f9SApple OSS Distributions
170*bbb1b6f9SApple OSS Distributions r = sysctlbyname("kern.thread_groups_supported", &supported, &supported_size,
171*bbb1b6f9SApple OSS Distributions NULL, 0);
172*bbb1b6f9SApple OSS Distributions if (r < 0) {
173*bbb1b6f9SApple OSS Distributions T_WITH_ERRNO;
174*bbb1b6f9SApple OSS Distributions T_SKIP("could not find \"kern.thread_groups_supported\" sysctl");
175*bbb1b6f9SApple OSS Distributions }
176*bbb1b6f9SApple OSS Distributions
177*bbb1b6f9SApple OSS Distributions if (!supported) {
178*bbb1b6f9SApple OSS Distributions T_SKIP("test was run even though kern.thread_groups_supported is not 1, see rdar://111297938");
179*bbb1b6f9SApple OSS Distributions }
180*bbb1b6f9SApple OSS Distributions
181*bbb1b6f9SApple OSS Distributions r = sysctlbyname("kern.development", &supported, &supported_size,
182*bbb1b6f9SApple OSS Distributions NULL, 0);
183*bbb1b6f9SApple OSS Distributions if (r < 0) {
184*bbb1b6f9SApple OSS Distributions T_WITH_ERRNO;
185*bbb1b6f9SApple OSS Distributions T_SKIP("could not find \"kern.development\" sysctl");
186*bbb1b6f9SApple OSS Distributions }
187*bbb1b6f9SApple OSS Distributions
188*bbb1b6f9SApple OSS Distributions if (!supported) {
189*bbb1b6f9SApple OSS Distributions T_SKIP("test was run even though kern.development is not 1, see rdar://111297938");
190*bbb1b6f9SApple OSS Distributions }
191*bbb1b6f9SApple OSS Distributions }
192*bbb1b6f9SApple OSS Distributions
193*bbb1b6f9SApple OSS Distributions T_DECL(entitled_carplay_mode_check_count, "carplay mode bit should affect coalition thread group flags",
194*bbb1b6f9SApple OSS Distributions T_META_REQUIRES_SYSCTL_EQ("kern.development", 1), T_META_REQUIRES_SYSCTL_EQ("kern.thread_groups_supported", 1))
195*bbb1b6f9SApple OSS Distributions {
196*bbb1b6f9SApple OSS Distributions T_LOG("uid: %d", getuid());
197*bbb1b6f9SApple OSS Distributions skip_if_unsupported();
198*bbb1b6f9SApple OSS Distributions
199*bbb1b6f9SApple OSS Distributions check_carplay_mode(false);
200*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(0);
201*bbb1b6f9SApple OSS Distributions
202*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_CARPLAY_MODE, 0, PRIO_DARWIN_CARPLAY_MODE_ON),
203*bbb1b6f9SApple OSS Distributions "setpriority(PRIO_DARWIN_CARPLAY_MODE_ON)");
204*bbb1b6f9SApple OSS Distributions
205*bbb1b6f9SApple OSS Distributions check_carplay_mode(true);
206*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(1);
207*bbb1b6f9SApple OSS Distributions
208*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_CARPLAY_MODE, 0, PRIO_DARWIN_CARPLAY_MODE_OFF),
209*bbb1b6f9SApple OSS Distributions "setpriority(PRIO_DARWIN_CARPLAY_MODE_OFF)");
210*bbb1b6f9SApple OSS Distributions
211*bbb1b6f9SApple OSS Distributions check_carplay_mode(false);
212*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(0);
213*bbb1b6f9SApple OSS Distributions }
214*bbb1b6f9SApple OSS Distributions
215*bbb1b6f9SApple OSS Distributions T_DECL(carplay_mode_child_exit, "carplay mode bit should disappear when child exits",
216*bbb1b6f9SApple OSS Distributions T_META_REQUIRES_SYSCTL_EQ("kern.development", 1), T_META_REQUIRES_SYSCTL_EQ("kern.thread_groups_supported", 1))
217*bbb1b6f9SApple OSS Distributions {
218*bbb1b6f9SApple OSS Distributions T_LOG("uid: %d", getuid());
219*bbb1b6f9SApple OSS Distributions skip_if_unsupported();
220*bbb1b6f9SApple OSS Distributions
221*bbb1b6f9SApple OSS Distributions check_carplay_mode(false);
222*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(0);
223*bbb1b6f9SApple OSS Distributions
224*bbb1b6f9SApple OSS Distributions T_LOG("Spawning child");
225*bbb1b6f9SApple OSS Distributions
226*bbb1b6f9SApple OSS Distributions pid_t child_pid = fork();
227*bbb1b6f9SApple OSS Distributions
228*bbb1b6f9SApple OSS Distributions if (child_pid == 0) {
229*bbb1b6f9SApple OSS Distributions /* child process */
230*bbb1b6f9SApple OSS Distributions
231*bbb1b6f9SApple OSS Distributions check_carplay_mode(false);
232*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(0);
233*bbb1b6f9SApple OSS Distributions
234*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_CARPLAY_MODE, 0, PRIO_DARWIN_CARPLAY_MODE_ON),
235*bbb1b6f9SApple OSS Distributions "setpriority(PRIO_DARWIN_CARPLAY_MODE_ON)");
236*bbb1b6f9SApple OSS Distributions
237*bbb1b6f9SApple OSS Distributions check_carplay_mode(true);
238*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(1);
239*bbb1b6f9SApple OSS Distributions
240*bbb1b6f9SApple OSS Distributions T_LOG("Exit pid %d with the caprlay mode bit on", getpid());
241*bbb1b6f9SApple OSS Distributions
242*bbb1b6f9SApple OSS Distributions exit(0);
243*bbb1b6f9SApple OSS Distributions } else {
244*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(child_pid, "fork, pid %d", child_pid);
245*bbb1b6f9SApple OSS Distributions
246*bbb1b6f9SApple OSS Distributions /* wait for child process to exit */
247*bbb1b6f9SApple OSS Distributions int exit_status = 0, signum = 0;
248*bbb1b6f9SApple OSS Distributions
249*bbb1b6f9SApple OSS Distributions T_ASSERT_TRUE(dt_waitpid(child_pid, &exit_status, &signum, 5),
250*bbb1b6f9SApple OSS Distributions "wait for child (%d) complete", child_pid);
251*bbb1b6f9SApple OSS Distributions
252*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_EQ(exit_status, 0, "dt_waitpid: exit_status");
253*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_EQ(signum, 0, "dt_waitpid: signum");
254*bbb1b6f9SApple OSS Distributions }
255*bbb1b6f9SApple OSS Distributions
256*bbb1b6f9SApple OSS Distributions check_carplay_mode(false);
257*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(0);
258*bbb1b6f9SApple OSS Distributions }
259*bbb1b6f9SApple OSS Distributions
260*bbb1b6f9SApple OSS Distributions
261*bbb1b6f9SApple OSS Distributions T_DECL(carplay_mode_double_set_and_child_exit, "carplay mode bit on parent should stay when carplay mode child exits",
262*bbb1b6f9SApple OSS Distributions T_META_REQUIRES_SYSCTL_EQ("kern.development", 1), T_META_REQUIRES_SYSCTL_EQ("kern.thread_groups_supported", 1))
263*bbb1b6f9SApple OSS Distributions {
264*bbb1b6f9SApple OSS Distributions T_LOG("uid: %d", getuid());
265*bbb1b6f9SApple OSS Distributions skip_if_unsupported();
266*bbb1b6f9SApple OSS Distributions
267*bbb1b6f9SApple OSS Distributions check_carplay_mode(false);
268*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(0);
269*bbb1b6f9SApple OSS Distributions
270*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_CARPLAY_MODE, 0, PRIO_DARWIN_CARPLAY_MODE_ON),
271*bbb1b6f9SApple OSS Distributions "setpriority(PRIO_DARWIN_CARPLAY_MODE_ON)");
272*bbb1b6f9SApple OSS Distributions
273*bbb1b6f9SApple OSS Distributions check_carplay_mode(true);
274*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(1);
275*bbb1b6f9SApple OSS Distributions
276*bbb1b6f9SApple OSS Distributions pid_t child_pid = fork();
277*bbb1b6f9SApple OSS Distributions
278*bbb1b6f9SApple OSS Distributions if (child_pid == 0) {
279*bbb1b6f9SApple OSS Distributions /* child process */
280*bbb1b6f9SApple OSS Distributions
281*bbb1b6f9SApple OSS Distributions check_carplay_mode(false);
282*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(1);
283*bbb1b6f9SApple OSS Distributions
284*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_CARPLAY_MODE, 0, PRIO_DARWIN_CARPLAY_MODE_ON),
285*bbb1b6f9SApple OSS Distributions "setpriority(PRIO_DARWIN_CARPLAY_MODE_ON)");
286*bbb1b6f9SApple OSS Distributions
287*bbb1b6f9SApple OSS Distributions check_carplay_mode(true);
288*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(2);
289*bbb1b6f9SApple OSS Distributions
290*bbb1b6f9SApple OSS Distributions T_LOG("Exit pid %d with the carplay mode bit on", getpid());
291*bbb1b6f9SApple OSS Distributions exit(0);
292*bbb1b6f9SApple OSS Distributions } else {
293*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(child_pid, "fork, pid %d", child_pid);
294*bbb1b6f9SApple OSS Distributions
295*bbb1b6f9SApple OSS Distributions /* wait for child process to exit */
296*bbb1b6f9SApple OSS Distributions int exit_status = 0, signum = 0;
297*bbb1b6f9SApple OSS Distributions
298*bbb1b6f9SApple OSS Distributions T_ASSERT_TRUE(dt_waitpid(child_pid, &exit_status, &signum, 5),
299*bbb1b6f9SApple OSS Distributions "wait for child (%d) complete", child_pid);
300*bbb1b6f9SApple OSS Distributions
301*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_EQ(exit_status, 0, "dt_waitpid: exit_status");
302*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_EQ(signum, 0, "dt_waitpid: signum");
303*bbb1b6f9SApple OSS Distributions }
304*bbb1b6f9SApple OSS Distributions
305*bbb1b6f9SApple OSS Distributions check_carplay_mode(true);
306*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(1);
307*bbb1b6f9SApple OSS Distributions
308*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_CARPLAY_MODE, 0, PRIO_DARWIN_CARPLAY_MODE_OFF),
309*bbb1b6f9SApple OSS Distributions "setpriority(PRIO_DARWIN_CARPLAY_MODE_OFF)");
310*bbb1b6f9SApple OSS Distributions
311*bbb1b6f9SApple OSS Distributions check_carplay_mode(false);
312*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(0);
313*bbb1b6f9SApple OSS Distributions }
314*bbb1b6f9SApple OSS Distributions
315*bbb1b6f9SApple OSS Distributions T_DECL(carplay_mode_double_set_and_child_unset, "caprlay mode bit on parent should stay when carplay mode child unsets",
316*bbb1b6f9SApple OSS Distributions T_META_REQUIRES_SYSCTL_EQ("kern.development", 1), T_META_REQUIRES_SYSCTL_EQ("kern.thread_groups_supported", 1))
317*bbb1b6f9SApple OSS Distributions {
318*bbb1b6f9SApple OSS Distributions T_LOG("uid: %d", getuid());
319*bbb1b6f9SApple OSS Distributions skip_if_unsupported();
320*bbb1b6f9SApple OSS Distributions
321*bbb1b6f9SApple OSS Distributions check_carplay_mode(false);
322*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(0);
323*bbb1b6f9SApple OSS Distributions
324*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_CARPLAY_MODE, 0, PRIO_DARWIN_CARPLAY_MODE_ON),
325*bbb1b6f9SApple OSS Distributions "setpriority(PRIO_DARWIN_CARPLAY_MODE_ON)");
326*bbb1b6f9SApple OSS Distributions
327*bbb1b6f9SApple OSS Distributions check_carplay_mode(true);
328*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(1);
329*bbb1b6f9SApple OSS Distributions
330*bbb1b6f9SApple OSS Distributions pid_t child_pid = fork();
331*bbb1b6f9SApple OSS Distributions
332*bbb1b6f9SApple OSS Distributions if (child_pid == 0) {
333*bbb1b6f9SApple OSS Distributions /* child process */
334*bbb1b6f9SApple OSS Distributions
335*bbb1b6f9SApple OSS Distributions check_carplay_mode(false);
336*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(1);
337*bbb1b6f9SApple OSS Distributions
338*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_CARPLAY_MODE, 0, PRIO_DARWIN_CARPLAY_MODE_ON),
339*bbb1b6f9SApple OSS Distributions "setpriority(PRIO_DARWIN_CARPLAY_MODE_ON)");
340*bbb1b6f9SApple OSS Distributions
341*bbb1b6f9SApple OSS Distributions check_carplay_mode(true);
342*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(2);
343*bbb1b6f9SApple OSS Distributions
344*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_CARPLAY_MODE, 0, PRIO_DARWIN_CARPLAY_MODE_OFF),
345*bbb1b6f9SApple OSS Distributions "setpriority(PRIO_DARWIN_CARPLAY_MODE_OFF)");
346*bbb1b6f9SApple OSS Distributions
347*bbb1b6f9SApple OSS Distributions check_carplay_mode(false);
348*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(1);
349*bbb1b6f9SApple OSS Distributions
350*bbb1b6f9SApple OSS Distributions T_LOG("Exit pid %d with the carplay mode bit off", getpid());
351*bbb1b6f9SApple OSS Distributions exit(0);
352*bbb1b6f9SApple OSS Distributions } else {
353*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(child_pid, "fork, pid %d", child_pid);
354*bbb1b6f9SApple OSS Distributions
355*bbb1b6f9SApple OSS Distributions /* wait for child process to exit */
356*bbb1b6f9SApple OSS Distributions int exit_status = 0, signum = 0;
357*bbb1b6f9SApple OSS Distributions
358*bbb1b6f9SApple OSS Distributions T_ASSERT_TRUE(dt_waitpid(child_pid, &exit_status, &signum, 5),
359*bbb1b6f9SApple OSS Distributions "wait for child (%d) complete", child_pid);
360*bbb1b6f9SApple OSS Distributions
361*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_EQ(exit_status, 0, "dt_waitpid: exit_status");
362*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_EQ(signum, 0, "dt_waitpid: signum");
363*bbb1b6f9SApple OSS Distributions }
364*bbb1b6f9SApple OSS Distributions
365*bbb1b6f9SApple OSS Distributions check_carplay_mode(true);
366*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(1);
367*bbb1b6f9SApple OSS Distributions
368*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_CARPLAY_MODE, 0, PRIO_DARWIN_CARPLAY_MODE_OFF),
369*bbb1b6f9SApple OSS Distributions "setpriority(PRIO_DARWIN_CARPLAY_MODE_OFF)");
370*bbb1b6f9SApple OSS Distributions
371*bbb1b6f9SApple OSS Distributions check_carplay_mode(false);
372*bbb1b6f9SApple OSS Distributions check_carplay_mode_count(0);
373*bbb1b6f9SApple OSS Distributions }
374