blob: ea9f3bd598029f4672c068d5b3195e604c956ca8 [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#include <fcntl.h>
33#include <sys/file.h>
34
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +000035#include "xlat/fcntlcmds.h"
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +000036#include "xlat/fdflags.h"
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +000037#include "xlat/flockcmds.h"
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +000038#include "xlat/lockfcmds.h"
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +000039#include "xlat/notifyflags.h"
Ben Noordhuis88eafd82013-02-04 00:04:57 +010040
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000041/*
42 * Assume that F_SETLK64, F_SETLKW64, and F_GETLK64 are either defined
43 * or not defined altogether.
44 */
Dmitry V. Levin594eb8f2013-11-12 21:49:03 +000045#if defined(F_SETLK64) && F_SETLK64 + 0 != F_SETLK
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000046# define USE_PRINTFLOCK64 1
Dmitry V. Levin594eb8f2013-11-12 21:49:03 +000047#else
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000048# define USE_PRINTFLOCK64 0
Dmitry V. Levin594eb8f2013-11-12 21:49:03 +000049#endif
50
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000051#if USE_PRINTFLOCK64
Dmitry V. Levin594eb8f2013-11-12 21:49:03 +000052
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000053# ifndef HAVE_STRUCT_FLOCK64
Dmitry V. Levin594eb8f2013-11-12 21:49:03 +000054struct flock64 {
55 short int l_type, l_whence;
56 int64_t l_start, l_len;
57 int l_pid;
58};
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000059# endif
Dmitry V. Levin594eb8f2013-11-12 21:49:03 +000060
Dmitry V. Levin0eeda2c2013-05-01 16:37:08 +000061static void
62printflock64(struct tcb *tcp, long addr, int getlk)
63{
64 struct flock64 fl;
65
Dmitry V. Levine6019fd2015-07-20 16:44:51 +000066 if (umove_or_printaddr(tcp, addr, &fl))
Dmitry V. Levin0eeda2c2013-05-01 16:37:08 +000067 return;
Dmitry V. Levin0eeda2c2013-05-01 16:37:08 +000068 tprints("{type=");
69 printxval(lockfcmds, fl.l_type, "F_???");
70 tprints(", whence=");
71 printxval(whence_codes, fl.l_whence, "SEEK_???");
72 tprintf(", start=%lld, len=%lld", (long long) fl.l_start, (long long) fl.l_len);
73 if (getlk)
74 tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
75 else
76 tprints("}");
77}
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000078#endif /* USE_PRINTFLOCK64 */
Dmitry V. Levin0eeda2c2013-05-01 16:37:08 +000079
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000080static void
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +000081printflock(struct tcb *tcp, long addr, int getlk)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000082{
83 struct flock fl;
84
Denys Vlasenko7a862d72009-04-15 13:22:59 +000085#if SUPPORTED_PERSONALITIES > 1
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000086 if (
87# if SIZEOF_OFF_T > SIZEOF_LONG
Dmitry V. Levine6019fd2015-07-20 16:44:51 +000088 current_personality != DEFAULT_PERSONALITY &&
89# endif
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000090 current_wordsize != sizeof(fl.l_start)) {
Denys Vlasenko9fd4f962012-03-19 09:36:42 +010091 if (current_wordsize == 4) {
Denys Vlasenko7a862d72009-04-15 13:22:59 +000092 /* 32-bit x86 app on x86_64 and similar cases */
93 struct {
94 short int l_type;
95 short int l_whence;
96 int32_t l_start; /* off_t */
97 int32_t l_len; /* off_t */
98 int32_t l_pid; /* pid_t */
99 } fl32;
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000100 if (umove_or_printaddr(tcp, addr, &fl32))
101 return;
102 fl.l_type = fl32.l_type;
103 fl.l_whence = fl32.l_whence;
104 fl.l_start = fl32.l_start;
105 fl.l_len = fl32.l_len;
106 fl.l_pid = fl32.l_pid;
Denys Vlasenko7a862d72009-04-15 13:22:59 +0000107 } else {
108 /* let people know we have a problem here */
Denys Vlasenko751acb32013-02-08 15:34:46 +0100109 tprintf("<decode error: unsupported wordsize %d>",
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100110 current_wordsize);
Denys Vlasenko7a862d72009-04-15 13:22:59 +0000111 return;
112 }
113 } else
114#endif
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000115 if (umove_or_printaddr(tcp, addr, &fl))
Dmitry V. Levin54cabef2014-03-03 23:09:47 +0000116 return;
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200117 tprints("{type=");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000118 printxval(lockfcmds, fl.l_type, "F_???");
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200119 tprints(", whence=");
Denys Vlasenko86738a22013-02-17 14:31:55 +0100120 printxval(whence_codes, fl.l_whence, "SEEK_???");
Dmitry V. Levin54cabef2014-03-03 23:09:47 +0000121#if SIZEOF_OFF_T > SIZEOF_LONG
Dmitry V. Levin0eeda2c2013-05-01 16:37:08 +0000122 tprintf(", start=%lld, len=%lld", fl.l_start, fl.l_len);
123#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000124 tprintf(", start=%ld, len=%ld", fl.l_start, fl.l_len);
John Hughesbdf48f52001-03-06 15:08:09 +0000125#endif
Dmitry V. Levin0eeda2c2013-05-01 16:37:08 +0000126 if (getlk)
127 tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
128 else
129 tprints("}");
130}
John Hughesbdf48f52001-03-06 15:08:09 +0000131
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000132SYS_FUNC(fcntl)
Dmitry V. Levin9b5b67e2007-01-11 23:19:55 +0000133{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000134 if (entering(tcp)) {
Dmitry V. Levin31382132011-03-04 05:08:02 +0300135 printfd(tcp, tcp->u_arg[0]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200136 tprints(", ");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000137 printxval(fcntlcmds, tcp->u_arg[1], "F_???");
138 switch (tcp->u_arg[1]) {
139 case F_SETFD:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200140 tprints(", ");
Roland McGrathb2dee132005-06-01 19:02:36 +0000141 printflags(fdflags, tcp->u_arg[2], "FD_???");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000142 break;
143 case F_SETOWN: case F_DUPFD:
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000144#ifdef F_DUPFD_CLOEXEC
145 case F_DUPFD_CLOEXEC:
146#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000147 tprintf(", %ld", tcp->u_arg[2]);
148 break;
149 case F_SETFL:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200150 tprints(", ");
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000151 tprint_open_modes(tcp->u_arg[2]);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000152 break;
153 case F_SETLK: case F_SETLKW:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200154 tprints(", ");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000155 printflock(tcp, tcp->u_arg[2], 0);
156 break;
Dmitry V. Levin54cabef2014-03-03 23:09:47 +0000157#if USE_PRINTFLOCK64
158 case F_SETLK64: case F_SETLKW64:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200159 tprints(", ");
John Hughesbdf48f52001-03-06 15:08:09 +0000160 printflock64(tcp, tcp->u_arg[2], 0);
161 break;
Dmitry V. Levin54cabef2014-03-03 23:09:47 +0000162#endif /* USE_PRINTFLOCK64 */
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000163#ifdef F_NOTIFY
164 case F_NOTIFY:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200165 tprints(", ");
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000166 printflags(notifyflags, tcp->u_arg[2], "DN_???");
167 break;
168#endif
169#ifdef F_SETLEASE
170 case F_SETLEASE:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200171 tprints(", ");
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000172 printxval(lockfcmds, tcp->u_arg[2], "F_???");
173 break;
174#endif
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000175 }
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +0000176 } else {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000177 switch (tcp->u_arg[1]) {
178 case F_DUPFD:
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000179#ifdef F_DUPFD_CLOEXEC
180 case F_DUPFD_CLOEXEC:
181#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000182 case F_SETFD: case F_SETFL:
183 case F_SETLK: case F_SETLKW:
184 case F_SETOWN: case F_GETOWN:
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000185#ifdef F_NOTIFY
186 case F_NOTIFY:
187#endif
188#ifdef F_SETLEASE
189 case F_SETLEASE:
190#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000191 break;
192 case F_GETFD:
Dmitry V. Levin21a75342008-09-03 01:22:18 +0000193 if (syserror(tcp) || tcp->u_rval == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000194 return 0;
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000195 tcp->auxstr = sprintflags("flags ", fdflags, tcp->u_rval);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000196 return RVAL_HEX|RVAL_STR;
197 case F_GETFL:
Dmitry V. Levin21a75342008-09-03 01:22:18 +0000198 if (syserror(tcp))
199 return 0;
Dmitry V. Levin9b5b67e2007-01-11 23:19:55 +0000200 tcp->auxstr = sprint_open_modes(tcp->u_rval);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000201 return RVAL_HEX|RVAL_STR;
202 case F_GETLK:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200203 tprints(", ");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000204 printflock(tcp, tcp->u_arg[2], 1);
205 break;
Dmitry V. Levin54cabef2014-03-03 23:09:47 +0000206#if USE_PRINTFLOCK64
John Hughesbdf48f52001-03-06 15:08:09 +0000207 case F_GETLK64:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200208 tprints(", ");
John Hughesbdf48f52001-03-06 15:08:09 +0000209 printflock64(tcp, tcp->u_arg[2], 1);
210 break;
211#endif
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000212#ifdef F_GETLEASE
213 case F_GETLEASE:
214 if (syserror(tcp))
215 return 0;
216 tcp->auxstr = xlookup(lockfcmds, tcp->u_rval);
217 return RVAL_HEX|RVAL_STR;
218#endif
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000219 default:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000220 tprintf(", %#lx", tcp->u_arg[2]);
221 break;
222 }
223 }
224 return 0;
225}
226
227#ifdef LOCK_SH
228
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000229SYS_FUNC(flock)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000230{
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +0000231 printfd(tcp, tcp->u_arg[0]);
232 tprints(", ");
233 printflags(flockcmds, tcp->u_arg[1], "LOCK_???");
234
235 return RVAL_DECODED;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000236}
237#endif /* LOCK_SH */
238
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000239SYS_FUNC(close)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000240{
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +0000241 printfd(tcp, tcp->u_arg[0]);
242
243 return RVAL_DECODED;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000244}
245
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000246SYS_FUNC(dup)
Zubin Mithra64aa1b12014-06-04 08:30:41 +0530247{
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +0000248 printfd(tcp, tcp->u_arg[0]);
249
250 return RVAL_DECODED | RVAL_FD;
Zubin Mithra64aa1b12014-06-04 08:30:41 +0530251}
252
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000253static int
254do_dup2(struct tcb *tcp, int flags_arg)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000255{
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +0000256 printfd(tcp, tcp->u_arg[0]);
257 tprints(", ");
258 printfd(tcp, tcp->u_arg[1]);
259 if (flags_arg >= 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200260 tprints(", ");
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +0000261 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000262 }
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +0000263
264 return RVAL_DECODED | RVAL_FD;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000265}
266
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000267SYS_FUNC(dup2)
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000268{
269 return do_dup2(tcp, -1);
270}
271
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000272SYS_FUNC(dup3)
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000273{
274 return do_dup2(tcp, 2);
275}
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000276
Denys Vlasenko84703742012-02-25 02:38:52 +0100277#if defined(ALPHA)
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000278SYS_FUNC(getdtablesize)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000279{
280 return 0;
281}
Denys Vlasenko84703742012-02-25 02:38:52 +0100282#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000283
Dmitry V. Levinc2982b52013-11-05 23:00:22 +0000284static int
Dmitry V. Levina7945a32006-12-13 17:10:11 +0000285decode_select(struct tcb *tcp, long *args, enum bitness_t bitness)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000286{
Denys Vlasenkoad5155a2011-09-01 18:18:04 +0200287 int i, j;
Denys Vlasenko1f65c3c2013-11-05 16:20:16 +0100288 int nfds, fdsize;
Dmitry V. Levin6522f132014-09-09 22:42:12 +0000289 fd_set *fds = NULL;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000290 const char *sep;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000291 long arg;
292
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000293 /* Kernel truncates arg[0] to int, we do the same. */
294 nfds = (int) args[0];
295
296 /* Kernel rejects negative nfds, so we don't parse it either. */
Dmitry V. Levin6522f132014-09-09 22:42:12 +0000297 if (nfds < 0)
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000298 nfds = 0;
Dmitry V. Levin6522f132014-09-09 22:42:12 +0000299
Denys Vlasenko79a79ea2011-09-01 16:35:44 +0200300 /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000301 if (nfds > 1024*1024)
302 nfds = 1024*1024;
303
304 /*
305 * We had bugs a-la "while (j < args[0])" and "umoven(args[0])" below.
Dr. David Alan Gilbert025f1082013-11-05 11:54:51 +0100306 * Instead of args[0], use nfds for fd count, fdsize for array lengths.
307 */
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100308 fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
Denys Vlasenko79a79ea2011-09-01 16:35:44 +0200309
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000310 if (entering(tcp)) {
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000311 tprintf("%d", (int) args[0]);
312
Dmitry V. Levin3e9d71f2015-05-25 20:41:02 +0000313 if (verbose(tcp) && fdsize > 0)
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000314 fds = malloc(fdsize);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000315 for (i = 0; i < 3; i++) {
316 arg = args[i+1];
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000317 tprints(", ");
Dmitry V. Levin2fc5d802015-01-28 01:26:04 +0000318 if (!fds) {
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000319 printaddr(arg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000320 continue;
321 }
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000322 if (umoven_or_printaddr(tcp, arg, fdsize, fds))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000323 continue;
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000324 tprints("[");
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100325 for (j = 0, sep = "";; j++) {
326 j = next_set_bit(fds, j, nfds);
327 if (j < 0)
328 break;
329 tprints(sep);
330 printfd(tcp, j);
331 sep = " ";
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000332 }
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200333 tprints("]");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000334 }
Roland McGrathe85aaa42005-02-06 01:55:12 +0000335 free(fds);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200336 tprints(", ");
Roland McGrath6afc5652007-07-24 01:57:11 +0000337 printtv_bitness(tcp, args[4], bitness, 0);
Dmitry V. Levindf0c18c2015-07-20 16:59:50 +0000338 } else {
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200339 static char outstr[1024];
340 char *outptr;
341#define end_outstr (outstr + sizeof(outstr))
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000342 int ready_fds;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000343
344 if (syserror(tcp))
345 return 0;
346
Dr. David Alan Gilbert025f1082013-11-05 11:54:51 +0100347 ready_fds = tcp->u_rval;
348 if (ready_fds == 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000349 tcp->auxstr = "Timeout";
350 return RVAL_STR;
351 }
Roland McGrathe85aaa42005-02-06 01:55:12 +0000352
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000353 fds = malloc(fdsize);
Roland McGrathe85aaa42005-02-06 01:55:12 +0000354
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200355 outptr = outstr;
356 sep = "";
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000357 for (i = 0; i < 3 && ready_fds > 0; i++) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000358 int first = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000359
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000360 arg = args[i+1];
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000361 if (!arg || !fds || umoven(tcp, arg, fdsize, fds) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000362 continue;
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100363 for (j = 0;; j++) {
364 j = next_set_bit(fds, j, nfds);
365 if (j < 0)
366 break;
367 /* +2 chars needed at the end: ']',NUL */
368 if (outptr < end_outstr - (sizeof(", except [") + sizeof(int)*3 + 2)) {
369 if (first) {
370 outptr += sprintf(outptr, "%s%s [%u",
371 sep,
372 i == 0 ? "in" : i == 1 ? "out" : "except",
373 j
374 );
375 first = 0;
376 sep = ", ";
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000377 }
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100378 else {
379 outptr += sprintf(outptr, " %u", j);
380 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000381 }
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100382 if (--ready_fds == 0)
383 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000384 }
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200385 if (outptr != outstr)
386 *outptr++ = ']';
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000387 }
Roland McGrathe85aaa42005-02-06 01:55:12 +0000388 free(fds);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000389 /* This contains no useful information on SunOS. */
390 if (args[4]) {
Denys Vlasenkoa1d541e2012-01-20 11:04:04 +0100391 if (outptr < end_outstr - (10 + TIMEVAL_TEXT_BUFSIZE)) {
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200392 outptr += sprintf(outptr, "%sleft ", sep);
Denys Vlasenkoa1d541e2012-01-20 11:04:04 +0100393 outptr = sprinttv(outptr, tcp, args[4], bitness, /*special:*/ 0);
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200394 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000395 }
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200396 *outptr = '\0';
Denys Vlasenko11617252011-09-01 11:27:37 +0200397 tcp->auxstr = outstr;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000398 return RVAL_STR;
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200399#undef end_outstr
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000400 }
401 return 0;
402}
403
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000404SYS_FUNC(oldselect)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000405{
Elvira Khabirovac44df3e2015-07-29 21:38:54 +0300406 long long_args[5];
407#undef oldselect_args
408#if SIZEOF_LONG == 4
409# define oldselect_args long_args
410#else
411 unsigned int oldselect_args[5];
412 unsigned int i;
413#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000414
Elvira Khabirovac44df3e2015-07-29 21:38:54 +0300415 if (umove(tcp, tcp->u_arg[0], &oldselect_args) < 0) {
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000416 printaddr(tcp->u_arg[0]);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000417 return 0;
418 }
Elvira Khabirovac44df3e2015-07-29 21:38:54 +0300419#ifndef oldselect_args
420 for (i = 0; i < 5; i++) {
421 long_args[i] = oldselect_args[i];
422 }
423#endif
424 return decode_select(tcp, long_args, BITNESS_CURRENT);
425#undef oldselect_args
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000426}
427
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000428#ifdef ALPHA
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000429SYS_FUNC(osf_select)
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000430{
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000431 return decode_select(tcp, tcp->u_arg, BITNESS_32);
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000432}
433#endif
434
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000435SYS_FUNC(select)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000436{
Dmitry V. Levina7945a32006-12-13 17:10:11 +0000437 return decode_select(tcp, tcp->u_arg, BITNESS_CURRENT);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000438}
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000439
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000440SYS_FUNC(pselect6)
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000441{
Dmitry V. Levina7945a32006-12-13 17:10:11 +0000442 int rc = decode_select(tcp, tcp->u_arg, BITNESS_CURRENT);
Roland McGrathfe10aa72007-11-01 21:52:20 +0000443 if (entering(tcp)) {
Dmitry V. Levinb172a942015-09-15 02:17:32 +0000444 unsigned long data[2];
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000445
446 tprints(", ");
Dmitry V. Levinb172a942015-09-15 02:17:32 +0000447 if (!umove_ulong_array_or_printaddr(tcp, tcp->u_arg[5], data,
448 ARRAY_SIZE(data))) {
Dmitry V. Levine6019fd2015-07-20 16:44:51 +0000449 tprints("{");
Dmitry V. Levinb172a942015-09-15 02:17:32 +0000450 /* NB: kernel requires data[1] == NSIG / 8 */
451 print_sigset_addr_len(tcp, data[0], data[1]);
452 tprintf(", %lu}", data[1]);
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000453 }
454 }
455 return rc;
456}