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.
12 * Test #1: check if dup(2) works.
13 * Test #2: check if dup2(2) works.
14 * Test #3: check if dup2(2) returned a fd we asked for.
15 * Test #4: check if dup2(2) cleared close-on-exec flag for duped fd.
16 * Test #5: check if dup2(2) allows to dup fd to itself.
17 * Test #6: check if dup2(2) returned a fd we asked for.
18 * Test #7: check if dup2(2) did not clear close-on-exec flag for duped fd.
19 * Test #8: check if fcntl(F_DUPFD) works.
20 * Test #9: check if fcntl(F_DUPFD) cleared close-on-exec flag for duped fd.
21 * Test #10: check if dup2() to a fd > current maximum number of open files
23 * Test #11: check if fcntl(F_DUP2FD) works.
24 * Test #12: check if fcntl(F_DUP2FD) returned a fd we asked for.
25 * Test #13: check if fcntl(F_DUP2FD) cleared close-on-exec flag for duped fd.
26 * Test #14: check if fcntl(F_DUP2FD) allows to dup fd to itself.
27 * Test #15: check if fcntl(F_DUP2FD) returned a fd we asked for.
28 * Test #16: check if fcntl(F_DUP2FD) did not clear close-on-exec flag for
30 * Test #17: check if fcntl(F_DUP2FD) to a fd > current maximum number of open
32 * Test #18: check if fcntl(F_DUPFD_CLOEXEC) works.
33 * Test #19: check if fcntl(F_DUPFD_CLOEXEC) set close-on-exec flag for duped
35 * Test #20: check if fcntl(F_DUP2FD_CLOEXEC) works.
36 * Test #21: check if fcntl(F_DUP2FD_CLOEXEC) returned a fd we asked for.
37 * Test #22: check if fcntl(F_DUP2FD_CLOEXEC) set close-on-exec flag for duped
39 * Test #23: check if fcntl(F_DUP2FD_CLOEXEC) to a fd > current maximum number
40 * of open files limit work.
41 * Test #24: check if dup3(O_CLOEXEC) works.
42 * Test #25: check if dup3(O_CLOEXEC) returned a fd we asked for.
43 * Test #26: check if dup3(O_CLOEXEC) set close-on-exec flag for duped fd.
44 * Test #27: check if dup3(0) works.
45 * Test #28: check if dup3(0) returned a fd we asked for.
46 * Test #29: check if dup3(0) cleared close-on-exec flag for duped fd.
47 * Test #30: check if dup3(O_CLOEXEC) fails if oldfd == newfd.
48 * Test #31: check if dup3(0) fails if oldfd == newfd.
49 * Test #32: check if dup3(O_CLOEXEC) to a fd > current maximum number of
50 * open files limit work.
54 #include <sys/types.h>
56 #include <sys/resource.h>
64 static int getafile(void);
71 char temp[] = "/tmp/dup2XXXXXXXXX";
73 if ((fd = mkstemp(temp)) < 0)
76 if (ftruncate(fd, 1024) != 0)
82 main(int __unused argc, char __unused *argv[])
85 int orgfd, fd1, fd2, test = 0;
91 /* If dup(2) ever work? */
92 if ((fd1 = dup(orgfd)) < 0)
94 printf("ok %d - dup(2) works\n", ++test);
96 /* Set close-on-exec */
97 if (fcntl(fd1, F_SETFD, 1) != 0)
98 err(1, "fcntl(F_SETFD)");
100 /* If dup2(2) ever work? */
101 if ((fd2 = dup2(fd1, fd1 + 1)) < 0)
103 printf("ok %d - dup2(2) works\n", ++test);
105 /* Do we get the right fd? */
108 printf("no ok %d - dup2(2) didn't give us the right fd\n",
111 printf("ok %d - dup2(2) returned a correct fd\n", test);
113 /* Was close-on-exec cleared? */
115 if (fcntl(fd2, F_GETFD) != 0)
116 printf("not ok %d - dup2(2) didn't clear close-on-exec\n",
119 printf("ok %d - dup2(2) cleared close-on-exec\n", test);
124 * We're testing a small tweak in dup2 semantics.
125 * Normally dup and dup2 will clear the close-on-exec
126 * flag on the new fd (which appears to be an implementation
127 * mistake from start and not some planned behavior).
128 * In today's implementations of dup and dup2 we have to make
129 * an effort to really clear that flag. But all tested
130 * implementations of dup2 have another tweak. If we
131 * dup2(old, new) when old == new, the syscall short-circuits
132 * and returns early (because there is no need to do all the
133 * work (and there is a risk for serious mistakes)).
134 * So although the docs say that dup2 should "take 'old',
135 * close 'new' perform a dup(2) of 'old' into 'new'"
136 * the docs are not really followed because close-on-exec
137 * is not cleared on 'new'.
139 * Since everyone has this bug, we pretend that this is
140 * the way it is supposed to be and test here that it really
143 * This is a fine example on where two separate implementation
144 * fuckups take out each other and make the end-result the way
145 * it was meant to be.
147 if ((fd2 = dup2(fd1, fd1)) < 0)
149 printf("ok %d - dup2(2) to itself works\n", ++test);
151 /* Do we get the right fd? */
154 printf("not ok %d - dup2(2) didn't give us the right fd\n",
157 printf("ok %d - dup2(2) to itself returned a correct fd\n",
160 /* Was close-on-exec cleared? */
162 if (fcntl(fd2, F_GETFD) == 0)
163 printf("not ok %d - dup2(2) cleared close-on-exec\n", test);
165 printf("ok %d - dup2(2) didn't clear close-on-exec\n", test);
167 /* Does fcntl(F_DUPFD) work? */
168 if ((fd2 = fcntl(fd1, F_DUPFD, 10)) < 0)
169 err(1, "fcntl(F_DUPFD)");
171 printf("not ok %d - fcntl(F_DUPFD) returned wrong fd %d\n",
174 printf("ok %d - fcntl(F_DUPFD) works\n", ++test);
176 /* Was close-on-exec cleared? */
178 if (fcntl(fd2, F_GETFD) != 0)
180 "not ok %d - fcntl(F_DUPFD) didn't clear close-on-exec\n",
183 printf("ok %d - fcntl(F_DUPFD) cleared close-on-exec\n", test);
186 if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
188 if ((fd2 = dup2(fd1, rlp.rlim_cur + 1)) >= 0)
189 printf("not ok %d - dup2(2) bypassed NOFILE limit\n", test);
191 printf("ok %d - dup2(2) didn't bypass NOFILE limit\n", test);
193 /* If fcntl(F_DUP2FD) ever work? */
194 if ((fd2 = fcntl(fd1, F_DUP2FD, fd1 + 1)) < 0)
195 err(1, "fcntl(F_DUP2FD)");
196 printf("ok %d - fcntl(F_DUP2FD) works\n", ++test);
198 /* Do we get the right fd? */
202 "no ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n",
205 printf("ok %d - fcntl(F_DUP2FD) returned a correct fd\n",
208 /* Was close-on-exec cleared? */
210 if (fcntl(fd2, F_GETFD) != 0)
212 "not ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n",
215 printf("ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n",
219 if ((fd2 = fcntl(fd1, F_DUP2FD, fd1)) < 0)
220 err(1, "fcntl(F_DUP2FD)");
221 printf("ok %d - fcntl(F_DUP2FD) to itself works\n", ++test);
223 /* Do we get the right fd? */
227 "not ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n",
231 "ok %d - fcntl(F_DUP2FD) to itself returned a correct fd\n",
234 /* Was close-on-exec cleared? */
236 if (fcntl(fd2, F_GETFD) == 0)
237 printf("not ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n",
240 printf("ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n",
244 if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
246 if ((fd2 = fcntl(fd1, F_DUP2FD, rlp.rlim_cur + 1)) >= 0)
247 printf("not ok %d - fcntl(F_DUP2FD) bypassed NOFILE limit\n",
250 printf("ok %d - fcntl(F_DUP2FD) didn't bypass NOFILE limit\n",
253 /* Does fcntl(F_DUPFD_CLOEXEC) work? */
254 if ((fd2 = fcntl(fd1, F_DUPFD_CLOEXEC, 10)) < 0)
255 err(1, "fcntl(F_DUPFD_CLOEXEC)");
257 printf("not ok %d - fcntl(F_DUPFD_CLOEXEC) returned wrong fd %d\n",
260 printf("ok %d - fcntl(F_DUPFD_CLOEXEC) works\n", ++test);
262 /* Was close-on-exec cleared? */
264 if (fcntl(fd2, F_GETFD) != 1)
266 "not ok %d - fcntl(F_DUPFD_CLOEXEC) didn't set close-on-exec\n",
269 printf("ok %d - fcntl(F_DUPFD_CLOEXEC) set close-on-exec\n",
272 /* If fcntl(F_DUP2FD_CLOEXEC) ever work? */
273 if ((fd2 = fcntl(fd1, F_DUP2FD_CLOEXEC, fd1 + 1)) < 0)
274 err(1, "fcntl(F_DUP2FD_CLOEXEC)");
275 printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) works\n", ++test);
277 /* Do we get the right fd? */
281 "no ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't give us the right fd\n",
284 printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) returned a correct fd\n",
287 /* Was close-on-exec set? */
289 if (fcntl(fd2, F_GETFD) != FD_CLOEXEC)
291 "not ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't set close-on-exec\n",
294 printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) set close-on-exec\n",
298 * It is unclear what F_DUP2FD_CLOEXEC should do when duplicating a
299 * file descriptor onto itself.
303 if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
305 if ((fd2 = fcntl(fd1, F_DUP2FD_CLOEXEC, rlp.rlim_cur + 1)) >= 0)
306 printf("not ok %d - fcntl(F_DUP2FD_CLOEXEC) bypassed NOFILE limit\n",
309 printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't bypass NOFILE limit\n",
312 /* Does dup3(O_CLOEXEC) ever work? */
313 if ((fd2 = dup3(fd1, fd1 + 1, O_CLOEXEC)) < 0)
314 err(1, "dup3(O_CLOEXEC)");
315 printf("ok %d - dup3(O_CLOEXEC) works\n", ++test);
317 /* Do we get the right fd? */
321 "no ok %d - dup3(O_CLOEXEC) didn't give us the right fd\n",
324 printf("ok %d - dup3(O_CLOEXEC) returned a correct fd\n",
327 /* Was close-on-exec set? */
329 if (fcntl(fd2, F_GETFD) != FD_CLOEXEC)
331 "not ok %d - dup3(O_CLOEXEC) didn't set close-on-exec\n",
334 printf("ok %d - dup3(O_CLOEXEC) set close-on-exec\n",
337 /* Does dup3(0) ever work? */
338 if ((fd2 = dup3(fd1, fd1 + 1, 0)) < 0)
340 printf("ok %d - dup3(0) works\n", ++test);
342 /* Do we get the right fd? */
346 "no ok %d - dup3(0) didn't give us the right fd\n",
349 printf("ok %d - dup3(0) returned a correct fd\n",
352 /* Was close-on-exec cleared? */
354 if (fcntl(fd2, F_GETFD) != 0)
356 "not ok %d - dup3(0) didn't clear close-on-exec\n",
359 printf("ok %d - dup3(0) cleared close-on-exec\n",
362 /* dup3() does not allow duplicating to the same fd */
364 if (dup3(fd1, fd1, O_CLOEXEC) != -1)
366 "not ok %d - dup3(fd1, fd1, O_CLOEXEC) succeeded\n", test);
368 printf("ok %d - dup3(fd1, fd1, O_CLOEXEC) failed\n", test);
371 if (dup3(fd1, fd1, 0) != -1)
373 "not ok %d - dup3(fd1, fd1, 0) succeeded\n", test);
375 printf("ok %d - dup3(fd1, fd1, 0) failed\n", test);
378 if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
380 if ((fd2 = dup3(fd1, rlp.rlim_cur + 1, O_CLOEXEC)) >= 0)
381 printf("not ok %d - dup3(O_CLOEXEC) bypassed NOFILE limit\n",
384 printf("ok %d - dup3(O_CLOEXEC) didn't bypass NOFILE limit\n",