Home | History | Annotate | Line # | Download | only in sys
t_pipe2.c revision 1.2.2.2
      1  1.2.2.2      yamt /* $NetBSD: t_pipe2.c,v 1.2.2.2 2012/05/23 10:08:21 yamt Exp $ */
      2      1.1  christos 
      3      1.1  christos /*-
      4      1.1  christos  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5      1.1  christos  * All rights reserved.
      6      1.1  christos  *
      7      1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1  christos  * by Christos Zoulas.
      9      1.1  christos  *
     10      1.1  christos  * Redistribution and use in source and binary forms, with or without
     11      1.1  christos  * modification, are permitted provided that the following conditions
     12      1.1  christos  * are met:
     13      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  christos  *    documentation and/or other materials provided with the distribution.
     18      1.1  christos  * 3. All advertising materials mentioning features or use of this software
     19      1.1  christos  *    must display the following acknowledgement:
     20      1.1  christos  *        This product includes software developed by the NetBSD
     21      1.1  christos  *        Foundation, Inc. and its contributors.
     22      1.1  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.1  christos  *    contributors may be used to endorse or promote products derived
     24      1.1  christos  *    from this software without specific prior written permission.
     25      1.1  christos  *
     26      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     37      1.1  christos  */
     38      1.1  christos #include <sys/cdefs.h>
     39  1.2.2.2      yamt __RCSID("$NetBSD: t_pipe2.c,v 1.2.2.2 2012/05/23 10:08:21 yamt Exp $");
     40      1.1  christos 
     41      1.1  christos #include <atf-c.h>
     42      1.1  christos #include <fcntl.h>
     43      1.1  christos #include <unistd.h>
     44      1.1  christos #include <stdlib.h>
     45      1.1  christos #include <errno.h>
     46  1.2.2.2      yamt #include <sys/resource.h>
     47      1.1  christos 
     48      1.1  christos static void
     49      1.1  christos run(int flags)
     50      1.1  christos {
     51      1.1  christos 	int fd[2], i;
     52      1.1  christos 
     53      1.2  christos 	while ((i = open("/", O_RDONLY)) < 3)
     54      1.1  christos 		ATF_REQUIRE(i != -1);
     55      1.1  christos 
     56      1.1  christos 	ATF_REQUIRE(fcntl(3, F_CLOSEM) != -1);
     57      1.1  christos 
     58      1.2  christos 	ATF_REQUIRE(pipe2(fd, flags) == 0);
     59      1.1  christos 
     60      1.1  christos 	ATF_REQUIRE(fd[0] == 3);
     61      1.1  christos 	ATF_REQUIRE(fd[1] == 4);
     62      1.1  christos 
     63      1.1  christos 	if (flags & O_CLOEXEC) {
     64      1.1  christos 		ATF_REQUIRE((fcntl(fd[0], F_GETFD) & FD_CLOEXEC) != 0);
     65      1.1  christos 		ATF_REQUIRE((fcntl(fd[1], F_GETFD) & FD_CLOEXEC) != 0);
     66      1.1  christos 	} else {
     67      1.1  christos 		ATF_REQUIRE((fcntl(fd[0], F_GETFD) & FD_CLOEXEC) == 0);
     68      1.1  christos 		ATF_REQUIRE((fcntl(fd[1], F_GETFD) & FD_CLOEXEC) == 0);
     69      1.1  christos 	}
     70      1.1  christos 
     71      1.2  christos 	if (flags & O_NONBLOCK) {
     72      1.1  christos 		ATF_REQUIRE((fcntl(fd[0], F_GETFL) & O_NONBLOCK) != 0);
     73      1.1  christos 		ATF_REQUIRE((fcntl(fd[1], F_GETFL) & O_NONBLOCK) != 0);
     74      1.1  christos 	} else {
     75      1.1  christos 		ATF_REQUIRE((fcntl(fd[0], F_GETFL) & O_NONBLOCK) == 0);
     76      1.1  christos 		ATF_REQUIRE((fcntl(fd[1], F_GETFL) & O_NONBLOCK) == 0);
     77      1.1  christos 	}
     78      1.1  christos 
     79  1.2.2.1      yamt 	if (flags & O_NOSIGPIPE) {
     80  1.2.2.1      yamt 		ATF_REQUIRE(fcntl(fd[0], F_GETNOSIGPIPE) != 0);
     81  1.2.2.1      yamt 		ATF_REQUIRE(fcntl(fd[1], F_GETNOSIGPIPE) != 0);
     82  1.2.2.1      yamt 	} else {
     83  1.2.2.1      yamt 		ATF_REQUIRE(fcntl(fd[0], F_GETNOSIGPIPE) == 0);
     84  1.2.2.1      yamt 		ATF_REQUIRE(fcntl(fd[1], F_GETNOSIGPIPE) == 0);
     85  1.2.2.1      yamt 	}
     86  1.2.2.1      yamt 
     87      1.1  christos 	ATF_REQUIRE(close(fd[0]) != -1);
     88      1.1  christos 	ATF_REQUIRE(close(fd[1]) != -1);
     89      1.1  christos }
     90      1.1  christos 
     91      1.1  christos ATF_TC(pipe2_basic);
     92      1.1  christos ATF_TC_HEAD(pipe2_basic, tc)
     93      1.1  christos {
     94      1.1  christos 	atf_tc_set_md_var(tc, "descr", "A basic test of pipe2(2)");
     95      1.1  christos }
     96      1.1  christos 
     97      1.1  christos ATF_TC_BODY(pipe2_basic, tc)
     98      1.1  christos {
     99      1.1  christos 	run(0);
    100      1.1  christos }
    101      1.1  christos 
    102  1.2.2.2      yamt ATF_TC(pipe2_consume);
    103  1.2.2.2      yamt ATF_TC_HEAD(pipe2_consume, tc)
    104  1.2.2.2      yamt {
    105  1.2.2.2      yamt 	atf_tc_set_md_var(tc, "descr", "Test that consuming file descriptors "
    106  1.2.2.2      yamt 	    "with pipe2(2) does not crash the system (PR kern/46457)");
    107  1.2.2.2      yamt }
    108  1.2.2.2      yamt 
    109  1.2.2.2      yamt ATF_TC_BODY(pipe2_consume, tc)
    110  1.2.2.2      yamt {
    111  1.2.2.2      yamt 	struct rlimit rl;
    112  1.2.2.2      yamt 	int err, filedes[2];
    113  1.2.2.2      yamt 
    114  1.2.2.2      yamt 	err = fcntl(4, F_CLOSEM);
    115  1.2.2.2      yamt 	ATF_REQUIRE(err == 0);
    116  1.2.2.2      yamt 
    117  1.2.2.2      yamt 	err = getrlimit(RLIMIT_NOFILE, &rl);
    118  1.2.2.2      yamt 	ATF_REQUIRE(err == 0);
    119  1.2.2.2      yamt 	/*
    120  1.2.2.2      yamt 	 * The heart of this test is to run against the number of open
    121  1.2.2.2      yamt 	 * file descriptor limit in the middle of a pipe2() call - i.e.
    122  1.2.2.2      yamt 	 * before the call only a single descriptor may be openend.
    123  1.2.2.2      yamt 	 */
    124  1.2.2.2      yamt 	rl.rlim_cur = 4;
    125  1.2.2.2      yamt 	err = setrlimit(RLIMIT_NOFILE, &rl);
    126  1.2.2.2      yamt 	ATF_REQUIRE(err == 0);
    127  1.2.2.2      yamt 
    128  1.2.2.2      yamt 	err = pipe2(filedes, O_CLOEXEC);
    129  1.2.2.2      yamt 	ATF_REQUIRE(err == -1);
    130  1.2.2.2      yamt }
    131  1.2.2.2      yamt 
    132      1.1  christos ATF_TC(pipe2_nonblock);
    133      1.1  christos ATF_TC_HEAD(pipe2_nonblock, tc)
    134      1.1  christos {
    135      1.1  christos 	atf_tc_set_md_var(tc, "descr", "A non-blocking test of pipe2(2)");
    136      1.1  christos }
    137      1.1  christos 
    138      1.1  christos ATF_TC_BODY(pipe2_nonblock, tc)
    139      1.1  christos {
    140      1.1  christos 	run(O_NONBLOCK);
    141      1.1  christos }
    142      1.1  christos 
    143      1.1  christos ATF_TC(pipe2_cloexec);
    144      1.1  christos ATF_TC_HEAD(pipe2_cloexec, tc)
    145      1.1  christos {
    146  1.2.2.1      yamt 	atf_tc_set_md_var(tc, "descr", "A close-on-exec test of pipe2(2)");
    147      1.1  christos }
    148      1.1  christos 
    149      1.1  christos ATF_TC_BODY(pipe2_cloexec, tc)
    150      1.1  christos {
    151      1.1  christos 	run(O_CLOEXEC);
    152      1.1  christos }
    153      1.1  christos 
    154  1.2.2.1      yamt ATF_TC(pipe2_nosigpipe);
    155  1.2.2.1      yamt ATF_TC_HEAD(pipe2_nosigpipe, tc)
    156  1.2.2.1      yamt {
    157  1.2.2.1      yamt 	atf_tc_set_md_var(tc, "descr", "A no sigpipe test of pipe2(2)");
    158  1.2.2.1      yamt }
    159  1.2.2.1      yamt 
    160  1.2.2.1      yamt ATF_TC_BODY(pipe2_nosigpipe, tc)
    161  1.2.2.1      yamt {
    162  1.2.2.1      yamt 	run(O_NOSIGPIPE);
    163  1.2.2.1      yamt }
    164  1.2.2.1      yamt 
    165      1.1  christos ATF_TC(pipe2_einval);
    166      1.1  christos ATF_TC_HEAD(pipe2_einval, tc)
    167      1.1  christos {
    168      1.1  christos 	atf_tc_set_md_var(tc, "descr", "A error check of pipe2(2)");
    169      1.1  christos }
    170      1.1  christos 
    171      1.1  christos ATF_TC_BODY(pipe2_einval, tc)
    172      1.1  christos {
    173      1.1  christos 	int fd[2];
    174      1.1  christos 	ATF_REQUIRE_ERRNO(EINVAL, pipe2(fd, O_ASYNC) == -1);
    175      1.1  christos }
    176      1.1  christos 
    177      1.1  christos ATF_TP_ADD_TCS(tp)
    178      1.1  christos {
    179      1.1  christos 
    180      1.1  christos 	ATF_TP_ADD_TC(tp, pipe2_basic);
    181  1.2.2.2      yamt 	ATF_TP_ADD_TC(tp, pipe2_consume);
    182      1.1  christos 	ATF_TP_ADD_TC(tp, pipe2_nonblock);
    183      1.1  christos 	ATF_TP_ADD_TC(tp, pipe2_cloexec);
    184  1.2.2.1      yamt 	ATF_TP_ADD_TC(tp, pipe2_nosigpipe);
    185      1.1  christos 	ATF_TP_ADD_TC(tp, pipe2_einval);
    186      1.1  christos 
    187      1.1  christos 	return atf_no_error();
    188      1.1  christos }
    189