1164191Smaxim/*
2164191Smaxim * $OpenBSD: dup2test.c,v 1.3 2003/07/31 21:48:08 deraadt Exp $
3164191Smaxim * $OpenBSD: dup2_self.c,v 1.3 2003/07/31 21:48:08 deraadt Exp $
4164191Smaxim * $OpenBSD: fcntl_dup.c,v 1.2 2003/07/31 21:48:08 deraadt Exp $
5164191Smaxim *
6164191Smaxim * Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain.
7164191Smaxim *
8164191Smaxim * $FreeBSD$
9164191Smaxim */
10164191Smaxim
11164191Smaxim/*
12164191Smaxim * Test #1:  check if dup(2) works.
13164191Smaxim * Test #2:  check if dup2(2) works.
14164191Smaxim * Test #3:  check if dup2(2) returned a fd we asked for.
15164191Smaxim * Test #4:  check if dup2(2) cleared close-on-exec flag for duped fd.
16164191Smaxim * Test #5:  check if dup2(2) allows to dup fd to itself.
17164191Smaxim * Test #6:  check if dup2(2) returned a fd we asked for.
18164191Smaxim * Test #7:  check if dup2(2) did not clear close-on-exec flag for duped fd.
19164191Smaxim * Test #8:  check if fcntl(F_DUPFD) works.
20164191Smaxim * Test #9:  check if fcntl(F_DUPFD) cleared close-on-exec flag for duped fd.
21164191Smaxim * Test #10: check if dup2() to a fd > current maximum number of open files
22164191Smaxim *           limit work.
23176957Santoine * Test #11: check if fcntl(F_DUP2FD) works.
24176957Santoine * Test #12: check if fcntl(F_DUP2FD) returned a fd we asked for.
25176957Santoine * Test #13: check if fcntl(F_DUP2FD) cleared close-on-exec flag for duped fd.
26176957Santoine * Test #14: check if fcntl(F_DUP2FD) allows to dup fd to itself.
27176957Santoine * Test #15: check if fcntl(F_DUP2FD) returned a fd we asked for.
28176957Santoine * Test #16: check if fcntl(F_DUP2FD) did not clear close-on-exec flag for
29176957Santoine *           duped fd.
30176957Santoine * Test #17: check if fcntl(F_DUP2FD) to a fd > current maximum number of open
31176957Santoine *           files limit work.
32250513Sjilles * Test #18: check if fcntl(F_DUPFD_CLOEXEC) works.
33250513Sjilles * Test #19: check if fcntl(F_DUPFD_CLOEXEC) set close-on-exec flag for duped
34250513Sjilles *           fd.
35250530Sjilles * Test #20: check if fcntl(F_DUP2FD_CLOEXEC) works.
36250530Sjilles * Test #21: check if fcntl(F_DUP2FD_CLOEXEC) returned a fd we asked for.
37250530Sjilles * Test #22: check if fcntl(F_DUP2FD_CLOEXEC) set close-on-exec flag for duped
38250530Sjilles *           fd.
39250530Sjilles * Test #23: check if fcntl(F_DUP2FD_CLOEXEC) to a fd > current maximum number
40250530Sjilles *           of open files limit work.
41254411Sjilles * Test #24: check if dup3(O_CLOEXEC) works.
42254411Sjilles * Test #25: check if dup3(O_CLOEXEC) returned a fd we asked for.
43254411Sjilles * Test #26: check if dup3(O_CLOEXEC) set close-on-exec flag for duped fd.
44254411Sjilles * Test #27: check if dup3(0) works.
45254411Sjilles * Test #28: check if dup3(0) returned a fd we asked for.
46254411Sjilles * Test #29: check if dup3(0) cleared close-on-exec flag for duped fd.
47254411Sjilles * Test #30: check if dup3(O_CLOEXEC) fails if oldfd == newfd.
48254411Sjilles * Test #31: check if dup3(0) fails if oldfd == newfd.
49254411Sjilles * Test #32: check if dup3(O_CLOEXEC) to a fd > current maximum number of
50254411Sjilles *           open files limit work.
51164191Smaxim */
52164191Smaxim
53164191Smaxim#include <sys/types.h>
54164191Smaxim#include <sys/time.h>
55164191Smaxim#include <sys/resource.h>
56164191Smaxim
57164191Smaxim#include <err.h>
58164191Smaxim#include <fcntl.h>
59164191Smaxim#include <stdio.h>
60164191Smaxim#include <stdlib.h>
61164191Smaxim#include <unistd.h>
62164191Smaxim
63164191Smaximstatic int	getafile(void);
64164191Smaxim
65164191Smaximstatic int
66164191Smaximgetafile(void)
67164191Smaxim{
68164191Smaxim	int fd;
69164191Smaxim
70164191Smaxim	char temp[] = "/tmp/dup2XXXXXXXXX";
71164191Smaxim	if ((fd = mkstemp(temp)) < 0)
72164191Smaxim		err(1, "mkstemp");
73164191Smaxim	remove(temp);
74164191Smaxim	if (ftruncate(fd, 1024) != 0)
75164191Smaxim		err(1, "ftruncate");
76164191Smaxim	return (fd);
77164191Smaxim}
78164191Smaxim
79164191Smaximint
80164191Smaximmain(int __unused argc, char __unused *argv[])
81164191Smaxim{
82164191Smaxim	struct rlimit rlp;
83164191Smaxim	int orgfd, fd1, fd2, test = 0;
84164191Smaxim
85164191Smaxim	orgfd = getafile();
86164191Smaxim
87254411Sjilles	printf("1..32\n");
88164191Smaxim
89164191Smaxim	/* If dup(2) ever work? */
90164191Smaxim	if ((fd1 = dup(orgfd)) < 0)
91164191Smaxim		err(1, "dup");
92164191Smaxim	printf("ok %d - dup(2) works\n", ++test);
93164191Smaxim
94164191Smaxim	/* Set close-on-exec */
95164191Smaxim	if (fcntl(fd1, F_SETFD, 1) != 0)
96164191Smaxim		err(1, "fcntl(F_SETFD)");
97164191Smaxim
98164191Smaxim	/* If dup2(2) ever work? */
99164191Smaxim	if ((fd2 = dup2(fd1, fd1 + 1)) < 0)
100164191Smaxim		err(1, "dup2");
101164191Smaxim	printf("ok %d - dup2(2) works\n", ++test);
102164191Smaxim
103164191Smaxim	/* Do we get the right fd? */
104164191Smaxim	++test;
105164191Smaxim	if (fd2 != fd1 + 1)
106164191Smaxim		printf("no ok %d - dup2(2) didn't give us the right fd\n",
107164191Smaxim		    test);
108164191Smaxim	else
109164191Smaxim		printf("ok %d - dup2(2) returned a correct fd\n", test);
110164191Smaxim
111164191Smaxim	/* Was close-on-exec cleared? */
112164191Smaxim	++test;
113164191Smaxim	if (fcntl(fd2, F_GETFD) != 0)
114164191Smaxim		printf("not ok %d - dup2(2) didn't clear close-on-exec\n",
115164191Smaxim		    test);
116164191Smaxim	else
117164191Smaxim		printf("ok %d - dup2(2) cleared close-on-exec\n", test);
118164191Smaxim
119164191Smaxim	/*
120164191Smaxim	 * Dup to itself.
121164191Smaxim	 *
122164191Smaxim	 * We're testing a small tweak in dup2 semantics.
123164191Smaxim	 * Normally dup and dup2 will clear the close-on-exec
124164191Smaxim	 * flag on the new fd (which appears to be an implementation
125164191Smaxim	 * mistake from start and not some planned behavior).
126228975Suqs	 * In today's implementations of dup and dup2 we have to make
127164191Smaxim	 * an effort to really clear that flag.  But all tested
128164191Smaxim	 * implementations of dup2 have another tweak.  If we
129164191Smaxim	 * dup2(old, new) when old == new, the syscall short-circuits
130164191Smaxim	 * and returns early (because there is no need to do all the
131164191Smaxim	 * work (and there is a risk for serious mistakes)).
132164191Smaxim	 * So although the docs say that dup2 should "take 'old',
133164191Smaxim	 * close 'new' perform a dup(2) of 'old' into 'new'"
134164191Smaxim	 * the docs are not really followed because close-on-exec
135164191Smaxim	 * is not cleared on 'new'.
136164191Smaxim	 *
137164191Smaxim	 * Since everyone has this bug, we pretend that this is
138164191Smaxim	 * the way it is supposed to be and test here that it really
139164191Smaxim	 * works that way.
140164191Smaxim	 *
141164191Smaxim	 * This is a fine example on where two separate implementation
142164191Smaxim	 * fuckups take out each other and make the end-result the way
143164191Smaxim	 * it was meant to be.
144164191Smaxim	 */
145164192Smaxim	if ((fd2 = dup2(fd1, fd1)) < 0)
146164191Smaxim		err(1, "dup2");
147164191Smaxim	printf("ok %d - dup2(2) to itself works\n", ++test);
148164191Smaxim
149164191Smaxim	/* Do we get the right fd? */
150164191Smaxim	++test;
151164191Smaxim	if (fd2 != fd1)
152164191Smaxim		printf("not ok %d - dup2(2) didn't give us the right fd\n",
153164191Smaxim		    test);
154164191Smaxim	else
155164191Smaxim		printf("ok %d - dup2(2) to itself returned a correct fd\n",
156164191Smaxim		    test);
157164191Smaxim
158164191Smaxim	/* Was close-on-exec cleared? */
159164191Smaxim	++test;
160164191Smaxim	if (fcntl(fd2, F_GETFD) == 0)
161164191Smaxim		printf("not ok %d - dup2(2) cleared close-on-exec\n", test);
162164191Smaxim	else
163164191Smaxim		printf("ok %d - dup2(2) didn't clear close-on-exec\n", test);
164164191Smaxim
165164191Smaxim	/* Does fcntl(F_DUPFD) work? */
166250512Sjilles	if ((fd2 = fcntl(fd1, F_DUPFD, 10)) < 0)
167164191Smaxim		err(1, "fcntl(F_DUPFD)");
168250512Sjilles	if (fd2 < 10)
169250512Sjilles		printf("not ok %d - fcntl(F_DUPFD) returned wrong fd %d\n",
170250512Sjilles		    ++test, fd2);
171250512Sjilles	else
172250512Sjilles		printf("ok %d - fcntl(F_DUPFD) works\n", ++test);
173164191Smaxim
174164191Smaxim	/* Was close-on-exec cleared? */
175164191Smaxim	++test;
176164191Smaxim        if (fcntl(fd2, F_GETFD) != 0)
177164191Smaxim		printf(
178164191Smaxim		    "not ok %d - fcntl(F_DUPFD) didn't clear close-on-exec\n",
179164191Smaxim		    test);
180164191Smaxim	else
181164191Smaxim		printf("ok %d - fcntl(F_DUPFD) cleared close-on-exec\n", test);
182164191Smaxim
183164191Smaxim	++test;
184164191Smaxim	if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
185164191Smaxim		err(1, "getrlimit");
186176957Santoine	if ((fd2 = dup2(fd1, rlp.rlim_cur + 1)) >= 0)
187164191Smaxim		printf("not ok %d - dup2(2) bypassed NOFILE limit\n", test);
188164191Smaxim	else
189164191Smaxim		printf("ok %d - dup2(2) didn't bypass NOFILE limit\n", test);
190164191Smaxim
191176957Santoine	/* If fcntl(F_DUP2FD) ever work? */
192176957Santoine	if ((fd2 = fcntl(fd1, F_DUP2FD, fd1 + 1)) < 0)
193176957Santoine		err(1, "fcntl(F_DUP2FD)");
194176957Santoine	printf("ok %d - fcntl(F_DUP2FD) works\n", ++test);
195176957Santoine
196176957Santoine	/* Do we get the right fd? */
197176957Santoine	++test;
198176957Santoine	if (fd2 != fd1 + 1)
199176957Santoine		printf(
200176957Santoine		    "no ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n",
201176957Santoine		    test);
202176957Santoine	else
203176957Santoine		printf("ok %d - fcntl(F_DUP2FD) returned a correct fd\n",
204176957Santoine		    test);
205176957Santoine
206176957Santoine	/* Was close-on-exec cleared? */
207176957Santoine	++test;
208176957Santoine	if (fcntl(fd2, F_GETFD) != 0)
209176957Santoine		printf(
210176957Santoine		    "not ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n",
211176957Santoine		    test);
212176957Santoine	else
213176957Santoine		printf("ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n",
214176957Santoine		    test);
215176957Santoine
216176957Santoine	/* Dup to itself */
217176957Santoine	if ((fd2 = fcntl(fd1, F_DUP2FD, fd1)) < 0)
218176957Santoine		err(1, "fcntl(F_DUP2FD)");
219176957Santoine	printf("ok %d - fcntl(F_DUP2FD) to itself works\n", ++test);
220176957Santoine
221176957Santoine	/* Do we get the right fd? */
222176957Santoine	++test;
223176957Santoine	if (fd2 != fd1)
224176957Santoine		printf(
225176957Santoine		    "not ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n",
226176957Santoine		    test);
227176957Santoine	else
228176957Santoine		printf(
229176957Santoine		    "ok %d - fcntl(F_DUP2FD) to itself returned a correct fd\n",
230176957Santoine		    test);
231176957Santoine
232176957Santoine	/* Was close-on-exec cleared? */
233176957Santoine	++test;
234176957Santoine	if (fcntl(fd2, F_GETFD) == 0)
235176957Santoine		printf("not ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n",
236176957Santoine		    test);
237176957Santoine	else
238176957Santoine		printf("ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n",
239176957Santoine		    test);
240176957Santoine
241176957Santoine	++test;
242176957Santoine	if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
243176957Santoine		err(1, "getrlimit");
244176957Santoine	if ((fd2 = fcntl(fd1, F_DUP2FD, rlp.rlim_cur + 1)) >= 0)
245176957Santoine		printf("not ok %d - fcntl(F_DUP2FD) bypassed NOFILE limit\n",
246176957Santoine		    test);
247176957Santoine	else
248176957Santoine		printf("ok %d - fcntl(F_DUP2FD) didn't bypass NOFILE limit\n",
249176957Santoine		    test);
250176957Santoine
251250513Sjilles	/* Does fcntl(F_DUPFD_CLOEXEC) work? */
252250513Sjilles	if ((fd2 = fcntl(fd1, F_DUPFD_CLOEXEC, 10)) < 0)
253250513Sjilles		err(1, "fcntl(F_DUPFD_CLOEXEC)");
254250513Sjilles	if (fd2 < 10)
255250513Sjilles		printf("not ok %d - fcntl(F_DUPFD_CLOEXEC) returned wrong fd %d\n",
256250513Sjilles		    ++test, fd2);
257250513Sjilles	else
258250513Sjilles		printf("ok %d - fcntl(F_DUPFD_CLOEXEC) works\n", ++test);
259250513Sjilles
260250513Sjilles	/* Was close-on-exec cleared? */
261250513Sjilles	++test;
262250513Sjilles        if (fcntl(fd2, F_GETFD) != 1)
263250513Sjilles		printf(
264250513Sjilles		    "not ok %d - fcntl(F_DUPFD_CLOEXEC) didn't set close-on-exec\n",
265250513Sjilles		    test);
266250513Sjilles	else
267250513Sjilles		printf("ok %d - fcntl(F_DUPFD_CLOEXEC) set close-on-exec\n",
268250513Sjilles		    test);
269250513Sjilles
270250530Sjilles	/* If fcntl(F_DUP2FD_CLOEXEC) ever work? */
271250530Sjilles	if ((fd2 = fcntl(fd1, F_DUP2FD_CLOEXEC, fd1 + 1)) < 0)
272250530Sjilles		err(1, "fcntl(F_DUP2FD_CLOEXEC)");
273250530Sjilles	printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) works\n", ++test);
274250530Sjilles
275250530Sjilles	/* Do we get the right fd? */
276250530Sjilles	++test;
277250530Sjilles	if (fd2 != fd1 + 1)
278250530Sjilles		printf(
279250530Sjilles		    "no ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't give us the right fd\n",
280250530Sjilles		    test);
281250530Sjilles	else
282250530Sjilles		printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) returned a correct fd\n",
283250530Sjilles		    test);
284250530Sjilles
285250530Sjilles	/* Was close-on-exec set? */
286250530Sjilles	++test;
287250530Sjilles	if (fcntl(fd2, F_GETFD) != FD_CLOEXEC)
288250530Sjilles		printf(
289250530Sjilles		    "not ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't set close-on-exec\n",
290250530Sjilles		    test);
291250530Sjilles	else
292250530Sjilles		printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) set close-on-exec\n",
293250530Sjilles		    test);
294250530Sjilles
295250530Sjilles	/*
296250530Sjilles	 * It is unclear what F_DUP2FD_CLOEXEC should do when duplicating a
297250530Sjilles	 * file descriptor onto itself.
298250530Sjilles	 */
299250530Sjilles
300250530Sjilles	++test;
301250530Sjilles	if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
302250530Sjilles		err(1, "getrlimit");
303250530Sjilles	if ((fd2 = fcntl(fd1, F_DUP2FD_CLOEXEC, rlp.rlim_cur + 1)) >= 0)
304250530Sjilles		printf("not ok %d - fcntl(F_DUP2FD_CLOEXEC) bypassed NOFILE limit\n",
305250530Sjilles		    test);
306250530Sjilles	else
307250530Sjilles		printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't bypass NOFILE limit\n",
308250530Sjilles		    test);
309250530Sjilles
310254411Sjilles	/* Does dup3(O_CLOEXEC) ever work? */
311254411Sjilles	if ((fd2 = dup3(fd1, fd1 + 1, O_CLOEXEC)) < 0)
312254411Sjilles		err(1, "dup3(O_CLOEXEC)");
313254411Sjilles	printf("ok %d - dup3(O_CLOEXEC) works\n", ++test);
314254411Sjilles
315254411Sjilles	/* Do we get the right fd? */
316254411Sjilles	++test;
317254411Sjilles	if (fd2 != fd1 + 1)
318254411Sjilles		printf(
319254411Sjilles		    "no ok %d - dup3(O_CLOEXEC) didn't give us the right fd\n",
320254411Sjilles		    test);
321254411Sjilles	else
322254411Sjilles		printf("ok %d - dup3(O_CLOEXEC) returned a correct fd\n",
323254411Sjilles		    test);
324254411Sjilles
325254411Sjilles	/* Was close-on-exec set? */
326254411Sjilles	++test;
327254411Sjilles	if (fcntl(fd2, F_GETFD) != FD_CLOEXEC)
328254411Sjilles		printf(
329254411Sjilles		    "not ok %d - dup3(O_CLOEXEC) didn't set close-on-exec\n",
330254411Sjilles		    test);
331254411Sjilles	else
332254411Sjilles		printf("ok %d - dup3(O_CLOEXEC) set close-on-exec\n",
333254411Sjilles		    test);
334254411Sjilles
335254411Sjilles	/* Does dup3(0) ever work? */
336254411Sjilles	if ((fd2 = dup3(fd1, fd1 + 1, 0)) < 0)
337254411Sjilles		err(1, "dup3(0)");
338254411Sjilles	printf("ok %d - dup3(0) works\n", ++test);
339254411Sjilles
340254411Sjilles	/* Do we get the right fd? */
341254411Sjilles	++test;
342254411Sjilles	if (fd2 != fd1 + 1)
343254411Sjilles		printf(
344254411Sjilles		    "no ok %d - dup3(0) didn't give us the right fd\n",
345254411Sjilles		    test);
346254411Sjilles	else
347254411Sjilles		printf("ok %d - dup3(0) returned a correct fd\n",
348254411Sjilles		    test);
349254411Sjilles
350254411Sjilles	/* Was close-on-exec cleared? */
351254411Sjilles	++test;
352254411Sjilles	if (fcntl(fd2, F_GETFD) != 0)
353254411Sjilles		printf(
354254411Sjilles		    "not ok %d - dup3(0) didn't clear close-on-exec\n",
355254411Sjilles		    test);
356254411Sjilles	else
357254411Sjilles		printf("ok %d - dup3(0) cleared close-on-exec\n",
358254411Sjilles		    test);
359254411Sjilles
360254411Sjilles	/* dup3() does not allow duplicating to the same fd */
361254411Sjilles	++test;
362254411Sjilles	if (dup3(fd1, fd1, O_CLOEXEC) != -1)
363254411Sjilles		printf(
364254411Sjilles		    "not ok %d - dup3(fd1, fd1, O_CLOEXEC) succeeded\n", test);
365254411Sjilles	else
366254411Sjilles		printf("ok %d - dup3(fd1, fd1, O_CLOEXEC) failed\n", test);
367254411Sjilles
368254411Sjilles	++test;
369254411Sjilles	if (dup3(fd1, fd1, 0) != -1)
370254411Sjilles		printf(
371254411Sjilles		    "not ok %d - dup3(fd1, fd1, 0) succeeded\n", test);
372254411Sjilles	else
373254411Sjilles		printf("ok %d - dup3(fd1, fd1, 0) failed\n", test);
374254411Sjilles
375254411Sjilles	++test;
376254411Sjilles	if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
377254411Sjilles		err(1, "getrlimit");
378254411Sjilles	if ((fd2 = dup3(fd1, rlp.rlim_cur + 1, O_CLOEXEC)) >= 0)
379254411Sjilles		printf("not ok %d - dup3(O_CLOEXEC) bypassed NOFILE limit\n",
380254411Sjilles		    test);
381254411Sjilles	else
382254411Sjilles		printf("ok %d - dup3(O_CLOEXEC) didn't bypass NOFILE limit\n",
383254411Sjilles		    test);
384254411Sjilles
385164191Smaxim	return (0);
386164191Smaxim}
387