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