t_spawn.c revision 1.5 1 1.5 christos /* $NetBSD: t_spawn.c,v 1.5 2021/11/15 13:59:16 christos Exp $ */
2 1.1 martin
3 1.1 martin /*-
4 1.1 martin * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 1.1 martin * All rights reserved.
6 1.1 martin *
7 1.1 martin * This code is derived from software contributed to The NetBSD Foundation
8 1.1 martin * by Charles Zhang <charles (at) NetBSD.org> and
9 1.1 martin * Martin Husemann <martin (at) NetBSD.org>.
10 1.1 martin *
11 1.1 martin * Redistribution and use in source and binary forms, with or without
12 1.1 martin * modification, are permitted provided that the following conditions
13 1.1 martin * are met:
14 1.1 martin * 1. Redistributions of source code must retain the above copyright
15 1.1 martin * notice, this list of conditions and the following disclaimer.
16 1.1 martin * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 martin * notice, this list of conditions and the following disclaimer in the
18 1.1 martin * documentation and/or other materials provided with the distribution.
19 1.1 martin *
20 1.1 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 martin * POSSIBILITY OF SUCH DAMAGE.
31 1.1 martin */
32 1.4 christos #include <sys/cdefs.h>
33 1.5 christos __RCSID("$NetBSD: t_spawn.c,v 1.5 2021/11/15 13:59:16 christos Exp $");
34 1.1 martin
35 1.4 christos #include <atf-c.h>
36 1.4 christos
37 1.4 christos #include <sys/fcntl.h>
38 1.4 christos #include <sys/types.h>
39 1.4 christos #include <sys/wait.h>
40 1.4 christos #include <sys/stat.h>
41 1.1 martin
42 1.1 martin #include <spawn.h>
43 1.1 martin #include <string.h>
44 1.1 martin #include <stdio.h>
45 1.1 martin #include <stdlib.h>
46 1.1 martin #include <errno.h>
47 1.4 christos #include <stdarg.h>
48 1.4 christos #include <fcntl.h>
49 1.4 christos #include <unistd.h>
50 1.4 christos
51 1.4 christos #include "fa_spawn_utils.h"
52 1.4 christos
53 1.4 christos
54 1.4 christos static void check_success(const char *, int, ...);
55 1.1 martin
56 1.1 martin ATF_TC(t_spawn_ls);
57 1.1 martin
58 1.1 martin ATF_TC_HEAD(t_spawn_ls, tc)
59 1.1 martin {
60 1.1 martin atf_tc_set_md_var(tc, "descr",
61 1.1 martin "Tests a simple posix_spawn executing /bin/ls");
62 1.1 martin }
63 1.1 martin
64 1.1 martin ATF_TC_BODY(t_spawn_ls, tc)
65 1.1 martin {
66 1.1 martin char * const args[] = { __UNCONST("ls"), __UNCONST("-la"), NULL };
67 1.1 martin int err;
68 1.1 martin
69 1.1 martin err = posix_spawn(NULL, "/bin/ls", NULL, NULL, args, NULL);
70 1.1 martin ATF_REQUIRE(err == 0);
71 1.1 martin }
72 1.1 martin
73 1.1 martin ATF_TC(t_spawnp_ls);
74 1.1 martin
75 1.1 martin ATF_TC_HEAD(t_spawnp_ls, tc)
76 1.1 martin {
77 1.1 martin atf_tc_set_md_var(tc, "descr",
78 1.1 martin "Tests a simple posix_spawnp executing ls via $PATH");
79 1.1 martin }
80 1.1 martin
81 1.1 martin ATF_TC_BODY(t_spawnp_ls, tc)
82 1.1 martin {
83 1.1 martin char * const args[] = { __UNCONST("ls"), __UNCONST("-la"), NULL };
84 1.1 martin int err;
85 1.1 martin
86 1.1 martin err = posix_spawnp(NULL, "ls", NULL, NULL, args, NULL);
87 1.1 martin ATF_REQUIRE(err == 0);
88 1.1 martin }
89 1.1 martin
90 1.1 martin ATF_TC(t_spawn_zero);
91 1.1 martin
92 1.1 martin ATF_TC_HEAD(t_spawn_zero, tc)
93 1.1 martin {
94 1.1 martin atf_tc_set_md_var(tc, "descr",
95 1.1 martin "posix_spawn an invalid binary");
96 1.1 martin }
97 1.1 martin
98 1.1 martin ATF_TC_BODY(t_spawn_zero, tc)
99 1.1 martin {
100 1.1 martin char buf[FILENAME_MAX];
101 1.1 martin char * const args[] = { __UNCONST("h_zero"), NULL };
102 1.1 martin int err;
103 1.1 martin
104 1.1 martin snprintf(buf, sizeof buf, "%s/h_zero", atf_tc_get_config_var(tc, "srcdir"));
105 1.1 martin err = posix_spawn(NULL, buf, NULL, NULL, args, NULL);
106 1.1 martin ATF_REQUIRE_MSG(err == ENOEXEC, "expected error %d, got %d when spawning %s", ENOEXEC, err, buf);
107 1.1 martin }
108 1.1 martin
109 1.1 martin ATF_TC(t_spawn_missing);
110 1.1 martin
111 1.1 martin ATF_TC_HEAD(t_spawn_missing, tc)
112 1.1 martin {
113 1.1 martin atf_tc_set_md_var(tc, "descr",
114 1.4 christos "posix_spawn a non existant binary");
115 1.1 martin }
116 1.1 martin
117 1.1 martin ATF_TC_BODY(t_spawn_missing, tc)
118 1.1 martin {
119 1.1 martin char buf[FILENAME_MAX];
120 1.1 martin char * const args[] = { __UNCONST("h_nonexist"), NULL };
121 1.1 martin int err;
122 1.1 martin
123 1.1 martin snprintf(buf, sizeof buf, "%s/h_nonexist",
124 1.1 martin atf_tc_get_config_var(tc, "srcdir"));
125 1.1 martin err = posix_spawn(NULL, buf, NULL, NULL, args, NULL);
126 1.1 martin ATF_REQUIRE_MSG(err == ENOENT, "expected error %d, got %d when spawning %s", ENOENT, err, buf);
127 1.1 martin }
128 1.1 martin
129 1.1 martin ATF_TC(t_spawn_nonexec);
130 1.1 martin
131 1.1 martin ATF_TC_HEAD(t_spawn_nonexec, tc)
132 1.1 martin {
133 1.1 martin atf_tc_set_md_var(tc, "descr",
134 1.4 christos "posix_spawn a script with non existing interpreter");
135 1.1 martin }
136 1.1 martin
137 1.1 martin ATF_TC_BODY(t_spawn_nonexec, tc)
138 1.1 martin {
139 1.1 martin char buf[FILENAME_MAX];
140 1.1 martin char * const args[] = { __UNCONST("h_nonexec"), NULL };
141 1.1 martin int err;
142 1.1 martin
143 1.1 martin snprintf(buf, sizeof buf, "%s/h_nonexec",
144 1.1 martin atf_tc_get_config_var(tc, "srcdir"));
145 1.1 martin err = posix_spawn(NULL, buf, NULL, NULL, args, NULL);
146 1.1 martin ATF_REQUIRE_MSG(err == ENOENT, "expected error %d, got %d when spawning %s", ENOENT, err, buf);
147 1.1 martin }
148 1.1 martin
149 1.1 martin ATF_TC(t_spawn_child);
150 1.1 martin
151 1.1 martin ATF_TC_HEAD(t_spawn_child, tc)
152 1.1 martin {
153 1.1 martin atf_tc_set_md_var(tc, "descr",
154 1.2 snj "posix_spawn a child and get its return code");
155 1.1 martin }
156 1.1 martin
157 1.1 martin ATF_TC_BODY(t_spawn_child, tc)
158 1.1 martin {
159 1.1 martin char buf[FILENAME_MAX];
160 1.1 martin char * const args0[] = { __UNCONST("h_spawn"), __UNCONST("0"), NULL };
161 1.1 martin char * const args1[] = { __UNCONST("h_spawn"), __UNCONST("1"), NULL };
162 1.1 martin char * const args7[] = { __UNCONST("h_spawn"), __UNCONST("7"), NULL };
163 1.1 martin int err, status;
164 1.1 martin pid_t pid;
165 1.1 martin
166 1.1 martin snprintf(buf, sizeof buf, "%s/h_spawn",
167 1.1 martin atf_tc_get_config_var(tc, "srcdir"));
168 1.1 martin
169 1.1 martin err = posix_spawn(&pid, buf, NULL, NULL, args0, NULL);
170 1.1 martin ATF_REQUIRE(err == 0);
171 1.1 martin ATF_REQUIRE(pid > 0);
172 1.1 martin waitpid(pid, &status, 0);
173 1.1 martin ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0);
174 1.1 martin
175 1.1 martin err = posix_spawn(&pid, buf, NULL, NULL, args1, NULL);
176 1.1 martin ATF_REQUIRE(err == 0);
177 1.1 martin ATF_REQUIRE(pid > 0);
178 1.1 martin waitpid(pid, &status, 0);
179 1.1 martin ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 1);
180 1.1 martin
181 1.1 martin err = posix_spawn(&pid, buf, NULL, NULL, args7, NULL);
182 1.1 martin ATF_REQUIRE(err == 0);
183 1.1 martin ATF_REQUIRE(pid > 0);
184 1.1 martin waitpid(pid, &status, 0);
185 1.1 martin ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 7);
186 1.1 martin }
187 1.1 martin
188 1.4 christos #define CHDIRPATH "/tmp"
189 1.4 christos #define FILENAME "output"
190 1.4 christos #define FILEPATH "/tmp/output"
191 1.4 christos
192 1.4 christos static void
193 1.4 christos check_success(const char *file, int argc, ...)
194 1.4 christos {
195 1.4 christos va_list ap;
196 1.5 christos ssize_t bytesRead;
197 1.5 christos int fd;
198 1.4 christos size_t sizeOfFile = (size_t)filesize(file);
199 1.4 christos size_t sizeOfStr;
200 1.4 christos char *contents;
201 1.4 christos const char *dir;
202 1.4 christos
203 1.4 christos contents = malloc(sizeOfFile);
204 1.4 christos ATF_REQUIRE(contents != NULL);
205 1.4 christos
206 1.4 christos /*
207 1.4 christos * for now only 1 variadic argument expected
208 1.5 christos * only from t_spawn_[f]chdir_rel
209 1.4 christos */
210 1.4 christos if (argc != 0) {
211 1.4 christos va_start(ap, argc);
212 1.4 christos dir = va_arg(ap, char *);
213 1.4 christos ATF_REQUIRE(dir != NULL);
214 1.4 christos va_end(ap);
215 1.4 christos } else
216 1.4 christos dir = CHDIRPATH;
217 1.4 christos
218 1.4 christos fd = open(file, O_RDONLY);
219 1.4 christos ATF_REQUIRE_MSG(fd != -1, "Can't open `%s' (%s)", file, strerror(errno));
220 1.4 christos
221 1.4 christos /*
222 1.4 christos * file contains form feed i.e ASCII - 10 at the end.
223 1.4 christos * Therefore sizeOfFile - 1
224 1.4 christos */
225 1.4 christos sizeOfStr = strlen(dir);
226 1.4 christos ATF_CHECK_MSG(sizeOfStr == sizeOfFile - 1, "%zu (%s) != %zu (%s)",
227 1.4 christos sizeOfStr, dir, sizeOfFile - 1, file);
228 1.4 christos
229 1.4 christos bytesRead = read(fd, contents, sizeOfFile - 1);
230 1.4 christos contents[sizeOfFile - 1] = '\0';
231 1.4 christos ATF_REQUIRE_MSG(strcmp(dir, contents) == 0,
232 1.4 christos "[%s] != [%s] Directories dont match", dir, contents);
233 1.4 christos
234 1.4 christos fd = close(fd);
235 1.4 christos ATF_REQUIRE(fd == 0);
236 1.4 christos
237 1.4 christos unlink(file);
238 1.4 christos free(contents);
239 1.4 christos
240 1.4 christos /* XXX not really required */
241 1.5 christos ATF_REQUIRE((size_t)bytesRead == sizeOfStr);
242 1.4 christos }
243 1.4 christos
244 1.4 christos ATF_TC(t_spawn_chdir_abs);
245 1.4 christos
246 1.4 christos ATF_TC_HEAD(t_spawn_chdir_abs, tc)
247 1.4 christos {
248 1.4 christos atf_tc_set_md_var(tc, "descr",
249 1.4 christos "Test posix_spawn_fa_addchdir for absolute path");
250 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
251 1.4 christos }
252 1.4 christos
253 1.4 christos ATF_TC_BODY(t_spawn_chdir_abs, tc)
254 1.4 christos {
255 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
256 1.4 christos int error, status;
257 1.4 christos pid_t pid;
258 1.4 christos posix_spawn_file_actions_t fa;
259 1.4 christos
260 1.4 christos empty_outfile(FILEPATH);
261 1.4 christos
262 1.4 christos error = posix_spawn_file_actions_init(&fa);
263 1.4 christos ATF_REQUIRE(error == 0);
264 1.4 christos
265 1.4 christos error = posix_spawn_file_actions_addchdir(&fa, CHDIRPATH);
266 1.4 christos ATF_REQUIRE(error == 0);
267 1.4 christos
268 1.4 christos error = posix_spawn_file_actions_addopen(&fa, STDOUT_FILENO, FILENAME,
269 1.4 christos O_WRONLY, 0);
270 1.4 christos ATF_REQUIRE(error == 0);
271 1.4 christos
272 1.4 christos error = posix_spawn(&pid, "/bin/pwd", &fa, NULL, args, NULL);
273 1.4 christos ATF_REQUIRE(error == 0);
274 1.4 christos
275 1.4 christos /* wait for the child to finish */
276 1.4 christos waitpid(pid, &status, 0);
277 1.4 christos ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS,
278 1.4 christos "%s", "chdir failed");
279 1.4 christos posix_spawn_file_actions_destroy(&fa);
280 1.4 christos
281 1.4 christos /* finally cross check the output of "pwd" directory */
282 1.4 christos check_success(FILEPATH, 0);
283 1.4 christos }
284 1.4 christos
285 1.4 christos ATF_TC(t_spawn_chdir_rel);
286 1.4 christos
287 1.4 christos ATF_TC_HEAD(t_spawn_chdir_rel, tc)
288 1.4 christos {
289 1.4 christos atf_tc_set_md_var(tc, "descr",
290 1.4 christos "Test posix_spawn_fa_addchdir for relative path");
291 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
292 1.4 christos }
293 1.4 christos
294 1.4 christos
295 1.4 christos ATF_TC_BODY(t_spawn_chdir_rel, tc)
296 1.4 christos {
297 1.4 christos const char *relative_dir = "ch-dir";
298 1.4 christos const char *testdir = getcwd(NULL, 0);
299 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
300 1.4 christos char *chdirwd, *filepath;
301 1.4 christos int error, status;
302 1.4 christos pid_t pid;
303 1.4 christos posix_spawn_file_actions_t fa;
304 1.4 christos
305 1.4 christos /* cleanup previous */
306 1.4 christos error = asprintf(&filepath, "%s/%s", relative_dir, FILENAME);
307 1.4 christos ATF_CHECK(error != -1);
308 1.4 christos unlink(filepath);
309 1.4 christos free(filepath);
310 1.4 christos rmdir(relative_dir);
311 1.4 christos
312 1.4 christos error = mkdir(relative_dir, 0777);
313 1.4 christos ATF_REQUIRE_MSG(error == 0, "mkdir `%s' (%s)", relative_dir,
314 1.4 christos strerror(errno));
315 1.4 christos
316 1.4 christos error = asprintf(&chdirwd, "%s/%s", testdir, relative_dir);
317 1.4 christos ATF_CHECK(error != -1);
318 1.4 christos
319 1.4 christos error = asprintf(&filepath, "%s/%s", chdirwd, FILENAME);
320 1.4 christos ATF_CHECK(error != -1);
321 1.4 christos
322 1.4 christos #ifdef DEBUG
323 1.4 christos printf("cwd: %s\n", testdir);
324 1.4 christos printf("chdirwd: %s\n", chdirwd);
325 1.4 christos printf("filepath: %s\n", filepath);
326 1.4 christos #endif
327 1.4 christos
328 1.4 christos empty_outfile(filepath);
329 1.4 christos
330 1.4 christos error = posix_spawn_file_actions_init(&fa);
331 1.4 christos ATF_REQUIRE(error == 0);
332 1.4 christos
333 1.4 christos error = posix_spawn_file_actions_addchdir(&fa, relative_dir);
334 1.4 christos ATF_REQUIRE(error == 0);
335 1.4 christos
336 1.4 christos error = posix_spawn_file_actions_addopen(&fa, STDOUT_FILENO, FILENAME,
337 1.4 christos O_WRONLY, 0);
338 1.4 christos ATF_REQUIRE(error == 0);
339 1.4 christos
340 1.4 christos error = posix_spawn(&pid, "/bin/pwd", &fa, NULL, args, NULL);
341 1.4 christos ATF_REQUIRE(error == 0);
342 1.4 christos
343 1.4 christos /* wait for the child to finish */
344 1.4 christos waitpid(pid, &status, 0);
345 1.4 christos ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS,
346 1.4 christos "%s", "chdir failed");
347 1.4 christos posix_spawn_file_actions_destroy(&fa);
348 1.4 christos
349 1.4 christos /* finally cross check the directory */
350 1.4 christos check_success(filepath, 1, chdirwd);
351 1.4 christos free(chdirwd);
352 1.4 christos free(filepath);
353 1.4 christos
354 1.4 christos rmdir(relative_dir);
355 1.4 christos }
356 1.4 christos
357 1.4 christos ATF_TC(t_spawn_chdir_file);
358 1.4 christos
359 1.4 christos ATF_TC_HEAD(t_spawn_chdir_file, tc)
360 1.4 christos {
361 1.4 christos atf_tc_set_md_var(tc, "descr",
362 1.4 christos "Test posix_spawn_fa_addchdir on plain file (not a directory)");
363 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
364 1.4 christos }
365 1.4 christos
366 1.4 christos ATF_TC_BODY(t_spawn_chdir_file, tc)
367 1.4 christos {
368 1.4 christos int error;
369 1.4 christos pid_t pid;
370 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
371 1.4 christos posix_spawnattr_t attr;
372 1.4 christos posix_spawn_file_actions_t fa;
373 1.4 christos
374 1.4 christos empty_outfile(FILEPATH);
375 1.4 christos
376 1.4 christos error = posix_spawnattr_init(&attr);
377 1.4 christos ATF_REQUIRE(error == 0);
378 1.4 christos /*
379 1.4 christos * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that
380 1.4 christos * will cause a "proper" return value from posix_spawn(2)
381 1.4 christos * instead of a (potential) success there and a 127 exit
382 1.4 christos * status from the child process (c.f. the non-diag variant
383 1.4 christos * of this test).
384 1.4 christos */
385 1.4 christos error = posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR);
386 1.4 christos ATF_REQUIRE(error == 0);
387 1.4 christos
388 1.4 christos error = posix_spawn_file_actions_init(&fa);
389 1.4 christos ATF_REQUIRE(error == 0);
390 1.4 christos
391 1.4 christos error = posix_spawn_file_actions_addchdir(&fa, FILEPATH);
392 1.4 christos ATF_REQUIRE(error == 0);
393 1.4 christos
394 1.4 christos error = posix_spawn(&pid, "/bin/pwd", &fa, &attr, args, NULL);
395 1.4 christos ATF_REQUIRE(error == ENOTDIR);
396 1.4 christos
397 1.4 christos posix_spawn_file_actions_destroy(&fa);
398 1.4 christos posix_spawnattr_destroy(&attr);
399 1.4 christos
400 1.4 christos unlink(FILEPATH);
401 1.4 christos }
402 1.4 christos
403 1.4 christos ATF_TC(t_spawn_chdir_invalid);
404 1.4 christos
405 1.4 christos ATF_TC_HEAD(t_spawn_chdir_invalid, tc)
406 1.4 christos {
407 1.4 christos atf_tc_set_md_var(tc, "descr",
408 1.4 christos "Test posix_spawn_fa_addchdir for an invalid dir");
409 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
410 1.4 christos }
411 1.4 christos
412 1.4 christos ATF_TC_BODY(t_spawn_chdir_invalid, tc)
413 1.4 christos {
414 1.4 christos int error;
415 1.4 christos pid_t pid;
416 1.4 christos const char *dirpath = "/not/a/valid/dir";
417 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
418 1.4 christos posix_spawnattr_t attr;
419 1.4 christos posix_spawn_file_actions_t fa;
420 1.4 christos
421 1.4 christos error = posix_spawnattr_init(&attr);
422 1.4 christos ATF_REQUIRE(error == 0);
423 1.4 christos /*
424 1.4 christos * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that
425 1.4 christos * will cause a "proper" return value from posix_spawn(2)
426 1.4 christos * instead of a (potential) success there and a 127 exit
427 1.4 christos * status from the child process (c.f. the non-diag variant
428 1.4 christos * of this test).
429 1.4 christos */
430 1.4 christos error = posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR);
431 1.4 christos ATF_REQUIRE(error == 0);
432 1.4 christos
433 1.4 christos error = posix_spawn_file_actions_init(&fa);
434 1.4 christos ATF_REQUIRE(error == 0);
435 1.4 christos
436 1.4 christos error = posix_spawn_file_actions_addchdir(&fa, dirpath);
437 1.4 christos ATF_REQUIRE(error == 0);
438 1.4 christos
439 1.4 christos error = posix_spawn(&pid, "/bin/pwd", &fa, &attr, args, NULL);
440 1.4 christos ATF_REQUIRE(error == ENOENT);
441 1.4 christos
442 1.4 christos posix_spawn_file_actions_destroy(&fa);
443 1.4 christos posix_spawnattr_destroy(&attr);
444 1.4 christos }
445 1.4 christos
446 1.4 christos ATF_TC(t_spawn_chdir_permissions);
447 1.4 christos
448 1.4 christos ATF_TC_HEAD(t_spawn_chdir_permissions, tc)
449 1.4 christos {
450 1.4 christos atf_tc_set_md_var(tc, "descr",
451 1.4 christos "Test posix_spawn_file_actions_addchdir for prohibited directory");
452 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
453 1.4 christos atf_tc_set_md_var(tc, "require.user", "unprivileged");
454 1.4 christos }
455 1.4 christos
456 1.4 christos ATF_TC_BODY(t_spawn_chdir_permissions, tc)
457 1.4 christos {
458 1.4 christos int error;
459 1.4 christos pid_t pid;
460 1.4 christos const char *restrRelDir = "prohibited";
461 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
462 1.4 christos posix_spawnattr_t attr;
463 1.4 christos posix_spawn_file_actions_t fa;
464 1.4 christos
465 1.4 christos error = mkdir(restrRelDir, 0055);
466 1.4 christos ATF_REQUIRE(error == 0);
467 1.4 christos
468 1.4 christos posix_spawnattr_init(&attr);
469 1.4 christos ATF_REQUIRE(error == 0);
470 1.4 christos /*
471 1.4 christos * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that
472 1.4 christos * will cause a "proper" return value from posix_spawn(2)
473 1.4 christos * instead of a (potential) success there and a 127 exit
474 1.4 christos * status from the child process (c.f. the non-diag variant
475 1.4 christos * of this test).
476 1.4 christos */
477 1.4 christos error = posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR);
478 1.4 christos ATF_REQUIRE(error == 0);
479 1.4 christos
480 1.4 christos error = posix_spawn_file_actions_init(&fa);
481 1.4 christos ATF_REQUIRE(error == 0);
482 1.4 christos
483 1.4 christos error = posix_spawn_file_actions_addchdir(&fa, restrRelDir);
484 1.4 christos ATF_REQUIRE(error == 0);
485 1.4 christos
486 1.4 christos error = posix_spawn(&pid, "/bin/pwd", &fa, &attr, args, NULL);
487 1.4 christos ATF_CHECK(error == EACCES);
488 1.4 christos
489 1.4 christos posix_spawn_file_actions_destroy(&fa);
490 1.4 christos posix_spawnattr_destroy(&attr);
491 1.4 christos
492 1.4 christos rmdir(restrRelDir);
493 1.4 christos }
494 1.4 christos
495 1.4 christos
496 1.5 christos ATF_TC(t_spawn_fchdir_abs);
497 1.4 christos
498 1.5 christos ATF_TC_HEAD(t_spawn_fchdir_abs, tc)
499 1.4 christos {
500 1.4 christos atf_tc_set_md_var(tc, "descr", "Test posix_spawn_fa_fchdir");
501 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
502 1.4 christos }
503 1.4 christos
504 1.5 christos ATF_TC_BODY(t_spawn_fchdir_abs, tc)
505 1.4 christos {
506 1.4 christos int error, fd, status;
507 1.4 christos pid_t pid;
508 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
509 1.4 christos posix_spawn_file_actions_t fa;
510 1.4 christos
511 1.4 christos empty_outfile(FILEPATH);
512 1.4 christos
513 1.4 christos fd = open(CHDIRPATH, O_RDONLY);
514 1.5 christos ATF_REQUIRE(fd != -1);
515 1.4 christos
516 1.4 christos error = posix_spawn_file_actions_init(&fa);
517 1.4 christos ATF_REQUIRE(error == 0);
518 1.4 christos
519 1.4 christos error = posix_spawn_file_actions_addfchdir(&fa, fd);
520 1.4 christos ATF_REQUIRE(error == 0);
521 1.4 christos
522 1.4 christos error = posix_spawn_file_actions_addopen(&fa, STDOUT_FILENO, FILENAME,
523 1.4 christos O_WRONLY, 0);
524 1.4 christos ATF_REQUIRE(error == 0);
525 1.4 christos
526 1.4 christos error = posix_spawn(&pid, "/bin/pwd", &fa, NULL, args, NULL);
527 1.4 christos ATF_REQUIRE(error == 0);
528 1.4 christos
529 1.4 christos /* wait for the child to finish */
530 1.4 christos waitpid(pid, &status, 0);
531 1.4 christos ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS,
532 1.4 christos "%s", "chdir failed");
533 1.4 christos posix_spawn_file_actions_destroy(&fa);
534 1.4 christos
535 1.4 christos /* The file should be closed before reopening in 'check_success' */
536 1.4 christos error = close(fd);
537 1.4 christos ATF_REQUIRE(error == 0);
538 1.4 christos
539 1.4 christos /* finally cross check the directory */
540 1.4 christos check_success(FILEPATH, 0);
541 1.4 christos }
542 1.4 christos
543 1.5 christos ATF_TC(t_spawn_fchdir_rel);
544 1.4 christos
545 1.5 christos ATF_TC_HEAD(t_spawn_fchdir_rel, tc)
546 1.4 christos {
547 1.4 christos atf_tc_set_md_var(tc, "descr",
548 1.5 christos "Testing posix_spawn_file_actions_addfchdir on a relative "
549 1.5 christos "directory");
550 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
551 1.4 christos }
552 1.4 christos
553 1.5 christos ATF_TC_BODY(t_spawn_fchdir_rel, tc)
554 1.4 christos {
555 1.5 christos int error, fd, status;
556 1.5 christos pid_t pid;
557 1.5 christos const char *relative_dir = "ch-dir";
558 1.5 christos const char *testdir = getcwd(NULL, 0);
559 1.5 christos char * const args[2] = { __UNCONST("pwd"), NULL };
560 1.5 christos char *chdirwd, *filepath;
561 1.4 christos posix_spawn_file_actions_t fa;
562 1.4 christos
563 1.5 christos error = mkdir(relative_dir, 0755);
564 1.5 christos ATF_REQUIRE(error == 0);
565 1.5 christos
566 1.5 christos /*
567 1.5 christos * This is done in parts purposely.
568 1.5 christos * It enbales the abs path of the relative dir
569 1.5 christos * to be passed to 'check_success()' for comparing
570 1.5 christos */
571 1.5 christos error = asprintf(&chdirwd, "%s/%s", testdir, relative_dir);
572 1.5 christos ATF_CHECK(error != -1);
573 1.5 christos
574 1.5 christos error = asprintf(&filepath, "%s/%s", chdirwd, FILENAME);
575 1.5 christos ATF_CHECK(error != -1);
576 1.5 christos
577 1.5 christos empty_outfile(filepath);
578 1.5 christos
579 1.5 christos fd = open(relative_dir, O_RDONLY);
580 1.5 christos ATF_REQUIRE(fd != -1);
581 1.4 christos
582 1.4 christos error = posix_spawn_file_actions_init(&fa);
583 1.4 christos ATF_REQUIRE(error == 0);
584 1.4 christos
585 1.4 christos error = posix_spawn_file_actions_addfchdir(&fa, fd);
586 1.5 christos ATF_REQUIRE(error == 0);
587 1.5 christos
588 1.5 christos error = posix_spawn_file_actions_addopen(&fa, STDOUT_FILENO, FILENAME,
589 1.5 christos O_WRONLY, 0);
590 1.5 christos ATF_REQUIRE(error == 0);
591 1.5 christos
592 1.5 christos error = posix_spawn(&pid, "/bin/pwd", &fa, NULL, args, NULL);
593 1.5 christos ATF_REQUIRE(error == 0);
594 1.5 christos
595 1.5 christos /* wait for the child to finish */
596 1.5 christos waitpid(pid, &status, 0);
597 1.5 christos ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS,
598 1.5 christos "%s", "chdir failed");
599 1.4 christos
600 1.4 christos posix_spawn_file_actions_destroy(&fa);
601 1.5 christos
602 1.5 christos error = close(fd);
603 1.5 christos ATF_REQUIRE(error == 0);
604 1.5 christos
605 1.5 christos /* finally cross check the directory */
606 1.5 christos check_success(filepath, 1, chdirwd);
607 1.5 christos free(chdirwd);
608 1.5 christos free(filepath);
609 1.5 christos rmdir(relative_dir);
610 1.4 christos }
611 1.4 christos
612 1.5 christos ATF_TC(t_spawn_fchdir_file);
613 1.4 christos
614 1.5 christos ATF_TC_HEAD(t_spawn_fchdir_file, tc)
615 1.4 christos {
616 1.4 christos atf_tc_set_md_var(tc, "descr",
617 1.5 christos "Testing posix_spawn_file_actions_addfchdir on a "
618 1.5 christos "regular file (not a directory)");
619 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
620 1.4 christos }
621 1.4 christos
622 1.5 christos ATF_TC_BODY(t_spawn_fchdir_file, tc)
623 1.4 christos {
624 1.5 christos int error, fd;
625 1.4 christos pid_t pid;
626 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
627 1.4 christos posix_spawnattr_t attr;
628 1.4 christos posix_spawn_file_actions_t fa;
629 1.4 christos
630 1.5 christos fd = open(FILEPATH, O_WRONLY | O_CREAT | O_TRUNC, 0644);
631 1.5 christos ATF_REQUIRE_MSG(fd != -1, "Can't open `%s' (%s)", FILEPATH,
632 1.5 christos strerror(errno));
633 1.4 christos
634 1.4 christos error = posix_spawnattr_init(&attr);
635 1.5 christos ATF_REQUIRE(error == 0);
636 1.4 christos /*
637 1.4 christos * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that
638 1.4 christos * will cause a "proper" return value from posix_spawn(2)
639 1.4 christos * instead of a (potential) success there and a 127 exit
640 1.4 christos * status from the child process (c.f. the non-diag variant
641 1.4 christos * of this test).
642 1.4 christos */
643 1.4 christos error = posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR);
644 1.4 christos ATF_REQUIRE(error == 0);
645 1.4 christos
646 1.4 christos error = posix_spawn_file_actions_init(&fa);
647 1.4 christos ATF_REQUIRE(error == 0);
648 1.4 christos
649 1.4 christos error = posix_spawn_file_actions_addfchdir(&fa, fd);
650 1.4 christos ATF_REQUIRE(error == 0);
651 1.4 christos
652 1.4 christos error = posix_spawn(&pid, "/bin/pwd", &fa, &attr, args, NULL);
653 1.5 christos ATF_CHECK(error == ENOTDIR);
654 1.5 christos
655 1.5 christos posix_spawn_file_actions_destroy(&fa);
656 1.5 christos posix_spawnattr_destroy(&attr);
657 1.5 christos
658 1.5 christos error = close(fd);
659 1.5 christos ATF_REQUIRE(error == 0);
660 1.5 christos
661 1.5 christos unlink(FILEPATH);
662 1.5 christos
663 1.5 christos }
664 1.5 christos
665 1.5 christos ATF_TC(t_spawn_fchdir_neg_fd);
666 1.5 christos
667 1.5 christos ATF_TC_HEAD(t_spawn_fchdir_neg_fd, tc)
668 1.5 christos {
669 1.5 christos atf_tc_set_md_var(tc, "descr",
670 1.5 christos "Testing posix_spawn_file_actions_addfchdir on a negative file "
671 1.5 christos "descriptor");
672 1.5 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
673 1.5 christos }
674 1.5 christos
675 1.5 christos ATF_TC_BODY(t_spawn_fchdir_neg_fd, tc)
676 1.5 christos {
677 1.5 christos int error, fd;
678 1.5 christos posix_spawn_file_actions_t fa;
679 1.5 christos
680 1.5 christos fd = -1;
681 1.5 christos
682 1.5 christos error = posix_spawn_file_actions_init(&fa);
683 1.5 christos ATF_REQUIRE(error == 0);
684 1.5 christos
685 1.5 christos error = posix_spawn_file_actions_addfchdir(&fa, fd);
686 1.4 christos ATF_REQUIRE(error == EBADF);
687 1.4 christos
688 1.4 christos posix_spawn_file_actions_destroy(&fa);
689 1.4 christos }
690 1.4 christos
691 1.5 christos ATF_TC(t_spawn_fchdir_closed);
692 1.4 christos
693 1.5 christos ATF_TC_HEAD(t_spawn_fchdir_closed, tc)
694 1.4 christos {
695 1.4 christos atf_tc_set_md_var(tc, "descr",
696 1.5 christos "Testing posix_spawn_file_actions_addfchdir for a closed fd");
697 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
698 1.4 christos }
699 1.4 christos
700 1.5 christos ATF_TC_BODY(t_spawn_fchdir_closed, tc)
701 1.4 christos {
702 1.5 christos int error, fd;
703 1.4 christos pid_t pid;
704 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
705 1.4 christos posix_spawnattr_t attr;
706 1.4 christos posix_spawn_file_actions_t fa;
707 1.4 christos
708 1.5 christos fd = 3;
709 1.4 christos error = posix_spawnattr_init(&attr);
710 1.5 christos ATF_CHECK(error == 0);
711 1.4 christos /*
712 1.4 christos * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that
713 1.4 christos * will cause a "proper" return value from posix_spawn(2)
714 1.4 christos * instead of a (potential) success there and a 127 exit
715 1.4 christos * status from the child process (c.f. the non-diag variant
716 1.4 christos * of this test).
717 1.4 christos */
718 1.4 christos error = posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR);
719 1.4 christos ATF_REQUIRE(error == 0);
720 1.4 christos
721 1.4 christos error = posix_spawn_file_actions_init(&fa);
722 1.4 christos ATF_REQUIRE(error == 0);
723 1.4 christos
724 1.4 christos error = posix_spawn_file_actions_addfchdir(&fa, fd);
725 1.4 christos ATF_REQUIRE(error == 0);
726 1.4 christos
727 1.4 christos error = posix_spawn(&pid, "/bin/pwd", &fa, &attr, args, NULL);
728 1.5 christos ATF_REQUIRE(error == EBADF);
729 1.4 christos
730 1.4 christos posix_spawn_file_actions_destroy(&fa);
731 1.4 christos posix_spawnattr_destroy(&attr);
732 1.4 christos }
733 1.4 christos
734 1.4 christos #undef CHDIRPATH
735 1.4 christos #undef FILENAME
736 1.4 christos #undef FILEPATH
737 1.4 christos
738 1.1 martin ATF_TP_ADD_TCS(tp)
739 1.1 martin {
740 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_ls);
741 1.1 martin ATF_TP_ADD_TC(tp, t_spawnp_ls);
742 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_zero);
743 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_missing);
744 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_nonexec);
745 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_child);
746 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_chdir_abs);
747 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_chdir_rel);
748 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_chdir_file);
749 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_chdir_invalid);
750 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_chdir_permissions);
751 1.5 christos ATF_TP_ADD_TC(tp, t_spawn_fchdir_abs);
752 1.5 christos ATF_TP_ADD_TC(tp, t_spawn_fchdir_rel);
753 1.5 christos ATF_TP_ADD_TC(tp, t_spawn_fchdir_file);
754 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_fchdir_neg_fd);
755 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_fchdir_closed);
756 1.1 martin
757 1.1 martin return atf_no_error();
758 1.1 martin }
759