Home | History | Annotate | Line # | Download | only in posix_spawn
t_fileactions.c revision 1.2.2.1
      1  1.2.2.1  sborrill /* $NetBSD: t_fileactions.c,v 1.2.2.1 2012/02/20 21:54:57 sborrill 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.2    martin 	int err;
    226      1.2    martin 	pid_t pid;
    227      1.2    martin 	char * const args[2] = { __UNCONST("cat"), NULL };
    228      1.2    martin 	posix_spawn_file_actions_t fa;
    229      1.2    martin 
    230      1.2    martin 	posix_spawn_file_actions_init(&fa);
    231      1.2    martin 	posix_spawn_file_actions_addopen(&fa, STDIN_FILENO,
    232      1.2    martin 	    "./non/ex/ist/ent", O_RDONLY, 0);
    233      1.2    martin 	err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
    234      1.2    martin 	ATF_REQUIRE(err == ENOENT);
    235      1.2    martin 	posix_spawn_file_actions_destroy(&fa);
    236      1.2    martin }
    237      1.2    martin 
    238      1.1    martin ATF_TC(t_spawn_fileactions);
    239      1.1    martin 
    240      1.1    martin ATF_TC_HEAD(t_spawn_fileactions, tc)
    241      1.1    martin {
    242      1.1    martin 	atf_tc_set_md_var(tc, "descr",
    243      1.2    martin 	    "Tests various complex fileactions");
    244      1.1    martin }
    245      1.1    martin 
    246      1.1    martin ATF_TC_BODY(t_spawn_fileactions, tc)
    247      1.1    martin {
    248      1.1    martin 	int fd1, fd2, fd3, status, err;
    249      1.1    martin 	pid_t pid;
    250      1.1    martin 	char * const args[2] = { __UNCONST("h_fileactions"), NULL };
    251      1.1    martin 	char helper[FILENAME_MAX];
    252      1.1    martin 	posix_spawn_file_actions_t fa;
    253      1.1    martin 
    254      1.1    martin 	posix_spawn_file_actions_init(&fa);
    255      1.1    martin 
    256      1.1    martin 	closefrom(fileno(stderr)+1);
    257      1.1    martin 
    258      1.1    martin 	fd1 = open("/dev/null", O_RDONLY);
    259      1.1    martin 	ATF_REQUIRE(fd1 == 3);
    260      1.1    martin 
    261      1.1    martin 	fd2 = open("/dev/null", O_WRONLY, O_CLOEXEC);
    262      1.1    martin 	ATF_REQUIRE(fd2 == 4);
    263      1.1    martin 
    264      1.1    martin 	fd3 = open("/dev/null", O_WRONLY);
    265      1.1    martin 	ATF_REQUIRE(fd3 == 5);
    266      1.1    martin 
    267      1.1    martin 	posix_spawn_file_actions_addclose(&fa, fd1);
    268      1.1    martin 	posix_spawn_file_actions_addopen(&fa, 6, "/dev/null", O_RDWR, 0);
    269      1.1    martin 	posix_spawn_file_actions_adddup2(&fa, 1, 7);
    270      1.1    martin 
    271      1.1    martin 	snprintf(helper, sizeof helper, "%s/h_fileactions",
    272      1.1    martin 	    atf_tc_get_config_var(tc, "srcdir"));
    273      1.1    martin 	err = posix_spawn(&pid, helper, &fa, NULL, args, NULL);
    274      1.2    martin 	posix_spawn_file_actions_destroy(&fa);
    275      1.2    martin 
    276      1.1    martin 	ATF_REQUIRE(err == 0);
    277      1.1    martin 
    278      1.1    martin 	waitpid(pid, &status, 0);
    279      1.1    martin 	ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
    280      1.1    martin }
    281      1.1    martin 
    282  1.2.2.1  sborrill ATF_TC(t_spawn_empty_fileactions);
    283  1.2.2.1  sborrill 
    284  1.2.2.1  sborrill ATF_TC_HEAD(t_spawn_empty_fileactions, tc)
    285  1.2.2.1  sborrill {
    286  1.2.2.1  sborrill 	atf_tc_set_md_var(tc, "descr",
    287  1.2.2.1  sborrill 	    "posix_spawn with empty fileactions (PR kern/46038)");
    288  1.2.2.1  sborrill 	atf_tc_set_md_var(tc, "require.progs", "/bin/cat");
    289  1.2.2.1  sborrill }
    290  1.2.2.1  sborrill 
    291  1.2.2.1  sborrill ATF_TC_BODY(t_spawn_empty_fileactions, tc)
    292  1.2.2.1  sborrill {
    293  1.2.2.1  sborrill 	int status, err;
    294  1.2.2.1  sborrill 	pid_t pid;
    295  1.2.2.1  sborrill 	char * const args[2] = { __UNCONST("cat"), NULL };
    296  1.2.2.1  sborrill 	posix_spawn_file_actions_t fa;
    297  1.2.2.1  sborrill 	size_t insize, outsize;
    298  1.2.2.1  sborrill 
    299  1.2.2.1  sborrill 	/*
    300  1.2.2.1  sborrill 	 * try a "cat < testfile > checkfile", but set up stdin/stdout
    301  1.2.2.1  sborrill 	 * already in the parent and pass empty file actions to the child.
    302  1.2.2.1  sborrill 	 */
    303  1.2.2.1  sborrill 	make_testfile(TESTFILE);
    304  1.2.2.1  sborrill 	unlink(CHECKFILE);
    305  1.2.2.1  sborrill 
    306  1.2.2.1  sborrill 	freopen(TESTFILE, "r", stdin);
    307  1.2.2.1  sborrill 	freopen(CHECKFILE, "w", stdout);
    308  1.2.2.1  sborrill 
    309  1.2.2.1  sborrill 	posix_spawn_file_actions_init(&fa);
    310  1.2.2.1  sborrill 	err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL);
    311  1.2.2.1  sborrill 	posix_spawn_file_actions_destroy(&fa);
    312  1.2.2.1  sborrill 
    313  1.2.2.1  sborrill 	ATF_REQUIRE(err == 0);
    314  1.2.2.1  sborrill 
    315  1.2.2.1  sborrill 	/* ok, wait for the child to finish */
    316  1.2.2.1  sborrill 	waitpid(pid, &status, 0);
    317  1.2.2.1  sborrill 	ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
    318  1.2.2.1  sborrill 
    319  1.2.2.1  sborrill 	/* now check that input and output have the same size */
    320  1.2.2.1  sborrill 	insize = filesize(TESTFILE);
    321  1.2.2.1  sborrill 	outsize = filesize(CHECKFILE);
    322  1.2.2.1  sborrill 	ATF_REQUIRE(insize == strlen(TESTCONTENT));
    323  1.2.2.1  sborrill 	ATF_REQUIRE(insize == outsize);
    324  1.2.2.1  sborrill }
    325  1.2.2.1  sborrill 
    326      1.1    martin ATF_TP_ADD_TCS(tp)
    327      1.1    martin {
    328      1.1    martin 	ATF_TP_ADD_TC(tp, t_spawn_fileactions);
    329      1.2    martin 	ATF_TP_ADD_TC(tp, t_spawn_open_nonexistent);
    330      1.2    martin 	ATF_TP_ADD_TC(tp, t_spawn_reopen);
    331      1.2    martin 	ATF_TP_ADD_TC(tp, t_spawn_openmode);
    332  1.2.2.1  sborrill 	ATF_TP_ADD_TC(tp, t_spawn_empty_fileactions);
    333      1.1    martin 
    334      1.1    martin 	return atf_no_error();
    335      1.1    martin }
    336