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