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