blob: b66c46d8a0019a7d854166c15a741fef1b4ed6ac [file] [log] [blame]
Dmitry V. Levin811bda62015-07-30 16:49:42 +00001/*
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
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000034static bool
35print_pollfd(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000036{
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000037 const struct pollfd *fds = elem_buf;
38
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000039 tprints("{fd=");
40 printfd(tcp, fds->fd);
41 if (fds->fd >= 0) {
42 tprints(", events=");
Dmitry V. Levin952d89b2016-05-16 22:03:51 +000043 printflags(pollflags, (unsigned short) fds->events, "POLL???");
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000044 }
45 tprints("}");
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000046
47 return true;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000048}
49
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000050static void
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000051decode_poll_entering(struct tcb *tcp)
Dmitry V. Levin811bda62015-07-30 16:49:42 +000052{
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000053 const unsigned long addr = tcp->u_arg[0];
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000054 const unsigned int nfds = tcp->u_arg[1];
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000055 struct pollfd fds;
Dmitry V. Levin811bda62015-07-30 16:49:42 +000056
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000057 print_array(tcp, addr, nfds, &fds, sizeof(fds),
58 umoven_or_printaddr, print_pollfd, 0);
59 tprintf(", %u, ", nfds);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000060}
61
62static int
63decode_poll_exiting(struct tcb *tcp, const long pts)
64{
65 struct pollfd fds;
66 const unsigned int nfds = tcp->u_arg[1];
67 const unsigned long size = sizeof(fds) * nfds;
68 const unsigned long start = tcp->u_arg[0];
69 const unsigned long end = start + size;
Dmitry V. Levin9f612ff2016-02-16 00:44:16 +000070 const unsigned long max_printed =
71 abbrev(tcp) ? max_strlen : (unsigned int) -1;
72 unsigned long printed, cur;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000073
74 static char outstr[1024];
75 char *outptr;
76#define end_outstr (outstr + sizeof(outstr))
77
78 if (syserror(tcp))
79 return 0;
80 if (tcp->u_rval == 0) {
81 tcp->auxstr = "Timeout";
82 return RVAL_STR;
83 }
84
85 if (!verbose(tcp) || !start || !nfds ||
86 size / sizeof(fds) != nfds || end < start)
87 return 0;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000088
89 outptr = outstr;
90
Dmitry V. Levin9f612ff2016-02-16 00:44:16 +000091 for (printed = 0, cur = start; cur < end; cur += sizeof(fds)) {
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000092 if (umove(tcp, cur, &fds) < 0) {
93 if (outptr == outstr)
94 *outptr++ = '[';
95 else
96 outptr = stpcpy(outptr, ", ");
Dmitry V. Levin8ef98092016-02-16 00:04:37 +000097 outptr += sprintf(outptr, "%#lx", cur);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000098 break;
99 }
100 if (!fds.revents)
101 continue;
102 if (outptr == outstr)
103 *outptr++ = '[';
104 else
105 outptr = stpcpy(outptr, ", ");
Dmitry V. Levin9f612ff2016-02-16 00:44:16 +0000106 if (printed >= max_printed) {
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000107 outptr = stpcpy(outptr, "...");
108 break;
109 }
110
111 static const char fmt[] = "{fd=%d, revents=";
112 char fdstr[sizeof(fmt) + sizeof(int) * 3];
113 sprintf(fdstr, fmt, fds.fd);
114
Dmitry V. Levin6274ecc2016-05-15 14:23:06 +0000115 const char *flagstr = sprintflags("", pollflags,
116 (unsigned short) fds.revents);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000117
Dmitry V. Levin8ef98092016-02-16 00:04:37 +0000118 if (outptr + strlen(fdstr) + strlen(flagstr) + 1 >=
119 end_outstr - (2 + 2 * sizeof(long) + sizeof(", ], ..."))) {
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000120 outptr = stpcpy(outptr, "...");
121 break;
122 }
123 outptr = stpcpy(outptr, fdstr);
124 outptr = stpcpy(outptr, flagstr);
125 *outptr++ = '}';
Dmitry V. Levin9f612ff2016-02-16 00:44:16 +0000126 ++printed;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000127 }
128
129 if (outptr != outstr)
130 *outptr++ = ']';
131
132 *outptr = '\0';
133 if (pts) {
Dmitry V. Levin2950de32015-09-18 17:44:16 +0000134 const char *str = sprint_timespec(tcp, pts);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000135
Dmitry V. Levin2950de32015-09-18 17:44:16 +0000136 if (outptr + sizeof(", left ") + strlen(str) < end_outstr) {
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000137 outptr = stpcpy(outptr, outptr == outstr ? "left " : ", left ");
Dmitry V. Levin2950de32015-09-18 17:44:16 +0000138 outptr = stpcpy(outptr, str);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000139 } else {
140 outptr = stpcpy(outptr, ", ...");
141 }
142 }
143
144 if (outptr == outstr)
145 return 0;
146
147 tcp->auxstr = outstr;
148 return RVAL_STR;
149#undef end_outstr
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000150}
151
152SYS_FUNC(poll)
153{
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000154 if (entering(tcp)) {
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +0000155 decode_poll_entering(tcp);
156 int timeout = tcp->u_arg[2];
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000157
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000158#ifdef INFTIM
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +0000159 if (INFTIM == timeout)
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000160 tprints("INFTIM");
161 else
162#endif
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +0000163 tprintf("%d", timeout);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000164
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +0000165 return 0;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000166 } else {
167 return decode_poll_exiting(tcp, 0);
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000168 }
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000169}
170
171SYS_FUNC(ppoll)
172{
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000173 if (entering(tcp)) {
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +0000174 decode_poll_entering(tcp);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000175
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000176 print_timespec(tcp, tcp->u_arg[2]);
177 tprints(", ");
178 /* NB: kernel requires arg[4] == NSIG / 8 */
179 print_sigset_addr_len(tcp, tcp->u_arg[3], tcp->u_arg[4]);
180 tprintf(", %lu", tcp->u_arg[4]);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000181
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +0000182 return 0;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000183 } else {
184 return decode_poll_exiting(tcp, tcp->u_arg[2]);
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000185 }
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000186}