]>
Commit | Line | Data |
---|---|---|
298d0e31 | 1 | /* Get directory entries. Linux/MIPSn64 LFS version. |
6d7e8eda | 2 | Copyright (C) 2018-2023 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 | ||
edcda4c0 AZ |
37 | static int getdents64_supported = true; |
38 | if (atomic_load_relaxed (&getdents64_supported)) | |
39 | { | |
40 | ssize_t ret = INLINE_SYSCALL_CALL (getdents64, fd, buf, nbytes); | |
41 | if (ret >= 0 || errno != ENOSYS) | |
42 | return ret; | |
43 | ||
44 | atomic_store_relaxed (&getdents64_supported, false); | |
45 | } | |
298d0e31 AZ |
46 | |
47 | /* Unfortunately getdents64 was only wire-up for MIPS n64 on Linux 3.10. | |
edcda4c0 AZ |
48 | If the syscall is not available it need to fallback to the non-LFS one. |
49 | Also to avoid an unbounded allocation through VLA/alloca or malloc (which | |
50 | would make the syscall non async-signal-safe) it uses a limited buffer. | |
51 | This is sub-optimal for large NBYTES, however this is a fallback | |
52 | mechanism to emulate a syscall that kernel should provide. */ | |
298d0e31 AZ |
53 | |
54 | struct kernel_dirent | |
edcda4c0 AZ |
55 | { |
56 | #if _MIPS_SIM == _ABI64 | |
57 | uint64_t d_ino; | |
58 | uint64_t d_off; | |
59 | #else | |
60 | uint32_t d_ino; | |
61 | uint32_t d_off; | |
62 | #endif | |
63 | unsigned short int d_reclen; | |
64 | char d_name[1]; | |
65 | }; | |
66 | ||
67 | /* The largest possible practical length of the d_name member are 255 | |
68 | Unicode characters in UTF-8 encoding, so d_name is 766 bytes long, plus | |
69 | 18 (mips64) / 10 (mips64n32) bytes from header, for total of 784 (mips64) | |
70 | / 776 (mips64n32) bytes total. Ensure that the minimum size holds at | |
71 | least one entry. */ | |
72 | enum { KBUF_SIZE = 1024 }; | |
73 | char kbuf[KBUF_SIZE]; | |
74 | size_t kbuf_size = nbytes < KBUF_SIZE ? nbytes : KBUF_SIZE; | |
298d0e31 AZ |
75 | |
76 | const size_t size_diff = (offsetof (struct dirent64, d_name) | |
77 | - offsetof (struct kernel_dirent, d_name)); | |
78 | ||
edcda4c0 | 79 | struct dirent64 *dp = (struct dirent64 *) buf; |
298d0e31 | 80 | |
edcda4c0 AZ |
81 | size_t nb = 0; |
82 | off64_t last_offset = -1; | |
298d0e31 | 83 | |
edcda4c0 AZ |
84 | ssize_t r = INLINE_SYSCALL_CALL (getdents, fd, kbuf, kbuf_size); |
85 | if (r <= 0) | |
86 | return r; | |
298d0e31 | 87 | |
edcda4c0 AZ |
88 | struct kernel_dirent *skdp, *kdp; |
89 | skdp = kdp = (struct kernel_dirent *) kbuf; | |
298d0e31 | 90 | |
edcda4c0 | 91 | while ((char *) kdp < (char *) skdp + r) |
298d0e31 | 92 | { |
edcda4c0 AZ |
93 | /* This is a conservative approximation, since some of size_diff might |
94 | fit into the existing padding for alignment. */ | |
42d62704 AZ |
95 | |
96 | /* Obtain the d_ino, d_off, and d_reclen from kernel filled buffer. */ | |
97 | struct kernel_dirent kdirent; | |
98 | memcpy (&kdirent, kdp, offsetof (struct kernel_dirent, d_name)); | |
99 | ||
100 | unsigned short int new_reclen = ALIGN_UP (kdirent.d_reclen + size_diff, | |
edcda4c0 AZ |
101 | _Alignof (struct dirent64)); |
102 | if (nb + new_reclen > nbytes) | |
103 | { | |
104 | /* Entry is too large for the fixed-size buffer. */ | |
105 | if (last_offset == -1) | |
298d0e31 | 106 | { |
edcda4c0 AZ |
107 | __set_errno (EINVAL); |
108 | return -1; | |
298d0e31 AZ |
109 | } |
110 | ||
edcda4c0 AZ |
111 | /* The new entry will overflow the input buffer, rewind to last |
112 | obtained entry and return. */ | |
113 | __lseek64 (fd, last_offset, SEEK_SET); | |
114 | return (char *) dp - (char *) buf; | |
298d0e31 | 115 | } |
edcda4c0 AZ |
116 | nb += new_reclen; |
117 | ||
42d62704 AZ |
118 | struct dirent64 d64; |
119 | d64.d_ino = kdirent.d_ino; | |
120 | d64.d_off = kdirent.d_off; | |
121 | d64.d_reclen = new_reclen; | |
122 | d64.d_type = *((char *) kdp + kdirent.d_reclen - 1); | |
123 | /* First copy only the header. */ | |
124 | memcpy (dp, &d64, offsetof (struct dirent64, d_name)); | |
125 | /* And then the d_name. */ | |
298d0e31 | 126 | memcpy (dp->d_name, kdp->d_name, |
42d62704 AZ |
127 | kdirent.d_reclen - offsetof (struct kernel_dirent, d_name)); |
128 | ||
129 | last_offset = kdirent.d_off; | |
298d0e31 AZ |
130 | |
131 | dp = (struct dirent64 *) ((char *) dp + new_reclen); | |
42d62704 | 132 | kdp = (struct kernel_dirent *) (((char *) kdp) + kdirent.d_reclen); |
298d0e31 AZ |
133 | } |
134 | ||
edcda4c0 | 135 | return (char *) dp - (char *) buf; |
298d0e31 | 136 | } |
51ea67d5 FW |
137 | libc_hidden_def (__getdents64) |
138 | weak_alias (__getdents64, getdents64) | |
139 | ||
50511ca4 | 140 | #if _DIRENT_MATCHES_DIRENT64 |
298d0e31 | 141 | strong_alias (__getdents64, __getdents) |
50511ca4 | 142 | #endif |