2 * $OpenBSD: dup2test.c,v 1.3 2003/07/31 21:48:08 deraadt Exp $
3 * $OpenBSD: dup2_self.c,v 1.3 2003/07/31 21:48:08 deraadt Exp $
4 * $OpenBSD: fcntl_dup.c,v 1.2 2003/07/31 21:48:08 deraadt Exp $
6 * Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain.
10 * Test #1: check if dup(2) works.
11 * Test #2: check if dup2(2) works.
12 * Test #3: check if dup2(2) returned a fd we asked for.
13 * Test #4: check if dup2(2) cleared close-on-exec flag for duped fd.
14 * Test #5: check if dup2(2) allows to dup fd to itself.
15 * Test #6: check if dup2(2) returned a fd we asked for.
16 * Test #7: check if dup2(2) did not clear close-on-exec flag for duped fd.
17 * Test #8: check if fcntl(F_DUPFD) works.
18 * Test #9: check if fcntl(F_DUPFD) cleared close-on-exec flag for duped fd.
19 * Test #10: check if dup2() to a fd > current maximum number of open files
21 * Test #11: check if fcntl(F_DUP2FD) works.
22 * Test #12: check if fcntl(F_DUP2FD) returned a fd we asked for.
23 * Test #13: check if fcntl(F_DUP2FD) cleared close-on-exec flag for duped fd.
24 * Test #14: check if fcntl(F_DUP2FD) allows to dup fd to itself.
25 * Test #15: check if fcntl(F_DUP2FD) returned a fd we asked for.
26 * Test #16: check if fcntl(F_DUP2FD) did not clear close-on-exec flag for
28 * Test #17: check if fcntl(F_DUP2FD) to a fd > current maximum number of open
30 * Test #18: check if fcntl(F_DUPFD_CLOEXEC) works.
31 * Test #19: check if fcntl(F_DUPFD_CLOEXEC) set close-on-exec flag for duped
33 * Test #20: check if fcntl(F_DUP2FD_CLOEXEC) works.
34 * Test #21: check if fcntl(F_DUP2FD_CLOEXEC) returned a fd we asked for.
35 * Test #22: check if fcntl(F_DUP2FD_CLOEXEC) set close-on-exec flag for duped
37 * Test #23: check if fcntl(F_DUP2FD_CLOEXEC) to a fd > current maximum number
38 * of open files limit work.
39 * Test #24: check if dup3(O_CLOEXEC) works.
40 * Test #25: check if dup3(O_CLOEXEC) returned a fd we asked for.
41 * Test #26: check if dup3(O_CLOEXEC) set close-on-exec flag for duped fd.
42 * Test #27: check if dup3(0) works.
43 * Test #28: check if dup3(0) returned a fd we asked for.
44 * Test #29: check if dup3(0) cleared close-on-exec flag for duped fd.
45 * Test #30: check if dup3(O_CLOEXEC) fails if oldfd == newfd.
46 * Test #31: check if dup3(0) fails if oldfd == newfd.
47 * Test #32: check if dup3(O_CLOEXEC) to a fd > current maximum number of
48 * open files limit work.
51 #include <sys/types.h>
53 #include <sys/resource.h>
61 static int getafile(void);
68 char temp[] = "/tmp/dup2XXXXXXXXX";
69 if ((fd = mkstemp(temp)) < 0)
72 if (ftruncate(fd, 1024) != 0)
78 main(int __unused argc, char __unused *argv[])
81 int orgfd, fd1, fd2, test = 0;
87 /* If dup(2) ever work? */
88 if ((fd1 = dup(orgfd)) < 0)
90 printf("ok %d - dup(2) works\n", ++test);
92 /* Set close-on-exec */
93 if (fcntl(fd1, F_SETFD, 1) != 0)
94 err(1, "fcntl(F_SETFD)");
96 /* If dup2(2) ever work? */
97 if ((fd2 = dup2(fd1, fd1 + 1)) < 0)
99 printf("ok %d - dup2(2) works\n", ++test);
101 /* Do we get the right fd? */
104 printf("no ok %d - dup2(2) didn't give us the right fd\n",
107 printf("ok %d - dup2(2) returned a correct fd\n", test);
109 /* Was close-on-exec cleared? */
111 if (fcntl(fd2, F_GETFD) != 0)
112 printf("not ok %d - dup2(2) didn't clear close-on-exec\n",
115 printf("ok %d - dup2(2) cleared close-on-exec\n", test);
120 * We're testing a small tweak in dup2 semantics.
121 * Normally dup and dup2 will clear the close-on-exec
122 * flag on the new fd (which appears to be an implementation
123 * mistake from start and not some planned behavior).
124 * In today's implementations of dup and dup2 we have to make
125 * an effort to really clear that flag. But all tested
126 * implementations of dup2 have another tweak. If we
127 * dup2(old, new) when old == new, the syscall short-circuits
128 * and returns early (because there is no need to do all the
129 * work (and there is a risk for serious mistakes)).
130 * So although the docs say that dup2 should "take 'old',
131 * close 'new' perform a dup(2) of 'old' into 'new'"
132 * the docs are not really followed because close-on-exec
133 * is not cleared on 'new'.
135 * Since everyone has this bug, we pretend that this is
136 * the way it is supposed to be and test here that it really
139 * This is a fine example on where two separate implementation
140 * fuckups take out each other and make the end-result the way
141 * it was meant to be.
143 if ((fd2 = dup2(fd1, fd1)) < 0)
145 printf("ok %d - dup2(2) to itself works\n", ++test);
147 /* Do we get the right fd? */
150 printf("not ok %d - dup2(2) didn't give us the right fd\n",
153 printf("ok %d - dup2(2) to itself returned a correct fd\n",
156 /* Was close-on-exec cleared? */
158 if (fcntl(fd2, F_GETFD) == 0)
159 printf("not ok %d - dup2(2) cleared close-on-exec\n", test);
161 printf("ok %d - dup2(2) didn't clear close-on-exec\n", test);
163 /* Does fcntl(F_DUPFD) work? */
164 if ((fd2 = fcntl(fd1, F_DUPFD, 10)) < 0)
165 err(1, "fcntl(F_DUPFD)");
167 printf("not ok %d - fcntl(F_DUPFD) returned wrong fd %d\n",
170 printf("ok %d - fcntl(F_DUPFD) works\n", ++test);
172 /* Was close-on-exec cleared? */
174 if (fcntl(fd2, F_GETFD) != 0)
176 "not ok %d - fcntl(F_DUPFD) didn't clear close-on-exec\n",
179 printf("ok %d - fcntl(F_DUPFD) cleared close-on-exec\n", test);
182 if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
184 if ((fd2 = dup2(fd1, rlp.rlim_cur + 1)) >= 0)
185 printf("not ok %d - dup2(2) bypassed NOFILE limit\n", test);
187 printf("ok %d - dup2(2) didn't bypass NOFILE limit\n", test);
189 /* If fcntl(F_DUP2FD) ever work? */
190 if ((fd2 = fcntl(fd1, F_DUP2FD, fd1 + 1)) < 0)
191 err(1, "fcntl(F_DUP2FD)");
192 printf("ok %d - fcntl(F_DUP2FD) works\n", ++test);
194 /* Do we get the right fd? */
198 "no ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n",
201 printf("ok %d - fcntl(F_DUP2FD) returned a correct fd\n",
204 /* Was close-on-exec cleared? */
206 if (fcntl(fd2, F_GETFD) != 0)
208 "not ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n",
211 printf("ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n",
215 if ((fd2 = fcntl(fd1, F_DUP2FD, fd1)) < 0)
216 err(1, "fcntl(F_DUP2FD)");
217 printf("ok %d - fcntl(F_DUP2FD) to itself works\n", ++test);
219 /* Do we get the right fd? */
223 "not ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n",
227 "ok %d - fcntl(F_DUP2FD) to itself returned a correct fd\n",
230 /* Was close-on-exec cleared? */
232 if (fcntl(fd2, F_GETFD) == 0)
233 printf("not ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n",
236 printf("ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n",
240 if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
242 if ((fd2 = fcntl(fd1, F_DUP2FD, rlp.rlim_cur + 1)) >= 0)
243 printf("not ok %d - fcntl(F_DUP2FD) bypassed NOFILE limit\n",
246 printf("ok %d - fcntl(F_DUP2FD) didn't bypass NOFILE limit\n",
249 /* Does fcntl(F_DUPFD_CLOEXEC) work? */
250 if ((fd2 = fcntl(fd1, F_DUPFD_CLOEXEC, 10)) < 0)
251 err(1, "fcntl(F_DUPFD_CLOEXEC)");
253 printf("not ok %d - fcntl(F_DUPFD_CLOEXEC) returned wrong fd %d\n",
256 printf("ok %d - fcntl(F_DUPFD_CLOEXEC) works\n", ++test);
258 /* Was close-on-exec cleared? */
260 if (fcntl(fd2, F_GETFD) != 1)
262 "not ok %d - fcntl(F_DUPFD_CLOEXEC) didn't set close-on-exec\n",
265 printf("ok %d - fcntl(F_DUPFD_CLOEXEC) set close-on-exec\n",
268 /* If fcntl(F_DUP2FD_CLOEXEC) ever work? */
269 if ((fd2 = fcntl(fd1, F_DUP2FD_CLOEXEC, fd1 + 1)) < 0)
270 err(1, "fcntl(F_DUP2FD_CLOEXEC)");
271 printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) works\n", ++test);
273 /* Do we get the right fd? */
277 "no ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't give us the right fd\n",
280 printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) returned a correct fd\n",
283 /* Was close-on-exec set? */
285 if (fcntl(fd2, F_GETFD) != FD_CLOEXEC)
287 "not ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't set close-on-exec\n",
290 printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) set close-on-exec\n",
294 * It is unclear what F_DUP2FD_CLOEXEC should do when duplicating a
295 * file descriptor onto itself.
299 if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
301 if ((fd2 = fcntl(fd1, F_DUP2FD_CLOEXEC, rlp.rlim_cur + 1)) >= 0)
302 printf("not ok %d - fcntl(F_DUP2FD_CLOEXEC) bypassed NOFILE limit\n",
305 printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't bypass NOFILE limit\n",
308 /* Does dup3(O_CLOEXEC) ever work? */
309 if ((fd2 = dup3(fd1, fd1 + 1, O_CLOEXEC)) < 0)
310 err(1, "dup3(O_CLOEXEC)");
311 printf("ok %d - dup3(O_CLOEXEC) works\n", ++test);
313 /* Do we get the right fd? */
317 "no ok %d - dup3(O_CLOEXEC) didn't give us the right fd\n",
320 printf("ok %d - dup3(O_CLOEXEC) returned a correct fd\n",
323 /* Was close-on-exec set? */
325 if (fcntl(fd2, F_GETFD) != FD_CLOEXEC)
327 "not ok %d - dup3(O_CLOEXEC) didn't set close-on-exec\n",
330 printf("ok %d - dup3(O_CLOEXEC) set close-on-exec\n",
333 /* Does dup3(0) ever work? */
334 if ((fd2 = dup3(fd1, fd1 + 1, 0)) < 0)
336 printf("ok %d - dup3(0) works\n", ++test);
338 /* Do we get the right fd? */
342 "no ok %d - dup3(0) didn't give us the right fd\n",
345 printf("ok %d - dup3(0) returned a correct fd\n",
348 /* Was close-on-exec cleared? */
350 if (fcntl(fd2, F_GETFD) != 0)
352 "not ok %d - dup3(0) didn't clear close-on-exec\n",
355 printf("ok %d - dup3(0) cleared close-on-exec\n",
358 /* dup3() does not allow duplicating to the same fd */
360 if (dup3(fd1, fd1, O_CLOEXEC) != -1)
362 "not ok %d - dup3(fd1, fd1, O_CLOEXEC) succeeded\n", test);
364 printf("ok %d - dup3(fd1, fd1, O_CLOEXEC) failed\n", test);
367 if (dup3(fd1, fd1, 0) != -1)
369 "not ok %d - dup3(fd1, fd1, 0) succeeded\n", test);
371 printf("ok %d - dup3(fd1, fd1, 0) failed\n", test);
374 if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
376 if ((fd2 = dup3(fd1, rlp.rlim_cur + 1, O_CLOEXEC)) >= 0)
377 printf("not ok %d - dup3(O_CLOEXEC) bypassed NOFILE limit\n",
380 printf("ok %d - dup3(O_CLOEXEC) didn't bypass NOFILE limit\n",