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