1*a1e26a70SApple OSS Distributions #include <spawn.h>
2*a1e26a70SApple OSS Distributions #include <libproc.h>
3*a1e26a70SApple OSS Distributions #include <darwintest.h>
4*a1e26a70SApple OSS Distributions #include <darwintest_utils.h>
5*a1e26a70SApple OSS Distributions
6*a1e26a70SApple OSS Distributions T_GLOBAL_META(
7*a1e26a70SApple OSS Distributions T_META_NAMESPACE("xnu.spawn"),
8*a1e26a70SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
9*a1e26a70SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("spawn"),
10*a1e26a70SApple OSS Distributions T_META_RUN_CONCURRENTLY(TRUE));
11*a1e26a70SApple OSS Distributions
12*a1e26a70SApple OSS Distributions static void
check_myself(char * name)13*a1e26a70SApple OSS Distributions check_myself(char *name)
14*a1e26a70SApple OSS Distributions {
15*a1e26a70SApple OSS Distributions struct proc_bsdinfo pinfo = {0};
16*a1e26a70SApple OSS Distributions int ret = proc_pidinfo(getpid(), PROC_PIDTBSDINFO, 0, &pinfo, sizeof(pinfo));
17*a1e26a70SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "proc_pidinfo");
18*a1e26a70SApple OSS Distributions
19*a1e26a70SApple OSS Distributions T_LOG("my process name is '%s' (comm is '%s')", pinfo.pbi_name, pinfo.pbi_comm);
20*a1e26a70SApple OSS Distributions
21*a1e26a70SApple OSS Distributions char *found = strstr(pinfo.pbi_name, "exec_set_proc_name");
22*a1e26a70SApple OSS Distributions T_ASSERT_NOTNULL(found, "proc name of %s", name);
23*a1e26a70SApple OSS Distributions }
24*a1e26a70SApple OSS Distributions
25*a1e26a70SApple OSS Distributions T_HELPER_DECL(spawned_helper, "spawned helper")
26*a1e26a70SApple OSS Distributions {
27*a1e26a70SApple OSS Distributions check_myself("child");
28*a1e26a70SApple OSS Distributions }
29*a1e26a70SApple OSS Distributions
30*a1e26a70SApple OSS Distributions T_DECL(set_proc_name, "check process name is correct", T_META_TAG_VM_PREFERRED)
31*a1e26a70SApple OSS Distributions {
32*a1e26a70SApple OSS Distributions int pid, ret, status;
33*a1e26a70SApple OSS Distributions
34*a1e26a70SApple OSS Distributions check_myself("parent");
35*a1e26a70SApple OSS Distributions
36*a1e26a70SApple OSS Distributions char binpath[MAXPATHLEN];
37*a1e26a70SApple OSS Distributions uint32_t size = sizeof(binpath);
38*a1e26a70SApple OSS Distributions ret = _NSGetExecutablePath(binpath, &size);
39*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_EQ(ret, 0, "get binary path");
40*a1e26a70SApple OSS Distributions
41*a1e26a70SApple OSS Distributions ret = dt_launch_tool(&pid, (char *[]) { binpath, "-n", "spawned_helper", NULL }, false, NULL, NULL);
42*a1e26a70SApple OSS Distributions T_ASSERT_POSIX_ZERO(ret, "posix_spawn");
43*a1e26a70SApple OSS Distributions
44*a1e26a70SApple OSS Distributions ret = waitpid(pid, &status, 0);
45*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "waitpid");
46*a1e26a70SApple OSS Distributions T_ASSERT_TRUE(WIFEXITED(status), "child exited");
47*a1e26a70SApple OSS Distributions T_ASSERT_EQ(WEXITSTATUS(status), 0, "child exit code");
48*a1e26a70SApple OSS Distributions }
49