2.5-18.1
[glibc.git] / sysdeps / unix / opendir.c
blob6aa738fb6913a644df02f7ef64a0d098dbf2adc4
1 /* Copyright (C) 1991-1996,98,2000-2003,2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <limits.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <dirent.h>
24 #include <fcntl.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <string.h>
31 #include <dirstream.h>
32 #include <not-cancel.h>
35 /* opendir() must not accidentally open something other than a directory.
36 Some OS's have kernel support for that, some don't. In the worst
37 case we have to stat() before the open() AND fstat() after.
39 We have to test at runtime for kernel support since libc may have
40 been compiled with different headers to the kernel it's running on.
41 This test can't be done reliably in the general case. We'll use
42 /dev/null, which if it's not a device lots of stuff will break, as
43 a guinea pig. It may be missing in chroot environments, so we
44 make sure to fail safe. */
45 #ifdef O_DIRECTORY
46 # ifdef O_DIRECTORY_WORKS
47 # define o_directory_works 1
48 # define tryopen_o_directory() while (1) /* This must not be called. */
49 # else
50 static int o_directory_works;
52 static void
53 tryopen_o_directory (void)
55 int serrno = errno;
56 int x = open_not_cancel_2 ("/dev/null", O_RDONLY|O_NDELAY|O_DIRECTORY);
58 if (x >= 0)
60 close_not_cancel_no_status (x);
61 o_directory_works = -1;
63 else if (errno != ENOTDIR)
64 o_directory_works = -1;
65 else
66 o_directory_works = 1;
68 __set_errno (serrno);
70 # endif
71 # define EXTRA_FLAGS O_DIRECTORY
72 #else
73 # define EXTRA_FLAGS 0
74 #endif
77 /* Open a directory stream on NAME. */
78 DIR *
79 __opendir (const char *name)
81 struct stat64 statbuf;
83 if (__builtin_expect (name[0], '\1') == '\0')
85 /* POSIX.1-1990 says an empty name gets ENOENT;
86 but `open' might like it fine. */
87 __set_errno (ENOENT);
88 return NULL;
91 #ifdef O_DIRECTORY
92 /* Test whether O_DIRECTORY works. */
93 if (o_directory_works == 0)
94 tryopen_o_directory ();
96 /* We can skip the expensive `stat' call if O_DIRECTORY works. */
97 if (o_directory_works < 0)
98 #endif
100 /* We first have to check whether the name is for a directory. We
101 cannot do this after the open() call since the open/close operation
102 performed on, say, a tape device might have undesirable effects. */
103 if (__builtin_expect (__xstat64 (_STAT_VER, name, &statbuf), 0) < 0)
104 return NULL;
105 if (__builtin_expect (! S_ISDIR (statbuf.st_mode), 0))
107 __set_errno (ENOTDIR);
108 return NULL;
112 int fd = open_not_cancel_2 (name, O_RDONLY|O_NDELAY|EXTRA_FLAGS|O_LARGEFILE);
113 if (__builtin_expect (fd, 0) < 0)
114 return NULL;
116 /* Now make sure this really is a directory and nothing changed since
117 the `stat' call. We do not have to perform the test for the
118 descriptor being associated with a directory if we know the
119 O_DIRECTORY flag is honored by the kernel. */
120 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &statbuf), 0) < 0)
121 goto lose;
122 #ifdef O_DIRECTORY
123 if (o_directory_works <= 0)
124 #endif
126 if (__builtin_expect (! S_ISDIR (statbuf.st_mode), 0))
128 __set_errno (ENOTDIR);
129 lose:
130 close_not_cancel_no_status (fd);
131 return NULL;
135 return __alloc_dir (fd, true, &statbuf);
137 weak_alias (__opendir, opendir)
140 DIR *
141 internal_function
142 __alloc_dir (int fd, bool close_fd, const struct stat64 *statp)
144 if (__builtin_expect (__fcntl (fd, F_SETFD, FD_CLOEXEC), 0) < 0)
145 goto lose;
147 size_t allocation;
148 #ifdef _STATBUF_ST_BLKSIZE
149 if (__builtin_expect ((size_t) statp->st_blksize >= sizeof (struct dirent64),
151 allocation = statp->st_blksize;
152 else
153 #endif
154 allocation = (BUFSIZ < sizeof (struct dirent64)
155 ? sizeof (struct dirent64) : BUFSIZ);
157 const int pad = -sizeof (DIR) % __alignof__ (struct dirent64);
159 DIR *dirp = (DIR *) malloc (sizeof (DIR) + allocation + pad);
160 if (dirp == NULL)
161 lose:
163 if (close_fd)
165 int save_errno = errno;
166 close_not_cancel_no_status (fd);
167 __set_errno (save_errno);
169 return NULL;
171 memset (dirp, '\0', sizeof (DIR));
172 dirp->data = (char *) (dirp + 1) + pad;
173 dirp->allocation = allocation;
174 dirp->fd = fd;
176 __libc_lock_init (dirp->lock);
178 return dirp;