poll.c revision 811bda62252afb7b14d2d3de092145947ac98650
1/*
2 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
3 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "defs.h"
30#include <poll.h>
31
32#include "xlat/pollflags.h"
33
34static int
35decode_poll(struct tcb *tcp, long pts)
36{
37	struct pollfd fds;
38	unsigned nfds;
39	unsigned long size, start, cur, end, abbrev_end;
40	int failed = 0;
41
42	if (entering(tcp)) {
43		nfds = tcp->u_arg[1];
44		size = sizeof(fds) * nfds;
45		start = tcp->u_arg[0];
46		end = start + size;
47		if (nfds == 0 || size / sizeof(fds) != nfds || end < start) {
48			tprintf("%#lx, %d, ",
49				tcp->u_arg[0], nfds);
50			return 0;
51		}
52		if (abbrev(tcp)) {
53			abbrev_end = start + max_strlen * sizeof(fds);
54			if (abbrev_end < start)
55				abbrev_end = end;
56		} else {
57			abbrev_end = end;
58		}
59		tprints("[");
60		for (cur = start; cur < end; cur += sizeof(fds)) {
61			if (cur > start)
62				tprints(", ");
63			if (cur >= abbrev_end) {
64				tprints("...");
65				break;
66			}
67			if (umoven(tcp, cur, sizeof fds, &fds) < 0) {
68				tprints("?");
69				failed = 1;
70				break;
71			}
72			if (fds.fd < 0) {
73				tprintf("{fd=%d}", fds.fd);
74				continue;
75			}
76			tprints("{fd=");
77			printfd(tcp, fds.fd);
78			tprints(", events=");
79			printflags(pollflags, fds.events, "POLL???");
80			tprints("}");
81		}
82		tprints("]");
83		if (failed)
84			tprintf(" %#lx", start);
85		tprintf(", %d, ", nfds);
86		return 0;
87	} else {
88		static char outstr[1024];
89		char *outptr;
90#define end_outstr (outstr + sizeof(outstr))
91		const char *flagstr;
92
93		if (syserror(tcp))
94			return 0;
95		if (tcp->u_rval == 0) {
96			tcp->auxstr = "Timeout";
97			return RVAL_STR;
98		}
99
100		nfds = tcp->u_arg[1];
101		size = sizeof(fds) * nfds;
102		start = tcp->u_arg[0];
103		end = start + size;
104		if (nfds == 0 || size / sizeof(fds) != nfds || end < start)
105			return 0;
106		if (abbrev(tcp)) {
107			abbrev_end = start + max_strlen * sizeof(fds);
108			if (abbrev_end < start)
109				abbrev_end = end;
110		} else {
111			abbrev_end = end;
112		}
113
114		outptr = outstr;
115
116		for (cur = start; cur < end; cur += sizeof(fds)) {
117			if (umoven(tcp, cur, sizeof fds, &fds) < 0) {
118				if (outptr < end_outstr - 2)
119					*outptr++ = '?';
120				failed = 1;
121				break;
122			}
123			if (!fds.revents)
124				continue;
125			if (outptr == outstr) {
126				*outptr++ = '[';
127			} else {
128				if (outptr < end_outstr - 3)
129					outptr = stpcpy(outptr, ", ");
130			}
131			if (cur >= abbrev_end) {
132				if (outptr < end_outstr - 4)
133					outptr = stpcpy(outptr, "...");
134				break;
135			}
136			if (outptr < end_outstr - (sizeof("{fd=%d, revents=") + sizeof(int)*3) + 1)
137				outptr += sprintf(outptr, "{fd=%d, revents=", fds.fd);
138			flagstr = sprintflags("", pollflags, fds.revents);
139			if (outptr < end_outstr - (strlen(flagstr) + 2)) {
140				outptr = stpcpy(outptr, flagstr);
141				*outptr++ = '}';
142			}
143		}
144		if (failed)
145			return 0;
146
147		if (outptr != outstr /* && outptr < end_outstr - 1 (always true)*/)
148			*outptr++ = ']';
149
150		*outptr = '\0';
151		if (pts) {
152			if (outptr < end_outstr - (10 + TIMESPEC_TEXT_BUFSIZE)) {
153				outptr = stpcpy(outptr, outptr == outstr ? "left " : ", left ");
154				sprint_timespec(outptr, tcp, pts);
155			}
156		}
157
158		if (outptr == outstr)
159			return 0;
160
161		tcp->auxstr = outstr;
162		return RVAL_STR;
163#undef end_outstr
164	}
165}
166
167SYS_FUNC(poll)
168{
169	int rc = decode_poll(tcp, 0);
170	if (entering(tcp)) {
171#ifdef INFTIM
172		if (tcp->u_arg[2] == INFTIM)
173			tprints("INFTIM");
174		else
175#endif
176			tprintf("%ld", tcp->u_arg[2]);
177	}
178	return rc;
179}
180
181SYS_FUNC(ppoll)
182{
183	int rc = decode_poll(tcp, tcp->u_arg[2]);
184	if (entering(tcp)) {
185		print_timespec(tcp, tcp->u_arg[2]);
186		tprints(", ");
187		/* NB: kernel requires arg[4] == NSIG / 8 */
188		print_sigset_addr_len(tcp, tcp->u_arg[3], tcp->u_arg[4]);
189		tprintf(", %lu", tcp->u_arg[4]);
190	}
191	return rc;
192}
193