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.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
36 printfd(tcp, tcp->u_arg[0]);
43 printfd(tcp, tcp->u_arg[0]);
45 return RVAL_DECODED | RVAL_FD;
49 do_dup2(struct tcb *tcp, int flags_arg)
51 printfd(tcp, tcp->u_arg[0]);
53 printfd(tcp, tcp->u_arg[1]);
56 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
59 return RVAL_DECODED | RVAL_FD;
64 return do_dup2(tcp, -1);
69 return do_dup2(tcp, 2);
73 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))
83 /* Kernel truncates args[0] to int, we do the same. */
86 /* Kernel rejects negative nfds, so we don't parse it either. */
90 /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
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.
98 fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
101 tprintf("%d", (int) args[0]);
103 if (verbose(tcp) && fdsize > 0)
104 fds = malloc(fdsize);
105 for (i = 0; i < 3; i++) {
112 if (umoven_or_printaddr(tcp, addr, fdsize, fds))
115 for (j = 0, sep = "";; j++) {
116 j = next_set_bit(fds, j, nfds);
127 print_tv_ts(tcp, args[4]);
129 static char outstr[1024];
131 #define end_outstr (outstr + sizeof(outstr))
137 ready_fds = tcp->u_rval;
138 if (ready_fds == 0) {
139 tcp->auxstr = "Timeout";
143 fds = malloc(fdsize);
147 for (i = 0; i < 3 && ready_fds > 0; i++) {
151 if (!addr || !fds || umoven(tcp, addr, fdsize, fds) < 0)
154 j = next_set_bit(fds, j, nfds);
157 /* +2 chars needed at the end: ']',NUL */
158 if (outptr < end_outstr - (sizeof(", except [") + sizeof(int)*3 + 2)) {
160 outptr += sprintf(outptr, "%s%s [%u",
162 i == 0 ? "in" : i == 1 ? "out" : "except",
168 outptr += sprintf(outptr, " %u", j);
171 if (--ready_fds == 0)
174 if (outptr != outstr)
178 /* This contains no useful information on SunOS. */
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);
186 tcp->auxstr = outstr;
195 kernel_ulong_t select_args[5];
196 unsigned int oldselect_args[5];
198 if (sizeof(*select_args) == sizeof(*oldselect_args)) {
199 if (umove_or_printaddr(tcp, tcp->u_arg[0], &select_args)) {
205 if (umove_or_printaddr(tcp, tcp->u_arg[0], &oldselect_args)) {
209 for (i = 0; i < 5; ++i) {
210 select_args[i] = oldselect_args[i];
214 return decode_select(tcp, select_args, print_timeval, sprint_timeval);
220 return decode_select(tcp, tcp->u_arg, print_timeval32, sprint_timeval32);
226 return decode_select(tcp, tcp->u_arg, print_timeval, sprint_timeval);
230 umove_kulong_array_or_printaddr(struct tcb *const tcp, const kernel_ulong_t addr,
231 kernel_ulong_t *const ptr, const size_t n)
233 #ifndef current_klongsize
234 if (current_klongsize < sizeof(*ptr)) {
236 int r = umove_or_printaddr(tcp, addr, &ptr32);
240 for (i = 0; i < n; ++i)
245 #endif /* !current_klongsize */
246 return umoven_or_printaddr(tcp, addr, n * sizeof(*ptr), ptr);
251 int rc = decode_select(tcp, tcp->u_arg, print_timespec, sprint_timespec);
253 kernel_ulong_t data[2];
256 if (!umove_kulong_array_or_printaddr(tcp, tcp->u_arg[5],
257 data, ARRAY_SIZE(data))) {
259 /* NB: kernel requires data[1] == NSIG_BYTES */
260 print_sigset_addr_len(tcp, data[0], data[1]);
261 tprintf(", %" PRI_klu "}", data[1]);