blob: 4695db5830d7dee5fd4a984da8f940f6e23b4024 [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>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00006 * 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.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000029 */
30
31#include "defs.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000032
Dmitry V. Levina0bd3742015-04-07 01:36:50 +000033SYS_FUNC(close)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000034{
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +000035 printfd(tcp, tcp->u_arg[0]);
36
37 return RVAL_DECODED;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000038}
39
Dmitry V. Levina0bd3742015-04-07 01:36:50 +000040SYS_FUNC(dup)
Zubin Mithra64aa1b12014-06-04 08:30:41 +053041{
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +000042 printfd(tcp, tcp->u_arg[0]);
43
44 return RVAL_DECODED | RVAL_FD;
Zubin Mithra64aa1b12014-06-04 08:30:41 +053045}
46
Dmitry V. Levin4371b102008-11-10 22:53:02 +000047static int
48do_dup2(struct tcb *tcp, int flags_arg)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000049{
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +000050 printfd(tcp, tcp->u_arg[0]);
51 tprints(", ");
52 printfd(tcp, tcp->u_arg[1]);
53 if (flags_arg >= 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +020054 tprints(", ");
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +000055 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000056 }
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +000057
58 return RVAL_DECODED | RVAL_FD;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000059}
60
Dmitry V. Levina0bd3742015-04-07 01:36:50 +000061SYS_FUNC(dup2)
Dmitry V. Levin4371b102008-11-10 22:53:02 +000062{
63 return do_dup2(tcp, -1);
64}
65
Dmitry V. Levina0bd3742015-04-07 01:36:50 +000066SYS_FUNC(dup3)
Dmitry V. Levin4371b102008-11-10 22:53:02 +000067{
68 return do_dup2(tcp, 2);
69}
Dmitry V. Levin4371b102008-11-10 22:53:02 +000070
Dmitry V. Levinc2982b52013-11-05 23:00:22 +000071static int
Dmitry V. Levin4cb5ccc2015-09-18 18:02:50 +000072decode_select(struct tcb *tcp, long *args,
73 void (*print_tv_ts) (struct tcb *, const long),
74 const char * (*sprint_tv_ts) (struct tcb *, const long))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000075{
Denys Vlasenkoad5155a2011-09-01 18:18:04 +020076 int i, j;
Denys Vlasenko1f65c3c2013-11-05 16:20:16 +010077 int nfds, fdsize;
Dmitry V. Levin6522f132014-09-09 22:42:12 +000078 fd_set *fds = NULL;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +000079 const char *sep;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000080 long arg;
81
Dmitry V. Levinf3696b32013-11-05 22:46:43 +000082 /* 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. */
Dmitry V. Levin6522f132014-09-09 22:42:12 +000086 if (nfds < 0)
Dmitry V. Levinf3696b32013-11-05 22:46:43 +000087 nfds = 0;
Dmitry V. Levin6522f132014-09-09 22:42:12 +000088
Denys Vlasenko79a79ea2011-09-01 16:35:44 +020089 /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
Dmitry V. Levinf3696b32013-11-05 22:46:43 +000090 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.
Dr. David Alan Gilbert025f1082013-11-05 11:54:51 +010095 * Instead of args[0], use nfds for fd count, fdsize for array lengths.
96 */
Denys Vlasenkob338f2d2013-11-09 20:40:31 +010097 fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
Denys Vlasenko79a79ea2011-09-01 16:35:44 +020098
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000099 if (entering(tcp)) {
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000100 tprintf("%d", (int) args[0]);
101
Dmitry V. Levin3e9d71f2015-05-25 20:41:02 +0000102 if (verbose(tcp) && fdsize > 0)
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000103 fds = malloc(fdsize);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000104 for (i = 0; i < 3; i++) {
105 arg = args[i+1];
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000106 tprints(", ");
Dmitry V. Levin2fc5d802015-01-28 01:26:04 +0000107 if (!fds) {
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000108 printaddr(arg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000109 continue;
110 }
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000111 if (umoven_or_printaddr(tcp, arg, fdsize, fds))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000112 continue;
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000113 tprints("[");
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100114 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 = " ";
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000121 }
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200122 tprints("]");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000123 }
Roland McGrathe85aaa42005-02-06 01:55:12 +0000124 free(fds);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200125 tprints(", ");
Dmitry V. Levin4cb5ccc2015-09-18 18:02:50 +0000126 print_tv_ts(tcp, args[4]);
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +0000127 } else {
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200128 static char outstr[1024];
129 char *outptr;
130#define end_outstr (outstr + sizeof(outstr))
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000131 int ready_fds;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000132
133 if (syserror(tcp))
134 return 0;
135
Dr. David Alan Gilbert025f1082013-11-05 11:54:51 +0100136 ready_fds = tcp->u_rval;
137 if (ready_fds == 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000138 tcp->auxstr = "Timeout";
139 return RVAL_STR;
140 }
Roland McGrathe85aaa42005-02-06 01:55:12 +0000141
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000142 fds = malloc(fdsize);
Roland McGrathe85aaa42005-02-06 01:55:12 +0000143
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200144 outptr = outstr;
145 sep = "";
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000146 for (i = 0; i < 3 && ready_fds > 0; i++) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000147 int first = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000148
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000149 arg = args[i+1];
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000150 if (!arg || !fds || umoven(tcp, arg, fdsize, fds) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000151 continue;
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100152 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 = ", ";
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000166 }
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100167 else {
168 outptr += sprintf(outptr, " %u", j);
169 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000170 }
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100171 if (--ready_fds == 0)
172 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000173 }
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200174 if (outptr != outstr)
175 *outptr++ = ']';
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000176 }
Roland McGrathe85aaa42005-02-06 01:55:12 +0000177 free(fds);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000178 /* This contains no useful information on SunOS. */
179 if (args[4]) {
Dmitry V. Levin4cb5ccc2015-09-18 18:02:50 +0000180 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);
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200183 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000184 }
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200185 *outptr = '\0';
Denys Vlasenko11617252011-09-01 11:27:37 +0200186 tcp->auxstr = outstr;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000187 return RVAL_STR;
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200188#undef end_outstr
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000189 }
190 return 0;
191}
192
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000193SYS_FUNC(oldselect)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000194{
Elvira Khabirovac44df3e2015-07-29 21:38:54 +0300195 long long_args[5];
196#undef oldselect_args
197#if SIZEOF_LONG == 4
198# define oldselect_args long_args
199#else
200 unsigned int oldselect_args[5];
201 unsigned int i;
202#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000203
Elvira Khabirovac44df3e2015-07-29 21:38:54 +0300204 if (umove(tcp, tcp->u_arg[0], &oldselect_args) < 0) {
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000205 printaddr(tcp->u_arg[0]);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000206 return 0;
207 }
Elvira Khabirovac44df3e2015-07-29 21:38:54 +0300208#ifndef oldselect_args
209 for (i = 0; i < 5; i++) {
210 long_args[i] = oldselect_args[i];
211 }
212#endif
Dmitry V. Levin4cb5ccc2015-09-18 18:02:50 +0000213 return decode_select(tcp, long_args, print_timeval, sprint_timeval);
Elvira Khabirovac44df3e2015-07-29 21:38:54 +0300214#undef oldselect_args
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000215}
216
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000217#ifdef ALPHA
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000218SYS_FUNC(osf_select)
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000219{
Dmitry V. Levin4cb5ccc2015-09-18 18:02:50 +0000220 return decode_select(tcp, tcp->u_arg, print_timeval32, sprint_timeval32);
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000221}
222#endif
223
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000224SYS_FUNC(select)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000225{
Dmitry V. Levin4cb5ccc2015-09-18 18:02:50 +0000226 return decode_select(tcp, tcp->u_arg, print_timeval, sprint_timeval);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000227}
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000228
Dmitry V. Levin4408e852015-11-27 04:37:46 +0000229#include "kernel_types.h"
230
231static int
232umove_kulong_array_or_printaddr(struct tcb *tcp, const long addr,
233 kernel_ulong_t *ptr, size_t n)
234{
235#if defined X86_64 || defined X32
236 if (current_personality == 1) {
237#else
238 if (current_wordsize < sizeof(*ptr)) {
239#endif
240 uint32_t ptr32[n];
241 int r = umove_or_printaddr(tcp, addr, &ptr32);
242 if (!r) {
243 size_t i;
244
245 for (i = 0; i < n; ++i)
246 ptr[i] = (kernel_ulong_t) ptr32[i];
247 }
248 return r;
249 }
250 return umoven_or_printaddr(tcp, addr, n * sizeof(*ptr), ptr);
251}
252
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000253SYS_FUNC(pselect6)
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000254{
Dmitry V. Levin4cb5ccc2015-09-18 18:02:50 +0000255 int rc = decode_select(tcp, tcp->u_arg, print_timespec, sprint_timespec);
Roland McGrathfe10aa72007-11-01 21:52:20 +0000256 if (entering(tcp)) {
Dmitry V. Levin4408e852015-11-27 04:37:46 +0000257 kernel_ulong_t data[2];
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000258
259 tprints(", ");
Dmitry V. Levin4408e852015-11-27 04:37:46 +0000260 if (!umove_kulong_array_or_printaddr(tcp, tcp->u_arg[5],
261 data, ARRAY_SIZE(data))) {
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000262 tprints("{");
Dmitry V. Levinb172a942015-09-15 02:17:32 +0000263 /* NB: kernel requires data[1] == NSIG / 8 */
Dmitry V. Levin4408e852015-11-27 04:37:46 +0000264 print_sigset_addr_len(tcp, (unsigned long) data[0],
265 (unsigned long) data[1]);
Dmitry V. Levine67c8e42015-12-16 00:07:16 +0000266 tprintf(", %llu}", (unsigned long long) data[1]);
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000267 }
268 }
Dmitry V. Levin4408e852015-11-27 04:37:46 +0000269
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000270 return rc;
271}