t_dup.c revision 1.1 1 /* $NetBSD: t_dup.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
18 *
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.
30 */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_dup.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $");
33
34 #include <sys/resource.h>
35 #include <sys/stat.h>
36
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include <atf-c.h>
44
45 static char path[] = "dup";
46
47 ATF_TC(dup_err);
48 ATF_TC_HEAD(dup_err, tc)
49 {
50 atf_tc_set_md_var(tc, "descr", "Test error conditions of dup(2)");
51 }
52
53 ATF_TC_BODY(dup_err, tc)
54 {
55 ATF_REQUIRE(dup(-1) != 0);
56 }
57
58 ATF_TC_WITH_CLEANUP(dup_max);
59 ATF_TC_HEAD(dup_max, tc)
60 {
61 atf_tc_set_md_var(tc, "descr", "Test dup(2) against limits");
62 }
63
64 ATF_TC_BODY(dup_max, tc)
65 {
66 int current, fd, *buf, serrno;
67 struct rlimit res;
68 long i, maxfd;
69
70 /*
71 * Open a temporary file until the
72 * maximum number of open files is
73 * reached. Ater that dup(2) should
74 * fail with EMFILE.
75 */
76 (void)memset(&res, 0, sizeof(struct rlimit));
77
78 ATF_REQUIRE(getrlimit(RLIMIT_NOFILE, &res) == 0);
79
80 ATF_REQUIRE(res.rlim_cur > 0);
81 ATF_REQUIRE(res.rlim_max > 0);
82
83 maxfd = res.rlim_cur;
84 buf = calloc(maxfd, sizeof(int));
85
86 if (buf == NULL)
87 return;
88
89 buf[0] = mkstemp(path);
90 ATF_REQUIRE(buf[0] != -1);
91
92 current = fcntl(0, F_MAXFD);
93 ATF_REQUIRE(current != -1);
94
95 fd = -1;
96 serrno = EMFILE;
97
98 for (i = current; i <= maxfd; i++) {
99
100 buf[i] = open(path, O_RDONLY);
101
102 if (buf[i] < 0)
103 goto out;
104 }
105
106 errno = 0;
107 fd = dup(buf[0]);
108 serrno = errno;
109
110 out:
111 for (i = 0; i <= maxfd; i++)
112 (void)close(buf[i]);
113
114 free(buf);
115
116 if (fd != -1 || serrno != EMFILE)
117 atf_tc_fail("dup(2) dupped more than RLIMIT_NOFILE");
118 }
119
120 ATF_TC_CLEANUP(dup_max, tc)
121 {
122 (void)unlink(path);
123 }
124
125 ATF_TC_WITH_CLEANUP(dup_mode);
126 ATF_TC_HEAD(dup_mode, tc)
127 {
128 atf_tc_set_md_var(tc, "descr", "A basic test of dup(2)");
129 }
130
131 ATF_TC_BODY(dup_mode, tc)
132 {
133 int mode[3] = { O_RDONLY, O_WRONLY, O_RDWR };
134 int perm[5] = { 0700, 0400, 0600, 0444, 0666 };
135 struct stat st1, st2;
136 int fd1, fd2;
137 size_t i, j;
138
139 /*
140 * Check that a duplicated descriptor
141 * retains the mode of the original file.
142 */
143 for (i = 0; i < __arraycount(mode); i++) {
144
145 for (j = 0; j < __arraycount(perm); j++) {
146
147 fd1 = open(path, mode[i] | O_CREAT, perm[j]);
148
149 if (fd1 < 0)
150 return;
151
152 fd2 = dup(fd1);
153 ATF_REQUIRE(fd2 >= 0);
154
155 (void)memset(&st1, 0, sizeof(struct stat));
156 (void)memset(&st2, 0, sizeof(struct stat));
157
158 ATF_REQUIRE(fstat(fd1, &st1) == 0);
159 ATF_REQUIRE(fstat(fd2, &st2) == 0);
160
161 if (st1.st_mode != st2.st_mode)
162 atf_tc_fail("invalid mode");
163
164 ATF_REQUIRE(close(fd1) == 0);
165 ATF_REQUIRE(close(fd2) == 0);
166 ATF_REQUIRE(unlink(path) == 0);
167 }
168 }
169 }
170
171 ATF_TC_CLEANUP(dup_mode, tc)
172 {
173 (void)unlink(path);
174 }
175
176 ATF_TP_ADD_TCS(tp)
177 {
178
179 ATF_TP_ADD_TC(tp, dup_err);
180 ATF_TP_ADD_TC(tp, dup_max);
181 ATF_TP_ADD_TC(tp, dup_mode);
182
183 return atf_no_error();
184 }
185