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