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 #ifdef O_LARGEFILE
     40 # if O_LARGEFILE == 0          /* biarch platforms in 64-bit mode */
     41 #  undef O_LARGEFILE
     42 #  ifdef SPARC64
     43 #   define O_LARGEFILE 0x40000
     44 #  elif defined X86_64 || defined S390X
     45 #   define O_LARGEFILE 0100000
     46 #  endif
     47 # endif
     48 #endif
     49 
     50 #include "xlat/open_access_modes.h"
     51 #include "xlat/open_mode_flags.h"
     52 
     53 #ifndef AT_FDCWD
     54 # define AT_FDCWD                -100
     55 #endif
     56 
     57 /* The fd is an "int", so when decoding x86 on x86_64, we need to force sign
     58  * extension to get the right value.  We do this by declaring fd as int here.
     59  */
     60 void
     61 print_dirfd(struct tcb *tcp, int fd)
     62 {
     63 	if (fd == AT_FDCWD)
     64 		tprints("AT_FDCWD, ");
     65 	else {
     66 		printfd(tcp, fd);
     67 		tprints(", ");
     68 	}
     69 }
     70 
     71 /*
     72  * low bits of the open(2) flags define access mode,
     73  * other bits are real flags.
     74  */
     75 const char *
     76 sprint_open_modes(int flags)
     77 {
     78 	static char outstr[(1 + ARRAY_SIZE(open_mode_flags)) * sizeof("O_LARGEFILE")];
     79 	char *p;
     80 	char sep;
     81 	const char *str;
     82 	const struct xlat *x;
     83 
     84 	sep = ' ';
     85 	p = stpcpy(outstr, "flags");
     86 	str = xlookup(open_access_modes, flags & 3);
     87 	if (str) {
     88 		*p++ = sep;
     89 		p = stpcpy(p, str);
     90 		flags &= ~3;
     91 		if (!flags)
     92 			return outstr;
     93 		sep = '|';
     94 	}
     95 
     96 	for (x = open_mode_flags; x->str; x++) {
     97 		if ((flags & x->val) == x->val) {
     98 			*p++ = sep;
     99 			p = stpcpy(p, x->str);
    100 			flags &= ~x->val;
    101 			if (!flags)
    102 				return outstr;
    103 			sep = '|';
    104 		}
    105 	}
    106 	/* flags is still nonzero */
    107 	*p++ = sep;
    108 	sprintf(p, "%#x", flags);
    109 	return outstr;
    110 }
    111 
    112 void
    113 tprint_open_modes(int flags)
    114 {
    115 	tprints(sprint_open_modes(flags) + sizeof("flags"));
    116 }
    117 
    118 static int
    119 decode_open(struct tcb *tcp, int offset)
    120 {
    121 	printpath(tcp, tcp->u_arg[offset]);
    122 	tprints(", ");
    123 	/* flags */
    124 	tprint_open_modes(tcp->u_arg[offset + 1]);
    125 	if (tcp->u_arg[offset + 1] & O_CREAT) {
    126 		/* mode */
    127 		tprintf(", %#lo", tcp->u_arg[offset + 2]);
    128 	}
    129 
    130 	return RVAL_DECODED | RVAL_FD;
    131 }
    132 
    133 SYS_FUNC(open)
    134 {
    135 	return decode_open(tcp, 0);
    136 }
    137 
    138 SYS_FUNC(openat)
    139 {
    140 	print_dirfd(tcp, tcp->u_arg[0]);
    141 	return decode_open(tcp, 1);
    142 }
    143 
    144 SYS_FUNC(creat)
    145 {
    146 	printpath(tcp, tcp->u_arg[0]);
    147 	tprintf(", %#lo", tcp->u_arg[1]);
    148 
    149 	return RVAL_DECODED | RVAL_FD;
    150 }
    151