]> granicus.if.org Git - strace/blob - file.c
Remove unused <sys/asynch.h> based code
[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 #include <sys/swap.h>
33
34 #if defined(SPARC) || defined(SPARC64)
35 struct stat {
36         unsigned short  st_dev;
37         unsigned int    st_ino;
38         unsigned short  st_mode;
39         short           st_nlink;
40         unsigned short  st_uid;
41         unsigned short  st_gid;
42         unsigned short  st_rdev;
43         unsigned int    st_size;
44         int             st_atime;
45         unsigned int    __unused1;
46         int             st_mtime;
47         unsigned int    __unused2;
48         int             st_ctime;
49         unsigned int    __unused3;
50         int             st_blksize;
51         int             st_blocks;
52         unsigned int    __unused4[2];
53 };
54 # if defined(SPARC64)
55 struct stat_sparc64 {
56         unsigned int    st_dev;
57         unsigned long   st_ino;
58         unsigned int    st_mode;
59         unsigned int    st_nlink;
60         unsigned int    st_uid;
61         unsigned int    st_gid;
62         unsigned int    st_rdev;
63         long            st_size;
64         long            st_atime;
65         long            st_mtime;
66         long            st_ctime;
67         long            st_blksize;
68         long            st_blocks;
69         unsigned long   __unused4[2];
70 };
71 # endif /* SPARC64 */
72 # define stat kernel_stat
73 # include <asm/stat.h>
74 # undef stat
75 #elif defined(X32)
76 struct stat {
77         unsigned long long      st_dev;
78         unsigned long long      st_ino;
79         unsigned long long      st_nlink;
80
81         unsigned int            st_mode;
82         unsigned int            st_uid;
83         unsigned int            st_gid;
84         unsigned int            __pad0;
85         unsigned long long      st_rdev;
86         long long               st_size;
87         long long               st_blksize;
88         long long               st_blocks;
89
90         unsigned long long      st_atime;
91         unsigned long long      st_atime_nsec;
92         unsigned long long      st_mtime;
93         unsigned long long      st_mtime_nsec;
94         unsigned long long      st_ctime;
95         unsigned long long      st_ctime_nsec;
96         long long               __unused[3];
97 };
98
99 struct stat64 {
100         unsigned long long      st_dev;
101         unsigned char           __pad0[4];
102         unsigned long           __st_ino;
103         unsigned int            st_mode;
104         unsigned int            st_nlink;
105         unsigned long           st_uid;
106         unsigned long           st_gid;
107         unsigned long long      st_rdev;
108         unsigned char           __pad3[4];
109         long long               st_size;
110         unsigned long           st_blksize;
111         unsigned long long      st_blocks;
112         unsigned long           st_atime;
113         unsigned long           st_atime_nsec;
114         unsigned long           st_mtime;
115         unsigned int            st_mtime_nsec;
116         unsigned long           st_ctime;
117         unsigned long           st_ctime_nsec;
118         unsigned long long      st_ino;
119 } __attribute__((packed));
120 # define HAVE_STAT64    1
121
122 struct __old_kernel_stat {
123         unsigned short st_dev;
124         unsigned short st_ino;
125         unsigned short st_mode;
126         unsigned short st_nlink;
127         unsigned short st_uid;
128         unsigned short st_gid;
129         unsigned short st_rdev;
130         unsigned int   st_size;
131         unsigned int   st_atime;
132         unsigned int   st_mtime;
133         unsigned int   st_ctime;
134 };
135 #else
136 # undef dev_t
137 # undef ino_t
138 # undef mode_t
139 # undef nlink_t
140 # undef uid_t
141 # undef gid_t
142 # undef off_t
143 # undef loff_t
144 # define dev_t __kernel_dev_t
145 # define ino_t __kernel_ino_t
146 # define mode_t __kernel_mode_t
147 # define nlink_t __kernel_nlink_t
148 # define uid_t __kernel_uid_t
149 # define gid_t __kernel_gid_t
150 # define off_t __kernel_off_t
151 # define loff_t __kernel_loff_t
152
153 # include <asm/stat.h>
154
155 # undef dev_t
156 # undef ino_t
157 # undef mode_t
158 # undef nlink_t
159 # undef uid_t
160 # undef gid_t
161 # undef off_t
162 # undef loff_t
163 # define dev_t dev_t
164 # define ino_t ino_t
165 # define mode_t mode_t
166 # define nlink_t nlink_t
167 # define uid_t uid_t
168 # define gid_t gid_t
169 # define off_t off_t
170 # define loff_t loff_t
171 #endif
172
173 #define stat libc_stat
174 #define stat64 libc_stat64
175 #include <sys/stat.h>
176 #undef stat
177 #undef stat64
178 /* These might be macros. */
179 #undef st_atime
180 #undef st_mtime
181 #undef st_ctime
182
183 #include <fcntl.h>
184 #ifdef HAVE_LINUX_XATTR_H
185 # include <linux/xattr.h>
186 #else
187 # define XATTR_CREATE 1
188 # define XATTR_REPLACE 2
189 #endif
190
191 #ifdef MAJOR_IN_SYSMACROS
192 # include <sys/sysmacros.h>
193 #endif
194
195 #ifdef MAJOR_IN_MKDEV
196 # include <sys/mkdev.h>
197 #endif
198
199 #ifdef O_LARGEFILE
200 # if O_LARGEFILE == 0          /* biarch platforms in 64-bit mode */
201 #  undef O_LARGEFILE
202 #  ifdef SPARC64
203 #   define O_LARGEFILE 0x40000
204 #  elif defined X86_64 || defined S390X
205 #   define O_LARGEFILE 0100000
206 #  endif
207 # endif
208 #endif
209
210 #include "xlat/open_access_modes.h"
211 #include "xlat/open_mode_flags.h"
212
213 #ifndef AT_FDCWD
214 # define AT_FDCWD                -100
215 #endif
216
217 /* The fd is an "int", so when decoding x86 on x86_64, we need to force sign
218  * extension to get the right value.  We do this by declaring fd as int here.
219  */
220 void
221 print_dirfd(struct tcb *tcp, int fd)
222 {
223         if (fd == AT_FDCWD)
224                 tprints("AT_FDCWD, ");
225         else {
226                 printfd(tcp, fd);
227                 tprints(", ");
228         }
229 }
230
231 /*
232  * low bits of the open(2) flags define access mode,
233  * other bits are real flags.
234  */
235 const char *
236 sprint_open_modes(int flags)
237 {
238         static char outstr[(1 + ARRAY_SIZE(open_mode_flags)) * sizeof("O_LARGEFILE")];
239         char *p;
240         char sep;
241         const char *str;
242         const struct xlat *x;
243
244         sep = ' ';
245         p = stpcpy(outstr, "flags");
246         str = xlookup(open_access_modes, flags & 3);
247         if (str) {
248                 *p++ = sep;
249                 p = stpcpy(p, str);
250                 flags &= ~3;
251                 if (!flags)
252                         return outstr;
253                 sep = '|';
254         }
255
256         for (x = open_mode_flags; x->str; x++) {
257                 if ((flags & x->val) == x->val) {
258                         *p++ = sep;
259                         p = stpcpy(p, x->str);
260                         flags &= ~x->val;
261                         if (!flags)
262                                 return outstr;
263                         sep = '|';
264                 }
265         }
266         /* flags is still nonzero */
267         *p++ = sep;
268         sprintf(p, "%#x", flags);
269         return outstr;
270 }
271
272 void
273 tprint_open_modes(int flags)
274 {
275         tprints(sprint_open_modes(flags) + sizeof("flags"));
276 }
277
278 static int
279 decode_open(struct tcb *tcp, int offset)
280 {
281         if (entering(tcp)) {
282                 printpath(tcp, tcp->u_arg[offset]);
283                 tprints(", ");
284                 /* flags */
285                 tprint_open_modes(tcp->u_arg[offset + 1]);
286                 if (tcp->u_arg[offset + 1] & O_CREAT) {
287                         /* mode */
288                         tprintf(", %#lo", tcp->u_arg[offset + 2]);
289                 }
290         }
291         return RVAL_FD;
292 }
293
294 int
295 sys_open(struct tcb *tcp)
296 {
297         return decode_open(tcp, 0);
298 }
299
300 int
301 sys_openat(struct tcb *tcp)
302 {
303         if (entering(tcp))
304                 print_dirfd(tcp, tcp->u_arg[0]);
305         return decode_open(tcp, 1);
306 }
307
308 #if defined(SPARC) || defined(SPARC64)
309 #include "xlat/openmodessol.h"
310
311 int
312 solaris_open(struct tcb *tcp)
313 {
314         if (entering(tcp)) {
315                 printpath(tcp, tcp->u_arg[0]);
316                 tprints(", ");
317                 /* flags */
318                 printflags(openmodessol, tcp->u_arg[1] + 1, "O_???");
319                 if (tcp->u_arg[1] & 0x100) {
320                         /* mode */
321                         tprintf(", %#lo", tcp->u_arg[2]);
322                 }
323         }
324         return 0;
325 }
326
327 #endif
328
329 int
330 sys_creat(struct tcb *tcp)
331 {
332         if (entering(tcp)) {
333                 printpath(tcp, tcp->u_arg[0]);
334                 tprintf(", %#lo", tcp->u_arg[1]);
335         }
336         return RVAL_FD;
337 }
338
339 #include "xlat/access_flags.h"
340
341 static int
342 decode_access(struct tcb *tcp, int offset)
343 {
344         if (entering(tcp)) {
345                 printpath(tcp, tcp->u_arg[offset]);
346                 tprints(", ");
347                 printflags(access_flags, tcp->u_arg[offset + 1], "?_OK");
348         }
349         return 0;
350 }
351
352 int
353 sys_access(struct tcb *tcp)
354 {
355         return decode_access(tcp, 0);
356 }
357
358 int
359 sys_faccessat(struct tcb *tcp)
360 {
361         if (entering(tcp))
362                 print_dirfd(tcp, tcp->u_arg[0]);
363         return decode_access(tcp, 1);
364 }
365
366 int
367 sys_umask(struct tcb *tcp)
368 {
369         if (entering(tcp)) {
370                 tprintf("%#lo", tcp->u_arg[0]);
371         }
372         return RVAL_OCTAL;
373 }
374
375 #include "xlat/whence_codes.h"
376
377 /* Linux kernel has exactly one version of lseek:
378  * fs/read_write.c::SYSCALL_DEFINE3(lseek, unsigned, fd, off_t, offset, unsigned, origin)
379  * In kernel, off_t is always the same as (kernel's) long
380  * (see include/uapi/asm-generic/posix_types.h),
381  * which means that on x32 we need to use tcp->ext_arg[N] to get offset argument.
382  * Use test/x32_lseek.c to test lseek decoding.
383  */
384 #if defined(LINUX_MIPSN32) || defined(X32)
385 int
386 sys_lseek(struct tcb *tcp)
387 {
388         long long offset;
389         int whence;
390
391         if (entering(tcp)) {
392                 printfd(tcp, tcp->u_arg[0]);
393                 offset = tcp->ext_arg[1];
394                 whence = tcp->u_arg[2];
395                 if (whence == SEEK_SET)
396                         tprintf(", %llu, ", offset);
397                 else
398                         tprintf(", %lld, ", offset);
399                 printxval(whence_codes, whence, "SEEK_???");
400         }
401         return RVAL_LUDECIMAL;
402 }
403 #else
404 int
405 sys_lseek(struct tcb *tcp)
406 {
407         long offset;
408         int whence;
409
410         if (entering(tcp)) {
411                 printfd(tcp, tcp->u_arg[0]);
412                 offset = tcp->u_arg[1];
413                 whence = tcp->u_arg[2];
414                 if (whence == SEEK_SET)
415                         tprintf(", %lu, ", offset);
416                 else
417                         tprintf(", %ld, ", offset);
418                 printxval(whence_codes, whence, "SEEK_???");
419         }
420         return RVAL_UDECIMAL;
421 }
422 #endif
423
424 /* llseek syscall takes explicitly two ulong arguments hi, lo,
425  * rather than one 64-bit argument for which LONG_LONG works
426  * appropriate for the native byte order.
427  *
428  * See kernel's fs/read_write.c::SYSCALL_DEFINE5(llseek, ...)
429  *
430  * hi,lo are "unsigned longs" and combined exactly this way in kernel:
431  * ((loff_t) hi << 32) | lo
432  * Note that for architectures with kernel's long wider than userspace long
433  * (such as x32), combining code will use *kernel's*, i.e. *wide* longs
434  * for hi and lo. We would need to use tcp->ext_arg[N] on x32...
435  * ...however, x32 (and x86_64) does not _have_ llseek syscall as such.
436  */
437 int
438 sys_llseek(struct tcb *tcp)
439 {
440         if (entering(tcp)) {
441                 printfd(tcp, tcp->u_arg[0]);
442                 if (tcp->u_arg[4] == SEEK_SET)
443                         tprintf(", %llu, ",
444                                 ((long long) tcp->u_arg[1]) << 32 |
445                                 (unsigned long long) (unsigned) tcp->u_arg[2]);
446                 else
447                         tprintf(", %lld, ",
448                                 ((long long) tcp->u_arg[1]) << 32 |
449                                 (unsigned long long) (unsigned) tcp->u_arg[2]);
450         }
451         else {
452                 long long off;
453                 if (syserror(tcp) || umove(tcp, tcp->u_arg[3], &off) < 0)
454                         tprintf("%#lx, ", tcp->u_arg[3]);
455                 else
456                         tprintf("[%llu], ", off);
457                 printxval(whence_codes, tcp->u_arg[4], "SEEK_???");
458         }
459         return 0;
460 }
461
462 int
463 sys_readahead(struct tcb *tcp)
464 {
465         if (entering(tcp)) {
466                 int argn;
467                 printfd(tcp, tcp->u_arg[0]);
468                 argn = printllval(tcp, ", %lld", 1);
469                 tprintf(", %ld", tcp->u_arg[argn]);
470         }
471         return 0;
472 }
473
474 int
475 sys_truncate(struct tcb *tcp)
476 {
477         if (entering(tcp)) {
478                 printpath(tcp, tcp->u_arg[0]);
479                 tprintf(", %lu", tcp->u_arg[1]);
480         }
481         return 0;
482 }
483
484 int
485 sys_truncate64(struct tcb *tcp)
486 {
487         if (entering(tcp)) {
488                 printpath(tcp, tcp->u_arg[0]);
489                 printllval(tcp, ", %llu", 1);
490         }
491         return 0;
492 }
493
494 int
495 sys_ftruncate(struct tcb *tcp)
496 {
497         if (entering(tcp)) {
498                 printfd(tcp, tcp->u_arg[0]);
499                 tprintf(", %lu", tcp->u_arg[1]);
500         }
501         return 0;
502 }
503
504 int
505 sys_ftruncate64(struct tcb *tcp)
506 {
507         if (entering(tcp)) {
508                 printfd(tcp, tcp->u_arg[0]);
509                 printllval(tcp, ", %llu", 1);
510         }
511         return 0;
512 }
513
514 /* several stats */
515
516 #include "xlat/modetypes.h"
517
518 static const char *
519 sprintmode(int mode)
520 {
521         static char buf[sizeof("S_IFSOCK|S_ISUID|S_ISGID|S_ISVTX|%o")
522                         + sizeof(int)*3
523                         + /*paranoia:*/ 8];
524         const char *s;
525
526         if ((mode & S_IFMT) == 0)
527                 s = "";
528         else if ((s = xlookup(modetypes, mode & S_IFMT)) == NULL) {
529                 sprintf(buf, "%#o", mode);
530                 return buf;
531         }
532         s = buf + sprintf(buf, "%s%s%s%s", s,
533                 (mode & S_ISUID) ? "|S_ISUID" : "",
534                 (mode & S_ISGID) ? "|S_ISGID" : "",
535                 (mode & S_ISVTX) ? "|S_ISVTX" : "");
536         mode &= ~(S_IFMT|S_ISUID|S_ISGID|S_ISVTX);
537         if (mode)
538                 sprintf((char*)s, "|%#o", mode);
539         s = (*buf == '|') ? buf + 1 : buf;
540         return *s ? s : "0";
541 }
542
543 static char *
544 sprinttime(time_t t)
545 {
546         struct tm *tmp;
547         static char buf[sizeof("yyyy/mm/dd-hh:mm:ss")];
548
549         if (t == 0) {
550                 strcpy(buf, "0");
551                 return buf;
552         }
553         tmp = localtime(&t);
554         if (tmp)
555                 snprintf(buf, sizeof buf, "%02d/%02d/%02d-%02d:%02d:%02d",
556                         tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
557                         tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
558         else
559                 snprintf(buf, sizeof buf, "%lu", (unsigned long) t);
560
561         return buf;
562 }
563
564 #if defined(SPARC) || defined(SPARC64)
565 typedef struct {
566         int     tv_sec;
567         int     tv_nsec;
568 } timestruct_t;
569
570 struct solstat {
571         unsigned        st_dev;
572         int             st_pad1[3];     /* network id */
573         unsigned        st_ino;
574         unsigned        st_mode;
575         unsigned        st_nlink;
576         unsigned        st_uid;
577         unsigned        st_gid;
578         unsigned        st_rdev;
579         int             st_pad2[2];
580         int             st_size;
581         int             st_pad3;        /* st_size, off_t expansion */
582         timestruct_t    st_atime;
583         timestruct_t    st_mtime;
584         timestruct_t    st_ctime;
585         int             st_blksize;
586         int             st_blocks;
587         char            st_fstype[16];
588         int             st_pad4[8];     /* expansion area */
589 };
590
591 static void
592 printstatsol(struct tcb *tcp, long addr)
593 {
594         struct solstat statbuf;
595
596         if (umove(tcp, addr, &statbuf) < 0) {
597                 tprints("{...}");
598                 return;
599         }
600         if (!abbrev(tcp)) {
601                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
602                         (unsigned long) ((statbuf.st_dev >> 18) & 0x3fff),
603                         (unsigned long) (statbuf.st_dev & 0x3ffff),
604                         (unsigned long) statbuf.st_ino,
605                         sprintmode(statbuf.st_mode));
606                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
607                         (unsigned long) statbuf.st_nlink,
608                         (unsigned long) statbuf.st_uid,
609                         (unsigned long) statbuf.st_gid);
610                 tprintf("st_blksize=%lu, ", (unsigned long) statbuf.st_blksize);
611                 tprintf("st_blocks=%lu, ", (unsigned long) statbuf.st_blocks);
612         }
613         else
614                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
615         switch (statbuf.st_mode & S_IFMT) {
616         case S_IFCHR: case S_IFBLK:
617                 tprintf("st_rdev=makedev(%lu, %lu), ",
618                         (unsigned long) ((statbuf.st_rdev >> 18) & 0x3fff),
619                         (unsigned long) (statbuf.st_rdev & 0x3ffff));
620                 break;
621         default:
622                 tprintf("st_size=%u, ", statbuf.st_size);
623                 break;
624         }
625         if (!abbrev(tcp)) {
626                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime.tv_sec));
627                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime.tv_sec));
628                 tprintf("st_ctime=%s}", sprinttime(statbuf.st_ctime.tv_sec));
629         }
630         else
631                 tprints("...}");
632 }
633
634 # if defined(SPARC64)
635 static void
636 printstat_sparc64(struct tcb *tcp, long addr)
637 {
638         struct stat_sparc64 statbuf;
639
640         if (umove(tcp, addr, &statbuf) < 0) {
641                 tprints("{...}");
642                 return;
643         }
644
645         if (!abbrev(tcp)) {
646                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
647                         (unsigned long) major(statbuf.st_dev),
648                         (unsigned long) minor(statbuf.st_dev),
649                         (unsigned long) statbuf.st_ino,
650                         sprintmode(statbuf.st_mode));
651                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
652                         (unsigned long) statbuf.st_nlink,
653                         (unsigned long) statbuf.st_uid,
654                         (unsigned long) statbuf.st_gid);
655                 tprintf("st_blksize=%lu, ",
656                         (unsigned long) statbuf.st_blksize);
657                 tprintf("st_blocks=%lu, ",
658                         (unsigned long) statbuf.st_blocks);
659         }
660         else
661                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
662         switch (statbuf.st_mode & S_IFMT) {
663         case S_IFCHR: case S_IFBLK:
664                 tprintf("st_rdev=makedev(%lu, %lu), ",
665                         (unsigned long) major(statbuf.st_rdev),
666                         (unsigned long) minor(statbuf.st_rdev));
667                 break;
668         default:
669                 tprintf("st_size=%lu, ", statbuf.st_size);
670                 break;
671         }
672         if (!abbrev(tcp)) {
673                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));
674                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));
675                 tprintf("st_ctime=%s}", sprinttime(statbuf.st_ctime));
676         }
677         else
678                 tprints("...}");
679 }
680 # endif /* SPARC64 */
681 #endif /* SPARC[64] */
682
683 #if defined POWERPC64
684 struct stat_powerpc32 {
685         unsigned int    st_dev;
686         unsigned int    st_ino;
687         unsigned int    st_mode;
688         unsigned short  st_nlink;
689         unsigned int    st_uid;
690         unsigned int    st_gid;
691         unsigned int    st_rdev;
692         unsigned int    st_size;
693         unsigned int    st_blksize;
694         unsigned int    st_blocks;
695         unsigned int    st_atime;
696         unsigned int    st_atime_nsec;
697         unsigned int    st_mtime;
698         unsigned int    st_mtime_nsec;
699         unsigned int    st_ctime;
700         unsigned int    st_ctime_nsec;
701         unsigned int    __unused4;
702         unsigned int    __unused5;
703 };
704
705 static void
706 printstat_powerpc32(struct tcb *tcp, long addr)
707 {
708         struct stat_powerpc32 statbuf;
709
710         if (umove(tcp, addr, &statbuf) < 0) {
711                 tprints("{...}");
712                 return;
713         }
714
715         if (!abbrev(tcp)) {
716                 tprintf("{st_dev=makedev(%u, %u), st_ino=%u, st_mode=%s, ",
717                         major(statbuf.st_dev), minor(statbuf.st_dev),
718                         statbuf.st_ino,
719                         sprintmode(statbuf.st_mode));
720                 tprintf("st_nlink=%u, st_uid=%u, st_gid=%u, ",
721                         statbuf.st_nlink, statbuf.st_uid, statbuf.st_gid);
722                 tprintf("st_blksize=%u, ", statbuf.st_blksize);
723                 tprintf("st_blocks=%u, ", statbuf.st_blocks);
724         }
725         else
726                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
727         switch (statbuf.st_mode & S_IFMT) {
728         case S_IFCHR: case S_IFBLK:
729                 tprintf("st_rdev=makedev(%lu, %lu), ",
730                         (unsigned long) major(statbuf.st_rdev),
731                         (unsigned long) minor(statbuf.st_rdev));
732                 break;
733         default:
734                 tprintf("st_size=%u, ", statbuf.st_size);
735                 break;
736         }
737         if (!abbrev(tcp)) {
738                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));
739                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));
740                 tprintf("st_ctime=%s}", sprinttime(statbuf.st_ctime));
741         }
742         else
743                 tprints("...}");
744 }
745 #endif /* POWERPC64 */
746
747 #include "xlat/fileflags.h"
748
749 static void
750 realprintstat(struct tcb *tcp, struct stat *statbuf)
751 {
752         if (!abbrev(tcp)) {
753                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
754                         (unsigned long) major(statbuf->st_dev),
755                         (unsigned long) minor(statbuf->st_dev),
756                         (unsigned long) statbuf->st_ino,
757                         sprintmode(statbuf->st_mode));
758                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
759                         (unsigned long) statbuf->st_nlink,
760                         (unsigned long) statbuf->st_uid,
761                         (unsigned long) statbuf->st_gid);
762 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
763                 tprintf("st_blksize=%lu, ", (unsigned long) statbuf->st_blksize);
764 #endif
765 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
766                 tprintf("st_blocks=%lu, ", (unsigned long) statbuf->st_blocks);
767 #endif
768         }
769         else
770                 tprintf("{st_mode=%s, ", sprintmode(statbuf->st_mode));
771         switch (statbuf->st_mode & S_IFMT) {
772         case S_IFCHR: case S_IFBLK:
773 #ifdef HAVE_STRUCT_STAT_ST_RDEV
774                 tprintf("st_rdev=makedev(%lu, %lu), ",
775                         (unsigned long) major(statbuf->st_rdev),
776                         (unsigned long) minor(statbuf->st_rdev));
777 #else /* !HAVE_STRUCT_STAT_ST_RDEV */
778                 tprintf("st_size=makedev(%lu, %lu), ",
779                         (unsigned long) major(statbuf->st_size),
780                         (unsigned long) minor(statbuf->st_size));
781 #endif /* !HAVE_STRUCT_STAT_ST_RDEV */
782                 break;
783         default:
784                 tprintf("st_size=%lu, ", (unsigned long) statbuf->st_size);
785                 break;
786         }
787         if (!abbrev(tcp)) {
788                 tprintf("st_atime=%s, ", sprinttime(statbuf->st_atime));
789                 tprintf("st_mtime=%s, ", sprinttime(statbuf->st_mtime));
790                 tprintf("st_ctime=%s", sprinttime(statbuf->st_ctime));
791 #if HAVE_STRUCT_STAT_ST_FLAGS
792                 tprints(", st_flags=");
793                 printflags(fileflags, statbuf->st_flags, "UF_???");
794 #endif
795 #if HAVE_STRUCT_STAT_ST_ACLCNT
796                 tprintf(", st_aclcnt=%d", statbuf->st_aclcnt);
797 #endif
798 #if HAVE_STRUCT_STAT_ST_LEVEL
799                 tprintf(", st_level=%ld", statbuf->st_level);
800 #endif
801 #if HAVE_STRUCT_STAT_ST_FSTYPE
802                 tprintf(", st_fstype=%.*s",
803                         (int) sizeof statbuf->st_fstype, statbuf->st_fstype);
804 #endif
805 #if HAVE_STRUCT_STAT_ST_GEN
806                 tprintf(", st_gen=%u", statbuf->st_gen);
807 #endif
808                 tprints("}");
809         }
810         else
811                 tprints("...}");
812 }
813
814 #ifndef X32
815 static void
816 printstat(struct tcb *tcp, long addr)
817 {
818         struct stat statbuf;
819
820         if (!addr) {
821                 tprints("NULL");
822                 return;
823         }
824         if (syserror(tcp) || !verbose(tcp)) {
825                 tprintf("%#lx", addr);
826                 return;
827         }
828
829 #if defined(SPARC) || defined(SPARC64)
830         if (current_personality == 1) {
831                 printstatsol(tcp, addr);
832                 return;
833         }
834 #ifdef SPARC64
835         else if (current_personality == 2) {
836                 printstat_sparc64(tcp, addr);
837                 return;
838         }
839 #endif
840 #endif /* SPARC[64] */
841
842 #if defined POWERPC64
843         if (current_personality == 1) {
844                 printstat_powerpc32(tcp, addr);
845                 return;
846         }
847 #endif
848
849         if (umove(tcp, addr, &statbuf) < 0) {
850                 tprints("{...}");
851                 return;
852         }
853
854         realprintstat(tcp, &statbuf);
855 }
856 #else /* X32 */
857 # define printstat printstat64
858 #endif
859
860 #if !defined HAVE_STAT64 && (defined AARCH64 || defined X86_64)
861 /*
862  * Linux x86_64 has unified `struct stat' but its i386 biarch needs
863  * `struct stat64'.  Its <asm-i386/stat.h> definition expects 32-bit `long'.
864  * <linux/include/asm-x86_64/ia32.h> is not in the public includes set.
865  * __GNUC__ is needed for the required __attribute__ below.
866  *
867  * Similarly, aarch64 has a unified `struct stat' but its arm personality
868  * needs `struct stat64' (which also expects a 32-bit `long' but which
869  * shouldn't be packed).
870  */
871 struct stat64 {
872         unsigned long long      st_dev;
873         unsigned char   __pad0[4];
874         unsigned int    __st_ino;
875         unsigned int    st_mode;
876         unsigned int    st_nlink;
877         unsigned int    st_uid;
878         unsigned int    st_gid;
879         unsigned long long      st_rdev;
880         unsigned char   __pad3[4];
881         long long       st_size;
882         unsigned int    st_blksize;
883         unsigned long long      st_blocks;
884         unsigned int    st_atime;
885         unsigned int    st_atime_nsec;
886         unsigned int    st_mtime;
887         unsigned int    st_mtime_nsec;
888         unsigned int    st_ctime;
889         unsigned int    st_ctime_nsec;
890         unsigned long long      st_ino;
891 }
892 # if defined X86_64
893    __attribute__((packed))
894 #  define STAT64_SIZE   96
895 #else
896 #  define STAT64_SIZE   104
897 # endif
898 ;
899 # define HAVE_STAT64    1
900 #endif
901
902 #ifdef HAVE_STAT64
903 static void
904 printstat64(struct tcb *tcp, long addr)
905 {
906 #ifdef X32
907         struct stat statbuf;
908 #else
909         struct stat64 statbuf;
910 #endif
911
912 #ifdef STAT64_SIZE
913         (void) sizeof(char[sizeof statbuf == STAT64_SIZE ? 1 : -1]);
914 #endif
915
916         if (!addr) {
917                 tprints("NULL");
918                 return;
919         }
920         if (syserror(tcp) || !verbose(tcp)) {
921                 tprintf("%#lx", addr);
922                 return;
923         }
924
925 #if defined(SPARC) || defined(SPARC64)
926         if (current_personality == 1) {
927                 printstatsol(tcp, addr);
928                 return;
929         }
930 # ifdef SPARC64
931         else if (current_personality == 2) {
932                 printstat_sparc64(tcp, addr);
933                 return;
934         }
935 # endif
936 #endif /* SPARC[64] */
937
938 #if defined AARCH64
939         if (current_personality != 0) {
940                 printstat(tcp, addr);
941                 return;
942         }
943 #endif
944 #if defined X86_64
945         if (current_personality != 1) {
946                 printstat(tcp, addr);
947                 return;
948         }
949 #endif
950
951         if (umove(tcp, addr, &statbuf) < 0) {
952                 tprints("{...}");
953                 return;
954         }
955
956         if (!abbrev(tcp)) {
957                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%llu, st_mode=%s, ",
958                         (unsigned long) major(statbuf.st_dev),
959                         (unsigned long) minor(statbuf.st_dev),
960                         (unsigned long long) statbuf.st_ino,
961                         sprintmode(statbuf.st_mode));
962                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
963                         (unsigned long) statbuf.st_nlink,
964                         (unsigned long) statbuf.st_uid,
965                         (unsigned long) statbuf.st_gid);
966 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
967                 tprintf("st_blksize=%lu, ",
968                         (unsigned long) statbuf.st_blksize);
969 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
970 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
971                 tprintf("st_blocks=%lu, ", (unsigned long) statbuf.st_blocks);
972 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
973         }
974         else
975                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
976         switch (statbuf.st_mode & S_IFMT) {
977         case S_IFCHR: case S_IFBLK:
978 #ifdef HAVE_STRUCT_STAT_ST_RDEV
979                 tprintf("st_rdev=makedev(%lu, %lu), ",
980                         (unsigned long) major(statbuf.st_rdev),
981                         (unsigned long) minor(statbuf.st_rdev));
982 #else /* !HAVE_STRUCT_STAT_ST_RDEV */
983                 tprintf("st_size=makedev(%lu, %lu), ",
984                         (unsigned long) major(statbuf.st_size),
985                         (unsigned long) minor(statbuf.st_size));
986 #endif /* !HAVE_STRUCT_STAT_ST_RDEV */
987                 break;
988         default:
989                 tprintf("st_size=%llu, ", (unsigned long long) statbuf.st_size);
990                 break;
991         }
992         if (!abbrev(tcp)) {
993                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));
994                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));
995                 tprintf("st_ctime=%s", sprinttime(statbuf.st_ctime));
996 #if HAVE_STRUCT_STAT_ST_FLAGS
997                 tprints(", st_flags=");
998                 printflags(fileflags, statbuf.st_flags, "UF_???");
999 #endif
1000 #if HAVE_STRUCT_STAT_ST_ACLCNT
1001                 tprintf(", st_aclcnt=%d", statbuf.st_aclcnt);
1002 #endif
1003 #if HAVE_STRUCT_STAT_ST_LEVEL
1004                 tprintf(", st_level=%ld", statbuf.st_level);
1005 #endif
1006 #if HAVE_STRUCT_STAT_ST_FSTYPE
1007                 tprintf(", st_fstype=%.*s",
1008                         (int) sizeof statbuf.st_fstype, statbuf.st_fstype);
1009 #endif
1010 #if HAVE_STRUCT_STAT_ST_GEN
1011                 tprintf(", st_gen=%u", statbuf.st_gen);
1012 #endif
1013                 tprints("}");
1014         }
1015         else
1016                 tprints("...}");
1017 }
1018 #endif /* HAVE_STAT64 */
1019
1020 #if defined(HAVE_STRUCT___OLD_KERNEL_STAT)
1021 static void
1022 convertoldstat(const struct __old_kernel_stat *oldbuf, struct stat *newbuf)
1023 {
1024         newbuf->st_dev = oldbuf->st_dev;
1025         newbuf->st_ino = oldbuf->st_ino;
1026         newbuf->st_mode = oldbuf->st_mode;
1027         newbuf->st_nlink = oldbuf->st_nlink;
1028         newbuf->st_uid = oldbuf->st_uid;
1029         newbuf->st_gid = oldbuf->st_gid;
1030         newbuf->st_rdev = oldbuf->st_rdev;
1031         newbuf->st_size = oldbuf->st_size;
1032         newbuf->st_atime = oldbuf->st_atime;
1033         newbuf->st_mtime = oldbuf->st_mtime;
1034         newbuf->st_ctime = oldbuf->st_ctime;
1035         newbuf->st_blksize = 0; /* not supported in old_stat */
1036         newbuf->st_blocks = 0; /* not supported in old_stat */
1037 }
1038
1039 static void
1040 printoldstat(struct tcb *tcp, long addr)
1041 {
1042         struct __old_kernel_stat statbuf;
1043         struct stat newstatbuf;
1044
1045         if (!addr) {
1046                 tprints("NULL");
1047                 return;
1048         }
1049         if (syserror(tcp) || !verbose(tcp)) {
1050                 tprintf("%#lx", addr);
1051                 return;
1052         }
1053
1054 # if defined(SPARC) || defined(SPARC64)
1055         if (current_personality == 1) {
1056                 printstatsol(tcp, addr);
1057                 return;
1058         }
1059 # endif
1060
1061         if (umove(tcp, addr, &statbuf) < 0) {
1062                 tprints("{...}");
1063                 return;
1064         }
1065
1066         convertoldstat(&statbuf, &newstatbuf);
1067         realprintstat(tcp, &newstatbuf);
1068 }
1069 #endif
1070
1071 int
1072 sys_stat(struct tcb *tcp)
1073 {
1074         if (entering(tcp)) {
1075                 printpath(tcp, tcp->u_arg[0]);
1076                 tprints(", ");
1077         } else {
1078                 printstat(tcp, tcp->u_arg[1]);
1079         }
1080         return 0;
1081 }
1082
1083 #ifdef X32
1084 static void
1085 printstat64_x32(struct tcb *tcp, long addr)
1086 {
1087         struct stat64 statbuf;
1088
1089         if (!addr) {
1090                 tprints("NULL");
1091                 return;
1092         }
1093         if (syserror(tcp) || !verbose(tcp)) {
1094                 tprintf("%#lx", addr);
1095                 return;
1096         }
1097
1098         if (umove(tcp, addr, &statbuf) < 0) {
1099                 tprints("{...}");
1100                 return;
1101         }
1102
1103         if (!abbrev(tcp)) {
1104                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%llu, st_mode=%s, ",
1105                         (unsigned long) major(statbuf.st_dev),
1106                         (unsigned long) minor(statbuf.st_dev),
1107                         (unsigned long long) statbuf.st_ino,
1108                         sprintmode(statbuf.st_mode));
1109                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
1110                         (unsigned long) statbuf.st_nlink,
1111                         (unsigned long) statbuf.st_uid,
1112                         (unsigned long) statbuf.st_gid);
1113                 tprintf("st_blksize=%lu, ",
1114                         (unsigned long) statbuf.st_blksize);
1115                 tprintf("st_blocks=%lu, ", (unsigned long) statbuf.st_blocks);
1116         }
1117         else
1118                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
1119         switch (statbuf.st_mode & S_IFMT) {
1120         case S_IFCHR: case S_IFBLK:
1121                 tprintf("st_rdev=makedev(%lu, %lu), ",
1122                         (unsigned long) major(statbuf.st_rdev),
1123                         (unsigned long) minor(statbuf.st_rdev));
1124                 break;
1125         default:
1126                 tprintf("st_size=%llu, ", (unsigned long long) statbuf.st_size);
1127                 break;
1128         }
1129         if (!abbrev(tcp)) {
1130                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));
1131                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));
1132                 tprintf("st_ctime=%s", sprinttime(statbuf.st_ctime));
1133                 tprints("}");
1134         }
1135         else
1136                 tprints("...}");
1137 }
1138 #endif /* X32 */
1139
1140 int
1141 sys_stat64(struct tcb *tcp)
1142 {
1143 #ifdef HAVE_STAT64
1144         if (entering(tcp)) {
1145                 printpath(tcp, tcp->u_arg[0]);
1146                 tprints(", ");
1147         } else {
1148 # ifdef X32
1149                 printstat64_x32(tcp, tcp->u_arg[1]);
1150 # else
1151                 printstat64(tcp, tcp->u_arg[1]);
1152 # endif
1153         }
1154         return 0;
1155 #else
1156         return printargs(tcp);
1157 #endif
1158 }
1159
1160 #ifndef AT_SYMLINK_NOFOLLOW
1161 # define AT_SYMLINK_NOFOLLOW    0x100
1162 #endif
1163 #ifndef AT_REMOVEDIR
1164 # define AT_REMOVEDIR           0x200
1165 #endif
1166 #ifndef AT_SYMLINK_FOLLOW
1167 # define AT_SYMLINK_FOLLOW      0x400
1168 #endif
1169 #ifndef AT_NO_AUTOMOUNT
1170 # define AT_NO_AUTOMOUNT        0x800
1171 #endif
1172 #ifndef AT_EMPTY_PATH
1173 # define AT_EMPTY_PATH          0x1000
1174 #endif
1175
1176 #include "xlat/at_flags.h"
1177
1178 int
1179 sys_newfstatat(struct tcb *tcp)
1180 {
1181         if (entering(tcp)) {
1182                 print_dirfd(tcp, tcp->u_arg[0]);
1183                 printpath(tcp, tcp->u_arg[1]);
1184                 tprints(", ");
1185         } else {
1186 #ifdef POWERPC64
1187                 if (current_personality == 0)
1188                         printstat(tcp, tcp->u_arg[2]);
1189                 else
1190                         printstat64(tcp, tcp->u_arg[2]);
1191 #elif defined HAVE_STAT64
1192                 printstat64(tcp, tcp->u_arg[2]);
1193 #else
1194                 printstat(tcp, tcp->u_arg[2]);
1195 #endif
1196                 tprints(", ");
1197                 printflags(at_flags, tcp->u_arg[3], "AT_???");
1198         }
1199         return 0;
1200 }
1201
1202 #if defined(HAVE_STRUCT___OLD_KERNEL_STAT)
1203 int
1204 sys_oldstat(struct tcb *tcp)
1205 {
1206         if (entering(tcp)) {
1207                 printpath(tcp, tcp->u_arg[0]);
1208                 tprints(", ");
1209         } else {
1210                 printoldstat(tcp, tcp->u_arg[1]);
1211         }
1212         return 0;
1213 }
1214 #endif
1215
1216 int
1217 sys_fstat(struct tcb *tcp)
1218 {
1219         if (entering(tcp)) {
1220                 printfd(tcp, tcp->u_arg[0]);
1221                 tprints(", ");
1222         } else {
1223                 printstat(tcp, tcp->u_arg[1]);
1224         }
1225         return 0;
1226 }
1227
1228 int
1229 sys_fstat64(struct tcb *tcp)
1230 {
1231 #ifdef HAVE_STAT64
1232         if (entering(tcp)) {
1233                 printfd(tcp, tcp->u_arg[0]);
1234                 tprints(", ");
1235         } else {
1236 # ifdef X32
1237                 printstat64_x32(tcp, tcp->u_arg[1]);
1238 # else
1239                 printstat64(tcp, tcp->u_arg[1]);
1240 # endif
1241         }
1242         return 0;
1243 #else
1244         return printargs(tcp);
1245 #endif
1246 }
1247
1248 #if defined(HAVE_STRUCT___OLD_KERNEL_STAT)
1249 int
1250 sys_oldfstat(struct tcb *tcp)
1251 {
1252         if (entering(tcp)) {
1253                 printfd(tcp, tcp->u_arg[0]);
1254                 tprints(", ");
1255         } else {
1256                 printoldstat(tcp, tcp->u_arg[1]);
1257         }
1258         return 0;
1259 }
1260 #endif
1261
1262 #if defined(SPARC) || defined(SPARC64)
1263
1264 int
1265 sys_xstat(struct tcb *tcp)
1266 {
1267         if (entering(tcp)) {
1268                 tprintf("%ld, ", tcp->u_arg[0]);
1269                 printpath(tcp, tcp->u_arg[1]);
1270                 tprints(", ");
1271         } else {
1272 # ifdef _STAT64_VER
1273                 if (tcp->u_arg[0] == _STAT64_VER)
1274                         printstat64(tcp, tcp->u_arg[2]);
1275                 else
1276 # endif
1277                 printstat(tcp, tcp->u_arg[2]);
1278         }
1279         return 0;
1280 }
1281
1282 int
1283 sys_fxstat(struct tcb *tcp)
1284 {
1285         if (entering(tcp))
1286                 tprintf("%ld, %ld, ", tcp->u_arg[0], tcp->u_arg[1]);
1287         else {
1288 # ifdef _STAT64_VER
1289                 if (tcp->u_arg[0] == _STAT64_VER)
1290                         printstat64(tcp, tcp->u_arg[2]);
1291                 else
1292 # endif
1293                 printstat(tcp, tcp->u_arg[2]);
1294         }
1295         return 0;
1296 }
1297
1298 int
1299 sys_lxstat(struct tcb *tcp)
1300 {
1301         if (entering(tcp)) {
1302                 tprintf("%ld, ", tcp->u_arg[0]);
1303                 printpath(tcp, tcp->u_arg[1]);
1304                 tprints(", ");
1305         } else {
1306 # ifdef _STAT64_VER
1307                 if (tcp->u_arg[0] == _STAT64_VER)
1308                         printstat64(tcp, tcp->u_arg[2]);
1309                 else
1310 # endif
1311                 printstat(tcp, tcp->u_arg[2]);
1312         }
1313         return 0;
1314 }
1315
1316 int
1317 sys_xmknod(struct tcb *tcp)
1318 {
1319         int mode = tcp->u_arg[2];
1320
1321         if (entering(tcp)) {
1322                 tprintf("%ld, ", tcp->u_arg[0]);
1323                 printpath(tcp, tcp->u_arg[1]);
1324                 tprintf(", %s", sprintmode(mode));
1325                 switch (mode & S_IFMT) {
1326                 case S_IFCHR: case S_IFBLK:
1327                         tprintf(", makedev(%lu, %lu)",
1328                                 (unsigned long) ((tcp->u_arg[3] >> 18) & 0x3fff),
1329                                 (unsigned long) (tcp->u_arg[3] & 0x3ffff));
1330                         break;
1331                 default:
1332                         break;
1333                 }
1334         }
1335         return 0;
1336 }
1337
1338 # ifdef HAVE_SYS_ACL_H
1339
1340 #  include <sys/acl.h>
1341
1342 #include "xlat/aclcmds.h"
1343
1344 int
1345 sys_acl(struct tcb *tcp)
1346 {
1347         if (entering(tcp)) {
1348                 printpath(tcp, tcp->u_arg[0]);
1349                 tprints(", ");
1350                 printxval(aclcmds, tcp->u_arg[1], "???ACL???");
1351                 tprintf(", %ld", tcp->u_arg[2]);
1352                 /*
1353                  * FIXME - dump out the list of aclent_t's pointed to
1354                  * by "tcp->u_arg[3]" if it's not NULL.
1355                  */
1356                 if (tcp->u_arg[3])
1357                         tprintf(", %#lx", tcp->u_arg[3]);
1358                 else
1359                         tprints(", NULL");
1360         }
1361         return 0;
1362 }
1363
1364 int
1365 sys_facl(struct tcb *tcp)
1366 {
1367         if (entering(tcp)) {
1368                 tprintf("%ld, ", tcp->u_arg[0]);
1369                 printxval(aclcmds, tcp->u_arg[1], "???ACL???");
1370                 tprintf(", %ld", tcp->u_arg[2]);
1371                 /*
1372                  * FIXME - dump out the list of aclent_t's pointed to
1373                  * by "tcp->u_arg[3]" if it's not NULL.
1374                  */
1375                 if (tcp->u_arg[3])
1376                         tprintf(", %#lx", tcp->u_arg[3]);
1377                 else
1378                         tprints(", NULL");
1379         }
1380         return 0;
1381 }
1382
1383 #include "xlat/aclipc.h"
1384
1385 int
1386 sys_aclipc(struct tcb *tcp)
1387 {
1388         if (entering(tcp)) {
1389                 printxval(aclipc, tcp->u_arg[0], "???IPC???");
1390                 tprintf(", %#lx, ", tcp->u_arg[1]);
1391                 printxval(aclcmds, tcp->u_arg[2], "???ACL???");
1392                 tprintf(", %ld", tcp->u_arg[3]);
1393                 /*
1394                  * FIXME - dump out the list of aclent_t's pointed to
1395                  * by "tcp->u_arg[4]" if it's not NULL.
1396                  */
1397                 if (tcp->u_arg[4])
1398                         tprintf(", %#lx", tcp->u_arg[4]);
1399                 else
1400                         tprints(", NULL");
1401         }
1402         return 0;
1403 }
1404
1405 # endif /* HAVE_SYS_ACL_H */
1406
1407 #endif /* SPARC[64] */
1408
1409 /* directory */
1410 int
1411 sys_chdir(struct tcb *tcp)
1412 {
1413         if (entering(tcp)) {
1414                 printpath(tcp, tcp->u_arg[0]);
1415         }
1416         return 0;
1417 }
1418
1419 static int
1420 decode_mkdir(struct tcb *tcp, int offset)
1421 {
1422         if (entering(tcp)) {
1423                 printpath(tcp, tcp->u_arg[offset]);
1424                 tprintf(", %#lo", tcp->u_arg[offset + 1]);
1425         }
1426         return 0;
1427 }
1428
1429 int
1430 sys_mkdir(struct tcb *tcp)
1431 {
1432         return decode_mkdir(tcp, 0);
1433 }
1434
1435 int
1436 sys_mkdirat(struct tcb *tcp)
1437 {
1438         if (entering(tcp))
1439                 print_dirfd(tcp, tcp->u_arg[0]);
1440         return decode_mkdir(tcp, 1);
1441 }
1442
1443 int
1444 sys_link(struct tcb *tcp)
1445 {
1446         if (entering(tcp)) {
1447                 printpath(tcp, tcp->u_arg[0]);
1448                 tprints(", ");
1449                 printpath(tcp, tcp->u_arg[1]);
1450         }
1451         return 0;
1452 }
1453
1454 int
1455 sys_linkat(struct tcb *tcp)
1456 {
1457         if (entering(tcp)) {
1458                 print_dirfd(tcp, tcp->u_arg[0]);
1459                 printpath(tcp, tcp->u_arg[1]);
1460                 tprints(", ");
1461                 print_dirfd(tcp, tcp->u_arg[2]);
1462                 printpath(tcp, tcp->u_arg[3]);
1463                 tprints(", ");
1464                 printflags(at_flags, tcp->u_arg[4], "AT_???");
1465         }
1466         return 0;
1467 }
1468
1469 int
1470 sys_unlinkat(struct tcb *tcp)
1471 {
1472         if (entering(tcp)) {
1473                 print_dirfd(tcp, tcp->u_arg[0]);
1474                 printpath(tcp, tcp->u_arg[1]);
1475                 tprints(", ");
1476                 printflags(at_flags, tcp->u_arg[2], "AT_???");
1477         }
1478         return 0;
1479 }
1480
1481 int
1482 sys_symlinkat(struct tcb *tcp)
1483 {
1484         if (entering(tcp)) {
1485                 printpath(tcp, tcp->u_arg[0]);
1486                 tprints(", ");
1487                 print_dirfd(tcp, tcp->u_arg[1]);
1488                 printpath(tcp, tcp->u_arg[2]);
1489         }
1490         return 0;
1491 }
1492
1493 static int
1494 decode_readlink(struct tcb *tcp, int offset)
1495 {
1496         if (entering(tcp)) {
1497                 printpath(tcp, tcp->u_arg[offset]);
1498                 tprints(", ");
1499         } else {
1500                 if (syserror(tcp))
1501                         tprintf("%#lx", tcp->u_arg[offset + 1]);
1502                 else
1503                         /* Used to use printpathn(), but readlink
1504                          * neither includes NUL in the returned count,
1505                          * nor actually writes it into memory.
1506                          * printpathn() would decide on printing
1507                          * "..." continuation based on garbage
1508                          * past return buffer's end.
1509                          */
1510                         printstr(tcp, tcp->u_arg[offset + 1], tcp->u_rval);
1511                 tprintf(", %lu", tcp->u_arg[offset + 2]);
1512         }
1513         return 0;
1514 }
1515
1516 int
1517 sys_readlink(struct tcb *tcp)
1518 {
1519         return decode_readlink(tcp, 0);
1520 }
1521
1522 int
1523 sys_readlinkat(struct tcb *tcp)
1524 {
1525         if (entering(tcp))
1526                 print_dirfd(tcp, tcp->u_arg[0]);
1527         return decode_readlink(tcp, 1);
1528 }
1529
1530 static void
1531 decode_renameat(struct tcb *tcp)
1532 {
1533         print_dirfd(tcp, tcp->u_arg[0]);
1534         printpath(tcp, tcp->u_arg[1]);
1535         tprints(", ");
1536         print_dirfd(tcp, tcp->u_arg[2]);
1537         printpath(tcp, tcp->u_arg[3]);
1538 }
1539
1540 int
1541 sys_renameat(struct tcb *tcp)
1542 {
1543         if (entering(tcp)) {
1544                 decode_renameat(tcp);
1545         }
1546         return 0;
1547 }
1548
1549 #include "xlat/rename_flags.h"
1550
1551 int
1552 sys_renameat2(struct tcb *tcp)
1553 {
1554         if (entering(tcp)) {
1555                 decode_renameat(tcp);
1556                 tprints(", ");
1557                 printflags(rename_flags, tcp->u_arg[4], "RENAME_??");
1558         }
1559         return 0;
1560 }
1561
1562 int
1563 sys_chown(struct tcb *tcp)
1564 {
1565         if (entering(tcp)) {
1566                 printpath(tcp, tcp->u_arg[0]);
1567                 printuid(", ", tcp->u_arg[1]);
1568                 printuid(", ", tcp->u_arg[2]);
1569         }
1570         return 0;
1571 }
1572
1573 int
1574 sys_fchownat(struct tcb *tcp)
1575 {
1576         if (entering(tcp)) {
1577                 print_dirfd(tcp, tcp->u_arg[0]);
1578                 printpath(tcp, tcp->u_arg[1]);
1579                 printuid(", ", tcp->u_arg[2]);
1580                 printuid(", ", tcp->u_arg[3]);
1581                 tprints(", ");
1582                 printflags(at_flags, tcp->u_arg[4], "AT_???");
1583         }
1584         return 0;
1585 }
1586
1587 int
1588 sys_fchown(struct tcb *tcp)
1589 {
1590         if (entering(tcp)) {
1591                 printfd(tcp, tcp->u_arg[0]);
1592                 printuid(", ", tcp->u_arg[1]);
1593                 printuid(", ", tcp->u_arg[2]);
1594         }
1595         return 0;
1596 }
1597
1598 static int
1599 decode_chmod(struct tcb *tcp, int offset)
1600 {
1601         if (entering(tcp)) {
1602                 printpath(tcp, tcp->u_arg[offset]);
1603                 tprintf(", %#lo", tcp->u_arg[offset + 1]);
1604         }
1605         return 0;
1606 }
1607
1608 int
1609 sys_chmod(struct tcb *tcp)
1610 {
1611         return decode_chmod(tcp, 0);
1612 }
1613
1614 int
1615 sys_fchmodat(struct tcb *tcp)
1616 {
1617         if (entering(tcp))
1618                 print_dirfd(tcp, tcp->u_arg[0]);
1619         return decode_chmod(tcp, 1);
1620 }
1621
1622 int
1623 sys_fchmod(struct tcb *tcp)
1624 {
1625         if (entering(tcp)) {
1626                 printfd(tcp, tcp->u_arg[0]);
1627                 tprintf(", %#lo", tcp->u_arg[1]);
1628         }
1629         return 0;
1630 }
1631
1632 #ifdef ALPHA
1633 int
1634 sys_osf_utimes(struct tcb *tcp)
1635 {
1636         if (entering(tcp)) {
1637                 printpath(tcp, tcp->u_arg[0]);
1638                 tprints(", ");
1639                 printtv_bitness(tcp, tcp->u_arg[1], BITNESS_32,  0);
1640         }
1641         return 0;
1642 }
1643 #endif
1644
1645 static int
1646 decode_utimes(struct tcb *tcp, int offset, int special)
1647 {
1648         if (entering(tcp)) {
1649                 printpath(tcp, tcp->u_arg[offset]);
1650                 tprints(", ");
1651                 if (tcp->u_arg[offset + 1] == 0)
1652                         tprints("NULL");
1653                 else {
1654                         tprints("{");
1655                         printtv_bitness(tcp, tcp->u_arg[offset + 1],
1656                                         BITNESS_CURRENT, special);
1657                         tprints(", ");
1658                         printtv_bitness(tcp, tcp->u_arg[offset + 1]
1659                                         + sizeof(struct timeval),
1660                                         BITNESS_CURRENT, special);
1661                         tprints("}");
1662                 }
1663         }
1664         return 0;
1665 }
1666
1667 int
1668 sys_utimes(struct tcb *tcp)
1669 {
1670         return decode_utimes(tcp, 0, 0);
1671 }
1672
1673 int
1674 sys_futimesat(struct tcb *tcp)
1675 {
1676         if (entering(tcp))
1677                 print_dirfd(tcp, tcp->u_arg[0]);
1678         return decode_utimes(tcp, 1, 0);
1679 }
1680
1681 int
1682 sys_utimensat(struct tcb *tcp)
1683 {
1684         if (entering(tcp)) {
1685                 print_dirfd(tcp, tcp->u_arg[0]);
1686                 decode_utimes(tcp, 1, 1);
1687                 tprints(", ");
1688                 printflags(at_flags, tcp->u_arg[3], "AT_???");
1689         }
1690         return 0;
1691 }
1692
1693 int
1694 sys_utime(struct tcb *tcp)
1695 {
1696         union {
1697                 long utl[2];
1698                 int uti[2];
1699                 long paranoia_for_huge_wordsize[4];
1700         } u;
1701         unsigned wordsize;
1702
1703         if (entering(tcp)) {
1704                 printpath(tcp, tcp->u_arg[0]);
1705                 tprints(", ");
1706
1707                 wordsize = current_wordsize;
1708                 if (!tcp->u_arg[1])
1709                         tprints("NULL");
1710                 else if (!verbose(tcp))
1711                         tprintf("%#lx", tcp->u_arg[1]);
1712                 else if (umoven(tcp, tcp->u_arg[1], 2 * wordsize, (char *) &u) < 0)
1713                         tprints("[?, ?]");
1714                 else if (wordsize == sizeof u.utl[0]) {
1715                         tprintf("[%s,", sprinttime(u.utl[0]));
1716                         tprintf(" %s]", sprinttime(u.utl[1]));
1717                 }
1718                 else if (wordsize == sizeof u.uti[0]) {
1719                         tprintf("[%s,", sprinttime(u.uti[0]));
1720                         tprintf(" %s]", sprinttime(u.uti[1]));
1721                 }
1722                 else
1723                         tprintf("<decode error: unsupported wordsize %d>",
1724                                 wordsize);
1725         }
1726         return 0;
1727 }
1728
1729 static int
1730 decode_mknod(struct tcb *tcp, int offset)
1731 {
1732         int mode = tcp->u_arg[offset + 1];
1733
1734         if (entering(tcp)) {
1735                 printpath(tcp, tcp->u_arg[offset]);
1736                 tprintf(", %s", sprintmode(mode));
1737                 switch (mode & S_IFMT) {
1738                 case S_IFCHR:
1739                 case S_IFBLK:
1740 #if defined(SPARC) || defined(SPARC64)
1741                         if (current_personality == 1)
1742                                 tprintf(", makedev(%lu, %lu)",
1743                                 (unsigned long) ((tcp->u_arg[offset + 2] >> 18) & 0x3fff),
1744                                 (unsigned long) (tcp->u_arg[offset + 2] & 0x3ffff));
1745                         else
1746 #endif
1747                                 tprintf(", makedev(%lu, %lu)",
1748                                 (unsigned long) major(tcp->u_arg[offset + 2]),
1749                                 (unsigned long) minor(tcp->u_arg[offset + 2]));
1750                         break;
1751                 default:
1752                         break;
1753                 }
1754         }
1755         return 0;
1756 }
1757
1758 int
1759 sys_mknod(struct tcb *tcp)
1760 {
1761         return decode_mknod(tcp, 0);
1762 }
1763
1764 int
1765 sys_mknodat(struct tcb *tcp)
1766 {
1767         if (entering(tcp))
1768                 print_dirfd(tcp, tcp->u_arg[0]);
1769         return decode_mknod(tcp, 1);
1770 }
1771
1772 int
1773 sys_getcwd(struct tcb *tcp)
1774 {
1775         if (exiting(tcp)) {
1776                 if (syserror(tcp))
1777                         tprintf("%#lx", tcp->u_arg[0]);
1778                 else
1779                         printpathn(tcp, tcp->u_arg[0], tcp->u_rval - 1);
1780                 tprintf(", %lu", tcp->u_arg[1]);
1781         }
1782         return 0;
1783 }
1784
1785 #include "xlat/xattrflags.h"
1786
1787 static void
1788 print_xattr_val(struct tcb *tcp, int failed,
1789                 unsigned long arg,
1790                 unsigned long insize,
1791                 unsigned long size)
1792 {
1793         if (insize == 0)
1794                 failed = 1;
1795         if (!failed) {
1796                 unsigned long capacity = 4 * size + 1;
1797                 unsigned char *buf = (capacity < size) ? NULL : malloc(capacity);
1798                 if (buf == NULL || /* probably a bogus size argument */
1799                         umoven(tcp, arg, size, (char *) &buf[3 * size]) < 0) {
1800                         failed = 1;
1801                 }
1802                 else {
1803                         unsigned char *out = buf;
1804                         unsigned char *in = &buf[3 * size];
1805                         size_t i;
1806                         for (i = 0; i < size; ++i) {
1807                                 if (in[i] >= ' ' && in[i] <= 0x7e)
1808                                         *out++ = in[i];
1809                                 else {
1810 #define tohex(n) "0123456789abcdef"[n]
1811                                         *out++ = '\\';
1812                                         *out++ = 'x';
1813                                         *out++ = tohex(in[i] / 16);
1814                                         *out++ = tohex(in[i] % 16);
1815                                 }
1816                         }
1817                         /* Don't print terminating NUL if there is one.  */
1818                         if (i > 0 && in[i - 1] == '\0')
1819                                 out -= 4;
1820                         *out = '\0';
1821                         tprintf(", \"%s\", %ld", buf, insize);
1822                 }
1823                 free(buf);
1824         }
1825         if (failed)
1826                 tprintf(", 0x%lx, %ld", arg, insize);
1827 }
1828
1829 int
1830 sys_setxattr(struct tcb *tcp)
1831 {
1832         if (entering(tcp)) {
1833                 printpath(tcp, tcp->u_arg[0]);
1834                 tprints(", ");
1835                 printstr(tcp, tcp->u_arg[1], -1);
1836                 print_xattr_val(tcp, 0, tcp->u_arg[2], tcp->u_arg[3], tcp->u_arg[3]);
1837                 tprints(", ");
1838                 printflags(xattrflags, tcp->u_arg[4], "XATTR_???");
1839         }
1840         return 0;
1841 }
1842
1843 int
1844 sys_fsetxattr(struct tcb *tcp)
1845 {
1846         if (entering(tcp)) {
1847                 printfd(tcp, tcp->u_arg[0]);
1848                 tprints(", ");
1849                 printstr(tcp, tcp->u_arg[1], -1);
1850                 print_xattr_val(tcp, 0, tcp->u_arg[2], tcp->u_arg[3], tcp->u_arg[3]);
1851                 tprints(", ");
1852                 printflags(xattrflags, tcp->u_arg[4], "XATTR_???");
1853         }
1854         return 0;
1855 }
1856
1857 int
1858 sys_getxattr(struct tcb *tcp)
1859 {
1860         if (entering(tcp)) {
1861                 printpath(tcp, tcp->u_arg[0]);
1862                 tprints(", ");
1863                 printstr(tcp, tcp->u_arg[1], -1);
1864         } else {
1865                 print_xattr_val(tcp, syserror(tcp), tcp->u_arg[2], tcp->u_arg[3],
1866                                 tcp->u_rval);
1867         }
1868         return 0;
1869 }
1870
1871 int
1872 sys_fgetxattr(struct tcb *tcp)
1873 {
1874         if (entering(tcp)) {
1875                 printfd(tcp, tcp->u_arg[0]);
1876                 tprints(", ");
1877                 printstr(tcp, tcp->u_arg[1], -1);
1878         } else {
1879                 print_xattr_val(tcp, syserror(tcp), tcp->u_arg[2], tcp->u_arg[3],
1880                                 tcp->u_rval);
1881         }
1882         return 0;
1883 }
1884
1885 static void
1886 print_xattr_list(struct tcb *tcp, unsigned long addr, unsigned long size)
1887 {
1888         if (syserror(tcp)) {
1889                 tprintf("%#lx", addr);
1890         } else {
1891                 if (!addr) {
1892                         tprints("NULL");
1893                 } else {
1894                         unsigned long len =
1895                                 (size < (unsigned long) tcp->u_rval) ?
1896                                         size : (unsigned long) tcp->u_rval;
1897                         printstr(tcp, addr, len);
1898                 }
1899         }
1900         tprintf(", %lu", size);
1901 }
1902
1903 int
1904 sys_listxattr(struct tcb *tcp)
1905 {
1906         if (entering(tcp)) {
1907                 printpath(tcp, tcp->u_arg[0]);
1908                 tprints(", ");
1909         } else {
1910                 print_xattr_list(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1911         }
1912         return 0;
1913 }
1914
1915 int
1916 sys_flistxattr(struct tcb *tcp)
1917 {
1918         if (entering(tcp)) {
1919                 printfd(tcp, tcp->u_arg[0]);
1920                 tprints(", ");
1921         } else {
1922                 print_xattr_list(tcp, tcp->u_arg[1], tcp->u_arg[2]);
1923         }
1924         return 0;
1925 }
1926
1927 int
1928 sys_removexattr(struct tcb *tcp)
1929 {
1930         if (entering(tcp)) {
1931                 printpath(tcp, tcp->u_arg[0]);
1932                 tprints(", ");
1933                 printstr(tcp, tcp->u_arg[1], -1);
1934         }
1935         return 0;
1936 }
1937
1938 int
1939 sys_fremovexattr(struct tcb *tcp)
1940 {
1941         if (entering(tcp)) {
1942                 printfd(tcp, tcp->u_arg[0]);
1943                 tprints(", ");
1944                 printstr(tcp, tcp->u_arg[1], -1);
1945         }
1946         return 0;
1947 }
1948
1949 #include "xlat/advise.h"
1950
1951 int
1952 sys_fadvise64(struct tcb *tcp)
1953 {
1954         if (entering(tcp)) {
1955                 int argn;
1956                 printfd(tcp, tcp->u_arg[0]);
1957                 argn = printllval(tcp, ", %lld", 1);
1958                 tprintf(", %ld, ", tcp->u_arg[argn++]);
1959                 printxval(advise, tcp->u_arg[argn], "POSIX_FADV_???");
1960         }
1961         return 0;
1962 }
1963
1964 int
1965 sys_fadvise64_64(struct tcb *tcp)
1966 {
1967         if (entering(tcp)) {
1968                 int argn;
1969                 printfd(tcp, tcp->u_arg[0]);
1970                 argn = printllval(tcp, ", %lld, ", 1);
1971                 argn = printllval(tcp, "%lld, ", argn);
1972 #if defined __ARM_EABI__ || defined AARCH64 || defined POWERPC || defined XTENSA
1973                 printxval(advise, tcp->u_arg[1], "POSIX_FADV_???");
1974 #else
1975                 printxval(advise, tcp->u_arg[argn], "POSIX_FADV_???");
1976 #endif
1977         }
1978         return 0;
1979 }
1980
1981 #include "xlat/sync_file_range_flags.h"
1982
1983 int
1984 sys_sync_file_range(struct tcb *tcp)
1985 {
1986         if (entering(tcp)) {
1987                 int argn;
1988                 printfd(tcp, tcp->u_arg[0]);
1989                 argn = printllval(tcp, ", %lld, ", 1);
1990                 argn = printllval(tcp, "%lld, ", argn);
1991                 printflags(sync_file_range_flags, tcp->u_arg[argn],
1992                            "SYNC_FILE_RANGE_???");
1993         }
1994         return 0;
1995 }
1996
1997 int
1998 sys_sync_file_range2(struct tcb *tcp)
1999 {
2000         if (entering(tcp)) {
2001                 int argn;
2002                 printfd(tcp, tcp->u_arg[0]);
2003                 printflags(sync_file_range_flags, 1,
2004                            "SYNC_FILE_RANGE_???");
2005                 argn = printllval(tcp, ", %lld, ", 2);
2006                 argn = printllval(tcp, "%lld, ", argn);
2007         }
2008         return 0;
2009 }
2010
2011 int
2012 sys_fallocate(struct tcb *tcp)
2013 {
2014         if (entering(tcp)) {
2015                 int argn;
2016                 printfd(tcp, tcp->u_arg[0]);            /* fd */
2017                 tprintf(", %#lo, ", tcp->u_arg[1]);     /* mode */
2018                 argn = printllval(tcp, "%llu, ", 2);    /* offset */
2019                 printllval(tcp, "%llu", argn);          /* len */
2020         }
2021         return 0;
2022 }
2023
2024 #ifndef SWAP_FLAG_PREFER
2025 # define SWAP_FLAG_PREFER 0x8000
2026 #endif
2027 #ifndef SWAP_FLAG_DISCARD
2028 # define SWAP_FLAG_DISCARD 0x10000
2029 #endif
2030 #include "xlat/swap_flags.h"
2031
2032 int
2033 sys_swapon(struct tcb *tcp)
2034 {
2035         if (entering(tcp)) {
2036                 int flags = tcp->u_arg[1];
2037                 printpath(tcp, tcp->u_arg[0]);
2038                 tprints(", ");
2039                 printflags(swap_flags, flags & ~SWAP_FLAG_PRIO_MASK,
2040                         "SWAP_FLAG_???");
2041                 if (flags & SWAP_FLAG_PREFER)
2042                         tprintf("|%d", flags & SWAP_FLAG_PRIO_MASK);
2043         }
2044         return 0;
2045 }