xref: /src/lib/libc/gen/opendir2.c (revision 387ae6390534b6e9b48931840e7bc76eeb0b258d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 "namespace.h"
33 #include <sys/types.h>
34 
35 #include <dirent.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <stdbool.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include "un-namespace.h"
43 
44 #include "gen-private.h"
45 #include "telldir.h"
46 
47 DIR *
__opendir2(const char * name,int flags)48 __opendir2(const char *name, int flags)
49 {
50 	int fd;
51 	DIR *dir;
52 	int saved_errno;
53 
54 	if ((flags & (__DTF_READALL | __DTF_SKIPREAD)) != 0)
55 		return (NULL);
56 	if ((fd = _open(name, O_DIRECTORY | O_RDONLY | O_CLOEXEC)) == -1)
57 		return (NULL);
58 
59 	dir = __opendir_common(fd, flags, false);
60 	if (dir == NULL) {
61 		saved_errno = errno;
62 		_close(fd);
63 		errno = saved_errno;
64 	}
65 	return (dir);
66 }
67 
68 static int
opendir_compar(const void * p1,const void * p2)69 opendir_compar(const void *p1, const void *p2)
70 {
71 	return (strcmp((*(const struct dirent * const *)p1)->d_name,
72 	    (*(const struct dirent * const *)p2)->d_name));
73 }
74 
75 /*
76  * For a directory at the top of a unionfs stack, the entire directory's
77  * contents are read and cached locally until the next call to rewinddir().
78  * For the fdopendir() case, the initial seek position must be preserved.
79  * For rewinddir(), the full directory should always be re-read from the
80  * beginning.
81  *
82  * If an error occurs, the existing buffer and state of 'dirp' is left
83  * unchanged.
84  */
85 bool
_filldir(DIR * dirp,bool use_current_pos)86 _filldir(DIR *dirp, bool use_current_pos)
87 {
88 	struct dirent **dpv;
89 	char *buf, *ddptr, *ddeptr;
90 	off_t pos;
91 	int fd2, incr, len, n, saved_errno, space;
92 
93 	len = 0;
94 	space = 0;
95 	buf = NULL;
96 	ddptr = NULL;
97 
98 	/*
99 	 * Use the system page size if that is a multiple of DIRBLKSIZ.
100 	 * Hopefully this can be a big win someday by allowing page
101 	 * trades to user space to be done by _getdirentries().
102 	 */
103 	incr = getpagesize();
104 	if ((incr % DIRBLKSIZ) != 0)
105 		incr = DIRBLKSIZ;
106 
107 	/*
108 	 * The strategy here is to read all the directory
109 	 * entries into a buffer, sort the buffer, and
110 	 * remove duplicate entries by setting the inode
111 	 * number to zero.
112 	 *
113 	 * We reopen the directory because _getdirentries()
114 	 * on a MNT_UNION mount modifies the open directory,
115 	 * making it refer to the lower directory after the
116 	 * upper directory's entries are exhausted.
117 	 * This would otherwise break software that uses
118 	 * the directory descriptor for fchdir or *at
119 	 * functions, such as fts.c.
120 	 */
121 	if ((fd2 = _openat(dirp->dd_fd, ".", O_RDONLY | O_CLOEXEC)) == -1)
122 		return (false);
123 
124 	if (use_current_pos) {
125 		pos = lseek(dirp->dd_fd, 0, SEEK_CUR);
126 		if (pos == -1 || lseek(fd2, pos, SEEK_SET) == -1) {
127 			saved_errno = errno;
128 			_close(fd2);
129 			errno = saved_errno;
130 			return (false);
131 		}
132 	}
133 
134 	do {
135 		/*
136 		 * Always make at least DIRBLKSIZ bytes
137 		 * available to _getdirentries
138 		 */
139 		if (space < DIRBLKSIZ) {
140 			space += incr;
141 			len += incr;
142 			buf = reallocf(buf, len);
143 			if (buf == NULL) {
144 				saved_errno = errno;
145 				_close(fd2);
146 				errno = saved_errno;
147 				return (false);
148 			}
149 			ddptr = buf + (len - space);
150 		}
151 
152 		n = _getdirentries(fd2, ddptr, space, &dirp->dd_seek);
153 		if (n > 0) {
154 			ddptr += n;
155 			space -= n;
156 		}
157 		if (n < 0) {
158 			saved_errno = errno;
159 			_close(fd2);
160 			errno = saved_errno;
161 			return (false);
162 		}
163 	} while (n > 0);
164 	_close(fd2);
165 
166 	ddeptr = ddptr;
167 
168 	/*
169 	 * There is now a buffer full of (possibly) duplicate
170 	 * names.
171 	 */
172 	dirp->dd_buf = buf;
173 
174 	/*
175 	 * Go round this loop twice...
176 	 *
177 	 * Scan through the buffer, counting entries.
178 	 * On the second pass, save pointers to each one.
179 	 * Then sort the pointers and remove duplicate names.
180 	 */
181 	for (dpv = NULL;;) {
182 		n = 0;
183 		ddptr = buf;
184 		while (ddptr < ddeptr) {
185 			struct dirent *dp;
186 
187 			dp = (struct dirent *) ddptr;
188 			if ((long)dp & 03L)
189 				break;
190 			if ((dp->d_reclen <= 0) ||
191 			    (dp->d_reclen > (ddeptr + 1 - ddptr)))
192 				break;
193 			ddptr += dp->d_reclen;
194 			if (dp->d_fileno) {
195 				if (dpv)
196 					dpv[n] = dp;
197 				n++;
198 			}
199 		}
200 
201 		if (dpv) {
202 			struct dirent *xp;
203 
204 			/*
205 			 * This sort must be stable.
206 			 */
207 			mergesort(dpv, n, sizeof(*dpv), opendir_compar);
208 
209 			dpv[n] = NULL;
210 			xp = NULL;
211 
212 			/*
213 			 * Scan through the buffer in sort order,
214 			 * zapping the inode number of any
215 			 * duplicate names.
216 			 */
217 			for (n = 0; dpv[n]; n++) {
218 				struct dirent *dp = dpv[n];
219 
220 				if ((xp == NULL) ||
221 				    strcmp(dp->d_name, xp->d_name)) {
222 					xp = dp;
223 				} else {
224 					dp->d_fileno = 0;
225 				}
226 				if (dp->d_type == DT_WHT &&
227 				    (dirp->dd_flags & DTF_HIDEW))
228 					dp->d_fileno = 0;
229 			}
230 
231 			free(dpv);
232 			break;
233 		} else {
234 			dpv = malloc((n+1) * sizeof(struct dirent *));
235 			if (dpv == NULL)
236 				break;
237 		}
238 	}
239 
240 	dirp->dd_len = len;
241 	dirp->dd_size = ddptr - dirp->dd_buf;
242 	return (true);
243 }
244 
245 /*
246  * Return true if the file descriptor is associated with a file from a
247  * union file system or from a file system mounted with the union flag.
248  */
249 static bool
is_unionstack(int fd)250 is_unionstack(int fd)
251 {
252 	/*
253 	 * This call shouldn't fail, but if it does, just assume that the
254 	 * answer is no.
255 	 */
256 	return (_fcntl(fd, F_ISUNIONSTACK, 0) > 0);
257 }
258 
259 /*
260  * Common routine for opendir(3), __opendir2(3) and fdopendir(3).
261  */
262 DIR *
__opendir_common(int fd,int flags,bool use_current_pos)263 __opendir_common(int fd, int flags, bool use_current_pos)
264 {
265 	DIR *dirp;
266 	ssize_t ret;
267 	int incr;
268 	int saved_errno;
269 	bool unionstack;
270 
271 	if ((dirp = malloc(sizeof(DIR) + sizeof(struct _telldir))) == NULL)
272 		return (NULL);
273 
274 	dirp->dd_buf = NULL;
275 	dirp->dd_fd = fd;
276 	dirp->dd_flags = flags;
277 	dirp->dd_loc = 0;
278 	dirp->dd_lock = NULL;
279 	dirp->dd_td = (struct _telldir *)((char *)dirp + sizeof(DIR));
280 	LIST_INIT(&dirp->dd_td->td_locq);
281 	dirp->dd_td->td_loccnt = 0;
282 	dirp->dd_compat_de = NULL;
283 
284 	/*
285 	 * Use the system page size if that is a multiple of DIRBLKSIZ.
286 	 * Hopefully this can be a big win someday by allowing page
287 	 * trades to user space to be done by _getdirentries().
288 	 */
289 	incr = getpagesize();
290 	if ((incr % DIRBLKSIZ) != 0)
291 		incr = DIRBLKSIZ;
292 
293 	/*
294 	 * Determine whether this directory is the top of a union stack.
295 	 */
296 	unionstack = false;
297 	if (flags & DTF_NODUP) {
298 		unionstack = is_unionstack(fd);
299 	}
300 
301 	if (unionstack) {
302 		if (!_filldir(dirp, use_current_pos))
303 			goto fail;
304 		dirp->dd_flags |= __DTF_READALL;
305 	} else {
306 		dirp->dd_len = incr;
307 		dirp->dd_buf = malloc(dirp->dd_len);
308 		if (dirp->dd_buf == NULL)
309 			goto fail;
310 		if (use_current_pos) {
311 			/*
312 			 * Read the first batch of directory entries
313 			 * to prime dd_seek.  This also checks if the
314 			 * fd passed to fdopendir() is a directory.
315 			 */
316 			ret = _getdirentries(dirp->dd_fd,
317 			    dirp->dd_buf, dirp->dd_len, &dirp->dd_seek);
318 			if (ret < 0)
319 				goto fail;
320 			dirp->dd_size = (size_t)ret;
321 			dirp->dd_flags |= __DTF_SKIPREAD;
322 		} else {
323 			dirp->dd_size = 0;
324 			dirp->dd_seek = 0;
325 		}
326 	}
327 
328 	return (dirp);
329 
330 fail:
331 	saved_errno = errno;
332 	free(dirp->dd_buf);
333 	free(dirp);
334 	errno = saved_errno;
335 	return (NULL);
336 }
337