xref: /minix3/tests/lib/libc/sys/t_dup.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /* $NetBSD: t_dup.c,v 1.8 2012/03/18 07:00:51 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 Jukka Ruohonen.
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  *
19*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*11be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*11be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*11be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*11be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*11be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*11be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*11be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*11be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*11be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
30*11be35a1SLionel Sambuc  */
31*11be35a1SLionel Sambuc #include <sys/cdefs.h>
32*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_dup.c,v 1.8 2012/03/18 07:00:51 jruoho Exp $");
33*11be35a1SLionel Sambuc 
34*11be35a1SLionel Sambuc #include <sys/resource.h>
35*11be35a1SLionel Sambuc #include <sys/stat.h>
36*11be35a1SLionel Sambuc #include <sys/wait.h>
37*11be35a1SLionel Sambuc 
38*11be35a1SLionel Sambuc #include <atf-c.h>
39*11be35a1SLionel Sambuc #include <errno.h>
40*11be35a1SLionel Sambuc #include <fcntl.h>
41*11be35a1SLionel Sambuc #include <limits.h>
42*11be35a1SLionel Sambuc #include <stdio.h>
43*11be35a1SLionel Sambuc #include <stdlib.h>
44*11be35a1SLionel Sambuc #include <string.h>
45*11be35a1SLionel Sambuc #include <unistd.h>
46*11be35a1SLionel Sambuc #include <sysexits.h>
47*11be35a1SLionel Sambuc 
48*11be35a1SLionel Sambuc static char	path[] = "dup";
49*11be35a1SLionel Sambuc static void	check_mode(bool, bool, bool);
50*11be35a1SLionel Sambuc 
51*11be35a1SLionel Sambuc static void
check_mode(bool _dup,bool _dup2,bool _dup3)52*11be35a1SLionel Sambuc check_mode(bool _dup, bool _dup2, bool _dup3)
53*11be35a1SLionel Sambuc {
54*11be35a1SLionel Sambuc 	int mode[3] = { O_RDONLY, O_WRONLY, O_RDWR   };
55*11be35a1SLionel Sambuc 	int perm[5] = { 0700, 0400, 0600, 0444, 0666 };
56*11be35a1SLionel Sambuc 	struct stat st, st1;
57*11be35a1SLionel Sambuc 	int fd, fd1, fd2;
58*11be35a1SLionel Sambuc 	size_t i, j;
59*11be35a1SLionel Sambuc 
60*11be35a1SLionel Sambuc 	/*
61*11be35a1SLionel Sambuc 	 * Check that a duplicated descriptor
62*11be35a1SLionel Sambuc 	 * retains the mode of the original file.
63*11be35a1SLionel Sambuc 	 */
64*11be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(mode); i++) {
65*11be35a1SLionel Sambuc 
66*11be35a1SLionel Sambuc 		for (j = 0; j < __arraycount(perm); j++) {
67*11be35a1SLionel Sambuc 
68*11be35a1SLionel Sambuc 			fd1 = open(path, mode[i] | O_CREAT, perm[j]);
69*11be35a1SLionel Sambuc 			fd2 = open("/etc/passwd", O_RDONLY);
70*11be35a1SLionel Sambuc 
71*11be35a1SLionel Sambuc 			ATF_REQUIRE(fd1 >= 0);
72*11be35a1SLionel Sambuc 			ATF_REQUIRE(fd2 >= 0);
73*11be35a1SLionel Sambuc 
74*11be35a1SLionel Sambuc 			if (_dup != false)
75*11be35a1SLionel Sambuc 				fd = dup(fd1);
76*11be35a1SLionel Sambuc 			else if (_dup2 != false)
77*11be35a1SLionel Sambuc 				fd = dup2(fd1, fd2);
78*11be35a1SLionel Sambuc 			else if (_dup3 != false)
79*11be35a1SLionel Sambuc 				fd = dup3(fd1, fd2, O_CLOEXEC);
80*11be35a1SLionel Sambuc 			else {
81*11be35a1SLionel Sambuc 				fd = -1;
82*11be35a1SLionel Sambuc 			}
83*11be35a1SLionel Sambuc 
84*11be35a1SLionel Sambuc 			ATF_REQUIRE(fd >= 0);
85*11be35a1SLionel Sambuc 
86*11be35a1SLionel Sambuc 			(void)memset(&st, 0, sizeof(struct stat));
87*11be35a1SLionel Sambuc 			(void)memset(&st1, 0, sizeof(struct stat));
88*11be35a1SLionel Sambuc 
89*11be35a1SLionel Sambuc 			ATF_REQUIRE(fstat(fd, &st) == 0);
90*11be35a1SLionel Sambuc 			ATF_REQUIRE(fstat(fd1, &st1) == 0);
91*11be35a1SLionel Sambuc 
92*11be35a1SLionel Sambuc 			if (st.st_mode != st1.st_mode)
93*11be35a1SLionel Sambuc 				atf_tc_fail("invalid mode");
94*11be35a1SLionel Sambuc 
95*11be35a1SLionel Sambuc 			(void)close(fd);
96*11be35a1SLionel Sambuc 			(void)close(fd1);
97*11be35a1SLionel Sambuc 			(void)close(fd2);
98*11be35a1SLionel Sambuc 			(void)unlink(path);
99*11be35a1SLionel Sambuc 		}
100*11be35a1SLionel Sambuc 	}
101*11be35a1SLionel Sambuc }
102*11be35a1SLionel Sambuc 
103*11be35a1SLionel Sambuc ATF_TC(dup2_basic);
ATF_TC_HEAD(dup2_basic,tc)104*11be35a1SLionel Sambuc ATF_TC_HEAD(dup2_basic, tc)
105*11be35a1SLionel Sambuc {
106*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "A basic test of dup2(2)");
107*11be35a1SLionel Sambuc }
108*11be35a1SLionel Sambuc 
ATF_TC_BODY(dup2_basic,tc)109*11be35a1SLionel Sambuc ATF_TC_BODY(dup2_basic, tc)
110*11be35a1SLionel Sambuc {
111*11be35a1SLionel Sambuc 	int fd, fd1, fd2;
112*11be35a1SLionel Sambuc 
113*11be35a1SLionel Sambuc 	fd1 = open("/etc/passwd", O_RDONLY);
114*11be35a1SLionel Sambuc 	fd2 = open("/etc/passwd", O_RDONLY);
115*11be35a1SLionel Sambuc 
116*11be35a1SLionel Sambuc 	ATF_REQUIRE(fd1 >= 0);
117*11be35a1SLionel Sambuc 	ATF_REQUIRE(fd2 >= 0);
118*11be35a1SLionel Sambuc 
119*11be35a1SLionel Sambuc 	fd = dup2(fd1, fd2);
120*11be35a1SLionel Sambuc 	ATF_REQUIRE(fd >= 0);
121*11be35a1SLionel Sambuc 
122*11be35a1SLionel Sambuc 	if (fd != fd2)
123*11be35a1SLionel Sambuc 		atf_tc_fail("invalid descriptor");
124*11be35a1SLionel Sambuc 
125*11be35a1SLionel Sambuc 	(void)close(fd);
126*11be35a1SLionel Sambuc 	(void)close(fd1);
127*11be35a1SLionel Sambuc 
128*11be35a1SLionel Sambuc 	ATF_REQUIRE(close(fd2) != 0);
129*11be35a1SLionel Sambuc }
130*11be35a1SLionel Sambuc 
131*11be35a1SLionel Sambuc ATF_TC(dup2_err);
ATF_TC_HEAD(dup2_err,tc)132*11be35a1SLionel Sambuc ATF_TC_HEAD(dup2_err, tc)
133*11be35a1SLionel Sambuc {
134*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test error conditions of dup2(2)");
135*11be35a1SLionel Sambuc }
136*11be35a1SLionel Sambuc 
ATF_TC_BODY(dup2_err,tc)137*11be35a1SLionel Sambuc ATF_TC_BODY(dup2_err, tc)
138*11be35a1SLionel Sambuc {
139*11be35a1SLionel Sambuc 	int fd;
140*11be35a1SLionel Sambuc 
141*11be35a1SLionel Sambuc 	fd = open("/etc/passwd", O_RDONLY);
142*11be35a1SLionel Sambuc 	ATF_REQUIRE(fd >= 0);
143*11be35a1SLionel Sambuc 
144*11be35a1SLionel Sambuc 	errno = 0;
145*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EBADF, dup2(-1, -1) == -1);
146*11be35a1SLionel Sambuc 
147*11be35a1SLionel Sambuc 	errno = 0;
148*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EBADF, dup2(fd, -1) == -1);
149*11be35a1SLionel Sambuc 
150*11be35a1SLionel Sambuc 	errno = 0;
151*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EBADF, dup2(-1, fd) == -1);
152*11be35a1SLionel Sambuc 
153*11be35a1SLionel Sambuc 	/*
154*11be35a1SLionel Sambuc 	 * Note that this should not fail with EINVAL.
155*11be35a1SLionel Sambuc 	 */
156*11be35a1SLionel Sambuc 	ATF_REQUIRE(dup2(fd, fd) != -1);
157*11be35a1SLionel Sambuc 
158*11be35a1SLionel Sambuc 	(void)close(fd);
159*11be35a1SLionel Sambuc }
160*11be35a1SLionel Sambuc 
161*11be35a1SLionel Sambuc ATF_TC(dup2_max);
ATF_TC_HEAD(dup2_max,tc)162*11be35a1SLionel Sambuc ATF_TC_HEAD(dup2_max, tc)
163*11be35a1SLionel Sambuc {
164*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test dup2(2) against limits");
165*11be35a1SLionel Sambuc }
166*11be35a1SLionel Sambuc 
ATF_TC_BODY(dup2_max,tc)167*11be35a1SLionel Sambuc ATF_TC_BODY(dup2_max, tc)
168*11be35a1SLionel Sambuc {
169*11be35a1SLionel Sambuc 	struct rlimit res;
170*11be35a1SLionel Sambuc 
171*11be35a1SLionel Sambuc 	(void)memset(&res, 0, sizeof(struct rlimit));
172*11be35a1SLionel Sambuc 	(void)getrlimit(RLIMIT_NOFILE, &res);
173*11be35a1SLionel Sambuc 
174*11be35a1SLionel Sambuc 	errno = 0;
175*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EBADF, dup2(STDERR_FILENO, res.rlim_cur + 1) == -1);
176*11be35a1SLionel Sambuc }
177*11be35a1SLionel Sambuc 
178*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(dup2_mode);
ATF_TC_HEAD(dup2_mode,tc)179*11be35a1SLionel Sambuc ATF_TC_HEAD(dup2_mode, tc)
180*11be35a1SLionel Sambuc {
181*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "A basic test of dup2(2)");
182*11be35a1SLionel Sambuc }
183*11be35a1SLionel Sambuc 
ATF_TC_BODY(dup2_mode,tc)184*11be35a1SLionel Sambuc ATF_TC_BODY(dup2_mode, tc)
185*11be35a1SLionel Sambuc {
186*11be35a1SLionel Sambuc 	check_mode(false, true, false);
187*11be35a1SLionel Sambuc }
188*11be35a1SLionel Sambuc 
ATF_TC_CLEANUP(dup2_mode,tc)189*11be35a1SLionel Sambuc ATF_TC_CLEANUP(dup2_mode, tc)
190*11be35a1SLionel Sambuc {
191*11be35a1SLionel Sambuc 	(void)unlink(path);
192*11be35a1SLionel Sambuc }
193*11be35a1SLionel Sambuc 
194*11be35a1SLionel Sambuc 
195*11be35a1SLionel Sambuc ATF_TC(dup3_err);
ATF_TC_HEAD(dup3_err,tc)196*11be35a1SLionel Sambuc ATF_TC_HEAD(dup3_err, tc)
197*11be35a1SLionel Sambuc {
198*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
199*11be35a1SLionel Sambuc 	    "Test error conditions of dup3(2) (PR lib/45148)");
200*11be35a1SLionel Sambuc }
201*11be35a1SLionel Sambuc 
ATF_TC_BODY(dup3_err,tc)202*11be35a1SLionel Sambuc ATF_TC_BODY(dup3_err, tc)
203*11be35a1SLionel Sambuc {
204*11be35a1SLionel Sambuc 	int fd;
205*11be35a1SLionel Sambuc 
206*11be35a1SLionel Sambuc 	fd = open("/etc/passwd", O_RDONLY);
207*11be35a1SLionel Sambuc 	ATF_REQUIRE(fd >= 0);
208*11be35a1SLionel Sambuc 
209*11be35a1SLionel Sambuc 	errno = 0;
210*11be35a1SLionel Sambuc 	ATF_REQUIRE(dup3(fd, fd, O_CLOEXEC) != -1);
211*11be35a1SLionel Sambuc 
212*11be35a1SLionel Sambuc 	errno = 0;
213*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EBADF, dup3(-1, -1, O_CLOEXEC) == -1);
214*11be35a1SLionel Sambuc 
215*11be35a1SLionel Sambuc 	errno = 0;
216*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EBADF, dup3(fd, -1, O_CLOEXEC) == -1);
217*11be35a1SLionel Sambuc 
218*11be35a1SLionel Sambuc 	errno = 0;
219*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EBADF, dup3(-1, fd, O_CLOEXEC) == -1);
220*11be35a1SLionel Sambuc 
221*11be35a1SLionel Sambuc 	errno = 0;
222*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EINVAL, dup3(fd, 1, O_NOFOLLOW) == -1);
223*11be35a1SLionel Sambuc 
224*11be35a1SLionel Sambuc 	(void)close(fd);
225*11be35a1SLionel Sambuc }
226*11be35a1SLionel Sambuc 
227*11be35a1SLionel Sambuc ATF_TC(dup3_max);
ATF_TC_HEAD(dup3_max,tc)228*11be35a1SLionel Sambuc ATF_TC_HEAD(dup3_max, tc)
229*11be35a1SLionel Sambuc {
230*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test dup3(2) against limits");
231*11be35a1SLionel Sambuc }
232*11be35a1SLionel Sambuc 
ATF_TC_BODY(dup3_max,tc)233*11be35a1SLionel Sambuc ATF_TC_BODY(dup3_max, tc)
234*11be35a1SLionel Sambuc {
235*11be35a1SLionel Sambuc 	struct rlimit res;
236*11be35a1SLionel Sambuc 
237*11be35a1SLionel Sambuc 	(void)memset(&res, 0, sizeof(struct rlimit));
238*11be35a1SLionel Sambuc 	(void)getrlimit(RLIMIT_NOFILE, &res);
239*11be35a1SLionel Sambuc 
240*11be35a1SLionel Sambuc 	errno = 0;
241*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EBADF, dup3(STDERR_FILENO,
242*11be35a1SLionel Sambuc 		res.rlim_cur + 1, O_CLOEXEC) == -1);
243*11be35a1SLionel Sambuc }
244*11be35a1SLionel Sambuc 
245*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(dup3_mode);
ATF_TC_HEAD(dup3_mode,tc)246*11be35a1SLionel Sambuc ATF_TC_HEAD(dup3_mode, tc)
247*11be35a1SLionel Sambuc {
248*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "A basic test of dup3(2)");
249*11be35a1SLionel Sambuc }
250*11be35a1SLionel Sambuc 
ATF_TC_BODY(dup3_mode,tc)251*11be35a1SLionel Sambuc ATF_TC_BODY(dup3_mode, tc)
252*11be35a1SLionel Sambuc {
253*11be35a1SLionel Sambuc 	check_mode(false, false, true);
254*11be35a1SLionel Sambuc }
255*11be35a1SLionel Sambuc 
ATF_TC_CLEANUP(dup3_mode,tc)256*11be35a1SLionel Sambuc ATF_TC_CLEANUP(dup3_mode, tc)
257*11be35a1SLionel Sambuc {
258*11be35a1SLionel Sambuc 	(void)unlink(path);
259*11be35a1SLionel Sambuc }
260*11be35a1SLionel Sambuc 
261*11be35a1SLionel Sambuc ATF_TC(dup_err);
ATF_TC_HEAD(dup_err,tc)262*11be35a1SLionel Sambuc ATF_TC_HEAD(dup_err, tc)
263*11be35a1SLionel Sambuc {
264*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test error conditions of dup(2)");
265*11be35a1SLionel Sambuc }
266*11be35a1SLionel Sambuc 
ATF_TC_BODY(dup_err,tc)267*11be35a1SLionel Sambuc ATF_TC_BODY(dup_err, tc)
268*11be35a1SLionel Sambuc {
269*11be35a1SLionel Sambuc 
270*11be35a1SLionel Sambuc 	errno = 0;
271*11be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EBADF, dup(-1) == -1);
272*11be35a1SLionel Sambuc }
273*11be35a1SLionel Sambuc 
274*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(dup_max);
ATF_TC_HEAD(dup_max,tc)275*11be35a1SLionel Sambuc ATF_TC_HEAD(dup_max, tc)
276*11be35a1SLionel Sambuc {
277*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test dup(2) against limits");
278*11be35a1SLionel Sambuc }
279*11be35a1SLionel Sambuc 
ATF_TC_BODY(dup_max,tc)280*11be35a1SLionel Sambuc ATF_TC_BODY(dup_max, tc)
281*11be35a1SLionel Sambuc {
282*11be35a1SLionel Sambuc 	struct rlimit res;
283*11be35a1SLionel Sambuc 	int *buf, fd, sta;
284*11be35a1SLionel Sambuc 	size_t i, n;
285*11be35a1SLionel Sambuc 	pid_t pid;
286*11be35a1SLionel Sambuc 
287*11be35a1SLionel Sambuc 	pid = fork();
288*11be35a1SLionel Sambuc 	ATF_REQUIRE(pid >= 0);
289*11be35a1SLionel Sambuc 
290*11be35a1SLionel Sambuc 	if (pid == 0) {
291*11be35a1SLionel Sambuc 
292*11be35a1SLionel Sambuc 		/*
293*11be35a1SLionel Sambuc 		 * Open a temporary file until the
294*11be35a1SLionel Sambuc 		 * maximum number of open files is
295*11be35a1SLionel Sambuc 		 * reached. Ater that dup(2) family
296*11be35a1SLionel Sambuc 		 * should fail with EMFILE.
297*11be35a1SLionel Sambuc 		 */
298*11be35a1SLionel Sambuc 		(void)closefrom(0);
299*11be35a1SLionel Sambuc 		(void)memset(&res, 0, sizeof(struct rlimit));
300*11be35a1SLionel Sambuc 
301*11be35a1SLionel Sambuc 		n = 10;
302*11be35a1SLionel Sambuc 		res.rlim_cur = res.rlim_max = n;
303*11be35a1SLionel Sambuc 		if (setrlimit(RLIMIT_NOFILE, &res) != 0)
304*11be35a1SLionel Sambuc 			_exit(EX_OSERR);
305*11be35a1SLionel Sambuc 
306*11be35a1SLionel Sambuc 		buf = calloc(n, sizeof(int));
307*11be35a1SLionel Sambuc 
308*11be35a1SLionel Sambuc 		if (buf == NULL)
309*11be35a1SLionel Sambuc 			_exit(EX_OSERR);
310*11be35a1SLionel Sambuc 
311*11be35a1SLionel Sambuc 		buf[0] = mkstemp(path);
312*11be35a1SLionel Sambuc 
313*11be35a1SLionel Sambuc 		if (buf[0] < 0)
314*11be35a1SLionel Sambuc 			_exit(EX_OSERR);
315*11be35a1SLionel Sambuc 
316*11be35a1SLionel Sambuc 		for (i = 1; i < n; i++) {
317*11be35a1SLionel Sambuc 
318*11be35a1SLionel Sambuc 			buf[i] = open(path, O_RDONLY);
319*11be35a1SLionel Sambuc 
320*11be35a1SLionel Sambuc 			if (buf[i] < 0)
321*11be35a1SLionel Sambuc 				_exit(EX_OSERR);
322*11be35a1SLionel Sambuc 		}
323*11be35a1SLionel Sambuc 
324*11be35a1SLionel Sambuc 		errno = 0;
325*11be35a1SLionel Sambuc 		fd = dup(buf[0]);
326*11be35a1SLionel Sambuc 
327*11be35a1SLionel Sambuc 		if (fd != -1 || errno != EMFILE)
328*11be35a1SLionel Sambuc 			_exit(EX_DATAERR);
329*11be35a1SLionel Sambuc 
330*11be35a1SLionel Sambuc 		_exit(EXIT_SUCCESS);
331*11be35a1SLionel Sambuc 	}
332*11be35a1SLionel Sambuc 
333*11be35a1SLionel Sambuc 	(void)wait(&sta);
334*11be35a1SLionel Sambuc 
335*11be35a1SLionel Sambuc 	if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
336*11be35a1SLionel Sambuc 
337*11be35a1SLionel Sambuc 		if (WEXITSTATUS(sta) == EX_OSERR)
338*11be35a1SLionel Sambuc 			atf_tc_fail("system call error");
339*11be35a1SLionel Sambuc 
340*11be35a1SLionel Sambuc 		if (WEXITSTATUS(sta) == EX_DATAERR)
341*11be35a1SLionel Sambuc 			atf_tc_fail("dup(2) dupped more than RLIMIT_NOFILE");
342*11be35a1SLionel Sambuc 
343*11be35a1SLionel Sambuc 		atf_tc_fail("unknown error");
344*11be35a1SLionel Sambuc 	}
345*11be35a1SLionel Sambuc 
346*11be35a1SLionel Sambuc 	(void)unlink(path);
347*11be35a1SLionel Sambuc }
348*11be35a1SLionel Sambuc 
ATF_TC_CLEANUP(dup_max,tc)349*11be35a1SLionel Sambuc ATF_TC_CLEANUP(dup_max, tc)
350*11be35a1SLionel Sambuc {
351*11be35a1SLionel Sambuc 	(void)unlink(path);
352*11be35a1SLionel Sambuc }
353*11be35a1SLionel Sambuc 
354*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(dup_mode);
ATF_TC_HEAD(dup_mode,tc)355*11be35a1SLionel Sambuc ATF_TC_HEAD(dup_mode, tc)
356*11be35a1SLionel Sambuc {
357*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "A basic test of dup(2)");
358*11be35a1SLionel Sambuc }
359*11be35a1SLionel Sambuc 
ATF_TC_BODY(dup_mode,tc)360*11be35a1SLionel Sambuc ATF_TC_BODY(dup_mode, tc)
361*11be35a1SLionel Sambuc {
362*11be35a1SLionel Sambuc 	check_mode(true, false, false);
363*11be35a1SLionel Sambuc }
364*11be35a1SLionel Sambuc 
ATF_TC_CLEANUP(dup_mode,tc)365*11be35a1SLionel Sambuc ATF_TC_CLEANUP(dup_mode, tc)
366*11be35a1SLionel Sambuc {
367*11be35a1SLionel Sambuc 	(void)unlink(path);
368*11be35a1SLionel Sambuc }
369*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)370*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
371*11be35a1SLionel Sambuc {
372*11be35a1SLionel Sambuc 
373*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, dup2_basic);
374*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, dup2_err);
375*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, dup2_max);
376*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, dup2_mode);
377*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, dup3_err);
378*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, dup3_max);
379*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, dup3_mode);
380*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, dup_err);
381*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, dup_max);
382*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, dup_mode);
383*11be35a1SLionel Sambuc 
384*11be35a1SLionel Sambuc 	return atf_no_error();
385*11be35a1SLionel Sambuc }
386