Home | History | Annotate | Line # | Download | only in posix_spawn
t_fileactions.c revision 1.5.2.2
      1  1.5.2.2  yamt /* $NetBSD: t_fileactions.c,v 1.5.2.2 2012/04/17 00:09:11 yamt Exp $ */
      2  1.5.2.2  yamt 
      3  1.5.2.2  yamt /*-
      4  1.5.2.2  yamt  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  1.5.2.2  yamt  * All rights reserved.
      6  1.5.2.2  yamt  *
      7  1.5.2.2  yamt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.5.2.2  yamt  * by Charles Zhang <charles (at) NetBSD.org> and
      9  1.5.2.2  yamt  * Martin Husemann <martin (at) NetBSD.org>.
     10  1.5.2.2  yamt  *
     11  1.5.2.2  yamt  * Redistribution and use in source and binary forms, with or without
     12  1.5.2.2  yamt  * modification, are permitted provided that the following conditions
     13  1.5.2.2  yamt  * are met:
     14  1.5.2.2  yamt  * 1. Redistributions of source code must retain the above copyright
     15  1.5.2.2  yamt  *    notice, this list of conditions and the following disclaimer.
     16  1.5.2.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.5.2.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     18  1.5.2.2  yamt  *    documentation and/or other materials provided with the distribution.
     19  1.5.2.2  yamt  *
     20  1.5.2.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.5.2.2  yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.5.2.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.5.2.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.5.2.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.5.2.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.5.2.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.5.2.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.5.2.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.5.2.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.5.2.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     31  1.5.2.2  yamt  */
     32  1.5.2.2  yamt 
     33  1.5.2.2  yamt 
     34  1.5.2.2  yamt #include <atf-c.h>
     35  1.5.2.2  yamt #include <stdio.h>
     36  1.5.2.2  yamt #include <stdlib.h>
     37  1.5.2.2  yamt #include <string.h>
     38  1.5.2.2  yamt #include <errno.h>
     39  1.5.2.2  yamt #include <fcntl.h>
     40  1.5.2.2  yamt #include <spawn.h>
     41  1.5.2.2  yamt #include <unistd.h>
     42  1.5.2.2  yamt #include <sys/wait.h>
     43  1.5.2.2  yamt 
     44  1.5.2.2  yamt 
     45  1.5.2.2  yamt ATF_TC(t_spawn_openmode);
     46  1.5.2.2  yamt 
     47  1.5.2.2  yamt ATF_TC_HEAD(t_spawn_openmode, tc)
     48  1.5.2.2  yamt {
     49  1.5.2.2  yamt 	atf_tc_set_md_var(tc, "descr",
     50  1.5.2.2  yamt 	    "Test the proper handling of 'mode' for 'open' fileactions");
     51  1.5.2.2  yamt 	atf_tc_set_md_var(tc, "require.progs", "/bin/cat");
     52  1.5.2.2  yamt }
     53  1.5.2.2  yamt 
     54  1.5.2.2  yamt static off_t
     55  1.5.2.2  yamt filesize(const char * restrict fname)
     56  1.5.2.2  yamt {
     57  1.5.2.2  yamt 	struct stat st;
     58  1.5.2.2  yamt 	int err;
     59  1.5.2.2  yamt 
     60  1.5.2.2  yamt 	err = stat(fname, &st);
     61  1.5.2.2  yamt 	ATF_REQUIRE(err == 0);
     62  1.5.2.2  yamt 	return st.st_size;
     63  1.5.2.2  yamt }
     64  1.5.2.2  yamt 
     65  1.5.2.2  yamt #define TESTFILE	"./the_input_data"
     66  1.5.2.2  yamt #define CHECKFILE	"./the_output_data"
     67  1.5.2.2  yamt #define TESTCONTENT	"marry has a little lamb"
     68  1.5.2.2  yamt 
     69  1.5.2.2  yamt static void
     70  1.5.2.2  yamt make_testfile(const char *restrict file)
     71  1.5.2.2  yamt {
     72  1.5.2.2  yamt 	FILE *f;
     73  1.5.2.2  yamt 	size_t written;
     74  1.5.2.2  yamt 
     75  1.5.2.2  yamt 	f = fopen(file, "w");
     76  1.5.2.2  yamt 	ATF_REQUIRE(f != NULL);
     77  1.5.2.2  yamt 	written = fwrite(TESTCONTENT, 1, strlen(TESTCONTENT), f);
     78  1.5.2.2  yamt 	fclose(f);
     79  1.5.2.2  yamt 	ATF_REQUIRE(written == strlen(TESTCONTENT));
     80  1.5.2.2  yamt }
     81  1.5.2.2  yamt 
     82  1.5.2.2  yamt static void
     83  1.5.2.2  yamt empty_outfile(const char *restrict filename)
     84  1.5.2.2  yamt {
     85  1.5.2.2  yamt 	FILE *f;
     86  1.5.2.2  yamt 
     87  1.5.2.2  yamt 	f = fopen(filename, "w");
     88  1.5.2.2  yamt 	ATF_REQUIRE(f != NULL);
     89  1.5.2.2  yamt 	fclose(f);
     90  1.5.2.2  yamt }
     91  1.5.2.2  yamt 
     92  1.5.2.2  yamt ATF_TC_BODY(t_spawn_openmode, tc)
     93  1.5.2.2  yamt {
     94  1.5.2.2  yamt 	int status, err;
     95  1.5.2.2  yamt 	pid_t pid;
     96  1.5.2.2  yamt 	size_t insize, outsize;
     97  1.5.2.2  yamt 	char * const args[2] = { __UNCONST("cat"), NULL };
     98  1.5.2.2  yamt 	posix_spawn_file_actions_t fa;
     99  1.5.2.2  yamt 
    100  1.5.2.2  yamt 	/*
    101  1.5.2.2  yamt 	 * try a "cat < testfile > checkfile"
    102  1.5.2.2  yamt 	 */
    103  1.5.2.2  yamt 	make_testfile(TESTFILE);
    104  1.5.2.2  yamt 	unlink(CHECKFILE);
    105  1.5.2.2  yamt 
    106  1.5.2.2  yamt 	posix_spawn_file_actions_init(&fa);
    107  1.5.2.2  yamt 	posix_spawn_file_actions_addopen(&fa, fileno(stdin),
    108  1.5.2.2  yamt 	    TESTFILE, O_RDONLY, 0);
    109  1.5.2.2  yamt 	posix_spawn_file_actions_addopen(&fa, fileno(stdout),
    110  1.5.2.2  yamt 	    CHECKFILE, O_WRONLY|O_CREAT, 0600);
    111  1.5.2.2  yamt 	err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
    112  1.5.2.2  yamt 	posix_spawn_file_actions_destroy(&fa);
    113  1.5.2.2  yamt 
    114  1.5.2.2  yamt 	ATF_REQUIRE(err == 0);
    115  1.5.2.2  yamt 
    116  1.5.2.2  yamt 	/* ok, wait for the child to finish */
    117  1.5.2.2  yamt 	waitpid(pid, &status, 0);
    118  1.5.2.2  yamt 	ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
    119  1.5.2.2  yamt 
    120  1.5.2.2  yamt 	/* now check that input and output have the same size */
    121  1.5.2.2  yamt 	insize = filesize(TESTFILE);
    122  1.5.2.2  yamt 	outsize = filesize(CHECKFILE);
    123  1.5.2.2  yamt 	ATF_REQUIRE(insize == strlen(TESTCONTENT));
    124  1.5.2.2  yamt 	ATF_REQUIRE(insize == outsize);
    125  1.5.2.2  yamt 
    126  1.5.2.2  yamt 	/*
    127  1.5.2.2  yamt 	 * try a "cat < testfile >> checkfile"
    128  1.5.2.2  yamt 	 */
    129  1.5.2.2  yamt 	make_testfile(TESTFILE);
    130  1.5.2.2  yamt 	make_testfile(CHECKFILE);
    131  1.5.2.2  yamt 
    132  1.5.2.2  yamt 	posix_spawn_file_actions_init(&fa);
    133  1.5.2.2  yamt 	posix_spawn_file_actions_addopen(&fa, fileno(stdin),
    134  1.5.2.2  yamt 	    TESTFILE, O_RDONLY, 0);
    135  1.5.2.2  yamt 	posix_spawn_file_actions_addopen(&fa, fileno(stdout),
    136  1.5.2.2  yamt 	    CHECKFILE, O_WRONLY|O_APPEND, 0);
    137  1.5.2.2  yamt 	err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
    138  1.5.2.2  yamt 	posix_spawn_file_actions_destroy(&fa);
    139  1.5.2.2  yamt 
    140  1.5.2.2  yamt 	ATF_REQUIRE(err == 0);
    141  1.5.2.2  yamt 
    142  1.5.2.2  yamt 	/* ok, wait for the child to finish */
    143  1.5.2.2  yamt 	waitpid(pid, &status, 0);
    144  1.5.2.2  yamt 	ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
    145  1.5.2.2  yamt 
    146  1.5.2.2  yamt 	/* now check that output is twice as long as input */
    147  1.5.2.2  yamt 	insize = filesize(TESTFILE);
    148  1.5.2.2  yamt 	outsize = filesize(CHECKFILE);
    149  1.5.2.2  yamt 	ATF_REQUIRE(insize == strlen(TESTCONTENT));
    150  1.5.2.2  yamt 	ATF_REQUIRE(insize*2 == outsize);
    151  1.5.2.2  yamt 
    152  1.5.2.2  yamt 	/*
    153  1.5.2.2  yamt 	 * try a "cat < testfile  > checkfile" with input and output swapped
    154  1.5.2.2  yamt 	 */
    155  1.5.2.2  yamt 	make_testfile(TESTFILE);
    156  1.5.2.2  yamt 	empty_outfile(CHECKFILE);
    157  1.5.2.2  yamt 
    158  1.5.2.2  yamt 	posix_spawn_file_actions_init(&fa);
    159  1.5.2.2  yamt 	posix_spawn_file_actions_addopen(&fa, fileno(stdout),
    160  1.5.2.2  yamt 	    TESTFILE, O_RDONLY, 0);
    161  1.5.2.2  yamt 	posix_spawn_file_actions_addopen(&fa, fileno(stdin),
    162  1.5.2.2  yamt 	    CHECKFILE, O_WRONLY, 0);
    163  1.5.2.2  yamt 	err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
    164  1.5.2.2  yamt 	posix_spawn_file_actions_destroy(&fa);
    165  1.5.2.2  yamt 
    166  1.5.2.2  yamt 	ATF_REQUIRE(err == 0);
    167  1.5.2.2  yamt 
    168  1.5.2.2  yamt 	/* ok, wait for the child to finish */
    169  1.5.2.2  yamt 	waitpid(pid, &status, 0);
    170  1.5.2.2  yamt 	ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_FAILURE);
    171  1.5.2.2  yamt 
    172  1.5.2.2  yamt 	/* now check that input and output are still the same size */
    173  1.5.2.2  yamt 	insize = filesize(TESTFILE);
    174  1.5.2.2  yamt 	outsize = filesize(CHECKFILE);
    175  1.5.2.2  yamt 	ATF_REQUIRE(insize == strlen(TESTCONTENT));
    176  1.5.2.2  yamt 	ATF_REQUIRE(outsize == 0);
    177  1.5.2.2  yamt }
    178  1.5.2.2  yamt 
    179  1.5.2.2  yamt ATF_TC(t_spawn_reopen);
    180  1.5.2.2  yamt 
    181  1.5.2.2  yamt ATF_TC_HEAD(t_spawn_reopen, tc)
    182  1.5.2.2  yamt {
    183  1.5.2.2  yamt 	atf_tc_set_md_var(tc, "descr",
    184  1.5.2.2  yamt 	    "an open filehandle can be replaced by a 'open' fileaction");
    185  1.5.2.2  yamt 	atf_tc_set_md_var(tc, "require.progs", "/bin/cat");
    186  1.5.2.2  yamt }
    187  1.5.2.2  yamt 
    188  1.5.2.2  yamt ATF_TC_BODY(t_spawn_reopen, tc)
    189  1.5.2.2  yamt {
    190  1.5.2.2  yamt 	int status, err;
    191  1.5.2.2  yamt 	pid_t pid;
    192  1.5.2.2  yamt 	char * const args[2] = { __UNCONST("cat"), NULL };
    193  1.5.2.2  yamt 	posix_spawn_file_actions_t fa;
    194  1.5.2.2  yamt 
    195  1.5.2.2  yamt 	/*
    196  1.5.2.2  yamt 	 * make sure stdin is open in the parent
    197  1.5.2.2  yamt 	 */
    198  1.5.2.2  yamt 	freopen("/dev/zero", "r", stdin);
    199  1.5.2.2  yamt 	/*
    200  1.5.2.2  yamt 	 * now request an open for this fd again in the child
    201  1.5.2.2  yamt 	 */
    202  1.5.2.2  yamt 	posix_spawn_file_actions_init(&fa);
    203  1.5.2.2  yamt 	posix_spawn_file_actions_addopen(&fa, fileno(stdin),
    204  1.5.2.2  yamt 	    "/dev/null", O_RDONLY, 0);
    205  1.5.2.2  yamt 	err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
    206  1.5.2.2  yamt 	posix_spawn_file_actions_destroy(&fa);
    207  1.5.2.2  yamt 
    208  1.5.2.2  yamt 	ATF_REQUIRE(err == 0);
    209  1.5.2.2  yamt 
    210  1.5.2.2  yamt 	waitpid(pid, &status, 0);
    211  1.5.2.2  yamt 	ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
    212  1.5.2.2  yamt }
    213  1.5.2.2  yamt 
    214  1.5.2.2  yamt ATF_TC(t_spawn_open_nonexistent);
    215  1.5.2.2  yamt 
    216  1.5.2.2  yamt ATF_TC_HEAD(t_spawn_open_nonexistent, tc)
    217  1.5.2.2  yamt {
    218  1.5.2.2  yamt 	atf_tc_set_md_var(tc, "descr",
    219  1.5.2.2  yamt 	    "posix_spawn fails when a file to open does not exist");
    220  1.5.2.2  yamt 	atf_tc_set_md_var(tc, "require.progs", "/bin/cat");
    221  1.5.2.2  yamt }
    222  1.5.2.2  yamt 
    223  1.5.2.2  yamt ATF_TC_BODY(t_spawn_open_nonexistent, tc)
    224  1.5.2.2  yamt {
    225  1.5.2.2  yamt 	int err, status;
    226  1.5.2.2  yamt 	pid_t pid;
    227  1.5.2.2  yamt 	char * const args[2] = { __UNCONST("cat"), NULL };
    228  1.5.2.2  yamt 	posix_spawn_file_actions_t fa;
    229  1.5.2.2  yamt 
    230  1.5.2.2  yamt 	posix_spawn_file_actions_init(&fa);
    231  1.5.2.2  yamt 	posix_spawn_file_actions_addopen(&fa, STDIN_FILENO,
    232  1.5.2.2  yamt 	    "./non/ex/ist/ent", O_RDONLY, 0);
    233  1.5.2.2  yamt 	err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
    234  1.5.2.2  yamt 	if (err == 0) {
    235  1.5.2.2  yamt 		/*
    236  1.5.2.2  yamt 		 * The child has been created - it should fail and
    237  1.5.2.2  yamt 		 * return exit code 127
    238  1.5.2.2  yamt 		 */
    239  1.5.2.2  yamt 		waitpid(pid, &status, 0);
    240  1.5.2.2  yamt 		ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 127);
    241  1.5.2.2  yamt 	} else {
    242  1.5.2.2  yamt 		/*
    243  1.5.2.2  yamt 		 * The error has been noticed early enough, no child has
    244  1.5.2.2  yamt 		 * been run
    245  1.5.2.2  yamt 		 */
    246  1.5.2.2  yamt 		ATF_REQUIRE(err == ENOENT);
    247  1.5.2.2  yamt 	}
    248  1.5.2.2  yamt 	posix_spawn_file_actions_destroy(&fa);
    249  1.5.2.2  yamt }
    250  1.5.2.2  yamt 
    251  1.5.2.2  yamt ATF_TC(t_spawn_open_nonexistent_diag);
    252  1.5.2.2  yamt 
    253  1.5.2.2  yamt ATF_TC_HEAD(t_spawn_open_nonexistent_diag, tc)
    254  1.5.2.2  yamt {
    255  1.5.2.2  yamt 	atf_tc_set_md_var(tc, "descr",
    256  1.5.2.2  yamt 	    "posix_spawn fails when a file to open does not exist "
    257  1.5.2.2  yamt 	    "and delivers proper diagnostic");
    258  1.5.2.2  yamt 	atf_tc_set_md_var(tc, "require.progs", "/bin/cat");
    259  1.5.2.2  yamt }
    260  1.5.2.2  yamt 
    261  1.5.2.2  yamt ATF_TC_BODY(t_spawn_open_nonexistent_diag, tc)
    262  1.5.2.2  yamt {
    263  1.5.2.2  yamt 	int err;
    264  1.5.2.2  yamt 	pid_t pid;
    265  1.5.2.2  yamt 	char * const args[2] = { __UNCONST("cat"), NULL };
    266  1.5.2.2  yamt 	posix_spawnattr_t attr;
    267  1.5.2.2  yamt 	posix_spawn_file_actions_t fa;
    268  1.5.2.2  yamt 
    269  1.5.2.2  yamt 	posix_spawnattr_init(&attr);
    270  1.5.2.2  yamt 	/*
    271  1.5.2.2  yamt 	 * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that
    272  1.5.2.2  yamt 	 * will cause a "proper" return value from posix_spawn(2)
    273  1.5.2.2  yamt 	 * instead of a (potential) success there and a 127 exit
    274  1.5.2.2  yamt 	 * status from the child process (c.f. the non-diag variant
    275  1.5.2.2  yamt 	 * of this test).
    276  1.5.2.2  yamt 	 */
    277  1.5.2.2  yamt 	posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR);
    278  1.5.2.2  yamt 	posix_spawn_file_actions_init(&fa);
    279  1.5.2.2  yamt 	posix_spawn_file_actions_addopen(&fa, STDIN_FILENO,
    280  1.5.2.2  yamt 	    "./non/ex/ist/ent", O_RDONLY, 0);
    281  1.5.2.2  yamt 	err = posix_spawn(&pid, "/bin/cat", &fa, &attr, args, NULL);
    282  1.5.2.2  yamt 	ATF_REQUIRE(err == ENOENT);
    283  1.5.2.2  yamt 	posix_spawn_file_actions_destroy(&fa);
    284  1.5.2.2  yamt 	posix_spawnattr_destroy(&attr);
    285  1.5.2.2  yamt }
    286  1.5.2.2  yamt 
    287  1.5.2.2  yamt ATF_TC(t_spawn_fileactions);
    288  1.5.2.2  yamt 
    289  1.5.2.2  yamt ATF_TC_HEAD(t_spawn_fileactions, tc)
    290  1.5.2.2  yamt {
    291  1.5.2.2  yamt 	atf_tc_set_md_var(tc, "descr",
    292  1.5.2.2  yamt 	    "Tests various complex fileactions");
    293  1.5.2.2  yamt }
    294  1.5.2.2  yamt 
    295  1.5.2.2  yamt ATF_TC_BODY(t_spawn_fileactions, tc)
    296  1.5.2.2  yamt {
    297  1.5.2.2  yamt 	int fd1, fd2, fd3, status, err;
    298  1.5.2.2  yamt 	pid_t pid;
    299  1.5.2.2  yamt 	char * const args[2] = { __UNCONST("h_fileactions"), NULL };
    300  1.5.2.2  yamt 	char helper[FILENAME_MAX];
    301  1.5.2.2  yamt 	posix_spawn_file_actions_t fa;
    302  1.5.2.2  yamt 
    303  1.5.2.2  yamt 	posix_spawn_file_actions_init(&fa);
    304  1.5.2.2  yamt 
    305  1.5.2.2  yamt 	closefrom(fileno(stderr)+1);
    306  1.5.2.2  yamt 
    307  1.5.2.2  yamt 	fd1 = open("/dev/null", O_RDONLY);
    308  1.5.2.2  yamt 	ATF_REQUIRE(fd1 == 3);
    309  1.5.2.2  yamt 
    310  1.5.2.2  yamt 	fd2 = open("/dev/null", O_WRONLY, O_CLOEXEC);
    311  1.5.2.2  yamt 	ATF_REQUIRE(fd2 == 4);
    312  1.5.2.2  yamt 
    313  1.5.2.2  yamt 	fd3 = open("/dev/null", O_WRONLY);
    314  1.5.2.2  yamt 	ATF_REQUIRE(fd3 == 5);
    315  1.5.2.2  yamt 
    316  1.5.2.2  yamt 	posix_spawn_file_actions_addclose(&fa, fd1);
    317  1.5.2.2  yamt 	posix_spawn_file_actions_addopen(&fa, 6, "/dev/null", O_RDWR, 0);
    318  1.5.2.2  yamt 	posix_spawn_file_actions_adddup2(&fa, 1, 7);
    319  1.5.2.2  yamt 
    320  1.5.2.2  yamt 	snprintf(helper, sizeof helper, "%s/h_fileactions",
    321  1.5.2.2  yamt 	    atf_tc_get_config_var(tc, "srcdir"));
    322  1.5.2.2  yamt 	err = posix_spawn(&pid, helper, &fa, NULL, args, NULL);
    323  1.5.2.2  yamt 	posix_spawn_file_actions_destroy(&fa);
    324  1.5.2.2  yamt 
    325  1.5.2.2  yamt 	ATF_REQUIRE(err == 0);
    326  1.5.2.2  yamt 
    327  1.5.2.2  yamt 	waitpid(pid, &status, 0);
    328  1.5.2.2  yamt 	ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
    329  1.5.2.2  yamt }
    330  1.5.2.2  yamt 
    331  1.5.2.2  yamt ATF_TC(t_spawn_empty_fileactions);
    332  1.5.2.2  yamt 
    333  1.5.2.2  yamt ATF_TC_HEAD(t_spawn_empty_fileactions, tc)
    334  1.5.2.2  yamt {
    335  1.5.2.2  yamt 	atf_tc_set_md_var(tc, "descr",
    336  1.5.2.2  yamt 	    "posix_spawn with empty fileactions (PR kern/46038)");
    337  1.5.2.2  yamt 	atf_tc_set_md_var(tc, "require.progs", "/bin/cat");
    338  1.5.2.2  yamt }
    339  1.5.2.2  yamt 
    340  1.5.2.2  yamt ATF_TC_BODY(t_spawn_empty_fileactions, tc)
    341  1.5.2.2  yamt {
    342  1.5.2.2  yamt 	int status, err;
    343  1.5.2.2  yamt 	pid_t pid;
    344  1.5.2.2  yamt 	char * const args[2] = { __UNCONST("cat"), NULL };
    345  1.5.2.2  yamt 	posix_spawn_file_actions_t fa;
    346  1.5.2.2  yamt 	size_t insize, outsize;
    347  1.5.2.2  yamt 
    348  1.5.2.2  yamt 	/*
    349  1.5.2.2  yamt 	 * try a "cat < testfile > checkfile", but set up stdin/stdout
    350  1.5.2.2  yamt 	 * already in the parent and pass empty file actions to the child.
    351  1.5.2.2  yamt 	 */
    352  1.5.2.2  yamt 	make_testfile(TESTFILE);
    353  1.5.2.2  yamt 	unlink(CHECKFILE);
    354  1.5.2.2  yamt 
    355  1.5.2.2  yamt 	freopen(TESTFILE, "r", stdin);
    356  1.5.2.2  yamt 	freopen(CHECKFILE, "w", stdout);
    357  1.5.2.2  yamt 
    358  1.5.2.2  yamt 	posix_spawn_file_actions_init(&fa);
    359  1.5.2.2  yamt 	err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
    360  1.5.2.2  yamt 	posix_spawn_file_actions_destroy(&fa);
    361  1.5.2.2  yamt 
    362  1.5.2.2  yamt 	ATF_REQUIRE(err == 0);
    363  1.5.2.2  yamt 
    364  1.5.2.2  yamt 	/* ok, wait for the child to finish */
    365  1.5.2.2  yamt 	waitpid(pid, &status, 0);
    366  1.5.2.2  yamt 	ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
    367  1.5.2.2  yamt 
    368  1.5.2.2  yamt 	/* now check that input and output have the same size */
    369  1.5.2.2  yamt 	insize = filesize(TESTFILE);
    370  1.5.2.2  yamt 	outsize = filesize(CHECKFILE);
    371  1.5.2.2  yamt 	ATF_REQUIRE(insize == strlen(TESTCONTENT));
    372  1.5.2.2  yamt 	ATF_REQUIRE(insize == outsize);
    373  1.5.2.2  yamt }
    374  1.5.2.2  yamt 
    375  1.5.2.2  yamt ATF_TP_ADD_TCS(tp)
    376  1.5.2.2  yamt {
    377  1.5.2.2  yamt 	ATF_TP_ADD_TC(tp, t_spawn_fileactions);
    378  1.5.2.2  yamt 	ATF_TP_ADD_TC(tp, t_spawn_open_nonexistent);
    379  1.5.2.2  yamt 	ATF_TP_ADD_TC(tp, t_spawn_open_nonexistent_diag);
    380  1.5.2.2  yamt 	ATF_TP_ADD_TC(tp, t_spawn_reopen);
    381  1.5.2.2  yamt 	ATF_TP_ADD_TC(tp, t_spawn_openmode);
    382  1.5.2.2  yamt 	ATF_TP_ADD_TC(tp, t_spawn_empty_fileactions);
    383  1.5.2.2  yamt 
    384  1.5.2.2  yamt 	return atf_no_error();
    385  1.5.2.2  yamt }
    386