1 | /* Get directory entries. Linux/MIPSn64 LFS version. |
2 | Copyright (C) 2018-2022 Free Software Foundation, Inc. |
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 |
17 | <https://www.gnu.org/licenses/>. */ |
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> |
25 | #include <limits.h> |
26 | |
27 | #include <include/libc-pointer-arith.h> |
28 | |
29 | ssize_t |
30 | __getdents64 (int fd, void *buf, size_t nbytes) |
31 | { |
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 | |
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 | } |
46 | |
47 | /* Unfortunately getdents64 was only wire-up for MIPS n64 on Linux 3.10. |
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. */ |
53 | |
54 | struct kernel_dirent |
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; |
75 | |
76 | const size_t size_diff = (offsetof (struct dirent64, d_name) |
77 | - offsetof (struct kernel_dirent, d_name)); |
78 | |
79 | struct dirent64 *dp = (struct dirent64 *) buf; |
80 | |
81 | size_t nb = 0; |
82 | off64_t last_offset = -1; |
83 | |
84 | ssize_t r = INLINE_SYSCALL_CALL (getdents, fd, kbuf, kbuf_size); |
85 | if (r <= 0) |
86 | return r; |
87 | |
88 | struct kernel_dirent *skdp, *kdp; |
89 | skdp = kdp = (struct kernel_dirent *) kbuf; |
90 | |
91 | while ((char *) kdp < (char *) skdp + r) |
92 | { |
93 | /* This is a conservative approximation, since some of size_diff might |
94 | fit into the existing padding for alignment. */ |
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, |
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) |
106 | { |
107 | __set_errno (EINVAL); |
108 | return -1; |
109 | } |
110 | |
111 | /* The new entry will overflow the input buffer, rewind to last |
112 | obtained entry and return. */ |
113 | __lseek64 (fd: fd, offset: last_offset, SEEK_SET); |
114 | return (char *) dp - (char *) buf; |
115 | } |
116 | nb += new_reclen; |
117 | |
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. */ |
126 | memcpy (dp->d_name, kdp->d_name, |
127 | kdirent.d_reclen - offsetof (struct kernel_dirent, d_name)); |
128 | |
129 | last_offset = kdirent.d_off; |
130 | |
131 | dp = (struct dirent64 *) ((char *) dp + new_reclen); |
132 | kdp = (struct kernel_dirent *) (((char *) kdp) + kdirent.d_reclen); |
133 | } |
134 | |
135 | return (char *) dp - (char *) buf; |
136 | } |
137 | libc_hidden_def (__getdents64) |
138 | weak_alias (__getdents64, getdents64) |
139 | |
140 | #if _DIRENT_MATCHES_DIRENT64 |
141 | strong_alias (__getdents64, __getdents) |
142 | #endif |
143 | |