xref: /xnu-11417.121.6/tests/vm/memorystatus_freeze_test_entitled.c (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
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 	T_META_TAG_VM_PREFERRED
22 	);
23 
24 
25 T_HELPER_DECL(simple_bg, "no-op bg process") {
26 	signal(SIGUSR1, SIG_IGN);
27 	dispatch_source_t ds_signal = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGUSR1, 0, dispatch_get_main_queue());
28 	if (ds_signal == NULL) {
29 		T_LOG("[fatal] dispatch source create failed.");
30 		exit(2);
31 	}
32 
33 	dispatch_source_set_event_handler(ds_signal, ^{
34 		exit(0);
35 	});
36 
37 	dispatch_activate(ds_signal);
38 	dispatch_main();
39 }
40 
41 static pid_t helper_pid;
42 static void
signal_helper_process(void)43 signal_helper_process(void)
44 {
45 	kill(helper_pid, SIGUSR1);
46 }
47 
48 T_DECL(memorystatus_disable_freeze_in_other_process, "memorystatus_disable_freezer for another process",
49     T_META_BOOTARGS_SET("freeze_enabled=1"),
50     T_META_REQUIRES_SYSCTL_EQ("vm.freeze_enabled", 1))
51 {
52 	helper_pid = launch_background_helper("simple_bg", true, true);
53 	T_ATEND(signal_helper_process);
54 
55 	kern_return_t kern_ret = memorystatus_control(MEMORYSTATUS_CMD_SET_PROCESS_IS_FREEZABLE, helper_pid, 0, NULL, 0);
56 	T_QUIET; T_ASSERT_POSIX_SUCCESS(kern_ret, "set helper process as not freezable");
57 }
58