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