1 #include <dispatch/dispatch.h>
2 #include <mach-o/dyld.h>
3 #include <signal.h>
4 #include <sys/kern_sysctl.h>
5 #include <sys/sysctl.h>
6 #include <sys/kern_memorystatus.h>
7
8 #include <darwintest.h>
9 #include <darwintest_utils.h>
10
11 #include "test_utils.h"
12
13 bool
is_development_kernel(void)14 is_development_kernel(void)
15 {
16 static dispatch_once_t is_development_once;
17 static bool is_development;
18
19 dispatch_once(&is_development_once, ^{
20 int dev;
21 size_t dev_size = sizeof(dev);
22
23 T_QUIET;
24 T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.development", &dev,
25 &dev_size, NULL, 0), NULL);
26 is_development = (dev != 0);
27 });
28
29 return is_development;
30 }
31
32 pid_t
launch_background_helper(const char * variant,bool start_suspended,bool memorystatus_managed)33 launch_background_helper(
34 const char* variant,
35 bool start_suspended,
36 bool memorystatus_managed)
37 {
38 pid_t pid;
39 char **launch_tool_args;
40 char testpath[PATH_MAX];
41 char *variant_cpy = strdup(variant);
42 uint32_t testpath_buf_size;
43 int ret;
44
45 testpath_buf_size = sizeof(testpath);
46 ret = _NSGetExecutablePath(testpath, &testpath_buf_size);
47 launch_tool_args = (char *[]){
48 testpath,
49 "-n",
50 variant_cpy,
51 NULL
52 };
53 ret = dt_launch_tool(&pid, launch_tool_args, start_suspended, NULL, NULL);
54 if (ret != 0) {
55 T_LOG("dt_launch tool returned %d with error code %d", ret, errno);
56 }
57 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "dt_launch_tool");
58 if (memorystatus_managed) {
59 set_process_memorystatus_managed(pid);
60 }
61 free(variant_cpy);
62 return pid;
63 }
64
65 void
set_process_memorystatus_managed(pid_t pid)66 set_process_memorystatus_managed(pid_t pid)
67 {
68 kern_return_t ret = memorystatus_control(MEMORYSTATUS_CMD_SET_PROCESS_IS_MANAGED, pid, 1, NULL, 0);
69 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "memorystatus_control");
70 }
71