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