xref: /xnu-8796.121.2/tests/prioritize_process_launch.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * prioritize process launch: Tests prioritized process launch across posix spawn and exec.
3*c54f35caSApple OSS Distributions  */
4*c54f35caSApple OSS Distributions 
5*c54f35caSApple OSS Distributions #ifdef T_NAMESPACE
6*c54f35caSApple OSS Distributions #undef T_NAMESPACE
7*c54f35caSApple OSS Distributions #endif
8*c54f35caSApple OSS Distributions 
9*c54f35caSApple OSS Distributions #include <darwintest.h>
10*c54f35caSApple OSS Distributions #include <darwintest_multiprocess.h>
11*c54f35caSApple OSS Distributions 
12*c54f35caSApple OSS Distributions #include <dispatch/dispatch.h>
13*c54f35caSApple OSS Distributions #include <pthread.h>
14*c54f35caSApple OSS Distributions #include <launch.h>
15*c54f35caSApple OSS Distributions #include <mach/mach.h>
16*c54f35caSApple OSS Distributions #include <mach/message.h>
17*c54f35caSApple OSS Distributions #include <mach/mach_voucher.h>
18*c54f35caSApple OSS Distributions #include <pthread/workqueue_private.h>
19*c54f35caSApple OSS Distributions #include <voucher/ipc_pthread_priority_types.h>
20*c54f35caSApple OSS Distributions #include <servers/bootstrap.h>
21*c54f35caSApple OSS Distributions #include <stdlib.h>
22*c54f35caSApple OSS Distributions #include <sys/event.h>
23*c54f35caSApple OSS Distributions #include <unistd.h>
24*c54f35caSApple OSS Distributions #include <crt_externs.h>
25*c54f35caSApple OSS Distributions #include <signal.h>
26*c54f35caSApple OSS Distributions #include <sys/types.h>
27*c54f35caSApple OSS Distributions #include <sys/sysctl.h>
28*c54f35caSApple OSS Distributions #include <libkern/OSAtomic.h>
29*c54f35caSApple OSS Distributions #include <sys/wait.h>
30*c54f35caSApple OSS Distributions #include <spawn.h>
31*c54f35caSApple OSS Distributions #include <spawn_private.h>
32*c54f35caSApple OSS Distributions 
33*c54f35caSApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.prioritize_process_launch"),
34*c54f35caSApple OSS Distributions     T_META_RUN_CONCURRENTLY(true));
35*c54f35caSApple OSS Distributions 
36*c54f35caSApple OSS Distributions #define HELPER_TIMEOUT_SECS (3000)
37*c54f35caSApple OSS Distributions #define MACH_RCV_OPTIONS  (MACH_RCV_MSG | MACH_RCV_LARGE | MACH_RCV_LARGE_IDENTITY | \
38*c54f35caSApple OSS Distributions 	            MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_CTX) | \
39*c54f35caSApple OSS Distributions 	            MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0))
40*c54f35caSApple OSS Distributions 
41*c54f35caSApple OSS Distributions static pthread_t
42*c54f35caSApple OSS Distributions thread_create_at_qos(qos_class_t qos, void * (*function)(void *), void *arg);
43*c54f35caSApple OSS Distributions static mach_port_t sr_port;
44*c54f35caSApple OSS Distributions 
45*c54f35caSApple OSS Distributions 
46*c54f35caSApple OSS Distributions #pragma mark Mach receive
47*c54f35caSApple OSS Distributions 
48*c54f35caSApple OSS Distributions static void
send(mach_port_t send_port,mach_port_t reply_port,mach_port_t msg_port,mach_msg_priority_t priority,mach_msg_option_t options,int send_disposition)49*c54f35caSApple OSS Distributions send(
50*c54f35caSApple OSS Distributions 	mach_port_t send_port,
51*c54f35caSApple OSS Distributions 	mach_port_t reply_port,
52*c54f35caSApple OSS Distributions 	mach_port_t msg_port,
53*c54f35caSApple OSS Distributions 	mach_msg_priority_t priority,
54*c54f35caSApple OSS Distributions 	mach_msg_option_t options,
55*c54f35caSApple OSS Distributions 	int send_disposition)
56*c54f35caSApple OSS Distributions {
57*c54f35caSApple OSS Distributions 	kern_return_t ret = 0;
58*c54f35caSApple OSS Distributions 
59*c54f35caSApple OSS Distributions 	struct {
60*c54f35caSApple OSS Distributions 		mach_msg_header_t header;
61*c54f35caSApple OSS Distributions 		mach_msg_body_t body;
62*c54f35caSApple OSS Distributions 		mach_msg_port_descriptor_t port_descriptor;
63*c54f35caSApple OSS Distributions 	} send_msg = {
64*c54f35caSApple OSS Distributions 		.header = {
65*c54f35caSApple OSS Distributions 			.msgh_remote_port = send_port,
66*c54f35caSApple OSS Distributions 			.msgh_local_port  = reply_port,
67*c54f35caSApple OSS Distributions 			.msgh_bits        = MACH_MSGH_BITS_SET(send_disposition,
68*c54f35caSApple OSS Distributions 	    reply_port ? MACH_MSG_TYPE_MAKE_SEND_ONCE : 0, 0,
69*c54f35caSApple OSS Distributions 	    MACH_MSGH_BITS_COMPLEX),
70*c54f35caSApple OSS Distributions 			.msgh_id          = 0x100,
71*c54f35caSApple OSS Distributions 			.msgh_size        = sizeof(send_msg),
72*c54f35caSApple OSS Distributions 		},
73*c54f35caSApple OSS Distributions 		.body = {
74*c54f35caSApple OSS Distributions 			.msgh_descriptor_count = 1,
75*c54f35caSApple OSS Distributions 		},
76*c54f35caSApple OSS Distributions 		.port_descriptor = {
77*c54f35caSApple OSS Distributions 			.name        = msg_port,
78*c54f35caSApple OSS Distributions 			.disposition = MACH_MSG_TYPE_MOVE_RECEIVE,
79*c54f35caSApple OSS Distributions 			.type        = MACH_MSG_PORT_DESCRIPTOR,
80*c54f35caSApple OSS Distributions 		},
81*c54f35caSApple OSS Distributions 	};
82*c54f35caSApple OSS Distributions 
83*c54f35caSApple OSS Distributions 	if (msg_port == MACH_PORT_NULL) {
84*c54f35caSApple OSS Distributions 		send_msg.body.msgh_descriptor_count = 0;
85*c54f35caSApple OSS Distributions 	}
86*c54f35caSApple OSS Distributions 
87*c54f35caSApple OSS Distributions 	ret = mach_msg(&(send_msg.header),
88*c54f35caSApple OSS Distributions 	    MACH_SEND_MSG |
89*c54f35caSApple OSS Distributions 	    MACH_SEND_TIMEOUT |
90*c54f35caSApple OSS Distributions 	    MACH_SEND_OVERRIDE |
91*c54f35caSApple OSS Distributions 	    options,
92*c54f35caSApple OSS Distributions 	    send_msg.header.msgh_size,
93*c54f35caSApple OSS Distributions 	    0,
94*c54f35caSApple OSS Distributions 	    MACH_PORT_NULL,
95*c54f35caSApple OSS Distributions 	    10000,
96*c54f35caSApple OSS Distributions 	    priority);
97*c54f35caSApple OSS Distributions 
98*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "client mach_msg");
99*c54f35caSApple OSS Distributions }
100*c54f35caSApple OSS Distributions 
101*c54f35caSApple OSS Distributions static void
receive(mach_port_t rcv_port,mach_port_t notify_port)102*c54f35caSApple OSS Distributions receive(
103*c54f35caSApple OSS Distributions 	mach_port_t rcv_port,
104*c54f35caSApple OSS Distributions 	mach_port_t notify_port)
105*c54f35caSApple OSS Distributions {
106*c54f35caSApple OSS Distributions 	kern_return_t ret = 0;
107*c54f35caSApple OSS Distributions 
108*c54f35caSApple OSS Distributions 	struct {
109*c54f35caSApple OSS Distributions 		mach_msg_header_t header;
110*c54f35caSApple OSS Distributions 		mach_msg_body_t body;
111*c54f35caSApple OSS Distributions 		mach_msg_port_descriptor_t port_descriptor;
112*c54f35caSApple OSS Distributions 		mach_msg_trailer_t trailer;
113*c54f35caSApple OSS Distributions 	} rcv_msg = {
114*c54f35caSApple OSS Distributions 		.header =
115*c54f35caSApple OSS Distributions 		{
116*c54f35caSApple OSS Distributions 			.msgh_remote_port = MACH_PORT_NULL,
117*c54f35caSApple OSS Distributions 			.msgh_local_port  = rcv_port,
118*c54f35caSApple OSS Distributions 			.msgh_size        = sizeof(rcv_msg),
119*c54f35caSApple OSS Distributions 		},
120*c54f35caSApple OSS Distributions 	};
121*c54f35caSApple OSS Distributions 
122*c54f35caSApple OSS Distributions 	T_LOG("Client: Starting sync receive\n");
123*c54f35caSApple OSS Distributions 
124*c54f35caSApple OSS Distributions 	ret = mach_msg(&(rcv_msg.header),
125*c54f35caSApple OSS Distributions 	    MACH_RCV_MSG |
126*c54f35caSApple OSS Distributions 	    MACH_RCV_SYNC_WAIT,
127*c54f35caSApple OSS Distributions 	    0,
128*c54f35caSApple OSS Distributions 	    rcv_msg.header.msgh_size,
129*c54f35caSApple OSS Distributions 	    rcv_port,
130*c54f35caSApple OSS Distributions 	    0,
131*c54f35caSApple OSS Distributions 	    notify_port);
132*c54f35caSApple OSS Distributions }
133*c54f35caSApple OSS Distributions 
134*c54f35caSApple OSS Distributions static int
get_pri(thread_t thread_port)135*c54f35caSApple OSS Distributions get_pri(thread_t thread_port)
136*c54f35caSApple OSS Distributions {
137*c54f35caSApple OSS Distributions 	kern_return_t kr;
138*c54f35caSApple OSS Distributions 
139*c54f35caSApple OSS Distributions 	thread_extended_info_data_t extended_info;
140*c54f35caSApple OSS Distributions 	mach_msg_type_number_t count = THREAD_EXTENDED_INFO_COUNT;
141*c54f35caSApple OSS Distributions 	kr = thread_info(thread_port, THREAD_EXTENDED_INFO,
142*c54f35caSApple OSS Distributions 	    (thread_info_t)&extended_info, &count);
143*c54f35caSApple OSS Distributions 
144*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_info");
145*c54f35caSApple OSS Distributions 
146*c54f35caSApple OSS Distributions 	return extended_info.pth_curpri;
147*c54f35caSApple OSS Distributions }
148*c54f35caSApple OSS Distributions 
149*c54f35caSApple OSS Distributions static void
set_thread_name(const char * fn_name)150*c54f35caSApple OSS Distributions set_thread_name(const char *fn_name)
151*c54f35caSApple OSS Distributions {
152*c54f35caSApple OSS Distributions 	char name[50] = "";
153*c54f35caSApple OSS Distributions 
154*c54f35caSApple OSS Distributions 	thread_t thread_port = pthread_mach_thread_np(pthread_self());
155*c54f35caSApple OSS Distributions 
156*c54f35caSApple OSS Distributions 	int pri = get_pri(thread_port);
157*c54f35caSApple OSS Distributions 
158*c54f35caSApple OSS Distributions 	snprintf(name, sizeof(name), "%s at pri %2d", fn_name, pri);
159*c54f35caSApple OSS Distributions 	pthread_setname_np(name);
160*c54f35caSApple OSS Distributions }
161*c54f35caSApple OSS Distributions 
162*c54f35caSApple OSS Distributions static void
thread_wait_to_block(mach_port_t thread_port)163*c54f35caSApple OSS Distributions thread_wait_to_block(mach_port_t thread_port)
164*c54f35caSApple OSS Distributions {
165*c54f35caSApple OSS Distributions 	thread_extended_info_data_t extended_info;
166*c54f35caSApple OSS Distributions 	kern_return_t kr;
167*c54f35caSApple OSS Distributions 
168*c54f35caSApple OSS Distributions 	while (1) {
169*c54f35caSApple OSS Distributions 		mach_msg_type_number_t count = THREAD_EXTENDED_INFO_COUNT;
170*c54f35caSApple OSS Distributions 		kr = thread_info(thread_port, THREAD_EXTENDED_INFO,
171*c54f35caSApple OSS Distributions 		    (thread_info_t)&extended_info, &count);
172*c54f35caSApple OSS Distributions 
173*c54f35caSApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_info");
174*c54f35caSApple OSS Distributions 
175*c54f35caSApple OSS Distributions 		if (extended_info.pth_run_state == TH_STATE_WAITING) {
176*c54f35caSApple OSS Distributions 			T_LOG("Target thread blocked\n");
177*c54f35caSApple OSS Distributions 			break;
178*c54f35caSApple OSS Distributions 		}
179*c54f35caSApple OSS Distributions 		thread_switch(thread_port, SWITCH_OPTION_DEPRESS, 0);
180*c54f35caSApple OSS Distributions 	}
181*c54f35caSApple OSS Distributions }
182*c54f35caSApple OSS Distributions 
183*c54f35caSApple OSS Distributions static void *
thread_sync_rcv(void * arg)184*c54f35caSApple OSS Distributions thread_sync_rcv(void *arg)
185*c54f35caSApple OSS Distributions {
186*c54f35caSApple OSS Distributions 	mach_port_t port = (mach_port_t)arg;
187*c54f35caSApple OSS Distributions 	mach_port_t special_reply_port;
188*c54f35caSApple OSS Distributions 
189*c54f35caSApple OSS Distributions 	set_thread_name(__FUNCTION__);
190*c54f35caSApple OSS Distributions 	special_reply_port = thread_get_special_reply_port();
191*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port), "get_thread_special_reply_port");
192*c54f35caSApple OSS Distributions 
193*c54f35caSApple OSS Distributions 	sr_port = special_reply_port;
194*c54f35caSApple OSS Distributions 	/* Do a sync rcv on special reply port and push on given arg port */
195*c54f35caSApple OSS Distributions 	receive(special_reply_port, port);
196*c54f35caSApple OSS Distributions 	return NULL;
197*c54f35caSApple OSS Distributions }
198*c54f35caSApple OSS Distributions 
199*c54f35caSApple OSS Distributions static pthread_t
thread_create_at_qos(qos_class_t qos,void * (* function)(void *),void * arg)200*c54f35caSApple OSS Distributions thread_create_at_qos(qos_class_t qos, void * (*function)(void *), void *arg)
201*c54f35caSApple OSS Distributions {
202*c54f35caSApple OSS Distributions 	qos_class_t qos_thread;
203*c54f35caSApple OSS Distributions 	pthread_t pthread;
204*c54f35caSApple OSS Distributions 	pthread_attr_t attr;
205*c54f35caSApple OSS Distributions 	int ret;
206*c54f35caSApple OSS Distributions 
207*c54f35caSApple OSS Distributions 	ret = setpriority(PRIO_DARWIN_ROLE, 0, PRIO_DARWIN_ROLE_UI_FOCAL);
208*c54f35caSApple OSS Distributions 	if (ret != 0) {
209*c54f35caSApple OSS Distributions 		T_LOG("set priority failed\n");
210*c54f35caSApple OSS Distributions 	}
211*c54f35caSApple OSS Distributions 
212*c54f35caSApple OSS Distributions 	pthread_attr_init(&attr);
213*c54f35caSApple OSS Distributions 	pthread_attr_set_qos_class_np(&attr, qos, 0);
214*c54f35caSApple OSS Distributions 	pthread_create(&pthread, &attr, function, arg);
215*c54f35caSApple OSS Distributions 
216*c54f35caSApple OSS Distributions 	T_LOG("pthread created\n");
217*c54f35caSApple OSS Distributions 	pthread_get_qos_class_np(pthread, &qos_thread, NULL);
218*c54f35caSApple OSS Distributions 	return pthread;
219*c54f35caSApple OSS Distributions }
220*c54f35caSApple OSS Distributions 
221*c54f35caSApple OSS Distributions static mach_port_t
get_sync_push_port_at_qos(qos_class_t qos)222*c54f35caSApple OSS Distributions get_sync_push_port_at_qos(qos_class_t qos)
223*c54f35caSApple OSS Distributions {
224*c54f35caSApple OSS Distributions 	mach_port_t port;
225*c54f35caSApple OSS Distributions 	kern_return_t kr;
226*c54f35caSApple OSS Distributions 	pthread_t pthread;
227*c54f35caSApple OSS Distributions 	thread_t thread;
228*c54f35caSApple OSS Distributions 
229*c54f35caSApple OSS Distributions 	/* Create a rcv right to have a sync ipc push from a thread */
230*c54f35caSApple OSS Distributions 	kr = mach_port_allocate(mach_task_self(),
231*c54f35caSApple OSS Distributions 	    MACH_PORT_RIGHT_RECEIVE,
232*c54f35caSApple OSS Distributions 	    &port);
233*c54f35caSApple OSS Distributions 
234*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "sync push port  mach_port_allocate");
235*c54f35caSApple OSS Distributions 
236*c54f35caSApple OSS Distributions 	kr = mach_port_insert_right(mach_task_self(),
237*c54f35caSApple OSS Distributions 	    port,
238*c54f35caSApple OSS Distributions 	    port,
239*c54f35caSApple OSS Distributions 	    MACH_MSG_TYPE_MAKE_SEND);
240*c54f35caSApple OSS Distributions 
241*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "sync push port mach_port_insert_right");
242*c54f35caSApple OSS Distributions 
243*c54f35caSApple OSS Distributions 	/* Create a thread at given qos and start a sync push on given port */
244*c54f35caSApple OSS Distributions 	pthread = thread_create_at_qos(qos, thread_sync_rcv, (void *)(uintptr_t)port);
245*c54f35caSApple OSS Distributions 	thread = pthread_mach_thread_np(pthread);
246*c54f35caSApple OSS Distributions 	thread_wait_to_block(thread);
247*c54f35caSApple OSS Distributions 
248*c54f35caSApple OSS Distributions 	return port;
249*c54f35caSApple OSS Distributions }
250*c54f35caSApple OSS Distributions 
251*c54f35caSApple OSS Distributions static mach_port_t
create_port_and_copyin_a_port(mach_port_t port)252*c54f35caSApple OSS Distributions create_port_and_copyin_a_port(mach_port_t port)
253*c54f35caSApple OSS Distributions {
254*c54f35caSApple OSS Distributions 	mach_port_t new_port;
255*c54f35caSApple OSS Distributions 	kern_return_t kr;
256*c54f35caSApple OSS Distributions 
257*c54f35caSApple OSS Distributions 	/* Create a rcv right */
258*c54f35caSApple OSS Distributions 	kr = mach_port_allocate(mach_task_self(),
259*c54f35caSApple OSS Distributions 	    MACH_PORT_RIGHT_RECEIVE,
260*c54f35caSApple OSS Distributions 	    &new_port);
261*c54f35caSApple OSS Distributions 
262*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "copyin  mach_port_allocate");
263*c54f35caSApple OSS Distributions 
264*c54f35caSApple OSS Distributions 	kr = mach_port_insert_right(mach_task_self(),
265*c54f35caSApple OSS Distributions 	    new_port,
266*c54f35caSApple OSS Distributions 	    new_port,
267*c54f35caSApple OSS Distributions 	    MACH_MSG_TYPE_MAKE_SEND);
268*c54f35caSApple OSS Distributions 
269*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "copyin mach_port_insert_right");
270*c54f35caSApple OSS Distributions 
271*c54f35caSApple OSS Distributions 	send(new_port, MACH_PORT_NULL, port, 0, 0, MACH_MSG_TYPE_COPY_SEND);
272*c54f35caSApple OSS Distributions 	return new_port;
273*c54f35caSApple OSS Distributions }
274*c54f35caSApple OSS Distributions 
275*c54f35caSApple OSS Distributions static pid_t
posix_spawn_child_with_watch_ports(char * binary,char * arg,mach_port_t * port_array,int arrayCnt)276*c54f35caSApple OSS Distributions posix_spawn_child_with_watch_ports(
277*c54f35caSApple OSS Distributions 	char *binary,
278*c54f35caSApple OSS Distributions 	char *arg,
279*c54f35caSApple OSS Distributions 	mach_port_t *port_array,
280*c54f35caSApple OSS Distributions 	int arrayCnt)
281*c54f35caSApple OSS Distributions {
282*c54f35caSApple OSS Distributions 	pid_t child_pid = 0;
283*c54f35caSApple OSS Distributions 	char *new_argv[] = { binary, arg, NULL};
284*c54f35caSApple OSS Distributions 	errno_t ret;
285*c54f35caSApple OSS Distributions 	posix_spawnattr_t attr;
286*c54f35caSApple OSS Distributions 
287*c54f35caSApple OSS Distributions 	ret = posix_spawnattr_init(&attr);
288*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_init");
289*c54f35caSApple OSS Distributions 
290*c54f35caSApple OSS Distributions 	ret = posix_spawnattr_set_importancewatch_port_np(&attr, arrayCnt, port_array);
291*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_set_importancewatch_port_np");
292*c54f35caSApple OSS Distributions 
293*c54f35caSApple OSS Distributions 	ret = posix_spawn(&child_pid, binary, NULL, &attr, new_argv, NULL);
294*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn");
295*c54f35caSApple OSS Distributions 
296*c54f35caSApple OSS Distributions 	ret = posix_spawnattr_destroy(&attr);
297*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_destroy");
298*c54f35caSApple OSS Distributions 
299*c54f35caSApple OSS Distributions 	return child_pid;
300*c54f35caSApple OSS Distributions }
301*c54f35caSApple OSS Distributions 
302*c54f35caSApple OSS Distributions static void
worker_cb(pthread_priority_t __unused priority)303*c54f35caSApple OSS Distributions worker_cb(pthread_priority_t __unused priority)
304*c54f35caSApple OSS Distributions {
305*c54f35caSApple OSS Distributions 	T_FAIL("a worker thread was created");
306*c54f35caSApple OSS Distributions }
307*c54f35caSApple OSS Distributions 
308*c54f35caSApple OSS Distributions static void
event_cb(void ** __unused events,int * __unused nevents)309*c54f35caSApple OSS Distributions event_cb(void ** __unused events, int * __unused nevents)
310*c54f35caSApple OSS Distributions {
311*c54f35caSApple OSS Distributions 	T_FAIL("a kevent routine was called instead of workloop");
312*c54f35caSApple OSS Distributions }
313*c54f35caSApple OSS Distributions 
314*c54f35caSApple OSS Distributions static void
workloop_cb_test_intransit(uint64_t * workloop_id __unused,void ** eventslist,int * events)315*c54f35caSApple OSS Distributions workloop_cb_test_intransit(uint64_t *workloop_id __unused, void **eventslist, int *events)
316*c54f35caSApple OSS Distributions {
317*c54f35caSApple OSS Distributions 	pid_t pid;
318*c54f35caSApple OSS Distributions 	int stat;
319*c54f35caSApple OSS Distributions 	int priority;
320*c54f35caSApple OSS Distributions 	mach_port_t port;
321*c54f35caSApple OSS Distributions 	struct kevent_qos_s *kev = *eventslist;
322*c54f35caSApple OSS Distributions 	mach_msg_header_t *hdr = (mach_msg_header_t *)kev->ext[0];
323*c54f35caSApple OSS Distributions 	port = hdr->msgh_local_port;
324*c54f35caSApple OSS Distributions 
325*c54f35caSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_intransit called. ");
326*c54f35caSApple OSS Distributions 	T_LOG("The total events returned is %d", *events);
327*c54f35caSApple OSS Distributions 
328*c54f35caSApple OSS Distributions 	priority = get_pri(mach_thread_self());
329*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(priority, 47, "Priority of servicer is %d", priority);
330*c54f35caSApple OSS Distributions 
331*c54f35caSApple OSS Distributions 	pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "WAIT", &port, 1);
332*c54f35caSApple OSS Distributions 
333*c54f35caSApple OSS Distributions 	/* Make sure our priority has dropped */
334*c54f35caSApple OSS Distributions 	priority = get_pri(mach_thread_self());
335*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(priority, 31, "Priority of servicer is %d", priority);
336*c54f35caSApple OSS Distributions 
337*c54f35caSApple OSS Distributions 	sleep(2);
338*c54f35caSApple OSS Distributions 
339*c54f35caSApple OSS Distributions 	/*enqueue the port to sever the temp onwer boost */
340*c54f35caSApple OSS Distributions 	create_port_and_copyin_a_port(port);
341*c54f35caSApple OSS Distributions 
342*c54f35caSApple OSS Distributions 	waitpid(pid, &stat, 0);
343*c54f35caSApple OSS Distributions 
344*c54f35caSApple OSS Distributions 	*events = 0;
345*c54f35caSApple OSS Distributions 
346*c54f35caSApple OSS Distributions 	T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
347*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(WEXITSTATUS(stat), 31, "Temp owner boost did not work correctly with knotes");
348*c54f35caSApple OSS Distributions 	T_END;
349*c54f35caSApple OSS Distributions }
350*c54f35caSApple OSS Distributions 
351*c54f35caSApple OSS Distributions static void
workloop_cb_test_knote_kill(uint64_t * workloop_id __unused,void ** eventslist,int * events)352*c54f35caSApple OSS Distributions workloop_cb_test_knote_kill(uint64_t *workloop_id __unused, void **eventslist, int *events)
353*c54f35caSApple OSS Distributions {
354*c54f35caSApple OSS Distributions 	pid_t pid;
355*c54f35caSApple OSS Distributions 	int stat;
356*c54f35caSApple OSS Distributions 	int priority;
357*c54f35caSApple OSS Distributions 	mach_port_t port;
358*c54f35caSApple OSS Distributions 	struct kevent_qos_s *kev = *eventslist;
359*c54f35caSApple OSS Distributions 	mach_msg_header_t *hdr = (mach_msg_header_t *)kev->ext[0];
360*c54f35caSApple OSS Distributions 	port = hdr->msgh_local_port;
361*c54f35caSApple OSS Distributions 
362*c54f35caSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_knote_kill called. ");
363*c54f35caSApple OSS Distributions 	T_LOG("The total events returned is %d", *events);
364*c54f35caSApple OSS Distributions 
365*c54f35caSApple OSS Distributions 	priority = get_pri(mach_thread_self());
366*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(priority, 47, "Priority of servicer is %d", priority);
367*c54f35caSApple OSS Distributions 
368*c54f35caSApple OSS Distributions 	pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "EXIT", &port, 1);
369*c54f35caSApple OSS Distributions 
370*c54f35caSApple OSS Distributions 	sleep(2);
371*c54f35caSApple OSS Distributions 
372*c54f35caSApple OSS Distributions 	/* Make sure our priority is boosted again */
373*c54f35caSApple OSS Distributions 	priority = get_pri(mach_thread_self());
374*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(priority, 47, "Priority of servicer is %d", priority);
375*c54f35caSApple OSS Distributions 
376*c54f35caSApple OSS Distributions 	waitpid(pid, &stat, 0);
377*c54f35caSApple OSS Distributions 
378*c54f35caSApple OSS Distributions 	*events = 0;
379*c54f35caSApple OSS Distributions 
380*c54f35caSApple OSS Distributions 	T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
381*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(WEXITSTATUS(stat), 47, "Temp owner boost did not work correctly with knotes");
382*c54f35caSApple OSS Distributions 	T_END;
383*c54f35caSApple OSS Distributions }
384*c54f35caSApple OSS Distributions 
385*c54f35caSApple OSS Distributions static void
workloop_cb_test_sync_bootstrap(uint64_t * workloop_id __unused,void ** eventslist,int * events)386*c54f35caSApple OSS Distributions workloop_cb_test_sync_bootstrap(uint64_t *workloop_id __unused, void **eventslist, int *events)
387*c54f35caSApple OSS Distributions {
388*c54f35caSApple OSS Distributions 	static pid_t pid = 0;
389*c54f35caSApple OSS Distributions 	int stat;
390*c54f35caSApple OSS Distributions 	int priority;
391*c54f35caSApple OSS Distributions 	static mach_port_t port = MACH_PORT_NULL;
392*c54f35caSApple OSS Distributions 	struct kevent_qos_s *kev = *eventslist;
393*c54f35caSApple OSS Distributions 	mach_msg_header_t *hdr = (mach_msg_header_t *)kev->ext[0];
394*c54f35caSApple OSS Distributions 
395*c54f35caSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_knote_kill called. ");
396*c54f35caSApple OSS Distributions 	T_LOG("The total events returned is %d", *events);
397*c54f35caSApple OSS Distributions 
398*c54f35caSApple OSS Distributions 	/* Check if called for peek */
399*c54f35caSApple OSS Distributions 	if (hdr == NULL) {
400*c54f35caSApple OSS Distributions 		priority = get_pri(mach_thread_self());
401*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(priority, 47, "Priority of servicer is %d", priority);
402*c54f35caSApple OSS Distributions 
403*c54f35caSApple OSS Distributions 		port = (mach_port_t)kev->ident;
404*c54f35caSApple OSS Distributions 		pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "MSGSYNC", &port, 1);
405*c54f35caSApple OSS Distributions 	} else {
406*c54f35caSApple OSS Distributions 		/* Wait till the priority of servicer is 47 */
407*c54f35caSApple OSS Distributions 		T_LOG("Waiting for the servicer to be boosted");
408*c54f35caSApple OSS Distributions 		do {
409*c54f35caSApple OSS Distributions 			sleep(1);
410*c54f35caSApple OSS Distributions 			priority = get_pri(mach_thread_self());
411*c54f35caSApple OSS Distributions 		} while (priority != 47);
412*c54f35caSApple OSS Distributions 
413*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(priority, 47, "Priority of servicer is %d", priority);
414*c54f35caSApple OSS Distributions 
415*c54f35caSApple OSS Distributions 		/* Get the reply port and send the receive right in it */
416*c54f35caSApple OSS Distributions 		mach_port_t reply_port = hdr->msgh_remote_port;
417*c54f35caSApple OSS Distributions 		T_LOG("The rcv right to send is %d", port);
418*c54f35caSApple OSS Distributions 		send(reply_port, MACH_PORT_NULL, port, 0, 0, MACH_MSG_TYPE_MOVE_SEND_ONCE);
419*c54f35caSApple OSS Distributions 
420*c54f35caSApple OSS Distributions 		waitpid(pid, &stat, 0);
421*c54f35caSApple OSS Distributions 
422*c54f35caSApple OSS Distributions 		/* The handler priority should not be boosted anymore */
423*c54f35caSApple OSS Distributions 		priority = get_pri(mach_thread_self());
424*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(priority, 31, "Priority of servicer is %d", priority);
425*c54f35caSApple OSS Distributions 
426*c54f35caSApple OSS Distributions 		T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
427*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(WEXITSTATUS(stat), 31, "Temp owner boost did not work correctly with knotes");
428*c54f35caSApple OSS Distributions 		T_END;
429*c54f35caSApple OSS Distributions 	}
430*c54f35caSApple OSS Distributions 	*events = 0;
431*c54f35caSApple OSS Distributions }
432*c54f35caSApple OSS Distributions 
433*c54f35caSApple OSS Distributions static void
register_workloop_for_port(mach_port_t port,pthread_workqueue_function_workloop_t func,unsigned int options)434*c54f35caSApple OSS Distributions register_workloop_for_port(
435*c54f35caSApple OSS Distributions 	mach_port_t port,
436*c54f35caSApple OSS Distributions 	pthread_workqueue_function_workloop_t func,
437*c54f35caSApple OSS Distributions 	unsigned int options)
438*c54f35caSApple OSS Distributions {
439*c54f35caSApple OSS Distributions 	int r;
440*c54f35caSApple OSS Distributions 
441*c54f35caSApple OSS Distributions 	/* register workloop handler with pthread */
442*c54f35caSApple OSS Distributions 	if (func != NULL) {
443*c54f35caSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
444*c54f35caSApple OSS Distributions 			    worker_cb, event_cb,
445*c54f35caSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)func, 0, 0), NULL);
446*c54f35caSApple OSS Distributions 	}
447*c54f35caSApple OSS Distributions 
448*c54f35caSApple OSS Distributions 	/* attach port to workloop */
449*c54f35caSApple OSS Distributions 	struct kevent_qos_s kev[] = {{
450*c54f35caSApple OSS Distributions 					     .ident = port,
451*c54f35caSApple OSS Distributions 					     .filter = EVFILT_MACHPORT,
452*c54f35caSApple OSS Distributions 					     .flags = EV_ADD | EV_UDATA_SPECIFIC | EV_DISPATCH | EV_VANISHED,
453*c54f35caSApple OSS Distributions 					     .fflags = options,
454*c54f35caSApple OSS Distributions 					     .data = 1,
455*c54f35caSApple OSS Distributions 					     .qos = (int32_t)_pthread_qos_class_encode(QOS_CLASS_DEFAULT, 0, 0)
456*c54f35caSApple OSS Distributions 				     }};
457*c54f35caSApple OSS Distributions 
458*c54f35caSApple OSS Distributions 	struct kevent_qos_s kev_err[] = {{ 0 }};
459*c54f35caSApple OSS Distributions 
460*c54f35caSApple OSS Distributions 	/* Setup workloop for mach msg rcv */
461*c54f35caSApple OSS Distributions 	r = kevent_id(25, kev, 1, kev_err, 1, NULL,
462*c54f35caSApple OSS Distributions 	    NULL, KEVENT_FLAG_WORKLOOP | KEVENT_FLAG_ERROR_EVENTS);
463*c54f35caSApple OSS Distributions 
464*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(r, "kevent_id");
465*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(r, 0, "no errors returned from kevent_id");
466*c54f35caSApple OSS Distributions }
467*c54f35caSApple OSS Distributions 
468*c54f35caSApple OSS Distributions /*
469*c54f35caSApple OSS Distributions  * Test 1: Test turnstile boosting for temp owner ports for posix_spawn.
470*c54f35caSApple OSS Distributions  *
471*c54f35caSApple OSS Distributions  * Create a port with sync IPC push and then pass the port to posix_spawn as a watch port and
472*c54f35caSApple OSS Distributions  * test that spawned binary has the temp owner push of the port.
473*c54f35caSApple OSS Distributions  */
474*c54f35caSApple OSS Distributions T_DECL(posix_spawn_basic_priority, "Basic posix spawn temp owner priority test", T_META_ASROOT(YES))
475*c54f35caSApple OSS Distributions {
476*c54f35caSApple OSS Distributions 	mach_port_t port;
477*c54f35caSApple OSS Distributions 	pid_t pid;
478*c54f35caSApple OSS Distributions 	int stat;
479*c54f35caSApple OSS Distributions 
480*c54f35caSApple OSS Distributions 	port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
481*c54f35caSApple OSS Distributions 	pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "EXIT", &port, 1);
482*c54f35caSApple OSS Distributions 
483*c54f35caSApple OSS Distributions 	waitpid(pid, &stat, 0);
484*c54f35caSApple OSS Distributions 
485*c54f35caSApple OSS Distributions 	T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
486*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(WEXITSTATUS(stat), 47, "spawn did not properly boost main thread");
487*c54f35caSApple OSS Distributions 	T_END;
488*c54f35caSApple OSS Distributions }
489*c54f35caSApple OSS Distributions 
490*c54f35caSApple OSS Distributions /*
491*c54f35caSApple OSS Distributions  * Test 2: Test turnstile boosting for temp owner ports for posix_spawn and exec.
492*c54f35caSApple OSS Distributions  *
493*c54f35caSApple OSS Distributions  * Create a port with sync IPC push and then pass the port to posix_spawn as a watch port and
494*c54f35caSApple OSS Distributions  * test that spawned binary has the temp owner push of the port. The spawned binary will exec
495*c54f35caSApple OSS Distributions  * and verify that it still has the push.
496*c54f35caSApple OSS Distributions  */
497*c54f35caSApple OSS Distributions T_DECL(posix_spawn_exec_basic_priority, "Basic posix spawn/exec temp owner priority test", T_META_ASROOT(YES))
498*c54f35caSApple OSS Distributions {
499*c54f35caSApple OSS Distributions 	mach_port_t port;
500*c54f35caSApple OSS Distributions 	pid_t pid;
501*c54f35caSApple OSS Distributions 	int stat;
502*c54f35caSApple OSS Distributions 
503*c54f35caSApple OSS Distributions 	port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
504*c54f35caSApple OSS Distributions 	pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "EXEC", &port, 1);
505*c54f35caSApple OSS Distributions 
506*c54f35caSApple OSS Distributions 	waitpid(pid, &stat, 0);
507*c54f35caSApple OSS Distributions 
508*c54f35caSApple OSS Distributions 	T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
509*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(WEXITSTATUS(stat), 47, "spawn/exec did not properly boost main thread");
510*c54f35caSApple OSS Distributions 	T_END;
511*c54f35caSApple OSS Distributions }
512*c54f35caSApple OSS Distributions 
513*c54f35caSApple OSS Distributions /*
514*c54f35caSApple OSS Distributions  * Test 3: Test turnstile boosting for temp owner ports for posix_spawn and set exec.
515*c54f35caSApple OSS Distributions  *
516*c54f35caSApple OSS Distributions  * Create a port with sync IPC push and then pass the port to posix_spawn as a watch port and
517*c54f35caSApple OSS Distributions  * test that spawned binary has the temp owner push of the port. The spawned binary will
518*c54f35caSApple OSS Distributions  * posix_spawn set exec and verify that it still has the push.
519*c54f35caSApple OSS Distributions  */
520*c54f35caSApple OSS Distributions T_DECL(posix_spawn_set_exec_basic_priority, "Basic posix spawn set exec temp owner priority test", T_META_ASROOT(YES))
521*c54f35caSApple OSS Distributions {
522*c54f35caSApple OSS Distributions 	mach_port_t port;
523*c54f35caSApple OSS Distributions 	pid_t pid;
524*c54f35caSApple OSS Distributions 	int stat;
525*c54f35caSApple OSS Distributions 
526*c54f35caSApple OSS Distributions 	port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
527*c54f35caSApple OSS Distributions 	pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "SETEXEC", &port, 1);
528*c54f35caSApple OSS Distributions 
529*c54f35caSApple OSS Distributions 	waitpid(pid, &stat, 0);
530*c54f35caSApple OSS Distributions 
531*c54f35caSApple OSS Distributions 	T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
532*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(WEXITSTATUS(stat), 47, "spawn set exec did not properly boost main thread");
533*c54f35caSApple OSS Distributions 	T_END;
534*c54f35caSApple OSS Distributions }
535*c54f35caSApple OSS Distributions 
536*c54f35caSApple OSS Distributions /*
537*c54f35caSApple OSS Distributions  * Test 4: Test turnstile boosting for temp owner ports for posix_spawn and set exec.
538*c54f35caSApple OSS Distributions  *
539*c54f35caSApple OSS Distributions  * Create a port with sync IPC push and then pass the port to posix_spawn as a watch port and
540*c54f35caSApple OSS Distributions  * test that spawned binary has the temp owner push of the port. The spawned binary already
541*c54f35caSApple OSS Distributions  * having the temp owner push will try to do set exec with watchports which should fail.
542*c54f35caSApple OSS Distributions  */
543*c54f35caSApple OSS Distributions T_DECL(posix_spawn_set_exec_with_more_ports, "posix spawn set exec with more watch ports", T_META_ASROOT(YES))
544*c54f35caSApple OSS Distributions {
545*c54f35caSApple OSS Distributions 	mach_port_t port;
546*c54f35caSApple OSS Distributions 	pid_t pid;
547*c54f35caSApple OSS Distributions 	int stat;
548*c54f35caSApple OSS Distributions 
549*c54f35caSApple OSS Distributions 	port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
550*c54f35caSApple OSS Distributions 	pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "SETEXEC_PORTS", &port, 1);
551*c54f35caSApple OSS Distributions 
552*c54f35caSApple OSS Distributions 	waitpid(pid, &stat, 0);
553*c54f35caSApple OSS Distributions 
554*c54f35caSApple OSS Distributions 	T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
555*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(WEXITSTATUS(stat), EINVAL, "spawn set exec did not error out when watchports were passed to already boosted process");
556*c54f35caSApple OSS Distributions 	T_END;
557*c54f35caSApple OSS Distributions }
558*c54f35caSApple OSS Distributions 
559*c54f35caSApple OSS Distributions /*
560*c54f35caSApple OSS Distributions  * Test 5: Test turnstile boosting for temp owner ports for multiple posix_spawns.
561*c54f35caSApple OSS Distributions  *
562*c54f35caSApple OSS Distributions  * Create a port with sync IPC push and then pass the port to posix_spawn as a watch port, then
563*c54f35caSApple OSS Distributions  * pass the same port as a watchport to another posix_spawn and verify that the boost was
564*c54f35caSApple OSS Distributions  * transferred to the new process.
565*c54f35caSApple OSS Distributions  */
566*c54f35caSApple OSS Distributions T_DECL(posix_spawn_multiple, "multiple posix_spawn with same watchport", T_META_ASROOT(YES))
567*c54f35caSApple OSS Distributions {
568*c54f35caSApple OSS Distributions 	mach_port_t port;
569*c54f35caSApple OSS Distributions 	pid_t pid1, pid2;
570*c54f35caSApple OSS Distributions 	int stat1, stat2;
571*c54f35caSApple OSS Distributions 
572*c54f35caSApple OSS Distributions 	port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
573*c54f35caSApple OSS Distributions 	pid1 = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "WAIT", &port, 1);
574*c54f35caSApple OSS Distributions 
575*c54f35caSApple OSS Distributions 	/* Let the child 1 execute a little, the sleep here is optional */
576*c54f35caSApple OSS Distributions 	sleep(2);
577*c54f35caSApple OSS Distributions 
578*c54f35caSApple OSS Distributions 	pid2 = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "EXIT", &port, 1);
579*c54f35caSApple OSS Distributions 
580*c54f35caSApple OSS Distributions 	waitpid(pid2, &stat2, 0);
581*c54f35caSApple OSS Distributions 	waitpid(pid1, &stat1, 0);
582*c54f35caSApple OSS Distributions 
583*c54f35caSApple OSS Distributions 	T_QUIET; T_LOG("The return stat for child 1 is is %d", WEXITSTATUS(stat1));
584*c54f35caSApple OSS Distributions 	T_QUIET; T_LOG("The return stat for child 2 is is %d", WEXITSTATUS(stat2));
585*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(WEXITSTATUS(stat2), 47, "spawn of multiple processes with same watchport did not transfer the boost correctly");
586*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(WEXITSTATUS(stat1), 31, "spawn of multiple processes with same watchport did not transfer the boost correctly");
587*c54f35caSApple OSS Distributions 	T_END;
588*c54f35caSApple OSS Distributions }
589*c54f35caSApple OSS Distributions 
590*c54f35caSApple OSS Distributions /*
591*c54f35caSApple OSS Distributions  * Test 6: Test turnstile boosting for temp owner ports for posix_spawn for dead port.
592*c54f35caSApple OSS Distributions  *
593*c54f35caSApple OSS Distributions  * Create a port with sync IPC push and then pass the port to posix_spawn as a watch port and
594*c54f35caSApple OSS Distributions  * test that spawned binary has the temp owner push of the port. Destroy the port and verify
595*c54f35caSApple OSS Distributions  * the temp owner push has gone away.
596*c54f35caSApple OSS Distributions  */
597*c54f35caSApple OSS Distributions T_DECL(posix_spawn_dead_reply_port, "posix spawn with reply port destory", T_META_ASROOT(YES))
598*c54f35caSApple OSS Distributions {
599*c54f35caSApple OSS Distributions 	mach_port_t port;
600*c54f35caSApple OSS Distributions 	kern_return_t kr;
601*c54f35caSApple OSS Distributions 	pid_t pid;
602*c54f35caSApple OSS Distributions 	int stat;
603*c54f35caSApple OSS Distributions 
604*c54f35caSApple OSS Distributions 	port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
605*c54f35caSApple OSS Distributions 	pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "WAIT", &port, 1);
606*c54f35caSApple OSS Distributions 
607*c54f35caSApple OSS Distributions 	/* Let the child execute a little, the sleep here is optional */
608*c54f35caSApple OSS Distributions 	sleep(2);
609*c54f35caSApple OSS Distributions 
610*c54f35caSApple OSS Distributions 	/* Destory the special reply port */
611*c54f35caSApple OSS Distributions 	kr = mach_port_mod_refs(mach_task_self(), sr_port, MACH_PORT_RIGHT_RECEIVE, -1);
612*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "posix_spaw_dead_port  mach_port_mod_refs");
613*c54f35caSApple OSS Distributions 
614*c54f35caSApple OSS Distributions 	waitpid(pid, &stat, 0);
615*c54f35caSApple OSS Distributions 
616*c54f35caSApple OSS Distributions 	T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
617*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(WEXITSTATUS(stat), 31, "Temp owner boost was not removed on port death");
618*c54f35caSApple OSS Distributions 	T_END;
619*c54f35caSApple OSS Distributions }
620*c54f35caSApple OSS Distributions 
621*c54f35caSApple OSS Distributions /*
622*c54f35caSApple OSS Distributions  * Test 7: Test turnstile boosting for temp owner ports for posix_spawn for dead port.
623*c54f35caSApple OSS Distributions  *
624*c54f35caSApple OSS Distributions  * Create a port with sync IPC push and then pass the port to posix_spawn as a watch port and
625*c54f35caSApple OSS Distributions  * test that spawned binary has the temp owner push of the port. Destroy the port and verify
626*c54f35caSApple OSS Distributions  * the temp owner push has gone.
627*c54f35caSApple OSS Distributions  */
628*c54f35caSApple OSS Distributions T_DECL(posix_spawn_dead_port, "posix spawn with port destory", T_META_ASROOT(YES))
629*c54f35caSApple OSS Distributions {
630*c54f35caSApple OSS Distributions 	mach_port_t port;
631*c54f35caSApple OSS Distributions 	kern_return_t kr;
632*c54f35caSApple OSS Distributions 	pid_t pid;
633*c54f35caSApple OSS Distributions 	int stat;
634*c54f35caSApple OSS Distributions 
635*c54f35caSApple OSS Distributions 	port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
636*c54f35caSApple OSS Distributions 	pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "WAIT", &port, 1);
637*c54f35caSApple OSS Distributions 
638*c54f35caSApple OSS Distributions 	/* Destory the port */
639*c54f35caSApple OSS Distributions 	kr = mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_RECEIVE, -1);
640*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "posix_spaw_dead_port  mach_port_mod_refs");
641*c54f35caSApple OSS Distributions 
642*c54f35caSApple OSS Distributions 	waitpid(pid, &stat, 0);
643*c54f35caSApple OSS Distributions 
644*c54f35caSApple OSS Distributions 	T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
645*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(WEXITSTATUS(stat), 31, "Temp owner boost was not removed on port death");
646*c54f35caSApple OSS Distributions 	T_END;
647*c54f35caSApple OSS Distributions }
648*c54f35caSApple OSS Distributions 
649*c54f35caSApple OSS Distributions /*
650*c54f35caSApple OSS Distributions  * Test 8: Test turnstile boosting for temp owner ports for posix_spawn when port is copied in.
651*c54f35caSApple OSS Distributions  *
652*c54f35caSApple OSS Distributions  * Create a port with sync IPC push and then pass the port to posix_spawn as a watch port and
653*c54f35caSApple OSS Distributions  * test that spawned binary has the temp owner push of the port. Copyin the port and verify
654*c54f35caSApple OSS Distributions  * the temp owner push has gone.
655*c54f35caSApple OSS Distributions  */
656*c54f35caSApple OSS Distributions T_DECL(posix_spawn_copyin_port, "posix spawn with copyin port", T_META_ASROOT(YES))
657*c54f35caSApple OSS Distributions {
658*c54f35caSApple OSS Distributions 	mach_port_t port;
659*c54f35caSApple OSS Distributions 	pid_t pid;
660*c54f35caSApple OSS Distributions 	int stat;
661*c54f35caSApple OSS Distributions 
662*c54f35caSApple OSS Distributions 	port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
663*c54f35caSApple OSS Distributions 	pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "WAIT", &port, 1);
664*c54f35caSApple OSS Distributions 
665*c54f35caSApple OSS Distributions 	/* Let the child execute a little, the sleep here is optional */
666*c54f35caSApple OSS Distributions 	sleep(2);
667*c54f35caSApple OSS Distributions 
668*c54f35caSApple OSS Distributions 	/* Copyin the port in another port */
669*c54f35caSApple OSS Distributions 	create_port_and_copyin_a_port(port);
670*c54f35caSApple OSS Distributions 
671*c54f35caSApple OSS Distributions 	waitpid(pid, &stat, 0);
672*c54f35caSApple OSS Distributions 
673*c54f35caSApple OSS Distributions 	T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
674*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(WEXITSTATUS(stat), 31, "Temp owner boost was not removed on port copyin");
675*c54f35caSApple OSS Distributions 	T_END;
676*c54f35caSApple OSS Distributions }
677*c54f35caSApple OSS Distributions 
678*c54f35caSApple OSS Distributions /*
679*c54f35caSApple OSS Distributions  * Test 9: Test turnstile boosting for temp owner ports for posix_spawn with multiple ports.
680*c54f35caSApple OSS Distributions  *
681*c54f35caSApple OSS Distributions  * Create multiple ports with sync IPC push and then pass the port to posix_spawn as watch ports and
682*c54f35caSApple OSS Distributions  * test that spawned binary has the temp owner push of the ports. Copyin ports one by one and verify
683*c54f35caSApple OSS Distributions  * the push has gone.
684*c54f35caSApple OSS Distributions  */
685*c54f35caSApple OSS Distributions T_DECL(posix_spawn_multiple_port, "posix spawn with multiple ports", T_META_ASROOT(YES))
686*c54f35caSApple OSS Distributions {
687*c54f35caSApple OSS Distributions 	mach_port_t port[2];
688*c54f35caSApple OSS Distributions 	pid_t pid;
689*c54f35caSApple OSS Distributions 	int stat;
690*c54f35caSApple OSS Distributions 
691*c54f35caSApple OSS Distributions 	port[0] = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
692*c54f35caSApple OSS Distributions 	port[1] = get_sync_push_port_at_qos(QOS_CLASS_USER_INITIATED);
693*c54f35caSApple OSS Distributions 	pid = posix_spawn_child_with_watch_ports("prioritize_process_launch_helper", "MULTIWAIT", port, 2);
694*c54f35caSApple OSS Distributions 
695*c54f35caSApple OSS Distributions 	/* Let the child execute a little, the sleep here is optional */
696*c54f35caSApple OSS Distributions 	sleep(2);
697*c54f35caSApple OSS Distributions 
698*c54f35caSApple OSS Distributions 	/* Copyin the port in another port */
699*c54f35caSApple OSS Distributions 	create_port_and_copyin_a_port(port[0]);
700*c54f35caSApple OSS Distributions 
701*c54f35caSApple OSS Distributions 	/* Let the child execute a little, the sleep here is optional */
702*c54f35caSApple OSS Distributions 	sleep(2);
703*c54f35caSApple OSS Distributions 
704*c54f35caSApple OSS Distributions 	/* Copyin the port in another port */
705*c54f35caSApple OSS Distributions 	create_port_and_copyin_a_port(port[1]);
706*c54f35caSApple OSS Distributions 
707*c54f35caSApple OSS Distributions 	waitpid(pid, &stat, 0);
708*c54f35caSApple OSS Distributions 
709*c54f35caSApple OSS Distributions 	T_QUIET; T_LOG("The return stat is %d", WEXITSTATUS(stat));
710*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(WEXITSTATUS(stat), 31, "Temp owner boost did not work correctly with multiple ports");
711*c54f35caSApple OSS Distributions 	T_END;
712*c54f35caSApple OSS Distributions }
713*c54f35caSApple OSS Distributions 
714*c54f35caSApple OSS Distributions /*
715*c54f35caSApple OSS Distributions  * Test 10: Test turnstile boosting for temp owner ports for posix_spawn when port attached to a knote.
716*c54f35caSApple OSS Distributions  *
717*c54f35caSApple OSS Distributions  * Create a port with sync IPC push attach a workloop knote to it, send a message on the port, then in the
718*c54f35caSApple OSS Distributions  * servicer pass the port to posix_spawn as a watch port and test that spawned binary has the temp owner
719*c54f35caSApple OSS Distributions  * push of the port and the servicer looses the boost.
720*c54f35caSApple OSS Distributions  */
721*c54f35caSApple OSS Distributions T_DECL(posix_spawn_knote, "posix spawn with temp owner port attached to knote", T_META_ASROOT(YES))
722*c54f35caSApple OSS Distributions {
723*c54f35caSApple OSS Distributions 	mach_port_t port;
724*c54f35caSApple OSS Distributions 
725*c54f35caSApple OSS Distributions 	port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
726*c54f35caSApple OSS Distributions 
727*c54f35caSApple OSS Distributions 	/* attach port to a workloop */
728*c54f35caSApple OSS Distributions 	register_workloop_for_port(port, workloop_cb_test_intransit, MACH_RCV_OPTIONS);
729*c54f35caSApple OSS Distributions 
730*c54f35caSApple OSS Distributions 	/* send a message on port to activate workloop handler */
731*c54f35caSApple OSS Distributions 	send(port, MACH_PORT_NULL, MACH_PORT_NULL,
732*c54f35caSApple OSS Distributions 	    mach_msg_priority_encode(0, THREAD_QOS_LEGACY, 0), 0, MACH_MSG_TYPE_COPY_SEND);
733*c54f35caSApple OSS Distributions 	sigsuspend(0);
734*c54f35caSApple OSS Distributions }
735*c54f35caSApple OSS Distributions 
736*c54f35caSApple OSS Distributions /*
737*c54f35caSApple OSS Distributions  * Test 11: Test turnstile boosting for temp owner ports for posix_spawn when port attached to a knote.
738*c54f35caSApple OSS Distributions  *
739*c54f35caSApple OSS Distributions  * Create a port with sync IPC push attach a workloop knote to it, send a message on the port, then in the
740*c54f35caSApple OSS Distributions  * servicer pass the port to posix_spawn as a watch port and test that spawned binary has the temp owner
741*c54f35caSApple OSS Distributions  * push of the port and the servicer looses the boost, verify that once the spawned binary dies, the servicer
742*c54f35caSApple OSS Distributions  * gets the push.
743*c54f35caSApple OSS Distributions  */
744*c54f35caSApple OSS Distributions T_DECL(posix_spawn_knote_ret, "posix spawn with temp owner port attached to knote with spawned binary dead", T_META_ASROOT(YES))
745*c54f35caSApple OSS Distributions {
746*c54f35caSApple OSS Distributions 	mach_port_t port;
747*c54f35caSApple OSS Distributions 
748*c54f35caSApple OSS Distributions 	port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
749*c54f35caSApple OSS Distributions 
750*c54f35caSApple OSS Distributions 	register_workloop_for_port(port, workloop_cb_test_knote_kill, MACH_RCV_OPTIONS);
751*c54f35caSApple OSS Distributions 
752*c54f35caSApple OSS Distributions 	/* send a message on port to activate workloop handler */
753*c54f35caSApple OSS Distributions 	send(port, MACH_PORT_NULL, MACH_PORT_NULL,
754*c54f35caSApple OSS Distributions 	    mach_msg_priority_encode(0, THREAD_QOS_LEGACY, 0), 0, MACH_MSG_TYPE_COPY_SEND);
755*c54f35caSApple OSS Distributions 	sigsuspend(0);
756*c54f35caSApple OSS Distributions }
757*c54f35caSApple OSS Distributions 
758*c54f35caSApple OSS Distributions /*
759*c54f35caSApple OSS Distributions  * Test 12: Test turnstile boosting for temp owner ports and mach msg option for sync bootstrap_checkin.
760*c54f35caSApple OSS Distributions  *
761*c54f35caSApple OSS Distributions  * Create a port with sync IPC push attach a workloop knote to it, send a message on the port, then in the
762*c54f35caSApple OSS Distributions  * servicer pass the port to posix_spawn as a watch port and test that spawned binary has the temp owner
763*c54f35caSApple OSS Distributions  * push of the port and the servicer looses the boost, the spawn binary then does a sync bootstrap_checkin
764*c54f35caSApple OSS Distributions  * with test binary to get the receive right and verify that is still has the boost.
765*c54f35caSApple OSS Distributions  */
766*c54f35caSApple OSS Distributions T_DECL(mach_msg_sync_boostrap_checkin, "test mach msg option for sync bootstrap_checkin", T_META_ASROOT(YES))
767*c54f35caSApple OSS Distributions {
768*c54f35caSApple OSS Distributions 	mach_port_t port;
769*c54f35caSApple OSS Distributions 	mach_port_t sync_port;
770*c54f35caSApple OSS Distributions 	kern_return_t kr;
771*c54f35caSApple OSS Distributions 
772*c54f35caSApple OSS Distributions 	port = get_sync_push_port_at_qos(QOS_CLASS_USER_INTERACTIVE);
773*c54f35caSApple OSS Distributions 
774*c54f35caSApple OSS Distributions 	register_workloop_for_port(port, workloop_cb_test_sync_bootstrap, MACH_RCV_SYNC_PEEK);
775*c54f35caSApple OSS Distributions 
776*c54f35caSApple OSS Distributions 	/* Create a mach port for spawned binary to do bootstrap checkin */
777*c54f35caSApple OSS Distributions 	kr = mach_port_allocate(mach_task_self(),
778*c54f35caSApple OSS Distributions 	    MACH_PORT_RIGHT_RECEIVE,
779*c54f35caSApple OSS Distributions 	    &sync_port);
780*c54f35caSApple OSS Distributions 
781*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_msg_sync_boostrap_checkin mach_port_allocate");
782*c54f35caSApple OSS Distributions 
783*c54f35caSApple OSS Distributions 	kr = mach_port_insert_right(mach_task_self(),
784*c54f35caSApple OSS Distributions 	    sync_port,
785*c54f35caSApple OSS Distributions 	    sync_port,
786*c54f35caSApple OSS Distributions 	    MACH_MSG_TYPE_MAKE_SEND);
787*c54f35caSApple OSS Distributions 
788*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_msg_sync_boostrap_checkin mach_port_insert_right");
789*c54f35caSApple OSS Distributions 
790*c54f35caSApple OSS Distributions 	kr = mach_port_mod_refs(mach_task_self(), sync_port, MACH_PORT_RIGHT_SEND, 1);
791*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_msg_sync_boostrap_checkin mach_port_mod_refs");
792*c54f35caSApple OSS Distributions 
793*c54f35caSApple OSS Distributions 	register_workloop_for_port(sync_port, NULL, MACH_RCV_OPTIONS);
794*c54f35caSApple OSS Distributions 
795*c54f35caSApple OSS Distributions 	/* Stash the port in task to make sure child also gets it */
796*c54f35caSApple OSS Distributions 	kr = mach_ports_register(mach_task_self(), &sync_port, 1);
797*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_msg_sync_boostrap_checkin mach_ports_register");
798*c54f35caSApple OSS Distributions 
799*c54f35caSApple OSS Distributions 	/* send a message on port to activate workloop handler */
800*c54f35caSApple OSS Distributions 	send(port, MACH_PORT_NULL, MACH_PORT_NULL,
801*c54f35caSApple OSS Distributions 	    mach_msg_priority_encode(0, THREAD_QOS_LEGACY, 0), 0, MACH_MSG_TYPE_COPY_SEND);
802*c54f35caSApple OSS Distributions 	sigsuspend(0);
803*c54f35caSApple OSS Distributions }
804