diff options
author | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2018-02-28 15:37:17 -0300 |
---|---|---|
committer | Adhemerval Zanella <adhemerval.zanella@linaro.org> | 2018-04-19 08:49:52 -0300 |
commit | 298d0e3129c0b5137f4989275b13fe30d0733c4d (patch) | |
tree | cb5fa271d08e03912b9aebd3caea853ee82a29e1 /sysdeps/unix/sysv/linux/mips/mips64/getdents64.c | |
parent | 0085be1415a38b40a5a1a12e49368498f1687380 (diff) | |
download | glibc-298d0e3129c0b5137f4989275b13fe30d0733c4d.tar glibc-298d0e3129c0b5137f4989275b13fe30d0733c4d.tar.gz glibc-298d0e3129c0b5137f4989275b13fe30d0733c4d.tar.bz2 glibc-298d0e3129c0b5137f4989275b13fe30d0733c4d.zip |
Consolidate Linux getdents{64} implementation
This patch consolidates Linux getdents{64} implementation on just
the default sysdeps/unix/sysv/linux/getdents{64}{_r}.c ones.
Although this symbol is used only internally, the non-LFS version
still need to be build due the non-LFS getdirentries which requires
its semantic.
The non-LFS default implementation now uses the wordsize-32 as base
which uses getdents64 syscall plus adjustment for overflow (it allows
to use the same code for architectures that does not support non-LFS
getdents syscall). It has two main differences to wordsize-32 one:
- DIRENT_SET_DP_INO is added to handle alpha requirement to zero
the padding.
- alloca is removed by allocating a bounded temporary buffer (it
increases stack usage by roughly 276 bytes).
The default implementation handle the Linux requirements:
* getdents is only built for _DIRENT_MATCHES_DIRENT64 being 0.
* getdents64 is always built and aliased to getdents for ABIs
that define _DIRENT_MATCHES_DIRENT64 to 1.
* A compat symbol is added for getdents64 for ABI that used to
export the old non-LFS version.
Checked on aarch64-linux-gnu, x86_64-linux-gnu, i686-linux-gnu,
sparcv9-linux-gnu, sparc64-linux-gnu, powerpc-linux-gnu, and
powerpc64le-linux-gnu.
* sysdeps/unix/sysv/linux/alpha/getdents.c: Add comments with alpha
requirements.
(_DIRENT_MATCHES_DIRENT64): Undef
* sysdeps/unix/sysv/linux/alpha/getdents64.c: Likewise.
* sysdeps/unix/sysv/linux/arm/getdents64.c: Remove file.
* sysdeps/unix/sysv/linux/generic/getdents.c: Likewise.
* sysdeps/unix/sysv/linux/generic/getdents64.c: Likewise.
* sysdeps/unix/sysv/linux/generic/wordsize-32/getdents.c: Likewise.
* sysdeps/unix/sysv/linux/getdents.c: Simplify implementation by
use getdents64 syscalls as base.
* sysdeps/unix/sysv/linux/getdents64.c: Likewise and add compatibility
symbol if required.
* sysdeps/unix/sysv/linux/hppa/getdents64.c: Likewise.
* sysdeps/unix/sysv/linux/i386/getdents64.c: Likewise.
* sysdeps/unix/sysv/linux/m68k/getdents64.c: Likewise.
* sysdeps/unix/sysv/linux/powerpc/getdents64.c: Likewise.
* sysdeps/unix/sysv/linux/s390/s390-32/getdents64.c: Likewise.
* sysdeps/unix/sysv/linux/sparc/sparc32/getdents64.c: Likewise.
* sysdeps/unix/sysv/linux/wordsize-64/getdents.c: Likewise.
* sysdeps/unix/sysv/linux/wordsize-64/getdents64.c: Likewise.
* sysdeps/unix/sysv/linux/sparc/sparc64/get_clockfreq.c
(__get_clockfreq_via_proc_openprom): Use __getdents64.
* sysdeps/unix/sysv/linux/mips/mips64/getdents64.c: New file.
Diffstat (limited to 'sysdeps/unix/sysv/linux/mips/mips64/getdents64.c')
-rw-r--r-- | sysdeps/unix/sysv/linux/mips/mips64/getdents64.c | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/sysdeps/unix/sysv/linux/mips/mips64/getdents64.c b/sysdeps/unix/sysv/linux/mips/mips64/getdents64.c new file mode 100644 index 0000000000..63afb2667e --- /dev/null +++ b/sysdeps/unix/sysv/linux/mips/mips64/getdents64.c @@ -0,0 +1,110 @@ +/* Get directory entries. Linux/MIPSn64 LFS version. + Copyright (C) 2018 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library. If not, see + <http://www.gnu.org/licenses/>. */ + +#include <string.h> +#include <dirent.h> +#include <errno.h> +#include <assert.h> +#include <sys/param.h> +#include <unistd.h> +#include <scratch_buffer.h> + +ssize_t +__getdents64 (int fd, char *buf, size_t nbytes) +{ +#ifdef __NR_getdents64 + ssize_t ret = INLINE_SYSCALL_CALL (getdents64, fd, buf, nbytes); + if (ret != -1) + return ret; +#endif + + /* Unfortunately getdents64 was only wire-up for MIPS n64 on Linux 3.10. + If syscall is not available it need to fallback to non-LFS one. */ + + struct kernel_dirent + { + unsigned long d_ino; + unsigned long d_off; + unsigned short int d_reclen; + char d_name[256]; + }; + + const size_t size_diff = (offsetof (struct dirent64, d_name) + - offsetof (struct kernel_dirent, d_name)); + + size_t red_nbytes = MIN (nbytes + - ((nbytes / (offsetof (struct dirent64, d_name) + + 14)) * size_diff), + nbytes - size_diff); + + struct scratch_buffer tmpbuf; + scratch_buffer_init (&tmpbuf); + if (!scratch_buffer_set_array_size (&tmpbuf, red_nbytes, sizeof (uint8_t))) + INLINE_SYSCALL_ERROR_RETURN_VALUE (ENOMEM); + + struct kernel_dirent *skdp, *kdp; + skdp = kdp = tmpbuf.data; + + ssize_t retval = INLINE_SYSCALL_CALL (getdents, fd, kdp, red_nbytes); + if (retval == -1) + { + scratch_buffer_free (&tmpbuf); + return -1; + } + + off64_t last_offset = -1; + struct dirent64 *dp = (struct dirent64 *) buf; + while ((char *) kdp < (char *) skdp + retval) + { + const size_t alignment = _Alignof (struct dirent64); + /* Since kdp->d_reclen is already aligned for the kernel structure + this may compute a value that is bigger than necessary. */ + size_t new_reclen = ((kdp->d_reclen + size_diff + alignment - 1) + & ~(alignment - 1)); + if ((char *) dp + new_reclen > buf + nbytes) + { + /* Our heuristic failed. We read too many entries. Reset + the stream. */ + assert (last_offset != -1); + __lseek64 (fd, last_offset, SEEK_SET); + + if ((char *) dp == buf) + { + scratch_buffer_free (&tmpbuf); + return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL); + } + + break; + } + + last_offset = kdp->d_off; + dp->d_ino = kdp->d_ino; + dp->d_off = kdp->d_off; + dp->d_reclen = new_reclen; + dp->d_type = *((char *) kdp + kdp->d_reclen - 1); + memcpy (dp->d_name, kdp->d_name, + kdp->d_reclen - offsetof (struct kernel_dirent, d_name)); + + dp = (struct dirent64 *) ((char *) dp + new_reclen); + kdp = (struct kernel_dirent *) (((char *) kdp) + kdp->d_reclen); + } + + scratch_buffer_free (&tmpbuf); + return (char *) dp - buf; +} +strong_alias (__getdents64, __getdents) |