Tue Apr 14 05:21:26 2015 UTC ()
Pull up following revision(s) (requested by christos in ticket #691):
	lib/libc/gen/opendir.c: revision 1.39
- Use O_DIRECTORY to open the file, so that we don't need to stat() after
  that.
- Move the stat() call to fdopendir() and change it's error handling so that
  it does not hide errors.
- According to POSIX, fdopendir() transfers ownership of the fd only on
  success, so don't close it on failure. XXX: We still make it non-blocking
  on failure, but that's nitpicking.


(snj)
diff -r1.38 -r1.38.20.1 src/lib/libc/gen/opendir.c

cvs diff -r1.38 -r1.38.20.1 src/lib/libc/gen/opendir.c (expand / switch to unified diff)

--- src/lib/libc/gen/opendir.c 2011/10/15 23:00:01 1.38
+++ src/lib/libc/gen/opendir.c 2015/04/14 05:21:26 1.38.20.1
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: opendir.c,v 1.38 2011/10/15 23:00:01 christos Exp $ */ 1/* $NetBSD: opendir.c,v 1.38.20.1 2015/04/14 05:21:26 snj Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1983, 1993 4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -24,27 +24,27 @@ @@ -24,27 +24,27 @@
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 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 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 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 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE. 29 * SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33#if defined(LIBC_SCCS) && !defined(lint) 33#if defined(LIBC_SCCS) && !defined(lint)
34#if 0 34#if 0
35static char sccsid[] = "@(#)opendir.c 8.7 (Berkeley) 12/10/94"; 35static char sccsid[] = "@(#)opendir.c 8.7 (Berkeley) 12/10/94";
36#else 36#else
37__RCSID("$NetBSD: opendir.c,v 1.38 2011/10/15 23:00:01 christos Exp $"); 37__RCSID("$NetBSD: opendir.c,v 1.38.20.1 2015/04/14 05:21:26 snj Exp $");
38#endif 38#endif
39#endif /* LIBC_SCCS and not lint */ 39#endif /* LIBC_SCCS and not lint */
40 40
41#include "namespace.h" 41#include "namespace.h"
42#include "reentrant.h" 42#include "reentrant.h"
43#include "extern.h" 43#include "extern.h"
44 44
45#include <sys/param.h> 45#include <sys/param.h>
46#include <sys/mount.h> 46#include <sys/mount.h>
47#include <sys/stat.h> 47#include <sys/stat.h>
48 48
49#include <assert.h> 49#include <assert.h>
50#include <dirent.h> 50#include <dirent.h>
@@ -65,57 +65,71 @@ __weak_alias(fdopendir,_fdopendir) @@ -65,57 +65,71 @@ __weak_alias(fdopendir,_fdopendir)
65 */ 65 */
66DIR * 66DIR *
67opendir(const char *name) 67opendir(const char *name)
68{ 68{
69 69
70 _DIAGASSERT(name != NULL); 70 _DIAGASSERT(name != NULL);
71 71
72 return (__opendir2(name, DTF_HIDEW|DTF_NODUP)); 72 return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
73} 73}
74 74
75DIR * 75DIR *
76__opendir2(const char *name, int flags) 76__opendir2(const char *name, int flags)
77{ 77{
 78 DIR *dirp;
78 int fd; 79 int fd;
79 80
80 if ((fd = open(name, O_RDONLY | O_NONBLOCK | O_CLOEXEC)) == -1) 81 if ((fd = open(name, O_RDONLY|O_DIRECTORY|O_NONBLOCK|O_CLOEXEC)) == -1)
81 return NULL; 82 return NULL;
82 return __opendir_common(fd, name, flags); 83
 84 dirp = __opendir_common(fd, name, flags);
 85 if (dirp == NULL) {
 86 int serrno = errno;
 87 (void)close(fd);
 88 errno = serrno;
 89 }
 90 return dirp;
83} 91}
84 92
85#ifndef __LIBC12_SOURCE__ 93#ifndef __LIBC12_SOURCE__
86DIR * 94DIR *
87_fdopendir(int fd) 95_fdopendir(int fd)
88{ 96{
 97 struct stat sb;
 98
 99 if (fstat(fd, &sb) == -1)
 100 return NULL;
 101
 102 if (!S_ISDIR(sb.st_mode)) {
 103 errno = ENOTDIR;
 104 return NULL;
 105 }
 106
 107 /* This is optional according to POSIX, but a good measure */
89 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) 108 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
90 return NULL; 109 return NULL;
91 110
92 return __opendir_common(fd, NULL, DTF_HIDEW|DTF_NODUP); 111 return __opendir_common(fd, NULL, DTF_HIDEW|DTF_NODUP);
93} 112}
94#endif 113#endif
95 114
96static DIR * 115static DIR *
97__opendir_common(int fd, const char *name, int flags) 116__opendir_common(int fd, const char *name, int flags)
98{ 117{
99 DIR *dirp = NULL; 118 DIR *dirp;
100 int serrno; 119 int serrno;
101 struct stat sb; 
102 struct statvfs sfb; 120 struct statvfs sfb;
103 int error; 121 int error;
104 122
105 if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) { 
106 errno = ENOTDIR; 
107 goto error; 
108 } 
109 if ((dirp = malloc(sizeof(*dirp))) == NULL) 123 if ((dirp = malloc(sizeof(*dirp))) == NULL)
110 goto error; 124 goto error;
111 dirp->dd_buf = NULL; 125 dirp->dd_buf = NULL;
112 dirp->dd_internal = NULL; 126 dirp->dd_internal = NULL;
113#ifdef _REENTRANT 127#ifdef _REENTRANT
114 if (__isthreaded) { 128 if (__isthreaded) {
115 if ((dirp->dd_lock = malloc(sizeof(mutex_t))) == NULL) 129 if ((dirp->dd_lock = malloc(sizeof(mutex_t))) == NULL)
116 goto error; 130 goto error;
117 mutex_init((mutex_t *)dirp->dd_lock, NULL); 131 mutex_init((mutex_t *)dirp->dd_lock, NULL);
118 } 132 }
119#endif 133#endif
120 134
121 /* 135 /*
@@ -147,18 +161,16 @@ __opendir_common(int fd, const char *nam @@ -147,18 +161,16 @@ __opendir_common(int fd, const char *nam
147 return (dirp); 161 return (dirp);
148error: 162error:
149 serrno = errno; 163 serrno = errno;
150 if (dirp != NULL) { 164 if (dirp != NULL) {
151#ifdef _REENTRANT 165#ifdef _REENTRANT
152 if (__isthreaded) { 166 if (__isthreaded) {
153 mutex_destroy((mutex_t *)dirp->dd_lock); 167 mutex_destroy((mutex_t *)dirp->dd_lock);
154 free(dirp->dd_lock); 168 free(dirp->dd_lock);
155 } 169 }
156#endif 170#endif
157 free(dirp->dd_buf); 171 free(dirp->dd_buf);
158 } 172 }
159 free(dirp); 173 free(dirp);
160 if (fd != -1) 
161 (void)close(fd); 
162 errno = serrno; 174 errno = serrno;
163 return NULL; 175 return NULL;
164} 176}