t_spawn.c revision 1.4 1 1.3 andvar /* $NetBSD: t_spawn.c,v 1.4 2021/11/07 15:46:20 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.4 christos __RCSID("$NetBSD: t_spawn.c,v 1.4 2021/11/07 15:46:20 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.4 christos int bytesRead, fd;
197 1.4 christos size_t sizeOfFile = (size_t)filesize(file);
198 1.4 christos size_t sizeOfStr;
199 1.4 christos char *contents;
200 1.4 christos const char *dir;
201 1.4 christos
202 1.4 christos contents = malloc(sizeOfFile);
203 1.4 christos ATF_REQUIRE(contents != NULL);
204 1.4 christos
205 1.4 christos /*
206 1.4 christos * for now only 1 variadic argument expected
207 1.4 christos * only from t_spawn_chdir_rel
208 1.4 christos */
209 1.4 christos if (argc != 0) {
210 1.4 christos va_start(ap, argc);
211 1.4 christos dir = va_arg(ap, char *);
212 1.4 christos ATF_REQUIRE(dir != NULL);
213 1.4 christos va_end(ap);
214 1.4 christos } else
215 1.4 christos dir = CHDIRPATH;
216 1.4 christos
217 1.4 christos fd = open(file, O_RDONLY);
218 1.4 christos ATF_REQUIRE_MSG(fd != -1, "Can't open `%s' (%s)", file, strerror(errno));
219 1.4 christos
220 1.4 christos /*
221 1.4 christos * file contains form feed i.e ASCII - 10 at the end.
222 1.4 christos * Therefore sizeOfFile - 1
223 1.4 christos */
224 1.4 christos sizeOfStr = strlen(dir);
225 1.4 christos ATF_CHECK_MSG(sizeOfStr == sizeOfFile - 1, "%zu (%s) != %zu (%s)",
226 1.4 christos sizeOfStr, dir, sizeOfFile - 1, file);
227 1.4 christos
228 1.4 christos bytesRead = read(fd, contents, sizeOfFile - 1);
229 1.4 christos contents[sizeOfFile - 1] = '\0';
230 1.4 christos ATF_REQUIRE_MSG(strcmp(dir, contents) == 0,
231 1.4 christos "[%s] != [%s] Directories dont match", dir, contents);
232 1.4 christos
233 1.4 christos fd = close(fd);
234 1.4 christos ATF_REQUIRE(fd == 0);
235 1.4 christos
236 1.4 christos unlink(file);
237 1.4 christos free(contents);
238 1.4 christos
239 1.4 christos /* XXX not really required */
240 1.4 christos ATF_REQUIRE(bytesRead = sizeOfStr);
241 1.4 christos }
242 1.4 christos
243 1.4 christos ATF_TC(t_spawn_chdir_abs);
244 1.4 christos
245 1.4 christos ATF_TC_HEAD(t_spawn_chdir_abs, tc)
246 1.4 christos {
247 1.4 christos atf_tc_set_md_var(tc, "descr",
248 1.4 christos "Test posix_spawn_fa_addchdir for absolute path");
249 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
250 1.4 christos }
251 1.4 christos
252 1.4 christos ATF_TC_BODY(t_spawn_chdir_abs, tc)
253 1.4 christos {
254 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
255 1.4 christos int error, status;
256 1.4 christos pid_t pid;
257 1.4 christos posix_spawn_file_actions_t fa;
258 1.4 christos
259 1.4 christos empty_outfile(FILEPATH);
260 1.4 christos
261 1.4 christos error = posix_spawn_file_actions_init(&fa);
262 1.4 christos ATF_REQUIRE(error == 0);
263 1.4 christos
264 1.4 christos error = posix_spawn_file_actions_addchdir(&fa, CHDIRPATH);
265 1.4 christos ATF_REQUIRE(error == 0);
266 1.4 christos
267 1.4 christos error = posix_spawn_file_actions_addopen(&fa, STDOUT_FILENO, FILENAME,
268 1.4 christos O_WRONLY, 0);
269 1.4 christos ATF_REQUIRE(error == 0);
270 1.4 christos
271 1.4 christos error = posix_spawn(&pid, "/bin/pwd", &fa, NULL, args, NULL);
272 1.4 christos ATF_REQUIRE(error == 0);
273 1.4 christos
274 1.4 christos /* wait for the child to finish */
275 1.4 christos waitpid(pid, &status, 0);
276 1.4 christos ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS,
277 1.4 christos "%s", "chdir failed");
278 1.4 christos posix_spawn_file_actions_destroy(&fa);
279 1.4 christos
280 1.4 christos /* finally cross check the output of "pwd" directory */
281 1.4 christos check_success(FILEPATH, 0);
282 1.4 christos }
283 1.4 christos
284 1.4 christos ATF_TC(t_spawn_chdir_rel);
285 1.4 christos
286 1.4 christos ATF_TC_HEAD(t_spawn_chdir_rel, tc)
287 1.4 christos {
288 1.4 christos atf_tc_set_md_var(tc, "descr",
289 1.4 christos "Test posix_spawn_fa_addchdir for relative path");
290 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
291 1.4 christos }
292 1.4 christos
293 1.4 christos
294 1.4 christos ATF_TC_BODY(t_spawn_chdir_rel, tc)
295 1.4 christos {
296 1.4 christos const char *relative_dir = "ch-dir";
297 1.4 christos const char *testdir = getcwd(NULL, 0);
298 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
299 1.4 christos char *chdirwd, *filepath;
300 1.4 christos int error, status;
301 1.4 christos pid_t pid;
302 1.4 christos posix_spawn_file_actions_t fa;
303 1.4 christos
304 1.4 christos /* cleanup previous */
305 1.4 christos error = asprintf(&filepath, "%s/%s", relative_dir, FILENAME);
306 1.4 christos ATF_CHECK(error != -1);
307 1.4 christos unlink(filepath);
308 1.4 christos free(filepath);
309 1.4 christos rmdir(relative_dir);
310 1.4 christos
311 1.4 christos error = mkdir(relative_dir, 0777);
312 1.4 christos ATF_REQUIRE_MSG(error == 0, "mkdir `%s' (%s)", relative_dir,
313 1.4 christos strerror(errno));
314 1.4 christos
315 1.4 christos error = asprintf(&chdirwd, "%s/%s", testdir, relative_dir);
316 1.4 christos ATF_CHECK(error != -1);
317 1.4 christos
318 1.4 christos error = asprintf(&filepath, "%s/%s", chdirwd, FILENAME);
319 1.4 christos ATF_CHECK(error != -1);
320 1.4 christos
321 1.4 christos #ifdef DEBUG
322 1.4 christos printf("cwd: %s\n", testdir);
323 1.4 christos printf("chdirwd: %s\n", chdirwd);
324 1.4 christos printf("filepath: %s\n", filepath);
325 1.4 christos #endif
326 1.4 christos
327 1.4 christos empty_outfile(filepath);
328 1.4 christos
329 1.4 christos error = posix_spawn_file_actions_init(&fa);
330 1.4 christos ATF_REQUIRE(error == 0);
331 1.4 christos
332 1.4 christos error = posix_spawn_file_actions_addchdir(&fa, relative_dir);
333 1.4 christos ATF_REQUIRE(error == 0);
334 1.4 christos
335 1.4 christos error = posix_spawn_file_actions_addopen(&fa, STDOUT_FILENO, FILENAME,
336 1.4 christos O_WRONLY, 0);
337 1.4 christos ATF_REQUIRE(error == 0);
338 1.4 christos
339 1.4 christos error = posix_spawn(&pid, "/bin/pwd", &fa, NULL, args, NULL);
340 1.4 christos ATF_REQUIRE(error == 0);
341 1.4 christos
342 1.4 christos /* wait for the child to finish */
343 1.4 christos waitpid(pid, &status, 0);
344 1.4 christos ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS,
345 1.4 christos "%s", "chdir failed");
346 1.4 christos posix_spawn_file_actions_destroy(&fa);
347 1.4 christos
348 1.4 christos /* finally cross check the directory */
349 1.4 christos check_success(filepath, 1, chdirwd);
350 1.4 christos free(chdirwd);
351 1.4 christos free(filepath);
352 1.4 christos
353 1.4 christos rmdir(relative_dir);
354 1.4 christos }
355 1.4 christos
356 1.4 christos ATF_TC(t_spawn_chdir_file);
357 1.4 christos
358 1.4 christos ATF_TC_HEAD(t_spawn_chdir_file, tc)
359 1.4 christos {
360 1.4 christos atf_tc_set_md_var(tc, "descr",
361 1.4 christos "Test posix_spawn_fa_addchdir on plain file (not a directory)");
362 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
363 1.4 christos }
364 1.4 christos
365 1.4 christos ATF_TC_BODY(t_spawn_chdir_file, tc)
366 1.4 christos {
367 1.4 christos int error;
368 1.4 christos pid_t pid;
369 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
370 1.4 christos posix_spawnattr_t attr;
371 1.4 christos posix_spawn_file_actions_t fa;
372 1.4 christos
373 1.4 christos empty_outfile(FILEPATH);
374 1.4 christos
375 1.4 christos error = posix_spawnattr_init(&attr);
376 1.4 christos ATF_REQUIRE(error == 0);
377 1.4 christos /*
378 1.4 christos * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that
379 1.4 christos * will cause a "proper" return value from posix_spawn(2)
380 1.4 christos * instead of a (potential) success there and a 127 exit
381 1.4 christos * status from the child process (c.f. the non-diag variant
382 1.4 christos * of this test).
383 1.4 christos */
384 1.4 christos error = posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR);
385 1.4 christos ATF_REQUIRE(error == 0);
386 1.4 christos
387 1.4 christos error = posix_spawn_file_actions_init(&fa);
388 1.4 christos ATF_REQUIRE(error == 0);
389 1.4 christos
390 1.4 christos error = posix_spawn_file_actions_addchdir(&fa, FILEPATH);
391 1.4 christos ATF_REQUIRE(error == 0);
392 1.4 christos
393 1.4 christos error = posix_spawn(&pid, "/bin/pwd", &fa, &attr, args, NULL);
394 1.4 christos ATF_REQUIRE(error == ENOTDIR);
395 1.4 christos
396 1.4 christos posix_spawn_file_actions_destroy(&fa);
397 1.4 christos posix_spawnattr_destroy(&attr);
398 1.4 christos
399 1.4 christos unlink(FILEPATH);
400 1.4 christos }
401 1.4 christos
402 1.4 christos ATF_TC(t_spawn_chdir_invalid);
403 1.4 christos
404 1.4 christos ATF_TC_HEAD(t_spawn_chdir_invalid, tc)
405 1.4 christos {
406 1.4 christos atf_tc_set_md_var(tc, "descr",
407 1.4 christos "Test posix_spawn_fa_addchdir for an invalid dir");
408 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
409 1.4 christos }
410 1.4 christos
411 1.4 christos ATF_TC_BODY(t_spawn_chdir_invalid, tc)
412 1.4 christos {
413 1.4 christos int error;
414 1.4 christos pid_t pid;
415 1.4 christos const char *dirpath = "/not/a/valid/dir";
416 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
417 1.4 christos posix_spawnattr_t attr;
418 1.4 christos posix_spawn_file_actions_t fa;
419 1.4 christos
420 1.4 christos error = posix_spawnattr_init(&attr);
421 1.4 christos ATF_REQUIRE(error == 0);
422 1.4 christos /*
423 1.4 christos * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that
424 1.4 christos * will cause a "proper" return value from posix_spawn(2)
425 1.4 christos * instead of a (potential) success there and a 127 exit
426 1.4 christos * status from the child process (c.f. the non-diag variant
427 1.4 christos * of this test).
428 1.4 christos */
429 1.4 christos error = posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR);
430 1.4 christos ATF_REQUIRE(error == 0);
431 1.4 christos
432 1.4 christos error = posix_spawn_file_actions_init(&fa);
433 1.4 christos ATF_REQUIRE(error == 0);
434 1.4 christos
435 1.4 christos error = posix_spawn_file_actions_addchdir(&fa, dirpath);
436 1.4 christos ATF_REQUIRE(error == 0);
437 1.4 christos
438 1.4 christos error = posix_spawn(&pid, "/bin/pwd", &fa, &attr, args, NULL);
439 1.4 christos ATF_REQUIRE(error == ENOENT);
440 1.4 christos
441 1.4 christos posix_spawn_file_actions_destroy(&fa);
442 1.4 christos posix_spawnattr_destroy(&attr);
443 1.4 christos }
444 1.4 christos
445 1.4 christos ATF_TC(t_spawn_chdir_permissions);
446 1.4 christos
447 1.4 christos ATF_TC_HEAD(t_spawn_chdir_permissions, tc)
448 1.4 christos {
449 1.4 christos atf_tc_set_md_var(tc, "descr",
450 1.4 christos "Test posix_spawn_file_actions_addchdir for prohibited directory");
451 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
452 1.4 christos atf_tc_set_md_var(tc, "require.user", "unprivileged");
453 1.4 christos }
454 1.4 christos
455 1.4 christos ATF_TC_BODY(t_spawn_chdir_permissions, tc)
456 1.4 christos {
457 1.4 christos int error;
458 1.4 christos pid_t pid;
459 1.4 christos const char *restrRelDir = "prohibited";
460 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
461 1.4 christos posix_spawnattr_t attr;
462 1.4 christos posix_spawn_file_actions_t fa;
463 1.4 christos
464 1.4 christos error = mkdir(restrRelDir, 0055);
465 1.4 christos ATF_REQUIRE(error == 0);
466 1.4 christos
467 1.4 christos posix_spawnattr_init(&attr);
468 1.4 christos ATF_REQUIRE(error == 0);
469 1.4 christos /*
470 1.4 christos * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that
471 1.4 christos * will cause a "proper" return value from posix_spawn(2)
472 1.4 christos * instead of a (potential) success there and a 127 exit
473 1.4 christos * status from the child process (c.f. the non-diag variant
474 1.4 christos * of this test).
475 1.4 christos */
476 1.4 christos error = posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR);
477 1.4 christos ATF_REQUIRE(error == 0);
478 1.4 christos
479 1.4 christos error = posix_spawn_file_actions_init(&fa);
480 1.4 christos ATF_REQUIRE(error == 0);
481 1.4 christos
482 1.4 christos error = posix_spawn_file_actions_addchdir(&fa, restrRelDir);
483 1.4 christos ATF_REQUIRE(error == 0);
484 1.4 christos
485 1.4 christos error = posix_spawn(&pid, "/bin/pwd", &fa, &attr, args, NULL);
486 1.4 christos ATF_CHECK(error == EACCES);
487 1.4 christos
488 1.4 christos posix_spawn_file_actions_destroy(&fa);
489 1.4 christos posix_spawnattr_destroy(&attr);
490 1.4 christos
491 1.4 christos rmdir(restrRelDir);
492 1.4 christos }
493 1.4 christos
494 1.4 christos
495 1.4 christos ATF_TC(t_spawn_fchdir);
496 1.4 christos
497 1.4 christos ATF_TC_HEAD(t_spawn_fchdir, tc)
498 1.4 christos {
499 1.4 christos atf_tc_set_md_var(tc, "descr", "Test posix_spawn_fa_fchdir");
500 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
501 1.4 christos }
502 1.4 christos
503 1.4 christos ATF_TC_BODY(t_spawn_fchdir, tc)
504 1.4 christos {
505 1.4 christos int error, fd, status;
506 1.4 christos pid_t pid;
507 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
508 1.4 christos posix_spawn_file_actions_t fa;
509 1.4 christos
510 1.4 christos empty_outfile(FILEPATH);
511 1.4 christos
512 1.4 christos fd = open(CHDIRPATH, O_RDONLY);
513 1.4 christos ATF_REQUIRE(fd >= 0);
514 1.4 christos
515 1.4 christos error = posix_spawn_file_actions_init(&fa);
516 1.4 christos ATF_REQUIRE(error == 0);
517 1.4 christos
518 1.4 christos error = posix_spawn_file_actions_addfchdir(&fa, fd);
519 1.4 christos ATF_REQUIRE(error == 0);
520 1.4 christos
521 1.4 christos error = posix_spawn_file_actions_addopen(&fa, STDOUT_FILENO, FILENAME,
522 1.4 christos O_WRONLY, 0);
523 1.4 christos ATF_REQUIRE(error == 0);
524 1.4 christos
525 1.4 christos error = posix_spawn(&pid, "/bin/pwd", &fa, NULL, args, NULL);
526 1.4 christos ATF_REQUIRE(error == 0);
527 1.4 christos
528 1.4 christos /* wait for the child to finish */
529 1.4 christos waitpid(pid, &status, 0);
530 1.4 christos ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS,
531 1.4 christos "%s", "chdir failed");
532 1.4 christos posix_spawn_file_actions_destroy(&fa);
533 1.4 christos
534 1.4 christos /* The file should be closed before reopening in 'check_success' */
535 1.4 christos error = close(fd);
536 1.4 christos ATF_REQUIRE(error == 0);
537 1.4 christos
538 1.4 christos /* finally cross check the directory */
539 1.4 christos check_success(FILEPATH, 0);
540 1.4 christos }
541 1.4 christos
542 1.4 christos ATF_TC(t_spawn_fchdir_neg_fd);
543 1.4 christos
544 1.4 christos ATF_TC_HEAD(t_spawn_fchdir_neg_fd, tc)
545 1.4 christos {
546 1.4 christos atf_tc_set_md_var(tc, "descr",
547 1.4 christos "Testing posix_spawn_file_actions_addfchdir on a negative file "
548 1.4 christos "descriptor");
549 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
550 1.4 christos }
551 1.4 christos
552 1.4 christos ATF_TC_BODY(t_spawn_fchdir_neg_fd, tc)
553 1.4 christos {
554 1.4 christos int error, fd;
555 1.4 christos posix_spawn_file_actions_t fa;
556 1.4 christos
557 1.4 christos fd = -1;
558 1.4 christos
559 1.4 christos error = posix_spawn_file_actions_init(&fa);
560 1.4 christos ATF_REQUIRE(error == 0);
561 1.4 christos
562 1.4 christos error = posix_spawn_file_actions_addfchdir(&fa, fd);
563 1.4 christos ATF_REQUIRE(error == EBADF);
564 1.4 christos
565 1.4 christos posix_spawn_file_actions_destroy(&fa);
566 1.4 christos }
567 1.4 christos
568 1.4 christos ATF_TC(t_spawn_fchdir_closed);
569 1.4 christos
570 1.4 christos ATF_TC_HEAD(t_spawn_fchdir_closed, tc)
571 1.4 christos {
572 1.4 christos atf_tc_set_md_var(tc, "descr",
573 1.4 christos "Testing posix_spawn_file_actions_addfchdir for a closed fd");
574 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
575 1.4 christos }
576 1.4 christos
577 1.4 christos ATF_TC_BODY(t_spawn_fchdir_closed, tc)
578 1.4 christos {
579 1.4 christos int error, fd;
580 1.4 christos pid_t pid;
581 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
582 1.4 christos posix_spawnattr_t attr;
583 1.4 christos posix_spawn_file_actions_t fa;
584 1.4 christos
585 1.4 christos /*
586 1.4 christos * fd = open(CHDIRPATH, O_RDONLY);
587 1.4 christos * ATF_REQUIRE(fd >= 0);
588 1.4 christos * error = close(fd);
589 1.4 christos * ATF_REQUIRE(error == 0);
590 1.4 christos */
591 1.4 christos fd = 3;
592 1.4 christos
593 1.4 christos error = posix_spawnattr_init(&attr);
594 1.4 christos ATF_CHECK(error == 0);
595 1.4 christos /*
596 1.4 christos * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that
597 1.4 christos * will cause a "proper" return value from posix_spawn(2)
598 1.4 christos * instead of a (potential) success there and a 127 exit
599 1.4 christos * status from the child process (c.f. the non-diag variant
600 1.4 christos * of this test).
601 1.4 christos */
602 1.4 christos error = posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR);
603 1.4 christos ATF_REQUIRE(error == 0);
604 1.4 christos
605 1.4 christos error = posix_spawn_file_actions_init(&fa);
606 1.4 christos ATF_REQUIRE(error == 0);
607 1.4 christos
608 1.4 christos error = posix_spawn_file_actions_addfchdir(&fa, fd);
609 1.4 christos ATF_REQUIRE(error == 0);
610 1.4 christos
611 1.4 christos error = posix_spawn(&pid, "/bin/pwd", &fa, &attr, args, NULL);
612 1.4 christos ATF_REQUIRE(error == EBADF);
613 1.4 christos
614 1.4 christos posix_spawn_file_actions_destroy(&fa);
615 1.4 christos posix_spawnattr_destroy(&attr);
616 1.4 christos }
617 1.4 christos
618 1.4 christos ATF_TC(t_spawn_fchdir_file);
619 1.4 christos
620 1.4 christos ATF_TC_HEAD(t_spawn_fchdir_file, tc)
621 1.4 christos {
622 1.4 christos atf_tc_set_md_var(tc, "descr",
623 1.4 christos "Testing posix_spawn_file_actions_addfchdir on a "
624 1.4 christos "regular file (not a directory)");
625 1.4 christos atf_tc_set_md_var(tc, "require.progs", "/bin/pwd");
626 1.4 christos }
627 1.4 christos
628 1.4 christos ATF_TC_BODY(t_spawn_fchdir_file, tc)
629 1.4 christos {
630 1.4 christos int error, fd;
631 1.4 christos pid_t pid;
632 1.4 christos char * const args[2] = { __UNCONST("pwd"), NULL };
633 1.4 christos posix_spawnattr_t attr;
634 1.4 christos posix_spawn_file_actions_t fa;
635 1.4 christos
636 1.4 christos fd = open(FILEPATH, O_WRONLY | O_CREAT | O_TRUNC, 0644);
637 1.4 christos ATF_REQUIRE_MSG(fd != -1, "Can't open `%s' (%s)", FILEPATH,
638 1.4 christos strerror(errno));
639 1.4 christos
640 1.4 christos error = posix_spawnattr_init(&attr);
641 1.4 christos ATF_REQUIRE(error == 0);
642 1.4 christos /*
643 1.4 christos * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that
644 1.4 christos * will cause a "proper" return value from posix_spawn(2)
645 1.4 christos * instead of a (potential) success there and a 127 exit
646 1.4 christos * status from the child process (c.f. the non-diag variant
647 1.4 christos * of this test).
648 1.4 christos */
649 1.4 christos error = posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR);
650 1.4 christos ATF_REQUIRE(error == 0);
651 1.4 christos
652 1.4 christos error = posix_spawn_file_actions_init(&fa);
653 1.4 christos ATF_REQUIRE(error == 0);
654 1.4 christos
655 1.4 christos error = posix_spawn_file_actions_addfchdir(&fa, fd);
656 1.4 christos ATF_REQUIRE(error == 0);
657 1.4 christos
658 1.4 christos error = posix_spawn(&pid, "/bin/pwd", &fa, &attr, args, NULL);
659 1.4 christos ATF_CHECK(error == ENOTDIR);
660 1.4 christos
661 1.4 christos posix_spawn_file_actions_destroy(&fa);
662 1.4 christos posix_spawnattr_destroy(&attr);
663 1.4 christos
664 1.4 christos error = close(fd);
665 1.4 christos ATF_REQUIRE(error == 0);
666 1.4 christos
667 1.4 christos unlink(FILEPATH);
668 1.4 christos
669 1.4 christos }
670 1.4 christos
671 1.4 christos #undef CHDIRPATH
672 1.4 christos #undef FILENAME
673 1.4 christos #undef FILEPATH
674 1.4 christos
675 1.1 martin ATF_TP_ADD_TCS(tp)
676 1.1 martin {
677 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_ls);
678 1.1 martin ATF_TP_ADD_TC(tp, t_spawnp_ls);
679 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_zero);
680 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_missing);
681 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_nonexec);
682 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_child);
683 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_chdir_abs);
684 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_chdir_rel);
685 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_chdir_file);
686 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_chdir_invalid);
687 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_chdir_permissions);
688 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_fchdir);
689 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_fchdir_neg_fd);
690 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_fchdir_closed);
691 1.4 christos ATF_TP_ADD_TC(tp, t_spawn_fchdir_file);
692 1.1 martin
693 1.1 martin return atf_no_error();
694 1.1 martin }
695