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