]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/getdents.c
powerpc: Remove unintended __longjmp symbol from ABI
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / getdents.c
CommitLineData
f7a9f785 1/* Copyright (C) 1993-2016 Free Software Foundation, Inc.
df4ef2ab
UD
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
41bdb6e2
AJ
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.
df4ef2ab
UD
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
41bdb6e2 12 Lesser General Public License for more details.
df4ef2ab 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
df4ef2ab 17
8d57beea 18#include <alloca.h>
4186c9f4 19#include <assert.h>
0dee6738 20#include <errno.h>
df4ef2ab
UD
21#include <dirent.h>
22#include <stddef.h>
da0fdef0 23#include <stdint.h>
df4ef2ab
UD
24#include <string.h>
25#include <unistd.h>
8d57beea 26#include <sys/param.h>
df4ef2ab
UD
27#include <sys/types.h>
28
0dee6738
UD
29#include <sysdep.h>
30#include <sys/syscall.h>
31
df4ef2ab
UD
32#include <linux/posix_types.h>
33
6ddd37a4 34#include <kernel-features.h>
14860991 35
df4ef2ab
UD
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
0dee6738 41 Here is the kernel definition of `struct dirent' as of 2.1.20: */
df4ef2ab
UD
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
14860991
UD
51struct kernel_dirent64
52 {
da0fdef0 53 uint64_t d_ino;
14860991
UD
54 int64_t d_off;
55 unsigned short int d_reclen;
56 unsigned char d_type;
57 char d_name[256];
58 };
59
56ddf355
UD
60#ifndef __GETDENTS
61# define __GETDENTS __getdents
14860991
UD
62#endif
63#ifndef DIRENT_TYPE
56ddf355
UD
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)
9756dfe1 68#endif
df4ef2ab
UD
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
831372e7 72 some heuristic. Assuming the directory contains names with 14
980e5832 73 characters on average we can compute an estimated number of entries
831372e7 74 which fit in the buffer. Taking this number allows us to specify a
980e5832
UD
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. */
8d57beea 78ssize_t
980e5832 79internal_function
56ddf355 80__GETDENTS (int fd, char *buf, size_t nbytes)
df4ef2ab 81{
14860991 82 ssize_t retval;
980e5832 83
7cd195df
L
84 /* The d_ino and d_off fields in kernel_dirent and dirent must have
85 the same sizes and alignments. */
86 if (sizeof (DIRENT_TYPE) == sizeof (struct dirent)
87 && (sizeof (((struct kernel_dirent *) 0)->d_ino)
88 == sizeof (((struct dirent *) 0)->d_ino))
89 && (sizeof (((struct kernel_dirent *) 0)->d_off)
90 == sizeof (((struct dirent *) 0)->d_off))
91 && (offsetof (struct kernel_dirent, d_off)
92 == offsetof (struct dirent, d_off))
93 && (offsetof (struct kernel_dirent, d_reclen)
94 == offsetof (struct dirent, d_reclen)))
afedc4af 95 {
a2da1673 96 retval = INLINE_SYSCALL (getdents, 3, fd, buf, nbytes);
afedc4af
UD
97
98 /* The kernel added the d_type value after the name. Change
99 this now. */
100 if (retval != -1)
101 {
102 union
103 {
104 struct kernel_dirent k;
105 struct dirent u;
106 } *kbuf = (void *) buf;
107
108 while ((char *) kbuf < buf + retval)
109 {
110 char d_type = *((char *) kbuf + kbuf->k.d_reclen - 1);
111 memmove (kbuf->u.d_name, kbuf->k.d_name,
112 strlen (kbuf->k.d_name) + 1);
113 kbuf->u.d_type = d_type;
114
115 kbuf = (void *) ((char *) kbuf + kbuf->k.d_reclen);
116 }
117 }
118
119 return retval;
120 }
afedc4af
UD
121
122 off64_t last_offset = -1;
123
14860991 124#ifdef __NR_getdents64
37ad3473
JM
125 {
126 union
df4ef2ab 127 {
37ad3473
JM
128 struct kernel_dirent64 k;
129 DIRENT_TYPE u;
130 char b[1];
131 } *kbuf = (void *) buf, *outp, *inp;
132 size_t kbytes = nbytes;
133 if (offsetof (DIRENT_TYPE, d_name)
134 < offsetof (struct kernel_dirent64, d_name)
135 && nbytes <= sizeof (DIRENT_TYPE))
c213fe9c 136 {
37ad3473
JM
137 kbytes = (nbytes + offsetof (struct kernel_dirent64, d_name)
138 - offsetof (DIRENT_TYPE, d_name));
139 kbuf = __alloca(kbytes);
140 }
141 retval = INLINE_SYSCALL (getdents64, 3, fd, kbuf, kbytes);
142 const size_t size_diff = (offsetof (struct kernel_dirent64, d_name)
143 - offsetof (DIRENT_TYPE, d_name));
144
145 /* Return the error if encountered. */
146 if (retval == -1)
147 return -1;
14860991 148
37ad3473
JM
149 /* If the structure returned by the kernel is identical to what we
150 need, don't do any conversions. */
151 if (offsetof (DIRENT_TYPE, d_name)
152 == offsetof (struct kernel_dirent64, d_name)
153 && sizeof (outp->u.d_ino) == sizeof (inp->k.d_ino)
154 && sizeof (outp->u.d_off) == sizeof (inp->k.d_off))
155 return retval;
156
157 /* These two pointers might alias the same memory buffer.
158 Standard C requires that we always use the same type for them,
159 so we must use the union type. */
160 inp = kbuf;
161 outp = (void *) buf;
162
163 while (&inp->b < &kbuf->b + retval)
164 {
165 const size_t alignment = __alignof__ (DIRENT_TYPE);
166 /* Since inp->k.d_reclen is already aligned for the kernel
167 structure this may compute a value that is bigger
168 than necessary. */
169 size_t old_reclen = inp->k.d_reclen;
170 size_t new_reclen = ((old_reclen - size_diff + alignment - 1)
171 & ~(alignment - 1));
172
173 /* Copy the data out of the old structure into temporary space.
174 Then copy the name, which may overlap if BUF == KBUF. */
175 const uint64_t d_ino = inp->k.d_ino;
176 const int64_t d_off = inp->k.d_off;
177 const uint8_t d_type = inp->k.d_type;
178
179 memmove (outp->u.d_name, inp->k.d_name,
180 old_reclen - offsetof (struct kernel_dirent64, d_name));
181
182 /* Now we have copied the data from INP and access only OUTP. */
183
184 DIRENT_SET_DP_INO (&outp->u, d_ino);
185 outp->u.d_off = d_off;
186 if ((sizeof (outp->u.d_ino) != sizeof (inp->k.d_ino)
187 && outp->u.d_ino != d_ino)
188 || (sizeof (outp->u.d_off) != sizeof (inp->k.d_off)
189 && outp->u.d_off != d_off))
190 {
191 /* Overflow. If there was at least one entry
192 before this one, return them without error,
193 otherwise signal overflow. */
194 if (last_offset != -1)
195 {
196 __lseek64 (fd, last_offset, SEEK_SET);
197 return outp->b - buf;
198 }
199 __set_errno (EOVERFLOW);
05ae4d6a 200 return -1;
37ad3473 201 }
05ae4d6a 202
37ad3473
JM
203 last_offset = d_off;
204 outp->u.d_reclen = new_reclen;
205 outp->u.d_type = d_type;
f11b9da6 206
37ad3473
JM
207 inp = (void *) inp + old_reclen;
208 outp = (void *) outp + new_reclen;
209 }
831372e7 210
37ad3473
JM
211 return outp->b - buf;
212 }
14860991
UD
213#endif
214 {
215 size_t red_nbytes;
216 struct kernel_dirent *skdp, *kdp;
217 const size_t size_diff = (offsetof (DIRENT_TYPE, d_name)
218 - offsetof (struct kernel_dirent, d_name));
219
220 red_nbytes = MIN (nbytes
221 - ((nbytes / (offsetof (DIRENT_TYPE, d_name) + 14))
222 * size_diff),
223 nbytes - size_diff);
224
14860991
UD
225 skdp = kdp = __alloca (red_nbytes);
226
a2da1673 227 retval = INLINE_SYSCALL (getdents, 3, fd, (char *) kdp, red_nbytes);
14860991
UD
228
229 if (retval == -1)
230 return -1;
231
c213fe9c 232 DIRENT_TYPE *dp = (DIRENT_TYPE *) buf;
14860991
UD
233 while ((char *) kdp < (char *) skdp + retval)
234 {
235 const size_t alignment = __alignof__ (DIRENT_TYPE);
236 /* Since kdp->d_reclen is already aligned for the kernel structure
237 this may compute a value that is bigger than necessary. */
238 size_t new_reclen = ((kdp->d_reclen + size_diff + alignment - 1)
239 & ~(alignment - 1));
240 if ((char *) dp + new_reclen > buf + nbytes)
241 {
242 /* Our heuristic failed. We read too many entries. Reset
243 the stream. */
244 assert (last_offset != -1);
f9ef3e72 245 __lseek64 (fd, last_offset, SEEK_SET);
14860991
UD
246
247 if ((char *) dp == buf)
248 {
249 /* The buffer the user passed in is too small to hold even
250 one entry. */
251 __set_errno (EINVAL);
252 return -1;
253 }
254
255 break;
256 }
257
258 last_offset = kdp->d_off;
259 DIRENT_SET_DP_INO(dp, kdp->d_ino);
260 dp->d_off = kdp->d_off;
261 dp->d_reclen = new_reclen;
1ac7a2c7 262 dp->d_type = *((char *) kdp + kdp->d_reclen - 1);
14860991
UD
263 memcpy (dp->d_name, kdp->d_name,
264 kdp->d_reclen - offsetof (struct kernel_dirent, d_name));
265
266 dp = (DIRENT_TYPE *) ((char *) dp + new_reclen);
267 kdp = (struct kernel_dirent *) (((char *) kdp) + kdp->d_reclen);
268 }
df4ef2ab 269
c213fe9c
RM
270 return (char *) dp - buf;
271 }
df4ef2ab 272}