t_dup.c revision 1.3 1 /* $NetBSD: t_dup.c,v 1.3 2011/07/15 09:40:16 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.3 2011/07/15 09:40:16 jruoho Exp $");
33
34 #include <sys/resource.h>
35 #include <sys/stat.h>
36 #include <sys/wait.h>
37
38 #include <atf-c.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <limits.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <sysexits.h>
47
48 static char path[] = "dup";
49 static void check_mode(bool, bool, bool);
50
51 static void
52 check_mode(bool _dup, bool _dup2, bool _dup3)
53 {
54 int mode[3] = { O_RDONLY, O_WRONLY, O_RDWR };
55 int perm[5] = { 0700, 0400, 0600, 0444, 0666 };
56 struct stat st, st1;
57 int fd, fd1, fd2;
58 size_t i, j;
59
60 /*
61 * Check that a duplicated descriptor
62 * retains the mode of the original file.
63 */
64 for (i = 0; i < __arraycount(mode); i++) {
65
66 for (j = 0; j < __arraycount(perm); j++) {
67
68 fd1 = open(path, mode[i] | O_CREAT, perm[j]);
69 fd2 = open("/etc/passwd", O_RDONLY);
70
71 ATF_REQUIRE(fd1 >= 0);
72 ATF_REQUIRE(fd2 >= 0);
73
74 if (_dup != false)
75 fd = dup(fd1);
76 else if (_dup2 != false)
77 fd = dup2(fd1, fd2);
78 else if (_dup3 != false)
79 fd = dup3(fd1, fd2, O_CLOEXEC);
80 else {
81 fd = -1;
82 }
83
84 ATF_REQUIRE(fd >= 0);
85
86 (void)memset(&st, 0, sizeof(struct stat));
87 (void)memset(&st1, 0, sizeof(struct stat));
88
89 ATF_REQUIRE(fstat(fd, &st) == 0);
90 ATF_REQUIRE(fstat(fd1, &st1) == 0);
91
92 if (st.st_mode != st1.st_mode)
93 atf_tc_fail("invalid mode");
94
95 (void)close(fd);
96 (void)close(fd1);
97 (void)close(fd2);
98 (void)unlink(path);
99 }
100 }
101 }
102
103 ATF_TC(dup2_basic);
104 ATF_TC_HEAD(dup2_basic, tc)
105 {
106 atf_tc_set_md_var(tc, "descr", "A basic test of dup2(2)");
107 }
108
109 ATF_TC_BODY(dup2_basic, tc)
110 {
111 int fd, fd1, fd2;
112
113 fd1 = open("/etc/passwd", O_RDONLY);
114 fd2 = open("/etc/passwd", O_RDONLY);
115
116 ATF_REQUIRE(fd1 >= 0);
117 ATF_REQUIRE(fd2 >= 0);
118
119 fd = dup2(fd1, fd2);
120 ATF_REQUIRE(fd >= 0);
121
122 if (fd != fd2)
123 atf_tc_fail("invalid descriptor");
124
125 (void)close(fd);
126 (void)close(fd1);
127
128 ATF_REQUIRE(close(fd2) != 0);
129 }
130
131 ATF_TC(dup2_err);
132 ATF_TC_HEAD(dup2_err, tc)
133 {
134 atf_tc_set_md_var(tc, "descr", "Test error conditions of dup2(2)");
135 }
136
137 ATF_TC_BODY(dup2_err, tc)
138 {
139 int fd;
140
141 fd = open("/etc/passwd", O_RDONLY);
142 ATF_REQUIRE(fd >= 0);
143
144 errno = 0;
145 ATF_REQUIRE_ERRNO(EBADF, dup2(-1, -1) == -1);
146
147 errno = 0;
148 ATF_REQUIRE_ERRNO(EBADF, dup2(fd, -1) == -1);
149
150 errno = 0;
151 ATF_REQUIRE_ERRNO(EBADF, dup2(-1, fd) == -1);
152
153 /*
154 * Note that this should not fail with EINVAL.
155 */
156 ATF_REQUIRE(dup2(fd, fd) != -1);
157
158 (void)close(fd);
159 }
160
161 ATF_TC_WITH_CLEANUP(dup2_mode);
162 ATF_TC_HEAD(dup2_mode, tc)
163 {
164 atf_tc_set_md_var(tc, "descr", "A basic test of dup2(2)");
165 }
166
167 ATF_TC_BODY(dup2_mode, tc)
168 {
169 check_mode(false, true, false);
170 }
171
172 ATF_TC_CLEANUP(dup2_mode, tc)
173 {
174 (void)unlink(path);
175 }
176
177
178 ATF_TC(dup3_err);
179 ATF_TC_HEAD(dup3_err, tc)
180 {
181 atf_tc_set_md_var(tc, "descr", "Test error conditions of dup3(2)");
182 }
183
184 ATF_TC_BODY(dup3_err, tc)
185 {
186 int fd;
187
188 atf_tc_expect_fail("PR lib/45148");
189
190 fd = open("/etc/passwd", O_RDONLY);
191 ATF_REQUIRE(fd >= 0);
192
193 errno = 0;
194 ATF_REQUIRE(dup3(fd, fd, O_CLOEXEC) != -1);
195
196 errno = 0;
197 ATF_REQUIRE_ERRNO(EBADF, dup3(-1, -1, O_CLOEXEC) == -1);
198
199 errno = 0;
200 ATF_REQUIRE_ERRNO(EBADF, dup3(fd, -1, O_CLOEXEC) == -1);
201
202 errno = 0;
203 ATF_REQUIRE_ERRNO(EBADF, dup3(-1, fd, O_CLOEXEC) == -1);
204
205 errno = 0;
206 ATF_REQUIRE_ERRNO(EINVAL, dup3(fd, 1, O_NOFOLLOW) == -1); /* Fails. */
207
208 (void)close(fd);
209 }
210
211 ATF_TC_WITH_CLEANUP(dup3_mode);
212 ATF_TC_HEAD(dup3_mode, tc)
213 {
214 atf_tc_set_md_var(tc, "descr", "A basic test of dup3(2)");
215 }
216
217 ATF_TC_BODY(dup3_mode, tc)
218 {
219 check_mode(false, false, true);
220 }
221
222 ATF_TC_CLEANUP(dup3_mode, tc)
223 {
224 (void)unlink(path);
225 }
226
227 ATF_TC(dup_err);
228 ATF_TC_HEAD(dup_err, tc)
229 {
230 atf_tc_set_md_var(tc, "descr", "Test error conditions of dup(2)");
231 }
232
233 ATF_TC_BODY(dup_err, tc)
234 {
235
236 errno = 0;
237 ATF_REQUIRE_ERRNO(EBADF, dup(-1) == -1);
238 }
239
240 ATF_TC_WITH_CLEANUP(dup_max);
241 ATF_TC_HEAD(dup_max, tc)
242 {
243 atf_tc_set_md_var(tc, "descr", "Test dup(2) against limits");
244 }
245
246 ATF_TC_BODY(dup_max, tc)
247 {
248 struct rlimit res;
249 int *buf, fd, sta;
250 size_t i, n;
251 pid_t pid;
252
253 pid = fork();
254 ATF_REQUIRE(pid >= 0);
255
256 if (pid == 0) {
257
258 /*
259 * Open a temporary file until the
260 * maximum number of open files is
261 * reached. Ater that dup(2) family
262 * should fail with EMFILE.
263 */
264 (void)closefrom(0);
265 (void)memset(&res, 0, sizeof(struct rlimit));
266
267 if (getrlimit(RLIMIT_NOFILE, &res) != 0)
268 _exit(EX_OSERR);
269
270 if (res.rlim_cur == 0 || res.rlim_max == 0)
271 _exit(EX_OSERR);
272
273 n = res.rlim_cur;
274 buf = calloc(n, sizeof(int));
275
276 if (buf == NULL)
277 _exit(EX_OSERR);
278
279 buf[0] = mkstemp(path);
280
281 if (buf[0] < 0)
282 _exit(EX_OSERR);
283
284 for (i = 1; i < n; i++) {
285
286 buf[i] = open(path, O_RDONLY);
287
288 if (buf[i] < 0)
289 _exit(EX_OSERR);
290 }
291
292 errno = 0;
293 fd = dup(buf[0]);
294
295 if (fd != -1 || errno != EMFILE)
296 _exit(EX_DATAERR);
297
298 _exit(EXIT_SUCCESS);
299 }
300
301 (void)wait(&sta);
302
303 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) {
304
305 if (WEXITSTATUS(sta) == EX_OSERR)
306 atf_tc_fail("system call error");
307
308 if (WEXITSTATUS(sta) == EX_DATAERR)
309 atf_tc_fail("dup(2) dupped more than RLIMIT_NOFILE");
310
311 atf_tc_fail("unknown error");
312 }
313
314 (void)unlink(path);
315 }
316
317 ATF_TC_CLEANUP(dup_max, tc)
318 {
319 (void)unlink(path);
320 }
321
322 ATF_TC_WITH_CLEANUP(dup_mode);
323 ATF_TC_HEAD(dup_mode, tc)
324 {
325 atf_tc_set_md_var(tc, "descr", "A basic test of dup(2)");
326 }
327
328 ATF_TC_BODY(dup_mode, tc)
329 {
330 check_mode(true, false, false);
331 }
332
333 ATF_TC_CLEANUP(dup_mode, tc)
334 {
335 (void)unlink(path);
336 }
337
338 ATF_TP_ADD_TCS(tp)
339 {
340
341 ATF_TP_ADD_TC(tp, dup2_basic);
342 ATF_TP_ADD_TC(tp, dup2_err);
343 ATF_TP_ADD_TC(tp, dup2_mode);
344 ATF_TP_ADD_TC(tp, dup3_err);
345 ATF_TP_ADD_TC(tp, dup3_mode);
346 ATF_TP_ADD_TC(tp, dup_err);
347 ATF_TP_ADD_TC(tp, dup_max);
348 ATF_TP_ADD_TC(tp, dup_mode);
349
350 return atf_no_error();
351 }
352