]> granicus.if.org Git - strace/blob - file.c
Fix readlink result display - was printing bogus "..." semi-randomly
[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  *      $Id$
31  */
32
33 #include "defs.h"
34
35 #include <dirent.h>
36
37 #ifdef LINUX
38 struct kernel_dirent {
39         unsigned long   d_ino;
40         unsigned long   d_off;
41         unsigned short  d_reclen;
42         char            d_name[1];
43 };
44 #else
45 # define kernel_dirent dirent
46 #endif
47
48 #ifdef LINUX
49 #  ifdef LINUXSPARC
50 struct stat {
51         unsigned short  st_dev;
52         unsigned int    st_ino;
53         unsigned short  st_mode;
54         short           st_nlink;
55         unsigned short  st_uid;
56         unsigned short  st_gid;
57         unsigned short  st_rdev;
58         unsigned int    st_size;
59         int             st_atime;
60         unsigned int    __unused1;
61         int             st_mtime;
62         unsigned int    __unused2;
63         int             st_ctime;
64         unsigned int    __unused3;
65         int             st_blksize;
66         int             st_blocks;
67         unsigned int    __unused4[2];
68 };
69 #if defined(SPARC64)
70 struct stat_sparc64 {
71         unsigned int    st_dev;
72         unsigned long   st_ino;
73         unsigned int    st_mode;
74         unsigned int    st_nlink;
75         unsigned int    st_uid;
76         unsigned int    st_gid;
77         unsigned int    st_rdev;
78         long            st_size;
79         long            st_atime;
80         long            st_mtime;
81         long            st_ctime;
82         long            st_blksize;
83         long            st_blocks;
84         unsigned long   __unused4[2];
85 };
86 #endif /* SPARC64 */
87 #    define stat kernel_stat
88 #    include <asm/stat.h>
89 #    undef stat
90 #  else
91 #    undef dev_t
92 #    undef ino_t
93 #    undef mode_t
94 #    undef nlink_t
95 #    undef uid_t
96 #    undef gid_t
97 #    undef off_t
98 #    undef loff_t
99
100 #    define dev_t __kernel_dev_t
101 #    define ino_t __kernel_ino_t
102 #    define mode_t __kernel_mode_t
103 #    define nlink_t __kernel_nlink_t
104 #    define uid_t __kernel_uid_t
105 #    define gid_t __kernel_gid_t
106 #    define off_t __kernel_off_t
107 #    define loff_t __kernel_loff_t
108
109 #    include <asm/stat.h>
110
111 #    undef dev_t
112 #    undef ino_t
113 #    undef mode_t
114 #    undef nlink_t
115 #    undef uid_t
116 #    undef gid_t
117 #    undef off_t
118 #    undef loff_t
119
120 #    define dev_t dev_t
121 #    define ino_t ino_t
122 #    define mode_t mode_t
123 #    define nlink_t nlink_t
124 #    define uid_t uid_t
125 #    define gid_t gid_t
126 #    define off_t off_t
127 #    define loff_t loff_t
128 #  endif
129 #  ifdef HPPA   /* asm-parisc/stat.h defines stat64 */
130 #    undef stat64
131 #  endif
132 #  define stat libc_stat
133 #  define stat64 libc_stat64
134 #  include <sys/stat.h>
135 #  undef stat
136 #  undef stat64
137    /* These might be macros. */
138 #  undef st_atime
139 #  undef st_mtime
140 #  undef st_ctime
141 #  ifdef HPPA
142 #    define stat64 hpux_stat64
143 #  endif
144 #else
145 #  include <sys/stat.h>
146 #endif
147
148 #include <fcntl.h>
149
150 #ifdef SVR4
151 #  include <sys/cred.h>
152 #endif /* SVR4 */
153
154 #ifdef HAVE_SYS_VFS_H
155 #include <sys/vfs.h>
156 #endif
157
158 #ifdef HAVE_LINUX_XATTR_H
159 #include <linux/xattr.h>
160 #elif defined linux
161 #define XATTR_CREATE 1
162 #define XATTR_REPLACE 2
163 #endif
164
165 #ifdef FREEBSD
166 #include <sys/param.h>
167 #include <sys/mount.h>
168 #include <sys/stat.h>
169 #endif
170
171 #if _LFS64_LARGEFILE && (defined(LINUX) || defined(SVR4))
172 # ifdef HAVE_INTTYPES_H
173 #  include <inttypes.h>
174 # else
175 #  define PRId64 "lld"
176 #  define PRIu64 "llu"
177 # endif
178 #endif
179
180 #if HAVE_LONG_LONG_OFF_T
181 /*
182  * Ugly hacks for systems that have typedef long long off_t
183  */
184
185 #define stat64 stat
186 #define HAVE_STAT64 1   /* Ugly hack */
187
188 #define sys_stat64      sys_stat
189 #define sys_fstat64     sys_fstat
190 #define sys_lstat64     sys_lstat
191 #define sys_lseek64     sys_lseek
192 #define sys_truncate64  sys_truncate
193 #define sys_ftruncate64 sys_ftruncate
194 #endif
195
196 #ifdef MAJOR_IN_SYSMACROS
197 #include <sys/sysmacros.h>
198 #endif
199
200 #ifdef MAJOR_IN_MKDEV
201 #include <sys/mkdev.h>
202 #endif
203
204 #ifdef HAVE_SYS_ASYNCH_H
205 #include <sys/asynch.h>
206 #endif
207
208 #ifdef SUNOS4
209 #include <ustat.h>
210 #endif
211
212 const struct xlat open_access_modes[] = {
213         { O_RDONLY,     "O_RDONLY"      },
214         { O_WRONLY,     "O_WRONLY"      },
215         { O_RDWR,       "O_RDWR"        },
216 #ifdef O_ACCMODE
217         { O_ACCMODE,    "O_ACCMODE"     },
218 #endif
219         { 0,            NULL            },
220 };
221
222 const struct xlat open_mode_flags[] = {
223         { O_CREAT,      "O_CREAT"       },
224         { O_EXCL,       "O_EXCL"        },
225         { O_NOCTTY,     "O_NOCTTY"      },
226         { O_TRUNC,      "O_TRUNC"       },
227         { O_APPEND,     "O_APPEND"      },
228         { O_NONBLOCK,   "O_NONBLOCK"    },
229 #ifdef O_SYNC
230         { O_SYNC,       "O_SYNC"        },
231 #endif
232 #ifdef O_ASYNC
233         { O_ASYNC,      "O_ASYNC"       },
234 #endif
235 #ifdef O_DSYNC
236         { O_DSYNC,      "O_DSYNC"       },
237 #endif
238 #ifdef O_RSYNC
239         { O_RSYNC,      "O_RSYNC"       },
240 #endif
241 #if defined(O_NDELAY) && (O_NDELAY != O_NONBLOCK)
242         { O_NDELAY,     "O_NDELAY"      },
243 #endif
244 #ifdef O_PRIV
245         { O_PRIV,       "O_PRIV"        },
246 #endif
247 #ifdef O_DIRECT
248         { O_DIRECT,     "O_DIRECT"      },
249 #endif
250 #ifdef O_LARGEFILE
251 # if O_LARGEFILE == 0           /* biarch platforms in 64-bit mode */
252 #  undef O_LARGEFILE
253 #  ifdef SPARC64
254 #   define O_LARGEFILE  0x40000
255 #  elif defined X86_64 || defined S390X
256 #   define O_LARGEFILE  0100000
257 #  endif
258 # endif
259 # ifdef O_LARGEFILE
260         { O_LARGEFILE,  "O_LARGEFILE"   },
261 # endif
262 #endif
263 #ifdef O_DIRECTORY
264         { O_DIRECTORY,  "O_DIRECTORY"   },
265 #endif
266 #ifdef O_NOFOLLOW
267         { O_NOFOLLOW,   "O_NOFOLLOW"    },
268 #endif
269 #ifdef O_NOATIME
270         { O_NOATIME,    "O_NOATIME"     },
271 #endif
272 #ifdef O_CLOEXEC
273         { O_CLOEXEC,    "O_CLOEXEC"     },
274 #endif
275
276 #ifdef FNDELAY
277         { FNDELAY,      "FNDELAY"       },
278 #endif
279 #ifdef FAPPEND
280         { FAPPEND,      "FAPPEND"       },
281 #endif
282 #ifdef FMARK
283         { FMARK,        "FMARK"         },
284 #endif
285 #ifdef FDEFER
286         { FDEFER,       "FDEFER"        },
287 #endif
288 #ifdef FASYNC
289         { FASYNC,       "FASYNC"        },
290 #endif
291 #ifdef FSHLOCK
292         { FSHLOCK,      "FSHLOCK"       },
293 #endif
294 #ifdef FEXLOCK
295         { FEXLOCK,      "FEXLOCK"       },
296 #endif
297 #ifdef FCREAT
298         { FCREAT,       "FCREAT"        },
299 #endif
300 #ifdef FTRUNC
301         { FTRUNC,       "FTRUNC"        },
302 #endif
303 #ifdef FEXCL
304         { FEXCL,        "FEXCL"         },
305 #endif
306 #ifdef FNBIO
307         { FNBIO,        "FNBIO"         },
308 #endif
309 #ifdef FSYNC
310         { FSYNC,        "FSYNC"         },
311 #endif
312 #ifdef FNOCTTY
313         { FNOCTTY,      "FNOCTTY"       },
314 #endif
315 #ifdef O_SHLOCK
316         { O_SHLOCK,     "O_SHLOCK"      },
317 #endif
318 #ifdef O_EXLOCK
319         { O_EXLOCK,     "O_EXLOCK"      },
320 #endif
321         { 0,            NULL            },
322 };
323
324 #ifdef LINUX
325
326 #ifndef AT_FDCWD
327 # define AT_FDCWD                -100
328 #endif
329
330 /* The fd is an "int", so when decoding x86 on x86_64, we need to force sign
331  * extension to get the right value.  We do this by declaring fd as int here.
332  */
333 static void
334 print_dirfd(struct tcb *tcp, int fd)
335 {
336         if (fd == AT_FDCWD)
337                 tprints("AT_FDCWD, ");
338         else {
339                 printfd(tcp, fd);
340                 tprints(", ");
341         }
342 }
343 #endif
344
345 /*
346  * low bits of the open(2) flags define access mode,
347  * other bits are real flags.
348  */
349 const char *
350 sprint_open_modes(mode_t flags)
351 {
352         static char outstr[1024];
353         char *p;
354         char sep = 0;
355         const char *str;
356         const struct xlat *x;
357
358         p = stpcpy(outstr, "flags ");
359         str = xlookup(open_access_modes, flags & 3);
360         if (str) {
361                 p = stpcpy(p, str);
362                 flags &= ~3;
363                 if (!flags)
364                         return outstr;
365                 sep = '|';
366         }
367
368         for (x = open_mode_flags; x->str; x++) {
369                 if ((flags & x->val) == x->val) {
370                         if (sep)
371                                 *p++ = sep;
372                         p = stpcpy(p, x->str);
373                         flags &= ~x->val;
374                         if (!flags)
375                                 return outstr;
376                         sep = '|';
377                 }
378         }
379         /* flags is still nonzero */
380         if (sep)
381                 *p++ = sep;
382         sprintf(p, "%#x", flags);
383         return outstr;
384 }
385
386 void
387 tprint_open_modes(mode_t flags)
388 {
389         tprints(sprint_open_modes(flags) + sizeof("flags"));
390 }
391
392 static int
393 decode_open(struct tcb *tcp, int offset)
394 {
395         if (entering(tcp)) {
396                 printpath(tcp, tcp->u_arg[offset]);
397                 tprints(", ");
398                 /* flags */
399                 tprint_open_modes(tcp->u_arg[offset + 1]);
400                 if (tcp->u_arg[offset + 1] & O_CREAT) {
401                         /* mode */
402                         tprintf(", %#lo", tcp->u_arg[offset + 2]);
403                 }
404         }
405         return 0;
406 }
407
408 int
409 sys_open(struct tcb *tcp)
410 {
411         return decode_open(tcp, 0);
412 }
413
414 #ifdef LINUX
415 int
416 sys_openat(struct tcb *tcp)
417 {
418         if (entering(tcp))
419                 print_dirfd(tcp, tcp->u_arg[0]);
420         return decode_open(tcp, 1);
421 }
422 #endif
423
424 #ifdef LINUXSPARC
425 static const struct xlat openmodessol[] = {
426         { 0,            "O_RDWR"        },
427         { 1,            "O_RDONLY"      },
428         { 2,            "O_WRONLY"      },
429         { 0x80,         "O_NONBLOCK"    },
430         { 8,            "O_APPEND"      },
431         { 0x100,        "O_CREAT"       },
432         { 0x200,        "O_TRUNC"       },
433         { 0x400,        "O_EXCL"        },
434         { 0x800,        "O_NOCTTY"      },
435         { 0x10,         "O_SYNC"        },
436         { 0x40,         "O_DSYNC"       },
437         { 0x8000,       "O_RSYNC"       },
438         { 4,            "O_NDELAY"      },
439         { 0x1000,       "O_PRIV"        },
440         { 0,            NULL            },
441 };
442
443 int
444 solaris_open(struct tcb *tcp)
445 {
446         if (entering(tcp)) {
447                 printpath(tcp, tcp->u_arg[0]);
448                 tprints(", ");
449                 /* flags */
450                 printflags(openmodessol, tcp->u_arg[1] + 1, "O_???");
451                 if (tcp->u_arg[1] & 0x100) {
452                         /* mode */
453                         tprintf(", %#lo", tcp->u_arg[2]);
454                 }
455         }
456         return 0;
457 }
458
459 #endif
460
461 int
462 sys_creat(struct tcb *tcp)
463 {
464         if (entering(tcp)) {
465                 printpath(tcp, tcp->u_arg[0]);
466                 tprintf(", %#lo", tcp->u_arg[1]);
467         }
468         return 0;
469 }
470
471 static const struct xlat access_flags[] = {
472         { F_OK,         "F_OK",         },
473         { R_OK,         "R_OK"          },
474         { W_OK,         "W_OK"          },
475         { X_OK,         "X_OK"          },
476 #ifdef EFF_ONLY_OK
477         { EFF_ONLY_OK,  "EFF_ONLY_OK"   },
478 #endif
479 #ifdef EX_OK
480         { EX_OK,        "EX_OK"         },
481 #endif
482         { 0,            NULL            },
483 };
484
485 static int
486 decode_access(struct tcb *tcp, int offset)
487 {
488         if (entering(tcp)) {
489                 printpath(tcp, tcp->u_arg[offset]);
490                 tprints(", ");
491                 printflags(access_flags, tcp->u_arg[offset + 1], "?_OK");
492         }
493         return 0;
494 }
495
496 int
497 sys_access(struct tcb *tcp)
498 {
499         return decode_access(tcp, 0);
500 }
501
502 #ifdef LINUX
503 int
504 sys_faccessat(struct tcb *tcp)
505 {
506         if (entering(tcp))
507                 print_dirfd(tcp, tcp->u_arg[0]);
508         return decode_access(tcp, 1);
509 }
510 #endif
511
512 int
513 sys_umask(struct tcb *tcp)
514 {
515         if (entering(tcp)) {
516                 tprintf("%#lo", tcp->u_arg[0]);
517         }
518         return RVAL_OCTAL;
519 }
520
521 static const struct xlat whence[] = {
522         { SEEK_SET,     "SEEK_SET"      },
523         { SEEK_CUR,     "SEEK_CUR"      },
524         { SEEK_END,     "SEEK_END"      },
525         { 0,            NULL            },
526 };
527
528 #ifndef HAVE_LONG_LONG_OFF_T
529 #if defined (LINUX_MIPSN32)
530 int
531 sys_lseek(struct tcb *tcp)
532 {
533         long long offset;
534         int _whence;
535
536         if (entering(tcp)) {
537                 printfd(tcp, tcp->u_arg[0]);
538                 tprints(", ");
539                 offset = tcp->ext_arg[1];
540                 _whence = tcp->u_arg[2];
541                 if (_whence == SEEK_SET)
542                         tprintf("%llu, ", offset);
543                 else
544                         tprintf("%lld, ", offset);
545                 printxval(whence, _whence, "SEEK_???");
546         }
547         return RVAL_UDECIMAL;
548 }
549 #else /* !LINUX_MIPSN32 */
550 int
551 sys_lseek(struct tcb *tcp)
552 {
553         off_t offset;
554         int _whence;
555
556         if (entering(tcp)) {
557                 printfd(tcp, tcp->u_arg[0]);
558                 tprints(", ");
559                 offset = tcp->u_arg[1];
560                 _whence = tcp->u_arg[2];
561                 if (_whence == SEEK_SET)
562                         tprintf("%lu, ", offset);
563                 else
564                         tprintf("%ld, ", offset);
565                 printxval(whence, _whence, "SEEK_???");
566         }
567         return RVAL_UDECIMAL;
568 }
569 #endif /* LINUX_MIPSN32 */
570 #endif
571
572 #ifdef LINUX
573 int
574 sys_llseek(struct tcb *tcp)
575 {
576         if (entering(tcp)) {
577                 printfd(tcp, tcp->u_arg[0]);
578                 /*
579                  * This one call takes explicitly two 32-bit arguments hi, lo,
580                  * rather than one 64-bit argument for which LONG_LONG works
581                  * appropriate for the native byte order.
582                  */
583                 if (tcp->u_arg[4] == SEEK_SET)
584                         tprintf(", %llu, ",
585                                 ((long long int) tcp->u_arg[1]) << 32 |
586                                 (unsigned long long) (unsigned) tcp->u_arg[2]);
587                 else
588                         tprintf(", %lld, ",
589                                 ((long long int) tcp->u_arg[1]) << 32 |
590                                 (unsigned long long) (unsigned) tcp->u_arg[2]);
591         }
592         else {
593                 long long int off;
594                 if (syserror(tcp) || umove(tcp, tcp->u_arg[3], &off) < 0)
595                         tprintf("%#lx, ", tcp->u_arg[3]);
596                 else
597                         tprintf("[%llu], ", off);
598                 printxval(whence, tcp->u_arg[4], "SEEK_???");
599         }
600         return 0;
601 }
602
603 int
604 sys_readahead(struct tcb *tcp)
605 {
606         if (entering(tcp)) {
607                 int argn;
608                 printfd(tcp, tcp->u_arg[0]);
609                 tprints(", ");
610                 argn = printllval(tcp, "%lld", 1);
611                 tprintf(", %ld", tcp->u_arg[argn]);
612         }
613         return 0;
614 }
615 #endif
616
617 #if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_T
618 int
619 sys_lseek64(struct tcb *tcp)
620 {
621         if (entering(tcp)) {
622                 int argn;
623                 printfd(tcp, tcp->u_arg[0]);
624                 tprints(", ");
625                 if (tcp->u_arg[3] == SEEK_SET)
626                         argn = printllval(tcp, "%llu, ", 1);
627                 else
628                         argn = printllval(tcp, "%lld, ", 1);
629                 printxval(whence, tcp->u_arg[argn], "SEEK_???");
630         }
631         return RVAL_LUDECIMAL;
632 }
633 #endif
634
635 #ifndef HAVE_LONG_LONG_OFF_T
636 int
637 sys_truncate(struct tcb *tcp)
638 {
639         if (entering(tcp)) {
640                 printpath(tcp, tcp->u_arg[0]);
641                 tprintf(", %lu", tcp->u_arg[1]);
642         }
643         return 0;
644 }
645 #endif
646
647 #if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_T
648 int
649 sys_truncate64(struct tcb *tcp)
650 {
651         if (entering(tcp)) {
652                 printpath(tcp, tcp->u_arg[0]);
653                 printllval(tcp, ", %llu", 1);
654         }
655         return 0;
656 }
657 #endif
658
659 #ifndef HAVE_LONG_LONG_OFF_T
660 int
661 sys_ftruncate(struct tcb *tcp)
662 {
663         if (entering(tcp)) {
664                 printfd(tcp, tcp->u_arg[0]);
665                 tprintf(", %lu", tcp->u_arg[1]);
666         }
667         return 0;
668 }
669 #endif
670
671 #if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_T
672 int
673 sys_ftruncate64(struct tcb *tcp)
674 {
675         if (entering(tcp)) {
676                 printfd(tcp, tcp->u_arg[0]);
677                 tprints(", ");
678                 printllval(tcp, "%llu", 1);
679         }
680         return 0;
681 }
682 #endif
683
684 /* several stats */
685
686 static const struct xlat modetypes[] = {
687         { S_IFREG,      "S_IFREG"       },
688         { S_IFSOCK,     "S_IFSOCK"      },
689         { S_IFIFO,      "S_IFIFO"       },
690         { S_IFLNK,      "S_IFLNK"       },
691         { S_IFDIR,      "S_IFDIR"       },
692         { S_IFBLK,      "S_IFBLK"       },
693         { S_IFCHR,      "S_IFCHR"       },
694         { 0,            NULL            },
695 };
696
697 static const char *
698 sprintmode(int mode)
699 {
700         static char buf[64];
701         const char *s;
702
703         if ((mode & S_IFMT) == 0)
704                 s = "";
705         else if ((s = xlookup(modetypes, mode & S_IFMT)) == NULL) {
706                 sprintf(buf, "%#o", mode);
707                 return buf;
708         }
709         sprintf(buf, "%s%s%s%s", s,
710                 (mode & S_ISUID) ? "|S_ISUID" : "",
711                 (mode & S_ISGID) ? "|S_ISGID" : "",
712                 (mode & S_ISVTX) ? "|S_ISVTX" : "");
713         mode &= ~(S_IFMT|S_ISUID|S_ISGID|S_ISVTX);
714         if (mode)
715                 sprintf(buf + strlen(buf), "|%#o", mode);
716         s = (*buf == '|') ? buf + 1 : buf;
717         return *s ? s : "0";
718 }
719
720 static char *
721 sprinttime(time_t t)
722 {
723         struct tm *tmp;
724         static char buf[32];
725
726         if (t == 0) {
727                 strcpy(buf, "0");
728                 return buf;
729         }
730         tmp = localtime(&t);
731         if (tmp)
732                 snprintf(buf, sizeof buf, "%02d/%02d/%02d-%02d:%02d:%02d",
733                         tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
734                         tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
735         else
736                 snprintf(buf, sizeof buf, "%lu", (unsigned long) t);
737
738         return buf;
739 }
740
741 #ifdef LINUXSPARC
742 typedef struct {
743         int     tv_sec;
744         int     tv_nsec;
745 } timestruct_t;
746
747 struct solstat {
748         unsigned        st_dev;
749         int             st_pad1[3];     /* network id */
750         unsigned        st_ino;
751         unsigned        st_mode;
752         unsigned        st_nlink;
753         unsigned        st_uid;
754         unsigned        st_gid;
755         unsigned        st_rdev;
756         int             st_pad2[2];
757         int             st_size;
758         int             st_pad3;        /* st_size, off_t expansion */
759         timestruct_t    st_atime;
760         timestruct_t    st_mtime;
761         timestruct_t    st_ctime;
762         int             st_blksize;
763         int             st_blocks;
764         char            st_fstype[16];
765         int             st_pad4[8];     /* expansion area */
766 };
767
768 static void
769 printstatsol(struct tcb *tcp, long addr)
770 {
771         struct solstat statbuf;
772
773         if (umove(tcp, addr, &statbuf) < 0) {
774                 tprints("{...}");
775                 return;
776         }
777         if (!abbrev(tcp)) {
778                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
779                         (unsigned long) ((statbuf.st_dev >> 18) & 0x3fff),
780                         (unsigned long) (statbuf.st_dev & 0x3ffff),
781                         (unsigned long) statbuf.st_ino,
782                         sprintmode(statbuf.st_mode));
783                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
784                         (unsigned long) statbuf.st_nlink,
785                         (unsigned long) statbuf.st_uid,
786                         (unsigned long) statbuf.st_gid);
787                 tprintf("st_blksize=%lu, ", (unsigned long) statbuf.st_blksize);
788                 tprintf("st_blocks=%lu, ", (unsigned long) statbuf.st_blocks);
789         }
790         else
791                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
792         switch (statbuf.st_mode & S_IFMT) {
793         case S_IFCHR: case S_IFBLK:
794                 tprintf("st_rdev=makedev(%lu, %lu), ",
795                         (unsigned long) ((statbuf.st_rdev >> 18) & 0x3fff),
796                         (unsigned long) (statbuf.st_rdev & 0x3ffff));
797                 break;
798         default:
799                 tprintf("st_size=%u, ", statbuf.st_size);
800                 break;
801         }
802         if (!abbrev(tcp)) {
803                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime.tv_sec));
804                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime.tv_sec));
805                 tprintf("st_ctime=%s}", sprinttime(statbuf.st_ctime.tv_sec));
806         }
807         else
808                 tprints("...}");
809 }
810
811 #if defined (SPARC64)
812 static void
813 printstat_sparc64(struct tcb *tcp, long addr)
814 {
815         struct stat_sparc64 statbuf;
816
817         if (umove(tcp, addr, &statbuf) < 0) {
818                 tprints("{...}");
819                 return;
820         }
821
822         if (!abbrev(tcp)) {
823                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
824                         (unsigned long) major(statbuf.st_dev),
825                         (unsigned long) minor(statbuf.st_dev),
826                         (unsigned long) statbuf.st_ino,
827                         sprintmode(statbuf.st_mode));
828                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
829                         (unsigned long) statbuf.st_nlink,
830                         (unsigned long) statbuf.st_uid,
831                         (unsigned long) statbuf.st_gid);
832                 tprintf("st_blksize=%lu, ",
833                         (unsigned long) statbuf.st_blksize);
834                 tprintf("st_blocks=%lu, ",
835                         (unsigned long) statbuf.st_blocks);
836         }
837         else
838                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
839         switch (statbuf.st_mode & S_IFMT) {
840         case S_IFCHR: case S_IFBLK:
841                 tprintf("st_rdev=makedev(%lu, %lu), ",
842                         (unsigned long) major(statbuf.st_rdev),
843                         (unsigned long) minor(statbuf.st_rdev));
844                 break;
845         default:
846                 tprintf("st_size=%lu, ", statbuf.st_size);
847                 break;
848         }
849         if (!abbrev(tcp)) {
850                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));
851                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));
852                 tprintf("st_ctime=%s", sprinttime(statbuf.st_ctime));
853                 tprints("}");
854         }
855         else
856                 tprints("...}");
857 }
858 #endif /* SPARC64 */
859 #endif /* LINUXSPARC */
860
861 #if defined LINUX && defined POWERPC64
862 struct stat_powerpc32 {
863         unsigned int    st_dev;
864         unsigned int    st_ino;
865         unsigned int    st_mode;
866         unsigned short  st_nlink;
867         unsigned int    st_uid;
868         unsigned int    st_gid;
869         unsigned int    st_rdev;
870         unsigned int    st_size;
871         unsigned int    st_blksize;
872         unsigned int    st_blocks;
873         unsigned int    st_atime;
874         unsigned int    st_atime_nsec;
875         unsigned int    st_mtime;
876         unsigned int    st_mtime_nsec;
877         unsigned int    st_ctime;
878         unsigned int    st_ctime_nsec;
879         unsigned int    __unused4;
880         unsigned int    __unused5;
881 };
882
883 static void
884 printstat_powerpc32(struct tcb *tcp, long addr)
885 {
886         struct stat_powerpc32 statbuf;
887
888         if (umove(tcp, addr, &statbuf) < 0) {
889                 tprints("{...}");
890                 return;
891         }
892
893         if (!abbrev(tcp)) {
894                 tprintf("{st_dev=makedev(%u, %u), st_ino=%u, st_mode=%s, ",
895                         major(statbuf.st_dev), minor(statbuf.st_dev),
896                         statbuf.st_ino,
897                         sprintmode(statbuf.st_mode));
898                 tprintf("st_nlink=%u, st_uid=%u, st_gid=%u, ",
899                         statbuf.st_nlink, statbuf.st_uid, statbuf.st_gid);
900                 tprintf("st_blksize=%u, ", statbuf.st_blksize);
901                 tprintf("st_blocks=%u, ", statbuf.st_blocks);
902         }
903         else
904                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
905         switch (statbuf.st_mode & S_IFMT) {
906         case S_IFCHR: case S_IFBLK:
907                 tprintf("st_rdev=makedev(%lu, %lu), ",
908                         (unsigned long) major(statbuf.st_rdev),
909                         (unsigned long) minor(statbuf.st_rdev));
910                 break;
911         default:
912                 tprintf("st_size=%u, ", statbuf.st_size);
913                 break;
914         }
915         if (!abbrev(tcp)) {
916                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));
917                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));
918                 tprintf("st_ctime=%s", sprinttime(statbuf.st_ctime));
919                 tprints("}");
920         }
921         else
922                 tprints("...}");
923 }
924 #endif /* LINUX && POWERPC64 */
925
926 static const struct xlat fileflags[] = {
927 #ifdef FREEBSD
928         { UF_NODUMP,    "UF_NODUMP"     },
929         { UF_IMMUTABLE, "UF_IMMUTABLE"  },
930         { UF_APPEND,    "UF_APPEND"     },
931         { UF_OPAQUE,    "UF_OPAQUE"     },
932         { UF_NOUNLINK,  "UF_NOUNLINK"   },
933         { SF_ARCHIVED,  "SF_ARCHIVED"   },
934         { SF_IMMUTABLE, "SF_IMMUTABLE"  },
935         { SF_APPEND,    "SF_APPEND"     },
936         { SF_NOUNLINK,  "SF_NOUNLINK"   },
937 #elif UNIXWARE >= 2
938 #ifdef _S_ISMLD
939         { _S_ISMLD,     "_S_ISMLD"      },
940 #endif
941 #ifdef _S_ISMOUNTED
942         { _S_ISMOUNTED, "_S_ISMOUNTED"  },
943 #endif
944 #endif
945         { 0,            NULL            },
946 };
947
948 #ifdef FREEBSD
949 int
950 sys_chflags(struct tcb *tcp)
951 {
952         if (entering(tcp)) {
953                 printpath(tcp, tcp->u_arg[0]);
954                 tprints(", ");
955                 printflags(fileflags, tcp->u_arg[1], "UF_???");
956         }
957         return 0;
958 }
959
960 int
961 sys_fchflags(struct tcb *tcp)
962 {
963         if (entering(tcp)) {
964                 tprintf("%ld, ", tcp->u_arg[0]);
965                 printflags(fileflags, tcp->u_arg[1], "UF_???");
966         }
967         return 0;
968 }
969 #endif
970
971 #ifndef HAVE_LONG_LONG_OFF_T
972 static void
973 realprintstat(struct tcb *tcp, struct stat *statbuf)
974 {
975         if (!abbrev(tcp)) {
976                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
977                         (unsigned long) major(statbuf->st_dev),
978                         (unsigned long) minor(statbuf->st_dev),
979                         (unsigned long) statbuf->st_ino,
980                         sprintmode(statbuf->st_mode));
981                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
982                         (unsigned long) statbuf->st_nlink,
983                         (unsigned long) statbuf->st_uid,
984                         (unsigned long) statbuf->st_gid);
985 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
986                 tprintf("st_blksize=%lu, ", (unsigned long) statbuf->st_blksize);
987 #endif
988 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
989                 tprintf("st_blocks=%lu, ", (unsigned long) statbuf->st_blocks);
990 #endif
991         }
992         else
993                 tprintf("{st_mode=%s, ", sprintmode(statbuf->st_mode));
994         switch (statbuf->st_mode & S_IFMT) {
995         case S_IFCHR: case S_IFBLK:
996 #ifdef HAVE_STRUCT_STAT_ST_RDEV
997                 tprintf("st_rdev=makedev(%lu, %lu), ",
998                         (unsigned long) major(statbuf->st_rdev),
999                         (unsigned long) minor(statbuf->st_rdev));
1000 #else /* !HAVE_STRUCT_STAT_ST_RDEV */
1001                 tprintf("st_size=makedev(%lu, %lu), ",
1002                         (unsigned long) major(statbuf->st_size),
1003                         (unsigned long) minor(statbuf->st_size));
1004 #endif /* !HAVE_STRUCT_STAT_ST_RDEV */
1005                 break;
1006         default:
1007                 tprintf("st_size=%lu, ", (unsigned long) statbuf->st_size);
1008                 break;
1009         }
1010         if (!abbrev(tcp)) {
1011                 tprintf("st_atime=%s, ", sprinttime(statbuf->st_atime));
1012                 tprintf("st_mtime=%s, ", sprinttime(statbuf->st_mtime));
1013                 tprintf("st_ctime=%s", sprinttime(statbuf->st_ctime));
1014 #if HAVE_STRUCT_STAT_ST_FLAGS
1015                 tprints(", st_flags=");
1016                 printflags(fileflags, statbuf->st_flags, "UF_???");
1017 #endif
1018 #if HAVE_STRUCT_STAT_ST_ACLCNT
1019                 tprintf(", st_aclcnt=%d", statbuf->st_aclcnt);
1020 #endif
1021 #if HAVE_STRUCT_STAT_ST_LEVEL
1022                 tprintf(", st_level=%ld", statbuf->st_level);
1023 #endif
1024 #if HAVE_STRUCT_STAT_ST_FSTYPE
1025                 tprintf(", st_fstype=%.*s",
1026                         (int) sizeof statbuf->st_fstype, statbuf->st_fstype);
1027 #endif
1028 #if HAVE_STRUCT_STAT_ST_GEN
1029                 tprintf(", st_gen=%u", statbuf->st_gen);
1030 #endif
1031                 tprints("}");
1032         }
1033         else
1034                 tprints("...}");
1035 }
1036
1037
1038 static void
1039 printstat(struct tcb *tcp, long addr)
1040 {
1041         struct stat statbuf;
1042
1043         if (!addr) {
1044                 tprints("NULL");
1045                 return;
1046         }
1047         if (syserror(tcp) || !verbose(tcp)) {
1048                 tprintf("%#lx", addr);
1049                 return;
1050         }
1051
1052 #ifdef LINUXSPARC
1053         if (current_personality == 1) {
1054                 printstatsol(tcp, addr);
1055                 return;
1056         }
1057 #ifdef SPARC64
1058         else if (current_personality == 2) {
1059                 printstat_sparc64(tcp, addr);
1060                 return;
1061         }
1062 #endif
1063 #endif /* LINUXSPARC */
1064
1065 #if defined LINUX && defined POWERPC64
1066         if (current_personality == 1) {
1067                 printstat_powerpc32(tcp, addr);
1068                 return;
1069         }
1070 #endif
1071
1072         if (umove(tcp, addr, &statbuf) < 0) {
1073                 tprints("{...}");
1074                 return;
1075         }
1076
1077         realprintstat(tcp, &statbuf);
1078 }
1079 #endif  /* !HAVE_LONG_LONG_OFF_T */
1080
1081 #if !defined HAVE_STAT64 && defined LINUX && defined X86_64
1082 /*
1083  * Linux x86_64 has unified `struct stat' but its i386 biarch needs
1084  * `struct stat64'.  Its <asm-i386/stat.h> definition expects 32-bit `long'.
1085  * <linux/include/asm-x86_64/ia32.h> is not in the public includes set.
1086  * __GNUC__ is needed for the required __attribute__ below.
1087  */
1088 struct stat64 {
1089         unsigned long long      st_dev;
1090         unsigned char   __pad0[4];
1091         unsigned int    __st_ino;
1092         unsigned int    st_mode;
1093         unsigned int    st_nlink;
1094         unsigned int    st_uid;
1095         unsigned int    st_gid;
1096         unsigned long long      st_rdev;
1097         unsigned char   __pad3[4];
1098         long long       st_size;
1099         unsigned int    st_blksize;
1100         unsigned long long      st_blocks;
1101         unsigned int    st_atime;
1102         unsigned int    st_atime_nsec;
1103         unsigned int    st_mtime;
1104         unsigned int    st_mtime_nsec;
1105         unsigned int    st_ctime;
1106         unsigned int    st_ctime_nsec;
1107         unsigned long long      st_ino;
1108 } __attribute__((packed));
1109 # define HAVE_STAT64    1
1110 # define STAT64_SIZE    96
1111 #endif
1112
1113 #ifdef HAVE_STAT64
1114 static void
1115 printstat64(struct tcb *tcp, long addr)
1116 {
1117         struct stat64 statbuf;
1118
1119 #ifdef STAT64_SIZE
1120         (void) sizeof(char[sizeof statbuf == STAT64_SIZE ? 1 : -1]);
1121 #endif
1122
1123         if (!addr) {
1124                 tprints("NULL");
1125                 return;
1126         }
1127         if (syserror(tcp) || !verbose(tcp)) {
1128                 tprintf("%#lx", addr);
1129                 return;
1130         }
1131
1132 #ifdef LINUXSPARC
1133         if (current_personality == 1) {
1134                 printstatsol(tcp, addr);
1135                 return;
1136         }
1137 # ifdef SPARC64
1138         else if (current_personality == 2) {
1139                 printstat_sparc64(tcp, addr);
1140                 return;
1141         }
1142 # endif
1143 #endif /* LINUXSPARC */
1144
1145 #if defined LINUX && defined X86_64
1146         if (current_personality == 0) {
1147                 printstat(tcp, addr);
1148                 return;
1149         }
1150 #endif
1151
1152         if (umove(tcp, addr, &statbuf) < 0) {
1153                 tprints("{...}");
1154                 return;
1155         }
1156
1157         if (!abbrev(tcp)) {
1158 #ifdef HAVE_LONG_LONG
1159                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%llu, st_mode=%s, ",
1160 #else
1161                 tprintf("{st_dev=makedev(%lu, %lu), st_ino=%lu, st_mode=%s, ",
1162 #endif
1163                         (unsigned long) major(statbuf.st_dev),
1164                         (unsigned long) minor(statbuf.st_dev),
1165 #ifdef HAVE_LONG_LONG
1166                         (unsigned long long) statbuf.st_ino,
1167 #else
1168                         (unsigned long) statbuf.st_ino,
1169 #endif
1170                         sprintmode(statbuf.st_mode));
1171                 tprintf("st_nlink=%lu, st_uid=%lu, st_gid=%lu, ",
1172                         (unsigned long) statbuf.st_nlink,
1173                         (unsigned long) statbuf.st_uid,
1174                         (unsigned long) statbuf.st_gid);
1175 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1176                 tprintf("st_blksize=%lu, ",
1177                         (unsigned long) statbuf.st_blksize);
1178 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1179 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1180                 tprintf("st_blocks=%lu, ", (unsigned long) statbuf.st_blocks);
1181 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
1182         }
1183         else
1184                 tprintf("{st_mode=%s, ", sprintmode(statbuf.st_mode));
1185         switch (statbuf.st_mode & S_IFMT) {
1186         case S_IFCHR: case S_IFBLK:
1187 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1188                 tprintf("st_rdev=makedev(%lu, %lu), ",
1189                         (unsigned long) major(statbuf.st_rdev),
1190                         (unsigned long) minor(statbuf.st_rdev));
1191 #else /* !HAVE_STRUCT_STAT_ST_RDEV */
1192                 tprintf("st_size=makedev(%lu, %lu), ",
1193                         (unsigned long) major(statbuf.st_size),
1194                         (unsigned long) minor(statbuf.st_size));
1195 #endif /* !HAVE_STRUCT_STAT_ST_RDEV */
1196                 break;
1197         default:
1198 #ifdef HAVE_LONG_LONG
1199                 tprintf("st_size=%llu, ", (unsigned long long) statbuf.st_size);
1200 #else
1201                 tprintf("st_size=%lu, ", (unsigned long) statbuf.st_size);
1202 #endif
1203                 break;
1204         }
1205         if (!abbrev(tcp)) {
1206                 tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime));
1207                 tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime));
1208                 tprintf("st_ctime=%s", sprinttime(statbuf.st_ctime));
1209 #if HAVE_STRUCT_STAT_ST_FLAGS
1210                 tprints(", st_flags=");
1211                 printflags(fileflags, statbuf.st_flags, "UF_???");
1212 #endif
1213 #if HAVE_STRUCT_STAT_ST_ACLCNT
1214                 tprintf(", st_aclcnt=%d", statbuf.st_aclcnt);
1215 #endif
1216 #if HAVE_STRUCT_STAT_ST_LEVEL
1217                 tprintf(", st_level=%ld", statbuf.st_level);
1218 #endif
1219 #if HAVE_STRUCT_STAT_ST_FSTYPE
1220                 tprintf(", st_fstype=%.*s",
1221                         (int) sizeof statbuf.st_fstype, statbuf.st_fstype);
1222 #endif
1223 #if HAVE_STRUCT_STAT_ST_GEN
1224                 tprintf(", st_gen=%u", statbuf.st_gen);
1225 #endif
1226                 tprints("}");
1227         }
1228         else
1229                 tprints("...}");
1230 }
1231 #endif /* HAVE_STAT64 */
1232
1233 #if defined(LINUX) && defined(HAVE_STRUCT___OLD_KERNEL_STAT)
1234 static void
1235 convertoldstat(const struct __old_kernel_stat *oldbuf, struct stat *newbuf)
1236 {
1237         newbuf->st_dev = oldbuf->st_dev;
1238         newbuf->st_ino = oldbuf->st_ino;
1239         newbuf->st_mode = oldbuf->st_mode;
1240         newbuf->st_nlink = oldbuf->st_nlink;
1241         newbuf->st_uid = oldbuf->st_uid;
1242         newbuf->st_gid = oldbuf->st_gid;
1243         newbuf->st_rdev = oldbuf->st_rdev;
1244         newbuf->st_size = oldbuf->st_size;
1245         newbuf->st_atime = oldbuf->st_atime;
1246         newbuf->st_mtime = oldbuf->st_mtime;
1247         newbuf->st_ctime = oldbuf->st_ctime;
1248         newbuf->st_blksize = 0; /* not supported in old_stat */
1249         newbuf->st_blocks = 0; /* not supported in old_stat */
1250 }
1251
1252
1253 static void
1254 printoldstat(struct tcb *tcp, long addr)
1255 {
1256         struct __old_kernel_stat statbuf;
1257         struct stat newstatbuf;
1258
1259         if (!addr) {
1260                 tprints("NULL");
1261                 return;
1262         }
1263         if (syserror(tcp) || !verbose(tcp)) {
1264                 tprintf("%#lx", addr);
1265                 return;
1266         }
1267
1268 #ifdef LINUXSPARC
1269         if (current_personality == 1) {
1270                 printstatsol(tcp, addr);
1271                 return;
1272         }
1273 #endif /* LINUXSPARC */
1274
1275         if (umove(tcp, addr, &statbuf) < 0) {
1276                 tprints("{...}");
1277                 return;
1278         }
1279
1280         convertoldstat(&statbuf, &newstatbuf);
1281         realprintstat(tcp, &newstatbuf);
1282 }
1283 #endif /* LINUX && !IA64 && !HPPA && !X86_64 && !S390 && !S390X */
1284
1285 #ifndef HAVE_LONG_LONG_OFF_T
1286 int
1287 sys_stat(struct tcb *tcp)
1288 {
1289         if (entering(tcp)) {
1290                 printpath(tcp, tcp->u_arg[0]);
1291                 tprints(", ");
1292         } else {
1293                 printstat(tcp, tcp->u_arg[1]);
1294         }
1295         return 0;
1296 }
1297 #endif
1298
1299 int
1300 sys_stat64(struct tcb *tcp)
1301 {
1302 #ifdef HAVE_STAT64
1303         if (entering(tcp)) {
1304                 printpath(tcp, tcp->u_arg[0]);
1305                 tprints(", ");
1306         } else {
1307                 printstat64(tcp, tcp->u_arg[1]);
1308         }
1309         return 0;
1310 #else
1311         return printargs(tcp);
1312 #endif
1313 }
1314
1315 #ifdef LINUX
1316 static const struct xlat fstatatflags[] = {
1317 #ifndef AT_SYMLINK_NOFOLLOW
1318 # define AT_SYMLINK_NOFOLLOW     0x100
1319 #endif
1320         { AT_SYMLINK_NOFOLLOW,  "AT_SYMLINK_NOFOLLOW"   },
1321         { 0,                    NULL                    },
1322 };
1323 #define utimensatflags fstatatflags
1324
1325 int
1326 sys_newfstatat(struct tcb *tcp)
1327 {
1328         if (entering(tcp)) {
1329                 print_dirfd(tcp, tcp->u_arg[0]);
1330                 printpath(tcp, tcp->u_arg[1]);
1331                 tprints(", ");
1332         } else {
1333 #ifdef POWERPC64
1334                 if (current_personality == 0)
1335                         printstat(tcp, tcp->u_arg[2]);
1336                 else
1337                         printstat64(tcp, tcp->u_arg[2]);
1338 #elif defined HAVE_STAT64
1339                 printstat64(tcp, tcp->u_arg[2]);
1340 #else
1341                 printstat(tcp, tcp->u_arg[2]);
1342 #endif
1343                 tprints(", ");
1344                 printflags(fstatatflags, tcp->u_arg[3], "AT_???");
1345         }
1346         return 0;
1347 }
1348 #endif
1349
1350 #if defined(LINUX) && defined(HAVE_STRUCT___OLD_KERNEL_STAT)
1351 int
1352 sys_oldstat(struct tcb *tcp)
1353 {
1354         if (entering(tcp)) {
1355                 printpath(tcp, tcp->u_arg[0]);
1356                 tprints(", ");
1357         } else {
1358                 printoldstat(tcp, tcp->u_arg[1]);
1359         }
1360         return 0;
1361 }
1362 #endif /* LINUX && HAVE_STRUCT___OLD_KERNEL_STAT */
1363
1364 #ifndef HAVE_LONG_LONG_OFF_T
1365 int
1366 sys_fstat(struct tcb *tcp)
1367 {
1368         if (entering(tcp)) {
1369                 printfd(tcp, tcp->u_arg[0]);
1370                 tprints(", ");
1371         } else {
1372                 printstat(tcp, tcp->u_arg[1]);
1373         }
1374         return 0;
1375 }
1376 #endif
1377
1378 int
1379 sys_fstat64(struct tcb *tcp)
1380 {
1381 #ifdef HAVE_STAT64
1382         if (entering(tcp)) {
1383                 printfd(tcp, tcp->u_arg[0]);
1384                 tprints(", ");
1385         } else {
1386                 printstat64(tcp, tcp->u_arg[1]);
1387         }
1388         return 0;
1389 #else
1390         return printargs(tcp);
1391 #endif
1392 }
1393
1394 #if defined(LINUX) && defined(HAVE_STRUCT___OLD_KERNEL_STAT)
1395 int
1396 sys_oldfstat(struct tcb *tcp)
1397 {
1398         if (entering(tcp)) {
1399                 printfd(tcp, tcp->u_arg[0]);
1400                 tprints(", ");
1401         } else {
1402                 printoldstat(tcp, tcp->u_arg[1]);
1403         }
1404         return 0;
1405 }
1406 #endif /* LINUX && HAVE_STRUCT___OLD_KERNEL_STAT */
1407
1408 #ifndef HAVE_LONG_LONG_OFF_T
1409 int
1410 sys_lstat(struct tcb *tcp)
1411 {
1412         if (entering(tcp)) {
1413                 printpath(tcp, tcp->u_arg[0]);
1414                 tprints(", ");
1415         } else {
1416                 printstat(tcp, tcp->u_arg[1]);
1417         }
1418         return 0;
1419 }
1420 #endif
1421
1422 int
1423 sys_lstat64(struct tcb *tcp)
1424 {
1425 #ifdef HAVE_STAT64
1426         if (entering(tcp)) {
1427                 printpath(tcp, tcp->u_arg[0]);
1428                 tprints(", ");
1429         } else {
1430                 printstat64(tcp, tcp->u_arg[1]);
1431         }
1432         return 0;
1433 #else
1434         return printargs(tcp);
1435 #endif
1436 }
1437
1438 #if defined(LINUX) && defined(HAVE_STRUCT___OLD_KERNEL_STAT)
1439 int
1440 sys_oldlstat(struct tcb *tcp)
1441 {
1442         if (entering(tcp)) {
1443                 printpath(tcp, tcp->u_arg[0]);
1444                 tprints(", ");
1445         } else {
1446                 printoldstat(tcp, tcp->u_arg[1]);
1447         }
1448         return 0;
1449 }
1450 #endif /* LINUX && HAVE_STRUCT___OLD_KERNEL_STAT */
1451
1452
1453 #if defined(SVR4) || defined(LINUXSPARC)
1454
1455 int
1456 sys_xstat(struct tcb *tcp)
1457 {
1458         if (entering(tcp)) {
1459                 tprintf("%ld, ", tcp->u_arg[0]);
1460                 printpath(tcp, tcp->u_arg[1]);
1461                 tprints(", ");
1462         } else {
1463 #ifdef _STAT64_VER
1464                 if (tcp->u_arg[0] == _STAT64_VER)
1465                         printstat64 (tcp, tcp->u_arg[2]);
1466                 else
1467 #endif
1468                 printstat(tcp, tcp->u_arg[2]);
1469         }
1470         return 0;
1471 }
1472
1473 int
1474 sys_fxstat(struct tcb *tcp)
1475 {
1476         if (entering(tcp))
1477                 tprintf("%ld, %ld, ", tcp->u_arg[0], tcp->u_arg[1]);
1478         else {
1479 #ifdef _STAT64_VER
1480                 if (tcp->u_arg[0] == _STAT64_VER)
1481                         printstat64 (tcp, tcp->u_arg[2]);
1482                 else
1483 #endif
1484                 printstat(tcp, tcp->u_arg[2]);
1485         }
1486         return 0;
1487 }
1488
1489 int
1490 sys_lxstat(struct tcb *tcp)
1491 {
1492         if (entering(tcp)) {
1493                 tprintf("%ld, ", tcp->u_arg[0]);
1494                 printpath(tcp, tcp->u_arg[1]);
1495                 tprints(", ");
1496         } else {
1497 #ifdef _STAT64_VER
1498                 if (tcp->u_arg[0] == _STAT64_VER)
1499                         printstat64 (tcp, tcp->u_arg[2]);
1500                 else
1501 #endif
1502                 printstat(tcp, tcp->u_arg[2]);
1503         }
1504         return 0;
1505 }
1506
1507 int
1508 sys_xmknod(struct tcb *tcp)
1509 {
1510         int mode = tcp->u_arg[2];
1511
1512         if (entering(tcp)) {
1513                 tprintf("%ld, ", tcp->u_arg[0]);
1514                 printpath(tcp, tcp->u_arg[1]);
1515                 tprintf(", %s", sprintmode(mode));
1516                 switch (mode & S_IFMT) {
1517                 case S_IFCHR: case S_IFBLK:
1518 #ifdef LINUXSPARC
1519                         tprintf(", makedev(%lu, %lu)",
1520                                 (unsigned long) ((tcp->u_arg[3] >> 18) & 0x3fff),
1521                                 (unsigned long) (tcp->u_arg[3] & 0x3ffff));
1522 #else
1523                         tprintf(", makedev(%lu, %lu)",
1524                                 (unsigned long) major(tcp->u_arg[3]),
1525                                 (unsigned long) minor(tcp->u_arg[3]));
1526 #endif
1527                         break;
1528                 default:
1529                         break;
1530                 }
1531         }
1532         return 0;
1533 }
1534
1535 #ifdef HAVE_SYS_ACL_H
1536
1537 #include <sys/acl.h>
1538
1539 static const struct xlat aclcmds[] = {
1540 #ifdef SETACL
1541         { SETACL,       "SETACL"        },
1542 #endif
1543 #ifdef GETACL
1544         { GETACL,       "GETACL"        },
1545 #endif
1546 #ifdef GETACLCNT
1547         { GETACLCNT,    "GETACLCNT"     },
1548 #endif
1549 #ifdef ACL_GET
1550         { ACL_GET,      "ACL_GET"       },
1551 #endif
1552 #ifdef ACL_SET
1553         { ACL_SET,      "ACL_SET"       },
1554 #endif
1555 #ifdef ACL_CNT
1556         { ACL_CNT,      "ACL_CNT"       },
1557 #endif
1558         { 0,            NULL            },
1559 };
1560
1561 int
1562 sys_acl(struct tcb *tcp)
1563 {
1564         if (entering(tcp)) {
1565                 printpath(tcp, tcp->u_arg[0]);
1566                 tprints(", ");
1567                 printxval(aclcmds, tcp->u_arg[1], "???ACL???");
1568                 tprintf(", %ld", tcp->u_arg[2]);
1569                 /*
1570                  * FIXME - dump out the list of aclent_t's pointed to
1571                  * by "tcp->u_arg[3]" if it's not NULL.
1572                  */
1573                 if (tcp->u_arg[3])
1574                         tprintf(", %#lx", tcp->u_arg[3]);
1575                 else
1576                         tprints(", NULL");
1577         }
1578         return 0;
1579 }
1580
1581
1582 int
1583 sys_facl(struct tcb *tcp)
1584 {
1585         if (entering(tcp)) {
1586                 tprintf("%ld, ", tcp->u_arg[0]);
1587                 printxval(aclcmds, tcp->u_arg[1], "???ACL???");
1588                 tprintf(", %ld", tcp->u_arg[2]);
1589                 /*
1590                  * FIXME - dump out the list of aclent_t's pointed to
1591                  * by "tcp->u_arg[3]" if it's not NULL.
1592                  */
1593                 if (tcp->u_arg[3])
1594                         tprintf(", %#lx", tcp->u_arg[3]);
1595                 else
1596                         tprints(", NULL");
1597         }
1598         return 0;
1599 }
1600
1601
1602 static const struct xlat aclipc[] = {
1603 #ifdef IPC_SHM
1604         { IPC_SHM,      "IPC_SHM"       },
1605 #endif
1606 #ifdef IPC_SEM
1607         { IPC_SEM,      "IPC_SEM"       },
1608 #endif
1609 #ifdef IPC_MSG
1610         { IPC_MSG,      "IPC_MSG"       },
1611 #endif
1612         { 0,            NULL            },
1613 };
1614
1615
1616 int
1617 sys_aclipc(struct tcb *tcp)
1618 {
1619         if (entering(tcp)) {
1620                 printxval(aclipc, tcp->u_arg[0], "???IPC???");
1621                 tprintf(", %#lx, ", tcp->u_arg[1]);
1622                 printxval(aclcmds, tcp->u_arg[2], "???ACL???");
1623                 tprintf(", %ld", tcp->u_arg[3]);
1624                 /*
1625                  * FIXME - dump out the list of aclent_t's pointed to
1626                  * by "tcp->u_arg[4]" if it's not NULL.
1627                  */
1628                 if (tcp->u_arg[4])
1629                         tprintf(", %#lx", tcp->u_arg[4]);
1630                 else
1631                         tprints(", NULL");
1632         }
1633         return 0;
1634 }
1635
1636 #endif /* HAVE_SYS_ACL_H */
1637
1638 #endif /* SVR4 || LINUXSPARC */
1639
1640 #ifdef LINUX
1641
1642 static const struct xlat fsmagic[] = {
1643         { 0x73757245,   "CODA_SUPER_MAGIC"      },
1644         { 0x012ff7b7,   "COH_SUPER_MAGIC"       },
1645         { 0x1373,       "DEVFS_SUPER_MAGIC"     },
1646         { 0x1cd1,       "DEVPTS_SUPER_MAGIC"    },
1647         { 0x414A53,     "EFS_SUPER_MAGIC"       },
1648         { 0xef51,       "EXT2_OLD_SUPER_MAGIC"  },
1649         { 0xef53,       "EXT2_SUPER_MAGIC"      },
1650         { 0x137d,       "EXT_SUPER_MAGIC"       },
1651         { 0xf995e849,   "HPFS_SUPER_MAGIC"      },
1652         { 0x9660,       "ISOFS_SUPER_MAGIC"     },
1653         { 0x137f,       "MINIX_SUPER_MAGIC"     },
1654         { 0x138f,       "MINIX_SUPER_MAGIC2"    },
1655         { 0x2468,       "MINIX2_SUPER_MAGIC"    },
1656         { 0x2478,       "MINIX2_SUPER_MAGIC2"   },
1657         { 0x4d44,       "MSDOS_SUPER_MAGIC"     },
1658         { 0x564c,       "NCP_SUPER_MAGIC"       },
1659         { 0x6969,       "NFS_SUPER_MAGIC"       },
1660         { 0x9fa0,       "PROC_SUPER_MAGIC"      },
1661         { 0x002f,       "QNX4_SUPER_MAGIC"      },
1662         { 0x52654973,   "REISERFS_SUPER_MAGIC"  },
1663         { 0x02011994,   "SHMFS_SUPER_MAGIC"     },
1664         { 0x517b,       "SMB_SUPER_MAGIC"       },
1665         { 0x012ff7b6,   "SYSV2_SUPER_MAGIC"     },
1666         { 0x012ff7b5,   "SYSV4_SUPER_MAGIC"     },
1667         { 0x00011954,   "UFS_MAGIC"             },
1668         { 0x54190100,   "UFS_CIGAM"             },
1669         { 0x012ff7b4,   "XENIX_SUPER_MAGIC"     },
1670         { 0x012fd16d,   "XIAFS_SUPER_MAGIC"     },
1671         { 0x62656572,   "SYSFS_MAGIC"           },
1672         { 0,            NULL                    },
1673 };
1674
1675 #endif /* LINUX */
1676
1677 #ifndef SVR4
1678
1679 static const char *
1680 sprintfstype(int magic)
1681 {
1682         static char buf[32];
1683 #ifdef LINUX
1684         const char *s;
1685
1686         s = xlookup(fsmagic, magic);
1687         if (s) {
1688                 sprintf(buf, "\"%s\"", s);
1689                 return buf;
1690         }
1691 #endif /* LINUX */
1692         sprintf(buf, "%#x", magic);
1693         return buf;
1694 }
1695
1696 static void
1697 printstatfs(struct tcb *tcp, long addr)
1698 {
1699         struct statfs statbuf;
1700
1701         if (syserror(tcp) || !verbose(tcp)) {
1702                 tprintf("%#lx", addr);
1703                 return;
1704         }
1705         if (umove(tcp, addr, &statbuf) < 0) {
1706                 tprints("{...}");
1707                 return;
1708         }
1709 #ifdef ALPHA
1710
1711         tprintf("{f_type=%s, f_fbsize=%u, f_blocks=%u, f_bfree=%u, ",
1712                 sprintfstype(statbuf.f_type),
1713                 statbuf.f_bsize, statbuf.f_blocks, statbuf.f_bfree);
1714         tprintf("f_bavail=%u, f_files=%u, f_ffree=%u, f_fsid={%d, %d}, f_namelen=%u",
1715                 statbuf.f_bavail, statbuf.f_files, statbuf.f_ffree,
1716                 statbuf.f_fsid.__val[0], statbuf.f_fsid.__val[1],
1717                 statbuf.f_namelen);
1718 #else /* !ALPHA */
1719         tprintf("{f_type=%s, f_bsize=%lu, f_blocks=%lu, f_bfree=%lu, ",
1720                 sprintfstype(statbuf.f_type),
1721                 (unsigned long)statbuf.f_bsize,
1722                 (unsigned long)statbuf.f_blocks,
1723                 (unsigned long)statbuf.f_bfree);
1724         tprintf("f_bavail=%lu, f_files=%lu, f_ffree=%lu, f_fsid={%d, %d}",
1725                 (unsigned long)statbuf.f_bavail,
1726                 (unsigned long)statbuf.f_files,
1727                 (unsigned long)statbuf.f_ffree,
1728                 statbuf.f_fsid.__val[0], statbuf.f_fsid.__val[1]);
1729 #ifdef LINUX
1730         tprintf(", f_namelen=%lu", (unsigned long)statbuf.f_namelen);
1731 #endif /* LINUX */
1732 #endif /* !ALPHA */
1733 #ifdef _STATFS_F_FRSIZE
1734         tprintf(", f_frsize=%lu", (unsigned long)statbuf.f_frsize);
1735 #endif
1736         tprints("}");
1737 }
1738
1739 int
1740 sys_statfs(struct tcb *tcp)
1741 {
1742         if (entering(tcp)) {
1743                 printpath(tcp, tcp->u_arg[0]);
1744                 tprints(", ");
1745         } else {
1746                 printstatfs(tcp, tcp->u_arg[1]);
1747         }
1748         return 0;
1749 }
1750
1751 int
1752 sys_fstatfs(struct tcb *tcp)
1753 {
1754         if (entering(tcp)) {
1755                 printfd(tcp, tcp->u_arg[0]);
1756                 tprints(", ");
1757         } else {
1758                 printstatfs(tcp, tcp->u_arg[1]);
1759         }
1760         return 0;
1761 }
1762
1763 #if defined LINUX && defined HAVE_STATFS64
1764 static void
1765 printstatfs64(struct tcb *tcp, long addr)
1766 {
1767         struct statfs64 statbuf;
1768
1769         if (syserror(tcp) || !verbose(tcp)) {
1770                 tprintf("%#lx", addr);
1771                 return;
1772         }
1773         if (umove(tcp, addr, &statbuf) < 0) {
1774                 tprints("{...}");
1775                 return;
1776         }
1777         tprintf("{f_type=%s, f_bsize=%llu, f_blocks=%llu, f_bfree=%llu, ",
1778                 sprintfstype(statbuf.f_type),
1779                 (unsigned long long)statbuf.f_bsize,
1780                 (unsigned long long)statbuf.f_blocks,
1781                 (unsigned long long)statbuf.f_bfree);
1782         tprintf("f_bavail=%llu, f_files=%llu, f_ffree=%llu, f_fsid={%d, %d}",
1783                 (unsigned long long)statbuf.f_bavail,
1784                 (unsigned long long)statbuf.f_files,
1785                 (unsigned long long)statbuf.f_ffree,
1786                 statbuf.f_fsid.__val[0], statbuf.f_fsid.__val[1]);
1787         tprintf(", f_namelen=%lu", (unsigned long)statbuf.f_namelen);
1788 #ifdef _STATFS_F_FRSIZE
1789         tprintf(", f_frsize=%llu", (unsigned long long)statbuf.f_frsize);
1790 #endif
1791 #ifdef _STATFS_F_FLAGS
1792         tprintf(", f_flags=%llu", (unsigned long long)statbuf.f_flags);
1793 #endif
1794         tprints("}");
1795 }
1796
1797 struct compat_statfs64 {
1798         uint32_t f_type;
1799         uint32_t f_bsize;
1800         uint64_t f_blocks;
1801         uint64_t f_bfree;
1802         uint64_t f_bavail;
1803         uint64_t f_files;
1804         uint64_t f_ffree;
1805         fsid_t f_fsid;
1806         uint32_t f_namelen;
1807         uint32_t f_frsize;
1808         uint32_t f_flags;
1809         uint32_t f_spare[4];
1810 }
1811 #if defined(X86_64) || defined(IA64)
1812   __attribute__ ((packed, aligned(4)))
1813 #endif
1814 ;
1815
1816 static void
1817 printcompat_statfs64(struct tcb *tcp, long addr)
1818 {
1819         struct compat_statfs64 statbuf;
1820
1821         if (syserror(tcp) || !verbose(tcp)) {
1822                 tprintf("%#lx", addr);
1823                 return;
1824         }
1825         if (umove(tcp, addr, &statbuf) < 0) {
1826                 tprints("{...}");
1827                 return;
1828         }
1829         tprintf("{f_type=%s, f_bsize=%lu, f_blocks=%llu, f_bfree=%llu, ",
1830                 sprintfstype(statbuf.f_type),
1831                 (unsigned long)statbuf.f_bsize,
1832                 (unsigned long long)statbuf.f_blocks,
1833                 (unsigned long long)statbuf.f_bfree);
1834         tprintf("f_bavail=%llu, f_files=%llu, f_ffree=%llu, f_fsid={%d, %d}",
1835                 (unsigned long long)statbuf.f_bavail,
1836                 (unsigned long long)statbuf.f_files,
1837                 (unsigned long long)statbuf.f_ffree,
1838                 statbuf.f_fsid.__val[0], statbuf.f_fsid.__val[1]);
1839         tprintf(", f_namelen=%lu", (unsigned long)statbuf.f_namelen);
1840         tprintf(", f_frsize=%lu", (unsigned long)statbuf.f_frsize);
1841         tprintf(", f_flags=%lu", (unsigned long)statbuf.f_frsize);
1842         tprints("}");
1843 }
1844
1845 int
1846 sys_statfs64(struct tcb *tcp)
1847 {
1848         if (entering(tcp)) {
1849                 printpath(tcp, tcp->u_arg[0]);
1850                 tprintf(", %lu, ", tcp->u_arg[1]);
1851         } else {
1852                 if (tcp->u_arg[1] == sizeof(struct statfs64))
1853                         printstatfs64(tcp, tcp->u_arg[2]);
1854                 else if (tcp->u_arg[1] == sizeof(struct compat_statfs64))
1855                         printcompat_statfs64(tcp, tcp->u_arg[2]);
1856                 else
1857                         tprints("{???}");
1858         }
1859         return 0;
1860 }
1861
1862 int
1863 sys_fstatfs64(struct tcb *tcp)
1864 {
1865         if (entering(tcp)) {
1866                 printfd(tcp, tcp->u_arg[0]);
1867                 tprintf(", %lu, ", tcp->u_arg[1]);
1868         } else {
1869                 if (tcp->u_arg[1] == sizeof(struct statfs64))
1870                         printstatfs64(tcp, tcp->u_arg[2]);
1871                 else if (tcp->u_arg[1] == sizeof(struct compat_statfs64))
1872                         printcompat_statfs64(tcp, tcp->u_arg[2]);
1873                 else
1874                         tprints("{???}");
1875         }
1876         return 0;
1877 }
1878 #endif
1879
1880 #if defined(LINUX) && defined(__alpha)
1881
1882 int
1883 osf_statfs(struct tcb *tcp)
1884 {
1885         if (entering(tcp)) {
1886                 printpath(tcp, tcp->u_arg[0]);
1887                 tprints(", ");
1888         } else {
1889                 printstatfs(tcp, tcp->u_arg[1]);
1890                 tprintf(", %lu", tcp->u_arg[2]);
1891         }
1892         return 0;
1893 }
1894
1895 int
1896 osf_fstatfs(struct tcb *tcp)
1897 {
1898         if (entering(tcp)) {
1899                 tprintf("%lu, ", tcp->u_arg[0]);
1900         } else {
1901                 printstatfs(tcp, tcp->u_arg[1]);
1902                 tprintf(", %lu", tcp->u_arg[2]);
1903         }
1904         return 0;
1905 }
1906 #endif /* LINUX && __alpha */
1907
1908 #endif /* !SVR4 */
1909
1910 #ifdef SUNOS4
1911 int
1912 sys_ustat(struct tcb *tcp)
1913 {
1914         struct ustat statbuf;
1915
1916         if (entering(tcp)) {
1917                 tprintf("makedev(%lu, %lu), ",
1918                                 (long) major(tcp->u_arg[0]),
1919                                 (long) minor(tcp->u_arg[0]));
1920         }
1921         else {
1922                 if (syserror(tcp) || !verbose(tcp))
1923                         tprintf("%#lx", tcp->u_arg[1]);
1924                 else if (umove(tcp, tcp->u_arg[1], &statbuf) < 0)
1925                         tprints("{...}");
1926                 else {
1927                         tprintf("{f_tfree=%lu, f_tinode=%lu, ",
1928                                 statbuf.f_tfree, statbuf.f_tinode);
1929                         tprintf("f_fname=\"%.*s\", ",
1930                                 (int) sizeof(statbuf.f_fname),
1931                                 statbuf.f_fname);
1932                         tprintf("f_fpack=\"%.*s\"}",
1933                                 (int) sizeof(statbuf.f_fpack),
1934                                 statbuf.f_fpack);
1935                 }
1936         }
1937         return 0;
1938 }
1939 #endif /* SUNOS4 */
1940
1941 /* directory */
1942 int
1943 sys_chdir(struct tcb *tcp)
1944 {
1945         if (entering(tcp)) {
1946                 printpath(tcp, tcp->u_arg[0]);
1947         }
1948         return 0;
1949 }
1950
1951 static int
1952 decode_mkdir(struct tcb *tcp, int offset)
1953 {
1954         if (entering(tcp)) {
1955                 printpath(tcp, tcp->u_arg[offset]);
1956                 tprintf(", %#lo", tcp->u_arg[offset + 1]);
1957         }
1958         return 0;
1959 }
1960
1961 int
1962 sys_mkdir(struct tcb *tcp)
1963 {
1964         return decode_mkdir(tcp, 0);
1965 }
1966
1967 #ifdef LINUX
1968 int
1969 sys_mkdirat(struct tcb *tcp)
1970 {
1971         if (entering(tcp))
1972                 print_dirfd(tcp, tcp->u_arg[0]);
1973         return decode_mkdir(tcp, 1);
1974 }
1975 #endif
1976
1977 int
1978 sys_link(struct tcb *tcp)
1979 {
1980         if (entering(tcp)) {
1981                 printpath(tcp, tcp->u_arg[0]);
1982                 tprints(", ");
1983                 printpath(tcp, tcp->u_arg[1]);
1984         }
1985         return 0;
1986 }
1987
1988 #ifdef LINUX
1989 int
1990 sys_linkat(struct tcb *tcp)
1991 {
1992         if (entering(tcp)) {
1993                 print_dirfd(tcp, tcp->u_arg[0]);
1994                 printpath(tcp, tcp->u_arg[1]);
1995                 tprints(", ");
1996                 print_dirfd(tcp, tcp->u_arg[2]);
1997                 printpath(tcp, tcp->u_arg[3]);
1998                 tprints(", ");
1999                 printfd(tcp, tcp->u_arg[4]);
2000         }
2001         return 0;
2002 }
2003 #endif
2004
2005 #ifdef LINUX
2006 static const struct xlat unlinkatflags[] = {
2007 #ifndef AT_REMOVEDIR
2008 # define AT_REMOVEDIR            0x200
2009 #endif
2010         { AT_REMOVEDIR, "AT_REMOVEDIR"  },
2011         { 0,            NULL            },
2012 };
2013
2014 int
2015 sys_unlinkat(struct tcb *tcp)
2016 {
2017         if (entering(tcp)) {
2018                 print_dirfd(tcp, tcp->u_arg[0]);
2019                 printpath(tcp, tcp->u_arg[1]);
2020                 tprints(", ");
2021                 printflags(unlinkatflags, tcp->u_arg[2], "AT_???");
2022         }
2023         return 0;
2024 }
2025 #endif
2026
2027 #ifdef LINUX
2028 int
2029 sys_symlinkat(struct tcb *tcp)
2030 {
2031         if (entering(tcp)) {
2032                 printpath(tcp, tcp->u_arg[0]);
2033                 tprints(", ");
2034                 print_dirfd(tcp, tcp->u_arg[1]);
2035                 printpath(tcp, tcp->u_arg[2]);
2036         }
2037         return 0;
2038 }
2039 #endif
2040
2041 static int
2042 decode_readlink(struct tcb *tcp, int offset)
2043 {
2044         if (entering(tcp)) {
2045                 printpath(tcp, tcp->u_arg[offset]);
2046                 tprints(", ");
2047         } else {
2048                 if (syserror(tcp))
2049                         tprintf("%#lx", tcp->u_arg[offset + 1]);
2050                 else
2051                         /* Used to use printpathn(), but readlink
2052                          * neither includes NUL in the returned count,
2053                          * nor actually writes it into memory.
2054                          * printpathn() would decide on printing
2055                          * "..." continuation based on garbage
2056                          * past return buffer's end.
2057                          */
2058                         printstr(tcp, tcp->u_arg[offset + 1], tcp->u_rval);
2059                 tprintf(", %lu", tcp->u_arg[offset + 2]);
2060         }
2061         return 0;
2062 }
2063
2064 int
2065 sys_readlink(struct tcb *tcp)
2066 {
2067         return decode_readlink(tcp, 0);
2068 }
2069
2070 #ifdef LINUX
2071 int
2072 sys_readlinkat(struct tcb *tcp)
2073 {
2074         if (entering(tcp))
2075                 print_dirfd(tcp, tcp->u_arg[0]);
2076         return decode_readlink(tcp, 1);
2077 }
2078 #endif
2079
2080 #ifdef LINUX
2081 int
2082 sys_renameat(struct tcb *tcp)
2083 {
2084         if (entering(tcp)) {
2085                 print_dirfd(tcp, tcp->u_arg[0]);
2086                 printpath(tcp, tcp->u_arg[1]);
2087                 tprints(", ");
2088                 print_dirfd(tcp, tcp->u_arg[2]);
2089                 printpath(tcp, tcp->u_arg[3]);
2090         }
2091         return 0;
2092 }
2093 #endif
2094
2095 int
2096 sys_chown(struct tcb *tcp)
2097 {
2098         if (entering(tcp)) {
2099                 printpath(tcp, tcp->u_arg[0]);
2100                 printuid(", ", tcp->u_arg[1]);
2101                 printuid(", ", tcp->u_arg[2]);
2102         }
2103         return 0;
2104 }
2105
2106 #ifdef LINUX
2107 int
2108 sys_fchownat(struct tcb *tcp)
2109 {
2110         if (entering(tcp)) {
2111                 print_dirfd(tcp, tcp->u_arg[0]);
2112                 printpath(tcp, tcp->u_arg[1]);
2113                 printuid(", ", tcp->u_arg[2]);
2114                 printuid(", ", tcp->u_arg[3]);
2115                 tprints(", ");
2116                 printflags(fstatatflags, tcp->u_arg[4], "AT_???");
2117         }
2118         return 0;
2119 }
2120 #endif
2121
2122 int
2123 sys_fchown(struct tcb *tcp)
2124 {
2125         if (entering(tcp)) {
2126                 printfd(tcp, tcp->u_arg[0]);
2127                 printuid(", ", tcp->u_arg[1]);
2128                 printuid(", ", tcp->u_arg[2]);
2129         }
2130         return 0;
2131 }
2132
2133 static int
2134 decode_chmod(struct tcb *tcp, int offset)
2135 {
2136         if (entering(tcp)) {
2137                 printpath(tcp, tcp->u_arg[offset]);
2138                 tprintf(", %#lo", tcp->u_arg[offset + 1]);
2139         }
2140         return 0;
2141 }
2142
2143 int
2144 sys_chmod(struct tcb *tcp)
2145 {
2146         return decode_chmod(tcp, 0);
2147 }
2148
2149 #ifdef LINUX
2150 int
2151 sys_fchmodat(struct tcb *tcp)
2152 {
2153         if (entering(tcp))
2154                 print_dirfd(tcp, tcp->u_arg[0]);
2155         return decode_chmod(tcp, 1);
2156 }
2157 #endif
2158
2159 int
2160 sys_fchmod(struct tcb *tcp)
2161 {
2162         if (entering(tcp)) {
2163                 printfd(tcp, tcp->u_arg[0]);
2164                 tprintf(", %#lo", tcp->u_arg[1]);
2165         }
2166         return 0;
2167 }
2168
2169 #ifdef ALPHA
2170 int
2171 sys_osf_utimes(struct tcb *tcp)
2172 {
2173         if (entering(tcp)) {
2174                 printpath(tcp, tcp->u_arg[0]);
2175                 tprints(", ");
2176                 printtv_bitness(tcp, tcp->u_arg[1], BITNESS_32,  0);
2177         }
2178         return 0;
2179 }
2180 #endif
2181
2182 static int
2183 decode_utimes(struct tcb *tcp, int offset, int special)
2184 {
2185         if (entering(tcp)) {
2186                 printpath(tcp, tcp->u_arg[offset]);
2187                 tprints(", ");
2188                 if (tcp->u_arg[offset + 1] == 0)
2189                         tprints("NULL");
2190                 else {
2191                         tprints("{");
2192                         printtv_bitness(tcp, tcp->u_arg[offset + 1],
2193                                         BITNESS_CURRENT, special);
2194                         tprints(", ");
2195                         printtv_bitness(tcp, tcp->u_arg[offset + 1]
2196                                         + sizeof(struct timeval),
2197                                         BITNESS_CURRENT, special);
2198                         tprints("}");
2199                 }
2200         }
2201         return 0;
2202 }
2203
2204 int
2205 sys_utimes(struct tcb *tcp)
2206 {
2207         return decode_utimes(tcp, 0, 0);
2208 }
2209
2210 #ifdef LINUX
2211 int
2212 sys_futimesat(struct tcb *tcp)
2213 {
2214         if (entering(tcp))
2215                 print_dirfd(tcp, tcp->u_arg[0]);
2216         return decode_utimes(tcp, 1, 0);
2217 }
2218
2219 int
2220 sys_utimensat(struct tcb *tcp)
2221 {
2222         if (entering(tcp)) {
2223                 print_dirfd(tcp, tcp->u_arg[0]);
2224                 decode_utimes(tcp, 1, 1);
2225                 tprints(", ");
2226                 printflags(utimensatflags, tcp->u_arg[3], "AT_???");
2227         }
2228         return 0;
2229 }
2230 #endif
2231
2232 int
2233 sys_utime(struct tcb *tcp)
2234 {
2235         union {
2236                 long utl[2];
2237                 int uti[2];
2238         } u;
2239
2240         if (entering(tcp)) {
2241                 printpath(tcp, tcp->u_arg[0]);
2242                 tprints(", ");
2243                 if (!tcp->u_arg[1])
2244                         tprints("NULL");
2245                 else if (!verbose(tcp))
2246                         tprintf("%#lx", tcp->u_arg[1]);
2247                 else if (umoven(tcp, tcp->u_arg[1],
2248                                 2 * personality_wordsize[current_personality],
2249                                 (char *) &u) < 0)
2250                         tprints("[?, ?]");
2251                 else if (personality_wordsize[current_personality]
2252                          == sizeof u.utl[0]) {
2253                         tprintf("[%s,", sprinttime(u.utl[0]));
2254                         tprintf(" %s]", sprinttime(u.utl[1]));
2255                 }
2256                 else if (personality_wordsize[current_personality]
2257                          == sizeof u.uti[0]) {
2258                         tprintf("[%s,", sprinttime(u.uti[0]));
2259                         tprintf(" %s]", sprinttime(u.uti[1]));
2260                 }
2261                 else
2262                         abort();
2263         }
2264         return 0;
2265 }
2266
2267 static int
2268 decode_mknod(struct tcb *tcp, int offset)
2269 {
2270         int mode = tcp->u_arg[offset + 1];
2271
2272         if (entering(tcp)) {
2273                 printpath(tcp, tcp->u_arg[offset]);
2274                 tprintf(", %s", sprintmode(mode));
2275                 switch (mode & S_IFMT) {
2276                 case S_IFCHR: case S_IFBLK:
2277 #ifdef LINUXSPARC
2278                         if (current_personality == 1)
2279                         tprintf(", makedev(%lu, %lu)",
2280                                 (unsigned long) ((tcp->u_arg[offset + 2] >> 18) & 0x3fff),
2281                                 (unsigned long) (tcp->u_arg[offset + 2] & 0x3ffff));
2282                         else
2283 #endif
2284                         tprintf(", makedev(%lu, %lu)",
2285                                 (unsigned long) major(tcp->u_arg[offset + 2]),
2286                                 (unsigned long) minor(tcp->u_arg[offset + 2]));
2287                         break;
2288                 default:
2289                         break;
2290                 }
2291         }
2292         return 0;
2293 }
2294
2295 int
2296 sys_mknod(struct tcb *tcp)
2297 {
2298         return decode_mknod(tcp, 0);
2299 }
2300
2301 #ifdef LINUX
2302 int
2303 sys_mknodat(struct tcb *tcp)
2304 {
2305         if (entering(tcp))
2306                 print_dirfd(tcp, tcp->u_arg[0]);
2307         return decode_mknod(tcp, 1);
2308 }
2309 #endif
2310
2311 #ifdef FREEBSD
2312 int
2313 sys_mkfifo(struct tcb *tcp)
2314 {
2315         if (entering(tcp)) {
2316                 printpath(tcp, tcp->u_arg[0]);
2317                 tprintf(", %#lo", tcp->u_arg[1]);
2318         }
2319         return 0;
2320 }
2321 #endif /* FREEBSD */
2322
2323 int
2324 sys_fsync(struct tcb *tcp)
2325 {
2326         if (entering(tcp)) {
2327                 printfd(tcp, tcp->u_arg[0]);
2328         }
2329         return 0;
2330 }
2331
2332 #ifdef LINUX
2333
2334 static void
2335 printdir(struct tcb *tcp, long addr)
2336 {
2337         struct dirent d;
2338
2339         if (!verbose(tcp)) {
2340                 tprintf("%#lx", addr);
2341                 return;
2342         }
2343         if (umove(tcp, addr, &d) < 0) {
2344                 tprints("{...}");
2345                 return;
2346         }
2347         tprintf("{d_ino=%ld, ", (unsigned long) d.d_ino);
2348         tprints("d_name=");
2349         printpathn(tcp, (long) ((struct dirent *) addr)->d_name, d.d_reclen);
2350         tprints("}");
2351 }
2352
2353 int
2354 sys_readdir(struct tcb *tcp)
2355 {
2356         if (entering(tcp)) {
2357                 printfd(tcp, tcp->u_arg[0]);
2358                 tprints(", ");
2359         } else {
2360                 if (syserror(tcp) || tcp->u_rval == 0 || !verbose(tcp))
2361                         tprintf("%#lx", tcp->u_arg[1]);
2362                 else
2363                         printdir(tcp, tcp->u_arg[1]);
2364                 /* Not much point in printing this out, it is always 1. */
2365                 if (tcp->u_arg[2] != 1)
2366                         tprintf(", %lu", tcp->u_arg[2]);
2367         }
2368         return 0;
2369 }
2370
2371 #endif /* LINUX */
2372
2373 #if defined FREEBSD || defined LINUX
2374 static const struct xlat direnttypes[] = {
2375         { DT_UNKNOWN,   "DT_UNKNOWN"    },
2376         { DT_FIFO,      "DT_FIFO"       },
2377         { DT_CHR,       "DT_CHR"        },
2378         { DT_DIR,       "DT_DIR"        },
2379         { DT_BLK,       "DT_BLK"        },
2380         { DT_REG,       "DT_REG"        },
2381         { DT_LNK,       "DT_LNK"        },
2382         { DT_SOCK,      "DT_SOCK"       },
2383         { DT_WHT,       "DT_WHT"        },
2384         { 0,            NULL            },
2385 };
2386
2387 #endif
2388
2389 int
2390 sys_getdents(struct tcb *tcp)
2391 {
2392         int i, len, dents = 0;
2393         char *buf;
2394
2395         if (entering(tcp)) {
2396                 printfd(tcp, tcp->u_arg[0]);
2397                 tprints(", ");
2398                 return 0;
2399         }
2400         if (syserror(tcp) || !verbose(tcp)) {
2401                 tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
2402                 return 0;
2403         }
2404         len = tcp->u_rval;
2405         /* Beware of insanely large or negative values in tcp->u_rval */
2406         if (tcp->u_rval > 1024*1024)
2407                 len = 1024*1024;
2408         if (tcp->u_rval < 0)
2409                 len = 0;
2410         buf = len ? malloc(len) : NULL;
2411         if (len && !buf)
2412                 die_out_of_memory();
2413         if (umoven(tcp, tcp->u_arg[1], len, buf) < 0) {
2414                 tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
2415                 free(buf);
2416                 return 0;
2417         }
2418         if (!abbrev(tcp))
2419                 tprints("{");
2420         for (i = 0; i < len;) {
2421                 struct kernel_dirent *d = (struct kernel_dirent *) &buf[i];
2422 #ifdef LINUX
2423                 if (!abbrev(tcp)) {
2424                         tprintf("%s{d_ino=%lu, d_off=%lu, ",
2425                                 i ? " " : "", d->d_ino, d->d_off);
2426                         tprintf("d_reclen=%u, d_name=\"%s\"}",
2427                                 d->d_reclen, d->d_name);
2428                 }
2429 #endif /* LINUX */
2430 #ifdef SVR4
2431                 if (!abbrev(tcp)) {
2432                         tprintf("%s{d_ino=%lu, d_off=%lu, ",
2433                                 i ? " " : "",
2434                                 (unsigned long) d->d_ino,
2435                                 (unsigned long) d->d_off);
2436                         tprintf("d_reclen=%u, d_name=\"%s\"}",
2437                                 d->d_reclen, d->d_name);
2438                 }
2439 #endif /* SVR4 */
2440 #ifdef SUNOS4
2441                 if (!abbrev(tcp)) {
2442                         tprintf("%s{d_off=%lu, d_fileno=%lu, d_reclen=%u, ",
2443                                 i ? " " : "", d->d_off, d->d_fileno,
2444                                 d->d_reclen);
2445                         tprintf("d_namlen=%u, d_name=\"%.*s\"}",
2446                                 d->d_namlen, d->d_namlen, d->d_name);
2447                 }
2448 #endif /* SUNOS4 */
2449 #ifdef FREEBSD
2450                 if (!abbrev(tcp)) {
2451                         tprintf("%s{d_fileno=%u, d_reclen=%u, d_type=",
2452                                 i ? " " : "", d->d_fileno, d->d_reclen);
2453                         printxval(direnttypes, d->d_type, "DT_???");
2454                         tprintf(", d_namlen=%u, d_name=\"%.*s\"}",
2455                                 d->d_namlen, d->d_namlen, d->d_name);
2456                 }
2457 #endif /* FREEBSD */
2458                 if (!d->d_reclen) {
2459                         tprints("/* d_reclen == 0, problem here */");
2460                         break;
2461                 }
2462                 i += d->d_reclen;
2463                 dents++;
2464         }
2465         if (!abbrev(tcp))
2466                 tprints("}");
2467         else
2468                 tprintf("/* %u entries */", dents);
2469         tprintf(", %lu", tcp->u_arg[2]);
2470         free(buf);
2471         return 0;
2472 }
2473
2474
2475 #if _LFS64_LARGEFILE
2476 int
2477 sys_getdents64(struct tcb *tcp)
2478 {
2479         int i, len, dents = 0;
2480         char *buf;
2481
2482         if (entering(tcp)) {
2483                 printfd(tcp, tcp->u_arg[0]);
2484                 tprints(", ");
2485                 return 0;
2486         }
2487         if (syserror(tcp) || !verbose(tcp)) {
2488                 tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
2489                 return 0;
2490         }
2491
2492         len = tcp->u_rval;
2493         /* Beware of insanely large or negative tcp->u_rval */
2494         if (tcp->u_rval > 1024*1024)
2495                 len = 1024*1024;
2496         if (tcp->u_rval < 0)
2497                 len = 0;
2498         buf = len ? malloc(len) : NULL;
2499         if (len && !buf)
2500                 die_out_of_memory();
2501
2502         if (umoven(tcp, tcp->u_arg[1], len, buf) < 0) {
2503                 tprintf("%#lx, %lu", tcp->u_arg[1], tcp->u_arg[2]);
2504                 free(buf);
2505                 return 0;
2506         }
2507         if (!abbrev(tcp))
2508                 tprints("{");
2509         for (i = 0; i < len;) {
2510                 struct dirent64 *d = (struct dirent64 *) &buf[i];
2511 #if defined(LINUX) || defined(SVR4)
2512                 if (!abbrev(tcp)) {
2513                         tprintf("%s{d_ino=%" PRIu64 ", d_off=%" PRId64 ", ",
2514                                 i ? " " : "",
2515                                 d->d_ino,
2516                                 d->d_off);
2517 #ifdef LINUX
2518                         tprints("d_type=");
2519                         printxval(direnttypes, d->d_type, "DT_???");
2520                         tprints(", ");
2521 #endif
2522                         tprintf("d_reclen=%u, d_name=\"%s\"}",
2523                                 d->d_reclen, d->d_name);
2524                 }
2525 #endif /* LINUX || SVR4 */
2526 #ifdef SUNOS4
2527                 if (!abbrev(tcp)) {
2528                         tprintf("%s{d_off=%lu, d_fileno=%lu, d_reclen=%u, ",
2529                                 i ? " " : "", d->d_off, d->d_fileno,
2530                                 d->d_reclen);
2531                         tprintf("d_namlen=%u, d_name=\"%.*s\"}",
2532                                 d->d_namlen, d->d_namlen, d->d_name);
2533                 }
2534 #endif /* SUNOS4 */
2535                 if (!d->d_reclen) {
2536                         tprints("/* d_reclen == 0, problem here */");
2537                         break;
2538                 }
2539                 i += d->d_reclen;
2540                 dents++;
2541         }
2542         if (!abbrev(tcp))
2543                 tprints("}");
2544         else
2545                 tprintf("/* %u entries */", dents);
2546         tprintf(", %lu", tcp->u_arg[2]);
2547         free(buf);
2548         return 0;
2549 }
2550 #endif
2551
2552 #ifdef FREEBSD
2553 int
2554 sys_getdirentries(struct tcb *tcp)
2555 {
2556         int i, len, dents = 0;
2557         long basep;
2558         char *buf;
2559
2560         if (entering(tcp)) {
2561                 printfd(tcp, tcp->u_arg[0]);
2562                 tprints(", ");
2563                 return 0;
2564         }
2565         if (syserror(tcp) || !verbose(tcp)) {
2566                 tprintf("%#lx, %lu, %#lx", tcp->u_arg[1], tcp->u_arg[2], tcp->u_arg[3]);
2567                 return 0;
2568         }
2569
2570         len = tcp->u_rval;
2571         /* Beware of insanely large or negative tcp->u_rval */
2572         if (tcp->u_rval > 1024*1024)
2573                 len = 1024*1024;
2574         if (tcp->u_rval < 0)
2575                 len = 0;
2576         buf = malloc(len);
2577         if (!buf)
2578                 die_out_of_memory();
2579
2580         if (umoven(tcp, tcp->u_arg[1], len, buf) < 0) {
2581                 tprintf("%#lx, %lu, %#lx", tcp->u_arg[1], tcp->u_arg[2], tcp->u_arg[3]);
2582                 free(buf);
2583                 return 0;
2584         }
2585         if (!abbrev(tcp))
2586                 tprints("{");
2587         for (i = 0; i < len;) {
2588                 struct kernel_dirent *d = (struct kernel_dirent *) &buf[i];
2589                 if (!abbrev(tcp)) {
2590                         tprintf("%s{d_fileno=%u, d_reclen=%u, d_type=",
2591                                 i ? " " : "", d->d_fileno, d->d_reclen);
2592                         printxval(direnttypes, d->d_type, "DT_???");
2593                         tprintf(", d_namlen=%u, d_name=\"%.*s\"}",
2594                                 d->d_namlen, d->d_namlen, d->d_name);
2595                 }
2596                 if (!d->d_reclen) {
2597                         tprints("/* d_reclen == 0, problem here */");
2598                         break;
2599                 }
2600                 i += d->d_reclen;
2601                 dents++;
2602         }
2603         if (!abbrev(tcp))
2604                 tprints("}");
2605         else
2606                 tprintf("/* %u entries */", dents);
2607         free(buf);
2608         tprintf(", %lu", tcp->u_arg[2]);
2609         if (umove(tcp, tcp->u_arg[3], &basep) < 0)
2610                 tprintf(", %#lx", tcp->u_arg[3]);
2611         else
2612                 tprintf(", [%lu]", basep);
2613         return 0;
2614 }
2615 #endif
2616
2617 #ifdef LINUX
2618 int
2619 sys_getcwd(struct tcb *tcp)
2620 {
2621         if (exiting(tcp)) {
2622                 if (syserror(tcp))
2623                         tprintf("%#lx", tcp->u_arg[0]);
2624                 else
2625                         printpathn(tcp, tcp->u_arg[0], tcp->u_rval - 1);
2626                 tprintf(", %lu", tcp->u_arg[1]);
2627         }
2628         return 0;
2629 }
2630 #endif /* LINUX */
2631
2632 #ifdef FREEBSD
2633 int
2634 sys___getcwd(struct tcb *tcp)
2635 {
2636         if (exiting(tcp)) {
2637                 if (syserror(tcp))
2638                         tprintf("%#lx", tcp->u_arg[0]);
2639                 else
2640                         printpathn(tcp, tcp->u_arg[0], tcp->u_arg[1]);
2641                 tprintf(", %lu", tcp->u_arg[1]);
2642         }
2643         return 0;
2644 }
2645 #endif
2646
2647 #ifdef HAVE_SYS_ASYNCH_H
2648
2649 int
2650 sys_aioread(struct tcb *tcp)
2651 {
2652         struct aio_result_t res;
2653
2654         if (entering(tcp)) {
2655                 tprintf("%lu, ", tcp->u_arg[0]);
2656         } else {
2657                 if (syserror(tcp))
2658                         tprintf("%#lx", tcp->u_arg[1]);
2659                 else
2660                         printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2661                 tprintf(", %lu, %lu, ", tcp->u_arg[2], tcp->u_arg[3]);
2662                 printxval(whence, tcp->u_arg[4], "L_???");
2663                 if (syserror(tcp) || tcp->u_arg[5] == 0
2664                     || umove(tcp, tcp->u_arg[5], &res) < 0)
2665                         tprintf(", %#lx", tcp->u_arg[5]);
2666                 else
2667                         tprintf(", {aio_return %d aio_errno %d}",
2668                                 res.aio_return, res.aio_errno);
2669         }
2670         return 0;
2671 }
2672
2673 int
2674 sys_aiowrite(struct tcb *tcp)
2675 {
2676         struct aio_result_t res;
2677
2678         if (entering(tcp)) {
2679                 tprintf("%lu, ", tcp->u_arg[0]);
2680                 printstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2681                 tprintf(", %lu, %lu, ", tcp->u_arg[2], tcp->u_arg[3]);
2682                 printxval(whence, tcp->u_arg[4], "L_???");
2683         }
2684         else {
2685                 if (tcp->u_arg[5] == 0)
2686                         tprints(", NULL");
2687                 else if (syserror(tcp)
2688                     || umove(tcp, tcp->u_arg[5], &res) < 0)
2689                         tprintf(", %#lx", tcp->u_arg[5]);
2690                 else
2691                         tprintf(", {aio_return %d aio_errno %d}",
2692                                 res.aio_return, res.aio_errno);
2693         }
2694         return 0;
2695 }
2696
2697 int
2698 sys_aiowait(struct tcb *tcp)
2699 {
2700         if (entering(tcp))
2701                 printtv(tcp, tcp->u_arg[0]);
2702         return 0;
2703 }
2704
2705 int
2706 sys_aiocancel(struct tcb *tcp)
2707 {
2708         struct aio_result_t res;
2709
2710         if (exiting(tcp)) {
2711                 if (tcp->u_arg[0] == 0)
2712                         tprints("NULL");
2713                 else if (syserror(tcp)
2714                     || umove(tcp, tcp->u_arg[0], &res) < 0)
2715                         tprintf("%#lx", tcp->u_arg[0]);
2716                 else
2717                         tprintf("{aio_return %d aio_errno %d}",
2718                                 res.aio_return, res.aio_errno);
2719         }
2720         return 0;
2721 }
2722
2723 #endif /* HAVE_SYS_ASYNCH_H */
2724
2725 static const struct xlat xattrflags[] = {
2726 #ifdef XATTR_CREATE
2727         { XATTR_CREATE,  "XATTR_CREATE" },
2728         { XATTR_REPLACE, "XATTR_REPLACE" },
2729 #endif
2730         { 0,             NULL }
2731 };
2732
2733 static void
2734 print_xattr_val(struct tcb *tcp, int failed,
2735                 unsigned long arg,
2736                 unsigned long insize,
2737                 unsigned long size)
2738 {
2739         if (!failed) {
2740                 unsigned long capacity = 4 * size + 1;
2741                 unsigned char *buf = (capacity < size) ? NULL : malloc(capacity);
2742                 if (buf == NULL || /* probably a bogus size argument */
2743                         umoven(tcp, arg, size, (char *) &buf[3 * size]) < 0) {
2744                         failed = 1;
2745                 }
2746                 else {
2747                         unsigned char *out = buf;
2748                         unsigned char *in = &buf[3 * size];
2749                         size_t i;
2750                         for (i = 0; i < size; ++i) {
2751                                 if (isprint(in[i]))
2752                                         *out++ = in[i];
2753                                 else {
2754 #define tohex(n) "0123456789abcdef"[n]
2755                                         *out++ = '\\';
2756                                         *out++ = 'x';
2757                                         *out++ = tohex(in[i] / 16);
2758                                         *out++ = tohex(in[i] % 16);
2759                                 }
2760                         }
2761                         /* Don't print terminating NUL if there is one.  */
2762                         if (i > 0 && in[i - 1] == '\0')
2763                                 out -= 4;
2764                         *out = '\0';
2765                         tprintf(", \"%s\", %ld", buf, insize);
2766                 }
2767                 free(buf);
2768         }
2769         if (failed)
2770                 tprintf(", 0x%lx, %ld", arg, insize);
2771 }
2772
2773 int
2774 sys_setxattr(struct tcb *tcp)
2775 {
2776         if (entering(tcp)) {
2777                 printpath(tcp, tcp->u_arg[0]);
2778                 tprints(", ");
2779                 printstr(tcp, tcp->u_arg[1], -1);
2780                 print_xattr_val(tcp, 0, tcp->u_arg[2], tcp->u_arg[3], tcp->u_arg[3]);
2781                 tprints(", ");
2782                 printflags(xattrflags, tcp->u_arg[4], "XATTR_???");
2783         }
2784         return 0;
2785 }
2786
2787 int
2788 sys_fsetxattr(struct tcb *tcp)
2789 {
2790         if (entering(tcp)) {
2791                 printfd(tcp, tcp->u_arg[0]);
2792                 tprints(", ");
2793                 printstr(tcp, tcp->u_arg[1], -1);
2794                 print_xattr_val(tcp, 0, tcp->u_arg[2], tcp->u_arg[3], tcp->u_arg[3]);
2795                 tprints(", ");
2796                 printflags(xattrflags, tcp->u_arg[4], "XATTR_???");
2797         }
2798         return 0;
2799 }
2800
2801 int
2802 sys_getxattr(struct tcb *tcp)
2803 {
2804         if (entering(tcp)) {
2805                 printpath(tcp, tcp->u_arg[0]);
2806                 tprints(", ");
2807                 printstr(tcp, tcp->u_arg[1], -1);
2808         } else {
2809                 print_xattr_val(tcp, syserror(tcp), tcp->u_arg[2], tcp->u_arg[3],
2810                                 tcp->u_rval);
2811         }
2812         return 0;
2813 }
2814
2815 int
2816 sys_fgetxattr(struct tcb *tcp)
2817 {
2818         if (entering(tcp)) {
2819                 printfd(tcp, tcp->u_arg[0]);
2820                 tprints(", ");
2821                 printstr(tcp, tcp->u_arg[1], -1);
2822         } else {
2823                 print_xattr_val(tcp, syserror(tcp), tcp->u_arg[2], tcp->u_arg[3],
2824                                 tcp->u_rval);
2825         }
2826         return 0;
2827 }
2828
2829 int
2830 sys_listxattr(struct tcb *tcp)
2831 {
2832         if (entering(tcp)) {
2833                 printpath(tcp, tcp->u_arg[0]);
2834         } else {
2835                 /* XXX Print value in format */
2836                 tprintf(", %p, %lu", (void *) tcp->u_arg[1], tcp->u_arg[2]);
2837         }
2838         return 0;
2839 }
2840
2841 int
2842 sys_flistxattr(struct tcb *tcp)
2843 {
2844         if (entering(tcp)) {
2845                 printfd(tcp, tcp->u_arg[0]);
2846         } else {
2847                 /* XXX Print value in format */
2848                 tprintf(", %p, %lu", (void *) tcp->u_arg[1], tcp->u_arg[2]);
2849         }
2850         return 0;
2851 }
2852
2853 int
2854 sys_removexattr(struct tcb *tcp)
2855 {
2856         if (entering(tcp)) {
2857                 printpath(tcp, tcp->u_arg[0]);
2858                 tprints(", ");
2859                 printstr(tcp, tcp->u_arg[1], -1);
2860         }
2861         return 0;
2862 }
2863
2864 int
2865 sys_fremovexattr(struct tcb *tcp)
2866 {
2867         if (entering(tcp)) {
2868                 printfd(tcp, tcp->u_arg[0]);
2869                 tprints(", ");
2870                 printstr(tcp, tcp->u_arg[1], -1);
2871         }
2872         return 0;
2873 }
2874
2875
2876 static const struct xlat advise[] = {
2877   { POSIX_FADV_NORMAL,          "POSIX_FADV_NORMAL"     },
2878   { POSIX_FADV_RANDOM,          "POSIX_FADV_RANDOM"     },
2879   { POSIX_FADV_SEQUENTIAL,      "POSIX_FADV_SEQUENTIAL" },
2880   { POSIX_FADV_WILLNEED,        "POSIX_FADV_WILLNEED"   },
2881   { POSIX_FADV_DONTNEED,        "POSIX_FADV_DONTNEED"   },
2882   { POSIX_FADV_NOREUSE,         "POSIX_FADV_NOREUSE"    },
2883   { 0,                          NULL                    }
2884 };
2885
2886
2887 #ifdef LINUX
2888 int
2889 sys_fadvise64(struct tcb *tcp)
2890 {
2891         if (entering(tcp)) {
2892                 int argn;
2893                 printfd(tcp, tcp->u_arg[0]);
2894                 tprints(", ");
2895                 argn = printllval(tcp, "%lld", 1);
2896                 tprintf(", %ld, ", tcp->u_arg[argn++]);
2897                 printxval(advise, tcp->u_arg[argn], "POSIX_FADV_???");
2898         }
2899         return 0;
2900 }
2901 #endif
2902
2903
2904 int
2905 sys_fadvise64_64(struct tcb *tcp)
2906 {
2907         if (entering(tcp)) {
2908                 int argn;
2909                 printfd(tcp, tcp->u_arg[0]);
2910                 tprints(", ");
2911 #if defined ARM || defined POWERPC
2912                 argn = printllval(tcp, "%lld, ", 2);
2913 #else
2914                 argn = printllval(tcp, "%lld, ", 1);
2915 #endif
2916                 argn = printllval(tcp, "%lld, ", argn);
2917 #if defined ARM || defined POWERPC
2918                 printxval(advise, tcp->u_arg[1], "POSIX_FADV_???");
2919 #else
2920                 printxval(advise, tcp->u_arg[argn], "POSIX_FADV_???");
2921 #endif
2922         }
2923         return 0;
2924 }
2925
2926 #ifdef LINUX
2927 static const struct xlat inotify_modes[] = {
2928         { 0x00000001,   "IN_ACCESS"     },
2929         { 0x00000002,   "IN_MODIFY"     },
2930         { 0x00000004,   "IN_ATTRIB"     },
2931         { 0x00000008,   "IN_CLOSE_WRITE"},
2932         { 0x00000010,   "IN_CLOSE_NOWRITE"},
2933         { 0x00000020,   "IN_OPEN"       },
2934         { 0x00000040,   "IN_MOVED_FROM" },
2935         { 0x00000080,   "IN_MOVED_TO"   },
2936         { 0x00000100,   "IN_CREATE"     },
2937         { 0x00000200,   "IN_DELETE"     },
2938         { 0x00000400,   "IN_DELETE_SELF"},
2939         { 0x00000800,   "IN_MOVE_SELF"  },
2940         { 0x00002000,   "IN_UNMOUNT"    },
2941         { 0x00004000,   "IN_Q_OVERFLOW" },
2942         { 0x00008000,   "IN_IGNORED"    },
2943         { 0x01000000,   "IN_ONLYDIR"    },
2944         { 0x02000000,   "IN_DONT_FOLLOW"},
2945         { 0x20000000,   "IN_MASK_ADD"   },
2946         { 0x40000000,   "IN_ISDIR"      },
2947         { 0x80000000,   "IN_ONESHOT"    },
2948         { 0,            NULL            }
2949 };
2950
2951 static const struct xlat inotify_init_flags[] = {
2952         { 0x00000800,   "IN_NONBLOCK"   },
2953         { 0x00080000,   "IN_CLOEXEC"    },
2954         { 0,            NULL            }
2955 };
2956
2957 int
2958 sys_inotify_add_watch(struct tcb *tcp)
2959 {
2960         if (entering(tcp)) {
2961                 printfd(tcp, tcp->u_arg[0]);
2962                 tprints(", ");
2963                 printpath(tcp, tcp->u_arg[1]);
2964                 tprints(", ");
2965                 printflags(inotify_modes, tcp->u_arg[2], "IN_???");
2966         }
2967         return 0;
2968 }
2969
2970 int
2971 sys_inotify_rm_watch(struct tcb *tcp)
2972 {
2973         if (entering(tcp)) {
2974                 printfd(tcp, tcp->u_arg[0]);
2975                 tprintf(", %ld", tcp->u_arg[1]);
2976         }
2977         return 0;
2978 }
2979
2980 int
2981 sys_inotify_init1(struct tcb *tcp)
2982 {
2983         if (entering(tcp))
2984                 printflags(inotify_init_flags, tcp->u_arg[0], "IN_???");
2985         return 0;
2986 }
2987
2988 int
2989 sys_fallocate(struct tcb *tcp)
2990 {
2991         if (entering(tcp)) {
2992                 int argn;
2993                 printfd(tcp, tcp->u_arg[0]);            /* fd */
2994                 tprints(", ");
2995                 tprintf("%#lo, ", tcp->u_arg[1]);       /* mode */
2996                 argn = printllval(tcp, "%llu, ", 2);    /* offset */
2997                 printllval(tcp, "%llu", argn);          /* len */
2998         }
2999         return 0;
3000 }
3001 #endif