desc.c revision 60fe8c139c6f2febefe595781812ddf0864a6ab8
1/*
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>
5 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * 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.
29 *
30 *	$Id$
31 */
32
33#include "defs.h"
34
35#include <fcntl.h>
36#include <sys/file.h>
37#ifdef LINUX
38#include <inttypes.h>
39#endif
40#ifdef HAVE_SYS_EPOLL_H
41#include <sys/epoll.h>
42#endif
43#ifdef HAVE_LIBAIO_H
44#include <libaio.h>
45#endif
46
47#if HAVE_LONG_LONG_OFF_T
48/*
49 * Hacks for systems that have a long long off_t
50 */
51#define flock64	flock		/* Horrid hack */
52#define printflock printflock64	/* Horrider hack */
53#endif
54
55
56static const struct xlat fcntlcmds[] = {
57	{ F_DUPFD,	"F_DUPFD"	},
58	{ F_GETFD,	"F_GETFD"	},
59	{ F_SETFD,	"F_SETFD"	},
60	{ F_GETFL,	"F_GETFL"	},
61	{ F_SETFL,	"F_SETFL"	},
62	{ F_GETLK,	"F_GETLK"	},
63	{ F_SETLK,	"F_SETLK"	},
64	{ F_SETLKW,	"F_SETLKW"	},
65	{ F_GETOWN,	"F_GETOWN"	},
66	{ F_SETOWN,	"F_SETOWN"	},
67#ifdef F_RSETLK
68	{ F_RSETLK,	"F_RSETLK"	},
69#endif
70#ifdef F_RSETLKW
71	{ F_RSETLKW,	"F_RSETLKW"	},
72#endif
73#ifdef F_RGETLK
74	{ F_RGETLK,	"F_RGETLK"	},
75#endif
76#ifdef F_CNVT
77	{ F_CNVT,	"F_CNVT"	},
78#endif
79#ifdef F_SETSIG
80	{ F_SETSIG,	"F_SETSIG"	},
81#endif
82#ifdef F_GETSIG
83	{ F_GETSIG,	"F_GETSIG"	},
84#endif
85#ifdef F_CHKFL
86	{ F_CHKFL,	"F_CHKFL"	},
87#endif
88#ifdef F_DUP2FD
89	{ F_DUP2FD,	"F_DUP2FD"	},
90#endif
91#ifdef F_ALLOCSP
92	{ F_ALLOCSP,	"F_ALLOCSP"	},
93#endif
94#ifdef F_ISSTREAM
95	{ F_ISSTREAM,	"F_ISSTREAM"	},
96#endif
97#ifdef F_PRIV
98	{ F_PRIV,	"F_PRIV"	},
99#endif
100#ifdef F_NPRIV
101	{ F_NPRIV,	"F_NPRIV"	},
102#endif
103#ifdef F_QUOTACL
104	{ F_QUOTACL,	"F_QUOTACL"	},
105#endif
106#ifdef F_BLOCKS
107	{ F_BLOCKS,	"F_BLOCKS"	},
108#endif
109#ifdef F_BLKSIZE
110	{ F_BLKSIZE,	"F_BLKSIZE"	},
111#endif
112#ifdef F_GETOWN
113	{ F_GETOWN,	"F_GETOWN"	},
114#endif
115#ifdef F_SETOWN
116	{ F_SETOWN,	"F_SETOWN"	},
117#endif
118#ifdef F_REVOKE
119	{ F_REVOKE,	"F_REVOKE"	},
120#endif
121#ifdef F_SETLK
122	{ F_SETLK,	"F_SETLK"	},
123#endif
124#ifdef F_SETLKW
125	{ F_SETLKW,	"F_SETLKW"	},
126#endif
127#ifdef F_FREESP
128	{ F_FREESP,	"F_FREESP"	},
129#endif
130#ifdef F_GETLK
131	{ F_GETLK,	"F_GETLK"	},
132#endif
133#ifdef F_SETLK64
134	{ F_SETLK64,	"F_SETLK64"	},
135#endif
136#ifdef F_SETLKW64
137	{ F_SETLKW64,	"F_SETLKW64"	},
138#endif
139#ifdef F_FREESP64
140	{ F_FREESP64,	"F_FREESP64"	},
141#endif
142#ifdef F_GETLK64
143	{ F_GETLK64,	"F_GETLK64"	},
144#endif
145#ifdef F_SHARE
146	{ F_SHARE,	"F_SHARE"	},
147#endif
148#ifdef F_UNSHARE
149	{ F_UNSHARE,	"F_UNSHARE"	},
150#endif
151#ifdef F_SETLEASE
152	{ F_SETLEASE,	"F_SETLEASE"	},
153#endif
154#ifdef F_GETLEASE
155	{ F_GETLEASE,	"F_GETLEASE"	},
156#endif
157#ifdef F_NOTIFY
158	{ F_NOTIFY,	"F_NOTIFY"	},
159#endif
160#ifdef F_DUPFD_CLOEXEC
161	{ F_DUPFD_CLOEXEC,"F_DUPFD_CLOEXEC"},
162#endif
163	{ 0,		NULL		},
164};
165
166static const struct xlat fdflags[] = {
167#ifdef FD_CLOEXEC
168	{ FD_CLOEXEC,	"FD_CLOEXEC"	},
169#endif
170	{ 0,		NULL		},
171};
172
173#ifdef LOCK_SH
174
175static const struct xlat flockcmds[] = {
176	{ LOCK_SH,	"LOCK_SH"	},
177	{ LOCK_EX,	"LOCK_EX"	},
178	{ LOCK_NB,	"LOCK_NB"	},
179	{ LOCK_UN,	"LOCK_UN"	},
180	{ 0,		NULL		},
181};
182
183#endif /* LOCK_SH */
184
185static const struct xlat lockfcmds[] = {
186	{ F_RDLCK,	"F_RDLCK"	},
187	{ F_WRLCK,	"F_WRLCK"	},
188	{ F_UNLCK,	"F_UNLCK"	},
189#ifdef F_EXLCK
190	{ F_EXLCK,	"F_EXLCK"	},
191#endif
192#ifdef F_SHLCK
193	{ F_SHLCK,	"F_SHLCK"	},
194#endif
195	{ 0,		NULL		},
196};
197
198#ifdef F_NOTIFY
199static const struct xlat notifyflags[] = {
200#ifdef DN_ACCESS
201	{ DN_ACCESS,	"DN_ACCESS"	},
202#endif
203#ifdef DN_MODIFY
204	{ DN_MODIFY,	"DN_MODIFY"	},
205#endif
206#ifdef DN_CREATE
207	{ DN_CREATE,	"DN_CREATE"	},
208#endif
209#ifdef DN_DELETE
210	{ DN_DELETE,	"DN_DELETE"	},
211#endif
212#ifdef DN_RENAME
213	{ DN_RENAME,	"DN_RENAME"	},
214#endif
215#ifdef DN_ATTRIB
216	{ DN_ATTRIB,	"DN_ATTRIB"	},
217#endif
218#ifdef DN_MULTISHOT
219	{ DN_MULTISHOT,	"DN_MULTISHOT"	},
220#endif
221	{ 0,		NULL		},
222};
223#endif
224
225static const struct xlat whence[] = {
226	{ SEEK_SET,	"SEEK_SET"	},
227	{ SEEK_CUR,	"SEEK_CUR"	},
228	{ SEEK_END,	"SEEK_END"	},
229	{ 0,		NULL		},
230};
231
232#ifndef HAVE_LONG_LONG_OFF_T
233/* fcntl/lockf */
234static void
235printflock(struct tcb *tcp, long addr, int getlk)
236{
237	struct flock fl;
238
239#if SUPPORTED_PERSONALITIES > 1
240	if (personality_wordsize[current_personality] != sizeof(fl.l_start)) {
241		if (personality_wordsize[current_personality] == 4) {
242			/* 32-bit x86 app on x86_64 and similar cases */
243			struct {
244				short int l_type;
245				short int l_whence;
246				int32_t l_start; /* off_t */
247				int32_t l_len; /* off_t */
248				int32_t l_pid; /* pid_t */
249			} fl32;
250			if (umove(tcp, addr, &fl32) < 0) {
251				tprints("{...}");
252				return;
253			}
254			fl.l_type = fl32.l_type;
255			fl.l_whence = fl32.l_whence;
256			fl.l_start = fl32.l_start;
257			fl.l_len = fl32.l_len;
258			fl.l_pid = fl32.l_pid;
259		} else {
260			/* let people know we have a problem here */
261			tprintf("{ <decode error: unsupported wordsize %d> }",
262				personality_wordsize[current_personality]);
263			return;
264		}
265	} else
266#endif
267	{
268		if (umove(tcp, addr, &fl) < 0) {
269			tprints("{...}");
270			return;
271		}
272	}
273	tprints("{type=");
274	printxval(lockfcmds, fl.l_type, "F_???");
275	tprints(", whence=");
276	printxval(whence, fl.l_whence, "SEEK_???");
277	tprintf(", start=%ld, len=%ld", fl.l_start, fl.l_len);
278	if (getlk)
279		tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
280	else
281		tprints("}");
282}
283#endif
284
285#if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_T
286/* fcntl/lockf */
287static void
288printflock64(struct tcb *tcp, long addr, int getlk)
289{
290	struct flock64 fl;
291
292	if (umove(tcp, addr, &fl) < 0) {
293		tprints("{...}");
294		return;
295	}
296	tprints("{type=");
297	printxval(lockfcmds, fl.l_type, "F_???");
298	tprints(", whence=");
299	printxval(whence, fl.l_whence, "SEEK_???");
300	tprintf(", start=%lld, len=%lld", (long long) fl.l_start, (long long) fl.l_len);
301	if (getlk)
302		tprintf(", pid=%lu}", (unsigned long) fl.l_pid);
303	else
304		tprints("}");
305}
306#endif
307
308int
309sys_fcntl(struct tcb *tcp)
310{
311	if (entering(tcp)) {
312		printfd(tcp, tcp->u_arg[0]);
313		tprints(", ");
314		printxval(fcntlcmds, tcp->u_arg[1], "F_???");
315		switch (tcp->u_arg[1]) {
316		case F_SETFD:
317			tprints(", ");
318			printflags(fdflags, tcp->u_arg[2], "FD_???");
319			break;
320		case F_SETOWN: case F_DUPFD:
321#ifdef F_DUPFD_CLOEXEC
322		case F_DUPFD_CLOEXEC:
323#endif
324			tprintf(", %ld", tcp->u_arg[2]);
325			break;
326		case F_SETFL:
327			tprints(", ");
328			tprint_open_modes(tcp->u_arg[2]);
329			break;
330		case F_SETLK: case F_SETLKW:
331#ifdef F_FREESP
332		case F_FREESP:
333#endif
334			tprints(", ");
335			printflock(tcp, tcp->u_arg[2], 0);
336			break;
337#if _LFS64_LARGEFILE
338#ifdef F_FREESP64
339		case F_FREESP64:
340#endif
341		/* Linux glibc defines SETLK64 as SETLK,
342		   even though the kernel has different values - as does Solaris. */
343#if defined(F_SETLK64) && F_SETLK64 + 0 != F_SETLK
344		case F_SETLK64:
345#endif
346#if defined(F_SETLKW64) && F_SETLKW64 + 0 != F_SETLKW
347		case F_SETLKW64:
348#endif
349			tprints(", ");
350			printflock64(tcp, tcp->u_arg[2], 0);
351			break;
352#endif
353#ifdef F_NOTIFY
354		case F_NOTIFY:
355			tprints(", ");
356			printflags(notifyflags, tcp->u_arg[2], "DN_???");
357			break;
358#endif
359#ifdef F_SETLEASE
360		case F_SETLEASE:
361			tprints(", ");
362			printxval(lockfcmds, tcp->u_arg[2], "F_???");
363			break;
364#endif
365		}
366	}
367	else {
368		switch (tcp->u_arg[1]) {
369		case F_DUPFD:
370#ifdef F_DUPFD_CLOEXEC
371		case F_DUPFD_CLOEXEC:
372#endif
373		case F_SETFD: case F_SETFL:
374		case F_SETLK: case F_SETLKW:
375		case F_SETOWN: case F_GETOWN:
376#ifdef F_NOTIFY
377		case F_NOTIFY:
378#endif
379#ifdef F_SETLEASE
380		case F_SETLEASE:
381#endif
382			break;
383		case F_GETFD:
384			if (syserror(tcp) || tcp->u_rval == 0)
385				return 0;
386			tcp->auxstr = sprintflags("flags ", fdflags, tcp->u_rval);
387			return RVAL_HEX|RVAL_STR;
388		case F_GETFL:
389			if (syserror(tcp))
390				return 0;
391			tcp->auxstr = sprint_open_modes(tcp->u_rval);
392			return RVAL_HEX|RVAL_STR;
393		case F_GETLK:
394			tprints(", ");
395			printflock(tcp, tcp->u_arg[2], 1);
396			break;
397#if _LFS64_LARGEFILE
398#if defined(F_GETLK64) && F_GETLK64+0 != F_GETLK
399		case F_GETLK64:
400#endif
401			tprints(", ");
402			printflock64(tcp, tcp->u_arg[2], 1);
403			break;
404#endif
405#ifdef F_GETLEASE
406		case F_GETLEASE:
407			if (syserror(tcp))
408				return 0;
409			tcp->auxstr = xlookup(lockfcmds, tcp->u_rval);
410			return RVAL_HEX|RVAL_STR;
411#endif
412		default:
413			tprintf(", %#lx", tcp->u_arg[2]);
414			break;
415		}
416	}
417	return 0;
418}
419
420#ifdef LOCK_SH
421
422int
423sys_flock(struct tcb *tcp)
424{
425	if (entering(tcp)) {
426		printfd(tcp, tcp->u_arg[0]);
427		tprints(", ");
428		printflags(flockcmds, tcp->u_arg[1], "LOCK_???");
429	}
430	return 0;
431}
432#endif /* LOCK_SH */
433
434int
435sys_close(struct tcb *tcp)
436{
437	if (entering(tcp)) {
438		printfd(tcp, tcp->u_arg[0]);
439	}
440	return 0;
441}
442
443int
444sys_dup(struct tcb *tcp)
445{
446	if (entering(tcp)) {
447		printfd(tcp, tcp->u_arg[0]);
448	}
449	return 0;
450}
451
452static int
453do_dup2(struct tcb *tcp, int flags_arg)
454{
455	if (entering(tcp)) {
456		printfd(tcp, tcp->u_arg[0]);
457		tprints(", ");
458		printfd(tcp, tcp->u_arg[1]);
459		if (flags_arg >= 0) {
460			tprints(", ");
461			printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
462		}
463	}
464	return 0;
465}
466
467int
468sys_dup2(struct tcb *tcp)
469{
470	return do_dup2(tcp, -1);
471}
472
473#ifdef LINUX
474int
475sys_dup3(struct tcb *tcp)
476{
477	return do_dup2(tcp, 2);
478}
479#endif
480
481#if defined(ALPHA) || defined(FREEBSD) || defined(SUNOS4)
482int
483sys_getdtablesize(struct tcb *tcp)
484{
485	return 0;
486}
487#endif /* ALPHA || FREEBSD || SUNOS4 */
488
489static int
490decode_select(struct tcb *tcp, long *args, enum bitness_t bitness)
491{
492	int i, j, nfds;
493	unsigned int fdsize = ((((args[0] + 7) / 8) + sizeof(long) - 1)
494			       & -sizeof(long));
495	fd_set *fds;
496	const char *sep;
497	long arg;
498
499	if (entering(tcp)) {
500		fds = malloc(fdsize);
501		if (!fds)
502			die_out_of_memory();
503		nfds = args[0];
504		tprintf("%d", nfds);
505		for (i = 0; i < 3; i++) {
506			arg = args[i+1];
507			if (arg == 0) {
508				tprints(", NULL");
509				continue;
510			}
511			if (!verbose(tcp)) {
512				tprintf(", %#lx", arg);
513				continue;
514			}
515			if (umoven(tcp, arg, fdsize, (char *) fds) < 0) {
516				tprints(", [?]");
517				continue;
518			}
519			tprints(", [");
520			for (j = 0, sep = ""; j < nfds; j++) {
521				if (FD_ISSET(j, fds)) {
522					tprints(sep);
523					printfd(tcp, j);
524					sep = " ";
525				}
526			}
527			tprints("]");
528		}
529		free(fds);
530		tprints(", ");
531		printtv_bitness(tcp, args[4], bitness, 0);
532	}
533	else {
534		static char outstr[1024];
535		char *outptr;
536#define end_outstr (outstr + sizeof(outstr))
537		const char *sep;
538
539		if (syserror(tcp))
540			return 0;
541
542		nfds = tcp->u_rval;
543		if (nfds == 0) {
544			tcp->auxstr = "Timeout";
545			return RVAL_STR;
546		}
547
548		fds = malloc(fdsize);
549		if (!fds)
550			die_out_of_memory();
551
552		tcp->auxstr = outstr;
553		outptr = outstr;
554		sep = "";
555		for (i = 0; i < 3; i++) {
556			int first = 1;
557
558			arg = args[i+1];
559			if (!arg || umoven(tcp, arg, fdsize, (char *) fds) < 0)
560				continue;
561			for (j = 0; j < args[0]; j++) {
562				if (FD_ISSET(j, fds)) {
563					/* +2 chars needed at the end: ']',NUL */
564					if (outptr < end_outstr - (sizeof(", except [") + sizeof(int)*3 + 2)) {
565						if (first) {
566							outptr += sprintf(outptr, "%s%s [%u",
567								sep,
568								i == 0 ? "in" : i == 1 ? "out" : "except",
569								j
570							);
571							first = 0;
572							sep = ", ";
573						}
574						else {
575							outptr += sprintf(outptr, " %u", j);
576						}
577					}
578					nfds--;
579				}
580			}
581			if (outptr != outstr)
582				*outptr++ = ']';
583			if (nfds == 0)
584				break;
585		}
586		free(fds);
587#ifdef LINUX
588		/* This contains no useful information on SunOS.  */
589		if (args[4]) {
590			if (outptr < end_outstr - 128) {
591				outptr += sprintf(outptr, "%sleft ", sep);
592				outptr = sprinttv(tcp, args[4], bitness, outptr);
593			}
594		}
595#endif /* LINUX */
596		*outptr = '\0';
597		return RVAL_STR;
598#undef end_outstr
599	}
600	return 0;
601}
602
603#ifdef LINUX
604
605int
606sys_oldselect(struct tcb *tcp)
607{
608	long args[5];
609
610	if (umoven(tcp, tcp->u_arg[0], sizeof args, (char *) args) < 0) {
611		tprints("[...]");
612		return 0;
613	}
614	return decode_select(tcp, args, BITNESS_CURRENT);
615}
616
617#ifdef ALPHA
618int
619sys_osf_select(struct tcb *tcp)
620{
621	long *args = tcp->u_arg;
622	return decode_select(tcp, args, BITNESS_32);
623}
624#endif
625
626static const struct xlat epollctls[] = {
627#ifdef EPOLL_CTL_ADD
628	{ EPOLL_CTL_ADD,	"EPOLL_CTL_ADD"	},
629#endif
630#ifdef EPOLL_CTL_MOD
631	{ EPOLL_CTL_MOD,	"EPOLL_CTL_MOD"	},
632#endif
633#ifdef EPOLL_CTL_DEL
634	{ EPOLL_CTL_DEL,	"EPOLL_CTL_DEL"	},
635#endif
636	{ 0,			NULL		}
637};
638
639static const struct xlat epollevents[] = {
640#ifdef EPOLLIN
641	{ EPOLLIN,	"EPOLLIN"	},
642#endif
643#ifdef EPOLLPRI
644	{ EPOLLPRI,	"EPOLLPRI"	},
645#endif
646#ifdef EPOLLOUT
647	{ EPOLLOUT,	"EPOLLOUT"	},
648#endif
649#ifdef EPOLLRDNORM
650	{ EPOLLRDNORM,	"EPOLLRDNORM"	},
651#endif
652#ifdef EPOLLRDBAND
653	{ EPOLLRDBAND,	"EPOLLRDBAND"	},
654#endif
655#ifdef EPOLLWRNORM
656	{ EPOLLWRNORM,	"EPOLLWRNORM"	},
657#endif
658#ifdef EPOLLWRBAND
659	{ EPOLLWRBAND,	"EPOLLWRBAND"	},
660#endif
661#ifdef EPOLLMSG
662	{ EPOLLMSG,	"EPOLLMSG"	},
663#endif
664#ifdef EPOLLERR
665	{ EPOLLERR,	"EPOLLERR"	},
666#endif
667#ifdef EPOLLHUP
668	{ EPOLLHUP,	"EPOLLHUP"	},
669#endif
670#ifdef EPOLLONESHOT
671	{ EPOLLONESHOT,	"EPOLLONESHOT"	},
672#endif
673#ifdef EPOLLET
674	{ EPOLLET,	"EPOLLET"	},
675#endif
676	{ 0,		NULL		}
677};
678
679int
680sys_epoll_create(struct tcb *tcp)
681{
682	if (entering(tcp))
683		tprintf("%ld", tcp->u_arg[0]);
684	return 0;
685}
686
687int
688sys_epoll_create1(struct tcb *tcp)
689{
690	if (entering(tcp))
691		printflags(open_mode_flags, tcp->u_arg[0], "O_???");
692	return 0;
693}
694
695#ifdef HAVE_SYS_EPOLL_H
696static void
697print_epoll_event(struct epoll_event *ev)
698{
699	tprints("{");
700	printflags(epollevents, ev->events, "EPOLL???");
701	/* We cannot know what format the program uses, so print u32 and u64
702	   which will cover every value.  */
703	tprintf(", {u32=%" PRIu32 ", u64=%" PRIu64 "}}",
704		ev->data.u32, ev->data.u64);
705}
706#endif
707
708int
709sys_epoll_ctl(struct tcb *tcp)
710{
711	if (entering(tcp)) {
712		printfd(tcp, tcp->u_arg[0]);
713		tprints(", ");
714		printxval(epollctls, tcp->u_arg[1], "EPOLL_CTL_???");
715		tprints(", ");
716		printfd(tcp, tcp->u_arg[2]);
717		tprints(", ");
718		if (tcp->u_arg[3] == 0)
719			tprints("NULL");
720		else {
721#ifdef HAVE_SYS_EPOLL_H
722			struct epoll_event ev;
723			if (umove(tcp, tcp->u_arg[3], &ev) == 0)
724				print_epoll_event(&ev);
725			else
726#endif
727				tprints("{...}");
728		}
729	}
730	return 0;
731}
732
733static void
734epoll_wait_common(struct tcb *tcp)
735{
736	if (entering(tcp)) {
737		printfd(tcp, tcp->u_arg[0]);
738		tprints(", ");
739	} else {
740		if (syserror(tcp))
741			tprintf("%lx", tcp->u_arg[1]);
742		else if (tcp->u_rval == 0)
743			tprints("{}");
744		else {
745#ifdef HAVE_SYS_EPOLL_H
746			struct epoll_event ev, *start, *cur, *end;
747			int failed = 0;
748
749			tprints("{");
750			start = (struct epoll_event *) tcp->u_arg[1];
751			end = start + tcp->u_rval;
752			for (cur = start; cur < end; ++cur) {
753				if (cur > start)
754					tprints(", ");
755				if (umove(tcp, (long) cur, &ev) == 0)
756					print_epoll_event(&ev);
757				else {
758					tprints("?");
759					failed = 1;
760					break;
761				}
762			}
763			tprints("}");
764			if (failed)
765				tprintf(" %#lx", (long) start);
766#else
767			tprints("{...}");
768#endif
769		}
770		tprintf(", %ld, %ld", tcp->u_arg[2], tcp->u_arg[3]);
771	}
772}
773
774int
775sys_epoll_wait(struct tcb *tcp)
776{
777	epoll_wait_common(tcp);
778	return 0;
779}
780
781int
782sys_epoll_pwait(struct tcb *tcp)
783{
784	epoll_wait_common(tcp);
785	if (exiting(tcp)) {
786		tprints(", ");
787		print_sigset(tcp, tcp->u_arg[4], 0);
788	}
789	return 0;
790}
791
792int
793sys_io_setup(struct tcb *tcp)
794{
795	if (entering(tcp))
796		tprintf("%ld, ", tcp->u_arg[0]);
797	else {
798		if (syserror(tcp))
799			tprintf("0x%0lx", tcp->u_arg[1]);
800		else {
801			unsigned long user_id;
802			if (umove(tcp, tcp->u_arg[1], &user_id) == 0)
803				tprintf("{%lu}", user_id);
804			else
805				tprints("{...}");
806		}
807	}
808	return 0;
809}
810
811int
812sys_io_destroy(struct tcb *tcp)
813{
814	if (entering(tcp))
815		tprintf("%lu", tcp->u_arg[0]);
816	return 0;
817}
818
819#ifdef HAVE_LIBAIO_H
820
821enum iocb_sub {
822	SUB_NONE, SUB_COMMON, SUB_POLL, SUB_VECTOR
823};
824
825static const char *
826iocb_cmd_lookup(unsigned cmd, enum iocb_sub *sub)
827{
828	static char buf[sizeof("%u /* SUB_??? */") + sizeof(int)*3];
829	static const struct {
830		const char *name;
831		enum iocb_sub sub;
832	} cmds[] = {
833		{ "pread", SUB_COMMON },
834		{ "pwrite", SUB_COMMON },
835		{ "fsync", SUB_NONE },
836		{ "fdsync", SUB_NONE },
837		{ "op4", SUB_NONE },
838		{ "poll", SUB_POLL },
839		{ "noop", SUB_NONE },
840		{ "preadv", SUB_VECTOR },
841		{ "pwritev", SUB_VECTOR },
842	};
843
844	if (cmd < ARRAY_SIZE(cmds)) {
845		*sub = cmds[cmd].sub;
846		return cmds[cmd].name;
847	}
848	*sub = SUB_NONE;
849	sprintf(buf, "%u /* SUB_??? */", cmd);
850	return buf;
851}
852
853/* Not defined in libaio.h */
854#ifndef IOCB_RESFD
855# define IOCB_RESFD (1 << 0)
856#endif
857
858static void
859print_common_flags(struct iocb *iocb)
860{
861	if (iocb->u.c.flags & IOCB_RESFD)
862		tprintf("resfd=%d, ", iocb->u.c.resfd);
863	if (iocb->u.c.flags & ~IOCB_RESFD)
864		tprintf("flags=%x, ", iocb->u.c.flags);
865}
866
867#endif /* HAVE_LIBAIO_H */
868
869int
870sys_io_submit(struct tcb *tcp)
871{
872	long nr;
873	if (entering(tcp)) {
874		tprintf("%lu, %ld, ", tcp->u_arg[0], tcp->u_arg[1]);
875		nr = tcp->u_arg[1];
876		/* and if nr is negative? */
877		if (nr == 0)
878			tprints("{}");
879		else {
880#ifdef HAVE_LIBAIO_H
881			long i;
882			struct iocb *iocbp, **iocbs = (void *)tcp->u_arg[2];
883
884			for (i = 0; i < nr; i++, iocbs++) {
885				enum iocb_sub sub;
886				struct iocb iocb;
887				if (i == 0)
888					tprints("{");
889				else
890					tprints(", ");
891
892				if (umove(tcp, (unsigned long)iocbs, &iocbp) ||
893				    umove(tcp, (unsigned long)iocbp, &iocb)) {
894					tprints("{...}");
895					continue;
896				}
897				tprints("{");
898				if (iocb.data)
899					tprintf("data:%p, ", iocb.data);
900				if (iocb.key)
901					tprintf("key:%u, ", iocb.key);
902				tprintf("%s, ", iocb_cmd_lookup(iocb.aio_lio_opcode, &sub));
903				if (iocb.aio_reqprio)
904					tprintf("reqprio:%d, ", iocb.aio_reqprio);
905				tprintf("filedes:%d", iocb.aio_fildes);
906				switch (sub) {
907				case SUB_COMMON:
908					if (iocb.aio_lio_opcode == IO_CMD_PWRITE) {
909						tprints(", str:");
910						printstr(tcp, (unsigned long)iocb.u.c.buf,
911							 iocb.u.c.nbytes);
912					} else {
913						tprintf(", buf:%p", iocb.u.c.buf);
914					}
915					tprintf(", nbytes:%lu, offset:%llx",
916						iocb.u.c.nbytes,
917						iocb.u.c.offset);
918					print_common_flags(&iocb);
919					break;
920				case SUB_VECTOR:
921					tprintf(", %llx, ", iocb.u.v.offset);
922					print_common_flags(&iocb);
923					tprint_iov(tcp, iocb.u.v.nr,
924						   (unsigned long)iocb.u.v.vec,
925						   iocb.aio_lio_opcode == IO_CMD_PWRITEV);
926					break;
927				case SUB_POLL:
928					tprintf(", %x", iocb.u.poll.events);
929					break;
930				case SUB_NONE:
931				        break;
932				}
933				tprints("}");
934			}
935			if (i)
936				tprints("}");
937#else
938#warning "libaio-devel is not available => no io_submit decoding"
939			tprintf("%#lx", tcp->u_arg[2]);
940#endif
941		}
942	}
943	return 0;
944}
945
946int
947sys_io_cancel(struct tcb *tcp)
948{
949	if (entering(tcp)) {
950#ifdef HAVE_LIBAIO_H
951		struct iocb iocb;
952#endif
953		tprintf("%lu, ", tcp->u_arg[0]);
954#ifdef HAVE_LIBAIO_H
955		if (umove(tcp, tcp->u_arg[1], &iocb) == 0) {
956			tprintf("{%p, %u, %hu, %hu, %d}, ",
957				iocb.data, iocb.key,
958				iocb.aio_lio_opcode,
959				iocb.aio_reqprio, iocb.aio_fildes);
960		} else
961#endif
962			tprints("{...}, ");
963	} else {
964		if (tcp->u_rval < 0)
965			tprints("{...}");
966		else {
967#ifdef HAVE_LIBAIO_H
968			struct io_event event;
969			if (umove(tcp, tcp->u_arg[2], &event) == 0)
970				tprintf("{%p, %p, %ld, %ld}",
971					event.data, event.obj,
972					event.res, event.res2);
973			else
974#endif
975				tprints("{...}");
976		}
977	}
978	return 0;
979}
980
981int
982sys_io_getevents(struct tcb *tcp)
983{
984	if (entering(tcp)) {
985		tprintf("%ld, %ld, %ld, ", tcp->u_arg[0], tcp->u_arg[1],
986			tcp->u_arg[2]);
987	} else {
988		if (tcp->u_rval == 0) {
989			tprints("{}");
990		} else  {
991#ifdef HAVE_LIBAIO_H
992			struct io_event *events = (void *)tcp->u_arg[3];
993			long i, nr = tcp->u_rval;
994
995			for (i = 0; i < nr; i++, events++) {
996				struct io_event event;
997
998				if (i == 0)
999					tprints("{");
1000				else
1001					tprints(", ");
1002
1003				if (umove(tcp, (unsigned long)events, &event) != 0) {
1004					tprints("{...}");
1005					continue;
1006				}
1007				tprintf("{%p, %p, %ld, %ld}", event.data,
1008					event.obj, event.res, event.res2);
1009			}
1010			tprints("}, ");
1011#else
1012				tprints("{...}");
1013#endif
1014		}
1015
1016		print_timespec(tcp, tcp->u_arg[4]);
1017	}
1018	return 0;
1019}
1020#endif /* LINUX */
1021
1022int
1023sys_select(struct tcb *tcp)
1024{
1025	return decode_select(tcp, tcp->u_arg, BITNESS_CURRENT);
1026}
1027
1028#ifdef LINUX
1029int
1030sys_pselect6(struct tcb *tcp)
1031{
1032	int rc = decode_select(tcp, tcp->u_arg, BITNESS_CURRENT);
1033	if (entering(tcp)) {
1034		struct {
1035			void *ss;
1036			unsigned long len;
1037		} data;
1038		if (umove(tcp, tcp->u_arg[5], &data) < 0)
1039			tprintf(", %#lx", tcp->u_arg[5]);
1040		else {
1041			tprints(", {");
1042			if (data.len < sizeof(long))
1043				tprintf("%#lx", (long)data.ss);
1044			else
1045				print_sigset(tcp, (long)data.ss, 0);
1046			tprintf(", %lu}", data.len);
1047		}
1048	}
1049	return rc;
1050}
1051
1052static int
1053do_eventfd(struct tcb *tcp, int flags_arg)
1054{
1055	if (entering(tcp)) {
1056		tprintf("%lu", tcp->u_arg[0]);
1057		if (flags_arg >= 0) {
1058			tprints(", ");
1059			printflags(open_mode_flags, tcp->u_arg[flags_arg], "O_???");
1060		}
1061	}
1062	return 0;
1063}
1064
1065int
1066sys_eventfd(struct tcb *tcp)
1067{
1068	return do_eventfd(tcp, -1);
1069}
1070
1071int
1072sys_eventfd2(struct tcb *tcp)
1073{
1074	return do_eventfd(tcp, 1);
1075}
1076#endif
1077