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