blob: f99d25c6877ba64893f65603ee99d2a4ad9c3d0e [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
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>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
Elliott Hughesb7556142018-02-20 17:03:16 -08006 * Copyright (c) 1999-2018 The strace developers.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00007 * 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.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000030 */
31
32#include "defs.h"
Elliott Hughesb7556142018-02-20 17:03:16 -080033#include "xstring.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000034
Dmitry V. Levina0bd3742015-04-07 01:36:50 +000035SYS_FUNC(close)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000036{
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +000037 printfd(tcp, tcp->u_arg[0]);
38
39 return RVAL_DECODED;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000040}
41
Dmitry V. Levina0bd3742015-04-07 01:36:50 +000042SYS_FUNC(dup)
Zubin Mithra64aa1b12014-06-04 08:30:41 +053043{
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +000044 printfd(tcp, tcp->u_arg[0]);
45
46 return RVAL_DECODED | RVAL_FD;
Zubin Mithra64aa1b12014-06-04 08:30:41 +053047}
48
Dmitry V. Levin4371b102008-11-10 22:53:02 +000049static int
50do_dup2(struct tcb *tcp, int flags_arg)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000051{
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +000052 printfd(tcp, tcp->u_arg[0]);
53 tprints(", ");
54 printfd(tcp, tcp->u_arg[1]);
55 if (flags_arg >= 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +020056 tprints(", ");
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +000057 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000058 }
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +000059
60 return RVAL_DECODED | RVAL_FD;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000061}
62
Dmitry V. Levina0bd3742015-04-07 01:36:50 +000063SYS_FUNC(dup2)
Dmitry V. Levin4371b102008-11-10 22:53:02 +000064{
65 return do_dup2(tcp, -1);
66}
67
Dmitry V. Levina0bd3742015-04-07 01:36:50 +000068SYS_FUNC(dup3)
Dmitry V. Levin4371b102008-11-10 22:53:02 +000069{
70 return do_dup2(tcp, 2);
71}
Dmitry V. Levin4371b102008-11-10 22:53:02 +000072
Dmitry V. Levinc2982b52013-11-05 23:00:22 +000073static int
Elliott Hughesd35df492017-02-15 15:19:05 -080074decode_select(struct tcb *const tcp, const kernel_ulong_t *const args,
75 void (*const print_tv_ts) (struct tcb *, kernel_ulong_t),
76 const char * (*const sprint_tv_ts) (struct tcb *, kernel_ulong_t))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000077{
Denys Vlasenkoad5155a2011-09-01 18:18:04 +020078 int i, j;
Denys Vlasenko1f65c3c2013-11-05 16:20:16 +010079 int nfds, fdsize;
Dmitry V. Levin6522f132014-09-09 22:42:12 +000080 fd_set *fds = NULL;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +000081 const char *sep;
Elliott Hughesd35df492017-02-15 15:19:05 -080082 kernel_ulong_t addr;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000083
Elliott Hughesd35df492017-02-15 15:19:05 -080084 /* Kernel truncates args[0] to int, we do the same. */
Dmitry V. Levinf3696b32013-11-05 22:46:43 +000085 nfds = (int) args[0];
86
87 /* Kernel rejects negative nfds, so we don't parse it either. */
Dmitry V. Levin6522f132014-09-09 22:42:12 +000088 if (nfds < 0)
Dmitry V. Levinf3696b32013-11-05 22:46:43 +000089 nfds = 0;
Dmitry V. Levin6522f132014-09-09 22:42:12 +000090
Denys Vlasenko79a79ea2011-09-01 16:35:44 +020091 /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
Dmitry V. Levinf3696b32013-11-05 22:46:43 +000092 if (nfds > 1024*1024)
93 nfds = 1024*1024;
94
95 /*
96 * We had bugs a-la "while (j < args[0])" and "umoven(args[0])" below.
Dr. David Alan Gilbert025f1082013-11-05 11:54:51 +010097 * Instead of args[0], use nfds for fd count, fdsize for array lengths.
98 */
Denys Vlasenkob338f2d2013-11-09 20:40:31 +010099 fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
Denys Vlasenko79a79ea2011-09-01 16:35:44 +0200100
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000101 if (entering(tcp)) {
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000102 tprintf("%d", (int) args[0]);
103
Dmitry V. Levin3e9d71f2015-05-25 20:41:02 +0000104 if (verbose(tcp) && fdsize > 0)
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000105 fds = malloc(fdsize);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000106 for (i = 0; i < 3; i++) {
Elliott Hughesd35df492017-02-15 15:19:05 -0800107 addr = args[i+1];
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000108 tprints(", ");
Dmitry V. Levin2fc5d802015-01-28 01:26:04 +0000109 if (!fds) {
Elliott Hughesd35df492017-02-15 15:19:05 -0800110 printaddr(addr);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000111 continue;
112 }
Elliott Hughesd35df492017-02-15 15:19:05 -0800113 if (umoven_or_printaddr(tcp, addr, fdsize, fds))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000114 continue;
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000115 tprints("[");
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100116 for (j = 0, sep = "";; j++) {
117 j = next_set_bit(fds, j, nfds);
118 if (j < 0)
119 break;
120 tprints(sep);
121 printfd(tcp, j);
122 sep = " ";
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000123 }
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200124 tprints("]");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000125 }
Roland McGrathe85aaa42005-02-06 01:55:12 +0000126 free(fds);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200127 tprints(", ");
Dmitry V. Levin4cb5ccc2015-09-18 18:02:50 +0000128 print_tv_ts(tcp, args[4]);
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +0000129 } else {
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200130 static char outstr[1024];
131 char *outptr;
132#define end_outstr (outstr + sizeof(outstr))
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000133 int ready_fds;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000134
135 if (syserror(tcp))
136 return 0;
137
Dr. David Alan Gilbert025f1082013-11-05 11:54:51 +0100138 ready_fds = tcp->u_rval;
139 if (ready_fds == 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000140 tcp->auxstr = "Timeout";
141 return RVAL_STR;
142 }
Roland McGrathe85aaa42005-02-06 01:55:12 +0000143
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000144 fds = malloc(fdsize);
Roland McGrathe85aaa42005-02-06 01:55:12 +0000145
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200146 outptr = outstr;
147 sep = "";
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000148 for (i = 0; i < 3 && ready_fds > 0; i++) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000149 int first = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000150
Elliott Hughesd35df492017-02-15 15:19:05 -0800151 addr = args[i+1];
152 if (!addr || !fds || umoven(tcp, addr, fdsize, fds) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000153 continue;
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100154 for (j = 0;; j++) {
155 j = next_set_bit(fds, j, nfds);
156 if (j < 0)
157 break;
158 /* +2 chars needed at the end: ']',NUL */
159 if (outptr < end_outstr - (sizeof(", except [") + sizeof(int)*3 + 2)) {
160 if (first) {
Elliott Hughesb7556142018-02-20 17:03:16 -0800161 outptr = xappendstr(outstr,
162 outptr,
163 "%s%s [%u",
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100164 sep,
165 i == 0 ? "in" : i == 1 ? "out" : "except",
166 j
167 );
168 first = 0;
169 sep = ", ";
Elliott Hughesdc75b012017-07-05 13:54:44 -0700170 } else {
Elliott Hughesb7556142018-02-20 17:03:16 -0800171 outptr = xappendstr(outstr,
172 outptr,
173 " %u", j);
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100174 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000175 }
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100176 if (--ready_fds == 0)
177 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000178 }
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200179 if (outptr != outstr)
180 *outptr++ = ']';
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000181 }
Roland McGrathe85aaa42005-02-06 01:55:12 +0000182 free(fds);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000183 /* This contains no useful information on SunOS. */
184 if (args[4]) {
Dmitry V. Levin4cb5ccc2015-09-18 18:02:50 +0000185 const char *str = sprint_tv_ts(tcp, args[4]);
186 if (outptr + sizeof("left ") + strlen(sep) + strlen(str) < end_outstr) {
Elliott Hughesb7556142018-02-20 17:03:16 -0800187 outptr = xappendstr(outstr, outptr,
188 "%sleft %s", sep, str);
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200189 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000190 }
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200191 *outptr = '\0';
Denys Vlasenko11617252011-09-01 11:27:37 +0200192 tcp->auxstr = outstr;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000193 return RVAL_STR;
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200194#undef end_outstr
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000195 }
196 return 0;
197}
198
Elliott Hughesb7556142018-02-20 17:03:16 -0800199#if HAVE_ARCH_OLD_SELECT
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000200SYS_FUNC(oldselect)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000201{
Elliott Hughesb7556142018-02-20 17:03:16 -0800202 kernel_ulong_t *args =
203 fetch_indirect_syscall_args(tcp, tcp->u_arg[0], 5);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000204
Elliott Hughesb7556142018-02-20 17:03:16 -0800205 if (args) {
206 return decode_select(tcp, args, print_timeval, sprint_timeval);
Elliott Hughesd35df492017-02-15 15:19:05 -0800207 } else {
Elliott Hughesb7556142018-02-20 17:03:16 -0800208 if (entering(tcp))
209 printaddr(tcp->u_arg[0]);
210 return RVAL_DECODED;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000211 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000212}
Elliott Hughesb7556142018-02-20 17:03:16 -0800213#endif /* HAVE_ARCH_OLD_SELECT */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000214
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000215#ifdef ALPHA
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000216SYS_FUNC(osf_select)
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000217{
Dmitry V. Levin4cb5ccc2015-09-18 18:02:50 +0000218 return decode_select(tcp, tcp->u_arg, print_timeval32, sprint_timeval32);
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000219}
220#endif
221
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000222SYS_FUNC(select)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000223{
Dmitry V. Levin4cb5ccc2015-09-18 18:02:50 +0000224 return decode_select(tcp, tcp->u_arg, print_timeval, sprint_timeval);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000225}
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000226
Dmitry V. Levin4408e852015-11-27 04:37:46 +0000227static int
Elliott Hughesd35df492017-02-15 15:19:05 -0800228umove_kulong_array_or_printaddr(struct tcb *const tcp, const kernel_ulong_t addr,
229 kernel_ulong_t *const ptr, const size_t n)
Dmitry V. Levin4408e852015-11-27 04:37:46 +0000230{
Elliott Hughesd35df492017-02-15 15:19:05 -0800231#ifndef current_klongsize
232 if (current_klongsize < sizeof(*ptr)) {
Dmitry V. Levin4408e852015-11-27 04:37:46 +0000233 uint32_t ptr32[n];
234 int r = umove_or_printaddr(tcp, addr, &ptr32);
235 if (!r) {
236 size_t i;
237
238 for (i = 0; i < n; ++i)
Elliott Hughesd35df492017-02-15 15:19:05 -0800239 ptr[i] = ptr32[i];
Dmitry V. Levin4408e852015-11-27 04:37:46 +0000240 }
241 return r;
242 }
Elliott Hughesd35df492017-02-15 15:19:05 -0800243#endif /* !current_klongsize */
Dmitry V. Levin4408e852015-11-27 04:37:46 +0000244 return umoven_or_printaddr(tcp, addr, n * sizeof(*ptr), ptr);
245}
246
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000247SYS_FUNC(pselect6)
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000248{
Dmitry V. Levin4cb5ccc2015-09-18 18:02:50 +0000249 int rc = decode_select(tcp, tcp->u_arg, print_timespec, sprint_timespec);
Roland McGrathfe10aa72007-11-01 21:52:20 +0000250 if (entering(tcp)) {
Dmitry V. Levin4408e852015-11-27 04:37:46 +0000251 kernel_ulong_t data[2];
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000252
253 tprints(", ");
Dmitry V. Levin4408e852015-11-27 04:37:46 +0000254 if (!umove_kulong_array_or_printaddr(tcp, tcp->u_arg[5],
255 data, ARRAY_SIZE(data))) {
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000256 tprints("{");
Elliott Hughesd35df492017-02-15 15:19:05 -0800257 /* NB: kernel requires data[1] == NSIG_BYTES */
258 print_sigset_addr_len(tcp, data[0], data[1]);
259 tprintf(", %" PRI_klu "}", data[1]);
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000260 }
261 }
Dmitry V. Levin4408e852015-11-27 04:37:46 +0000262
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000263 return rc;
264}