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