xref: /xnu-10063.121.3/tests/subsystem_root_path_helper.c (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions #include <string.h>
2*2c2f96dcSApple OSS Distributions #include <spawn_private.h>
3*2c2f96dcSApple OSS Distributions #include <_simple.h>
4*2c2f96dcSApple OSS Distributions #include "subsystem_root_path.h"
5*2c2f96dcSApple OSS Distributions 
6*2c2f96dcSApple OSS Distributions int
main(int argc,char ** argv,char ** env,const char ** apple)7*2c2f96dcSApple OSS Distributions main(int argc, char **argv, char **env, const char **apple)
8*2c2f96dcSApple OSS Distributions {
9*2c2f96dcSApple OSS Distributions 	int retval = 0;
10*2c2f96dcSApple OSS Distributions 
11*2c2f96dcSApple OSS Distributions 	if (argc != 3) {
12*2c2f96dcSApple OSS Distributions 		return 1;
13*2c2f96dcSApple OSS Distributions 	}
14*2c2f96dcSApple OSS Distributions 
15*2c2f96dcSApple OSS Distributions 	char * behavior = argv[1];
16*2c2f96dcSApple OSS Distributions 	char * expected_subsystem_root_path = argv[2];
17*2c2f96dcSApple OSS Distributions 
18*2c2f96dcSApple OSS Distributions 	if (!strcmp(behavior, HELPER_BEHAVIOR_SET)) {
19*2c2f96dcSApple OSS Distributions 		const char * subsystem_root_path = _simple_getenv(apple, SUBSYSTEM_ROOT_PATH_KEY);
20*2c2f96dcSApple OSS Distributions 		if (strcmp(subsystem_root_path, expected_subsystem_root_path)) {
21*2c2f96dcSApple OSS Distributions 			retval = 1;
22*2c2f96dcSApple OSS Distributions 		}
23*2c2f96dcSApple OSS Distributions 	} else if (!strcmp(behavior, HELPER_BEHAVIOR_NOT_SET)) {
24*2c2f96dcSApple OSS Distributions 		const char * subsystem_root_path = _simple_getenv(apple, SUBSYSTEM_ROOT_PATH_KEY);
25*2c2f96dcSApple OSS Distributions 		if (subsystem_root_path != NULL) {
26*2c2f96dcSApple OSS Distributions 			retval = 1;
27*2c2f96dcSApple OSS Distributions 		}
28*2c2f96dcSApple OSS Distributions 	} else if (!strcmp(behavior, HELPER_BEHAVIOR_FORK_EXEC)) {
29*2c2f96dcSApple OSS Distributions 		int pid = fork();
30*2c2f96dcSApple OSS Distributions 
31*2c2f96dcSApple OSS Distributions 		if (pid > 0) {
32*2c2f96dcSApple OSS Distributions 			/* Parent */
33*2c2f96dcSApple OSS Distributions 			int status;
34*2c2f96dcSApple OSS Distributions 			if (waitpid(pid, &status, 0) < 0) {
35*2c2f96dcSApple OSS Distributions 				retval = 1;
36*2c2f96dcSApple OSS Distributions 			}
37*2c2f96dcSApple OSS Distributions 
38*2c2f96dcSApple OSS Distributions 			if (!(WIFEXITED(status) && (WEXITSTATUS(status) == 0))) {
39*2c2f96dcSApple OSS Distributions 				retval = 1;
40*2c2f96dcSApple OSS Distributions 			}
41*2c2f96dcSApple OSS Distributions 		} else if (pid == 0) {
42*2c2f96dcSApple OSS Distributions 			/* Child */
43*2c2f96dcSApple OSS Distributions 			char *new_argv[] = {argv[0], HELPER_BEHAVIOR_SET, argv[2], NULL};
44*2c2f96dcSApple OSS Distributions 			execv(new_argv[0], new_argv);
45*2c2f96dcSApple OSS Distributions 			retval = 1;
46*2c2f96dcSApple OSS Distributions 		} else if (pid < 0) {
47*2c2f96dcSApple OSS Distributions 			/* Failed */
48*2c2f96dcSApple OSS Distributions 			retval = 1;
49*2c2f96dcSApple OSS Distributions 		}
50*2c2f96dcSApple OSS Distributions 	} else if (!strcmp(behavior, HELPER_BEHAVIOR_SPAWN)) {
51*2c2f96dcSApple OSS Distributions 		char * new_argv[] = {argv[0], HELPER_BEHAVIOR_SET, "/helper_root/", NULL};
52*2c2f96dcSApple OSS Distributions 		posix_spawnattr_t attr;
53*2c2f96dcSApple OSS Distributions 		posix_spawnattr_init(&attr);
54*2c2f96dcSApple OSS Distributions 		posix_spawnattr_set_subsystem_root_path_np(&attr, new_argv[2]);
55*2c2f96dcSApple OSS Distributions 		retval = _spawn_and_wait(new_argv, &attr);
56*2c2f96dcSApple OSS Distributions 		posix_spawnattr_destroy(&attr);
57*2c2f96dcSApple OSS Distributions 	}
58*2c2f96dcSApple OSS Distributions 
59*2c2f96dcSApple OSS Distributions 	return retval;
60*2c2f96dcSApple OSS Distributions }
61