Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / unix / opendir.c
blobe093142f62654575ba79085209f9d1cbaf150dd0
1 /* Copyright (C) 1991-1996,98,2000-2003,2005,2007,2009,2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <dirent.h>
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include <dirstream.h>
33 #include <not-cancel.h>
34 #include <kernel-features.h>
37 /* opendir() must not accidentally open something other than a directory.
38 Some OS's have kernel support for that, some don't. In the worst
39 case we have to stat() before the open() AND fstat() after.
41 We have to test at runtime for kernel support since libc may have
42 been compiled with different headers to the kernel it's running on.
43 This test can't be done reliably in the general case. We'll use
44 /dev/null, which if it's not a device lots of stuff will break, as
45 a guinea pig. It may be missing in chroot environments, so we
46 make sure to fail safe. */
47 #ifdef O_DIRECTORY
48 # ifdef O_DIRECTORY_WORKS
49 # define o_directory_works 1
50 # define tryopen_o_directory() while (1) /* This must not be called. */
51 # else
52 static int o_directory_works;
54 static void
55 tryopen_o_directory (void)
57 int serrno = errno;
58 int x = open_not_cancel_2 ("/dev/null", O_RDONLY|O_NDELAY|O_DIRECTORY);
60 if (x >= 0)
62 close_not_cancel_no_status (x);
63 o_directory_works = -1;
65 else if (errno != ENOTDIR)
66 o_directory_works = -1;
67 else
68 o_directory_works = 1;
70 __set_errno (serrno);
72 # endif
73 # define EXTRA_FLAGS O_DIRECTORY
74 #else
75 # define EXTRA_FLAGS 0
76 #endif
79 DIR *
80 internal_function
81 __opendirat (int dfd, const char *name)
83 struct stat64 statbuf;
84 struct stat64 *statp = NULL;
86 if (__builtin_expect (name[0], '\1') == '\0')
88 /* POSIX.1-1990 says an empty name gets ENOENT;
89 but `open' might like it fine. */
90 __set_errno (ENOENT);
91 return NULL;
94 #ifdef O_DIRECTORY
95 /* Test whether O_DIRECTORY works. */
96 if (o_directory_works == 0)
97 tryopen_o_directory ();
99 /* We can skip the expensive `stat' call if O_DIRECTORY works. */
100 if (o_directory_works < 0)
101 #endif
103 /* We first have to check whether the name is for a directory. We
104 cannot do this after the open() call since the open/close operation
105 performed on, say, a tape device might have undesirable effects. */
106 if (__builtin_expect (__xstat64 (_STAT_VER, name, &statbuf), 0) < 0)
107 return NULL;
108 if (__builtin_expect (! S_ISDIR (statbuf.st_mode), 0))
110 __set_errno (ENOTDIR);
111 return NULL;
115 int flags = O_RDONLY|O_NDELAY|EXTRA_FLAGS|O_LARGEFILE;
116 #ifdef O_CLOEXEC
117 flags |= O_CLOEXEC;
118 #endif
119 int fd;
120 #ifdef IS_IN_rtld
121 assert (dfd == AT_FDCWD);
122 fd = open_not_cancel_2 (name, flags);
123 #else
124 fd = openat_not_cancel_3 (dfd, name, flags);
125 #endif
126 if (__builtin_expect (fd, 0) < 0)
127 return NULL;
129 #ifdef O_DIRECTORY
130 if (o_directory_works <= 0)
131 #endif
133 /* Now make sure this really is a directory and nothing changed since
134 the `stat' call. */
135 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &statbuf), 0) < 0)
136 goto lose;
137 if (__builtin_expect (! S_ISDIR (statbuf.st_mode), 0))
139 __set_errno (ENOTDIR);
140 lose:
141 close_not_cancel_no_status (fd);
142 return NULL;
144 statp = &statbuf;
147 return __alloc_dir (fd, true, 0, statp);
151 /* Open a directory stream on NAME. */
152 DIR *
153 __opendir (const char *name)
155 return __opendirat (AT_FDCWD, name);
157 weak_alias (__opendir, opendir)
160 #ifdef __ASSUME_O_CLOEXEC
161 # define check_have_o_cloexec(fd) 1
162 #else
163 static int
164 check_have_o_cloexec (int fd)
166 if (__have_o_cloexec == 0)
167 __have_o_cloexec = (__fcntl (fd, F_GETFD, 0) & FD_CLOEXEC) == 0 ? -1 : 1;
168 return __have_o_cloexec > 0;
170 #endif
173 DIR *
174 internal_function
175 __alloc_dir (int fd, bool close_fd, int flags, const struct stat64 *statp)
177 /* We always have to set the close-on-exit flag if the user provided
178 the file descriptor. Otherwise only if we have no working
179 O_CLOEXEC support. */
180 #ifdef O_CLOEXEC
181 if ((! close_fd && (flags & O_CLOEXEC) == 0)
182 || ! check_have_o_cloexec (fd))
183 #endif
185 if (__builtin_expect (__fcntl (fd, F_SETFD, FD_CLOEXEC), 0) < 0)
186 goto lose;
189 const size_t default_allocation = (4 * BUFSIZ < sizeof (struct dirent64)
190 ? sizeof (struct dirent64) : 4 * BUFSIZ);
191 const size_t small_allocation = (BUFSIZ < sizeof (struct dirent64)
192 ? sizeof (struct dirent64) : BUFSIZ);
193 size_t allocation = default_allocation;
194 #ifdef _STATBUF_ST_BLKSIZE
195 if (statp != NULL && default_allocation < statp->st_blksize)
196 allocation = statp->st_blksize;
197 #endif
199 DIR *dirp = (DIR *) malloc (sizeof (DIR) + allocation);
200 if (dirp == NULL)
202 allocation = small_allocation;
203 dirp = (DIR *) malloc (sizeof (DIR) + allocation);
205 if (dirp == NULL)
206 lose:
208 if (close_fd)
210 int save_errno = errno;
211 close_not_cancel_no_status (fd);
212 __set_errno (save_errno);
214 return NULL;
218 dirp->fd = fd;
219 #ifndef NOT_IN_libc
220 __libc_lock_init (dirp->lock);
221 #endif
222 dirp->allocation = allocation;
223 dirp->size = 0;
224 dirp->offset = 0;
225 dirp->filepos = 0;
227 return dirp;