1 /* $NetBSD: opendir.c,v 1.38 2011/10/15 23:00:01 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #if defined(LIBC_SCCS) && !defined(lint) 34 #if 0 35 static char sccsid[] = "@(#)opendir.c 8.7 (Berkeley) 12/10/94"; 36 #else 37 __RCSID("$NetBSD: opendir.c,v 1.38 2011/10/15 23:00:01 christos Exp $"); 38 #endif 39 #endif /* LIBC_SCCS and not lint */ 40 41 #include "namespace.h" 42 #include "reentrant.h" 43 44 #ifdef __minix 45 #include <sys/cdefs.h> 46 #include <sys/types.h> 47 48 #if !defined(O_CLOEXEC) 49 #define O_CLOEXEC 0 50 #endif 51 52 #endif 53 54 #include "extern.h" 55 56 #include <sys/param.h> 57 #include <sys/mount.h> 58 #include <sys/stat.h> 59 60 #include <assert.h> 61 #include <dirent.h> 62 #include <errno.h> 63 #include <fcntl.h> 64 #include <stdlib.h> 65 #include <string.h> 66 #include <unistd.h> 67 68 #include "dirent_private.h" 69 70 static DIR *__opendir_common(int, const char *, int); 71 72 __weak_alias(fdopendir,_fdopendir) 73 74 #if defined(__weak_alias) && defined(__minix) 75 __weak_alias(opendir,__opendir230) 76 #endif 77 78 /* 79 * Open a directory. 80 */ 81 DIR * 82 opendir(const char *name) 83 { 84 85 _DIAGASSERT(name != NULL); 86 87 return (__opendir2(name, DTF_HIDEW|DTF_NODUP)); 88 } 89 90 DIR * 91 __opendir2(const char *name, int flags) 92 { 93 int fd; 94 95 if ((fd = open(name, O_RDONLY | O_NONBLOCK | O_CLOEXEC)) == -1) 96 return NULL; 97 #if defined(__minix) 98 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { 99 close(fd); 100 return NULL; 101 } 102 #endif /* defined(__minix) */ 103 104 return __opendir_common(fd, name, flags); 105 } 106 107 #ifndef __LIBC12_SOURCE__ 108 DIR * 109 _fdopendir(int fd) 110 { 111 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) 112 return NULL; 113 114 return __opendir_common(fd, NULL, DTF_HIDEW|DTF_NODUP); 115 } 116 #endif 117 118 static DIR * 119 __opendir_common(int fd, const char *name, int flags) 120 { 121 DIR *dirp = NULL; 122 int serrno; 123 struct stat sb; 124 #ifndef __minix 125 struct statvfs sfb; 126 #endif 127 int error; 128 129 if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) { 130 errno = ENOTDIR; 131 goto error; 132 } 133 if ((dirp = malloc(sizeof(*dirp))) == NULL) 134 goto error; 135 dirp->dd_buf = NULL; 136 dirp->dd_internal = NULL; 137 #ifdef _REENTRANT 138 if (__isthreaded) { 139 if ((dirp->dd_lock = malloc(sizeof(mutex_t))) == NULL) 140 goto error; 141 mutex_init((mutex_t *)dirp->dd_lock, NULL); 142 } 143 #endif 144 145 /* 146 * Tweak flags for the underlying filesystem. 147 */ 148 149 #ifdef __minix 150 /* MOUNT_UNION and MOUNT_NFS not supported */ 151 flags &= ~DTF_NODUP; 152 #else 153 if (fstatvfs1(fd, &sfb, ST_NOWAIT) < 0) 154 goto error; 155 if ((flags & DTF_NODUP) != 0) { 156 if (!strncmp(sfb.f_fstypename, MOUNT_UNION, 157 sizeof(sfb.f_fstypename)) || 158 (sfb.f_flag & MNT_UNION) != 0) { 159 flags |= __DTF_READALL; 160 } else { 161 flags &= ~DTF_NODUP; 162 } 163 } 164 if (!strncmp(sfb.f_fstypename, MOUNT_NFS, sizeof(sfb.f_fstypename))) { 165 flags |= __DTF_READALL | __DTF_RETRY_ON_BADCOOKIE; 166 } 167 #endif 168 169 dirp->dd_flags = flags; 170 error = _initdir(dirp, fd, name); 171 if (error) { 172 errno = error; 173 goto error; 174 } 175 176 return (dirp); 177 error: 178 serrno = errno; 179 if (dirp != NULL) { 180 #ifdef _REENTRANT 181 if (__isthreaded) { 182 mutex_destroy((mutex_t *)dirp->dd_lock); 183 free(dirp->dd_lock); 184 } 185 #endif 186 free(dirp->dd_buf); 187 } 188 free(dirp); 189 if (fd != -1) 190 (void)close(fd); 191 errno = serrno; 192 return NULL; 193 } 194