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