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.
53 #include <sys/types.h>
55 #include <sys/resource.h>
63 static int getafile(void);
70 char temp[] = "/tmp/dup2XXXXXXXXX";
71 if ((fd = mkstemp(temp)) < 0)
74 if (ftruncate(fd, 1024) != 0)
80 main(int __unused argc, char __unused *argv[])
83 int orgfd, fd1, fd2, test = 0;
89 /* If dup(2) ever work? */
90 if ((fd1 = dup(orgfd)) < 0)
92 printf("ok %d - dup(2) works\n", ++test);
94 /* Set close-on-exec */
95 if (fcntl(fd1, F_SETFD, 1) != 0)
96 err(1, "fcntl(F_SETFD)");
98 /* If dup2(2) ever work? */
99 if ((fd2 = dup2(fd1, fd1 + 1)) < 0)
101 printf("ok %d - dup2(2) works\n", ++test);
103 /* Do we get the right fd? */
106 printf("no ok %d - dup2(2) didn't give us the right fd\n",
109 printf("ok %d - dup2(2) returned a correct fd\n", test);
111 /* Was close-on-exec cleared? */
113 if (fcntl(fd2, F_GETFD) != 0)
114 printf("not ok %d - dup2(2) didn't clear close-on-exec\n",
117 printf("ok %d - dup2(2) cleared close-on-exec\n", test);
122 * We're testing a small tweak in dup2 semantics.
123 * Normally dup and dup2 will clear the close-on-exec
124 * flag on the new fd (which appears to be an implementation
125 * mistake from start and not some planned behavior).
126 * In today's implementations of dup and dup2 we have to make
127 * an effort to really clear that flag. But all tested
128 * implementations of dup2 have another tweak. If we
129 * dup2(old, new) when old == new, the syscall short-circuits
130 * and returns early (because there is no need to do all the
131 * work (and there is a risk for serious mistakes)).
132 * So although the docs say that dup2 should "take 'old',
133 * close 'new' perform a dup(2) of 'old' into 'new'"
134 * the docs are not really followed because close-on-exec
135 * is not cleared on 'new'.
137 * Since everyone has this bug, we pretend that this is
138 * the way it is supposed to be and test here that it really
141 * This is a fine example on where two separate implementation
142 * fuckups take out each other and make the end-result the way
143 * it was meant to be.
145 if ((fd2 = dup2(fd1, fd1)) < 0)
147 printf("ok %d - dup2(2) to itself works\n", ++test);
149 /* Do we get the right fd? */
152 printf("not ok %d - dup2(2) didn't give us the right fd\n",
155 printf("ok %d - dup2(2) to itself returned a correct fd\n",
158 /* Was close-on-exec cleared? */
160 if (fcntl(fd2, F_GETFD) == 0)
161 printf("not ok %d - dup2(2) cleared close-on-exec\n", test);
163 printf("ok %d - dup2(2) didn't clear close-on-exec\n", test);
165 /* Does fcntl(F_DUPFD) work? */
166 if ((fd2 = fcntl(fd1, F_DUPFD, 10)) < 0)
167 err(1, "fcntl(F_DUPFD)");
169 printf("not ok %d - fcntl(F_DUPFD) returned wrong fd %d\n",
172 printf("ok %d - fcntl(F_DUPFD) works\n", ++test);
174 /* Was close-on-exec cleared? */
176 if (fcntl(fd2, F_GETFD) != 0)
178 "not ok %d - fcntl(F_DUPFD) didn't clear close-on-exec\n",
181 printf("ok %d - fcntl(F_DUPFD) cleared close-on-exec\n", test);
184 if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
186 if ((fd2 = dup2(fd1, rlp.rlim_cur + 1)) >= 0)
187 printf("not ok %d - dup2(2) bypassed NOFILE limit\n", test);
189 printf("ok %d - dup2(2) didn't bypass NOFILE limit\n", test);
191 /* If fcntl(F_DUP2FD) ever work? */
192 if ((fd2 = fcntl(fd1, F_DUP2FD, fd1 + 1)) < 0)
193 err(1, "fcntl(F_DUP2FD)");
194 printf("ok %d - fcntl(F_DUP2FD) works\n", ++test);
196 /* Do we get the right fd? */
200 "no ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n",
203 printf("ok %d - fcntl(F_DUP2FD) returned a correct fd\n",
206 /* Was close-on-exec cleared? */
208 if (fcntl(fd2, F_GETFD) != 0)
210 "not ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n",
213 printf("ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n",
217 if ((fd2 = fcntl(fd1, F_DUP2FD, fd1)) < 0)
218 err(1, "fcntl(F_DUP2FD)");
219 printf("ok %d - fcntl(F_DUP2FD) to itself works\n", ++test);
221 /* Do we get the right fd? */
225 "not ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n",
229 "ok %d - fcntl(F_DUP2FD) to itself returned a correct fd\n",
232 /* Was close-on-exec cleared? */
234 if (fcntl(fd2, F_GETFD) == 0)
235 printf("not ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n",
238 printf("ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n",
242 if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
244 if ((fd2 = fcntl(fd1, F_DUP2FD, rlp.rlim_cur + 1)) >= 0)
245 printf("not ok %d - fcntl(F_DUP2FD) bypassed NOFILE limit\n",
248 printf("ok %d - fcntl(F_DUP2FD) didn't bypass NOFILE limit\n",
251 /* Does fcntl(F_DUPFD_CLOEXEC) work? */
252 if ((fd2 = fcntl(fd1, F_DUPFD_CLOEXEC, 10)) < 0)
253 err(1, "fcntl(F_DUPFD_CLOEXEC)");
255 printf("not ok %d - fcntl(F_DUPFD_CLOEXEC) returned wrong fd %d\n",
258 printf("ok %d - fcntl(F_DUPFD_CLOEXEC) works\n", ++test);
260 /* Was close-on-exec cleared? */
262 if (fcntl(fd2, F_GETFD) != 1)
264 "not ok %d - fcntl(F_DUPFD_CLOEXEC) didn't set close-on-exec\n",
267 printf("ok %d - fcntl(F_DUPFD_CLOEXEC) set close-on-exec\n",
270 /* If fcntl(F_DUP2FD_CLOEXEC) ever work? */
271 if ((fd2 = fcntl(fd1, F_DUP2FD_CLOEXEC, fd1 + 1)) < 0)
272 err(1, "fcntl(F_DUP2FD_CLOEXEC)");
273 printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) works\n", ++test);
275 /* Do we get the right fd? */
279 "no ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't give us the right fd\n",
282 printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) returned a correct fd\n",
285 /* Was close-on-exec set? */
287 if (fcntl(fd2, F_GETFD) != FD_CLOEXEC)
289 "not ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't set close-on-exec\n",
292 printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) set close-on-exec\n",
296 * It is unclear what F_DUP2FD_CLOEXEC should do when duplicating a
297 * file descriptor onto itself.
301 if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
303 if ((fd2 = fcntl(fd1, F_DUP2FD_CLOEXEC, rlp.rlim_cur + 1)) >= 0)
304 printf("not ok %d - fcntl(F_DUP2FD_CLOEXEC) bypassed NOFILE limit\n",
307 printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't bypass NOFILE limit\n",
310 /* Does dup3(O_CLOEXEC) ever work? */
311 if ((fd2 = dup3(fd1, fd1 + 1, O_CLOEXEC)) < 0)
312 err(1, "dup3(O_CLOEXEC)");
313 printf("ok %d - dup3(O_CLOEXEC) works\n", ++test);
315 /* Do we get the right fd? */
319 "no ok %d - dup3(O_CLOEXEC) didn't give us the right fd\n",
322 printf("ok %d - dup3(O_CLOEXEC) returned a correct fd\n",
325 /* Was close-on-exec set? */
327 if (fcntl(fd2, F_GETFD) != FD_CLOEXEC)
329 "not ok %d - dup3(O_CLOEXEC) didn't set close-on-exec\n",
332 printf("ok %d - dup3(O_CLOEXEC) set close-on-exec\n",
335 /* Does dup3(0) ever work? */
336 if ((fd2 = dup3(fd1, fd1 + 1, 0)) < 0)
338 printf("ok %d - dup3(0) works\n", ++test);
340 /* Do we get the right fd? */
344 "no ok %d - dup3(0) didn't give us the right fd\n",
347 printf("ok %d - dup3(0) returned a correct fd\n",
350 /* Was close-on-exec cleared? */
352 if (fcntl(fd2, F_GETFD) != 0)
354 "not ok %d - dup3(0) didn't clear close-on-exec\n",
357 printf("ok %d - dup3(0) cleared close-on-exec\n",
360 /* dup3() does not allow duplicating to the same fd */
362 if (dup3(fd1, fd1, O_CLOEXEC) != -1)
364 "not ok %d - dup3(fd1, fd1, O_CLOEXEC) succeeded\n", test);
366 printf("ok %d - dup3(fd1, fd1, O_CLOEXEC) failed\n", test);
369 if (dup3(fd1, fd1, 0) != -1)
371 "not ok %d - dup3(fd1, fd1, 0) succeeded\n", test);
373 printf("ok %d - dup3(fd1, fd1, 0) failed\n", test);
376 if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
378 if ((fd2 = dup3(fd1, rlp.rlim_cur + 1, O_CLOEXEC)) >= 0)
379 printf("not ok %d - dup3(O_CLOEXEC) bypassed NOFILE limit\n",
382 printf("ok %d - dup3(O_CLOEXEC) didn't bypass NOFILE limit\n",