1 #include <darwintest.h>
7 #include <mach/mach_types.h>
9 #include <mach/task_policy.h>
11 extern char **environ
;
13 int task_inspect_for_pid(mach_port_name_t target_tport
, int pid
, mach_port_name_t
*t
);
14 int task_for_pid(mach_port_name_t target_tport
, int pid
, mach_port_name_t
*t
);
15 int task_name_for_pid(mach_port_name_t target_tport
, int pid
, mach_port_name_t
*t
);
17 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
19 #if defined(UNENTITLED)
21 T_DECL(task_policy_set_task_name
, "task_policy_set with task name (not entitled)")
23 struct task_qos_policy qosinfo
= {
24 .task_latency_qos_tier
= LATENCY_QOS_TIER_0
,
25 .task_throughput_qos_tier
= THROUGHPUT_QOS_TIER_0
,
27 task_name_t task_name
= TASK_NAME_NULL
;
30 T_ASSERT_MACH_SUCCESS(task_name_for_pid(mach_task_self(), getpid(),
34 T_ASSERT_MACH_ERROR(task_policy_set(task_name
,
36 (task_policy_t
)&qosinfo
,
37 TASK_QOS_POLICY_COUNT
),
38 KERN_INVALID_ARGUMENT
, NULL
);
41 T_DECL(task_policy_set_task
, "task_policy_set with task (not entitled)")
43 struct task_qos_policy qosinfo
= {
44 .task_latency_qos_tier
= LATENCY_QOS_TIER_0
,
45 .task_throughput_qos_tier
= THROUGHPUT_QOS_TIER_0
,
48 T_ASSERT_MACH_SUCCESS(task_policy_set(mach_task_self(),
50 (task_policy_t
)&qosinfo
,
51 TASK_QOS_POLICY_COUNT
),
55 T_DECL(task_policy_set_inspect
, "task_policy_set with task inspect (not entitled)")
57 struct task_qos_policy qosinfo
= {
58 .task_latency_qos_tier
= LATENCY_QOS_TIER_0
,
59 .task_throughput_qos_tier
= THROUGHPUT_QOS_TIER_0
,
61 task_inspect_t task_inspect
= TASK_INSPECT_NULL
;
64 T_ASSERT_MACH_SUCCESS(task_inspect_for_pid(mach_task_self(), getpid(),
65 &task_inspect
), NULL
);
69 T_ASSERT_MACH_ERROR(task_policy_set(task_inspect
,
71 (task_policy_t
)&qosinfo
,
72 TASK_QOS_POLICY_COUNT
),
73 KERN_INVALID_ARGUMENT
, NULL
);
76 T_DECL(task_policy_set_foreign_task
, "task_policy_set for foreign task (not entitled)", T_META_ASROOT(true))
78 struct task_qos_policy qosinfo
= {
79 .task_latency_qos_tier
= LATENCY_QOS_TIER_0
,
80 .task_throughput_qos_tier
= THROUGHPUT_QOS_TIER_0
,
82 task_t task
= TASK_NULL
;
83 kern_return_t ret
= KERN_FAILURE
;
84 char *args
[] = { "sleep", "10", NULL
};
89 ret
= posix_spawnp(&pid
, args
[0], NULL
, NULL
, args
, environ
);
90 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "spawning sleep 10");
92 T_ASSERT_MACH_SUCCESS(task_for_pid(mach_task_self(), pid
,
96 T_ASSERT_MACH_SUCCESS(task_policy_set(task
,
98 (task_policy_t
)&qosinfo
,
99 TASK_QOS_POLICY_COUNT
),
102 ret
= kill(pid
, SIGTERM
);
103 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "killing sleep");
106 T_DECL(task_policy_set_foreign_task_name
, "task_policy_set for foreign task name (not entitled)", T_META_ASROOT(true))
108 struct task_qos_policy qosinfo
= {
109 .task_latency_qos_tier
= LATENCY_QOS_TIER_0
,
110 .task_throughput_qos_tier
= THROUGHPUT_QOS_TIER_0
,
112 task_name_t task_name
= TASK_NAME_NULL
;
113 kern_return_t ret
= KERN_FAILURE
;
114 char *args
[] = { "sleep", "10", NULL
};
119 ret
= posix_spawnp(&pid
, args
[0], NULL
, NULL
, args
, environ
);
120 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "spawning sleep 10");
122 T_ASSERT_MACH_SUCCESS(task_name_for_pid(mach_task_self(), pid
,
126 T_ASSERT_MACH_ERROR(task_policy_set(task_name
,
127 TASK_BASE_QOS_POLICY
,
128 (task_policy_t
)&qosinfo
,
129 TASK_QOS_POLICY_COUNT
),
130 KERN_INVALID_ARGUMENT
, NULL
);
132 ret
= kill(pid
, SIGTERM
);
133 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "killing sleep");
136 T_DECL(task_policy_set_foreign_task_inspect
, "task_policy_set for foreign task inspect (not entitled)", T_META_ASROOT(true))
138 struct task_qos_policy qosinfo
= {
139 .task_latency_qos_tier
= LATENCY_QOS_TIER_0
,
140 .task_throughput_qos_tier
= THROUGHPUT_QOS_TIER_0
,
142 task_inspect_t task_inspect
= TASK_INSPECT_NULL
;
143 kern_return_t ret
= KERN_FAILURE
;
144 char *args
[] = { "sleep", "10", NULL
};
149 ret
= posix_spawnp(&pid
, args
[0], NULL
, NULL
, args
, environ
);
150 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "spawning sleep 10");
152 T_ASSERT_MACH_SUCCESS(task_inspect_for_pid(mach_task_self(), pid
,
153 &task_inspect
), NULL
);
156 T_ASSERT_MACH_ERROR(task_policy_set(task_inspect
,
157 TASK_BASE_QOS_POLICY
,
158 (task_policy_t
)&qosinfo
,
159 TASK_QOS_POLICY_COUNT
),
160 KERN_INVALID_ARGUMENT
, NULL
);
162 ret
= kill(pid
, SIGTERM
);
163 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "killing sleep");
166 T_DECL(task_policy_get_name
, "task_policy_get with task name (not entitled)")
168 task_name_t task_name
= TASK_NAME_NULL
;
169 struct task_category_policy role
[TASK_CATEGORY_POLICY_COUNT
];
170 mach_msg_type_number_t count
= TASK_CATEGORY_POLICY_COUNT
;
171 boolean_t get_default
= FALSE
;
174 T_ASSERT_MACH_SUCCESS(task_name_for_pid(mach_task_self(), getpid(),
178 T_ASSERT_MACH_ERROR(task_policy_get(task_name
,
179 TASK_CATEGORY_POLICY
,
183 KERN_INVALID_ARGUMENT
, NULL
);
186 T_DECL(task_policy_get_task
, "task_policy_get with task (not entitled)")
188 struct task_category_policy role
[TASK_CATEGORY_POLICY_COUNT
];
189 mach_msg_type_number_t count
= TASK_CATEGORY_POLICY_COUNT
;
190 boolean_t get_default
= FALSE
;
192 T_ASSERT_MACH_SUCCESS(task_policy_get(mach_task_self(),
193 TASK_CATEGORY_POLICY
,
200 T_DECL(task_policy_get_inspect
, "task_policy_get with task inspect (not entitled)")
202 task_inspect_t task_inspect
= TASK_INSPECT_NULL
;
203 struct task_category_policy role
[TASK_CATEGORY_POLICY_COUNT
];
204 mach_msg_type_number_t count
= TASK_CATEGORY_POLICY_COUNT
;
205 boolean_t get_default
= FALSE
;
208 T_ASSERT_MACH_SUCCESS(task_inspect_for_pid(mach_task_self(), getpid(),
209 &task_inspect
), NULL
);
212 T_ASSERT_MACH_SUCCESS(task_policy_get(task_inspect
,
213 TASK_CATEGORY_POLICY
,
220 T_DECL(task_policy_get_foreign_task_inspect
, "task_policy_get for foreign task inspect (not entitled)", T_META_ASROOT(true))
222 task_inspect_t task_inspect
= TASK_INSPECT_NULL
;
223 struct task_category_policy role
[TASK_CATEGORY_POLICY_COUNT
];
224 mach_msg_type_number_t count
= TASK_CATEGORY_POLICY_COUNT
;
225 boolean_t get_default
= FALSE
;
226 kern_return_t ret
= KERN_FAILURE
;
227 char *args
[] = { "sleep", "10", NULL
};
232 ret
= posix_spawnp(&pid
, args
[0], NULL
, NULL
, args
, environ
);
233 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "spawning sleep 10");
235 T_ASSERT_MACH_SUCCESS(task_inspect_for_pid(mach_task_self(), pid
,
236 &task_inspect
), NULL
);
239 T_ASSERT_MACH_SUCCESS(task_policy_get(task_inspect
,
240 TASK_CATEGORY_POLICY
,
246 ret
= kill(pid
, SIGTERM
);
247 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "killing sleep");
250 T_DECL(task_policy_get_foreign_task
, "task_policy_get for foreign task (not entitled)", T_META_ASROOT(true))
252 task_t task
= TASK_NULL
;
253 struct task_category_policy role
[TASK_CATEGORY_POLICY_COUNT
];
254 mach_msg_type_number_t count
= TASK_CATEGORY_POLICY_COUNT
;
255 boolean_t get_default
= FALSE
;
256 kern_return_t ret
= KERN_FAILURE
;
257 char *args
[] = { "sleep", "10", NULL
};
262 ret
= posix_spawnp(&pid
, args
[0], NULL
, NULL
, args
, environ
);
263 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "spawning sleep 10");
265 T_ASSERT_MACH_SUCCESS(task_for_pid(mach_task_self(), pid
,
269 T_ASSERT_MACH_SUCCESS(task_policy_get(task
,
270 TASK_CATEGORY_POLICY
,
276 ret
= kill(pid
, SIGTERM
);
277 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "killing sleep");
280 T_DECL(task_policy_get_foreign_task_name
, "task_policy_get for foreign task name (not entitled)")
282 task_name_t task_name
= TASK_NAME_NULL
;
283 struct task_category_policy role
[TASK_CATEGORY_POLICY_COUNT
];
284 mach_msg_type_number_t count
= TASK_CATEGORY_POLICY_COUNT
;
285 boolean_t get_default
= FALSE
;
286 kern_return_t ret
= KERN_FAILURE
;
287 char *args
[] = { "sleep", "10", NULL
};
292 ret
= posix_spawnp(&pid
, args
[0], NULL
, NULL
, args
, environ
);
293 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "spawning sleep 10");
295 T_ASSERT_MACH_SUCCESS(task_name_for_pid(mach_task_self(), pid
,
299 T_ASSERT_MACH_ERROR(task_policy_get(task_name
,
300 TASK_CATEGORY_POLICY
,
304 KERN_INVALID_ARGUMENT
, NULL
);
306 ret
= kill(pid
, SIGTERM
);
307 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "killing sleep");
312 T_DECL(task_policy_set_task_name_entitled
, "task_policy_set with task name (entitled)", T_META_ASROOT(true), T_META_ASROOT(true))
314 struct task_qos_policy qosinfo
= {
315 .task_latency_qos_tier
= LATENCY_QOS_TIER_0
,
316 .task_throughput_qos_tier
= THROUGHPUT_QOS_TIER_0
,
318 task_name_t task_name
= TASK_NAME_NULL
;
321 T_ASSERT_MACH_SUCCESS(task_name_for_pid(mach_task_self(), getpid(),
325 T_ASSERT_MACH_SUCCESS(task_policy_set(task_name
,
326 TASK_BASE_QOS_POLICY
,
327 (task_policy_t
)&qosinfo
,
328 TASK_QOS_POLICY_COUNT
),
332 T_DECL(task_policy_set_task_entitled
, "task_policy_set with task (entitled)")
334 struct task_qos_policy qosinfo
= {
335 .task_latency_qos_tier
= LATENCY_QOS_TIER_0
,
336 .task_throughput_qos_tier
= THROUGHPUT_QOS_TIER_0
,
339 T_ASSERT_MACH_SUCCESS(task_policy_set(mach_task_self(),
340 TASK_BASE_QOS_POLICY
,
341 (task_policy_t
)&qosinfo
,
342 TASK_QOS_POLICY_COUNT
),
346 T_DECL(task_policy_set_inspect_entitled
, "task_policy_set with task inspect (entitled)")
348 struct task_qos_policy qosinfo
= {
349 .task_latency_qos_tier
= LATENCY_QOS_TIER_0
,
350 .task_throughput_qos_tier
= THROUGHPUT_QOS_TIER_0
,
352 task_inspect_t task_inspect
= TASK_INSPECT_NULL
;
355 T_ASSERT_MACH_SUCCESS(task_inspect_for_pid(mach_task_self(), getpid(),
356 &task_inspect
), NULL
);
359 T_ASSERT_MACH_SUCCESS(task_policy_set(task_inspect
,
360 TASK_BASE_QOS_POLICY
,
361 (task_policy_t
)&qosinfo
,
362 TASK_QOS_POLICY_COUNT
),
366 T_DECL(task_policy_set_foreign_task_entitled
, "task_policy_set for foreign task (entitled)", T_META_ASROOT(true))
368 struct task_qos_policy qosinfo
= {
369 .task_latency_qos_tier
= LATENCY_QOS_TIER_0
,
370 .task_throughput_qos_tier
= THROUGHPUT_QOS_TIER_0
,
372 task_t task
= TASK_NULL
;
373 kern_return_t ret
= KERN_FAILURE
;
374 char *args
[] = { "sleep", "10", NULL
};
379 ret
= posix_spawnp(&pid
, args
[0], NULL
, NULL
, args
, environ
);
380 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "spawning sleep 10");
382 T_ASSERT_MACH_SUCCESS(task_for_pid(mach_task_self(), pid
,
386 T_ASSERT_MACH_SUCCESS(task_policy_set(task
,
387 TASK_BASE_QOS_POLICY
,
388 (task_policy_t
)&qosinfo
,
389 TASK_QOS_POLICY_COUNT
),
392 ret
= kill(pid
, SIGTERM
);
393 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "killing sleep");
396 T_DECL(task_policy_set_foreign_task_name_entitled
, "task_policy_set for foreign task name (entitled)", T_META_ASROOT(true), T_META_ASROOT(true))
398 struct task_qos_policy qosinfo
= {
399 .task_latency_qos_tier
= LATENCY_QOS_TIER_0
,
400 .task_throughput_qos_tier
= THROUGHPUT_QOS_TIER_0
,
402 task_name_t task_name
= TASK_NAME_NULL
;
403 kern_return_t ret
= KERN_FAILURE
;
404 char *args
[] = { "sleep", "10", NULL
};
409 ret
= posix_spawnp(&pid
, args
[0], NULL
, NULL
, args
, environ
);
410 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "spawning sleep 10");
412 T_ASSERT_MACH_SUCCESS(task_name_for_pid(mach_task_self(), pid
,
416 T_ASSERT_MACH_SUCCESS(task_policy_set(task_name
,
417 TASK_BASE_QOS_POLICY
,
418 (task_policy_t
)&qosinfo
,
419 TASK_QOS_POLICY_COUNT
),
422 ret
= kill(pid
, SIGTERM
);
423 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "killing sleep");
426 T_DECL(task_policy_set_foreign_task_inspect_entitled
, "task_policy_set for foreign task inspect (entitled)", T_META_ASROOT(true))
428 struct task_qos_policy qosinfo
= {
429 .task_latency_qos_tier
= LATENCY_QOS_TIER_0
,
430 .task_throughput_qos_tier
= THROUGHPUT_QOS_TIER_0
,
432 task_inspect_t task_inspect
= TASK_INSPECT_NULL
;
433 kern_return_t ret
= KERN_FAILURE
;
434 char *args
[] = { "sleep", "10", NULL
};
439 ret
= posix_spawnp(&pid
, args
[0], NULL
, NULL
, args
, environ
);
440 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "spawning sleep 10");
442 T_ASSERT_MACH_SUCCESS(task_inspect_for_pid(mach_task_self(), pid
,
443 &task_inspect
), NULL
);
446 T_ASSERT_MACH_SUCCESS(task_policy_set(task_inspect
,
447 TASK_BASE_QOS_POLICY
,
448 (task_policy_t
)&qosinfo
,
449 TASK_QOS_POLICY_COUNT
),
452 ret
= kill(pid
, SIGTERM
);
453 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "killing sleep");
456 T_DECL(task_policy_get_name_entitled
, "task_policy_get with task name (entitled)", T_META_ASROOT(true))
458 task_name_t task_name
= TASK_NAME_NULL
;
459 struct task_category_policy role
[TASK_CATEGORY_POLICY_COUNT
];
460 mach_msg_type_number_t count
= TASK_CATEGORY_POLICY_COUNT
;
461 boolean_t get_default
= FALSE
;
464 T_ASSERT_MACH_SUCCESS(task_name_for_pid(mach_task_self(), getpid(),
468 T_ASSERT_MACH_SUCCESS(task_policy_get(task_name
,
469 TASK_CATEGORY_POLICY
,
476 T_DECL(task_policy_get_task_entitled
, "task_policy_get with task (entitled)")
478 struct task_category_policy role
[TASK_CATEGORY_POLICY_COUNT
];
479 mach_msg_type_number_t count
= TASK_CATEGORY_POLICY_COUNT
;
480 boolean_t get_default
= FALSE
;
482 T_ASSERT_MACH_SUCCESS(task_policy_get(mach_task_self(),
483 TASK_CATEGORY_POLICY
,
490 T_DECL(task_policy_get_inspect_entitled
, "task_policy_get with task inspect (entitled)")
492 task_inspect_t task_inspect
= TASK_INSPECT_NULL
;
493 struct task_category_policy role
[TASK_CATEGORY_POLICY_COUNT
];
494 mach_msg_type_number_t count
= TASK_CATEGORY_POLICY_COUNT
;
495 boolean_t get_default
= FALSE
;
498 T_ASSERT_MACH_SUCCESS(task_inspect_for_pid(mach_task_self(), getpid(),
499 &task_inspect
), NULL
);
502 T_ASSERT_MACH_SUCCESS(task_policy_get(task_inspect
,
503 TASK_CATEGORY_POLICY
,
510 T_DECL(task_policy_get_foreign_task_inspect_entitled
, "task_policy_get for foreign task inspect (entitled)", T_META_ASROOT(true))
512 task_inspect_t task_inspect
= TASK_INSPECT_NULL
;
513 struct task_category_policy role
[TASK_CATEGORY_POLICY_COUNT
];
514 mach_msg_type_number_t count
= TASK_CATEGORY_POLICY_COUNT
;
515 boolean_t get_default
= FALSE
;
516 kern_return_t ret
= KERN_FAILURE
;
517 char *args
[] = { "sleep", "10", NULL
};
522 ret
= posix_spawnp(&pid
, args
[0], NULL
, NULL
, args
, environ
);
523 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "spawning sleep 10");
525 T_ASSERT_MACH_SUCCESS(task_inspect_for_pid(mach_task_self(), pid
,
526 &task_inspect
), NULL
);
529 T_ASSERT_MACH_SUCCESS(task_policy_get(task_inspect
,
530 TASK_CATEGORY_POLICY
,
536 ret
= kill(pid
, SIGTERM
);
537 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "killing sleep");
540 T_DECL(task_policy_get_foreign_task_entitled
, "task_policy_get for foreign task (entitled)", T_META_ASROOT(true))
542 task_t task
= TASK_NULL
;
543 struct task_category_policy role
[TASK_CATEGORY_POLICY_COUNT
];
544 mach_msg_type_number_t count
= TASK_CATEGORY_POLICY_COUNT
;
545 boolean_t get_default
= FALSE
;
546 kern_return_t ret
= KERN_FAILURE
;
547 char *args
[] = { "sleep", "10", NULL
};
552 ret
= posix_spawnp(&pid
, args
[0], NULL
, NULL
, args
, environ
);
553 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "spawning sleep 10");
555 T_ASSERT_MACH_SUCCESS(task_for_pid(mach_task_self(), pid
,
559 T_ASSERT_MACH_SUCCESS(task_policy_get(task
,
560 TASK_CATEGORY_POLICY
,
566 ret
= kill(pid
, SIGTERM
);
567 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "killing sleep");
570 T_DECL(task_policy_get_foreign_task_name_entitled
, "task_policy_get for foreign task name (entitled)", T_META_ASROOT(true))
572 task_name_t task_name
= TASK_NAME_NULL
;
573 struct task_category_policy role
[TASK_CATEGORY_POLICY_COUNT
];
574 mach_msg_type_number_t count
= TASK_CATEGORY_POLICY_COUNT
;
575 boolean_t get_default
= FALSE
;
576 kern_return_t ret
= KERN_FAILURE
;
577 char *args
[] = { "sleep", "10", NULL
};
582 ret
= posix_spawnp(&pid
, args
[0], NULL
, NULL
, args
, environ
);
583 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "spawning sleep 10");
585 T_ASSERT_MACH_SUCCESS(task_name_for_pid(mach_task_self(), pid
,
589 T_ASSERT_MACH_SUCCESS(task_policy_get(task_name
,
590 TASK_CATEGORY_POLICY
,
596 ret
= kill(pid
, SIGTERM
);
597 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "killing sleep");
600 #endif /* UNENTITLED */