blob: e18735caf70dc93e85f8a13d693a834f75ebc014 [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
Roland McGrath37b9f842005-05-09 08:02:00 +000037#ifdef HAVE_LIBAIO_H
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +010038# include <libaio.h>
Roland McGrath37b9f842005-05-09 08:02:00 +000039#endif
Ben Noordhuis88eafd82013-02-04 00:04:57 +010040#ifdef HAVE_LINUX_PERF_EVENT_H
41# include <linux/perf_event.h>
42#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000043
Roland McGrathd9f816f2004-09-04 03:39:20 +000044static const struct xlat fcntlcmds[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000045 { F_DUPFD, "F_DUPFD" },
46 { F_GETFD, "F_GETFD" },
47 { F_SETFD, "F_SETFD" },
48 { F_GETFL, "F_GETFL" },
49 { F_SETFL, "F_SETFL" },
50 { F_GETLK, "F_GETLK" },
51 { F_SETLK, "F_SETLK" },
52 { F_SETLKW, "F_SETLKW" },
53 { F_GETOWN, "F_GETOWN" },
54 { F_SETOWN, "F_SETOWN" },
55#ifdef F_RSETLK
56 { F_RSETLK, "F_RSETLK" },
57#endif
58#ifdef F_RSETLKW
59 { F_RSETLKW, "F_RSETLKW" },
60#endif
61#ifdef F_RGETLK
62 { F_RGETLK, "F_RGETLK" },
63#endif
64#ifdef F_CNVT
65 { F_CNVT, "F_CNVT" },
66#endif
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +000067#ifdef F_SETSIG
68 { F_SETSIG, "F_SETSIG" },
69#endif
70#ifdef F_GETSIG
71 { F_GETSIG, "F_GETSIG" },
72#endif
John Hughesbdf48f52001-03-06 15:08:09 +000073#ifdef F_CHKFL
74 { F_CHKFL, "F_CHKFL" },
75#endif
76#ifdef F_DUP2FD
77 { F_DUP2FD, "F_DUP2FD" },
78#endif
79#ifdef F_ALLOCSP
80 { F_ALLOCSP, "F_ALLOCSP" },
81#endif
82#ifdef F_ISSTREAM
83 { F_ISSTREAM, "F_ISSTREAM" },
84#endif
85#ifdef F_PRIV
86 { F_PRIV, "F_PRIV" },
87#endif
88#ifdef F_NPRIV
89 { F_NPRIV, "F_NPRIV" },
90#endif
91#ifdef F_QUOTACL
92 { F_QUOTACL, "F_QUOTACL" },
93#endif
94#ifdef F_BLOCKS
95 { F_BLOCKS, "F_BLOCKS" },
96#endif
97#ifdef F_BLKSIZE
98 { F_BLKSIZE, "F_BLKSIZE" },
99#endif
100#ifdef F_GETOWN
101 { F_GETOWN, "F_GETOWN" },
102#endif
103#ifdef F_SETOWN
104 { F_SETOWN, "F_SETOWN" },
105#endif
106#ifdef F_REVOKE
107 { F_REVOKE, "F_REVOKE" },
108#endif
109#ifdef F_SETLK
110 { F_SETLK, "F_SETLK" },
111#endif
112#ifdef F_SETLKW
113 { F_SETLKW, "F_SETLKW" },
114#endif
115#ifdef F_FREESP
116 { F_FREESP, "F_FREESP" },
117#endif
118#ifdef F_GETLK
119 { F_GETLK, "F_GETLK" },
120#endif
121#ifdef F_SETLK64
122 { F_SETLK64, "F_SETLK64" },
123#endif
124#ifdef F_SETLKW64
125 { F_SETLKW64, "F_SETLKW64" },
126#endif
127#ifdef F_FREESP64
128 { F_FREESP64, "F_FREESP64" },
129#endif
130#ifdef F_GETLK64
131 { F_GETLK64, "F_GETLK64" },
132#endif
133#ifdef F_SHARE
134 { F_SHARE, "F_SHARE" },
135#endif
136#ifdef F_UNSHARE
137 { F_UNSHARE, "F_UNSHARE" },
138#endif
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000139#ifdef F_SETLEASE
140 { F_SETLEASE, "F_SETLEASE" },
141#endif
142#ifdef F_GETLEASE
143 { F_GETLEASE, "F_GETLEASE" },
144#endif
145#ifdef F_NOTIFY
146 { F_NOTIFY, "F_NOTIFY" },
147#endif
148#ifdef F_DUPFD_CLOEXEC
149 { F_DUPFD_CLOEXEC,"F_DUPFD_CLOEXEC"},
150#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000151 { 0, NULL },
152};
153
Roland McGrathd9f816f2004-09-04 03:39:20 +0000154static const struct xlat fdflags[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000155#ifdef FD_CLOEXEC
156 { FD_CLOEXEC, "FD_CLOEXEC" },
157#endif
158 { 0, NULL },
159};
160
161#ifdef LOCK_SH
162
Roland McGrathd9f816f2004-09-04 03:39:20 +0000163static const struct xlat flockcmds[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000164 { LOCK_SH, "LOCK_SH" },
165 { LOCK_EX, "LOCK_EX" },
166 { LOCK_NB, "LOCK_NB" },
167 { LOCK_UN, "LOCK_UN" },
168 { 0, NULL },
169};
170
171#endif /* LOCK_SH */
172
Roland McGrathd9f816f2004-09-04 03:39:20 +0000173static const struct xlat lockfcmds[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000174 { F_RDLCK, "F_RDLCK" },
175 { F_WRLCK, "F_WRLCK" },
176 { F_UNLCK, "F_UNLCK" },
177#ifdef F_EXLCK
178 { F_EXLCK, "F_EXLCK" },
179#endif
180#ifdef F_SHLCK
181 { F_SHLCK, "F_SHLCK" },
182#endif
183 { 0, NULL },
184};
185
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000186#ifdef F_NOTIFY
187static const struct xlat notifyflags[] = {
188#ifdef DN_ACCESS
189 { DN_ACCESS, "DN_ACCESS" },
190#endif
191#ifdef DN_MODIFY
192 { DN_MODIFY, "DN_MODIFY" },
193#endif
194#ifdef DN_CREATE
195 { DN_CREATE, "DN_CREATE" },
196#endif
197#ifdef DN_DELETE
198 { DN_DELETE, "DN_DELETE" },
199#endif
200#ifdef DN_RENAME
201 { DN_RENAME, "DN_RENAME" },
202#endif
203#ifdef DN_ATTRIB
204 { DN_ATTRIB, "DN_ATTRIB" },
205#endif
206#ifdef DN_MULTISHOT
207 { DN_MULTISHOT, "DN_MULTISHOT" },
208#endif
209 { 0, NULL },
210};
211#endif
212
Ben Noordhuis88eafd82013-02-04 00:04:57 +0100213static const struct xlat perf_event_open_flags[] = {
214#ifdef PERF_FLAG_FD_NO_GROUP
215 { PERF_FLAG_FD_NO_GROUP, "PERF_FLAG_FD_NO_GROUP" },
216#endif
217#ifdef PERF_FLAG_FD_OUTPUT
218 { PERF_FLAG_FD_OUTPUT, "PERF_FLAG_FD_OUTPUT" },
219#endif
220#ifdef PERF_FLAG_PID_CGROUP
221 { PERF_FLAG_PID_CGROUP, "PERF_FLAG_PID_CGROUP" },
222#endif
223 { 0, NULL },
224};
225
Dmitry V. Levin0eeda2c2013-05-01 16:37:08 +0000226#if _LFS64_LARGEFILE
227/* fcntl/lockf */
228static void
229printflock64(struct tcb *tcp, long addr, int getlk)
230{
231 struct flock64 fl;
232
233 if (umove(tcp, addr, &fl) < 0) {
234 tprints("{...}");
235 return;
236 }
237 tprints("{type=");
238 printxval(lockfcmds, fl.l_type, "F_???");
239 tprints(", whence=");
240 printxval(whence_codes, fl.l_whence, "SEEK_???");
241 tprintf(", start=%lld, len=%lld", (long long) fl.l_start, (long long) fl.l_len);
242 if (getlk)
243 tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
244 else
245 tprints("}");
246}
247#endif
248
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000249/* fcntl/lockf */
250static void
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000251printflock(struct tcb *tcp, long addr, int getlk)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000252{
253 struct flock fl;
254
Denys Vlasenko7a862d72009-04-15 13:22:59 +0000255#if SUPPORTED_PERSONALITIES > 1
Dmitry V. Levin0eeda2c2013-05-01 16:37:08 +0000256# ifdef X32
257 if (current_personality == 0) {
258 printflock64(tcp, addr, getlk);
259 return;
260 }
261# endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100262 if (current_wordsize != sizeof(fl.l_start)) {
263 if (current_wordsize == 4) {
Denys Vlasenko7a862d72009-04-15 13:22:59 +0000264 /* 32-bit x86 app on x86_64 and similar cases */
265 struct {
266 short int l_type;
267 short int l_whence;
268 int32_t l_start; /* off_t */
269 int32_t l_len; /* off_t */
270 int32_t l_pid; /* pid_t */
271 } fl32;
272 if (umove(tcp, addr, &fl32) < 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200273 tprints("{...}");
Denys Vlasenko7a862d72009-04-15 13:22:59 +0000274 return;
275 }
276 fl.l_type = fl32.l_type;
277 fl.l_whence = fl32.l_whence;
278 fl.l_start = fl32.l_start;
279 fl.l_len = fl32.l_len;
280 fl.l_pid = fl32.l_pid;
281 } else {
282 /* let people know we have a problem here */
Denys Vlasenko751acb32013-02-08 15:34:46 +0100283 tprintf("<decode error: unsupported wordsize %d>",
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100284 current_wordsize);
Denys Vlasenko7a862d72009-04-15 13:22:59 +0000285 return;
286 }
287 } else
288#endif
289 {
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000290 if (umove(tcp, addr, &fl) < 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200291 tprints("{...}");
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000292 return;
293 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000294 }
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200295 tprints("{type=");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000296 printxval(lockfcmds, fl.l_type, "F_???");
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200297 tprints(", whence=");
Denys Vlasenko86738a22013-02-17 14:31:55 +0100298 printxval(whence_codes, fl.l_whence, "SEEK_???");
Dmitry V. Levin0eeda2c2013-05-01 16:37:08 +0000299#ifdef X32
300 tprintf(", start=%lld, len=%lld", fl.l_start, fl.l_len);
301#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000302 tprintf(", start=%ld, len=%ld", fl.l_start, fl.l_len);
John Hughesbdf48f52001-03-06 15:08:09 +0000303#endif
Dmitry V. Levin0eeda2c2013-05-01 16:37:08 +0000304 if (getlk)
305 tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
306 else
307 tprints("}");
308}
John Hughesbdf48f52001-03-06 15:08:09 +0000309
Dmitry V. Levin9b5b67e2007-01-11 23:19:55 +0000310int
311sys_fcntl(struct tcb *tcp)
312{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000313 if (entering(tcp)) {
Dmitry V. Levin31382132011-03-04 05:08:02 +0300314 printfd(tcp, tcp->u_arg[0]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200315 tprints(", ");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000316 printxval(fcntlcmds, tcp->u_arg[1], "F_???");
317 switch (tcp->u_arg[1]) {
318 case F_SETFD:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200319 tprints(", ");
Roland McGrathb2dee132005-06-01 19:02:36 +0000320 printflags(fdflags, tcp->u_arg[2], "FD_???");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000321 break;
322 case F_SETOWN: case F_DUPFD:
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000323#ifdef F_DUPFD_CLOEXEC
324 case F_DUPFD_CLOEXEC:
325#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000326 tprintf(", %ld", tcp->u_arg[2]);
327 break;
328 case F_SETFL:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200329 tprints(", ");
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000330 tprint_open_modes(tcp->u_arg[2]);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000331 break;
332 case F_SETLK: case F_SETLKW:
John Hughesbdf48f52001-03-06 15:08:09 +0000333#ifdef F_FREESP
334 case F_FREESP:
335#endif
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200336 tprints(", ");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000337 printflock(tcp, tcp->u_arg[2], 0);
338 break;
John Hughesbdf48f52001-03-06 15:08:09 +0000339#if _LFS64_LARGEFILE
340#ifdef F_FREESP64
341 case F_FREESP64:
342#endif
Roland McGrathe948faf2002-12-15 23:58:18 +0000343 /* Linux glibc defines SETLK64 as SETLK,
John Hughesbdf48f52001-03-06 15:08:09 +0000344 even though the kernel has different values - as does Solaris. */
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000345#if defined(F_SETLK64) && F_SETLK64 + 0 != F_SETLK
John Hughesbdf48f52001-03-06 15:08:09 +0000346 case F_SETLK64:
347#endif
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000348#if defined(F_SETLKW64) && F_SETLKW64 + 0 != F_SETLKW
John Hughesbdf48f52001-03-06 15:08:09 +0000349 case F_SETLKW64:
350#endif
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200351 tprints(", ");
John Hughesbdf48f52001-03-06 15:08:09 +0000352 printflock64(tcp, tcp->u_arg[2], 0);
353 break;
354#endif
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000355#ifdef F_NOTIFY
356 case F_NOTIFY:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200357 tprints(", ");
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000358 printflags(notifyflags, tcp->u_arg[2], "DN_???");
359 break;
360#endif
361#ifdef F_SETLEASE
362 case F_SETLEASE:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200363 tprints(", ");
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000364 printxval(lockfcmds, tcp->u_arg[2], "F_???");
365 break;
366#endif
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000367 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000368 }
369 else {
370 switch (tcp->u_arg[1]) {
371 case F_DUPFD:
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000372#ifdef F_DUPFD_CLOEXEC
373 case F_DUPFD_CLOEXEC:
374#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000375 case F_SETFD: case F_SETFL:
376 case F_SETLK: case F_SETLKW:
377 case F_SETOWN: case F_GETOWN:
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000378#ifdef F_NOTIFY
379 case F_NOTIFY:
380#endif
381#ifdef F_SETLEASE
382 case F_SETLEASE:
383#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000384 break;
385 case F_GETFD:
Dmitry V. Levin21a75342008-09-03 01:22:18 +0000386 if (syserror(tcp) || tcp->u_rval == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000387 return 0;
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000388 tcp->auxstr = sprintflags("flags ", fdflags, tcp->u_rval);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000389 return RVAL_HEX|RVAL_STR;
390 case F_GETFL:
Dmitry V. Levin21a75342008-09-03 01:22:18 +0000391 if (syserror(tcp))
392 return 0;
Dmitry V. Levin9b5b67e2007-01-11 23:19:55 +0000393 tcp->auxstr = sprint_open_modes(tcp->u_rval);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000394 return RVAL_HEX|RVAL_STR;
395 case F_GETLK:
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200396 tprints(", ");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000397 printflock(tcp, tcp->u_arg[2], 1);
398 break;
John Hughesbdf48f52001-03-06 15:08:09 +0000399#if _LFS64_LARGEFILE
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200400#if defined(F_GETLK64) && F_GETLK64+0 != F_GETLK
John Hughesbdf48f52001-03-06 15:08:09 +0000401 case F_GETLK64:
402#endif
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200403 tprints(", ");
John Hughesbdf48f52001-03-06 15:08:09 +0000404 printflock64(tcp, tcp->u_arg[2], 1);
405 break;
406#endif
Denys Vlasenkoeedaac72009-03-10 20:41:58 +0000407#ifdef F_GETLEASE
408 case F_GETLEASE:
409 if (syserror(tcp))
410 return 0;
411 tcp->auxstr = xlookup(lockfcmds, tcp->u_rval);
412 return RVAL_HEX|RVAL_STR;
413#endif
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000414 default:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000415 tprintf(", %#lx", tcp->u_arg[2]);
416 break;
417 }
418 }
419 return 0;
420}
421
422#ifdef LOCK_SH
423
424int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000425sys_flock(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000426{
427 if (entering(tcp)) {
Dmitry V. Levin31382132011-03-04 05:08:02 +0300428 printfd(tcp, tcp->u_arg[0]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200429 tprints(", ");
Roland McGrathb2dee132005-06-01 19:02:36 +0000430 printflags(flockcmds, tcp->u_arg[1], "LOCK_???");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000431 }
432 return 0;
433}
434#endif /* LOCK_SH */
435
436int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000437sys_close(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000438{
439 if (entering(tcp)) {
Dmitry V. Levin31382132011-03-04 05:08:02 +0300440 printfd(tcp, tcp->u_arg[0]);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000441 }
442 return 0;
443}
444
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000445static int
446do_dup2(struct tcb *tcp, int flags_arg)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000447{
448 if (entering(tcp)) {
Dmitry V. Levin31382132011-03-04 05:08:02 +0300449 printfd(tcp, tcp->u_arg[0]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200450 tprints(", ");
Dmitry V. Levin31382132011-03-04 05:08:02 +0300451 printfd(tcp, tcp->u_arg[1]);
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000452 if (flags_arg >= 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200453 tprints(", ");
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000454 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
455 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000456 }
457 return 0;
458}
459
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000460int
461sys_dup2(struct tcb *tcp)
462{
463 return do_dup2(tcp, -1);
464}
465
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000466int
467sys_dup3(struct tcb *tcp)
468{
469 return do_dup2(tcp, 2);
470}
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000471
Denys Vlasenko84703742012-02-25 02:38:52 +0100472#if defined(ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000473int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000474sys_getdtablesize(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000475{
476 return 0;
477}
Denys Vlasenko84703742012-02-25 02:38:52 +0100478#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000479
480static int
Dmitry V. Levinc2982b52013-11-05 23:00:22 +0000481fd_isset(int d, fd_set *fds)
482{
483 const int bpl = 8 * sizeof(long);
484 long *s = (long *) fds;
485 return !!(s[d / bpl] & (1L << (d % bpl)));
486}
487
488static int
Dmitry V. Levina7945a32006-12-13 17:10:11 +0000489decode_select(struct tcb *tcp, long *args, enum bitness_t bitness)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000490{
Denys Vlasenkoad5155a2011-09-01 18:18:04 +0200491 int i, j;
Denys Vlasenko1f65c3c2013-11-05 16:20:16 +0100492 int nfds, fdsize;
Roland McGrathe85aaa42005-02-06 01:55:12 +0000493 fd_set *fds;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000494 const char *sep;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000495 long arg;
496
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000497 /* Kernel truncates arg[0] to int, we do the same. */
498 nfds = (int) args[0];
499
500 /* Kernel rejects negative nfds, so we don't parse it either. */
501 if (nfds < 0) {
502 nfds = 0;
503 fds = NULL;
504 }
Denys Vlasenko79a79ea2011-09-01 16:35:44 +0200505 /* Beware of select(2^31-1, NULL, NULL, NULL) and similar... */
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000506 if (nfds > 1024*1024)
507 nfds = 1024*1024;
508
509 /*
510 * We had bugs a-la "while (j < args[0])" and "umoven(args[0])" below.
Dr. David Alan Gilbert025f1082013-11-05 11:54:51 +0100511 * Instead of args[0], use nfds for fd count, fdsize for array lengths.
512 */
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000513 fdsize = (((nfds + 7) / 8) + sizeof(long)-1) & -sizeof(long);
Denys Vlasenko79a79ea2011-09-01 16:35:44 +0200514
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000515 if (entering(tcp)) {
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000516 tprintf("%d", (int) args[0]);
517
518 if (fdsize > 0) {
519 fds = malloc(fdsize);
520 if (!fds)
521 die_out_of_memory();
522 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000523 for (i = 0; i < 3; i++) {
524 arg = args[i+1];
525 if (arg == 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200526 tprints(", NULL");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000527 continue;
528 }
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000529 if (!verbose(tcp) || !fds) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000530 tprintf(", %#lx", arg);
531 continue;
532 }
Roland McGrathe85aaa42005-02-06 01:55:12 +0000533 if (umoven(tcp, arg, fdsize, (char *) fds) < 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200534 tprints(", [?]");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000535 continue;
536 }
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200537 tprints(", [");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000538 for (j = 0, sep = ""; j < nfds; j++) {
Dmitry V. Levinc2982b52013-11-05 23:00:22 +0000539 if (fd_isset(j, fds)) {
Denys Vlasenko5940e652011-09-01 09:55:05 +0200540 tprints(sep);
Dmitry V. Levin31382132011-03-04 05:08:02 +0300541 printfd(tcp, j);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000542 sep = " ";
543 }
544 }
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200545 tprints("]");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000546 }
Roland McGrathe85aaa42005-02-06 01:55:12 +0000547 free(fds);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200548 tprints(", ");
Roland McGrath6afc5652007-07-24 01:57:11 +0000549 printtv_bitness(tcp, args[4], bitness, 0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000550 }
Denys Vlasenko7b609d52011-06-22 14:32:43 +0200551 else {
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200552 static char outstr[1024];
553 char *outptr;
554#define end_outstr (outstr + sizeof(outstr))
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000555 int ready_fds;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000556
557 if (syserror(tcp))
558 return 0;
559
Dr. David Alan Gilbert025f1082013-11-05 11:54:51 +0100560 ready_fds = tcp->u_rval;
561 if (ready_fds == 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000562 tcp->auxstr = "Timeout";
563 return RVAL_STR;
564 }
Roland McGrathe85aaa42005-02-06 01:55:12 +0000565
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200566 fds = malloc(fdsize);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200567 if (!fds)
568 die_out_of_memory();
Roland McGrathe85aaa42005-02-06 01:55:12 +0000569
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200570 outptr = outstr;
571 sep = "";
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000572 for (i = 0; i < 3 && ready_fds > 0; i++) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000573 int first = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000574
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000575 arg = args[i+1];
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200576 if (!arg || umoven(tcp, arg, fdsize, (char *) fds) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000577 continue;
Dr. David Alan Gilbert025f1082013-11-05 11:54:51 +0100578 for (j = 0; j < nfds; j++) {
Dmitry V. Levinc2982b52013-11-05 23:00:22 +0000579 if (fd_isset(j, fds)) {
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200580 /* +2 chars needed at the end: ']',NUL */
581 if (outptr < end_outstr - (sizeof(", except [") + sizeof(int)*3 + 2)) {
582 if (first) {
583 outptr += sprintf(outptr, "%s%s [%u",
584 sep,
585 i == 0 ? "in" : i == 1 ? "out" : "except",
586 j
587 );
588 first = 0;
589 sep = ", ";
590 }
591 else {
592 outptr += sprintf(outptr, " %u", j);
593 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000594 }
Dmitry V. Levinf3696b32013-11-05 22:46:43 +0000595 if (--ready_fds == 0)
596 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000597 }
598 }
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200599 if (outptr != outstr)
600 *outptr++ = ']';
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000601 }
Roland McGrathe85aaa42005-02-06 01:55:12 +0000602 free(fds);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000603 /* This contains no useful information on SunOS. */
604 if (args[4]) {
Denys Vlasenkoa1d541e2012-01-20 11:04:04 +0100605 if (outptr < end_outstr - (10 + TIMEVAL_TEXT_BUFSIZE)) {
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200606 outptr += sprintf(outptr, "%sleft ", sep);
Denys Vlasenkoa1d541e2012-01-20 11:04:04 +0100607 outptr = sprinttv(outptr, tcp, args[4], bitness, /*special:*/ 0);
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200608 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000609 }
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200610 *outptr = '\0';
Denys Vlasenko11617252011-09-01 11:27:37 +0200611 tcp->auxstr = outstr;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000612 return RVAL_STR;
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200613#undef end_outstr
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000614 }
615 return 0;
616}
617
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000618int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000619sys_oldselect(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000620{
621 long args[5];
622
623 if (umoven(tcp, tcp->u_arg[0], sizeof args, (char *) args) < 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200624 tprints("[...]");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000625 return 0;
626 }
Dmitry V. Levina7945a32006-12-13 17:10:11 +0000627 return decode_select(tcp, args, BITNESS_CURRENT);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000628}
629
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000630#ifdef ALPHA
Roland McGrathe948faf2002-12-15 23:58:18 +0000631int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000632sys_osf_select(struct tcb *tcp)
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000633{
634 long *args = tcp->u_arg;
Dmitry V. Levina7945a32006-12-13 17:10:11 +0000635 return decode_select(tcp, args, BITNESS_32);
Wichert Akkermanf5eeabb1999-11-18 17:09:47 +0000636}
637#endif
638
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000639static const struct xlat epollctls[] = {
Roland McGrath63d6e542004-10-20 02:17:41 +0000640#ifdef EPOLL_CTL_ADD
Roland McGrath93817bf2004-10-06 22:23:31 +0000641 { EPOLL_CTL_ADD, "EPOLL_CTL_ADD" },
Roland McGrath63d6e542004-10-20 02:17:41 +0000642#endif
643#ifdef EPOLL_CTL_MOD
Roland McGrath93817bf2004-10-06 22:23:31 +0000644 { EPOLL_CTL_MOD, "EPOLL_CTL_MOD" },
Roland McGrath63d6e542004-10-20 02:17:41 +0000645#endif
646#ifdef EPOLL_CTL_DEL
Roland McGrath93817bf2004-10-06 22:23:31 +0000647 { EPOLL_CTL_DEL, "EPOLL_CTL_DEL" },
Roland McGrath63d6e542004-10-20 02:17:41 +0000648#endif
Roland McGrath93817bf2004-10-06 22:23:31 +0000649 { 0, NULL }
650};
651
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000652static const struct xlat epollevents[] = {
Roland McGrath63d6e542004-10-20 02:17:41 +0000653#ifdef EPOLLIN
Roland McGrath93817bf2004-10-06 22:23:31 +0000654 { EPOLLIN, "EPOLLIN" },
Roland McGrath63d6e542004-10-20 02:17:41 +0000655#endif
656#ifdef EPOLLPRI
Roland McGrath93817bf2004-10-06 22:23:31 +0000657 { EPOLLPRI, "EPOLLPRI" },
Roland McGrath63d6e542004-10-20 02:17:41 +0000658#endif
659#ifdef EPOLLOUT
Roland McGrath93817bf2004-10-06 22:23:31 +0000660 { EPOLLOUT, "EPOLLOUT" },
Roland McGrath63d6e542004-10-20 02:17:41 +0000661#endif
662#ifdef EPOLLRDNORM
Roland McGrath93817bf2004-10-06 22:23:31 +0000663 { EPOLLRDNORM, "EPOLLRDNORM" },
Roland McGrath63d6e542004-10-20 02:17:41 +0000664#endif
665#ifdef EPOLLRDBAND
Roland McGrath93817bf2004-10-06 22:23:31 +0000666 { EPOLLRDBAND, "EPOLLRDBAND" },
Roland McGrath63d6e542004-10-20 02:17:41 +0000667#endif
668#ifdef EPOLLWRNORM
Roland McGrath93817bf2004-10-06 22:23:31 +0000669 { EPOLLWRNORM, "EPOLLWRNORM" },
Roland McGrath63d6e542004-10-20 02:17:41 +0000670#endif
671#ifdef EPOLLWRBAND
Roland McGrath93817bf2004-10-06 22:23:31 +0000672 { EPOLLWRBAND, "EPOLLWRBAND" },
Roland McGrath63d6e542004-10-20 02:17:41 +0000673#endif
674#ifdef EPOLLMSG
Roland McGrath93817bf2004-10-06 22:23:31 +0000675 { EPOLLMSG, "EPOLLMSG" },
Roland McGrath63d6e542004-10-20 02:17:41 +0000676#endif
677#ifdef EPOLLERR
Roland McGrath93817bf2004-10-06 22:23:31 +0000678 { EPOLLERR, "EPOLLERR" },
Roland McGrath63d6e542004-10-20 02:17:41 +0000679#endif
680#ifdef EPOLLHUP
Roland McGrath93817bf2004-10-06 22:23:31 +0000681 { EPOLLHUP, "EPOLLHUP" },
Roland McGrath63d6e542004-10-20 02:17:41 +0000682#endif
Dmitry V. Levin5967c1a2011-10-11 15:55:40 +0000683#ifdef EPOLLRDHUP
684 { EPOLLRDHUP, "EPOLLRDHUP" },
685#endif
Roland McGrath63d6e542004-10-20 02:17:41 +0000686#ifdef EPOLLONESHOT
Roland McGrath93817bf2004-10-06 22:23:31 +0000687 { EPOLLONESHOT, "EPOLLONESHOT" },
Roland McGrath63d6e542004-10-20 02:17:41 +0000688#endif
689#ifdef EPOLLET
Roland McGrath93817bf2004-10-06 22:23:31 +0000690 { EPOLLET, "EPOLLET" },
Roland McGrath63d6e542004-10-20 02:17:41 +0000691#endif
Roland McGrath93817bf2004-10-06 22:23:31 +0000692 { 0, NULL }
693};
694
Denys Vlasenko72879c62012-02-27 14:18:02 +0100695/* Not aliased to printargs_ld: we want it to have a distinct address */
Roland McGrath93817bf2004-10-06 22:23:31 +0000696int
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000697sys_epoll_create(struct tcb *tcp)
Roland McGrath93817bf2004-10-06 22:23:31 +0000698{
Denys Vlasenko72879c62012-02-27 14:18:02 +0100699 return printargs_ld(tcp);
Roland McGrath93817bf2004-10-06 22:23:31 +0000700}
701
Mike Frysingeraed334c2011-10-13 22:33:45 -0400702static const struct xlat epollflags[] = {
703#ifdef EPOLL_CLOEXEC
704 { EPOLL_CLOEXEC, "EPOLL_CLOEXEC" },
705#endif
706#ifdef EPOLL_NONBLOCK
707 { EPOLL_NONBLOCK, "EPOLL_NONBLOCK" },
708#endif
709 { 0, NULL }
710};
711
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000712int
713sys_epoll_create1(struct tcb *tcp)
714{
715 if (entering(tcp))
Mike Frysingeraed334c2011-10-13 22:33:45 -0400716 printflags(epollflags, tcp->u_arg[0], "EPOLL_???");
Dmitry V. Levin4371b102008-11-10 22:53:02 +0000717 return 0;
718}
719
Roland McGrath63d6e542004-10-20 02:17:41 +0000720#ifdef HAVE_SYS_EPOLL_H
Roland McGrath93817bf2004-10-06 22:23:31 +0000721static void
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000722print_epoll_event(struct epoll_event *ev)
Roland McGrath93817bf2004-10-06 22:23:31 +0000723{
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200724 tprints("{");
Roland McGrathb2dee132005-06-01 19:02:36 +0000725 printflags(epollevents, ev->events, "EPOLL???");
Roland McGrath93817bf2004-10-06 22:23:31 +0000726 /* We cannot know what format the program uses, so print u32 and u64
727 which will cover every value. */
728 tprintf(", {u32=%" PRIu32 ", u64=%" PRIu64 "}}",
729 ev->data.u32, ev->data.u64);
730}
Roland McGrath63d6e542004-10-20 02:17:41 +0000731#endif
Roland McGrath93817bf2004-10-06 22:23:31 +0000732
733int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000734sys_epoll_ctl(struct tcb *tcp)
Roland McGrath93817bf2004-10-06 22:23:31 +0000735{
736 if (entering(tcp)) {
Dmitry V. Levin31382132011-03-04 05:08:02 +0300737 printfd(tcp, tcp->u_arg[0]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200738 tprints(", ");
Denys Vlasenkoadedb512008-12-30 18:47:55 +0000739 printxval(epollctls, tcp->u_arg[1], "EPOLL_CTL_???");
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200740 tprints(", ");
Dmitry V. Levin31382132011-03-04 05:08:02 +0300741 printfd(tcp, tcp->u_arg[2]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200742 tprints(", ");
Roland McGrath93817bf2004-10-06 22:23:31 +0000743 if (tcp->u_arg[3] == 0)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200744 tprints("NULL");
Roland McGrath63d6e542004-10-20 02:17:41 +0000745 else {
746#ifdef HAVE_SYS_EPOLL_H
747 struct epoll_event ev;
Roland McGrath6e52d232004-10-20 02:17:41 +0000748 if (umove(tcp, tcp->u_arg[3], &ev) == 0)
Roland McGrath63d6e542004-10-20 02:17:41 +0000749 print_epoll_event(&ev);
750 else
751#endif
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200752 tprints("{...}");
Roland McGrath63d6e542004-10-20 02:17:41 +0000753 }
Roland McGrath93817bf2004-10-06 22:23:31 +0000754 }
755 return 0;
756}
757
Roland McGrathf2400052007-08-02 01:13:26 +0000758static void
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000759epoll_wait_common(struct tcb *tcp)
Roland McGrath93817bf2004-10-06 22:23:31 +0000760{
Dmitry V. Levin31382132011-03-04 05:08:02 +0300761 if (entering(tcp)) {
762 printfd(tcp, tcp->u_arg[0]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200763 tprints(", ");
Dmitry V. Levin31382132011-03-04 05:08:02 +0300764 } else {
Roland McGrath93817bf2004-10-06 22:23:31 +0000765 if (syserror(tcp))
766 tprintf("%lx", tcp->u_arg[1]);
767 else if (tcp->u_rval == 0)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200768 tprints("{}");
Roland McGrath93817bf2004-10-06 22:23:31 +0000769 else {
Roland McGrath63d6e542004-10-20 02:17:41 +0000770#ifdef HAVE_SYS_EPOLL_H
Roland McGrathaa524c82005-06-01 19:22:06 +0000771 struct epoll_event ev, *start, *cur, *end;
772 int failed = 0;
773
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200774 tprints("{");
Roland McGrathaa524c82005-06-01 19:22:06 +0000775 start = (struct epoll_event *) tcp->u_arg[1];
776 end = start + tcp->u_rval;
777 for (cur = start; cur < end; ++cur) {
778 if (cur > start)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200779 tprints(", ");
Roland McGrathaa524c82005-06-01 19:22:06 +0000780 if (umove(tcp, (long) cur, &ev) == 0)
781 print_epoll_event(&ev);
782 else {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200783 tprints("?");
Roland McGrathaa524c82005-06-01 19:22:06 +0000784 failed = 1;
785 break;
Roland McGrath93817bf2004-10-06 22:23:31 +0000786 }
Roland McGrath93817bf2004-10-06 22:23:31 +0000787 }
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200788 tprints("}");
Roland McGrathaa524c82005-06-01 19:22:06 +0000789 if (failed)
790 tprintf(" %#lx", (long) start);
791#else
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200792 tprints("{...}");
Roland McGrath63d6e542004-10-20 02:17:41 +0000793#endif
Roland McGrath93817bf2004-10-06 22:23:31 +0000794 }
Dmitry V. Levinc327d712011-10-11 16:05:57 +0000795 tprintf(", %d, %d", (int) tcp->u_arg[2], (int) tcp->u_arg[3]);
Roland McGrath93817bf2004-10-06 22:23:31 +0000796 }
Roland McGrathf2400052007-08-02 01:13:26 +0000797}
798
799int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000800sys_epoll_wait(struct tcb *tcp)
Roland McGrathf2400052007-08-02 01:13:26 +0000801{
802 epoll_wait_common(tcp);
803 return 0;
804}
805
806int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000807sys_epoll_pwait(struct tcb *tcp)
Roland McGrathf2400052007-08-02 01:13:26 +0000808{
809 epoll_wait_common(tcp);
Dmitry V. Levin96764992010-04-06 23:54:18 +0000810 if (exiting(tcp)) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200811 tprints(", ");
Denys Vlasenko5e133aa2013-07-18 17:02:21 +0200812 /* NB: kernel requires arg[5] == NSIG / 8 */
813 print_sigset_addr_len(tcp, tcp->u_arg[4], tcp->u_arg[5]);
814 tprintf(", %lu", tcp->u_arg[5]);
Dmitry V. Levin96764992010-04-06 23:54:18 +0000815 }
Roland McGrath93817bf2004-10-06 22:23:31 +0000816 return 0;
817}
Roland McGrath37b9f842005-05-09 08:02:00 +0000818
819int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000820sys_io_setup(struct tcb *tcp)
Roland McGrath37b9f842005-05-09 08:02:00 +0000821{
822 if (entering(tcp))
823 tprintf("%ld, ", tcp->u_arg[0]);
824 else {
825 if (syserror(tcp))
826 tprintf("0x%0lx", tcp->u_arg[1]);
827 else {
828 unsigned long user_id;
829 if (umove(tcp, tcp->u_arg[1], &user_id) == 0)
830 tprintf("{%lu}", user_id);
831 else
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200832 tprints("{...}");
Roland McGrath37b9f842005-05-09 08:02:00 +0000833 }
834 }
835 return 0;
836}
837
838int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000839sys_io_destroy(struct tcb *tcp)
Roland McGrath37b9f842005-05-09 08:02:00 +0000840{
841 if (entering(tcp))
842 tprintf("%lu", tcp->u_arg[0]);
843 return 0;
844}
845
Dmitry V. Levin081e72f2011-06-21 15:11:57 +0000846#ifdef HAVE_LIBAIO_H
847
Andi Kleendfcd7412011-06-13 22:05:44 +0000848enum iocb_sub {
849 SUB_NONE, SUB_COMMON, SUB_POLL, SUB_VECTOR
850};
851
852static const char *
853iocb_cmd_lookup(unsigned cmd, enum iocb_sub *sub)
854{
Denys Vlasenko79992622011-08-19 19:44:17 +0200855 static char buf[sizeof("%u /* SUB_??? */") + sizeof(int)*3];
856 static const struct {
Andi Kleendfcd7412011-06-13 22:05:44 +0000857 const char *name;
858 enum iocb_sub sub;
859 } cmds[] = {
860 { "pread", SUB_COMMON },
861 { "pwrite", SUB_COMMON },
862 { "fsync", SUB_NONE },
863 { "fdsync", SUB_NONE },
864 { "op4", SUB_NONE },
865 { "poll", SUB_POLL },
866 { "noop", SUB_NONE },
867 { "preadv", SUB_VECTOR },
868 { "pwritev", SUB_VECTOR },
869 };
870
871 if (cmd < ARRAY_SIZE(cmds)) {
872 *sub = cmds[cmd].sub;
873 return cmds[cmd].name;
874 }
875 *sub = SUB_NONE;
Denys Vlasenko79992622011-08-19 19:44:17 +0200876 sprintf(buf, "%u /* SUB_??? */", cmd);
Andi Kleendfcd7412011-06-13 22:05:44 +0000877 return buf;
878}
879
880/* Not defined in libaio.h */
881#ifndef IOCB_RESFD
882# define IOCB_RESFD (1 << 0)
883#endif
884
885static void
886print_common_flags(struct iocb *iocb)
887{
Dmitry V. Levin3b211d82013-03-18 23:28:29 +0000888#if HAVE_STRUCT_IOCB_U_C_FLAGS
Andi Kleendfcd7412011-06-13 22:05:44 +0000889 if (iocb->u.c.flags & IOCB_RESFD)
Dmitry V. Levinc108ba22013-03-18 22:58:28 +0000890 tprintf(", resfd=%d", iocb->u.c.resfd);
Andi Kleendfcd7412011-06-13 22:05:44 +0000891 if (iocb->u.c.flags & ~IOCB_RESFD)
Dmitry V. Levinc108ba22013-03-18 22:58:28 +0000892 tprintf(", flags=%x", iocb->u.c.flags);
Dmitry V. Levin3b211d82013-03-18 23:28:29 +0000893#else
894# warning "libaio.h is too old => limited io_submit decoding"
895#endif
Andi Kleendfcd7412011-06-13 22:05:44 +0000896}
Dmitry V. Levin081e72f2011-06-21 15:11:57 +0000897
898#endif /* HAVE_LIBAIO_H */
899
Roland McGrath37b9f842005-05-09 08:02:00 +0000900int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000901sys_io_submit(struct tcb *tcp)
Roland McGrath37b9f842005-05-09 08:02:00 +0000902{
903 long nr;
904 if (entering(tcp)) {
905 tprintf("%lu, %ld, ", tcp->u_arg[0], tcp->u_arg[1]);
906 nr = tcp->u_arg[1];
907 /* and if nr is negative? */
908 if (nr == 0)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200909 tprints("{}");
Roland McGrath37b9f842005-05-09 08:02:00 +0000910 else {
911#ifdef HAVE_LIBAIO_H
912 long i;
913 struct iocb *iocbp, **iocbs = (void *)tcp->u_arg[2];
914
915 for (i = 0; i < nr; i++, iocbs++) {
Andi Kleendfcd7412011-06-13 22:05:44 +0000916 enum iocb_sub sub;
Roland McGrath37b9f842005-05-09 08:02:00 +0000917 struct iocb iocb;
918 if (i == 0)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200919 tprints("{");
Roland McGrath37b9f842005-05-09 08:02:00 +0000920 else
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200921 tprints(", ");
Roland McGrath37b9f842005-05-09 08:02:00 +0000922
923 if (umove(tcp, (unsigned long)iocbs, &iocbp) ||
924 umove(tcp, (unsigned long)iocbp, &iocb)) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200925 tprints("{...}");
Roland McGrath37b9f842005-05-09 08:02:00 +0000926 continue;
927 }
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200928 tprints("{");
Andi Kleendfcd7412011-06-13 22:05:44 +0000929 if (iocb.data)
930 tprintf("data:%p, ", iocb.data);
931 if (iocb.key)
932 tprintf("key:%u, ", iocb.key);
933 tprintf("%s, ", iocb_cmd_lookup(iocb.aio_lio_opcode, &sub));
934 if (iocb.aio_reqprio)
935 tprintf("reqprio:%d, ", iocb.aio_reqprio);
936 tprintf("filedes:%d", iocb.aio_fildes);
937 switch (sub) {
938 case SUB_COMMON:
Dmitry V. Levin3b211d82013-03-18 23:28:29 +0000939#if HAVE_DECL_IO_CMD_PWRITE
Andi Kleendfcd7412011-06-13 22:05:44 +0000940 if (iocb.aio_lio_opcode == IO_CMD_PWRITE) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200941 tprints(", str:");
Andi Kleendfcd7412011-06-13 22:05:44 +0000942 printstr(tcp, (unsigned long)iocb.u.c.buf,
943 iocb.u.c.nbytes);
Dmitry V. Levin3b211d82013-03-18 23:28:29 +0000944 } else
945#endif
Andi Kleendfcd7412011-06-13 22:05:44 +0000946 tprintf(", buf:%p", iocb.u.c.buf);
Zev Weiss72cdefb2013-03-30 19:19:50 -0500947 tprintf(", nbytes:%lu, offset:%lld",
Andi Kleendfcd7412011-06-13 22:05:44 +0000948 iocb.u.c.nbytes,
949 iocb.u.c.offset);
950 print_common_flags(&iocb);
951 break;
952 case SUB_VECTOR:
Zev Weiss72cdefb2013-03-30 19:19:50 -0500953 tprintf(", %lld", iocb.u.v.offset);
Andi Kleendfcd7412011-06-13 22:05:44 +0000954 print_common_flags(&iocb);
Dmitry V. Levinc108ba22013-03-18 22:58:28 +0000955 tprints(", ");
Andi Kleendfcd7412011-06-13 22:05:44 +0000956 tprint_iov(tcp, iocb.u.v.nr,
957 (unsigned long)iocb.u.v.vec,
Dmitry V. Levin3b211d82013-03-18 23:28:29 +0000958#if HAVE_DECL_IO_CMD_PWRITEV
959 iocb.aio_lio_opcode == IO_CMD_PWRITEV
960#else
961 0
962#endif
963 );
Andi Kleendfcd7412011-06-13 22:05:44 +0000964 break;
965 case SUB_POLL:
966 tprintf(", %x", iocb.u.poll.events);
967 break;
968 case SUB_NONE:
969 break;
970 }
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200971 tprints("}");
Roland McGrath37b9f842005-05-09 08:02:00 +0000972 }
973 if (i)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200974 tprints("}");
Roland McGrath37b9f842005-05-09 08:02:00 +0000975#else
Dmitry V. Levin3b211d82013-03-18 23:28:29 +0000976#warning "libaio.h is not available => no io_submit decoding"
Andi Kleendfcd7412011-06-13 22:05:44 +0000977 tprintf("%#lx", tcp->u_arg[2]);
Roland McGrath37b9f842005-05-09 08:02:00 +0000978#endif
979 }
980 }
981 return 0;
982}
983
984int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +0000985sys_io_cancel(struct tcb *tcp)
Roland McGrath37b9f842005-05-09 08:02:00 +0000986{
987 if (entering(tcp)) {
988#ifdef HAVE_LIBAIO_H
989 struct iocb iocb;
990#endif
991 tprintf("%lu, ", tcp->u_arg[0]);
992#ifdef HAVE_LIBAIO_H
993 if (umove(tcp, tcp->u_arg[1], &iocb) == 0) {
Denys Vlasenko61d62cf2012-04-16 18:12:27 +0200994 tprintf("{%p, %u, %u, %u, %d}, ",
Roland McGrath37b9f842005-05-09 08:02:00 +0000995 iocb.data, iocb.key,
Denys Vlasenko61d62cf2012-04-16 18:12:27 +0200996 (unsigned)iocb.aio_lio_opcode,
997 (unsigned)iocb.aio_reqprio, iocb.aio_fildes);
Roland McGrath37b9f842005-05-09 08:02:00 +0000998 } else
999#endif
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02001000 tprints("{...}, ");
Roland McGrath37b9f842005-05-09 08:02:00 +00001001 } else {
1002 if (tcp->u_rval < 0)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02001003 tprints("{...}");
Roland McGrath37b9f842005-05-09 08:02:00 +00001004 else {
1005#ifdef HAVE_LIBAIO_H
1006 struct io_event event;
1007 if (umove(tcp, tcp->u_arg[2], &event) == 0)
1008 tprintf("{%p, %p, %ld, %ld}",
1009 event.data, event.obj,
1010 event.res, event.res2);
Dmitry V. Levin414fe7d2009-07-08 11:21:17 +00001011 else
Roland McGrath37b9f842005-05-09 08:02:00 +00001012#endif
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02001013 tprints("{...}");
Roland McGrath37b9f842005-05-09 08:02:00 +00001014 }
1015 }
1016 return 0;
1017}
1018
1019int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +00001020sys_io_getevents(struct tcb *tcp)
Roland McGrath37b9f842005-05-09 08:02:00 +00001021{
1022 if (entering(tcp)) {
1023 tprintf("%ld, %ld, %ld, ", tcp->u_arg[0], tcp->u_arg[1],
1024 tcp->u_arg[2]);
1025 } else {
1026 if (tcp->u_rval == 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02001027 tprints("{}");
Denys Vlasenko3e3490a2012-03-17 01:27:37 +01001028 } else {
Roland McGrath37b9f842005-05-09 08:02:00 +00001029#ifdef HAVE_LIBAIO_H
1030 struct io_event *events = (void *)tcp->u_arg[3];
1031 long i, nr = tcp->u_rval;
1032
1033 for (i = 0; i < nr; i++, events++) {
1034 struct io_event event;
1035
1036 if (i == 0)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02001037 tprints("{");
Roland McGrath37b9f842005-05-09 08:02:00 +00001038 else
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02001039 tprints(", ");
Roland McGrath37b9f842005-05-09 08:02:00 +00001040
1041 if (umove(tcp, (unsigned long)events, &event) != 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02001042 tprints("{...}");
Roland McGrath37b9f842005-05-09 08:02:00 +00001043 continue;
1044 }
1045 tprintf("{%p, %p, %ld, %ld}", event.data,
1046 event.obj, event.res, event.res2);
1047 }
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02001048 tprints("}, ");
Roland McGrath37b9f842005-05-09 08:02:00 +00001049#else
Denys Vlasenkob237b1b2012-02-27 13:56:59 +01001050 tprints("{...}");
Roland McGrath37b9f842005-05-09 08:02:00 +00001051#endif
1052 }
1053
Roland McGrath6bc09da2007-11-01 21:50:54 +00001054 print_timespec(tcp, tcp->u_arg[4]);
Roland McGrath37b9f842005-05-09 08:02:00 +00001055 }
1056 return 0;
1057}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001058
1059int
Denys Vlasenko30b5e5a2009-01-06 15:12:52 +00001060sys_select(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001061{
Dmitry V. Levina7945a32006-12-13 17:10:11 +00001062 return decode_select(tcp, tcp->u_arg, BITNESS_CURRENT);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001063}
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +00001064
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +00001065int
1066sys_pselect6(struct tcb *tcp)
1067{
Dmitry V. Levina7945a32006-12-13 17:10:11 +00001068 int rc = decode_select(tcp, tcp->u_arg, BITNESS_CURRENT);
Roland McGrathfe10aa72007-11-01 21:52:20 +00001069 if (entering(tcp)) {
Denys Vlasenko8a1ebbb2013-07-18 18:10:13 +02001070 long r;
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +00001071 struct {
Denys Vlasenko8a1ebbb2013-07-18 18:10:13 +02001072 unsigned long ptr;
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +00001073 unsigned long len;
1074 } data;
Denys Vlasenko8a1ebbb2013-07-18 18:10:13 +02001075#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1076 if (current_wordsize == 4) {
1077 struct {
1078 uint32_t ptr;
1079 uint32_t len;
1080 } data32;
1081 r = umove(tcp, tcp->u_arg[5], &data32);
1082 data.ptr = data32.ptr;
1083 data.len = data32.len;
1084 } else
1085#endif
1086 r = umove(tcp, tcp->u_arg[5], &data);
1087 if (r < 0)
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +00001088 tprintf(", %#lx", tcp->u_arg[5]);
1089 else {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02001090 tprints(", {");
Denys Vlasenko5e133aa2013-07-18 17:02:21 +02001091 /* NB: kernel requires data.len == NSIG / 8 */
Denys Vlasenko8a1ebbb2013-07-18 18:10:13 +02001092 print_sigset_addr_len(tcp, data.ptr, data.len);
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +00001093 tprintf(", %lu}", data.len);
1094 }
1095 }
1096 return rc;
1097}
Roland McGrathe7c39672007-08-02 01:32:17 +00001098
Dmitry V. Levin4371b102008-11-10 22:53:02 +00001099static int
1100do_eventfd(struct tcb *tcp, int flags_arg)
Roland McGrathe7c39672007-08-02 01:32:17 +00001101{
Dmitry V. Levin4371b102008-11-10 22:53:02 +00001102 if (entering(tcp)) {
Roland McGrathe7c39672007-08-02 01:32:17 +00001103 tprintf("%lu", tcp->u_arg[0]);
Dmitry V. Levin4371b102008-11-10 22:53:02 +00001104 if (flags_arg >= 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02001105 tprints(", ");
Dmitry V. Levin4371b102008-11-10 22:53:02 +00001106 printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
1107 }
1108 }
Roland McGrathe7c39672007-08-02 01:32:17 +00001109 return 0;
1110}
Dmitry V. Levin4371b102008-11-10 22:53:02 +00001111
1112int
1113sys_eventfd(struct tcb *tcp)
1114{
1115 return do_eventfd(tcp, -1);
1116}
1117
1118int
1119sys_eventfd2(struct tcb *tcp)
1120{
1121 return do_eventfd(tcp, 1);
1122}
Ben Noordhuis88eafd82013-02-04 00:04:57 +01001123
1124int
1125sys_perf_event_open(struct tcb *tcp)
1126{
1127 if (entering(tcp)) {
1128 tprintf("%#lx, %d, %d, %d, ",
1129 tcp->u_arg[0],
1130 (int) tcp->u_arg[1],
1131 (int) tcp->u_arg[2],
1132 (int) tcp->u_arg[3]);
1133 printflags(perf_event_open_flags, tcp->u_arg[4],
1134 "PERF_FLAG_???");
1135 }
1136 return 0;
1137}