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