1*1b191cb5SApple OSS Distributions #include <darwintest.h> 2*1b191cb5SApple OSS Distributions 3*1b191cb5SApple OSS Distributions #include <errno.h> 4*1b191cb5SApple OSS Distributions #include <fcntl.h> 5*1b191cb5SApple OSS Distributions #include <signal.h> 6*1b191cb5SApple OSS Distributions #include <spawn.h> 7*1b191cb5SApple OSS Distributions #include <spawn_private.h> 8*1b191cb5SApple OSS Distributions #include <stdbool.h> 9*1b191cb5SApple OSS Distributions #include <stdint.h> 10*1b191cb5SApple OSS Distributions #include <stdio.h> 11*1b191cb5SApple OSS Distributions #include <stdlib.h> 12*1b191cb5SApple OSS Distributions #include <string.h> 13*1b191cb5SApple OSS Distributions #include <sys/spawn_internal.h> 14*1b191cb5SApple OSS Distributions #include <sys/sysctl.h> 15*1b191cb5SApple OSS Distributions #include <sys/syslimits.h> 16*1b191cb5SApple OSS Distributions #include <sysexits.h> 17*1b191cb5SApple OSS Distributions #include <unistd.h> 18*1b191cb5SApple OSS Distributions 19*1b191cb5SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true)); 20*1b191cb5SApple OSS Distributions 21*1b191cb5SApple OSS Distributions /* TEST_PATH needs to be something that exists, but is not the cwd */ 22*1b191cb5SApple OSS Distributions #define TEST_PATH "/System/Library/Caches" 23*1b191cb5SApple OSS Distributions 24*1b191cb5SApple OSS Distributions T_DECL(posix_spawn_file_actions_addchdir_np, "Check posix_spawn_file_actions_addchdir_np", 25*1b191cb5SApple OSS Distributions T_META_ASROOT(true)) 26*1b191cb5SApple OSS Distributions { 27*1b191cb5SApple OSS Distributions posix_spawn_file_actions_t file_actions; 28*1b191cb5SApple OSS Distributions int ret; 29*1b191cb5SApple OSS Distributions 30*1b191cb5SApple OSS Distributions ret = posix_spawn_file_actions_init(&file_actions); 31*1b191cb5SApple OSS Distributions T_QUIET; 32*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn_file_actions_init"); 33*1b191cb5SApple OSS Distributions 34*1b191cb5SApple OSS Distributions ret = posix_spawn_file_actions_addchdir_np(&file_actions, TEST_PATH); 35*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn_file_actions_addchdir_np"); 36*1b191cb5SApple OSS Distributions 37*1b191cb5SApple OSS Distributions char * const prog = "/bin/sh"; 38*1b191cb5SApple OSS Distributions char * const argv_child[] = { prog, 39*1b191cb5SApple OSS Distributions "-c", 40*1b191cb5SApple OSS Distributions "test $(pwd) = \"" TEST_PATH "\"", 41*1b191cb5SApple OSS Distributions NULL, }; 42*1b191cb5SApple OSS Distributions pid_t child_pid; 43*1b191cb5SApple OSS Distributions extern char **environ; 44*1b191cb5SApple OSS Distributions 45*1b191cb5SApple OSS Distributions ret = posix_spawn(&child_pid, prog, &file_actions, NULL, argv_child, environ); 46*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn"); 47*1b191cb5SApple OSS Distributions 48*1b191cb5SApple OSS Distributions T_LOG("parent: spawned child with pid %d\n", child_pid); 49*1b191cb5SApple OSS Distributions 50*1b191cb5SApple OSS Distributions ret = posix_spawn_file_actions_destroy(&file_actions); 51*1b191cb5SApple OSS Distributions T_QUIET; 52*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn_file_actions_destroy"); 53*1b191cb5SApple OSS Distributions 54*1b191cb5SApple OSS Distributions T_LOG("parent: waiting for child process\n"); 55*1b191cb5SApple OSS Distributions 56*1b191cb5SApple OSS Distributions int status = 0; 57*1b191cb5SApple OSS Distributions int waitpid_result = waitpid(child_pid, &status, 0); 58*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid_result, "waitpid"); 59*1b191cb5SApple OSS Distributions T_ASSERT_EQ(waitpid_result, child_pid, "waitpid should return child we spawned"); 60*1b191cb5SApple OSS Distributions T_ASSERT_EQ(WIFEXITED(status), 1, "child should have exited normally"); 61*1b191cb5SApple OSS Distributions T_ASSERT_EQ(WEXITSTATUS(status), EX_OK, "child should have exited with success"); 62*1b191cb5SApple OSS Distributions } 63*1b191cb5SApple OSS Distributions 64*1b191cb5SApple OSS Distributions T_DECL(posix_spawn_file_actions_addchdir_np_errors, "Check posix_spawn_file_actions_addchdir_np errors", 65*1b191cb5SApple OSS Distributions T_META_ASROOT(true)) 66*1b191cb5SApple OSS Distributions { 67*1b191cb5SApple OSS Distributions char longpath[PATH_MAX + 1]; 68*1b191cb5SApple OSS Distributions posix_spawn_file_actions_t file_actions; 69*1b191cb5SApple OSS Distributions int ret; 70*1b191cb5SApple OSS Distributions 71*1b191cb5SApple OSS Distributions memset(longpath, 'a', PATH_MAX); 72*1b191cb5SApple OSS Distributions longpath[PATH_MAX] = '\0'; 73*1b191cb5SApple OSS Distributions 74*1b191cb5SApple OSS Distributions ret = posix_spawn_file_actions_init(&file_actions); 75*1b191cb5SApple OSS Distributions T_QUIET; 76*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn_file_actions_init"); 77*1b191cb5SApple OSS Distributions 78*1b191cb5SApple OSS Distributions ret = posix_spawn_file_actions_addchdir_np(NULL, "/"); 79*1b191cb5SApple OSS Distributions T_ASSERT_EQ(ret, EINVAL, "NULL *file_actions returns EINVAL"); 80*1b191cb5SApple OSS Distributions 81*1b191cb5SApple OSS Distributions ret = posix_spawn_file_actions_addchdir_np(&file_actions, longpath); 82*1b191cb5SApple OSS Distributions T_ASSERT_EQ(ret, ENAMETOOLONG, "Path longer than PATH_MAX returns ENAMETOOLONG"); 83*1b191cb5SApple OSS Distributions 84*1b191cb5SApple OSS Distributions ret = posix_spawn_file_actions_destroy(&file_actions); 85*1b191cb5SApple OSS Distributions T_QUIET; 86*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn_file_actions_destroy"); 87*1b191cb5SApple OSS Distributions } 88*1b191cb5SApple OSS Distributions 89*1b191cb5SApple OSS Distributions T_DECL(posix_spawn_file_actions_addfchdir_np, "Check posix_spawn_file_actions_addfchdir_np", 90*1b191cb5SApple OSS Distributions T_META_ASROOT(true)) 91*1b191cb5SApple OSS Distributions { 92*1b191cb5SApple OSS Distributions posix_spawn_file_actions_t file_actions; 93*1b191cb5SApple OSS Distributions int ret; 94*1b191cb5SApple OSS Distributions int test_fd; 95*1b191cb5SApple OSS Distributions 96*1b191cb5SApple OSS Distributions ret = posix_spawn_file_actions_init(&file_actions); 97*1b191cb5SApple OSS Distributions T_QUIET; 98*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn_file_actions_init"); 99*1b191cb5SApple OSS Distributions 100*1b191cb5SApple OSS Distributions test_fd = open(TEST_PATH, O_RDONLY | O_CLOEXEC); 101*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(test_fd, "open " TEST_PATH); 102*1b191cb5SApple OSS Distributions 103*1b191cb5SApple OSS Distributions ret = posix_spawn_file_actions_addfchdir_np(&file_actions, test_fd); 104*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn_file_actions_addfchdir_np"); 105*1b191cb5SApple OSS Distributions 106*1b191cb5SApple OSS Distributions char * const prog = "/bin/sh"; 107*1b191cb5SApple OSS Distributions char * const argv_child[] = { prog, 108*1b191cb5SApple OSS Distributions "-c", 109*1b191cb5SApple OSS Distributions "test $(pwd) = \"" TEST_PATH "\"", 110*1b191cb5SApple OSS Distributions NULL, }; 111*1b191cb5SApple OSS Distributions pid_t child_pid; 112*1b191cb5SApple OSS Distributions extern char **environ; 113*1b191cb5SApple OSS Distributions 114*1b191cb5SApple OSS Distributions ret = posix_spawn(&child_pid, prog, &file_actions, NULL, argv_child, environ); 115*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn"); 116*1b191cb5SApple OSS Distributions 117*1b191cb5SApple OSS Distributions T_LOG("parent: spawned child with pid %d\n", child_pid); 118*1b191cb5SApple OSS Distributions 119*1b191cb5SApple OSS Distributions ret = posix_spawn_file_actions_destroy(&file_actions); 120*1b191cb5SApple OSS Distributions T_QUIET; 121*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn_file_actions_destroy"); 122*1b191cb5SApple OSS Distributions 123*1b191cb5SApple OSS Distributions T_LOG("parent: waiting for child process\n"); 124*1b191cb5SApple OSS Distributions 125*1b191cb5SApple OSS Distributions int status = 0; 126*1b191cb5SApple OSS Distributions int waitpid_result = waitpid(child_pid, &status, 0); 127*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid_result, "waitpid"); 128*1b191cb5SApple OSS Distributions T_ASSERT_EQ(waitpid_result, child_pid, "waitpid should return child we spawned"); 129*1b191cb5SApple OSS Distributions T_ASSERT_EQ(WIFEXITED(status), 1, "child should have exited normally"); 130*1b191cb5SApple OSS Distributions T_ASSERT_EQ(WEXITSTATUS(status), EX_OK, "child should have exited with success"); 131*1b191cb5SApple OSS Distributions 132*1b191cb5SApple OSS Distributions ret = close(test_fd); 133*1b191cb5SApple OSS Distributions T_QUIET; 134*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "close test fd"); 135*1b191cb5SApple OSS Distributions } 136*1b191cb5SApple OSS Distributions 137*1b191cb5SApple OSS Distributions T_DECL(posix_spawn_file_actions_addfchdir_np_errors, "Check posix_spawn_file_actions_addfchdir_np errors", 138*1b191cb5SApple OSS Distributions T_META_ASROOT(true)) 139*1b191cb5SApple OSS Distributions { 140*1b191cb5SApple OSS Distributions posix_spawn_file_actions_t file_actions; 141*1b191cb5SApple OSS Distributions int ret; 142*1b191cb5SApple OSS Distributions 143*1b191cb5SApple OSS Distributions ret = posix_spawn_file_actions_init(&file_actions); 144*1b191cb5SApple OSS Distributions T_QUIET; 145*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn_file_actions_init"); 146*1b191cb5SApple OSS Distributions 147*1b191cb5SApple OSS Distributions ret = posix_spawn_file_actions_addfchdir_np(NULL, 0); 148*1b191cb5SApple OSS Distributions T_ASSERT_EQ(ret, EINVAL, "NULL *file_actions returns EINVAL"); 149*1b191cb5SApple OSS Distributions 150*1b191cb5SApple OSS Distributions ret = posix_spawn_file_actions_addfchdir_np(&file_actions, -1); 151*1b191cb5SApple OSS Distributions T_ASSERT_EQ(ret, EBADF, "-1 file descriptor returns EBADF"); 152*1b191cb5SApple OSS Distributions 153*1b191cb5SApple OSS Distributions ret = posix_spawn_file_actions_destroy(&file_actions); 154*1b191cb5SApple OSS Distributions T_QUIET; 155*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn_file_actions_destroy"); 156*1b191cb5SApple OSS Distributions } 157