xref: /openbsd/libexec/ld.so/dir.c (revision 4bdff4be)
1 /*	$OpenBSD: dir.c,v 1.25 2022/01/08 06:49:41 guenther Exp $	*/
2 /*
3  * Copyright (c) 1983, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/stat.h>
32 
33 #include <dirent.h>
34 #include <fcntl.h>
35 
36 #include "syscall.h"
37 #include "util.h"
38 #include "dir.h"
39 
40 struct _dl_dirdesc {
41 	long	dd_loc;		/* offset in current buffer */
42 	long	dd_size;	/* amount of data returned by getdents() */
43 	char	*dd_buf;	/* data buffer */
44 	int	dd_len;		/* size of data buffer */
45 	int	dd_fd;		/* file descriptor associated with directory */
46 };
47 
48 /*
49  * Open a directory.
50  */
51 _dl_DIR *
52 _dl_opendir(const char *name)
53 {
54 	_dl_DIR *dirp;
55 	int fd;
56 	struct stat sb;
57 
58 	if ((fd = _dl_open(name, O_RDONLY | O_DIRECTORY | O_CLOEXEC)) < 0)
59 		return (NULL);
60 	if (_dl_fstat(fd, &sb) || (dirp = _dl_malloc(sizeof(*dirp))) == NULL) {
61 		_dl_close(fd);
62 		return (NULL);
63 	}
64 
65 	dirp->dd_fd = fd;
66 	dirp->dd_loc = 0;
67 	dirp->dd_size = 0;
68 	dirp->dd_len = _dl_round_page(sb.st_blksize);
69 	dirp->dd_buf = _dl_malloc(dirp->dd_len);
70 	if (dirp->dd_buf == NULL) {
71 		_dl_free(dirp);
72 		_dl_close(fd);
73 		return (NULL);
74 	}
75 
76 	return (dirp);
77 }
78 
79 
80 /*
81  * close a directory.
82  */
83 int
84 _dl_closedir(_dl_DIR *dirp)
85 {
86 	int ret;
87 
88 	ret = _dl_close(dirp->dd_fd);
89 	_dl_free(dirp->dd_buf);
90 	_dl_free(dirp);
91 	return ret;
92 }
93 
94 
95 /*
96  * get next entry in a directory.
97  */
98 struct dirent *
99 _dl_readdir(_dl_DIR *dirp)
100 {
101 	struct dirent *dp;
102 
103 	for (;;) {
104 		if (dirp->dd_loc >= dirp->dd_size) {
105 			dirp->dd_loc = 0;
106 		}
107 		if (dirp->dd_loc == 0) {
108 			dirp->dd_size = _dl_getdents(dirp->dd_fd,
109 			    dirp->dd_buf, dirp->dd_len);
110 			if (dirp->dd_size <= 0)
111 				return (NULL);
112 		}
113 		dp = (struct dirent *)(dirp->dd_buf + dirp->dd_loc);
114 		if ((long)dp & 03)	/* bogus pointer check */
115 			return (NULL);
116 		if (dp->d_reclen <= 0 ||
117 		    dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc)
118 			return (NULL);
119 		dirp->dd_loc += dp->d_reclen;
120 		if (dp->d_ino == 0)
121 			continue;
122 		return (dp);
123 	}
124 }
125