t_fileactions.c revision 1.5 1 1.5 martin /* $NetBSD: t_fileactions.c,v 1.5 2012/04/09 19:42:07 martin 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.1 martin
33 1.1 martin
34 1.1 martin #include <atf-c.h>
35 1.1 martin #include <stdio.h>
36 1.1 martin #include <stdlib.h>
37 1.1 martin #include <string.h>
38 1.2 martin #include <errno.h>
39 1.1 martin #include <fcntl.h>
40 1.1 martin #include <spawn.h>
41 1.1 martin #include <unistd.h>
42 1.1 martin #include <sys/wait.h>
43 1.1 martin
44 1.2 martin
45 1.2 martin ATF_TC(t_spawn_openmode);
46 1.2 martin
47 1.2 martin ATF_TC_HEAD(t_spawn_openmode, tc)
48 1.2 martin {
49 1.2 martin atf_tc_set_md_var(tc, "descr",
50 1.2 martin "Test the proper handling of 'mode' for 'open' fileactions");
51 1.2 martin atf_tc_set_md_var(tc, "require.progs", "/bin/cat");
52 1.2 martin }
53 1.2 martin
54 1.2 martin static off_t
55 1.2 martin filesize(const char * restrict fname)
56 1.2 martin {
57 1.2 martin struct stat st;
58 1.2 martin int err;
59 1.2 martin
60 1.2 martin err = stat(fname, &st);
61 1.2 martin ATF_REQUIRE(err == 0);
62 1.2 martin return st.st_size;
63 1.2 martin }
64 1.2 martin
65 1.2 martin #define TESTFILE "./the_input_data"
66 1.2 martin #define CHECKFILE "./the_output_data"
67 1.2 martin #define TESTCONTENT "marry has a little lamb"
68 1.2 martin
69 1.2 martin static void
70 1.2 martin make_testfile(const char *restrict file)
71 1.2 martin {
72 1.2 martin FILE *f;
73 1.2 martin size_t written;
74 1.2 martin
75 1.2 martin f = fopen(file, "w");
76 1.2 martin ATF_REQUIRE(f != NULL);
77 1.2 martin written = fwrite(TESTCONTENT, 1, strlen(TESTCONTENT), f);
78 1.2 martin fclose(f);
79 1.2 martin ATF_REQUIRE(written == strlen(TESTCONTENT));
80 1.2 martin }
81 1.2 martin
82 1.2 martin static void
83 1.2 martin empty_outfile(const char *restrict filename)
84 1.2 martin {
85 1.2 martin FILE *f;
86 1.2 martin
87 1.2 martin f = fopen(filename, "w");
88 1.2 martin ATF_REQUIRE(f != NULL);
89 1.2 martin fclose(f);
90 1.2 martin }
91 1.2 martin
92 1.2 martin ATF_TC_BODY(t_spawn_openmode, tc)
93 1.2 martin {
94 1.2 martin int status, err;
95 1.2 martin pid_t pid;
96 1.2 martin size_t insize, outsize;
97 1.2 martin char * const args[2] = { __UNCONST("cat"), NULL };
98 1.2 martin posix_spawn_file_actions_t fa;
99 1.2 martin
100 1.2 martin /*
101 1.2 martin * try a "cat < testfile > checkfile"
102 1.2 martin */
103 1.2 martin make_testfile(TESTFILE);
104 1.2 martin unlink(CHECKFILE);
105 1.2 martin
106 1.2 martin posix_spawn_file_actions_init(&fa);
107 1.2 martin posix_spawn_file_actions_addopen(&fa, fileno(stdin),
108 1.2 martin TESTFILE, O_RDONLY, 0);
109 1.2 martin posix_spawn_file_actions_addopen(&fa, fileno(stdout),
110 1.2 martin CHECKFILE, O_WRONLY|O_CREAT, 0600);
111 1.2 martin err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
112 1.2 martin posix_spawn_file_actions_destroy(&fa);
113 1.2 martin
114 1.2 martin ATF_REQUIRE(err == 0);
115 1.2 martin
116 1.2 martin /* ok, wait for the child to finish */
117 1.2 martin waitpid(pid, &status, 0);
118 1.2 martin ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
119 1.2 martin
120 1.2 martin /* now check that input and output have the same size */
121 1.2 martin insize = filesize(TESTFILE);
122 1.2 martin outsize = filesize(CHECKFILE);
123 1.2 martin ATF_REQUIRE(insize == strlen(TESTCONTENT));
124 1.2 martin ATF_REQUIRE(insize == outsize);
125 1.2 martin
126 1.2 martin /*
127 1.2 martin * try a "cat < testfile >> checkfile"
128 1.2 martin */
129 1.2 martin make_testfile(TESTFILE);
130 1.2 martin make_testfile(CHECKFILE);
131 1.2 martin
132 1.2 martin posix_spawn_file_actions_init(&fa);
133 1.2 martin posix_spawn_file_actions_addopen(&fa, fileno(stdin),
134 1.2 martin TESTFILE, O_RDONLY, 0);
135 1.2 martin posix_spawn_file_actions_addopen(&fa, fileno(stdout),
136 1.2 martin CHECKFILE, O_WRONLY|O_APPEND, 0);
137 1.2 martin err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
138 1.2 martin posix_spawn_file_actions_destroy(&fa);
139 1.2 martin
140 1.2 martin ATF_REQUIRE(err == 0);
141 1.2 martin
142 1.2 martin /* ok, wait for the child to finish */
143 1.2 martin waitpid(pid, &status, 0);
144 1.2 martin ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
145 1.2 martin
146 1.2 martin /* now check that output is twice as long as input */
147 1.2 martin insize = filesize(TESTFILE);
148 1.2 martin outsize = filesize(CHECKFILE);
149 1.2 martin ATF_REQUIRE(insize == strlen(TESTCONTENT));
150 1.2 martin ATF_REQUIRE(insize*2 == outsize);
151 1.2 martin
152 1.2 martin /*
153 1.2 martin * try a "cat < testfile > checkfile" with input and output swapped
154 1.2 martin */
155 1.2 martin make_testfile(TESTFILE);
156 1.2 martin empty_outfile(CHECKFILE);
157 1.2 martin
158 1.2 martin posix_spawn_file_actions_init(&fa);
159 1.2 martin posix_spawn_file_actions_addopen(&fa, fileno(stdout),
160 1.2 martin TESTFILE, O_RDONLY, 0);
161 1.2 martin posix_spawn_file_actions_addopen(&fa, fileno(stdin),
162 1.2 martin CHECKFILE, O_WRONLY, 0);
163 1.2 martin err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
164 1.2 martin posix_spawn_file_actions_destroy(&fa);
165 1.2 martin
166 1.2 martin ATF_REQUIRE(err == 0);
167 1.2 martin
168 1.2 martin /* ok, wait for the child to finish */
169 1.2 martin waitpid(pid, &status, 0);
170 1.2 martin ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_FAILURE);
171 1.2 martin
172 1.2 martin /* now check that input and output are still the same size */
173 1.2 martin insize = filesize(TESTFILE);
174 1.2 martin outsize = filesize(CHECKFILE);
175 1.2 martin ATF_REQUIRE(insize == strlen(TESTCONTENT));
176 1.2 martin ATF_REQUIRE(outsize == 0);
177 1.2 martin }
178 1.2 martin
179 1.2 martin ATF_TC(t_spawn_reopen);
180 1.2 martin
181 1.2 martin ATF_TC_HEAD(t_spawn_reopen, tc)
182 1.2 martin {
183 1.2 martin atf_tc_set_md_var(tc, "descr",
184 1.2 martin "an open filehandle can be replaced by a 'open' fileaction");
185 1.2 martin atf_tc_set_md_var(tc, "require.progs", "/bin/cat");
186 1.2 martin }
187 1.2 martin
188 1.2 martin ATF_TC_BODY(t_spawn_reopen, tc)
189 1.2 martin {
190 1.2 martin int status, err;
191 1.2 martin pid_t pid;
192 1.2 martin char * const args[2] = { __UNCONST("cat"), NULL };
193 1.2 martin posix_spawn_file_actions_t fa;
194 1.2 martin
195 1.2 martin /*
196 1.2 martin * make sure stdin is open in the parent
197 1.2 martin */
198 1.2 martin freopen("/dev/zero", "r", stdin);
199 1.2 martin /*
200 1.2 martin * now request an open for this fd again in the child
201 1.2 martin */
202 1.2 martin posix_spawn_file_actions_init(&fa);
203 1.2 martin posix_spawn_file_actions_addopen(&fa, fileno(stdin),
204 1.2 martin "/dev/null", O_RDONLY, 0);
205 1.2 martin err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
206 1.2 martin posix_spawn_file_actions_destroy(&fa);
207 1.2 martin
208 1.2 martin ATF_REQUIRE(err == 0);
209 1.2 martin
210 1.2 martin waitpid(pid, &status, 0);
211 1.2 martin ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
212 1.2 martin }
213 1.2 martin
214 1.2 martin ATF_TC(t_spawn_open_nonexistent);
215 1.2 martin
216 1.2 martin ATF_TC_HEAD(t_spawn_open_nonexistent, tc)
217 1.2 martin {
218 1.2 martin atf_tc_set_md_var(tc, "descr",
219 1.2 martin "posix_spawn fails when a file to open does not exist");
220 1.2 martin atf_tc_set_md_var(tc, "require.progs", "/bin/cat");
221 1.2 martin }
222 1.2 martin
223 1.2 martin ATF_TC_BODY(t_spawn_open_nonexistent, tc)
224 1.2 martin {
225 1.4 martin int err, status;
226 1.4 martin pid_t pid;
227 1.4 martin char * const args[2] = { __UNCONST("cat"), NULL };
228 1.4 martin posix_spawn_file_actions_t fa;
229 1.4 martin
230 1.4 martin posix_spawn_file_actions_init(&fa);
231 1.4 martin posix_spawn_file_actions_addopen(&fa, STDIN_FILENO,
232 1.4 martin "./non/ex/ist/ent", O_RDONLY, 0);
233 1.4 martin err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
234 1.4 martin if (err == 0) {
235 1.4 martin /*
236 1.4 martin * The child has been created - it should fail and
237 1.4 martin * return exit code 127
238 1.4 martin */
239 1.4 martin waitpid(pid, &status, 0);
240 1.5 martin ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 127);
241 1.4 martin } else {
242 1.4 martin /*
243 1.4 martin * The error has been noticed early enough, no child has
244 1.4 martin * been run
245 1.4 martin */
246 1.4 martin ATF_REQUIRE(err == ENOENT);
247 1.4 martin }
248 1.4 martin posix_spawn_file_actions_destroy(&fa);
249 1.4 martin }
250 1.4 martin
251 1.4 martin ATF_TC(t_spawn_open_nonexistent_diag);
252 1.4 martin
253 1.4 martin ATF_TC_HEAD(t_spawn_open_nonexistent_diag, tc)
254 1.4 martin {
255 1.4 martin atf_tc_set_md_var(tc, "descr",
256 1.4 martin "posix_spawn fails when a file to open does not exist "
257 1.4 martin "and delivers proper diagnostic");
258 1.4 martin atf_tc_set_md_var(tc, "require.progs", "/bin/cat");
259 1.4 martin }
260 1.4 martin
261 1.4 martin ATF_TC_BODY(t_spawn_open_nonexistent_diag, tc)
262 1.4 martin {
263 1.2 martin int err;
264 1.2 martin pid_t pid;
265 1.2 martin char * const args[2] = { __UNCONST("cat"), NULL };
266 1.4 martin posix_spawnattr_t attr;
267 1.2 martin posix_spawn_file_actions_t fa;
268 1.2 martin
269 1.4 martin posix_spawnattr_init(&attr);
270 1.4 martin /*
271 1.4 martin * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that
272 1.4 martin * will cause a "proper" return value from posix_spawn(2)
273 1.4 martin * instead of a (potential) success there and a 127 exit
274 1.4 martin * status from the child process (c.f. the non-diag variant
275 1.4 martin * of this test).
276 1.4 martin */
277 1.4 martin posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR);
278 1.2 martin posix_spawn_file_actions_init(&fa);
279 1.2 martin posix_spawn_file_actions_addopen(&fa, STDIN_FILENO,
280 1.2 martin "./non/ex/ist/ent", O_RDONLY, 0);
281 1.4 martin err = posix_spawn(&pid, "/bin/cat", &fa, &attr, args, NULL);
282 1.2 martin ATF_REQUIRE(err == ENOENT);
283 1.2 martin posix_spawn_file_actions_destroy(&fa);
284 1.4 martin posix_spawnattr_destroy(&attr);
285 1.2 martin }
286 1.2 martin
287 1.1 martin ATF_TC(t_spawn_fileactions);
288 1.1 martin
289 1.1 martin ATF_TC_HEAD(t_spawn_fileactions, tc)
290 1.1 martin {
291 1.1 martin atf_tc_set_md_var(tc, "descr",
292 1.2 martin "Tests various complex fileactions");
293 1.1 martin }
294 1.1 martin
295 1.1 martin ATF_TC_BODY(t_spawn_fileactions, tc)
296 1.1 martin {
297 1.1 martin int fd1, fd2, fd3, status, err;
298 1.1 martin pid_t pid;
299 1.1 martin char * const args[2] = { __UNCONST("h_fileactions"), NULL };
300 1.1 martin char helper[FILENAME_MAX];
301 1.1 martin posix_spawn_file_actions_t fa;
302 1.1 martin
303 1.1 martin posix_spawn_file_actions_init(&fa);
304 1.1 martin
305 1.1 martin closefrom(fileno(stderr)+1);
306 1.1 martin
307 1.1 martin fd1 = open("/dev/null", O_RDONLY);
308 1.1 martin ATF_REQUIRE(fd1 == 3);
309 1.1 martin
310 1.1 martin fd2 = open("/dev/null", O_WRONLY, O_CLOEXEC);
311 1.1 martin ATF_REQUIRE(fd2 == 4);
312 1.1 martin
313 1.1 martin fd3 = open("/dev/null", O_WRONLY);
314 1.1 martin ATF_REQUIRE(fd3 == 5);
315 1.1 martin
316 1.1 martin posix_spawn_file_actions_addclose(&fa, fd1);
317 1.1 martin posix_spawn_file_actions_addopen(&fa, 6, "/dev/null", O_RDWR, 0);
318 1.1 martin posix_spawn_file_actions_adddup2(&fa, 1, 7);
319 1.1 martin
320 1.1 martin snprintf(helper, sizeof helper, "%s/h_fileactions",
321 1.1 martin atf_tc_get_config_var(tc, "srcdir"));
322 1.1 martin err = posix_spawn(&pid, helper, &fa, NULL, args, NULL);
323 1.2 martin posix_spawn_file_actions_destroy(&fa);
324 1.2 martin
325 1.1 martin ATF_REQUIRE(err == 0);
326 1.1 martin
327 1.1 martin waitpid(pid, &status, 0);
328 1.1 martin ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
329 1.1 martin }
330 1.1 martin
331 1.3 martin ATF_TC(t_spawn_empty_fileactions);
332 1.3 martin
333 1.3 martin ATF_TC_HEAD(t_spawn_empty_fileactions, tc)
334 1.3 martin {
335 1.3 martin atf_tc_set_md_var(tc, "descr",
336 1.3 martin "posix_spawn with empty fileactions (PR kern/46038)");
337 1.3 martin atf_tc_set_md_var(tc, "require.progs", "/bin/cat");
338 1.3 martin }
339 1.3 martin
340 1.3 martin ATF_TC_BODY(t_spawn_empty_fileactions, tc)
341 1.3 martin {
342 1.3 martin int status, err;
343 1.3 martin pid_t pid;
344 1.3 martin char * const args[2] = { __UNCONST("cat"), NULL };
345 1.3 martin posix_spawn_file_actions_t fa;
346 1.3 martin size_t insize, outsize;
347 1.3 martin
348 1.3 martin /*
349 1.3 martin * try a "cat < testfile > checkfile", but set up stdin/stdout
350 1.3 martin * already in the parent and pass empty file actions to the child.
351 1.3 martin */
352 1.3 martin make_testfile(TESTFILE);
353 1.3 martin unlink(CHECKFILE);
354 1.3 martin
355 1.3 martin freopen(TESTFILE, "r", stdin);
356 1.3 martin freopen(CHECKFILE, "w", stdout);
357 1.3 martin
358 1.3 martin posix_spawn_file_actions_init(&fa);
359 1.3 martin err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
360 1.3 martin posix_spawn_file_actions_destroy(&fa);
361 1.3 martin
362 1.3 martin ATF_REQUIRE(err == 0);
363 1.3 martin
364 1.3 martin /* ok, wait for the child to finish */
365 1.3 martin waitpid(pid, &status, 0);
366 1.3 martin ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
367 1.3 martin
368 1.3 martin /* now check that input and output have the same size */
369 1.3 martin insize = filesize(TESTFILE);
370 1.3 martin outsize = filesize(CHECKFILE);
371 1.3 martin ATF_REQUIRE(insize == strlen(TESTCONTENT));
372 1.3 martin ATF_REQUIRE(insize == outsize);
373 1.3 martin }
374 1.3 martin
375 1.1 martin ATF_TP_ADD_TCS(tp)
376 1.1 martin {
377 1.1 martin ATF_TP_ADD_TC(tp, t_spawn_fileactions);
378 1.2 martin ATF_TP_ADD_TC(tp, t_spawn_open_nonexistent);
379 1.4 martin ATF_TP_ADD_TC(tp, t_spawn_open_nonexistent_diag);
380 1.2 martin ATF_TP_ADD_TC(tp, t_spawn_reopen);
381 1.2 martin ATF_TP_ADD_TC(tp, t_spawn_openmode);
382 1.3 martin ATF_TP_ADD_TC(tp, t_spawn_empty_fileactions);
383 1.1 martin
384 1.1 martin return atf_no_error();
385 1.1 martin }
386