Home | History | Annotate | Line # | Download | only in posix_spawn
t_fileactions.c revision 1.1
      1 /* $NetBSD: t_fileactions.c,v 1.1 2012/02/13 21:03:08 martin Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles Zhang <charles (at) NetBSD.org> and
      9  * Martin Husemann <martin (at) NetBSD.org>.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     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.
     19  *
     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.
     31  */
     32 
     33 
     34 #include <atf-c.h>
     35 #include <stdio.h>
     36 #include <stdlib.h>
     37 #include <string.h>
     38 #include <fcntl.h>
     39 #include <spawn.h>
     40 #include <unistd.h>
     41 #include <sys/wait.h>
     42 
     43 ATF_TC(t_spawn_fileactions);
     44 
     45 ATF_TC_HEAD(t_spawn_fileactions, tc)
     46 {
     47 	atf_tc_set_md_var(tc, "descr",
     48 	    "Tests posix_spawn with fileactions");
     49 }
     50 
     51 ATF_TC_BODY(t_spawn_fileactions, tc)
     52 {
     53 	int fd1, fd2, fd3, status, err;
     54 	pid_t pid;
     55 	char * const args[2] = { __UNCONST("h_fileactions"), NULL };
     56 	char helper[FILENAME_MAX];
     57 	posix_spawn_file_actions_t fa;
     58 
     59 	posix_spawn_file_actions_init(&fa);
     60 
     61 	closefrom(fileno(stderr)+1);
     62 
     63 	fd1 = open("/dev/null", O_RDONLY);
     64 	ATF_REQUIRE(fd1 == 3);
     65 
     66 	fd2 = open("/dev/null", O_WRONLY, O_CLOEXEC);
     67 	ATF_REQUIRE(fd2 == 4);
     68 
     69 	fd3 = open("/dev/null", O_WRONLY);
     70 	ATF_REQUIRE(fd3 == 5);
     71 
     72 	posix_spawn_file_actions_addclose(&fa, fd1);
     73 	posix_spawn_file_actions_addopen(&fa, 6, "/dev/null", O_RDWR, 0);
     74 	posix_spawn_file_actions_adddup2(&fa, 1, 7);
     75 
     76 	snprintf(helper, sizeof helper, "%s/h_fileactions",
     77 	    atf_tc_get_config_var(tc, "srcdir"));
     78 	err = posix_spawn(&pid, helper, &fa, NULL, args, NULL);
     79 	ATF_REQUIRE(err == 0);
     80 
     81 	waitpid(pid, &status, 0);
     82 	ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
     83 
     84 	posix_spawn_file_actions_destroy(&fa);
     85 }
     86 
     87 ATF_TP_ADD_TCS(tp)
     88 {
     89 	ATF_TP_ADD_TC(tp, t_spawn_fileactions);
     90 
     91 	return atf_no_error();
     92 }
     93