]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/file/dup_test.c
Fix Coverity warnings about mkstemp in tests
[FreeBSD/FreeBSD.git] / tests / sys / file / dup_test.c
1 /*
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 $
5  *
6  * Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain.
7  *
8  * $FreeBSD$
9  */
10
11 /*
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
22  *           limit work.
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
29  *           duped fd.
30  * Test #17: check if fcntl(F_DUP2FD) to a fd > current maximum number of open
31  *           files limit work.
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
34  *           fd.
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
38  *           fd.
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.
51  */
52
53 #include <sys/stat.h>
54 #include <sys/types.h>
55 #include <sys/time.h>
56 #include <sys/resource.h>
57
58 #include <err.h>
59 #include <fcntl.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <unistd.h>
63
64 static int      getafile(void);
65
66 static int
67 getafile(void)
68 {
69         int fd;
70
71         char temp[] = "/tmp/dup2XXXXXXXXX";
72         umask(0077);
73         if ((fd = mkstemp(temp)) < 0)
74                 err(1, "mkstemp");
75         remove(temp);
76         if (ftruncate(fd, 1024) != 0)
77                 err(1, "ftruncate");
78         return (fd);
79 }
80
81 int
82 main(int __unused argc, char __unused *argv[])
83 {
84         struct rlimit rlp;
85         int orgfd, fd1, fd2, test = 0;
86
87         orgfd = getafile();
88
89         printf("1..32\n");
90
91         /* If dup(2) ever work? */
92         if ((fd1 = dup(orgfd)) < 0)
93                 err(1, "dup");
94         printf("ok %d - dup(2) works\n", ++test);
95
96         /* Set close-on-exec */
97         if (fcntl(fd1, F_SETFD, 1) != 0)
98                 err(1, "fcntl(F_SETFD)");
99
100         /* If dup2(2) ever work? */
101         if ((fd2 = dup2(fd1, fd1 + 1)) < 0)
102                 err(1, "dup2");
103         printf("ok %d - dup2(2) works\n", ++test);
104
105         /* Do we get the right fd? */
106         ++test;
107         if (fd2 != fd1 + 1)
108                 printf("no ok %d - dup2(2) didn't give us the right fd\n",
109                     test);
110         else
111                 printf("ok %d - dup2(2) returned a correct fd\n", test);
112
113         /* Was close-on-exec cleared? */
114         ++test;
115         if (fcntl(fd2, F_GETFD) != 0)
116                 printf("not ok %d - dup2(2) didn't clear close-on-exec\n",
117                     test);
118         else
119                 printf("ok %d - dup2(2) cleared close-on-exec\n", test);
120
121         /*
122          * Dup to itself.
123          *
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'.
138          *
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
141          * works that way.
142          *
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.
146          */
147         if ((fd2 = dup2(fd1, fd1)) < 0)
148                 err(1, "dup2");
149         printf("ok %d - dup2(2) to itself works\n", ++test);
150
151         /* Do we get the right fd? */
152         ++test;
153         if (fd2 != fd1)
154                 printf("not ok %d - dup2(2) didn't give us the right fd\n",
155                     test);
156         else
157                 printf("ok %d - dup2(2) to itself returned a correct fd\n",
158                     test);
159
160         /* Was close-on-exec cleared? */
161         ++test;
162         if (fcntl(fd2, F_GETFD) == 0)
163                 printf("not ok %d - dup2(2) cleared close-on-exec\n", test);
164         else
165                 printf("ok %d - dup2(2) didn't clear close-on-exec\n", test);
166
167         /* Does fcntl(F_DUPFD) work? */
168         if ((fd2 = fcntl(fd1, F_DUPFD, 10)) < 0)
169                 err(1, "fcntl(F_DUPFD)");
170         if (fd2 < 10)
171                 printf("not ok %d - fcntl(F_DUPFD) returned wrong fd %d\n",
172                     ++test, fd2);
173         else
174                 printf("ok %d - fcntl(F_DUPFD) works\n", ++test);
175
176         /* Was close-on-exec cleared? */
177         ++test;
178         if (fcntl(fd2, F_GETFD) != 0)
179                 printf(
180                     "not ok %d - fcntl(F_DUPFD) didn't clear close-on-exec\n",
181                     test);
182         else
183                 printf("ok %d - fcntl(F_DUPFD) cleared close-on-exec\n", test);
184
185         ++test;
186         if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
187                 err(1, "getrlimit");
188         if ((fd2 = dup2(fd1, rlp.rlim_cur + 1)) >= 0)
189                 printf("not ok %d - dup2(2) bypassed NOFILE limit\n", test);
190         else
191                 printf("ok %d - dup2(2) didn't bypass NOFILE limit\n", test);
192
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);
197
198         /* Do we get the right fd? */
199         ++test;
200         if (fd2 != fd1 + 1)
201                 printf(
202                     "no ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n",
203                     test);
204         else
205                 printf("ok %d - fcntl(F_DUP2FD) returned a correct fd\n",
206                     test);
207
208         /* Was close-on-exec cleared? */
209         ++test;
210         if (fcntl(fd2, F_GETFD) != 0)
211                 printf(
212                     "not ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n",
213                     test);
214         else
215                 printf("ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n",
216                     test);
217
218         /* Dup to itself */
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);
222
223         /* Do we get the right fd? */
224         ++test;
225         if (fd2 != fd1)
226                 printf(
227                     "not ok %d - fcntl(F_DUP2FD) didn't give us the right fd\n",
228                     test);
229         else
230                 printf(
231                     "ok %d - fcntl(F_DUP2FD) to itself returned a correct fd\n",
232                     test);
233
234         /* Was close-on-exec cleared? */
235         ++test;
236         if (fcntl(fd2, F_GETFD) == 0)
237                 printf("not ok %d - fcntl(F_DUP2FD) cleared close-on-exec\n",
238                     test);
239         else
240                 printf("ok %d - fcntl(F_DUP2FD) didn't clear close-on-exec\n",
241                     test);
242
243         ++test;
244         if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
245                 err(1, "getrlimit");
246         if ((fd2 = fcntl(fd1, F_DUP2FD, rlp.rlim_cur + 1)) >= 0)
247                 printf("not ok %d - fcntl(F_DUP2FD) bypassed NOFILE limit\n",
248                     test);
249         else
250                 printf("ok %d - fcntl(F_DUP2FD) didn't bypass NOFILE limit\n",
251                     test);
252
253         /* Does fcntl(F_DUPFD_CLOEXEC) work? */
254         if ((fd2 = fcntl(fd1, F_DUPFD_CLOEXEC, 10)) < 0)
255                 err(1, "fcntl(F_DUPFD_CLOEXEC)");
256         if (fd2 < 10)
257                 printf("not ok %d - fcntl(F_DUPFD_CLOEXEC) returned wrong fd %d\n",
258                     ++test, fd2);
259         else
260                 printf("ok %d - fcntl(F_DUPFD_CLOEXEC) works\n", ++test);
261
262         /* Was close-on-exec cleared? */
263         ++test;
264         if (fcntl(fd2, F_GETFD) != 1)
265                 printf(
266                     "not ok %d - fcntl(F_DUPFD_CLOEXEC) didn't set close-on-exec\n",
267                     test);
268         else
269                 printf("ok %d - fcntl(F_DUPFD_CLOEXEC) set close-on-exec\n",
270                     test);
271
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);
276
277         /* Do we get the right fd? */
278         ++test;
279         if (fd2 != fd1 + 1)
280                 printf(
281                     "no ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't give us the right fd\n",
282                     test);
283         else
284                 printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) returned a correct fd\n",
285                     test);
286
287         /* Was close-on-exec set? */
288         ++test;
289         if (fcntl(fd2, F_GETFD) != FD_CLOEXEC)
290                 printf(
291                     "not ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't set close-on-exec\n",
292                     test);
293         else
294                 printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) set close-on-exec\n",
295                     test);
296
297         /*
298          * It is unclear what F_DUP2FD_CLOEXEC should do when duplicating a
299          * file descriptor onto itself.
300          */
301
302         ++test;
303         if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
304                 err(1, "getrlimit");
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",
307                     test);
308         else
309                 printf("ok %d - fcntl(F_DUP2FD_CLOEXEC) didn't bypass NOFILE limit\n",
310                     test);
311
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);
316
317         /* Do we get the right fd? */
318         ++test;
319         if (fd2 != fd1 + 1)
320                 printf(
321                     "no ok %d - dup3(O_CLOEXEC) didn't give us the right fd\n",
322                     test);
323         else
324                 printf("ok %d - dup3(O_CLOEXEC) returned a correct fd\n",
325                     test);
326
327         /* Was close-on-exec set? */
328         ++test;
329         if (fcntl(fd2, F_GETFD) != FD_CLOEXEC)
330                 printf(
331                     "not ok %d - dup3(O_CLOEXEC) didn't set close-on-exec\n",
332                     test);
333         else
334                 printf("ok %d - dup3(O_CLOEXEC) set close-on-exec\n",
335                     test);
336
337         /* Does dup3(0) ever work? */
338         if ((fd2 = dup3(fd1, fd1 + 1, 0)) < 0)
339                 err(1, "dup3(0)");
340         printf("ok %d - dup3(0) works\n", ++test);
341
342         /* Do we get the right fd? */
343         ++test;
344         if (fd2 != fd1 + 1)
345                 printf(
346                     "no ok %d - dup3(0) didn't give us the right fd\n",
347                     test);
348         else
349                 printf("ok %d - dup3(0) returned a correct fd\n",
350                     test);
351
352         /* Was close-on-exec cleared? */
353         ++test;
354         if (fcntl(fd2, F_GETFD) != 0)
355                 printf(
356                     "not ok %d - dup3(0) didn't clear close-on-exec\n",
357                     test);
358         else
359                 printf("ok %d - dup3(0) cleared close-on-exec\n",
360                     test);
361
362         /* dup3() does not allow duplicating to the same fd */
363         ++test;
364         if (dup3(fd1, fd1, O_CLOEXEC) != -1)
365                 printf(
366                     "not ok %d - dup3(fd1, fd1, O_CLOEXEC) succeeded\n", test);
367         else
368                 printf("ok %d - dup3(fd1, fd1, O_CLOEXEC) failed\n", test);
369
370         ++test;
371         if (dup3(fd1, fd1, 0) != -1)
372                 printf(
373                     "not ok %d - dup3(fd1, fd1, 0) succeeded\n", test);
374         else
375                 printf("ok %d - dup3(fd1, fd1, 0) failed\n", test);
376
377         ++test;
378         if (getrlimit(RLIMIT_NOFILE, &rlp) < 0)
379                 err(1, "getrlimit");
380         if ((fd2 = dup3(fd1, rlp.rlim_cur + 1, O_CLOEXEC)) >= 0)
381                 printf("not ok %d - dup3(O_CLOEXEC) bypassed NOFILE limit\n",
382                     test);
383         else
384                 printf("ok %d - dup3(O_CLOEXEC) didn't bypass NOFILE limit\n",
385                     test);
386
387         return (0);
388 }