1/* Copyright (C) 1993-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
17
18#include <alloca.h>
19#include <assert.h>
20#include <errno.h>
21#include <dirent.h>
22#include <stddef.h>
23#include <stdint.h>
24#include <string.h>
25#include <unistd.h>
26#include <sys/param.h>
27#include <sys/types.h>
28
29#include <sysdep.h>
30#include <sys/syscall.h>
31
32#include <linux/posix_types.h>
33
34#include <kernel-features.h>
35
36/* For Linux we need a special version of this file since the
37 definition of `struct dirent' is not the same for the kernel and
38 the libc. There is one additional field which might be introduced
39 in the kernel structure in the future.
40
41 Here is the kernel definition of `struct dirent' as of 2.1.20: */
42
43struct kernel_dirent
44 {
45 long int d_ino;
46 __kernel_off_t d_off;
47 unsigned short int d_reclen;
48 char d_name[256];
49 };
50
51struct kernel_dirent64
52 {
53 uint64_t d_ino;
54 int64_t d_off;
55 unsigned short int d_reclen;
56 unsigned char d_type;
57 char d_name[256];
58 };
59
60#ifndef __GETDENTS
61# define __GETDENTS __getdents
62#endif
63#ifndef DIRENT_TYPE
64# define DIRENT_TYPE struct dirent
65#endif
66#ifndef DIRENT_SET_DP_INO
67# define DIRENT_SET_DP_INO(dp, value) (dp)->d_ino = (value)
68#endif
69
70/* The problem here is that we cannot simply read the next NBYTES
71 bytes. We need to take the additional field into account. We use
72 some heuristic. Assuming the directory contains names with 14
73 characters on average we can compute an estimated number of entries
74 which fit in the buffer. Taking this number allows us to specify a
75 reasonable number of bytes to read. If we should be wrong, we can
76 reset the file descriptor. In practice the kernel is limiting the
77 amount of data returned much more then the reduced buffer size. */
78ssize_t
79__GETDENTS (int fd, char *buf, size_t nbytes)
80{
81 ssize_t retval;
82
83 /* The d_ino and d_off fields in kernel_dirent and dirent must have
84 the same sizes and alignments. */
85 if (sizeof (DIRENT_TYPE) == sizeof (struct dirent)
86 && (sizeof (((struct kernel_dirent *) 0)->d_ino)
87 == sizeof (((struct dirent *) 0)->d_ino))
88 && (sizeof (((struct kernel_dirent *) 0)->d_off)
89 == sizeof (((struct dirent *) 0)->d_off))
90 && (offsetof (struct kernel_dirent, d_off)
91 == offsetof (struct dirent, d_off))
92 && (offsetof (struct kernel_dirent, d_reclen)
93 == offsetof (struct dirent, d_reclen)))
94 {
95 retval = INLINE_SYSCALL (getdents, 3, fd, buf, nbytes);
96
97 /* The kernel added the d_type value after the name. Change
98 this now. */
99 if (retval != -1)
100 {
101 union
102 {
103 struct kernel_dirent k;
104 struct dirent u;
105 } *kbuf = (void *) buf;
106
107 while ((char *) kbuf < buf + retval)
108 {
109 char d_type = *((char *) kbuf + kbuf->k.d_reclen - 1);
110 memmove (kbuf->u.d_name, kbuf->k.d_name,
111 strlen (kbuf->k.d_name) + 1);
112 kbuf->u.d_type = d_type;
113
114 kbuf = (void *) ((char *) kbuf + kbuf->k.d_reclen);
115 }
116 }
117
118 return retval;
119 }
120
121 off64_t last_offset = -1;
122
123#ifdef __NR_getdents64
124 {
125 union
126 {
127 struct kernel_dirent64 k;
128 DIRENT_TYPE u;
129 char b[1];
130 } *kbuf = (void *) buf, *outp, *inp;
131 size_t kbytes = nbytes;
132 if (offsetof (DIRENT_TYPE, d_name)
133 < offsetof (struct kernel_dirent64, d_name)
134 && nbytes <= sizeof (DIRENT_TYPE))
135 {
136 kbytes = (nbytes + offsetof (struct kernel_dirent64, d_name)
137 - offsetof (DIRENT_TYPE, d_name));
138 kbuf = __alloca(kbytes);
139 }
140 retval = INLINE_SYSCALL (getdents64, 3, fd, kbuf, kbytes);
141 const size_t size_diff = (offsetof (struct kernel_dirent64, d_name)
142 - offsetof (DIRENT_TYPE, d_name));
143
144 /* Return the error if encountered. */
145 if (retval == -1)
146 return -1;
147
148 /* If the structure returned by the kernel is identical to what we
149 need, don't do any conversions. */
150 if (offsetof (DIRENT_TYPE, d_name)
151 == offsetof (struct kernel_dirent64, d_name)
152 && sizeof (outp->u.d_ino) == sizeof (inp->k.d_ino)
153 && sizeof (outp->u.d_off) == sizeof (inp->k.d_off))
154 return retval;
155
156 /* These two pointers might alias the same memory buffer.
157 Standard C requires that we always use the same type for them,
158 so we must use the union type. */
159 inp = kbuf;
160 outp = (void *) buf;
161
162 while (&inp->b < &kbuf->b + retval)
163 {
164 const size_t alignment = __alignof__ (DIRENT_TYPE);
165 /* Since inp->k.d_reclen is already aligned for the kernel
166 structure this may compute a value that is bigger
167 than necessary. */
168 size_t old_reclen = inp->k.d_reclen;
169 size_t new_reclen = ((old_reclen - size_diff + alignment - 1)
170 & ~(alignment - 1));
171
172 /* Copy the data out of the old structure into temporary space.
173 Then copy the name, which may overlap if BUF == KBUF. */
174 const uint64_t d_ino = inp->k.d_ino;
175 const int64_t d_off = inp->k.d_off;
176 const uint8_t d_type = inp->k.d_type;
177
178 memmove (outp->u.d_name, inp->k.d_name,
179 old_reclen - offsetof (struct kernel_dirent64, d_name));
180
181 /* Now we have copied the data from INP and access only OUTP. */
182
183 DIRENT_SET_DP_INO (&outp->u, d_ino);
184 outp->u.d_off = d_off;
185 if ((sizeof (outp->u.d_ino) != sizeof (inp->k.d_ino)
186 && outp->u.d_ino != d_ino)
187 || (sizeof (outp->u.d_off) != sizeof (inp->k.d_off)
188 && outp->u.d_off != d_off))
189 {
190 /* Overflow. If there was at least one entry
191 before this one, return them without error,
192 otherwise signal overflow. */
193 if (last_offset != -1)
194 {
195 __lseek64 (fd, last_offset, SEEK_SET);
196 return outp->b - buf;
197 }
198 __set_errno (EOVERFLOW);
199 return -1;
200 }
201
202 last_offset = d_off;
203 outp->u.d_reclen = new_reclen;
204 outp->u.d_type = d_type;
205
206 inp = (void *) inp + old_reclen;
207 outp = (void *) outp + new_reclen;
208 }
209
210 return outp->b - buf;
211 }
212#endif
213 {
214 size_t red_nbytes;
215 struct kernel_dirent *skdp, *kdp;
216 const size_t size_diff = (offsetof (DIRENT_TYPE, d_name)
217 - offsetof (struct kernel_dirent, d_name));
218
219 red_nbytes = MIN (nbytes
220 - ((nbytes / (offsetof (DIRENT_TYPE, d_name) + 14))
221 * size_diff),
222 nbytes - size_diff);
223
224 skdp = kdp = __alloca (red_nbytes);
225
226 retval = INLINE_SYSCALL (getdents, 3, fd, (char *) kdp, red_nbytes);
227
228 if (retval == -1)
229 return -1;
230
231 DIRENT_TYPE *dp = (DIRENT_TYPE *) buf;
232 while ((char *) kdp < (char *) skdp + retval)
233 {
234 const size_t alignment = __alignof__ (DIRENT_TYPE);
235 /* Since kdp->d_reclen is already aligned for the kernel structure
236 this may compute a value that is bigger than necessary. */
237 size_t new_reclen = ((kdp->d_reclen + size_diff + alignment - 1)
238 & ~(alignment - 1));
239 if ((char *) dp + new_reclen > buf + nbytes)
240 {
241 /* Our heuristic failed. We read too many entries. Reset
242 the stream. */
243 assert (last_offset != -1);
244 __lseek64 (fd, last_offset, SEEK_SET);
245
246 if ((char *) dp == buf)
247 {
248 /* The buffer the user passed in is too small to hold even
249 one entry. */
250 __set_errno (EINVAL);
251 return -1;
252 }
253
254 break;
255 }
256
257 last_offset = kdp->d_off;
258 DIRENT_SET_DP_INO(dp, kdp->d_ino);
259 dp->d_off = kdp->d_off;
260 dp->d_reclen = new_reclen;
261 dp->d_type = *((char *) kdp + kdp->d_reclen - 1);
262 memcpy (dp->d_name, kdp->d_name,
263 kdp->d_reclen - offsetof (struct kernel_dirent, d_name));
264
265 dp = (DIRENT_TYPE *) ((char *) dp + new_reclen);
266 kdp = (struct kernel_dirent *) (((char *) kdp) + kdp->d_reclen);
267 }
268
269 return (char *) dp - buf;
270 }
271}
272