dup_test.c revision 250512
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: head/tools/regression/file/dup/dup.c 250512 2013-05-11 15:45:44Z jilles $
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.
32164191Smaxim */
33164191Smaxim
34164191Smaxim#include <sys/types.h>
35164191Smaxim#include <sys/time.h>
36164191Smaxim#include <sys/resource.h>
37164191Smaxim
38164191Smaxim#include <err.h>
39164191Smaxim#include <fcntl.h>
40164191Smaxim#include <stdio.h>
41164191Smaxim#include <stdlib.h>
42164191Smaxim#include <unistd.h>
43164191Smaxim
44164191Smaximstatic int	getafile(void);
45164191Smaxim
46164191Smaximstatic int
47164191Smaximgetafile(void)
48164191Smaxim{
49164191Smaxim	int fd;
50164191Smaxim
51164191Smaxim	char temp[] = "/tmp/dup2XXXXXXXXX";
52164191Smaxim	if ((fd = mkstemp(temp)) < 0)
53164191Smaxim		err(1, "mkstemp");
54164191Smaxim	remove(temp);
55164191Smaxim	if (ftruncate(fd, 1024) != 0)
56164191Smaxim		err(1, "ftruncate");
57164191Smaxim	return (fd);
58164191Smaxim}
59164191Smaxim
60164191Smaximint
61164191Smaximmain(int __unused argc, char __unused *argv[])
62164191Smaxim{
63164191Smaxim	struct rlimit rlp;
64164191Smaxim	int orgfd, fd1, fd2, test = 0;
65164191Smaxim
66164191Smaxim	orgfd = getafile();
67164191Smaxim
68176957Santoine	printf("1..17\n");
69164191Smaxim
70164191Smaxim	/* If dup(2) ever work? */
71164191Smaxim	if ((fd1 = dup(orgfd)) < 0)
72164191Smaxim		err(1, "dup");
73164191Smaxim	printf("ok %d - dup(2) works\n", ++test);
74164191Smaxim
75164191Smaxim	/* Set close-on-exec */
76164191Smaxim	if (fcntl(fd1, F_SETFD, 1) != 0)
77164191Smaxim		err(1, "fcntl(F_SETFD)");
78164191Smaxim
79164191Smaxim	/* If dup2(2) ever work? */
80164191Smaxim	if ((fd2 = dup2(fd1, fd1 + 1)) < 0)
81164191Smaxim		err(1, "dup2");
82164191Smaxim	printf("ok %d - dup2(2) works\n", ++test);
83164191Smaxim
84164191Smaxim	/* Do we get the right fd? */
85164191Smaxim	++test;
86164191Smaxim	if (fd2 != fd1 + 1)
87164191Smaxim		printf("no ok %d - dup2(2) didn't give us the right fd\n",
88164191Smaxim		    test);
89164191Smaxim	else
90164191Smaxim		printf("ok %d - dup2(2) returned a correct fd\n", test);
91164191Smaxim
92164191Smaxim	/* Was close-on-exec cleared? */
93164191Smaxim	++test;
94164191Smaxim	if (fcntl(fd2, F_GETFD) != 0)
95164191Smaxim		printf("not ok %d - dup2(2) didn't clear close-on-exec\n",
96164191Smaxim		    test);
97164191Smaxim	else
98164191Smaxim		printf("ok %d - dup2(2) cleared close-on-exec\n", test);
99164191Smaxim
100164191Smaxim	/*
101164191Smaxim	 * Dup to itself.
102164191Smaxim	 *
103164191Smaxim	 * We're testing a small tweak in dup2 semantics.
104164191Smaxim	 * Normally dup and dup2 will clear the close-on-exec
105164191Smaxim	 * flag on the new fd (which appears to be an implementation
106164191Smaxim	 * mistake from start and not some planned behavior).
107228975Suqs	 * In today's implementations of dup and dup2 we have to make
108164191Smaxim	 * an effort to really clear that flag.  But all tested
109164191Smaxim	 * implementations of dup2 have another tweak.  If we
110164191Smaxim	 * dup2(old, new) when old == new, the syscall short-circuits
111164191Smaxim	 * and returns early (because there is no need to do all the
112164191Smaxim	 * work (and there is a risk for serious mistakes)).
113164191Smaxim	 * So although the docs say that dup2 should "take 'old',
114164191Smaxim	 * close 'new' perform a dup(2) of 'old' into 'new'"
115164191Smaxim	 * the docs are not really followed because close-on-exec
116164191Smaxim	 * is not cleared on 'new'.
117164191Smaxim	 *
118164191Smaxim	 * Since everyone has this bug, we pretend that this is
119164191Smaxim	 * the way it is supposed to be and test here that it really
120164191Smaxim	 * works that way.
121164191Smaxim	 *
122164191Smaxim	 * This is a fine example on where two separate implementation
123164191Smaxim	 * fuckups take out each other and make the end-result the way
124164191Smaxim	 * it was meant to be.
125164191Smaxim	 */
126164192Smaxim	if ((fd2 = dup2(fd1, fd1)) < 0)
127164191Smaxim		err(1, "dup2");
128164191Smaxim	printf("ok %d - dup2(2) to itself works\n", ++test);
129164191Smaxim
130164191Smaxim	/* Do we get the right fd? */
131164191Smaxim	++test;
132164191Smaxim	if (fd2 != fd1)
133164191Smaxim		printf("not ok %d - dup2(2) didn't give us the right fd\n",
134164191Smaxim		    test);
135164191Smaxim	else
136164191Smaxim		printf("ok %d - dup2(2) to itself returned a correct fd\n",
137164191Smaxim		    test);
138164191Smaxim
139164191Smaxim	/* Was close-on-exec cleared? */
140164191Smaxim	++test;
141164191Smaxim	if (fcntl(fd2, F_GETFD) == 0)
142164191Smaxim		printf("not ok %d - dup2(2) cleared close-on-exec\n", test);
143164191Smaxim	else
144164191Smaxim		printf("ok %d - dup2(2) didn't clear close-on-exec\n", test);
145164191Smaxim
146164191Smaxim	/* Does fcntl(F_DUPFD) work? */
147250512Sjilles	if ((fd2 = fcntl(fd1, F_DUPFD, 10)) < 0)
148164191Smaxim		err(1, "fcntl(F_DUPFD)");
149250512Sjilles	if (fd2 < 10)
150250512Sjilles		printf("not ok %d - fcntl(F_DUPFD) returned wrong fd %d\n",
151250512Sjilles		    ++test, fd2);
152250512Sjilles	else
153250512Sjilles		printf("ok %d - fcntl(F_DUPFD) works\n", ++test);
154164191Smaxim
155164191Smaxim	/* Was close-on-exec cleared? */
156164191Smaxim	++test;
157164191Smaxim        if (fcntl(fd2, F_GETFD) != 0)
158164191Smaxim		printf(
159164191Smaxim		    "not ok %d - fcntl(F_DUPFD) didn't clear close-on-exec\n",
160164191Smaxim		    test);
161164191Smaxim	else
162164191Smaxim		printf("ok %d - fcntl(F_DUPFD) cleared close-on-exec\n", test);
163164191Smaxim
164164191Smaxim	++test;
165164191Smaxim	if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
166164191Smaxim		err(1, "getrlimit");
167176957Santoine	if ((fd2 = dup2(fd1, rlp.rlim_cur + 1)) >= 0)
168164191Smaxim		printf("not ok %d - dup2(2) bypassed NOFILE limit\n", test);
169164191Smaxim	else
170164191Smaxim		printf("ok %d - dup2(2) didn't bypass NOFILE limit\n", test);
171164191Smaxim
172176957Santoine	/* If fcntl(F_DUP2FD) ever work? */
173176957Santoine	if ((fd2 = fcntl(fd1, F_DUP2FD, fd1 + 1)) < 0)
174176957Santoine		err(1, "fcntl(F_DUP2FD)");
175176957Santoine	printf("ok %d - fcntl(F_DUP2FD) works\n", ++test);
176176957Santoine
177176957Santoine	/* Do we get the right fd? */
178176957Santoine	++test;
179176957Santoine	if (fd2 != fd1 + 1)
180176957Santoine		printf(
181176957Santoine		    "no ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n",
182176957Santoine		    test);
183176957Santoine	else
184176957Santoine		printf("ok %d - fcntl(F_DUP2FD) returned a correct fd\n",
185176957Santoine		    test);
186176957Santoine
187176957Santoine	/* Was close-on-exec cleared? */
188176957Santoine	++test;
189176957Santoine	if (fcntl(fd2, F_GETFD) != 0)
190176957Santoine		printf(
191176957Santoine		    "not ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n",
192176957Santoine		    test);
193176957Santoine	else
194176957Santoine		printf("ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n",
195176957Santoine		    test);
196176957Santoine
197176957Santoine	/* Dup to itself */
198176957Santoine	if ((fd2 = fcntl(fd1, F_DUP2FD, fd1)) < 0)
199176957Santoine		err(1, "fcntl(F_DUP2FD)");
200176957Santoine	printf("ok %d - fcntl(F_DUP2FD) to itself works\n", ++test);
201176957Santoine
202176957Santoine	/* Do we get the right fd? */
203176957Santoine	++test;
204176957Santoine	if (fd2 != fd1)
205176957Santoine		printf(
206176957Santoine		    "not ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n",
207176957Santoine		    test);
208176957Santoine	else
209176957Santoine		printf(
210176957Santoine		    "ok %d - fcntl(F_DUP2FD) to itself returned a correct fd\n",
211176957Santoine		    test);
212176957Santoine
213176957Santoine	/* Was close-on-exec cleared? */
214176957Santoine	++test;
215176957Santoine	if (fcntl(fd2, F_GETFD) == 0)
216176957Santoine		printf("not ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n",
217176957Santoine		    test);
218176957Santoine	else
219176957Santoine		printf("ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n",
220176957Santoine		    test);
221176957Santoine
222176957Santoine	++test;
223176957Santoine	if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
224176957Santoine		err(1, "getrlimit");
225176957Santoine	if ((fd2 = fcntl(fd1, F_DUP2FD, rlp.rlim_cur + 1)) >= 0)
226176957Santoine		printf("not ok %d - fcntl(F_DUP2FD) bypassed NOFILE limit\n",
227176957Santoine		    test);
228176957Santoine	else
229176957Santoine		printf("ok %d - fcntl(F_DUP2FD) didn't bypass NOFILE limit\n",
230176957Santoine		    test);
231176957Santoine
232164191Smaxim	return (0);
233164191Smaxim}
234