xref: /xnu-8796.141.3/tests/vm/memorystatus_freeze_test_entitled.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1 /*
2  * Freezer unit tests that require the memorystatus entitlement.
3  * All other freezer unit tests should go in vm/memorystatus_freeze_test.c
4  */
5 
6 #include <dispatch/dispatch.h>
7 #include <signal.h>
8 #include <sys/kern_memorystatus.h>
9 #include <sys/kern_memorystatus_freeze.h>
10 
11 #include <darwintest.h>
12 #include <darwintest_utils.h>
13 
14 #include "test_utils.h"
15 
16 T_GLOBAL_META(
17 	T_META_NAMESPACE("xnu.vm"),
18 	T_META_RADAR_COMPONENT_NAME("xnu"),
19 	T_META_RADAR_COMPONENT_VERSION("VM"),
20 	T_META_CHECK_LEAKS(false)
21 	);
22 
23 
24 T_HELPER_DECL(simple_bg, "no-op bg process") {
25 	signal(SIGUSR1, SIG_IGN);
26 	dispatch_source_t ds_signal = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, dispatch_get_main_queue());
27 	if (ds_signal == NULL) {
28 		T_LOG("[fatal] dispatch source create failed.");
29 		exit(2);
30 	}
31 
32 	dispatch_source_set_event_handler(ds_signal, ^{
33 		exit(0);
34 	});
35 
36 	dispatch_activate(ds_signal);
37 	dispatch_main();
38 }
39 
40 static pid_t helper_pid;
41 static void
signal_helper_process()42 signal_helper_process()
43 {
44 	kill(helper_pid, SIGUSR1);
45 }
46 
47 T_DECL(memorystatus_disable_freeze_in_other_process, "memorystatus_disable_freezer for another process")
48 {
49 	helper_pid = launch_background_helper("simple_bg", true, true);
50 	T_ATEND(signal_helper_process);
51 
52 	kern_return_t kern_ret = memorystatus_control(MEMORYSTATUS_CMD_SET_PROCESS_IS_FREEZABLE, helper_pid, 0, NULL, 0);
53 	T_QUIET; T_ASSERT_POSIX_SUCCESS(kern_ret, "set helper process as not freezable");
54 }
55