]> granicus.if.org Git - strace/blob - desc.c
Check for current_klongsize instead of current_personality where appropriate
[strace] / desc.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  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "defs.h"
32
33 SYS_FUNC(close)
34 {
35         printfd(tcp, tcp->u_arg[0]);
36
37         return RVAL_DECODED;
38 }
39
40 SYS_FUNC(dup)
41 {
42         printfd(tcp, tcp->u_arg[0]);
43
44         return RVAL_DECODED | RVAL_FD;
45 }
46
47 static int
48 do_dup2(struct tcb *tcp, int flags_arg)
49 {
50         printfd(tcp, tcp->u_arg[0]);
51         tprints(", ");
52         printfd(tcp, tcp->u_arg[1]);
53         if (flags_arg >= 0) {
54                 tprints(", ");
55                 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
56         }
57
58         return RVAL_DECODED | RVAL_FD;
59 }
60
61 SYS_FUNC(dup2)
62 {
63         return do_dup2(tcp, -1);
64 }
65
66 SYS_FUNC(dup3)
67 {
68         return do_dup2(tcp, 2);
69 }
70
71 static int
72 decode_select(struct tcb *tcp, kernel_ureg_t *args,
73               void (*print_tv_ts) (struct tcb *, const long),
74               const char * (*sprint_tv_ts) (struct tcb *, const long))
75 {
76         int i, j;
77         int nfds, fdsize;
78         fd_set *fds = NULL;
79         const char *sep;
80         long arg;
81
82         /* Kernel truncates arg[0] to int, we do the same. */
83         nfds = (int) args[0];
84
85         /* Kernel rejects negative nfds, so we don't parse it either. */
86         if (nfds < 0)
87                 nfds = 0;
88
89         /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
90         if (nfds > 1024*1024)
91                 nfds = 1024*1024;
92
93         /*
94          * We had bugs a-la "while (j < args[0])" and "umoven(args[0])" below.
95          * Instead of args[0], use nfds for fd count, fdsize for array lengths.
96          */
97         fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
98
99         if (entering(tcp)) {
100                 tprintf("%d", (int) args[0]);
101
102                 if (verbose(tcp) && fdsize > 0)
103                         fds = malloc(fdsize);
104                 for (i = 0; i < 3; i++) {
105                         arg = args[i+1];
106                         tprints(", ");
107                         if (!fds) {
108                                 printaddr(arg);
109                                 continue;
110                         }
111                         if (umoven_or_printaddr(tcp, arg, fdsize, fds))
112                                 continue;
113                         tprints("[");
114                         for (j = 0, sep = "";; j++) {
115                                 j = next_set_bit(fds, j, nfds);
116                                 if (j < 0)
117                                         break;
118                                 tprints(sep);
119                                 printfd(tcp, j);
120                                 sep = " ";
121                         }
122                         tprints("]");
123                 }
124                 free(fds);
125                 tprints(", ");
126                 print_tv_ts(tcp, args[4]);
127         } else {
128                 static char outstr[1024];
129                 char *outptr;
130 #define end_outstr (outstr + sizeof(outstr))
131                 int ready_fds;
132
133                 if (syserror(tcp))
134                         return 0;
135
136                 ready_fds = tcp->u_rval;
137                 if (ready_fds == 0) {
138                         tcp->auxstr = "Timeout";
139                         return RVAL_STR;
140                 }
141
142                 fds = malloc(fdsize);
143
144                 outptr = outstr;
145                 sep = "";
146                 for (i = 0; i < 3 && ready_fds > 0; i++) {
147                         int first = 1;
148
149                         arg = args[i+1];
150                         if (!arg || !fds || umoven(tcp, arg, fdsize, fds) < 0)
151                                 continue;
152                         for (j = 0;; j++) {
153                                 j = next_set_bit(fds, j, nfds);
154                                 if (j < 0)
155                                         break;
156                                 /* +2 chars needed at the end: ']',NUL */
157                                 if (outptr < end_outstr - (sizeof(", except [") + sizeof(int)*3 + 2)) {
158                                         if (first) {
159                                                 outptr += sprintf(outptr, "%s%s [%u",
160                                                         sep,
161                                                         i == 0 ? "in" : i == 1 ? "out" : "except",
162                                                         j
163                                                 );
164                                                 first = 0;
165                                                 sep = ", ";
166                                         }
167                                         else {
168                                                 outptr += sprintf(outptr, " %u", j);
169                                         }
170                                 }
171                                 if (--ready_fds == 0)
172                                         break;
173                         }
174                         if (outptr != outstr)
175                                 *outptr++ = ']';
176                 }
177                 free(fds);
178                 /* This contains no useful information on SunOS.  */
179                 if (args[4]) {
180                         const char *str = sprint_tv_ts(tcp, args[4]);
181                         if (outptr + sizeof("left ") + strlen(sep) + strlen(str) < end_outstr) {
182                                 outptr += sprintf(outptr, "%sleft %s", sep, str);
183                         }
184                 }
185                 *outptr = '\0';
186                 tcp->auxstr = outstr;
187                 return RVAL_STR;
188 #undef end_outstr
189         }
190         return 0;
191 }
192
193 SYS_FUNC(oldselect)
194 {
195         kernel_ureg_t select_args[5];
196         unsigned int oldselect_args[5];
197
198         if (sizeof(*select_args) == sizeof(*oldselect_args)) {
199                 if (umove_or_printaddr(tcp, tcp->u_arg[0], &select_args)) {
200                         return 0;
201                 }
202         } else {
203                 unsigned int i;
204
205                 if (umove_or_printaddr(tcp, tcp->u_arg[0], &oldselect_args)) {
206                         return 0;
207                 }
208
209                 for (i = 0; i < 5; ++i) {
210                         select_args[i] = oldselect_args[i];
211                 }
212         }
213
214         return decode_select(tcp, select_args, print_timeval, sprint_timeval);
215 }
216
217 #ifdef ALPHA
218 SYS_FUNC(osf_select)
219 {
220         return decode_select(tcp, tcp->u_arg, print_timeval32, sprint_timeval32);
221 }
222 #endif
223
224 SYS_FUNC(select)
225 {
226         return decode_select(tcp, tcp->u_arg, print_timeval, sprint_timeval);
227 }
228
229 static int
230 umove_kulong_array_or_printaddr(struct tcb *tcp, const long addr,
231                                 kernel_ulong_t *ptr, size_t n)
232 {
233 #ifndef current_klongsize
234         if (current_klongsize < sizeof(*ptr)) {
235                 uint32_t ptr32[n];
236                 int r = umove_or_printaddr(tcp, addr, &ptr32);
237                 if (!r) {
238                         size_t i;
239
240                         for (i = 0; i < n; ++i)
241                                 ptr[i] = (kernel_ulong_t) ptr32[i];
242                 }
243                 return r;
244         }
245 #endif /* !current_klongsize */
246         return umoven_or_printaddr(tcp, addr, n * sizeof(*ptr), ptr);
247 }
248
249 SYS_FUNC(pselect6)
250 {
251         int rc = decode_select(tcp, tcp->u_arg, print_timespec, sprint_timespec);
252         if (entering(tcp)) {
253                 kernel_ulong_t data[2];
254
255                 tprints(", ");
256                 if (!umove_kulong_array_or_printaddr(tcp, tcp->u_arg[5],
257                                                      data, ARRAY_SIZE(data))) {
258                         tprints("{");
259                         /* NB: kernel requires data[1] == NSIG / 8 */
260                         print_sigset_addr_len(tcp, (unsigned long) data[0],
261                                               (unsigned long) data[1]);
262                         tprintf(", %" PRI_klu "}", data[1]);
263                 }
264         }
265
266         return rc;
267 }