]> granicus.if.org Git - strace/blob - file.c
tests: rewrite umovestr2.test without using grep
[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
178 static void
179 printstat32(struct tcb *tcp, long addr)
180 {
181         struct stat32 statbuf;
182
183         if (umove(tcp, addr, &statbuf) < 0) {
184                 tprints("{...}");
185                 return;
186         }
187
188         do_printstat32(tcp, &statbuf);
189 }
190 #endif /* STAT32_PERSONALITY */
191
192 #if defined(SPARC) || defined(SPARC64)
193
194 struct solstat {
195         unsigned        st_dev;
196         unsigned int    st_pad1[3];     /* network id */
197         unsigned        st_ino;
198         unsigned        st_mode;
199         unsigned        st_nlink;
200         unsigned        st_uid;
201         unsigned        st_gid;
202         unsigned        st_rdev;
203         unsigned int    st_pad2[2];
204         unsigned int    st_size;
205         unsigned int    st_pad3;        /* st_size, off_t expansion */
206         unsigned int    st_atime;
207         unsigned int    st_atime_nsec;
208         unsigned int    st_mtime;
209         unsigned int    st_mtime_nsec;
210         unsigned int    st_ctime;
211         unsigned int    st_ctime_nsec;
212         unsigned int    st_blksize;
213         unsigned int    st_blocks;
214         char            st_fstype[16];
215         unsigned int    st_pad4[8];     /* expansion area */
216 };
217
218 # define DO_PRINTSTAT   do_printstat_sol
219 # define STRUCT_STAT    struct solstat
220 # define STAT_MAJOR(x)  (((x) >> 18) & 0x3fff)
221 # define STAT_MINOR(x)  ((x) & 0x3ffff)
222 # undef HAVE_STRUCT_STAT_ST_FLAGS
223 # undef HAVE_STRUCT_STAT_ST_FSTYPE
224 # undef HAVE_STRUCT_STAT_ST_GEN
225 # include "printstat.h"
226
227 static void
228 printstatsol(struct tcb *tcp, long addr)
229 {
230         struct solstat statbuf;
231
232         if (umove(tcp, addr, &statbuf) < 0) {
233                 tprints("{...}");
234                 return;
235         }
236
237         do_printstat_sol(tcp, &statbuf);
238 }
239
240 #endif /* SPARC || SPARC64 */
241
242 static void
243 printstat(struct tcb *tcp, long addr)
244 {
245         struct stat statbuf;
246
247         if (!addr) {
248                 tprints("NULL");
249                 return;
250         }
251         if (syserror(tcp) || !verbose(tcp)) {
252                 tprintf("%#lx", addr);
253                 return;
254         }
255
256 #ifdef STAT32_PERSONALITY
257         if (current_personality == STAT32_PERSONALITY) {
258                 printstat32(tcp, addr);
259                 return;
260         }
261 #endif
262
263 #if defined(SPARC) || defined(SPARC64)
264         if (current_personality == 1) {
265                 printstatsol(tcp, addr);
266                 return;
267         }
268 #endif /* SPARC || SPARC64 */
269
270         if (umove(tcp, addr, &statbuf) < 0) {
271                 tprints("{...}");
272                 return;
273         }
274
275         do_printstat(tcp, &statbuf);
276 }
277
278 int
279 sys_stat(struct tcb *tcp)
280 {
281         if (entering(tcp)) {
282                 printpath(tcp, tcp->u_arg[0]);
283                 tprints(", ");
284         } else {
285                 printstat(tcp, tcp->u_arg[1]);
286         }
287         return 0;
288 }
289
290 int
291 sys_fstat(struct tcb *tcp)
292 {
293         if (entering(tcp)) {
294                 printfd(tcp, tcp->u_arg[0]);
295                 tprints(", ");
296         } else {
297                 printstat(tcp, tcp->u_arg[1]);
298         }
299         return 0;
300 }
301
302 #if defined STAT32_PERSONALITY && !defined HAVE_STRUCT_STAT64
303 # if defined AARCH64 || defined X86_64 || defined X32
304 /*
305  * Linux x86_64 and x32 have unified `struct stat' but their i386 personality
306  * needs `struct stat64'.
307  * linux/arch/x86/include/uapi/asm/stat.h defines `struct stat64' only for i386.
308  *
309  * Similarly, aarch64 has a unified `struct stat' but its arm personality
310  * needs `struct stat64' (unlike x86, it shouldn't be packed).
311  */
312 struct stat64 {
313         unsigned long long      st_dev;
314         unsigned char   __pad0[4];
315         unsigned int    __st_ino;
316         unsigned int    st_mode;
317         unsigned int    st_nlink;
318         unsigned int    st_uid;
319         unsigned int    st_gid;
320         unsigned long long      st_rdev;
321         unsigned char   __pad3[4];
322         long long       st_size;
323         unsigned int    st_blksize;
324         unsigned long long      st_blocks;
325         unsigned int    st_atime;
326         unsigned int    st_atime_nsec;
327         unsigned int    st_mtime;
328         unsigned int    st_mtime_nsec;
329         unsigned int    st_ctime;
330         unsigned int    st_ctime_nsec;
331         unsigned long long      st_ino;
332 }
333 #  if defined X86_64 || defined X32
334   ATTRIBUTE_PACKED
335 #   define STAT64_SIZE  96
336 #  else
337 #   define STAT64_SIZE  104
338 #  endif
339 ;
340 #  define HAVE_STRUCT_STAT64    1
341 # else /* !(AARCH64 || X86_64 || X32) */
342 #  warning FIXME: check whether struct stat64 definition is needed for this architecture!
343 # endif
344 #endif /* STAT32_PERSONALITY && !HAVE_STRUCT_STAT64 */
345
346 #ifdef HAVE_STRUCT_STAT64
347
348 # define DO_PRINTSTAT do_printstat64
349 # define STRUCT_STAT struct stat64
350 # undef HAVE_STRUCT_STAT_ST_FLAGS
351 # undef HAVE_STRUCT_STAT_ST_FSTYPE
352 # undef HAVE_STRUCT_STAT_ST_GEN
353 # include "printstat.h"
354
355 static void
356 printstat64(struct tcb *tcp, long addr)
357 {
358         struct stat64 statbuf;
359
360 # ifdef STAT64_SIZE
361         (void) sizeof(char[sizeof statbuf == STAT64_SIZE ? 1 : -1]);
362 # endif
363
364         if (!addr) {
365                 tprints("NULL");
366                 return;
367         }
368         if (syserror(tcp) || !verbose(tcp)) {
369                 tprintf("%#lx", addr);
370                 return;
371         }
372
373 # ifdef STAT32_PERSONALITY
374         if (current_personality != STAT32_PERSONALITY) {
375                 printstat(tcp, addr);
376                 return;
377         }
378 # endif /* STAT32_PERSONALITY */
379
380         if (umove(tcp, addr, &statbuf) < 0) {
381                 tprints("{...}");
382                 return;
383         }
384
385         do_printstat64(tcp, &statbuf);
386 }
387
388 int
389 sys_stat64(struct tcb *tcp)
390 {
391         if (entering(tcp)) {
392                 printpath(tcp, tcp->u_arg[0]);
393                 tprints(", ");
394         } else {
395                 printstat64(tcp, tcp->u_arg[1]);
396         }
397         return 0;
398 }
399
400 int
401 sys_fstat64(struct tcb *tcp)
402 {
403         if (entering(tcp)) {
404                 printfd(tcp, tcp->u_arg[0]);
405                 tprints(", ");
406         } else {
407                 printstat64(tcp, tcp->u_arg[1]);
408         }
409         return 0;
410 }
411
412 #else
413
414 int
415 sys_stat64(struct tcb *tcp)
416 {
417         return sys_stat(tcp);
418 }
419
420 int
421 sys_fstat64(struct tcb *tcp)
422 {
423         return sys_fstat(tcp);
424 }
425
426 #endif /* HAVE_STRUCT_STAT64 */
427
428 int
429 sys_newfstatat(struct tcb *tcp)
430 {
431         if (entering(tcp)) {
432                 print_dirfd(tcp, tcp->u_arg[0]);
433                 printpath(tcp, tcp->u_arg[1]);
434                 tprints(", ");
435         } else {
436 #if defined STAT32_PERSONALITY
437                 if (current_personality == STAT32_PERSONALITY)
438                         printstat64(tcp, tcp->u_arg[2]);
439                 else
440                         printstat(tcp, tcp->u_arg[2]);
441 #elif defined HAVE_STRUCT_STAT64
442                 printstat64(tcp, tcp->u_arg[2]);
443 #else
444                 printstat(tcp, tcp->u_arg[2]);
445 #endif /* STAT32_PERSONALITY || HAVE_STRUCT_STAT64 */
446                 tprints(", ");
447                 printflags(at_flags, tcp->u_arg[3], "AT_???");
448         }
449         return 0;
450 }
451
452 #if defined(HAVE_STRUCT___OLD_KERNEL_STAT)
453
454 static void
455 convertoldstat(const struct __old_kernel_stat *oldbuf, struct stat *newbuf)
456 {
457         memset(newbuf, 0, sizeof(*newbuf));
458         newbuf->st_dev = oldbuf->st_dev;
459         newbuf->st_ino = oldbuf->st_ino;
460         newbuf->st_mode = oldbuf->st_mode;
461         newbuf->st_nlink = oldbuf->st_nlink;
462         newbuf->st_uid = oldbuf->st_uid;
463         newbuf->st_gid = oldbuf->st_gid;
464         newbuf->st_rdev = oldbuf->st_rdev;
465         newbuf->st_size = oldbuf->st_size;
466         newbuf->st_atime = oldbuf->st_atime;
467         newbuf->st_mtime = oldbuf->st_mtime;
468         newbuf->st_ctime = oldbuf->st_ctime;
469 }
470
471 static void
472 printoldstat(struct tcb *tcp, long addr)
473 {
474         struct __old_kernel_stat statbuf;
475         struct stat newstatbuf;
476
477         if (!addr) {
478                 tprints("NULL");
479                 return;
480         }
481         if (syserror(tcp) || !verbose(tcp)) {
482                 tprintf("%#lx", addr);
483                 return;
484         }
485
486 # if defined(SPARC) || defined(SPARC64)
487         if (current_personality == 1) {
488                 printstatsol(tcp, addr);
489                 return;
490         }
491 # endif
492
493         if (umove(tcp, addr, &statbuf) < 0) {
494                 tprints("{...}");
495                 return;
496         }
497
498         convertoldstat(&statbuf, &newstatbuf);
499         do_printstat(tcp, &newstatbuf);
500 }
501
502 int
503 sys_oldstat(struct tcb *tcp)
504 {
505         if (entering(tcp)) {
506                 printpath(tcp, tcp->u_arg[0]);
507                 tprints(", ");
508         } else {
509                 printoldstat(tcp, tcp->u_arg[1]);
510         }
511         return 0;
512 }
513
514 int
515 sys_oldfstat(struct tcb *tcp)
516 {
517         if (entering(tcp)) {
518                 printfd(tcp, tcp->u_arg[0]);
519                 tprints(", ");
520         } else {
521                 printoldstat(tcp, tcp->u_arg[1]);
522         }
523         return 0;
524 }
525
526 #endif /* HAVE_STRUCT___OLD_KERNEL_STAT */
527
528 #if defined(SPARC) || defined(SPARC64)
529
530 int
531 sys_xstat(struct tcb *tcp)
532 {
533         if (entering(tcp)) {
534                 tprintf("%ld, ", tcp->u_arg[0]);
535                 printpath(tcp, tcp->u_arg[1]);
536                 tprints(", ");
537         } else {
538                 printstat(tcp, tcp->u_arg[2]);
539         }
540         return 0;
541 }
542
543 int
544 sys_fxstat(struct tcb *tcp)
545 {
546         if (entering(tcp)) {
547                 tprintf("%ld, ", tcp->u_arg[0]);
548                 printfd(tcp, tcp->u_arg[1]);
549                 tprints(", ");
550         } else {
551                 printstat(tcp, tcp->u_arg[2]);
552         }
553         return 0;
554 }
555
556 #endif /* SPARC || SPARC64 */