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