blob: 5b8869bcf92e426bfd85b2410cf58ff449f2291b [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>
Roland McGrath63d6e542004-10-20 02:17:41 +000034#ifdef HAVE_SYS_EPOLL_H
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +010035# include <sys/epoll.h>
Roland McGrath93817bf2004-10-06 22:23:31 +000036#endif
Ben Noordhuis88eafd82013-02-04 00:04:57 +010037#ifdef HAVE_LINUX_PERF_EVENT_H
38# include <linux/perf_event.h>
39#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000040
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +000041#include "xlat/fcntlcmds.h"
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +000042#include "xlat/fdflags.h"
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +000043#include "xlat/flockcmds.h"
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +000044#include "xlat/lockfcmds.h"
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +000045#include "xlat/notifyflags.h"
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +000046#include "xlat/perf_event_open_flags.h"
Ben Noordhuis88eafd82013-02-04 00:04:57 +010047
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000048/*
49 * Assume that F_SETLK64, F_SETLKW64, and F_GETLK64 are either defined
50 * or not defined altogether.
51 */
Dmitry V. Levin594eb8f2013-11-12 21:49:03 +000052#if defined(F_SETLK64) && F_SETLK64 + 0 != F_SETLK
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000053# define USE_PRINTFLOCK64 1
Dmitry V. Levin594eb8f2013-11-12 21:49:03 +000054#else
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000055# define USE_PRINTFLOCK64 0
Dmitry V. Levin594eb8f2013-11-12 21:49:03 +000056#endif
57
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000058#if USE_PRINTFLOCK64
Dmitry V. Levin594eb8f2013-11-12 21:49:03 +000059
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000060# ifndef HAVE_STRUCT_FLOCK64
Dmitry V. Levin594eb8f2013-11-12 21:49:03 +000061struct flock64 {
62 short int l_type, l_whence;
63 int64_t l_start, l_len;
64 int l_pid;
65};
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000066# endif
Dmitry V. Levin594eb8f2013-11-12 21:49:03 +000067
Dmitry V. Levin0eeda2c2013-05-01 16:37:08 +000068static void
69printflock64(struct tcb *tcp, long addr, int getlk)
70{
71 struct flock64 fl;
72
73 if (umove(tcp, addr, &fl) < 0) {
74 tprints("{...}");
75 return;
76 }
77 tprints("{type=");
78 printxval(lockfcmds, fl.l_type, "F_???");
79 tprints(", whence=");
80 printxval(whence_codes, fl.l_whence, "SEEK_???");
81 tprintf(", start=%lld, len=%lld", (long long) fl.l_start, (long long) fl.l_len);
82 if (getlk)
83 tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
84 else
85 tprints("}");
86}
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000087#endif /* USE_PRINTFLOCK64 */
Dmitry V. Levin0eeda2c2013-05-01 16:37:08 +000088
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000089static void
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +000090printflock(struct tcb *tcp, long addr, int getlk)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000091{
92 struct flock fl;
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000093 int r;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000094
Denys Vlasenko7a862d72009-04-15 13:22:59 +000095#if SUPPORTED_PERSONALITIES > 1
Dmitry V. Levin54cabef2014-03-03 23:09:47 +000096 if (
97# if SIZEOF_OFF_T > SIZEOF_LONG
98 current_personality > 0 &&
99#endif
100 current_wordsize != sizeof(fl.l_start)) {
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100101 if (current_wordsize == 4) {
Denys Vlasenko7a862d72009-04-15 13:22:59 +0000102 /* 32-bit x86 app on x86_64 and similar cases */
103 struct {
104 short int l_type;
105 short int l_whence;
106 int32_t l_start; /* off_t */
107 int32_t l_len; /* off_t */
108 int32_t l_pid; /* pid_t */
109 } fl32;
Dmitry V. Levin54cabef2014-03-03 23:09:47 +0000110 r = umove(tcp, addr, &fl32);
111 if (r >= 0) {
112 fl.l_type = fl32.l_type;
113 fl.l_whence = fl32.l_whence;
114 fl.l_start = fl32.l_start;
115 fl.l_len = fl32.l_len;
116 fl.l_pid = fl32.l_pid;
Denys Vlasenko7a862d72009-04-15 13:22:59 +0000117 }
Denys Vlasenko7a862d72009-04-15 13:22:59 +0000118 } else {
119 /* let people know we have a problem here */
Denys Vlasenko751acb32013-02-08 15:34:46 +0100120 tprintf("<decode error: unsupported wordsize %d>",
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100121 current_wordsize);
Denys Vlasenko7a862d72009-04-15 13:22:59 +0000122 return;
123 }
124 } else
125#endif
126 {
Dmitry V. Levin54cabef2014-03-03 23:09:47 +0000127 r = umove(tcp, addr, &fl);
128 }
129 if (r < 0) {
130 tprints("{...}");
131 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000132 }
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200133 tprints("{type=");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000134 printxval(lockfcmds, fl.l_type, "F_???");
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200135 tprints(", whence=");
Denys Vlasenko86738a22013-02-17 14:31:55 +0100136 printxval(whence_codes, fl.l_whence, "SEEK_???");
Dmitry V. Levin54cabef2014-03-03 23:09:47 +0000137#if SIZEOF_OFF_T > SIZEOF_LONG
Dmitry V. Levin0eeda2c2013-05-01 16:37:08 +0000138 tprintf(", start=%lld, len=%lld", fl.l_start, fl.l_len);
139#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000140 tprintf(", start=%ld, len=%ld", fl.l_start, fl.l_len);
John Hughesbdf48f52001-03-06 15:08:09 +0000141#endif
Dmitry V. Levin0eeda2c2013-05-01 16:37:08 +0000142 if (getlk)
143 tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
144 else
145 tprints("}");
146}
John Hughesbdf48f52001-03-06 15:08:09 +0000147
Dmitry V. Levin9b5b67e2007-01-11 23:19:55 +0000148int
149sys_fcntl(struct tcb *tcp)
150{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000151 if (entering(tcp)) {
Dmitry V. Levin31382132011-03-04 05:08:02 +0300152 printfd(tcp, tcp->u_arg[0]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200153 tprints(", ");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000154 printxval(fcntlcmds, tcp->u_arg[1], "F_???");
155 switch (tcp->u_arg[1]) {
156 case F_SETFD:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200157 tprints(", ");
Roland McGrathb2dee132005-06-01 19:02:36 +0000158 printflags(fdflags, tcp->u_arg[2], "FD_???");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000159 break;
160 case F_SETOWN: case F_DUPFD:
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000161#ifdef F_DUPFD_CLOEXEC
162 case F_DUPFD_CLOEXEC:
163#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000164 tprintf(", %ld", tcp->u_arg[2]);
165 break;
166 case F_SETFL:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200167 tprints(", ");
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000168 tprint_open_modes(tcp->u_arg[2]);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000169 break;
170 case F_SETLK: case F_SETLKW:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200171 tprints(", ");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000172 printflock(tcp, tcp->u_arg[2], 0);
173 break;
Dmitry V. Levin54cabef2014-03-03 23:09:47 +0000174#if USE_PRINTFLOCK64
175 case F_SETLK64: case F_SETLKW64:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200176 tprints(", ");
John Hughesbdf48f52001-03-06 15:08:09 +0000177 printflock64(tcp, tcp->u_arg[2], 0);
178 break;
Dmitry V. Levin54cabef2014-03-03 23:09:47 +0000179#endif /* USE_PRINTFLOCK64 */
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000180#ifdef F_NOTIFY
181 case F_NOTIFY:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200182 tprints(", ");
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000183 printflags(notifyflags, tcp->u_arg[2], "DN_???");
184 break;
185#endif
186#ifdef F_SETLEASE
187 case F_SETLEASE:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200188 tprints(", ");
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000189 printxval(lockfcmds, tcp->u_arg[2], "F_???");
190 break;
191#endif
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000192 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000193 }
194 else {
195 switch (tcp->u_arg[1]) {
196 case F_DUPFD:
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000197#ifdef F_DUPFD_CLOEXEC
198 case F_DUPFD_CLOEXEC:
199#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000200 case F_SETFD: case F_SETFL:
201 case F_SETLK: case F_SETLKW:
202 case F_SETOWN: case F_GETOWN:
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000203#ifdef F_NOTIFY
204 case F_NOTIFY:
205#endif
206#ifdef F_SETLEASE
207 case F_SETLEASE:
208#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000209 break;
210 case F_GETFD:
Dmitry V. Levin21a75342008-09-03 01:22:18 +0000211 if (syserror(tcp) || tcp->u_rval == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000212 return 0;
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000213 tcp->auxstr = sprintflags("flags ", fdflags, tcp->u_rval);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000214 return RVAL_HEX|RVAL_STR;
215 case F_GETFL:
Dmitry V. Levin21a75342008-09-03 01:22:18 +0000216 if (syserror(tcp))
217 return 0;
Dmitry V. Levin9b5b67e2007-01-11 23:19:55 +0000218 tcp->auxstr = sprint_open_modes(tcp->u_rval);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000219 return RVAL_HEX|RVAL_STR;
220 case F_GETLK:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200221 tprints(", ");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000222 printflock(tcp, tcp->u_arg[2], 1);
223 break;
Dmitry V. Levin54cabef2014-03-03 23:09:47 +0000224#if USE_PRINTFLOCK64
John Hughesbdf48f52001-03-06 15:08:09 +0000225 case F_GETLK64:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200226 tprints(", ");
John Hughesbdf48f52001-03-06 15:08:09 +0000227 printflock64(tcp, tcp->u_arg[2], 1);
228 break;
229#endif
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000230#ifdef F_GETLEASE
231 case F_GETLEASE:
232 if (syserror(tcp))
233 return 0;
234 tcp->auxstr = xlookup(lockfcmds, tcp->u_rval);
235 return RVAL_HEX|RVAL_STR;
236#endif
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000237 default:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000238 tprintf(", %#lx", tcp->u_arg[2]);
239 break;
240 }
241 }
242 return 0;
243}
244
245#ifdef LOCK_SH
246
247int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000248sys_flock(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000249{
250 if (entering(tcp)) {
Dmitry V. Levin31382132011-03-04 05:08:02 +0300251 printfd(tcp, tcp->u_arg[0]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200252 tprints(", ");
Roland McGrathb2dee132005-06-01 19:02:36 +0000253 printflags(flockcmds, tcp->u_arg[1], "LOCK_???");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000254 }
255 return 0;
256}
257#endif /* LOCK_SH */
258
259int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000260sys_close(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000261{
262 if (entering(tcp)) {
Dmitry V. Levin31382132011-03-04 05:08:02 +0300263 printfd(tcp, tcp->u_arg[0]);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000264 }
265 return 0;
266}
267
Zubin Mithra64aa1b12014-06-04 08:30:41 +0530268int
269sys_dup(struct tcb *tcp)
270{
271 if (entering(tcp)) {
272 printfd(tcp, tcp->u_arg[0]);
273 }
274 return RVAL_FD;
275}
276
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000277static int
278do_dup2(struct tcb *tcp, int flags_arg)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000279{
280 if (entering(tcp)) {
Dmitry V. Levin31382132011-03-04 05:08:02 +0300281 printfd(tcp, tcp->u_arg[0]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200282 tprints(", ");
Dmitry V. Levin31382132011-03-04 05:08:02 +0300283 printfd(tcp, tcp->u_arg[1]);
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000284 if (flags_arg >= 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200285 tprints(", ");
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000286 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
287 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000288 }
Zubin Mithra64aa1b12014-06-04 08:30:41 +0530289 return RVAL_FD;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000290}
291
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000292int
293sys_dup2(struct tcb *tcp)
294{
295 return do_dup2(tcp, -1);
296}
297
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000298int
299sys_dup3(struct tcb *tcp)
300{
301 return do_dup2(tcp, 2);
302}
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000303
Denys Vlasenko84703742012-02-25 02:38:52 +0100304#if defined(ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000305int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000306sys_getdtablesize(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000307{
308 return 0;
309}
Denys Vlasenko84703742012-02-25 02:38:52 +0100310#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000311
Dmitry V. Levinc2982b52013-11-05 23:00:22 +0000312static int
Dmitry V. Levina7945a32006-12-13 17:10:11 +0000313decode_select(struct tcb *tcp, long *args, enum bitness_t bitness)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000314{
Denys Vlasenkoad5155a2011-09-01 18:18:04 +0200315 int i, j;
Denys Vlasenko1f65c3c2013-11-05 16:20:16 +0100316 int nfds, fdsize;
Roland McGrathe85aaa42005-02-06 01:55:12 +0000317 fd_set *fds;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000318 const char *sep;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000319 long arg;
320
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000321 /* Kernel truncates arg[0] to int, we do the same. */
322 nfds = (int) args[0];
323
324 /* Kernel rejects negative nfds, so we don't parse it either. */
325 if (nfds < 0) {
326 nfds = 0;
327 fds = NULL;
328 }
Denys Vlasenko79a79ea2011-09-01 16:35:44 +0200329 /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000330 if (nfds > 1024*1024)
331 nfds = 1024*1024;
332
333 /*
334 * We had bugs a-la "while (j < args[0])" and "umoven(args[0])" below.
Dr. David Alan Gilbert025f1082013-11-05 11:54:51 +0100335 * Instead of args[0], use nfds for fd count, fdsize for array lengths.
336 */
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100337 fdsize = (((nfds + 7) / 8) + current_wordsize-1) & -current_wordsize;
Denys Vlasenko79a79ea2011-09-01 16:35:44 +0200338
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000339 if (entering(tcp)) {
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000340 tprintf("%d", (int) args[0]);
341
342 if (fdsize > 0) {
343 fds = malloc(fdsize);
344 if (!fds)
345 die_out_of_memory();
346 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000347 for (i = 0; i < 3; i++) {
348 arg = args[i+1];
349 if (arg == 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200350 tprints(", NULL");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000351 continue;
352 }
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000353 if (!verbose(tcp) || !fds) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000354 tprintf(", %#lx", arg);
355 continue;
356 }
Roland McGrathe85aaa42005-02-06 01:55:12 +0000357 if (umoven(tcp, arg, fdsize, (char *) fds) < 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200358 tprints(", [?]");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000359 continue;
360 }
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200361 tprints(", [");
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100362 for (j = 0, sep = "";; j++) {
363 j = next_set_bit(fds, j, nfds);
364 if (j < 0)
365 break;
366 tprints(sep);
367 printfd(tcp, j);
368 sep = " ";
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000369 }
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200370 tprints("]");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000371 }
Roland McGrathe85aaa42005-02-06 01:55:12 +0000372 free(fds);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200373 tprints(", ");
Roland McGrath6afc5652007-07-24 01:57:11 +0000374 printtv_bitness(tcp, args[4], bitness, 0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000375 }
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200376 else {
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200377 static char outstr[1024];
378 char *outptr;
379#define end_outstr (outstr + sizeof(outstr))
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000380 int ready_fds;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000381
382 if (syserror(tcp))
383 return 0;
384
Dr. David Alan Gilbert025f1082013-11-05 11:54:51 +0100385 ready_fds = tcp->u_rval;
386 if (ready_fds == 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000387 tcp->auxstr = "Timeout";
388 return RVAL_STR;
389 }
Roland McGrathe85aaa42005-02-06 01:55:12 +0000390
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200391 fds = malloc(fdsize);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200392 if (!fds)
393 die_out_of_memory();
Roland McGrathe85aaa42005-02-06 01:55:12 +0000394
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200395 outptr = outstr;
396 sep = "";
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000397 for (i = 0; i < 3 && ready_fds > 0; i++) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000398 int first = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000399
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000400 arg = args[i+1];
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200401 if (!arg || umoven(tcp, arg, fdsize, (char *) fds) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000402 continue;
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100403 for (j = 0;; j++) {
404 j = next_set_bit(fds, j, nfds);
405 if (j < 0)
406 break;
407 /* +2 chars needed at the end: ']',NUL */
408 if (outptr < end_outstr - (sizeof(", except [") + sizeof(int)*3 + 2)) {
409 if (first) {
410 outptr += sprintf(outptr, "%s%s [%u",
411 sep,
412 i == 0 ? "in" : i == 1 ? "out" : "except",
413 j
414 );
415 first = 0;
416 sep = ", ";
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000417 }
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100418 else {
419 outptr += sprintf(outptr, " %u", j);
420 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000421 }
Denys Vlasenkob338f2d2013-11-09 20:40:31 +0100422 if (--ready_fds == 0)
423 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000424 }
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200425 if (outptr != outstr)
426 *outptr++ = ']';
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000427 }
Roland McGrathe85aaa42005-02-06 01:55:12 +0000428 free(fds);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000429 /* This contains no useful information on SunOS. */
430 if (args[4]) {
Denys Vlasenkoa1d541e2012-01-20 11:04:04 +0100431 if (outptr < end_outstr - (10 + TIMEVAL_TEXT_BUFSIZE)) {
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200432 outptr += sprintf(outptr, "%sleft ", sep);
Denys Vlasenkoa1d541e2012-01-20 11:04:04 +0100433 outptr = sprinttv(outptr, tcp, args[4], bitness, /*special:*/ 0);
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200434 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000435 }
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200436 *outptr = '\0';
Denys Vlasenko11617252011-09-01 11:27:37 +0200437 tcp->auxstr = outstr;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000438 return RVAL_STR;
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200439#undef end_outstr
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000440 }
441 return 0;
442}
443
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000444int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000445sys_oldselect(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000446{
447 long args[5];
448
449 if (umoven(tcp, tcp->u_arg[0], sizeof args, (char *) args) < 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200450 tprints("[...]");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000451 return 0;
452 }
Dmitry V. Levina7945a32006-12-13 17:10:11 +0000453 return decode_select(tcp, args, BITNESS_CURRENT);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000454}
455
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000456#ifdef ALPHA
Roland McGrathe948faf2002-12-15 23:58:18 +0000457int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000458sys_osf_select(struct tcb *tcp)
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000459{
460 long *args = tcp->u_arg;
Dmitry V. Levina7945a32006-12-13 17:10:11 +0000461 return decode_select(tcp, args, BITNESS_32);
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000462}
463#endif
464
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +0000465#include "xlat/epollctls.h"
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +0000466#include "xlat/epollevents.h"
Dmitry V. Levind35bdca2014-04-26 18:10:19 +0000467#include "xlat/epollflags.h"
Roland McGrath93817bf2004-10-06 22:23:31 +0000468
Denys Vlasenko72879c62012-02-27 14:18:02 +0100469/* Not aliased to printargs_ld: we want it to have a distinct address */
Roland McGrath93817bf2004-10-06 22:23:31 +0000470int
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000471sys_epoll_create(struct tcb *tcp)
Roland McGrath93817bf2004-10-06 22:23:31 +0000472{
Denys Vlasenko72879c62012-02-27 14:18:02 +0100473 return printargs_ld(tcp);
Roland McGrath93817bf2004-10-06 22:23:31 +0000474}
475
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000476int
477sys_epoll_create1(struct tcb *tcp)
478{
479 if (entering(tcp))
Mike Frysingeraed334c2011-10-13 22:33:45 -0400480 printflags(epollflags, tcp->u_arg[0], "EPOLL_???");
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000481 return 0;
482}
483
Roland McGrath63d6e542004-10-20 02:17:41 +0000484#ifdef HAVE_SYS_EPOLL_H
Roland McGrath93817bf2004-10-06 22:23:31 +0000485static void
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000486print_epoll_event(struct epoll_event *ev)
Roland McGrath93817bf2004-10-06 22:23:31 +0000487{
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200488 tprints("{");
Roland McGrathb2dee132005-06-01 19:02:36 +0000489 printflags(epollevents, ev->events, "EPOLL???");
Roland McGrath93817bf2004-10-06 22:23:31 +0000490 /* We cannot know what format the program uses, so print u32 and u64
491 which will cover every value. */
492 tprintf(", {u32=%" PRIu32 ", u64=%" PRIu64 "}}",
493 ev->data.u32, ev->data.u64);
494}
Roland McGrath63d6e542004-10-20 02:17:41 +0000495#endif
Roland McGrath93817bf2004-10-06 22:23:31 +0000496
497int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000498sys_epoll_ctl(struct tcb *tcp)
Roland McGrath93817bf2004-10-06 22:23:31 +0000499{
500 if (entering(tcp)) {
Dmitry V. Levin31382132011-03-04 05:08:02 +0300501 printfd(tcp, tcp->u_arg[0]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200502 tprints(", ");
Denys Vlasenkoadedb512008-12-30 18:47:55 +0000503 printxval(epollctls, tcp->u_arg[1], "EPOLL_CTL_???");
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200504 tprints(", ");
Dmitry V. Levin31382132011-03-04 05:08:02 +0300505 printfd(tcp, tcp->u_arg[2]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200506 tprints(", ");
Roland McGrath93817bf2004-10-06 22:23:31 +0000507 if (tcp->u_arg[3] == 0)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200508 tprints("NULL");
Roland McGrath63d6e542004-10-20 02:17:41 +0000509 else {
510#ifdef HAVE_SYS_EPOLL_H
511 struct epoll_event ev;
Dmitry V. Levine51ce472014-04-17 14:33:59 +0000512 if (
513#ifdef EPOLL_CTL_DEL
514 (tcp->u_arg[1] != EPOLL_CTL_DEL) &&
515#endif
516 umove(tcp, tcp->u_arg[3], &ev) == 0)
Roland McGrath63d6e542004-10-20 02:17:41 +0000517 print_epoll_event(&ev);
518 else
519#endif
Dmitry V. Levine51ce472014-04-17 14:33:59 +0000520 tprintf("%lx", tcp->u_arg[3]);
Roland McGrath63d6e542004-10-20 02:17:41 +0000521 }
Roland McGrath93817bf2004-10-06 22:23:31 +0000522 }
523 return 0;
524}
525
Roland McGrathf2400052007-08-02 01:13:26 +0000526static void
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000527epoll_wait_common(struct tcb *tcp)
Roland McGrath93817bf2004-10-06 22:23:31 +0000528{
Dmitry V. Levin31382132011-03-04 05:08:02 +0300529 if (entering(tcp)) {
530 printfd(tcp, tcp->u_arg[0]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200531 tprints(", ");
Dmitry V. Levin31382132011-03-04 05:08:02 +0300532 } else {
Roland McGrath93817bf2004-10-06 22:23:31 +0000533 if (syserror(tcp))
534 tprintf("%lx", tcp->u_arg[1]);
535 else if (tcp->u_rval == 0)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200536 tprints("{}");
Roland McGrath93817bf2004-10-06 22:23:31 +0000537 else {
Roland McGrath63d6e542004-10-20 02:17:41 +0000538#ifdef HAVE_SYS_EPOLL_H
Roland McGrathaa524c82005-06-01 19:22:06 +0000539 struct epoll_event ev, *start, *cur, *end;
540 int failed = 0;
541
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200542 tprints("{");
Roland McGrathaa524c82005-06-01 19:22:06 +0000543 start = (struct epoll_event *) tcp->u_arg[1];
544 end = start + tcp->u_rval;
545 for (cur = start; cur < end; ++cur) {
546 if (cur > start)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200547 tprints(", ");
Roland McGrathaa524c82005-06-01 19:22:06 +0000548 if (umove(tcp, (long) cur, &ev) == 0)
549 print_epoll_event(&ev);
550 else {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200551 tprints("?");
Roland McGrathaa524c82005-06-01 19:22:06 +0000552 failed = 1;
553 break;
Roland McGrath93817bf2004-10-06 22:23:31 +0000554 }
Roland McGrath93817bf2004-10-06 22:23:31 +0000555 }
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200556 tprints("}");
Roland McGrathaa524c82005-06-01 19:22:06 +0000557 if (failed)
558 tprintf(" %#lx", (long) start);
559#else
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200560 tprints("{...}");
Roland McGrath63d6e542004-10-20 02:17:41 +0000561#endif
Roland McGrath93817bf2004-10-06 22:23:31 +0000562 }
Dmitry V. Levinc327d712011-10-11 16:05:57 +0000563 tprintf(", %d, %d", (int) tcp->u_arg[2], (int) tcp->u_arg[3]);
Roland McGrath93817bf2004-10-06 22:23:31 +0000564 }
Roland McGrathf2400052007-08-02 01:13:26 +0000565}
566
567int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000568sys_epoll_wait(struct tcb *tcp)
Roland McGrathf2400052007-08-02 01:13:26 +0000569{
570 epoll_wait_common(tcp);
571 return 0;
572}
573
574int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000575sys_epoll_pwait(struct tcb *tcp)
Roland McGrathf2400052007-08-02 01:13:26 +0000576{
577 epoll_wait_common(tcp);
Dmitry V. Levin96764992010-04-06 23:54:18 +0000578 if (exiting(tcp)) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200579 tprints(", ");
Denys Vlasenko5e133aa2013-07-18 17:02:21 +0200580 /* NB: kernel requires arg[5] == NSIG / 8 */
581 print_sigset_addr_len(tcp, tcp->u_arg[4], tcp->u_arg[5]);
582 tprintf(", %lu", tcp->u_arg[5]);
Dmitry V. Levin96764992010-04-06 23:54:18 +0000583 }
Roland McGrath93817bf2004-10-06 22:23:31 +0000584 return 0;
585}
Roland McGrath37b9f842005-05-09 08:02:00 +0000586
587int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000588sys_select(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000589{
Dmitry V. Levina7945a32006-12-13 17:10:11 +0000590 return decode_select(tcp, tcp->u_arg, BITNESS_CURRENT);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000591}
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000592
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000593int
594sys_pselect6(struct tcb *tcp)
595{
Dmitry V. Levina7945a32006-12-13 17:10:11 +0000596 int rc = decode_select(tcp, tcp->u_arg, BITNESS_CURRENT);
Roland McGrathfe10aa72007-11-01 21:52:20 +0000597 if (entering(tcp)) {
Denys Vlasenko8a1ebbb2013-07-18 18:10:13 +0200598 long r;
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000599 struct {
Denys Vlasenko8a1ebbb2013-07-18 18:10:13 +0200600 unsigned long ptr;
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000601 unsigned long len;
602 } data;
Denys Vlasenko8a1ebbb2013-07-18 18:10:13 +0200603#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
604 if (current_wordsize == 4) {
605 struct {
606 uint32_t ptr;
607 uint32_t len;
608 } data32;
609 r = umove(tcp, tcp->u_arg[5], &data32);
610 data.ptr = data32.ptr;
611 data.len = data32.len;
612 } else
613#endif
614 r = umove(tcp, tcp->u_arg[5], &data);
615 if (r < 0)
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000616 tprintf(", %#lx", tcp->u_arg[5]);
617 else {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200618 tprints(", {");
Denys Vlasenko5e133aa2013-07-18 17:02:21 +0200619 /* NB: kernel requires data.len == NSIG / 8 */
Denys Vlasenko8a1ebbb2013-07-18 18:10:13 +0200620 print_sigset_addr_len(tcp, data.ptr, data.len);
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000621 tprintf(", %lu}", data.len);
622 }
623 }
624 return rc;
625}
Roland McGrathe7c39672007-08-02 01:32:17 +0000626
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000627static int
628do_eventfd(struct tcb *tcp, int flags_arg)
Roland McGrathe7c39672007-08-02 01:32:17 +0000629{
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000630 if (entering(tcp)) {
Roland McGrathe7c39672007-08-02 01:32:17 +0000631 tprintf("%lu", tcp->u_arg[0]);
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000632 if (flags_arg >= 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200633 tprints(", ");
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000634 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
635 }
636 }
Roland McGrathe7c39672007-08-02 01:32:17 +0000637 return 0;
638}
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000639
640int
641sys_eventfd(struct tcb *tcp)
642{
643 return do_eventfd(tcp, -1);
644}
645
646int
647sys_eventfd2(struct tcb *tcp)
648{
649 return do_eventfd(tcp, 1);
650}
Ben Noordhuis88eafd82013-02-04 00:04:57 +0100651
652int
653sys_perf_event_open(struct tcb *tcp)
654{
655 if (entering(tcp)) {
656 tprintf("%#lx, %d, %d, %d, ",
657 tcp->u_arg[0],
658 (int) tcp->u_arg[1],
659 (int) tcp->u_arg[2],
660 (int) tcp->u_arg[3]);
661 printflags(perf_event_open_flags, tcp->u_arg[4],
662 "PERF_FLAG_???");
663 }
664 return 0;
665}