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