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