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