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