| 1 | /* Get file status. Linux version. |
| 2 | Copyright (C) 2020-2021 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 <sys/stat.h> |
| 20 | #include <kernel_stat.h> |
| 21 | #include <sysdep.h> |
| 22 | |
| 23 | #if !XSTAT_IS_XSTAT64 |
| 24 | # include <kstat_cp.h> |
| 25 | |
| 26 | int |
| 27 | __fstatat (int fd, const char *file, struct stat *buf, int flag) |
| 28 | { |
| 29 | int r; |
| 30 | |
| 31 | # if STAT_IS_KERNEL_STAT |
| 32 | /* New kABIs which uses generic pre 64-bit time Linux ABI, e.g. |
| 33 | csky, nios2 */ |
| 34 | r = INTERNAL_SYSCALL_CALL (fstatat64, fd, file, buf, flag); |
| 35 | if (r == 0 && (buf->__st_ino_pad != 0 |
| 36 | || buf->__st_size_pad != 0 |
| 37 | || buf->__st_blocks_pad != 0)) |
| 38 | return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW); |
| 39 | # else |
| 40 | # ifdef __NR_fstatat64 |
| 41 | /* Old KABIs with old non-LFS support, e.g. arm, i386, hppa, m68k, mips32, |
| 42 | microblaze, s390, sh, powerpc, and sparc. */ |
| 43 | struct stat64 st64; |
| 44 | r = INTERNAL_SYSCALL_CALL (fstatat64, fd, file, &st64, flag); |
| 45 | if (r == 0) |
| 46 | { |
| 47 | if (! in_ino_t_range (st64.st_ino) |
| 48 | || ! in_off_t_range (st64.st_size) |
| 49 | || ! in_blkcnt_t_range (st64.st_blocks)) |
| 50 | return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW); |
| 51 | |
| 52 | /* Clear internal pad and reserved fields. */ |
| 53 | memset (buf, 0, sizeof (*buf)); |
| 54 | |
| 55 | buf->st_dev = st64.st_dev, |
| 56 | buf->st_ino = st64.st_ino; |
| 57 | buf->st_mode = st64.st_mode; |
| 58 | buf->st_nlink = st64.st_nlink; |
| 59 | buf->st_uid = st64.st_uid; |
| 60 | buf->st_gid = st64.st_gid; |
| 61 | buf->st_rdev = st64.st_rdev; |
| 62 | buf->st_size = st64.st_size; |
| 63 | buf->st_blksize = st64.st_blksize; |
| 64 | buf->st_blocks = st64.st_blocks; |
| 65 | buf->st_atim.tv_sec = st64.st_atim.tv_sec; |
| 66 | buf->st_atim.tv_nsec = st64.st_atim.tv_nsec; |
| 67 | buf->st_mtim.tv_sec = st64.st_mtim.tv_sec; |
| 68 | buf->st_mtim.tv_nsec = st64.st_mtim.tv_nsec; |
| 69 | buf->st_ctim.tv_sec = st64.st_ctim.tv_sec; |
| 70 | buf->st_ctim.tv_nsec = st64.st_ctim.tv_nsec; |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | # else |
| 75 | /* 64-bit kabi outlier, e.g. mips64 and mips64-n32. */ |
| 76 | struct kernel_stat kst; |
| 77 | r = INTERNAL_SYSCALL_CALL (newfstatat, fd, file, &kst, flag); |
| 78 | if (r == 0) |
| 79 | r = __cp_kstat_stat (&kst, buf); |
| 80 | # endif /* __nr_fstatat64 */ |
| 81 | # endif /* STAT_IS_KERNEL_STAT */ |
| 82 | |
| 83 | return INTERNAL_SYSCALL_ERROR_P (r) |
| 84 | ? INLINE_SYSCALL_ERROR_RETURN_VALUE (-r) |
| 85 | : 0; |
| 86 | } |
| 87 | |
| 88 | weak_alias (__fstatat, fstatat) |
| 89 | #endif |
| 90 | |