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