xref: /openbsd-src/libexec/ld.so/dir.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: dir.c,v 1.23 2015/01/16 16:18:07 deraadt 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/types.h>
32 #include <sys/stat.h>
33 
34 #include <dirent.h>
35 #include <fcntl.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 
39 #include "syscall.h"
40 #include "archdep.h"
41 #include "util.h"
42 #include "dir.h"
43 
44 struct _dl_dirdesc {
45 	long	dd_loc;		/* offset in current buffer */
46 	long	dd_size;	/* amount of data returned by getdents() */
47 	char	*dd_buf;	/* data buffer */
48 	int	dd_len;		/* size of data buffer */
49 	int	dd_fd;		/* file descriptor associated with directory */
50 };
51 
52 /*
53  * Open a directory.
54  */
55 _dl_DIR *
56 _dl_opendir(const char *name)
57 {
58 	_dl_DIR *dirp;
59 	int fd;
60 	struct stat sb;
61 
62 	if ((fd = _dl_open(name, O_RDONLY | O_DIRECTORY | O_CLOEXEC)) < 0)
63 		return (NULL);
64 	if (_dl_fstat(fd, &sb) || (dirp = _dl_malloc(sizeof(*dirp))) == NULL) {
65 		_dl_close(fd);
66 		return (NULL);
67 	}
68 
69 	dirp->dd_fd = fd;
70 	dirp->dd_loc = 0;
71 	dirp->dd_size = 0;
72 	dirp->dd_len = _dl_round_page(sb.st_blksize);
73 	dirp->dd_buf = _dl_malloc(dirp->dd_len);
74 	if (dirp->dd_buf == NULL) {
75 		_dl_free(dirp);
76 		_dl_close (fd);
77 		return (NULL);
78 	}
79 
80 	return (dirp);
81 }
82 
83 
84 /*
85  * close a directory.
86  */
87 int
88 _dl_closedir(_dl_DIR *dirp)
89 {
90 	int ret;
91 
92 	ret = _dl_close(dirp->dd_fd);
93 	_dl_free(dirp->dd_buf);
94 	_dl_free(dirp);
95 	return ret;
96 }
97 
98 
99 /*
100  * get next entry in a directory.
101  */
102 struct dirent *
103 _dl_readdir(_dl_DIR *dirp)
104 {
105 	struct dirent *dp;
106 
107 	for (;;) {
108 		if (dirp->dd_loc >= dirp->dd_size) {
109 			dirp->dd_loc = 0;
110 		}
111 		if (dirp->dd_loc == 0) {
112 			dirp->dd_size = _dl_getdents(dirp->dd_fd,
113 			    dirp->dd_buf, dirp->dd_len);
114 			if (dirp->dd_size <= 0)
115 				return (NULL);
116 		}
117 		dp = (struct dirent *)(dirp->dd_buf + dirp->dd_loc);
118 		if ((long)dp & 03)	/* bogus pointer check */
119 			return (NULL);
120 		if (dp->d_reclen <= 0 ||
121 		    dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc)
122 			return (NULL);
123 		dirp->dd_loc += dp->d_reclen;
124 		if (dp->d_ino == 0)
125 			continue;
126 		return (dp);
127 	}
128 }
129