1*0f4c859eSApple OSS Distributions #include <darwintest.h> 2*0f4c859eSApple OSS Distributions 3*0f4c859eSApple OSS Distributions #include <errno.h> 4*0f4c859eSApple OSS Distributions #include <libproc.h> 5*0f4c859eSApple OSS Distributions #include <signal.h> 6*0f4c859eSApple OSS Distributions #include <spawn.h> 7*0f4c859eSApple OSS Distributions #include <spawn_private.h> 8*0f4c859eSApple OSS Distributions #include <stdbool.h> 9*0f4c859eSApple OSS Distributions #include <stdint.h> 10*0f4c859eSApple OSS Distributions #include <stdio.h> 11*0f4c859eSApple OSS Distributions #include <stdlib.h> 12*0f4c859eSApple OSS Distributions #include <string.h> 13*0f4c859eSApple OSS Distributions #include <sys/proc_info.h> 14*0f4c859eSApple OSS Distributions #include <sys/spawn_internal.h> 15*0f4c859eSApple OSS Distributions #include <sys/sysctl.h> 16*0f4c859eSApple OSS Distributions #include <sysexits.h> 17*0f4c859eSApple OSS Distributions #include <unistd.h> 18*0f4c859eSApple OSS Distributions #include <sys/fileport.h> 19*0f4c859eSApple OSS Distributions 20*0f4c859eSApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true)); 21*0f4c859eSApple OSS Distributions 22*0f4c859eSApple OSS Distributions T_DECL(posix_spawn_file_actions_add_fileportdup2_np, 23*0f4c859eSApple OSS Distributions "Check posix_spawnattr for posix_spawn_file_actions_add_fileportdup2_np", 24*0f4c859eSApple OSS Distributions T_META_ASROOT(true)) 25*0f4c859eSApple OSS Distributions { 26*0f4c859eSApple OSS Distributions posix_spawnattr_t attr; 27*0f4c859eSApple OSS Distributions posix_spawn_file_actions_t fact; 28*0f4c859eSApple OSS Distributions int ret, pipes[2]; 29*0f4c859eSApple OSS Distributions mach_port_t mp; 30*0f4c859eSApple OSS Distributions 31*0f4c859eSApple OSS Distributions ret = pipe(pipes); 32*0f4c859eSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "pipe"); 33*0f4c859eSApple OSS Distributions 34*0f4c859eSApple OSS Distributions ret = fileport_makeport(pipes[1], &mp); 35*0f4c859eSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "fileport_makefd"); 36*0f4c859eSApple OSS Distributions 37*0f4c859eSApple OSS Distributions ret = posix_spawnattr_init(&attr); 38*0f4c859eSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_init"); 39*0f4c859eSApple OSS Distributions 40*0f4c859eSApple OSS Distributions ret = posix_spawn_file_actions_init(&fact); 41*0f4c859eSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn_file_actions_init"); 42*0f4c859eSApple OSS Distributions 43*0f4c859eSApple OSS Distributions ret = posix_spawn_file_actions_add_fileportdup2_np(&fact, mp, STDOUT_FILENO); 44*0f4c859eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn_file_actions_add_fileportdup2_np"); 45*0f4c859eSApple OSS Distributions 46*0f4c859eSApple OSS Distributions char * const prog = "/bin/echo"; 47*0f4c859eSApple OSS Distributions char * const argv_child[] = { prog, "1", NULL }; 48*0f4c859eSApple OSS Distributions pid_t child_pid; 49*0f4c859eSApple OSS Distributions extern char **environ; 50*0f4c859eSApple OSS Distributions 51*0f4c859eSApple OSS Distributions ret = posix_spawn(&child_pid, prog, &fact, &attr, argv_child, environ); 52*0f4c859eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn"); 53*0f4c859eSApple OSS Distributions 54*0f4c859eSApple OSS Distributions ret = posix_spawn_file_actions_destroy(&fact); 55*0f4c859eSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawn_file_actions_destroy"); 56*0f4c859eSApple OSS Distributions 57*0f4c859eSApple OSS Distributions ret = posix_spawnattr_destroy(&attr); 58*0f4c859eSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "posix_spawnattr_destroy"); 59*0f4c859eSApple OSS Distributions 60*0f4c859eSApple OSS Distributions T_LOG("parent: spawned child with pid %d\n", child_pid); 61*0f4c859eSApple OSS Distributions 62*0f4c859eSApple OSS Distributions int status = 0; 63*0f4c859eSApple OSS Distributions int waitpid_result = waitpid(child_pid, &status, 0); 64*0f4c859eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(waitpid_result, "waitpid"); 65*0f4c859eSApple OSS Distributions T_ASSERT_EQ(waitpid_result, child_pid, "waitpid should return child we spawned"); 66*0f4c859eSApple OSS Distributions T_ASSERT_EQ(WIFEXITED(status), 1, "child should have exited normally"); 67*0f4c859eSApple OSS Distributions T_ASSERT_EQ(WEXITSTATUS(status), EX_OK, "child should have exited with success"); 68*0f4c859eSApple OSS Distributions 69*0f4c859eSApple OSS Distributions char buf[1]; 70*0f4c859eSApple OSS Distributions ssize_t rc = read(pipes[0], buf, sizeof(buf)); 71*0f4c859eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(rc, "read"); 72*0f4c859eSApple OSS Distributions T_ASSERT_EQ(rc, 1l, "should have read one byte"); 73*0f4c859eSApple OSS Distributions T_ASSERT_EQ(buf[0], '1', "should have read '1'"); 74*0f4c859eSApple OSS Distributions } 75