1 /* $NetBSD: t_fileactions.c,v 1.5 2012/04/09 19:42:07 martin Exp $ */
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles Zhang <charles@NetBSD.org> and
9 * Martin Husemann <martin@NetBSD.org>.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
48 ATF_TC(t_spawn_openmode);
50 ATF_TC_HEAD(t_spawn_openmode, tc)
52 atf_tc_set_md_var(tc, "descr",
53 "Test the proper handling of 'mode' for 'open' fileactions");
54 atf_tc_set_md_var(tc, "require.progs", "/bin/cat");
58 filesize(const char * restrict fname)
63 err = stat(fname, &st);
64 ATF_REQUIRE(err == 0);
68 #define TESTFILE "./the_input_data"
69 #define CHECKFILE "./the_output_data"
70 #define TESTCONTENT "marry has a little lamb"
73 make_testfile(const char *restrict file)
79 ATF_REQUIRE(f != NULL);
80 written = fwrite(TESTCONTENT, 1, strlen(TESTCONTENT), f);
82 ATF_REQUIRE(written == strlen(TESTCONTENT));
86 empty_outfile(const char *restrict filename)
90 f = fopen(filename, "w");
91 ATF_REQUIRE(f != NULL);
95 ATF_TC_BODY(t_spawn_openmode, tc)
99 size_t insize, outsize;
100 char * const args[2] = { __UNCONST("cat"), NULL };
101 posix_spawn_file_actions_t fa;
104 * try a "cat < testfile > checkfile"
106 make_testfile(TESTFILE);
109 posix_spawn_file_actions_init(&fa);
110 posix_spawn_file_actions_addopen(&fa, fileno(stdin),
111 TESTFILE, O_RDONLY, 0);
112 posix_spawn_file_actions_addopen(&fa, fileno(stdout),
113 CHECKFILE, O_WRONLY|O_CREAT, 0600);
114 err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
115 posix_spawn_file_actions_destroy(&fa);
117 ATF_REQUIRE(err == 0);
119 /* ok, wait for the child to finish */
120 waitpid(pid, &status, 0);
121 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
123 /* now check that input and output have the same size */
124 insize = filesize(TESTFILE);
125 outsize = filesize(CHECKFILE);
126 ATF_REQUIRE(insize == strlen(TESTCONTENT));
127 ATF_REQUIRE(insize == outsize);
130 * try a "cat < testfile >> checkfile"
132 make_testfile(TESTFILE);
133 make_testfile(CHECKFILE);
135 posix_spawn_file_actions_init(&fa);
136 posix_spawn_file_actions_addopen(&fa, fileno(stdin),
137 TESTFILE, O_RDONLY, 0);
138 posix_spawn_file_actions_addopen(&fa, fileno(stdout),
139 CHECKFILE, O_WRONLY|O_APPEND, 0);
140 err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
141 posix_spawn_file_actions_destroy(&fa);
143 ATF_REQUIRE(err == 0);
145 /* ok, wait for the child to finish */
146 waitpid(pid, &status, 0);
147 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
149 /* now check that output is twice as long as input */
150 insize = filesize(TESTFILE);
151 outsize = filesize(CHECKFILE);
152 ATF_REQUIRE(insize == strlen(TESTCONTENT));
153 ATF_REQUIRE(insize*2 == outsize);
156 * try a "cat < testfile > checkfile" with input and output swapped
158 make_testfile(TESTFILE);
159 empty_outfile(CHECKFILE);
161 posix_spawn_file_actions_init(&fa);
162 posix_spawn_file_actions_addopen(&fa, fileno(stdout),
163 TESTFILE, O_RDONLY, 0);
164 posix_spawn_file_actions_addopen(&fa, fileno(stdin),
165 CHECKFILE, O_WRONLY, 0);
166 err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
167 posix_spawn_file_actions_destroy(&fa);
169 ATF_REQUIRE(err == 0);
171 /* ok, wait for the child to finish */
172 waitpid(pid, &status, 0);
173 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_FAILURE);
175 /* now check that input and output are still the same size */
176 insize = filesize(TESTFILE);
177 outsize = filesize(CHECKFILE);
178 ATF_REQUIRE(insize == strlen(TESTCONTENT));
179 ATF_REQUIRE(outsize == 0);
182 ATF_TC(t_spawn_reopen);
184 ATF_TC_HEAD(t_spawn_reopen, tc)
186 atf_tc_set_md_var(tc, "descr",
187 "an open filehandle can be replaced by a 'open' fileaction");
188 atf_tc_set_md_var(tc, "require.progs", "/bin/cat");
191 ATF_TC_BODY(t_spawn_reopen, tc)
195 char * const args[2] = { __UNCONST("cat"), NULL };
196 posix_spawn_file_actions_t fa;
199 * make sure stdin is open in the parent
201 freopen("/dev/zero", "r", stdin);
203 * now request an open for this fd again in the child
205 posix_spawn_file_actions_init(&fa);
206 posix_spawn_file_actions_addopen(&fa, fileno(stdin),
207 "/dev/null", O_RDONLY, 0);
208 err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
209 posix_spawn_file_actions_destroy(&fa);
211 ATF_REQUIRE(err == 0);
213 waitpid(pid, &status, 0);
214 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
217 ATF_TC(t_spawn_open_nonexistent);
219 ATF_TC_HEAD(t_spawn_open_nonexistent, tc)
221 atf_tc_set_md_var(tc, "descr",
222 "posix_spawn fails when a file to open does not exist");
223 atf_tc_set_md_var(tc, "require.progs", "/bin/cat");
226 ATF_TC_BODY(t_spawn_open_nonexistent, tc)
230 char * const args[2] = { __UNCONST("cat"), NULL };
231 posix_spawn_file_actions_t fa;
233 posix_spawn_file_actions_init(&fa);
234 posix_spawn_file_actions_addopen(&fa, STDIN_FILENO,
235 "./non/ex/ist/ent", O_RDONLY, 0);
236 err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
239 * The child has been created - it should fail and
240 * return exit code 127
242 waitpid(pid, &status, 0);
243 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 127);
246 * The error has been noticed early enough, no child has
249 ATF_REQUIRE(err == ENOENT);
251 posix_spawn_file_actions_destroy(&fa);
255 ATF_TC(t_spawn_open_nonexistent_diag);
257 ATF_TC_HEAD(t_spawn_open_nonexistent_diag, tc)
259 atf_tc_set_md_var(tc, "descr",
260 "posix_spawn fails when a file to open does not exist "
261 "and delivers proper diagnostic");
262 atf_tc_set_md_var(tc, "require.progs", "/bin/cat");
265 ATF_TC_BODY(t_spawn_open_nonexistent_diag, tc)
269 char * const args[2] = { __UNCONST("cat"), NULL };
270 posix_spawnattr_t attr;
271 posix_spawn_file_actions_t fa;
273 posix_spawnattr_init(&attr);
275 * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that
276 * will cause a "proper" return value from posix_spawn(2)
277 * instead of a (potential) success there and a 127 exit
278 * status from the child process (c.f. the non-diag variant
281 posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR);
282 posix_spawn_file_actions_init(&fa);
283 posix_spawn_file_actions_addopen(&fa, STDIN_FILENO,
284 "./non/ex/ist/ent", O_RDONLY, 0);
285 err = posix_spawn(&pid, "/bin/cat", &fa, &attr, args, NULL);
286 ATF_REQUIRE(err == ENOENT);
287 posix_spawn_file_actions_destroy(&fa);
288 posix_spawnattr_destroy(&attr);
292 ATF_TC(t_spawn_fileactions);
294 ATF_TC_HEAD(t_spawn_fileactions, tc)
296 atf_tc_set_md_var(tc, "descr",
297 "Tests various complex fileactions");
300 ATF_TC_BODY(t_spawn_fileactions, tc)
302 int fd1, fd2, fd3, status, err;
304 char * const args[2] = { __UNCONST("h_fileactions"), NULL };
305 char helper[FILENAME_MAX];
306 posix_spawn_file_actions_t fa;
308 posix_spawn_file_actions_init(&fa);
310 closefrom(fileno(stderr)+1);
312 fd1 = open("/dev/null", O_RDONLY);
313 ATF_REQUIRE(fd1 == 3);
315 fd2 = open("/dev/null", O_WRONLY, O_CLOEXEC);
316 ATF_REQUIRE(fd2 == 4);
318 fd3 = open("/dev/null", O_WRONLY);
319 ATF_REQUIRE(fd3 == 5);
321 posix_spawn_file_actions_addclose(&fa, fd1);
322 posix_spawn_file_actions_addopen(&fa, 6, "/dev/null", O_RDWR, 0);
323 posix_spawn_file_actions_adddup2(&fa, 1, 7);
325 snprintf(helper, sizeof helper, "%s/h_fileactions",
326 atf_tc_get_config_var(tc, "srcdir"));
327 err = posix_spawn(&pid, helper, &fa, NULL, args, NULL);
328 posix_spawn_file_actions_destroy(&fa);
330 ATF_REQUIRE(err == 0);
332 waitpid(pid, &status, 0);
333 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
336 ATF_TC(t_spawn_empty_fileactions);
338 ATF_TC_HEAD(t_spawn_empty_fileactions, tc)
340 atf_tc_set_md_var(tc, "descr",
341 "posix_spawn with empty fileactions (PR kern/46038)");
342 atf_tc_set_md_var(tc, "require.progs", "/bin/cat");
345 ATF_TC_BODY(t_spawn_empty_fileactions, tc)
349 char * const args[2] = { __UNCONST("cat"), NULL };
350 posix_spawn_file_actions_t fa;
351 size_t insize, outsize;
354 * try a "cat < testfile > checkfile", but set up stdin/stdout
355 * already in the parent and pass empty file actions to the child.
357 make_testfile(TESTFILE);
360 freopen(TESTFILE, "r", stdin);
361 freopen(CHECKFILE, "w", stdout);
363 posix_spawn_file_actions_init(&fa);
364 err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
365 posix_spawn_file_actions_destroy(&fa);
367 ATF_REQUIRE(err == 0);
369 /* ok, wait for the child to finish */
370 waitpid(pid, &status, 0);
371 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
373 /* now check that input and output have the same size */
374 insize = filesize(TESTFILE);
375 outsize = filesize(CHECKFILE);
376 ATF_REQUIRE(insize == strlen(TESTCONTENT));
377 ATF_REQUIRE(insize == outsize);
382 ATF_TP_ADD_TC(tp, t_spawn_fileactions);
383 ATF_TP_ADD_TC(tp, t_spawn_open_nonexistent);
385 ATF_TP_ADD_TC(tp, t_spawn_open_nonexistent_diag);
387 ATF_TP_ADD_TC(tp, t_spawn_reopen);
388 ATF_TP_ADD_TC(tp, t_spawn_openmode);
389 ATF_TP_ADD_TC(tp, t_spawn_empty_fileactions);
391 return atf_no_error();