11936
|
1 /* Test that directory streams leave standard fds alone. |
|
2 Copyright (C) 2009 Free Software Foundation, Inc. |
|
3 |
|
4 This program is free software: you can redistribute it and/or modify |
|
5 it under the terms of the GNU General Public License as published by |
|
6 the Free Software Foundation; either version 3 of the License, or |
|
7 (at your option) any later version. |
|
8 |
|
9 This program is distributed in the hope that it will be useful, |
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 GNU General Public License for more details. |
|
13 |
|
14 You should have received a copy of the GNU General Public License |
|
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
|
16 |
|
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */ |
|
18 |
|
19 #include <config.h> |
|
20 |
|
21 #include "dirent--.h" |
|
22 |
|
23 #include <errno.h> |
|
24 #include <fcntl.h> |
|
25 #include <stdio.h> |
|
26 #include <stdlib.h> |
|
27 #include <unistd.h> |
|
28 |
|
29 #include "unistd-safer.h" |
|
30 |
|
31 /* This test intentionally closes stderr. So, we arrange to have fd 10 |
|
32 (outside the range of interesting fd's during the test) set up to |
|
33 duplicate the original stderr. */ |
|
34 |
|
35 #define BACKUP_STDERR_FILENO 10 |
|
36 static FILE *myerr; |
|
37 |
|
38 #define ASSERT(expr) \ |
|
39 do \ |
|
40 { \ |
|
41 if (!(expr)) \ |
|
42 { \ |
|
43 fprintf (myerr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ |
|
44 fflush (myerr); \ |
|
45 abort (); \ |
|
46 } \ |
|
47 } \ |
|
48 while (0) |
|
49 |
|
50 int |
|
51 main () |
|
52 { |
|
53 int i; |
|
54 DIR *dp; |
|
55 /* The dirent-safer module works without the use of fdopendir (which |
|
56 would also pull in fchdir and openat); but if those modules were |
|
57 also used, we ensure that they are safe. In particular, the |
|
58 gnulib version of fdopendir is unable to guarantee that |
|
59 dirfd(fdopendir(fd))==fd, but we can at least guarantee that if |
|
60 they are not equal, the fd returned by dirfd is safe. */ |
|
61 #if HAVE_FDOPENDIR || GNULIB_FDOPENDIR |
|
62 int dfd; |
|
63 #endif |
|
64 |
|
65 /* We close fd 2 later, so save it in fd 10. */ |
|
66 if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO |
|
67 || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL) |
|
68 return 2; |
|
69 |
|
70 #if HAVE_FDOPENDIR || GNULIB_FDOPENDIR |
|
71 dfd = open (".", O_RDONLY); |
|
72 ASSERT (STDERR_FILENO < dfd); |
|
73 #endif |
|
74 |
|
75 /* Four iterations, with progressively more standard descriptors |
|
76 closed. */ |
|
77 for (i = -1; i <= STDERR_FILENO; i++) |
|
78 { |
|
79 if (0 <= i) |
|
80 ASSERT (close (i) == 0); |
|
81 dp = opendir ("."); |
|
82 ASSERT (dp); |
|
83 ASSERT (dirfd (dp) == -1 || STDERR_FILENO < dirfd (dp)); |
|
84 ASSERT (closedir (dp) == 0); |
|
85 |
|
86 #if HAVE_FDOPENDIR || GNULIB_FDOPENDIR |
|
87 { |
|
88 int fd = dup_safer (dfd); |
|
89 ASSERT (STDERR_FILENO < fd); |
|
90 dp = fdopendir (fd); |
|
91 ASSERT (dp); |
|
92 ASSERT (dirfd (dp) == -1 || STDERR_FILENO < dirfd (dp)); |
|
93 ASSERT (closedir (dp) == 0); |
|
94 errno = 0; |
|
95 ASSERT (close (fd) == -1); |
|
96 ASSERT (errno == EBADF); |
|
97 } |
|
98 #endif |
|
99 } |
|
100 |
|
101 #if HAVE_FDOPENDIR || GNULIB_FDOPENDIR |
|
102 ASSERT (close (dfd) == 0); |
|
103 #endif |
|
104 |
|
105 return 0; |
|
106 } |