Home | History | Annotate | Download | only in strace
      1 /*
      2  * Copyright (c) 1991, 1992 Paul Kranenburg <pk (at) cs.few.eur.nl>
      3  * Copyright (c) 1993 Branko Lankester <branko (at) hacktic.nl>
      4  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs (at) world.std.com>
      5  * Copyright (c) 1996-1999 Wichert Akkerman <wichert (at) cistron.nl>
      6  * Copyright (c) 2005-2007 Roland McGrath <roland (at) redhat.com>
      7  * Copyright (c) 2006-2007 Ulrich Drepper <drepper (at) redhat.com>
      8  * Copyright (c) 2009-2013 Denys Vlasenko <dvlasenk (at) redhat.com>
      9  * Copyright (c) 2005-2015 Dmitry V. Levin <ldv (at) altlinux.org>
     10  * All rights reserved.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include "defs.h"
     36 
     37 #include <fcntl.h>
     38 
     39 /* some libcs are guilty of messing up with O_ACCMODE */
     40 #undef O_ACCMODE
     41 #define O_ACCMODE 03
     42 
     43 #ifdef O_LARGEFILE
     44 # if O_LARGEFILE == 0          /* biarch platforms in 64-bit mode */
     45 #  undef O_LARGEFILE
     46 #  ifdef SPARC64
     47 #   define O_LARGEFILE 0x40000
     48 #  elif defined X86_64 || defined S390X
     49 #   define O_LARGEFILE 0100000
     50 #  endif
     51 # endif
     52 #endif
     53 
     54 #include "xlat/open_access_modes.h"
     55 #include "xlat/open_mode_flags.h"
     56 
     57 #ifndef AT_FDCWD
     58 # define AT_FDCWD                -100
     59 #endif
     60 
     61 /* The fd is an "int", so when decoding x86 on x86_64, we need to force sign
     62  * extension to get the right value.  We do this by declaring fd as int here.
     63  */
     64 void
     65 print_dirfd(struct tcb *tcp, int fd)
     66 {
     67 	if (fd == AT_FDCWD)
     68 		tprints("AT_FDCWD, ");
     69 	else {
     70 		printfd(tcp, fd);
     71 		tprints(", ");
     72 	}
     73 }
     74 
     75 /*
     76  * low bits of the open(2) flags define access mode,
     77  * other bits are real flags.
     78  */
     79 const char *
     80 sprint_open_modes(unsigned int flags)
     81 {
     82 	static char outstr[(1 + ARRAY_SIZE(open_mode_flags)) * sizeof("O_LARGEFILE")];
     83 	char *p;
     84 	char sep;
     85 	const char *str;
     86 	const struct xlat *x;
     87 
     88 	sep = ' ';
     89 	p = stpcpy(outstr, "flags");
     90 	str = xlookup(open_access_modes, flags & 3);
     91 	if (str) {
     92 		*p++ = sep;
     93 		p = stpcpy(p, str);
     94 		flags &= ~3;
     95 		if (!flags)
     96 			return outstr;
     97 		sep = '|';
     98 	}
     99 
    100 	for (x = open_mode_flags; x->str; x++) {
    101 		if ((flags & x->val) == x->val) {
    102 			*p++ = sep;
    103 			p = stpcpy(p, x->str);
    104 			flags &= ~x->val;
    105 			if (!flags)
    106 				return outstr;
    107 			sep = '|';
    108 		}
    109 	}
    110 	/* flags is still nonzero */
    111 	*p++ = sep;
    112 	sprintf(p, "%#x", flags);
    113 	return outstr;
    114 }
    115 
    116 void
    117 tprint_open_modes(unsigned int flags)
    118 {
    119 	tprints(sprint_open_modes(flags) + sizeof("flags"));
    120 }
    121 
    122 #ifdef O_TMPFILE
    123 /* The kernel & C libraries often inline O_DIRECTORY. */
    124 # define STRACE_O_TMPFILE (O_TMPFILE & ~O_DIRECTORY)
    125 #else /* !O_TMPFILE */
    126 # define STRACE_O_TMPFILE 0
    127 #endif
    128 
    129 static int
    130 decode_open(struct tcb *tcp, int offset)
    131 {
    132 	printpath(tcp, tcp->u_arg[offset]);
    133 	tprints(", ");
    134 	/* flags */
    135 	tprint_open_modes(tcp->u_arg[offset + 1]);
    136 	if (tcp->u_arg[offset + 1] & (O_CREAT | STRACE_O_TMPFILE)) {
    137 		/* mode */
    138 		tprints(", ");
    139 		print_numeric_umode_t(tcp->u_arg[offset + 2]);
    140 	}
    141 
    142 	return RVAL_DECODED | RVAL_FD;
    143 }
    144 
    145 SYS_FUNC(open)
    146 {
    147 	return decode_open(tcp, 0);
    148 }
    149 
    150 SYS_FUNC(openat)
    151 {
    152 	print_dirfd(tcp, tcp->u_arg[0]);
    153 	return decode_open(tcp, 1);
    154 }
    155 
    156 SYS_FUNC(creat)
    157 {
    158 	printpath(tcp, tcp->u_arg[0]);
    159 	tprints(", ");
    160 	print_numeric_umode_t(tcp->u_arg[1]);
    161 
    162 	return RVAL_DECODED | RVAL_FD;
    163 }
    164