1/* Get file status. Linux version.
2 Copyright (C) 2020-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#define __fstatat __redirect___fstatat
20#define fstatat __redirect_fstatat
21#include <sys/stat.h>
22#include <fcntl.h>
23#include <string.h>
24#include <kernel_stat.h>
25#include <sysdep.h>
26#include <time.h>
27#include <kstat_cp.h>
28#include <stat_t64_cp.h>
29#include <sys/sysmacros.h>
30
31#if __TIMESIZE == 64 \
32 && (__WORDSIZE == 32 \
33 && (!defined __SYSCALL_WORDSIZE || __SYSCALL_WORDSIZE == 32))
34/* Sanity check to avoid newer 32-bit ABI to support non-LFS calls. */
35_Static_assert (sizeof (__off_t) == sizeof (__off64_t),
36 "__blkcnt_t and __blkcnt64_t must match");
37_Static_assert (sizeof (__ino_t) == sizeof (__ino64_t),
38 "__blkcnt_t and __blkcnt64_t must match");
39_Static_assert (sizeof (__blkcnt_t) == sizeof (__blkcnt64_t),
40 "__blkcnt_t and __blkcnt64_t must match");
41#endif
42
43#if (__WORDSIZE == 32 \
44 && (!defined __SYSCALL_WORDSIZE || __SYSCALL_WORDSIZE == 32)) \
45 || defined STAT_HAS_TIME32 \
46 || (!defined __NR_newfstatat && !defined __NR_fstatat64)
47# define FSTATAT_USE_STATX 1
48
49static inline int
50fstatat64_time64_statx (int fd, const char *file, struct __stat64_t64 *buf,
51 int flag)
52{
53 /* 32-bit kABI with default 64-bit time_t, e.g. arc, riscv32. Also
54 64-bit time_t support is done through statx syscall. */
55 struct statx tmp;
56 int r = INTERNAL_SYSCALL_CALL (statx, fd, file, AT_NO_AUTOMOUNT | flag,
57 STATX_BASIC_STATS, &tmp);
58 if (r != 0)
59 return r;
60
61 *buf = (struct __stat64_t64) {
62 .st_dev = __gnu_dev_makedev (tmp.stx_dev_major, tmp.stx_dev_minor),
63 .st_rdev = __gnu_dev_makedev (tmp.stx_rdev_major, tmp.stx_rdev_minor),
64 .st_ino = tmp.stx_ino,
65 .st_mode = tmp.stx_mode,
66 .st_nlink = tmp.stx_nlink,
67 .st_uid = tmp.stx_uid,
68 .st_gid = tmp.stx_gid,
69 .st_atime = tmp.stx_atime.tv_sec,
70 .st_atim.tv_nsec = tmp.stx_atime.tv_nsec,
71 .st_mtime = tmp.stx_mtime.tv_sec,
72 .st_mtim.tv_nsec = tmp.stx_mtime.tv_nsec,
73 .st_ctime = tmp.stx_ctime.tv_sec,
74 .st_ctim.tv_nsec = tmp.stx_ctime.tv_nsec,
75 .st_size = tmp.stx_size,
76 .st_blocks = tmp.stx_blocks,
77 .st_blksize = tmp.stx_blksize,
78 };
79
80 return r;
81}
82#else
83# define FSTATAT_USE_STATX 0
84#endif
85
86/* Only statx supports 64-bit timestamps for 32-bit architectures with
87 __ASSUME_STATX, so there is no point in building the fallback. */
88#if !FSTATAT_USE_STATX || (FSTATAT_USE_STATX && !defined __ASSUME_STATX)
89static inline int
90fstatat64_time64_stat (int fd, const char *file, struct __stat64_t64 *buf,
91 int flag)
92{
93 int r;
94
95#if XSTAT_IS_XSTAT64
96# ifdef __NR_newfstatat
97 /* 64-bit kABI, e.g. aarch64, ia64, powerpc64*, s390x, riscv64, and
98 x86_64. */
99 r = INTERNAL_SYSCALL_CALL (newfstatat, fd, file, buf, flag);
100# elif defined __NR_fstatat64
101# if STAT64_IS_KERNEL_STAT64
102 /* 64-bit kABI outlier, e.g. alpha */
103 r = INTERNAL_SYSCALL_CALL (fstatat64, fd, file, buf, flag);
104# else
105 /* 64-bit kABI outlier, e.g. sparc64. */
106 struct kernel_stat64 kst64;
107 r = INTERNAL_SYSCALL_CALL (fstatat64, fd, file, &kst64, flag);
108 if (r == 0)
109 __cp_stat64_kstat64 (buf, &kst64);
110# endif
111# endif
112#else
113# ifdef __NR_fstatat64
114 /* All kABIs with non-LFS support and with old 32-bit time_t support
115 e.g. arm, csky, i386, hppa, m68k, microblaze, nios2, sh, powerpc32,
116 and sparc32. */
117 struct stat64 st64;
118 r = INTERNAL_SYSCALL_CALL (fstatat64, fd, file, &st64, flag);
119 if (r == 0)
120 {
121 /* Clear both pad and reserved fields. */
122 memset (buf, 0, sizeof (*buf));
123
124 buf->st_dev = st64.st_dev,
125 buf->st_ino = st64.st_ino;
126 buf->st_mode = st64.st_mode;
127 buf->st_nlink = st64.st_nlink;
128 buf->st_uid = st64.st_uid;
129 buf->st_gid = st64.st_gid;
130 buf->st_rdev = st64.st_rdev;
131 buf->st_size = st64.st_size;
132 buf->st_blksize = st64.st_blksize;
133 buf->st_blocks = st64.st_blocks;
134 buf->st_atim = valid_timespec_to_timespec64 (st64.st_atim);
135 buf->st_mtim = valid_timespec_to_timespec64 (st64.st_mtim);
136 buf->st_ctim = valid_timespec_to_timespec64 (st64.st_ctim);
137 }
138# else
139 /* 64-bit kabi outlier, e.g. mips64 and mips64-n32. */
140 struct kernel_stat kst;
141 r = INTERNAL_SYSCALL_CALL (newfstatat, fd, file, &kst, flag);
142 if (r == 0)
143 __cp_kstat_stat64_t64 (&kst, buf);
144# endif
145#endif
146
147 return r;
148}
149#endif
150
151int
152__fstatat64_time64 (int fd, const char *file, struct __stat64_t64 *buf,
153 int flag)
154{
155 int r;
156
157#if FSTATAT_USE_STATX
158 r = fstatat64_time64_statx (fd, file, buf, flag);
159# ifndef __ASSUME_STATX
160 if (r == -ENOSYS)
161 r = fstatat64_time64_stat (fd, file, buf, flag);
162# endif
163#else
164 r = fstatat64_time64_stat (fd, file, buf, flag);
165#endif
166
167 return INTERNAL_SYSCALL_ERROR_P (r)
168 ? INLINE_SYSCALL_ERROR_RETURN_VALUE (-r)
169 : 0;
170}
171#if __TIMESIZE != 64
172hidden_def (__fstatat64_time64)
173
174int
175__fstatat64 (int fd, const char *file, struct stat64 *buf, int flags)
176{
177 struct __stat64_t64 st_t64;
178 return __fstatat64_time64 (fd, file, &st_t64, flags)
179 ?: __cp_stat64_t64_stat64 (&st_t64, buf);
180}
181#endif
182
183#undef __fstatat
184#undef fstatat
185
186hidden_def (__fstatat64)
187weak_alias (__fstatat64, fstatat64)
188
189#if XSTAT_IS_XSTAT64
190strong_alias (__fstatat64, __fstatat)
191weak_alias (__fstatat64, fstatat)
192strong_alias (__fstatat64, __GI___fstatat);
193#endif
194