1 /* $NetBSD: opendir.c,v 1.37 2010/09/26 02:26:59 yamt 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.37 2010/09/26 02:26:59 yamt 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 #endif 48 49 #include "extern.h" 50 51 #include <sys/param.h> 52 #include <sys/mount.h> 53 #include <sys/stat.h> 54 55 #include <assert.h> 56 #include <dirent.h> 57 #include <errno.h> 58 #include <fcntl.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <unistd.h> 62 63 #include "dirent_private.h" 64 65 static DIR *__opendir_common(int, const char *, int); 66 67 __weak_alias(fdopendir,_fdopendir) 68 69 /* 70 * Open a directory. 71 */ 72 DIR * 73 opendir(const char *name) 74 { 75 76 _DIAGASSERT(name != NULL); 77 78 return (__opendir2(name, DTF_HIDEW|DTF_NODUP)); 79 } 80 81 DIR * 82 __opendir2(const char *name, int flags) 83 { 84 int fd; 85 86 if ((fd = open(name, O_RDONLY | O_NONBLOCK)) == -1) 87 return NULL; 88 return __opendir_common(fd, name, flags); 89 } 90 91 #ifndef __LIBC12_SOURCE__ 92 DIR * 93 _fdopendir(int fd) 94 { 95 96 return __opendir_common(fd, NULL, DTF_HIDEW|DTF_NODUP); 97 } 98 #endif 99 100 static DIR * 101 __opendir_common(int fd, const char *name, int flags) 102 { 103 DIR *dirp = NULL; 104 int serrno; 105 struct stat sb; 106 #ifndef __minix 107 struct statvfs sfb; 108 #endif 109 int error; 110 111 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) 112 goto error; 113 if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) { 114 errno = ENOTDIR; 115 goto error; 116 } 117 if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL) 118 goto error; 119 dirp->dd_buf = NULL; 120 dirp->dd_internal = NULL; 121 #ifdef _REENTRANT 122 if (__isthreaded) { 123 if ((dirp->dd_lock = malloc(sizeof(mutex_t))) == NULL) 124 goto error; 125 mutex_init((mutex_t *)dirp->dd_lock, NULL); 126 } 127 #endif 128 129 /* 130 * Tweak flags for the underlying filesystem. 131 */ 132 133 #ifdef __minix 134 /* MOUNT_UNION and MOUNT_NFS not supported */ 135 flags &= ~DTF_NODUP; 136 #else 137 if (fstatvfs1(fd, &sfb, ST_NOWAIT) < 0) 138 goto error; 139 if ((flags & DTF_NODUP) != 0) { 140 if (!strncmp(sfb.f_fstypename, MOUNT_UNION, 141 sizeof(sfb.f_fstypename)) || 142 (sfb.f_flag & MNT_UNION) != 0) { 143 flags |= __DTF_READALL; 144 } else { 145 flags &= ~DTF_NODUP; 146 } 147 } 148 if (!strncmp(sfb.f_fstypename, MOUNT_NFS, sizeof(sfb.f_fstypename))) { 149 flags |= __DTF_READALL | __DTF_RETRY_ON_BADCOOKIE; 150 } 151 #endif 152 153 dirp->dd_flags = flags; 154 error = _initdir(dirp, fd, name); 155 if (error) { 156 errno = error; 157 goto error; 158 } 159 160 return (dirp); 161 error: 162 serrno = errno; 163 if (dirp != NULL) { 164 #ifdef _REENTRANT 165 if (__isthreaded) { 166 mutex_destroy((mutex_t *)dirp->dd_lock); 167 free(dirp->dd_lock); 168 } 169 #endif 170 free(dirp->dd_buf); 171 } 172 free(dirp); 173 if (fd != -1) 174 (void)close(fd); 175 errno = serrno; 176 return NULL; 177 } 178