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