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