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