blob: 2ff0ac7d47754cac0a8c4c5d8a48237ec8729a0e [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00006 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000029 */
30
31#include "defs.h"
Roland McGrathc531e572008-08-01 01:13:10 +000032
Dmitry V. Levinf19836b2014-12-30 11:18:47 +000033#undef dev_t
34#undef ino_t
35#undef mode_t
36#undef nlink_t
37#undef uid_t
38#undef gid_t
39#undef off_t
40#undef loff_t
41#define dev_t __kernel_dev_t
42#define ino_t __kernel_ino_t
43#define mode_t __kernel_mode_t
44#define nlink_t __kernel_nlink_t
45#define uid_t __kernel_uid_t
46#define gid_t __kernel_gid_t
47#define off_t __kernel_off_t
48#define loff_t __kernel_loff_t
Wichert Akkermana6013701999-07-08 14:00:58 +000049
Dmitry V. Levinc31481f2015-12-04 14:37:02 +000050#include "asm_stat.h"
Wichert Akkermana6013701999-07-08 14:00:58 +000051
Dmitry V. Levinf19836b2014-12-30 11:18:47 +000052#undef dev_t
53#undef ino_t
54#undef mode_t
55#undef nlink_t
56#undef uid_t
57#undef gid_t
58#undef off_t
59#undef loff_t
60#define dev_t dev_t
61#define ino_t ino_t
62#define mode_t mode_t
63#define nlink_t nlink_t
64#define uid_t uid_t
65#define gid_t gid_t
66#define off_t off_t
67#define loff_t loff_t
Denys Vlasenko84703742012-02-25 02:38:52 +010068
Dmitry V. Levinf19836b2014-12-30 11:18:47 +000069/* for S_IFMT */
Denys Vlasenko84703742012-02-25 02:38:52 +010070#define stat libc_stat
71#define stat64 libc_stat64
72#include <sys/stat.h>
73#undef stat
74#undef stat64
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +010075/* These might be macros. */
Denys Vlasenko84703742012-02-25 02:38:52 +010076#undef st_atime
77#undef st_mtime
78#undef st_ctime
Wichert Akkermand4d8e921999-04-18 23:30:29 +000079
Dmitry V. Levinf19836b2014-12-30 11:18:47 +000080#if defined MAJOR_IN_SYSMACROS
Denys Vlasenko84703742012-02-25 02:38:52 +010081# include <sys/sysmacros.h>
Dmitry V. Levinf19836b2014-12-30 11:18:47 +000082#elif defined MAJOR_IN_MKDEV
Denys Vlasenko84703742012-02-25 02:38:52 +010083# include <sys/mkdev.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000084#endif
85
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000086/* several stats */
87
Dmitry V. Levin721de782015-01-08 02:59:04 +000088#include "printstat.h"
89
Dmitry V. Levin525eed32015-08-08 12:12:13 +000090/* all locally defined structures provide these fields */
91#undef HAVE_STRUCT_STAT_ST_ATIME_NSEC
92#define HAVE_STRUCT_STAT_ST_ATIME_NSEC 1
93#undef HAVE_STRUCT_STAT_ST_CTIME_NSEC
94#define HAVE_STRUCT_STAT_ST_CTIME_NSEC 1
95#undef HAVE_STRUCT_STAT_ST_MTIME_NSEC
96#define HAVE_STRUCT_STAT_ST_MTIME_NSEC 1
97
Dmitry V. Levinf19836b2014-12-30 11:18:47 +000098#undef STAT32_PERSONALITY
99#if SUPPORTED_PERSONALITIES > 1
Dmitry V. Levin09dee9a2016-08-09 09:50:13 +0000100# include "stat32.h"
101#endif
Andreas Schwabd69fa492010-07-12 21:39:57 +0200102
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000103#ifdef STAT32_PERSONALITY
Dmitry V. Levin721de782015-01-08 02:59:04 +0000104# define DO_PRINTSTAT do_printstat32
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000105# define STRUCT_STAT struct stat32
Dmitry V. Levin721de782015-01-08 02:59:04 +0000106# include "printstat.h"
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000107#endif /* STAT32_PERSONALITY */
Andreas Schwabd69fa492010-07-12 21:39:57 +0200108
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000109static void
Denys Vlasenko1d632462009-04-14 12:51:00 +0000110printstat(struct tcb *tcp, long addr)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000111{
Wichert Akkerman328c5e71999-04-16 00:21:26 +0000112 struct stat statbuf;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000113
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000114#ifdef STAT32_PERSONALITY
115 if (current_personality == STAT32_PERSONALITY) {
Dmitry V. Levinb6d80ff2015-07-20 12:09:44 +0000116 struct stat32 statbuf;
117
118 if (!umove_or_printaddr(tcp, addr, &statbuf))
119 do_printstat32(tcp, &statbuf);
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000120 return;
121 }
122#endif
123
Dmitry V. Levinb6d80ff2015-07-20 12:09:44 +0000124 if (!umove_or_printaddr(tcp, addr, &statbuf))
125 do_printstat(tcp, &statbuf);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000126}
127
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000128SYS_FUNC(stat)
Dmitry V. Levinc6ee0ec2015-01-08 01:14:53 +0000129{
130 if (entering(tcp)) {
131 printpath(tcp, tcp->u_arg[0]);
132 tprints(", ");
133 } else {
134 printstat(tcp, tcp->u_arg[1]);
135 }
136 return 0;
137}
138
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000139SYS_FUNC(fstat)
Dmitry V. Levinc6ee0ec2015-01-08 01:14:53 +0000140{
141 if (entering(tcp)) {
142 printfd(tcp, tcp->u_arg[0]);
143 tprints(", ");
144 } else {
145 printstat(tcp, tcp->u_arg[1]);
146 }
147 return 0;
148}
149
Dmitry V. Levin7b9bc442015-01-08 04:06:06 +0000150#if defined STAT32_PERSONALITY && !defined HAVE_STRUCT_STAT64
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000151# if defined AARCH64 || defined X86_64 || defined X32
Roland McGrathe6d0f712007-08-07 01:22:49 +0000152/*
Dmitry V. Levin8d411082014-12-28 16:41:24 +0000153 * Linux x86_64 and x32 have unified `struct stat' but their i386 personality
154 * needs `struct stat64'.
155 * linux/arch/x86/include/uapi/asm/stat.h defines `struct stat64' only for i386.
Elliott Hughes391c0d82014-04-03 17:50:14 -0700156 *
157 * Similarly, aarch64 has a unified `struct stat' but its arm personality
Dmitry V. Levin8d411082014-12-28 16:41:24 +0000158 * needs `struct stat64' (unlike x86, it shouldn't be packed).
Roland McGrathe6d0f712007-08-07 01:22:49 +0000159 */
160struct stat64 {
161 unsigned long long st_dev;
162 unsigned char __pad0[4];
163 unsigned int __st_ino;
164 unsigned int st_mode;
165 unsigned int st_nlink;
166 unsigned int st_uid;
167 unsigned int st_gid;
168 unsigned long long st_rdev;
169 unsigned char __pad3[4];
170 long long st_size;
171 unsigned int st_blksize;
172 unsigned long long st_blocks;
173 unsigned int st_atime;
174 unsigned int st_atime_nsec;
175 unsigned int st_mtime;
176 unsigned int st_mtime_nsec;
177 unsigned int st_ctime;
178 unsigned int st_ctime_nsec;
179 unsigned long long st_ino;
Elliott Hughes391c0d82014-04-03 17:50:14 -0700180}
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000181# if defined X86_64 || defined X32
Dmitry V. Levin5647cf82015-03-29 22:45:03 +0000182 ATTRIBUTE_PACKED
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000183# define STAT64_SIZE 96
184# else
185# define STAT64_SIZE 104
186# endif
Elliott Hughes391c0d82014-04-03 17:50:14 -0700187;
Dmitry V. Levin7b9bc442015-01-08 04:06:06 +0000188# define HAVE_STRUCT_STAT64 1
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000189# else /* !(AARCH64 || X86_64 || X32) */
190# warning FIXME: check whether struct stat64 definition is needed for this architecture!
191# endif
Dmitry V. Levin7b9bc442015-01-08 04:06:06 +0000192#endif /* STAT32_PERSONALITY && !HAVE_STRUCT_STAT64 */
Roland McGrathe6d0f712007-08-07 01:22:49 +0000193
Dmitry V. Levin7b9bc442015-01-08 04:06:06 +0000194#ifdef HAVE_STRUCT_STAT64
Dmitry V. Levinb8ad3932014-12-28 18:56:23 +0000195
196# define DO_PRINTSTAT do_printstat64
197# define STRUCT_STAT struct stat64
Dmitry V. Levinb8ad3932014-12-28 18:56:23 +0000198# include "printstat.h"
199
Wichert Akkerman328c5e71999-04-16 00:21:26 +0000200static void
Denys Vlasenko1d632462009-04-14 12:51:00 +0000201printstat64(struct tcb *tcp, long addr)
Ulrich Drepper7f02c4d1999-12-24 08:01:34 +0000202{
203 struct stat64 statbuf;
204
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000205# ifdef STAT64_SIZE
Roland McGrathe6d0f712007-08-07 01:22:49 +0000206 (void) sizeof(char[sizeof statbuf == STAT64_SIZE ? 1 : -1]);
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000207# endif
Roland McGrathe6d0f712007-08-07 01:22:49 +0000208
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000209# ifdef STAT32_PERSONALITY
210 if (current_personality != STAT32_PERSONALITY) {
Elliott Hughes391c0d82014-04-03 17:50:14 -0700211 printstat(tcp, addr);
212 return;
213 }
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000214# endif /* STAT32_PERSONALITY */
Dmitry V. Levinff896f72009-10-21 13:43:57 +0000215
Dmitry V. Levinb6d80ff2015-07-20 12:09:44 +0000216 if (!umove_or_printaddr(tcp, addr, &statbuf))
217 do_printstat64(tcp, &statbuf);
Ulrich Drepper7f02c4d1999-12-24 08:01:34 +0000218}
219
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000220SYS_FUNC(stat64)
Ulrich Drepper7f02c4d1999-12-24 08:01:34 +0000221{
Ulrich Drepper7f02c4d1999-12-24 08:01:34 +0000222 if (entering(tcp)) {
223 printpath(tcp, tcp->u_arg[0]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200224 tprints(", ");
Ulrich Drepper7f02c4d1999-12-24 08:01:34 +0000225 } else {
226 printstat64(tcp, tcp->u_arg[1]);
227 }
228 return 0;
Ulrich Drepper7f02c4d1999-12-24 08:01:34 +0000229}
230
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000231SYS_FUNC(fstat64)
Dmitry V. Levinc6ee0ec2015-01-08 01:14:53 +0000232{
Dmitry V. Levinc6ee0ec2015-01-08 01:14:53 +0000233 if (entering(tcp)) {
234 printfd(tcp, tcp->u_arg[0]);
235 tprints(", ");
236 } else {
237 printstat64(tcp, tcp->u_arg[1]);
238 }
239 return 0;
Dmitry V. Levinc6ee0ec2015-01-08 01:14:53 +0000240}
241
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000242#else
243
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000244SYS_FUNC(stat64)
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000245{
246 return sys_stat(tcp);
247}
248
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000249SYS_FUNC(fstat64)
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000250{
251 return sys_fstat(tcp);
252}
253
Dmitry V. Levin7b9bc442015-01-08 04:06:06 +0000254#endif /* HAVE_STRUCT_STAT64 */
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000255
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000256SYS_FUNC(newfstatat)
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000257{
258 if (entering(tcp)) {
Dmitry V. Levin31382132011-03-04 05:08:02 +0300259 print_dirfd(tcp, tcp->u_arg[0]);
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000260 printpath(tcp, tcp->u_arg[1]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200261 tprints(", ");
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000262 } else {
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000263#if defined STAT32_PERSONALITY
264 if (current_personality == STAT32_PERSONALITY)
Andreas Schwabd69fa492010-07-12 21:39:57 +0200265 printstat64(tcp, tcp->u_arg[2]);
Dmitry V. Levinf19836b2014-12-30 11:18:47 +0000266 else
267 printstat(tcp, tcp->u_arg[2]);
Dmitry V. Levin7b9bc442015-01-08 04:06:06 +0000268#elif defined HAVE_STRUCT_STAT64
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000269 printstat64(tcp, tcp->u_arg[2]);
270#else
271 printstat(tcp, tcp->u_arg[2]);
Dmitry V. Levin7b9bc442015-01-08 04:06:06 +0000272#endif /* STAT32_PERSONALITY || HAVE_STRUCT_STAT64 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200273 tprints(", ");
Dmitry V. Levin7989ad42012-03-13 23:26:01 +0000274 printflags(at_flags, tcp->u_arg[3], "AT_???");
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000275 }
276 return 0;
277}
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000278
Denys Vlasenko8435d672013-02-18 15:47:57 +0100279#if defined(HAVE_STRUCT___OLD_KERNEL_STAT)
Dmitry V. Levin1fb1c132014-12-28 19:10:58 +0000280
281static void
282convertoldstat(const struct __old_kernel_stat *oldbuf, struct stat *newbuf)
283{
284 memset(newbuf, 0, sizeof(*newbuf));
285 newbuf->st_dev = oldbuf->st_dev;
286 newbuf->st_ino = oldbuf->st_ino;
287 newbuf->st_mode = oldbuf->st_mode;
288 newbuf->st_nlink = oldbuf->st_nlink;
289 newbuf->st_uid = oldbuf->st_uid;
290 newbuf->st_gid = oldbuf->st_gid;
291 newbuf->st_rdev = oldbuf->st_rdev;
292 newbuf->st_size = oldbuf->st_size;
293 newbuf->st_atime = oldbuf->st_atime;
294 newbuf->st_mtime = oldbuf->st_mtime;
295 newbuf->st_ctime = oldbuf->st_ctime;
296}
297
298static void
299printoldstat(struct tcb *tcp, long addr)
300{
301 struct __old_kernel_stat statbuf;
302 struct stat newstatbuf;
303
Dmitry V. Levinb6d80ff2015-07-20 12:09:44 +0000304 if (!umove_or_printaddr(tcp, addr, &statbuf)) {
305 convertoldstat(&statbuf, &newstatbuf);
306 do_printstat(tcp, &newstatbuf);
Dmitry V. Levin1fb1c132014-12-28 19:10:58 +0000307 }
Dmitry V. Levin1fb1c132014-12-28 19:10:58 +0000308}
309
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000310SYS_FUNC(oldstat)
Dmitry V. Levin1fb1c132014-12-28 19:10:58 +0000311{
312 if (entering(tcp)) {
313 printpath(tcp, tcp->u_arg[0]);
314 tprints(", ");
315 } else {
316 printoldstat(tcp, tcp->u_arg[1]);
317 }
318 return 0;
319}
320
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000321SYS_FUNC(oldfstat)
Wichert Akkerman328c5e71999-04-16 00:21:26 +0000322{
Dmitry V. Levin31382132011-03-04 05:08:02 +0300323 if (entering(tcp)) {
324 printfd(tcp, tcp->u_arg[0]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200325 tprints(", ");
Dmitry V. Levin31382132011-03-04 05:08:02 +0300326 } else {
Wichert Akkerman328c5e71999-04-16 00:21:26 +0000327 printoldstat(tcp, tcp->u_arg[1]);
328 }
329 return 0;
330}
Dmitry V. Levin1fb1c132014-12-28 19:10:58 +0000331
332#endif /* HAVE_STRUCT___OLD_KERNEL_STAT */
Wichert Akkerman328c5e71999-04-16 00:21:26 +0000333
Denys Vlasenko9472a272013-02-12 11:43:46 +0100334#if defined(SPARC) || defined(SPARC64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000335
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000336SYS_FUNC(xstat)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000337{
338 if (entering(tcp)) {
339 tprintf("%ld, ", tcp->u_arg[0]);
340 printpath(tcp, tcp->u_arg[1]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200341 tprints(", ");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000342 } else {
343 printstat(tcp, tcp->u_arg[2]);
344 }
345 return 0;
346}
347
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000348SYS_FUNC(fxstat)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000349{
Dmitry V. Levine00a9572014-12-29 03:15:42 +0000350 if (entering(tcp)) {
351 tprintf("%ld, ", tcp->u_arg[0]);
352 printfd(tcp, tcp->u_arg[1]);
353 tprints(", ");
354 } else {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000355 printstat(tcp, tcp->u_arg[2]);
356 }
357 return 0;
358}
359
Dmitry V. Levin964d80a2014-12-06 03:53:16 +0000360#endif /* SPARC || SPARC64 */