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