1 /* $NetBSD: t_dup.c,v 1.8 2012/03/18 07:00:51 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_dup.c,v 1.8 2012/03/18 07:00:51 jruoho Exp $");
34 #include <sys/resource.h>
52 static char path[] = "dup";
54 static void check_mode(bool, bool, bool);
58 check_mode(bool _dup, bool _dup2, bool _dup3)
60 int mode[3] = { O_RDONLY, O_WRONLY, O_RDWR };
61 int perm[5] = { 0700, 0400, 0600, 0444, 0666 };
67 * Check that a duplicated descriptor
68 * retains the mode of the original file.
70 for (i = 0; i < __arraycount(mode); i++) {
72 for (j = 0; j < __arraycount(perm); j++) {
74 fd1 = open(path, mode[i] | O_CREAT, perm[j]);
75 fd2 = open("/etc/passwd", O_RDONLY);
77 ATF_REQUIRE(fd1 >= 0);
78 ATF_REQUIRE(fd2 >= 0);
82 else if (_dup2 != false)
84 else if (_dup3 != false)
85 fd = dup3(fd1, fd2, O_CLOEXEC);
92 (void)memset(&st, 0, sizeof(struct stat));
93 (void)memset(&st1, 0, sizeof(struct stat));
95 ATF_REQUIRE(fstat(fd, &st) == 0);
96 ATF_REQUIRE(fstat(fd1, &st1) == 0);
98 if (st.st_mode != st1.st_mode)
99 atf_tc_fail("invalid mode");
110 ATF_TC_HEAD(dup2_basic, tc)
112 atf_tc_set_md_var(tc, "descr", "A basic test of dup2(2)");
115 ATF_TC_BODY(dup2_basic, tc)
119 fd1 = open("/etc/passwd", O_RDONLY);
120 fd2 = open("/etc/passwd", O_RDONLY);
122 ATF_REQUIRE(fd1 >= 0);
123 ATF_REQUIRE(fd2 >= 0);
126 ATF_REQUIRE(fd >= 0);
129 atf_tc_fail("invalid descriptor");
134 ATF_REQUIRE(close(fd2) != 0);
138 ATF_TC_HEAD(dup2_err, tc)
140 atf_tc_set_md_var(tc, "descr", "Test error conditions of dup2(2)");
143 ATF_TC_BODY(dup2_err, tc)
147 fd = open("/etc/passwd", O_RDONLY);
148 ATF_REQUIRE(fd >= 0);
151 ATF_REQUIRE_ERRNO(EBADF, dup2(-1, -1) == -1);
154 ATF_REQUIRE_ERRNO(EBADF, dup2(fd, -1) == -1);
157 ATF_REQUIRE_ERRNO(EBADF, dup2(-1, fd) == -1);
160 * Note that this should not fail with EINVAL.
162 ATF_REQUIRE(dup2(fd, fd) != -1);
168 ATF_TC_HEAD(dup2_max, tc)
170 atf_tc_set_md_var(tc, "descr", "Test dup2(2) against limits");
173 ATF_TC_BODY(dup2_max, tc)
177 (void)memset(&res, 0, sizeof(struct rlimit));
178 (void)getrlimit(RLIMIT_NOFILE, &res);
181 ATF_REQUIRE_ERRNO(EBADF, dup2(STDERR_FILENO, res.rlim_cur + 1) == -1);
184 ATF_TC_WITH_CLEANUP(dup2_mode);
185 ATF_TC_HEAD(dup2_mode, tc)
187 atf_tc_set_md_var(tc, "descr", "A basic test of dup2(2)");
190 ATF_TC_BODY(dup2_mode, tc)
192 check_mode(false, true, false);
195 ATF_TC_CLEANUP(dup2_mode, tc)
202 ATF_TC_HEAD(dup3_err, tc)
204 atf_tc_set_md_var(tc, "descr",
205 "Test error conditions of dup3(2) (PR lib/45148)");
208 ATF_TC_BODY(dup3_err, tc)
212 fd = open("/etc/passwd", O_RDONLY);
213 ATF_REQUIRE(fd >= 0);
216 #if defined(__FreeBSD__) || defined(__linux__)
218 * FreeBSD and linux return EINVAL, because...
220 * [EINVAL] The oldd argument is equal to the newd argument.
222 ATF_REQUIRE(dup3(fd, fd, O_CLOEXEC) == -1);
224 ATF_REQUIRE(dup3(fd, fd, O_CLOEXEC) != -1);
228 #if defined(__FreeBSD__) || defined(__linux__)
229 ATF_REQUIRE_ERRNO(EINVAL, dup3(-1, -1, O_CLOEXEC) == -1);
230 ATF_REQUIRE_ERRNO(EBADF, dup3(fd, -1, O_CLOEXEC) == -1);
232 ATF_REQUIRE_ERRNO(EBADF, dup3(-1, -1, O_CLOEXEC) == -1);
236 ATF_REQUIRE_ERRNO(EBADF, dup3(fd, -1, O_CLOEXEC) == -1);
239 ATF_REQUIRE_ERRNO(EBADF, dup3(-1, fd, O_CLOEXEC) == -1);
242 ATF_REQUIRE_ERRNO(EINVAL, dup3(fd, 1, O_NOFOLLOW) == -1);
248 ATF_TC_HEAD(dup3_max, tc)
250 atf_tc_set_md_var(tc, "descr", "Test dup3(2) against limits");
253 ATF_TC_BODY(dup3_max, tc)
257 (void)memset(&res, 0, sizeof(struct rlimit));
258 (void)getrlimit(RLIMIT_NOFILE, &res);
261 ATF_REQUIRE_ERRNO(EBADF, dup3(STDERR_FILENO,
262 res.rlim_cur + 1, O_CLOEXEC) == -1);
265 ATF_TC_WITH_CLEANUP(dup3_mode);
266 ATF_TC_HEAD(dup3_mode, tc)
268 atf_tc_set_md_var(tc, "descr", "A basic test of dup3(2)");
271 ATF_TC_BODY(dup3_mode, tc)
273 check_mode(false, false, true);
276 ATF_TC_CLEANUP(dup3_mode, tc)
282 ATF_TC_HEAD(dup_err, tc)
284 atf_tc_set_md_var(tc, "descr", "Test error conditions of dup(2)");
287 ATF_TC_BODY(dup_err, tc)
291 ATF_REQUIRE_ERRNO(EBADF, dup(-1) == -1);
294 ATF_TC_WITH_CLEANUP(dup_max);
295 ATF_TC_HEAD(dup_max, tc)
297 atf_tc_set_md_var(tc, "descr", "Test dup(2) against limits");
300 ATF_TC_BODY(dup_max, tc)
308 ATF_REQUIRE(pid >= 0);
313 * Open a temporary file until the
314 * maximum number of open files is
315 * reached. Ater that dup(2) family
316 * should fail with EMFILE.
319 (void)memset(&res, 0, sizeof(struct rlimit));
322 res.rlim_cur = res.rlim_max = n;
323 if (setrlimit(RLIMIT_NOFILE, &res) != 0)
326 buf = calloc(n, sizeof(int));
331 buf[0] = mkstemp(path);
336 for (i = 1; i < n; i++) {
338 buf[i] = open(path, O_RDONLY);
347 if (fd != -1 || errno != EMFILE)
355 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
357 if (WEXITSTATUS(sta) == EX_OSERR)
358 atf_tc_fail("system call error");
360 if (WEXITSTATUS(sta) == EX_DATAERR)
361 atf_tc_fail("dup(2) dupped more than RLIMIT_NOFILE");
363 atf_tc_fail("unknown error");
369 ATF_TC_CLEANUP(dup_max, tc)
374 ATF_TC_WITH_CLEANUP(dup_mode);
375 ATF_TC_HEAD(dup_mode, tc)
377 atf_tc_set_md_var(tc, "descr", "A basic test of dup(2)");
380 ATF_TC_BODY(dup_mode, tc)
382 check_mode(true, false, false);
385 ATF_TC_CLEANUP(dup_mode, tc)
393 ATF_TP_ADD_TC(tp, dup2_basic);
394 ATF_TP_ADD_TC(tp, dup2_err);
395 ATF_TP_ADD_TC(tp, dup2_max);
396 ATF_TP_ADD_TC(tp, dup2_mode);
397 ATF_TP_ADD_TC(tp, dup3_err);
398 ATF_TP_ADD_TC(tp, dup3_max);
399 ATF_TP_ADD_TC(tp, dup3_mode);
400 ATF_TP_ADD_TC(tp, dup_err);
401 ATF_TP_ADD_TC(tp, dup_max);
402 ATF_TP_ADD_TC(tp, dup_mode);
404 return atf_no_error();