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