t_spawn.c revision 1.8 1 1.8 andvar /* $NetBSD: t_spawn.c,v 1.8 2022/05/31 11:22:34 andvar Exp $ */
2 1.1 martin
3 1.1 martin /*-
4 1.6 christos * Copyright (c) 2012, 2021 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.8 andvar __RCSID("$NetBSD: t_spawn.c,v 1.8 2022/05/31 11:22:34 andvar 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.6 christos static void
91 1.6 christos spawn_error(const atf_tc_t *tc, const char *name, int error)
92 1.6 christos {
93 1.6 christos char buf[FILENAME_MAX];
94 1.6 christos char * const args[] = { __UNCONST(name), NULL };
95 1.6 christos int err;
96 1.6 christos
97 1.6 christos snprintf(buf, sizeof buf, "%s/%s",
98 1.6 christos atf_tc_get_config_var(tc, "srcdir"), name);
99 1.6 christos err = posix_spawn(NULL, buf, NULL, NULL, args, NULL);
100 1.6 christos ATF_REQUIRE_MSG(err == error, "expected error %d, "
101 1.6 christos "got %d when spawning %s", error, err, buf);
102 1.6 christos }
103 1.6 christos
104 1.1 martin ATF_TC(t_spawn_zero);
105 1.1 martin
106 1.1 martin ATF_TC_HEAD(t_spawn_zero, tc)
107 1.1 martin {
108 1.1 martin atf_tc_set_md_var(tc, "descr",
109 1.1 martin "posix_spawn an invalid binary");
110 1.1 martin }
111 1.1 martin
112 1.1 martin ATF_TC_BODY(t_spawn_zero, tc)
113 1.1 martin {
114 1.6 christos spawn_error(tc, "h_zero", ENOEXEC);
115 1.1 martin }
116 1.1 martin
117 1.1 martin ATF_TC(t_spawn_missing);
118 1.1 martin
119 1.1 martin ATF_TC_HEAD(t_spawn_missing, tc)
120 1.1 martin {
121 1.1 martin atf_tc_set_md_var(tc, "descr",
122 1.4 christos "posix_spawn a non existant binary");
123 1.1 martin }
124 1.1 martin
125 1.1 martin ATF_TC_BODY(t_spawn_missing, tc)
126 1.1 martin {
127 1.6 christos spawn_error(tc, "h_nonexist", ENOENT);
128 1.1 martin }
129 1.1 martin
130 1.1 martin ATF_TC(t_spawn_nonexec);
131 1.1 martin
132 1.1 martin ATF_TC_HEAD(t_spawn_nonexec, tc)
133 1.1 martin {
134 1.1 martin atf_tc_set_md_var(tc, "descr",
135 1.4 christos "posix_spawn a script with non existing interpreter");
136 1.1 martin }
137 1.1 martin
138 1.1 martin ATF_TC_BODY(t_spawn_nonexec, tc)
139 1.1 martin {
140 1.6 christos spawn_error(tc, "h_nonexec", ENOENT);
141 1.1 martin }
142 1.1 martin
143 1.1 martin ATF_TC(t_spawn_child);
144 1.1 martin
145 1.1 martin ATF_TC_HEAD(t_spawn_child, tc)
146 1.1 martin {
147 1.1 martin atf_tc_set_md_var(tc, "descr",
148 1.2 snj "posix_spawn a child and get its return code");
149 1.1 martin }
150 1.1 martin
151 1.1 martin ATF_TC_BODY(t_spawn_child, tc)
152 1.1 martin {
153 1.1 martin char buf[FILENAME_MAX];
154 1.6 christos char rv[2] = { '0', '\0' };
155 1.6 christos char * const args0[] = { __UNCONST("h_spawn"), rv, NULL };
156 1.6 christos int rets[] = { 0, 1, 7 };
157 1.1 martin int err, status;
158 1.1 martin pid_t pid;
159 1.1 martin
160 1.1 martin snprintf(buf, sizeof buf, "%s/h_spawn",
161 1.1 martin atf_tc_get_config_var(tc, "srcdir"));
162 1.1 martin
163 1.6 christos for (size_t i = 0; i < __arraycount(rets); i++) {
164 1.6 christos rv[0] = rets[i] + '0';
165 1.6 christos err = posix_spawn(&pid, buf, NULL, NULL, args0, NULL);
166 1.6 christos ATF_REQUIRE(err == 0);
167 1.6 christos ATF_REQUIRE(pid > 0);
168 1.6 christos waitpid(pid, &status, 0);
169 1.6 christos ATF_REQUIRE(WIFEXITED(status) &&
170 1.6 christos WEXITSTATUS(status) == rets[i]);
171 1.6 christos }
172 1.1 martin }
173 1.1 martin
174 1.4 christos #define CHDIRPATH "/tmp"
175 1.4 christos #define FILENAME "output"
176 1.4 christos #define FILEPATH "/tmp/output"
177 1.4 christos
178 1.6 christos #define CHDIR 1
179 1.6 christos #define FCHDIR 2
180 1.6 christos
181 1.4 christos static void
182 1.4 christos check_success(const char *file, int argc, ...)
183 1.4 christos {
184 1.4 christos va_list ap;
185 1.5 christos ssize_t bytesRead;
186 1.5 christos int fd;
187 1.4 christos size_t sizeOfFile = (size_t)filesize(file);
188 1.4 christos size_t sizeOfStr;
189 1.4 christos char *contents;
190 1.4 christos const char *dir;
191 1.4 christos
192 1.4 christos contents = malloc(sizeOfFile);
193 1.4 christos ATF_REQUIRE(contents != NULL);
194 1.4 christos
195 1.4 christos /*
196 1.4 christos * for now only 1 variadic argument expected
197 1.5 christos * only from t_spawn_[f]chdir_rel
198 1.4 christos */
199 1.4 christos if (argc != 0) {
200 1.4 christos va_start(ap, argc);
201 1.4 christos dir = va_arg(ap, char *);
202 1.4 christos ATF_REQUIRE(dir != NULL);
203 1.4 christos va_end(ap);
204 1.4 christos } else
205 1.4 christos dir = CHDIRPATH;
206 1.4 christos
207 1.4 christos fd = open(file, O_RDONLY);
208 1.6 christos ATF_REQUIRE_MSG(fd != -1, "Can't open `%s' (%s)", file,
209 1.6 christos strerror(errno));
210 1.4 christos
211 1.4 christos /*
212 1.4 christos * file contains form feed i.e ASCII - 10 at the end.
213 1.4 christos * Therefore sizeOfFile - 1
214 1.4 christos */
215 1.4 christos sizeOfStr = strlen(dir);
216 1.6 christos ATF_CHECK_MSG(sizeOfStr == sizeOfFile - 1, "%zu (%s) != %zu (%s)",
217 1.4 christos sizeOfStr, dir, sizeOfFile - 1, file);
218 1.4 christos
219 1.4 christos bytesRead = read(fd, contents, sizeOfFile - 1);
220 1.4 christos contents[sizeOfFile - 1] = '\0';
221 1.4 christos ATF_REQUIRE_MSG(strcmp(dir, contents) == 0,
222 1.4 christos "[%s] != [%s] Directories dont match", dir, contents);
223 1.4 christos
224 1.4 christos fd = close(fd);
225 1.4 christos ATF_REQUIRE(fd == 0);
226 1.4 christos
227 1.4 christos unlink(file);
228 1.4 christos free(contents);
229 1.4 christos
230 1.4 christos /* XXX not really required */
231 1.5 christos ATF_REQUIRE((size_t)bytesRead == sizeOfStr);
232 1.4 christos }
233 1.4 christos
234 1.6 christos static void
235 1.6 christos spawn_chdir(const char *dirpath, const char *filepath, int operation,
236 1.6 christos int expected_error)
237 1.4 christos {
238 1.6 christos int error, fd=-1, status;
239 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
240 1.4 christos pid_t pid;
241 1.6 christos posix_spawnattr_t attr, *attr_p;
242 1.4 christos posix_spawn_file_actions_t fa;
243 1.4 christos
244 1.6 christos if (filepath)
245 1.6 christos empty_outfile(filepath);
246 1.4 christos
247 1.4 christos error = posix_spawn_file_actions_init(&fa);
248 1.4 christos ATF_REQUIRE(error == 0);
249 1.4 christos
250 1.6 christos switch(operation) {
251 1.6 christos case CHDIR:
252 1.6 christos error = posix_spawn_file_actions_addchdir(&fa, dirpath);
253 1.6 christos break;
254 1.6 christos
255 1.6 christos case FCHDIR:
256 1.6 christos fd = open(dirpath, O_RDONLY);
257 1.6 christos ATF_REQUIRE(fd != -1);
258 1.6 christos
259 1.6 christos error = posix_spawn_file_actions_addfchdir(&fa, fd);
260 1.6 christos break;
261 1.6 christos }
262 1.4 christos ATF_REQUIRE(error == 0);
263 1.4 christos
264 1.6 christos /*
265 1.6 christos * if POSIX_SPAWN_RETURNERROR is expected, then no need to open the
266 1.6 christos * file
267 1.6 christos */
268 1.6 christos if (expected_error == 0) {
269 1.6 christos error = posix_spawn_file_actions_addopen(&fa, STDOUT_FILENO,
270 1.6 christos FILENAME, O_WRONLY, 0);
271 1.6 christos ATF_REQUIRE(error == 0);
272 1.6 christos attr_p = NULL;
273 1.6 christos } else {
274 1.6 christos error = posix_spawnattr_init(&attr);
275 1.6 christos ATF_REQUIRE(error == 0);
276 1.6 christos
277 1.6 christos /*
278 1.6 christos * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that
279 1.6 christos * will cause a "proper" return value from posix_spawn(2)
280 1.6 christos * instead of a (potential) success there and a 127 exit
281 1.6 christos * status from the child process (c.f. the non-diag variant
282 1.6 christos * of this test).
283 1.6 christos */
284 1.6 christos error = posix_spawnattr_setflags(&attr,
285 1.6 christos POSIX_SPAWN_RETURNERROR);
286 1.6 christos ATF_REQUIRE(error == 0);
287 1.6 christos attr_p = &attr;
288 1.6 christos }
289 1.6 christos
290 1.6 christos error = posix_spawn(&pid, "/bin/pwd", &fa, attr_p, args, NULL);
291 1.6 christos ATF_REQUIRE(error == expected_error);
292 1.6 christos
293 1.6 christos /* wait for the child to finish only when no spawnattr */
294 1.6 christos if (attr_p) {
295 1.6 christos posix_spawnattr_destroy(&attr);
296 1.6 christos } else {
297 1.6 christos waitpid(pid, &status, 0);
298 1.6 christos ATF_REQUIRE_MSG(WIFEXITED(status) &&
299 1.6 christos WEXITSTATUS(status) == EXIT_SUCCESS,
300 1.6 christos "%s", "[f]chdir failed");
301 1.6 christos }
302 1.6 christos
303 1.6 christos posix_spawn_file_actions_destroy(&fa);
304 1.6 christos
305 1.6 christos /*
306 1.6 christos * The file incase of fchdir(),
307 1.6 christos * should be closed before reopening in 'check_success'
308 1.6 christos */
309 1.6 christos if (fd != -1) {
310 1.6 christos error = close(fd);
311 1.6 christos ATF_REQUIRE(error == 0);
312 1.6 christos }
313 1.6 christos }
314 1.6 christos
315 1.6 christos ATF_TC(t_spawn_chdir_abs);
316 1.4 christos
317 1.6 christos ATF_TC_HEAD(t_spawn_chdir_abs, tc)
318 1.6 christos {
319 1.6 christos atf_tc_set_md_var(tc, "descr",
320 1.6 christos "Test posix_spawn_fa_addchdir for absolute path");
321 1.6 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
322 1.6 christos }
323 1.4 christos
324 1.6 christos ATF_TC_BODY(t_spawn_chdir_abs, tc)
325 1.6 christos {
326 1.6 christos spawn_chdir(CHDIRPATH, FILEPATH, 1, 0);
327 1.4 christos
328 1.4 christos /* finally cross check the output of "pwd" directory */
329 1.4 christos check_success(FILEPATH, 0);
330 1.4 christos }
331 1.4 christos
332 1.4 christos ATF_TC(t_spawn_chdir_rel);
333 1.4 christos
334 1.4 christos ATF_TC_HEAD(t_spawn_chdir_rel, tc)
335 1.4 christos {
336 1.4 christos atf_tc_set_md_var(tc, "descr",
337 1.4 christos "Test posix_spawn_fa_addchdir for relative path");
338 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
339 1.4 christos }
340 1.4 christos
341 1.4 christos
342 1.4 christos ATF_TC_BODY(t_spawn_chdir_rel, tc)
343 1.4 christos {
344 1.6 christos int error;
345 1.4 christos const char *relative_dir = "ch-dir";
346 1.4 christos const char *testdir = getcwd(NULL, 0);
347 1.4 christos char *chdirwd, *filepath;
348 1.4 christos
349 1.4 christos /* cleanup previous */
350 1.4 christos error = asprintf(&filepath, "%s/%s", relative_dir, FILENAME);
351 1.4 christos ATF_CHECK(error != -1);
352 1.4 christos unlink(filepath);
353 1.4 christos free(filepath);
354 1.6 christos
355 1.4 christos rmdir(relative_dir);
356 1.4 christos error = mkdir(relative_dir, 0777);
357 1.4 christos ATF_REQUIRE_MSG(error == 0, "mkdir `%s' (%s)", relative_dir,
358 1.4 christos strerror(errno));
359 1.4 christos
360 1.4 christos error = asprintf(&chdirwd, "%s/%s", testdir, relative_dir);
361 1.4 christos ATF_CHECK(error != -1);
362 1.4 christos
363 1.4 christos error = asprintf(&filepath, "%s/%s", chdirwd, FILENAME);
364 1.4 christos ATF_CHECK(error != -1);
365 1.4 christos
366 1.4 christos #ifdef DEBUG
367 1.4 christos printf("cwd: %s\n", testdir);
368 1.4 christos printf("chdirwd: %s\n", chdirwd);
369 1.4 christos printf("filepath: %s\n", filepath);
370 1.4 christos #endif
371 1.4 christos
372 1.6 christos spawn_chdir(relative_dir, filepath, 1, 0);
373 1.4 christos
374 1.4 christos /* finally cross check the directory */
375 1.4 christos check_success(filepath, 1, chdirwd);
376 1.4 christos free(chdirwd);
377 1.4 christos free(filepath);
378 1.4 christos }
379 1.4 christos
380 1.4 christos ATF_TC(t_spawn_chdir_file);
381 1.4 christos
382 1.4 christos ATF_TC_HEAD(t_spawn_chdir_file, tc)
383 1.4 christos {
384 1.4 christos atf_tc_set_md_var(tc, "descr",
385 1.4 christos "Test posix_spawn_fa_addchdir on plain file (not a directory)");
386 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
387 1.4 christos }
388 1.4 christos
389 1.4 christos ATF_TC_BODY(t_spawn_chdir_file, tc)
390 1.4 christos {
391 1.6 christos spawn_chdir(FILEPATH, FILEPATH, 1, ENOTDIR);
392 1.4 christos
393 1.4 christos unlink(FILEPATH);
394 1.4 christos }
395 1.4 christos
396 1.4 christos ATF_TC(t_spawn_chdir_invalid);
397 1.4 christos
398 1.4 christos ATF_TC_HEAD(t_spawn_chdir_invalid, tc)
399 1.4 christos {
400 1.4 christos atf_tc_set_md_var(tc, "descr",
401 1.4 christos "Test posix_spawn_fa_addchdir for an invalid dir");
402 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
403 1.4 christos }
404 1.4 christos
405 1.4 christos ATF_TC_BODY(t_spawn_chdir_invalid, tc)
406 1.4 christos {
407 1.6 christos spawn_chdir("/not/a/valid/dir", NULL, 1, ENOENT);
408 1.4 christos
409 1.4 christos }
410 1.4 christos
411 1.4 christos ATF_TC(t_spawn_chdir_permissions);
412 1.4 christos
413 1.4 christos ATF_TC_HEAD(t_spawn_chdir_permissions, tc)
414 1.4 christos {
415 1.4 christos atf_tc_set_md_var(tc, "descr",
416 1.4 christos "Test posix_spawn_file_actions_addchdir for prohibited directory");
417 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
418 1.4 christos atf_tc_set_md_var(tc, "require.user", "unprivileged");
419 1.4 christos }
420 1.4 christos
421 1.4 christos ATF_TC_BODY(t_spawn_chdir_permissions, tc)
422 1.4 christos {
423 1.4 christos int error;
424 1.4 christos const char *restrRelDir = "prohibited";
425 1.4 christos
426 1.6 christos rmdir(restrRelDir);
427 1.4 christos error = mkdir(restrRelDir, 0055);
428 1.4 christos ATF_REQUIRE(error == 0);
429 1.4 christos
430 1.6 christos spawn_chdir(restrRelDir, NULL, 1, EACCES);
431 1.6 christos }
432 1.4 christos
433 1.4 christos
434 1.5 christos ATF_TC(t_spawn_fchdir_abs);
435 1.4 christos
436 1.5 christos ATF_TC_HEAD(t_spawn_fchdir_abs, tc)
437 1.4 christos {
438 1.4 christos atf_tc_set_md_var(tc, "descr", "Test posix_spawn_fa_fchdir");
439 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
440 1.4 christos }
441 1.4 christos
442 1.5 christos ATF_TC_BODY(t_spawn_fchdir_abs, tc)
443 1.4 christos {
444 1.6 christos spawn_chdir(CHDIRPATH, FILEPATH, 2, 0);
445 1.4 christos
446 1.4 christos /* finally cross check the directory */
447 1.4 christos check_success(FILEPATH, 0);
448 1.4 christos }
449 1.4 christos
450 1.5 christos ATF_TC(t_spawn_fchdir_rel);
451 1.4 christos
452 1.5 christos ATF_TC_HEAD(t_spawn_fchdir_rel, tc)
453 1.4 christos {
454 1.4 christos atf_tc_set_md_var(tc, "descr",
455 1.5 christos "Testing posix_spawn_file_actions_addfchdir on a relative "
456 1.5 christos "directory");
457 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
458 1.4 christos }
459 1.4 christos
460 1.5 christos ATF_TC_BODY(t_spawn_fchdir_rel, tc)
461 1.4 christos {
462 1.6 christos int error;
463 1.5 christos const char *relative_dir = "ch-dir";
464 1.5 christos const char *testdir = getcwd(NULL, 0);
465 1.5 christos char *chdirwd, *filepath;
466 1.4 christos
467 1.6 christos rmdir(relative_dir);
468 1.5 christos error = mkdir(relative_dir, 0755);
469 1.5 christos ATF_REQUIRE(error == 0);
470 1.5 christos
471 1.5 christos /*
472 1.5 christos * This is done in parts purposely.
473 1.8 andvar * It enables the abs path of the relative dir
474 1.5 christos * to be passed to 'check_success()' for comparing
475 1.5 christos */
476 1.5 christos error = asprintf(&chdirwd, "%s/%s", testdir, relative_dir);
477 1.5 christos ATF_CHECK(error != -1);
478 1.5 christos
479 1.5 christos error = asprintf(&filepath, "%s/%s", chdirwd, FILENAME);
480 1.5 christos ATF_CHECK(error != -1);
481 1.5 christos
482 1.6 christos spawn_chdir(relative_dir, filepath, 2, 0);
483 1.5 christos
484 1.5 christos /* finally cross check the directory */
485 1.5 christos check_success(filepath, 1, chdirwd);
486 1.5 christos free(chdirwd);
487 1.5 christos free(filepath);
488 1.4 christos }
489 1.4 christos
490 1.5 christos ATF_TC(t_spawn_fchdir_file);
491 1.4 christos
492 1.5 christos ATF_TC_HEAD(t_spawn_fchdir_file, tc)
493 1.4 christos {
494 1.4 christos atf_tc_set_md_var(tc, "descr",
495 1.5 christos "Testing posix_spawn_file_actions_addfchdir on a "
496 1.5 christos "regular file (not a directory)");
497 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
498 1.4 christos }
499 1.4 christos
500 1.5 christos ATF_TC_BODY(t_spawn_fchdir_file, tc)
501 1.4 christos {
502 1.6 christos int error, fd;
503 1.4 christos
504 1.5 christos fd = open(FILEPATH, O_WRONLY | O_CREAT | O_TRUNC, 0644);
505 1.5 christos ATF_REQUIRE_MSG(fd != -1, "Can't open `%s' (%s)", FILEPATH,
506 1.5 christos strerror(errno));
507 1.4 christos
508 1.6 christos error = close(fd);
509 1.4 christos ATF_REQUIRE(error == 0);
510 1.4 christos
511 1.6 christos spawn_chdir(FILEPATH, NULL, 2, ENOTDIR);
512 1.5 christos
513 1.5 christos unlink(FILEPATH);
514 1.5 christos
515 1.5 christos }
516 1.5 christos
517 1.5 christos ATF_TC(t_spawn_fchdir_neg_fd);
518 1.5 christos
519 1.5 christos ATF_TC_HEAD(t_spawn_fchdir_neg_fd, tc)
520 1.5 christos {
521 1.5 christos atf_tc_set_md_var(tc, "descr",
522 1.5 christos "Testing posix_spawn_file_actions_addfchdir on a negative file "
523 1.5 christos "descriptor");
524 1.5 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
525 1.5 christos }
526 1.5 christos
527 1.5 christos ATF_TC_BODY(t_spawn_fchdir_neg_fd, tc)
528 1.5 christos {
529 1.5 christos int error, fd;
530 1.5 christos posix_spawn_file_actions_t fa;
531 1.5 christos
532 1.5 christos fd = -1;
533 1.5 christos
534 1.5 christos error = posix_spawn_file_actions_init(&fa);
535 1.5 christos ATF_REQUIRE(error == 0);
536 1.5 christos
537 1.5 christos error = posix_spawn_file_actions_addfchdir(&fa, fd);
538 1.4 christos ATF_REQUIRE(error == EBADF);
539 1.4 christos
540 1.4 christos posix_spawn_file_actions_destroy(&fa);
541 1.4 christos }
542 1.4 christos
543 1.5 christos ATF_TC(t_spawn_fchdir_closed);
544 1.4 christos
545 1.5 christos ATF_TC_HEAD(t_spawn_fchdir_closed, 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 for a closed fd");
549 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
550 1.4 christos }
551 1.4 christos
552 1.5 christos ATF_TC_BODY(t_spawn_fchdir_closed, tc)
553 1.4 christos {
554 1.5 christos int error, fd;
555 1.4 christos pid_t pid;
556 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
557 1.4 christos posix_spawnattr_t attr;
558 1.4 christos posix_spawn_file_actions_t fa;
559 1.4 christos
560 1.5 christos fd = 3;
561 1.4 christos error = posix_spawnattr_init(&attr);
562 1.5 christos ATF_CHECK(error == 0);
563 1.4 christos /*
564 1.4 christos * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that
565 1.4 christos * will cause a "proper" return value from posix_spawn(2)
566 1.4 christos * instead of a (potential) success there and a 127 exit
567 1.4 christos * status from the child process (c.f. the non-diag variant
568 1.4 christos * of this test).
569 1.4 christos */
570 1.4 christos error = posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR);
571 1.4 christos ATF_REQUIRE(error == 0);
572 1.4 christos
573 1.4 christos error = posix_spawn_file_actions_init(&fa);
574 1.4 christos ATF_REQUIRE(error == 0);
575 1.4 christos
576 1.4 christos error = posix_spawn_file_actions_addfchdir(&fa, fd);
577 1.4 christos ATF_REQUIRE(error == 0);
578 1.4 christos
579 1.4 christos error = posix_spawn(&pid, "/bin/pwd", &fa, &attr, args, NULL);
580 1.5 christos ATF_REQUIRE(error == EBADF);
581 1.4 christos
582 1.4 christos posix_spawn_file_actions_destroy(&fa);
583 1.4 christos posix_spawnattr_destroy(&attr);
584 1.4 christos }
585 1.4 christos
586 1.7 christos #undef CHDIR
587 1.7 christos #undef FCHDIR
588 1.4 christos #undef CHDIRPATH
589 1.4 christos #undef FILENAME
590 1.4 christos #undef FILEPATH
591 1.4 christos
592 1.1 martin ATF_TP_ADD_TCS(tp)
593 1.1 martin {
594 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_ls);
595 1.1 martin ATF_TP_ADD_TC(tp, t_spawnp_ls);
596 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_zero);
597 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_missing);
598 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_nonexec);
599 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_child);
600 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_chdir_abs);
601 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_chdir_rel);
602 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_chdir_file);
603 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_chdir_invalid);
604 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_chdir_permissions);
605 1.5 christos ATF_TP_ADD_TC(tp, t_spawn_fchdir_abs);
606 1.5 christos ATF_TP_ADD_TC(tp, t_spawn_fchdir_rel);
607 1.5 christos ATF_TP_ADD_TC(tp, t_spawn_fchdir_file);
608 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_fchdir_neg_fd);
609 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_fchdir_closed);
610 1.1 martin
611 1.1 martin return atf_no_error();
612 1.1 martin }
613