]> granicus.if.org Git - strace/blob - file.c
file.c: use umove_or_printaddr
[strace] / file.c
1 /*
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>
5  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6  * 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.
29  */
30
31 #include "defs.h"
32
33 #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
49
50 #include <asm/stat.h>
51
52 #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
68
69 /* for S_IFMT */
70 #define stat libc_stat
71 #define stat64 libc_stat64
72 #include <sys/stat.h>
73 #undef stat
74 #undef stat64
75 /* These might be macros. */
76 #undef st_atime
77 #undef st_mtime
78 #undef st_ctime
79
80 #if defined MAJOR_IN_SYSMACROS
81 # include <sys/sysmacros.h>
82 #elif defined MAJOR_IN_MKDEV
83 # include <sys/mkdev.h>
84 #endif
85
86 /* several stats */
87
88 #include "printstat.h"
89
90 #undef STAT32_PERSONALITY
91 #if SUPPORTED_PERSONALITIES > 1
92 # if defined AARCH64 || defined X86_64 || defined X32
93 struct stat32 {
94         unsigned int    st_dev;
95         unsigned int    st_ino;
96         unsigned short  st_mode;
97         unsigned short  st_nlink;
98         unsigned short  st_uid;
99         unsigned short  st_gid;
100         unsigned int    st_rdev;
101         unsigned int    st_size;
102         unsigned int    st_blksize;
103         unsigned int    st_blocks;
104         unsigned int    st_atime;
105         unsigned int    st_atime_nsec;
106         unsigned int    st_mtime;
107         unsigned int    st_mtime_nsec;
108         unsigned int    st_ctime;
109         unsigned int    st_ctime_nsec;
110         unsigned int    __unused4;
111         unsigned int    __unused5;
112 };
113 #  ifdef AARCH64
114 #   define STAT32_PERSONALITY 0
115 #  else
116 #   define STAT32_PERSONALITY 1
117 #  endif
118 # elif defined POWERPC64
119 struct stat32 {
120         unsigned int    st_dev;
121         unsigned int    st_ino;
122         unsigned int    st_mode;
123         unsigned short  st_nlink;
124         unsigned int    st_uid;
125         unsigned int    st_gid;
126         unsigned int    st_rdev;
127         unsigned int    st_size;
128         unsigned int    st_blksize;
129         unsigned int    st_blocks;
130         unsigned int    st_atime;
131         unsigned int    st_atime_nsec;
132         unsigned int    st_mtime;
133         unsigned int    st_mtime_nsec;
134         unsigned int    st_ctime;
135         unsigned int    st_ctime_nsec;
136         unsigned int    __unused4;
137         unsigned int    __unused5;
138 };
139 #  define STAT32_PERSONALITY 1
140 # elif defined SPARC64
141 struct stat32 {
142         unsigned short  st_dev;
143         unsigned int    st_ino;
144         unsigned short  st_mode;
145         unsigned short  st_nlink;
146         unsigned short  st_uid;
147         unsigned short  st_gid;
148         unsigned short  st_rdev;
149         unsigned int    st_size;
150         unsigned int    st_atime;
151         unsigned int    st_atime_nsec;
152         unsigned int    st_mtime;
153         unsigned int    st_mtime_nsec;
154         unsigned int    st_ctime;
155         unsigned int    st_ctime_nsec;
156         unsigned int    st_blksize;
157         unsigned int    st_blocks;
158         unsigned int    __unused4[2];
159 };
160 #  define STAT32_PERSONALITY 0
161 # elif defined SPARC
162 #  /* no 64-bit personalities */
163 # elif defined TILE
164 #  /* no 32-bit stat */
165 # else
166 #  warning FIXME: check whether struct stat32 definition is needed for this architecture!
167 # endif /* X86_64 || X32 || POWERPC64 */
168 #endif /* SUPPORTED_PERSONALITIES > 1 */
169
170 #ifdef STAT32_PERSONALITY
171 # define DO_PRINTSTAT do_printstat32
172 # define STRUCT_STAT struct stat32
173 # undef HAVE_STRUCT_STAT_ST_FLAGS
174 # undef HAVE_STRUCT_STAT_ST_FSTYPE
175 # undef HAVE_STRUCT_STAT_ST_GEN
176 # include "printstat.h"
177 #endif /* STAT32_PERSONALITY */
178
179 #if defined(SPARC) || defined(SPARC64)
180
181 struct solstat {
182         unsigned        st_dev;
183         unsigned int    st_pad1[3];     /* network id */
184         unsigned        st_ino;
185         unsigned        st_mode;
186         unsigned        st_nlink;
187         unsigned        st_uid;
188         unsigned        st_gid;
189         unsigned        st_rdev;
190         unsigned int    st_pad2[2];
191         unsigned int    st_size;
192         unsigned int    st_pad3;        /* st_size, off_t expansion */
193         unsigned int    st_atime;
194         unsigned int    st_atime_nsec;
195         unsigned int    st_mtime;
196         unsigned int    st_mtime_nsec;
197         unsigned int    st_ctime;
198         unsigned int    st_ctime_nsec;
199         unsigned int    st_blksize;
200         unsigned int    st_blocks;
201         char            st_fstype[16];
202         unsigned int    st_pad4[8];     /* expansion area */
203 };
204
205 # define DO_PRINTSTAT   do_printstat_sol
206 # define STRUCT_STAT    struct solstat
207 # define STAT_MAJOR(x)  (((x) >> 18) & 0x3fff)
208 # define STAT_MINOR(x)  ((x) & 0x3ffff)
209 # undef HAVE_STRUCT_STAT_ST_FLAGS
210 # undef HAVE_STRUCT_STAT_ST_FSTYPE
211 # undef HAVE_STRUCT_STAT_ST_GEN
212 # include "printstat.h"
213 #endif /* SPARC || SPARC64 */
214
215 static void
216 printstat(struct tcb *tcp, long addr)
217 {
218         struct stat statbuf;
219
220 #ifdef STAT32_PERSONALITY
221         if (current_personality == STAT32_PERSONALITY) {
222                 struct stat32 statbuf;
223
224                 if (!umove_or_printaddr(tcp, addr, &statbuf))
225                         do_printstat32(tcp, &statbuf);
226                 return;
227         }
228 #endif
229
230 #if defined(SPARC) || defined(SPARC64)
231         if (current_personality == 1) {
232                 struct solstat statbuf;
233
234                 if (!umove_or_printaddr(tcp, addr, &statbuf))
235                         do_printstat_sol(tcp, &statbuf);
236                 return;
237         }
238 #endif /* SPARC || SPARC64 */
239
240         if (!umove_or_printaddr(tcp, addr, &statbuf))
241                 do_printstat(tcp, &statbuf);
242 }
243
244 SYS_FUNC(stat)
245 {
246         if (entering(tcp)) {
247                 printpath(tcp, tcp->u_arg[0]);
248                 tprints(", ");
249         } else {
250                 printstat(tcp, tcp->u_arg[1]);
251         }
252         return 0;
253 }
254
255 SYS_FUNC(fstat)
256 {
257         if (entering(tcp)) {
258                 printfd(tcp, tcp->u_arg[0]);
259                 tprints(", ");
260         } else {
261                 printstat(tcp, tcp->u_arg[1]);
262         }
263         return 0;
264 }
265
266 #if defined STAT32_PERSONALITY && !defined HAVE_STRUCT_STAT64
267 # if defined AARCH64 || defined X86_64 || defined X32
268 /*
269  * Linux x86_64 and x32 have unified `struct stat' but their i386 personality
270  * needs `struct stat64'.
271  * linux/arch/x86/include/uapi/asm/stat.h defines `struct stat64' only for i386.
272  *
273  * Similarly, aarch64 has a unified `struct stat' but its arm personality
274  * needs `struct stat64' (unlike x86, it shouldn't be packed).
275  */
276 struct stat64 {
277         unsigned long long      st_dev;
278         unsigned char   __pad0[4];
279         unsigned int    __st_ino;
280         unsigned int    st_mode;
281         unsigned int    st_nlink;
282         unsigned int    st_uid;
283         unsigned int    st_gid;
284         unsigned long long      st_rdev;
285         unsigned char   __pad3[4];
286         long long       st_size;
287         unsigned int    st_blksize;
288         unsigned long long      st_blocks;
289         unsigned int    st_atime;
290         unsigned int    st_atime_nsec;
291         unsigned int    st_mtime;
292         unsigned int    st_mtime_nsec;
293         unsigned int    st_ctime;
294         unsigned int    st_ctime_nsec;
295         unsigned long long      st_ino;
296 }
297 #  if defined X86_64 || defined X32
298   ATTRIBUTE_PACKED
299 #   define STAT64_SIZE  96
300 #  else
301 #   define STAT64_SIZE  104
302 #  endif
303 ;
304 #  define HAVE_STRUCT_STAT64    1
305 # else /* !(AARCH64 || X86_64 || X32) */
306 #  warning FIXME: check whether struct stat64 definition is needed for this architecture!
307 # endif
308 #endif /* STAT32_PERSONALITY && !HAVE_STRUCT_STAT64 */
309
310 #ifdef HAVE_STRUCT_STAT64
311
312 # define DO_PRINTSTAT do_printstat64
313 # define STRUCT_STAT struct stat64
314 # undef HAVE_STRUCT_STAT_ST_FLAGS
315 # undef HAVE_STRUCT_STAT_ST_FSTYPE
316 # undef HAVE_STRUCT_STAT_ST_GEN
317 # include "printstat.h"
318
319 static void
320 printstat64(struct tcb *tcp, long addr)
321 {
322         struct stat64 statbuf;
323
324 # ifdef STAT64_SIZE
325         (void) sizeof(char[sizeof statbuf == STAT64_SIZE ? 1 : -1]);
326 # endif
327
328 # ifdef STAT32_PERSONALITY
329         if (current_personality != STAT32_PERSONALITY) {
330                 printstat(tcp, addr);
331                 return;
332         }
333 # endif /* STAT32_PERSONALITY */
334
335         if (!umove_or_printaddr(tcp, addr, &statbuf))
336                 do_printstat64(tcp, &statbuf);
337 }
338
339 SYS_FUNC(stat64)
340 {
341         if (entering(tcp)) {
342                 printpath(tcp, tcp->u_arg[0]);
343                 tprints(", ");
344         } else {
345                 printstat64(tcp, tcp->u_arg[1]);
346         }
347         return 0;
348 }
349
350 SYS_FUNC(fstat64)
351 {
352         if (entering(tcp)) {
353                 printfd(tcp, tcp->u_arg[0]);
354                 tprints(", ");
355         } else {
356                 printstat64(tcp, tcp->u_arg[1]);
357         }
358         return 0;
359 }
360
361 #else
362
363 SYS_FUNC(stat64)
364 {
365         return sys_stat(tcp);
366 }
367
368 SYS_FUNC(fstat64)
369 {
370         return sys_fstat(tcp);
371 }
372
373 #endif /* HAVE_STRUCT_STAT64 */
374
375 SYS_FUNC(newfstatat)
376 {
377         if (entering(tcp)) {
378                 print_dirfd(tcp, tcp->u_arg[0]);
379                 printpath(tcp, tcp->u_arg[1]);
380                 tprints(", ");
381         } else {
382 #if defined STAT32_PERSONALITY
383                 if (current_personality == STAT32_PERSONALITY)
384                         printstat64(tcp, tcp->u_arg[2]);
385                 else
386                         printstat(tcp, tcp->u_arg[2]);
387 #elif defined HAVE_STRUCT_STAT64
388                 printstat64(tcp, tcp->u_arg[2]);
389 #else
390                 printstat(tcp, tcp->u_arg[2]);
391 #endif /* STAT32_PERSONALITY || HAVE_STRUCT_STAT64 */
392                 tprints(", ");
393                 printflags(at_flags, tcp->u_arg[3], "AT_???");
394         }
395         return 0;
396 }
397
398 #if defined(HAVE_STRUCT___OLD_KERNEL_STAT)
399
400 static void
401 convertoldstat(const struct __old_kernel_stat *oldbuf, struct stat *newbuf)
402 {
403         memset(newbuf, 0, sizeof(*newbuf));
404         newbuf->st_dev = oldbuf->st_dev;
405         newbuf->st_ino = oldbuf->st_ino;
406         newbuf->st_mode = oldbuf->st_mode;
407         newbuf->st_nlink = oldbuf->st_nlink;
408         newbuf->st_uid = oldbuf->st_uid;
409         newbuf->st_gid = oldbuf->st_gid;
410         newbuf->st_rdev = oldbuf->st_rdev;
411         newbuf->st_size = oldbuf->st_size;
412         newbuf->st_atime = oldbuf->st_atime;
413         newbuf->st_mtime = oldbuf->st_mtime;
414         newbuf->st_ctime = oldbuf->st_ctime;
415 }
416
417 static void
418 printoldstat(struct tcb *tcp, long addr)
419 {
420         struct __old_kernel_stat statbuf;
421         struct stat newstatbuf;
422
423 # if defined(SPARC) || defined(SPARC64)
424         if (current_personality == 1) {
425                 struct solstat statbuf;
426
427                 if (!umove_or_printaddr(tcp, addr, &statbuf))
428                         do_printstat_sol(tcp, &statbuf);
429                 return;
430         }
431 # endif
432
433         if (!umove_or_printaddr(tcp, addr, &statbuf)) {
434                 convertoldstat(&statbuf, &newstatbuf);
435                 do_printstat(tcp, &newstatbuf);
436         }
437 }
438
439 SYS_FUNC(oldstat)
440 {
441         if (entering(tcp)) {
442                 printpath(tcp, tcp->u_arg[0]);
443                 tprints(", ");
444         } else {
445                 printoldstat(tcp, tcp->u_arg[1]);
446         }
447         return 0;
448 }
449
450 SYS_FUNC(oldfstat)
451 {
452         if (entering(tcp)) {
453                 printfd(tcp, tcp->u_arg[0]);
454                 tprints(", ");
455         } else {
456                 printoldstat(tcp, tcp->u_arg[1]);
457         }
458         return 0;
459 }
460
461 #endif /* HAVE_STRUCT___OLD_KERNEL_STAT */
462
463 #if defined(SPARC) || defined(SPARC64)
464
465 SYS_FUNC(xstat)
466 {
467         if (entering(tcp)) {
468                 tprintf("%ld, ", tcp->u_arg[0]);
469                 printpath(tcp, tcp->u_arg[1]);
470                 tprints(", ");
471         } else {
472                 printstat(tcp, tcp->u_arg[2]);
473         }
474         return 0;
475 }
476
477 SYS_FUNC(fxstat)
478 {
479         if (entering(tcp)) {
480                 tprintf("%ld, ", tcp->u_arg[0]);
481                 printfd(tcp, tcp->u_arg[1]);
482                 tprints(", ");
483         } else {
484                 printstat(tcp, tcp->u_arg[2]);
485         }
486         return 0;
487 }
488
489 #endif /* SPARC || SPARC64 */