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