]>
Commit | Line | Data |
---|---|---|
298d0e31 | 1 | /* Get directory entries. Linux/MIPSn64 LFS version. |
04277e02 | 2 | Copyright (C) 2018-2019 Free Software Foundation, Inc. |
298d0e31 AZ |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library. If not, see | |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
298d0e31 AZ |
18 | |
19 | #include <string.h> | |
20 | #include <dirent.h> | |
21 | #include <errno.h> | |
22 | #include <assert.h> | |
23 | #include <sys/param.h> | |
24 | #include <unistd.h> | |
a620bd79 | 25 | #include <limits.h> |
298d0e31 | 26 | |
edcda4c0 AZ |
27 | #include <include/libc-pointer-arith.h> |
28 | ||
298d0e31 | 29 | ssize_t |
edcda4c0 | 30 | __getdents64 (int fd, void *buf, size_t nbytes) |
298d0e31 | 31 | { |
a620bd79 FW |
32 | /* The system call takes an unsigned int argument, and some length |
33 | checks in the kernel use an int type. */ | |
34 | if (nbytes > INT_MAX) | |
35 | nbytes = INT_MAX; | |
36 | ||
298d0e31 | 37 | #ifdef __NR_getdents64 |
edcda4c0 AZ |
38 | static int getdents64_supported = true; |
39 | if (atomic_load_relaxed (&getdents64_supported)) | |
40 | { | |
41 | ssize_t ret = INLINE_SYSCALL_CALL (getdents64, fd, buf, nbytes); | |
42 | if (ret >= 0 || errno != ENOSYS) | |
43 | return ret; | |
44 | ||
45 | atomic_store_relaxed (&getdents64_supported, false); | |
46 | } | |
298d0e31 AZ |
47 | #endif |
48 | ||
49 | /* Unfortunately getdents64 was only wire-up for MIPS n64 on Linux 3.10. | |
edcda4c0 AZ |
50 | If the syscall is not available it need to fallback to the non-LFS one. |
51 | Also to avoid an unbounded allocation through VLA/alloca or malloc (which | |
52 | would make the syscall non async-signal-safe) it uses a limited buffer. | |
53 | This is sub-optimal for large NBYTES, however this is a fallback | |
54 | mechanism to emulate a syscall that kernel should provide. */ | |
298d0e31 AZ |
55 | |
56 | struct kernel_dirent | |
edcda4c0 AZ |
57 | { |
58 | #if _MIPS_SIM == _ABI64 | |
59 | uint64_t d_ino; | |
60 | uint64_t d_off; | |
61 | #else | |
62 | uint32_t d_ino; | |
63 | uint32_t d_off; | |
64 | #endif | |
65 | unsigned short int d_reclen; | |
66 | char d_name[1]; | |
67 | }; | |
68 | ||
69 | /* The largest possible practical length of the d_name member are 255 | |
70 | Unicode characters in UTF-8 encoding, so d_name is 766 bytes long, plus | |
71 | 18 (mips64) / 10 (mips64n32) bytes from header, for total of 784 (mips64) | |
72 | / 776 (mips64n32) bytes total. Ensure that the minimum size holds at | |
73 | least one entry. */ | |
74 | enum { KBUF_SIZE = 1024 }; | |
75 | char kbuf[KBUF_SIZE]; | |
76 | size_t kbuf_size = nbytes < KBUF_SIZE ? nbytes : KBUF_SIZE; | |
298d0e31 AZ |
77 | |
78 | const size_t size_diff = (offsetof (struct dirent64, d_name) | |
79 | - offsetof (struct kernel_dirent, d_name)); | |
80 | ||
edcda4c0 | 81 | struct dirent64 *dp = (struct dirent64 *) buf; |
298d0e31 | 82 | |
edcda4c0 AZ |
83 | size_t nb = 0; |
84 | off64_t last_offset = -1; | |
298d0e31 | 85 | |
edcda4c0 AZ |
86 | ssize_t r = INLINE_SYSCALL_CALL (getdents, fd, kbuf, kbuf_size); |
87 | if (r <= 0) | |
88 | return r; | |
298d0e31 | 89 | |
edcda4c0 AZ |
90 | struct kernel_dirent *skdp, *kdp; |
91 | skdp = kdp = (struct kernel_dirent *) kbuf; | |
298d0e31 | 92 | |
edcda4c0 | 93 | while ((char *) kdp < (char *) skdp + r) |
298d0e31 | 94 | { |
edcda4c0 AZ |
95 | /* This macro is used to avoid aliasing violation. */ |
96 | #define KDP_MEMBER(src, member) \ | |
97 | (__typeof__((struct kernel_dirent){0}.member) *) \ | |
98 | memcpy (&((__typeof__((struct kernel_dirent){0}.member)){0}), \ | |
99 | ((char *)(src) + offsetof (struct kernel_dirent, member)),\ | |
100 | sizeof ((struct kernel_dirent){0}.member)) | |
101 | ||
102 | /* This is a conservative approximation, since some of size_diff might | |
103 | fit into the existing padding for alignment. */ | |
104 | unsigned short int k_reclen = *KDP_MEMBER (kdp, d_reclen); | |
105 | unsigned short int new_reclen = ALIGN_UP (k_reclen + size_diff, | |
106 | _Alignof (struct dirent64)); | |
107 | if (nb + new_reclen > nbytes) | |
108 | { | |
109 | /* Entry is too large for the fixed-size buffer. */ | |
110 | if (last_offset == -1) | |
298d0e31 | 111 | { |
edcda4c0 AZ |
112 | __set_errno (EINVAL); |
113 | return -1; | |
298d0e31 AZ |
114 | } |
115 | ||
edcda4c0 AZ |
116 | /* The new entry will overflow the input buffer, rewind to last |
117 | obtained entry and return. */ | |
118 | __lseek64 (fd, last_offset, SEEK_SET); | |
119 | return (char *) dp - (char *) buf; | |
298d0e31 | 120 | } |
edcda4c0 AZ |
121 | nb += new_reclen; |
122 | ||
123 | memcpy (((char *) dp + offsetof (struct dirent64, d_ino)), | |
124 | KDP_MEMBER (kdp, d_ino), sizeof ((struct dirent64){0}.d_ino)); | |
125 | memcpy (((char *) dp + offsetof (struct dirent64, d_off)), | |
126 | KDP_MEMBER (kdp, d_off), sizeof ((struct dirent64){0}.d_off)); | |
127 | last_offset = *KDP_MEMBER (kdp, d_off); | |
128 | memcpy (((char *) dp + offsetof (struct dirent64, d_reclen)), | |
129 | &new_reclen, sizeof (new_reclen)); | |
130 | dp->d_type = *((char *) kdp + k_reclen - 1); | |
298d0e31 | 131 | memcpy (dp->d_name, kdp->d_name, |
edcda4c0 | 132 | k_reclen - offsetof (struct kernel_dirent, d_name)); |
298d0e31 AZ |
133 | |
134 | dp = (struct dirent64 *) ((char *) dp + new_reclen); | |
edcda4c0 | 135 | kdp = (struct kernel_dirent *) (((char *) kdp) + k_reclen); |
298d0e31 AZ |
136 | } |
137 | ||
edcda4c0 | 138 | return (char *) dp - (char *) buf; |
298d0e31 | 139 | } |
51ea67d5 FW |
140 | libc_hidden_def (__getdents64) |
141 | weak_alias (__getdents64, getdents64) | |
142 | ||
50511ca4 | 143 | #if _DIRENT_MATCHES_DIRENT64 |
298d0e31 | 144 | strong_alias (__getdents64, __getdents) |
50511ca4 | 145 | #endif |