1*0a6a1f1dSLionel Sambuc /* $NetBSD: opendir.c,v 1.39 2014/11/26 16:48:43 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 1983, 1993
52fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras * are met:
102fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras * without specific prior written permission.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras * SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras #include <sys/cdefs.h>
332fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
342fe8fb19SBen Gras #if 0
352fe8fb19SBen Gras static char sccsid[] = "@(#)opendir.c 8.7 (Berkeley) 12/10/94";
362fe8fb19SBen Gras #else
37*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: opendir.c,v 1.39 2014/11/26 16:48:43 christos Exp $");
382fe8fb19SBen Gras #endif
392fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
402fe8fb19SBen Gras
412fe8fb19SBen Gras #include "namespace.h"
422fe8fb19SBen Gras #include "reentrant.h"
432fe8fb19SBen Gras #include "extern.h"
442fe8fb19SBen Gras
452fe8fb19SBen Gras #include <sys/param.h>
462fe8fb19SBen Gras #include <sys/mount.h>
472fe8fb19SBen Gras #include <sys/stat.h>
482fe8fb19SBen Gras
492fe8fb19SBen Gras #include <assert.h>
502fe8fb19SBen Gras #include <dirent.h>
512fe8fb19SBen Gras #include <errno.h>
522fe8fb19SBen Gras #include <fcntl.h>
532fe8fb19SBen Gras #include <stdlib.h>
542fe8fb19SBen Gras #include <string.h>
552fe8fb19SBen Gras #include <unistd.h>
562fe8fb19SBen Gras
572fe8fb19SBen Gras #include "dirent_private.h"
582fe8fb19SBen Gras
592fe8fb19SBen Gras static DIR *__opendir_common(int, const char *, int);
602fe8fb19SBen Gras
__weak_alias(fdopendir,_fdopendir)612fe8fb19SBen Gras __weak_alias(fdopendir,_fdopendir)
622fe8fb19SBen Gras
632fe8fb19SBen Gras /*
642fe8fb19SBen Gras * Open a directory.
652fe8fb19SBen Gras */
662fe8fb19SBen Gras DIR *
672fe8fb19SBen Gras opendir(const char *name)
682fe8fb19SBen Gras {
692fe8fb19SBen Gras
702fe8fb19SBen Gras _DIAGASSERT(name != NULL);
712fe8fb19SBen Gras
722fe8fb19SBen Gras return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
732fe8fb19SBen Gras }
742fe8fb19SBen Gras
752fe8fb19SBen Gras DIR *
__opendir2(const char * name,int flags)762fe8fb19SBen Gras __opendir2(const char *name, int flags)
772fe8fb19SBen Gras {
78*0a6a1f1dSLionel Sambuc DIR *dirp;
792fe8fb19SBen Gras int fd;
802fe8fb19SBen Gras
81*0a6a1f1dSLionel Sambuc if ((fd = open(name, O_RDONLY|O_DIRECTORY|O_NONBLOCK|O_CLOEXEC)) == -1)
822fe8fb19SBen Gras return NULL;
83*0a6a1f1dSLionel Sambuc
84*0a6a1f1dSLionel Sambuc dirp = __opendir_common(fd, name, flags);
85*0a6a1f1dSLionel Sambuc if (dirp == NULL) {
86*0a6a1f1dSLionel Sambuc int serrno = errno;
87*0a6a1f1dSLionel Sambuc (void)close(fd);
88*0a6a1f1dSLionel Sambuc errno = serrno;
89*0a6a1f1dSLionel Sambuc }
90*0a6a1f1dSLionel Sambuc return dirp;
912fe8fb19SBen Gras }
922fe8fb19SBen Gras
932fe8fb19SBen Gras #ifndef __LIBC12_SOURCE__
942fe8fb19SBen Gras DIR *
_fdopendir(int fd)952fe8fb19SBen Gras _fdopendir(int fd)
962fe8fb19SBen Gras {
97*0a6a1f1dSLionel Sambuc struct stat sb;
98*0a6a1f1dSLionel Sambuc
99*0a6a1f1dSLionel Sambuc if (fstat(fd, &sb) == -1)
100*0a6a1f1dSLionel Sambuc return NULL;
101*0a6a1f1dSLionel Sambuc
102*0a6a1f1dSLionel Sambuc if (!S_ISDIR(sb.st_mode)) {
103*0a6a1f1dSLionel Sambuc errno = ENOTDIR;
104*0a6a1f1dSLionel Sambuc return NULL;
105*0a6a1f1dSLionel Sambuc }
106*0a6a1f1dSLionel Sambuc
107*0a6a1f1dSLionel Sambuc /* This is optional according to POSIX, but a good measure */
108f14fb602SLionel Sambuc if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
109f14fb602SLionel Sambuc return NULL;
1102fe8fb19SBen Gras
1112fe8fb19SBen Gras return __opendir_common(fd, NULL, DTF_HIDEW|DTF_NODUP);
1122fe8fb19SBen Gras }
1132fe8fb19SBen Gras #endif
1142fe8fb19SBen Gras
1152fe8fb19SBen Gras static DIR *
__opendir_common(int fd,const char * name,int flags)1162fe8fb19SBen Gras __opendir_common(int fd, const char *name, int flags)
1172fe8fb19SBen Gras {
118*0a6a1f1dSLionel Sambuc DIR *dirp;
1192fe8fb19SBen Gras int serrno;
12084d9c625SLionel Sambuc #if !defined(__minix)
1212fe8fb19SBen Gras struct statvfs sfb;
122*0a6a1f1dSLionel Sambuc #else
123*0a6a1f1dSLionel Sambuc struct stat sb;
12484d9c625SLionel Sambuc #endif /* !defined(__minix) */
1252fe8fb19SBen Gras int error;
1262fe8fb19SBen Gras
127*0a6a1f1dSLionel Sambuc #if defined(__minix)
128*0a6a1f1dSLionel Sambuc dirp = NULL; /* LSC: Make sure the pointer is initialized */
1292fe8fb19SBen Gras if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) {
1302fe8fb19SBen Gras errno = ENOTDIR;
1312fe8fb19SBen Gras goto error;
1322fe8fb19SBen Gras }
133*0a6a1f1dSLionel Sambuc #endif /* defined(__minix) */
134*0a6a1f1dSLionel Sambuc
135f14fb602SLionel Sambuc if ((dirp = malloc(sizeof(*dirp))) == NULL)
1362fe8fb19SBen Gras goto error;
1372fe8fb19SBen Gras dirp->dd_buf = NULL;
1382fe8fb19SBen Gras dirp->dd_internal = NULL;
1392fe8fb19SBen Gras #ifdef _REENTRANT
1402fe8fb19SBen Gras if (__isthreaded) {
1412fe8fb19SBen Gras if ((dirp->dd_lock = malloc(sizeof(mutex_t))) == NULL)
1422fe8fb19SBen Gras goto error;
1432fe8fb19SBen Gras mutex_init((mutex_t *)dirp->dd_lock, NULL);
1442fe8fb19SBen Gras }
1452fe8fb19SBen Gras #endif
1462fe8fb19SBen Gras
1472fe8fb19SBen Gras /*
1482fe8fb19SBen Gras * Tweak flags for the underlying filesystem.
1492fe8fb19SBen Gras */
1502fe8fb19SBen Gras
15184d9c625SLionel Sambuc #if defined(__minix)
1522fe8fb19SBen Gras /* MOUNT_UNION and MOUNT_NFS not supported */
1532fe8fb19SBen Gras flags &= ~DTF_NODUP;
1542fe8fb19SBen Gras #else
1552fe8fb19SBen Gras if (fstatvfs1(fd, &sfb, ST_NOWAIT) < 0)
1562fe8fb19SBen Gras goto error;
1572fe8fb19SBen Gras if ((flags & DTF_NODUP) != 0) {
1582fe8fb19SBen Gras if (!strncmp(sfb.f_fstypename, MOUNT_UNION,
1592fe8fb19SBen Gras sizeof(sfb.f_fstypename)) ||
1602fe8fb19SBen Gras (sfb.f_flag & MNT_UNION) != 0) {
1612fe8fb19SBen Gras flags |= __DTF_READALL;
1622fe8fb19SBen Gras } else {
1632fe8fb19SBen Gras flags &= ~DTF_NODUP;
1642fe8fb19SBen Gras }
1652fe8fb19SBen Gras }
1662fe8fb19SBen Gras if (!strncmp(sfb.f_fstypename, MOUNT_NFS, sizeof(sfb.f_fstypename))) {
1672fe8fb19SBen Gras flags |= __DTF_READALL | __DTF_RETRY_ON_BADCOOKIE;
1682fe8fb19SBen Gras }
16984d9c625SLionel Sambuc #endif /* !defined(__minix) */
1702fe8fb19SBen Gras
1712fe8fb19SBen Gras dirp->dd_flags = flags;
1722fe8fb19SBen Gras error = _initdir(dirp, fd, name);
1732fe8fb19SBen Gras if (error) {
1742fe8fb19SBen Gras errno = error;
1752fe8fb19SBen Gras goto error;
1762fe8fb19SBen Gras }
1772fe8fb19SBen Gras
1782fe8fb19SBen Gras return (dirp);
1792fe8fb19SBen Gras error:
1802fe8fb19SBen Gras serrno = errno;
1812fe8fb19SBen Gras if (dirp != NULL) {
1822fe8fb19SBen Gras #ifdef _REENTRANT
1832fe8fb19SBen Gras if (__isthreaded) {
1842fe8fb19SBen Gras mutex_destroy((mutex_t *)dirp->dd_lock);
1852fe8fb19SBen Gras free(dirp->dd_lock);
1862fe8fb19SBen Gras }
1872fe8fb19SBen Gras #endif
1882fe8fb19SBen Gras free(dirp->dd_buf);
1892fe8fb19SBen Gras }
1902fe8fb19SBen Gras free(dirp);
1912fe8fb19SBen Gras errno = serrno;
1922fe8fb19SBen Gras return NULL;
1932fe8fb19SBen Gras }
194