xref: /xnu-10063.101.15/tests/subsystem_root_path.c (revision 94d3b452840153a99b38a3a9659680b2a006908e)
1*94d3b452SApple OSS Distributions #include <spawn_private.h>
2*94d3b452SApple OSS Distributions #include "subsystem_root_path.h"
3*94d3b452SApple OSS Distributions 
4*94d3b452SApple OSS Distributions #include <darwintest.h>
5*94d3b452SApple OSS Distributions #include <darwintest_utils.h>
6*94d3b452SApple OSS Distributions 
7*94d3b452SApple OSS Distributions #define UNENTITLED_EXECUTABLE_PATH "./subsystem_root_path_helper"
8*94d3b452SApple OSS Distributions #define ENTITLED_EXECUTABLE_PATH "./subsystem_root_path_helper_entitled"
9*94d3b452SApple OSS Distributions 
10*94d3b452SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
11*94d3b452SApple OSS Distributions 
12*94d3b452SApple OSS Distributions T_DECL(subsystem_root_path,
13*94d3b452SApple OSS Distributions     "Test the support for setting subsystem_root_path",
14*94d3b452SApple OSS Distributions     T_META_CHECK_LEAKS(false))
15*94d3b452SApple OSS Distributions {
16*94d3b452SApple OSS Distributions 	char * args[] = { ENTITLED_EXECUTABLE_PATH, HELPER_BEHAVIOR_NOT_SET, "/main_root/", NULL};
17*94d3b452SApple OSS Distributions 	int pid = 0;
18*94d3b452SApple OSS Distributions 	posix_spawnattr_t attr = NULL;
19*94d3b452SApple OSS Distributions 
20*94d3b452SApple OSS Distributions 	T_ASSERT_EQ_INT(_spawn_and_wait(args, NULL), 0, "posix_spawn without attributes");
21*94d3b452SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(posix_spawnattr_init(&attr), "posix_spawnattr_init");
22*94d3b452SApple OSS Distributions 	T_ASSERT_EQ_INT(_spawn_and_wait(args, &attr), 0, "posix_spawn with attributes");
23*94d3b452SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(posix_spawnattr_set_subsystem_root_path_np(&attr, args[2]), "Set subsystem root path");
24*94d3b452SApple OSS Distributions 
25*94d3b452SApple OSS Distributions 	args[1] = HELPER_BEHAVIOR_SET;
26*94d3b452SApple OSS Distributions 
27*94d3b452SApple OSS Distributions 	T_ASSERT_EQ_INT(_spawn_and_wait(args, &attr), 0, "posix_spawn with subsystem root path");
28*94d3b452SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(posix_spawnattr_set_subsystem_root_path_np(&attr, NULL), "Clear subsystem root path attribute");
29*94d3b452SApple OSS Distributions 
30*94d3b452SApple OSS Distributions 	args[1] = HELPER_BEHAVIOR_NOT_SET;
31*94d3b452SApple OSS Distributions 
32*94d3b452SApple OSS Distributions 	T_ASSERT_EQ_INT(_spawn_and_wait(args, &attr), 0, "Spawn without subsystem root path");
33*94d3b452SApple OSS Distributions 
34*94d3b452SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(posix_spawnattr_set_subsystem_root_path_np(&attr, args[2]), "Set subsystem root path (again)");
35*94d3b452SApple OSS Distributions 
36*94d3b452SApple OSS Distributions 	args[1] = HELPER_BEHAVIOR_FORK_EXEC;
37*94d3b452SApple OSS Distributions 
38*94d3b452SApple OSS Distributions 	T_ASSERT_EQ_INT(_spawn_and_wait(args, &attr), 0, "Subsystem root path inheritence across fork/exec");
39*94d3b452SApple OSS Distributions 
40*94d3b452SApple OSS Distributions 	args[1] = HELPER_BEHAVIOR_SPAWN;
41*94d3b452SApple OSS Distributions 
42*94d3b452SApple OSS Distributions 	T_ASSERT_EQ_INT(_spawn_and_wait(args, &attr), 0, "Subsystem root path override through posix_spawn");
43*94d3b452SApple OSS Distributions 
44*94d3b452SApple OSS Distributions 	args[0] = UNENTITLED_EXECUTABLE_PATH;
45*94d3b452SApple OSS Distributions 
46*94d3b452SApple OSS Distributions 	T_ASSERT_NE_INT(_spawn_and_wait(args, &attr), 0, "Entitlement check");
47*94d3b452SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(posix_spawnattr_destroy(&attr), "posix_spawnattr_destroy");
48*94d3b452SApple OSS Distributions }
49