blob: e3b497caa1dcfec4cc62780dbe2f4ee803d28134 [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>
Elliott Hughes39bac052017-05-25 16:56:11 -07004 * Copyright (c) 1999-2017 The strace developers.
Dmitry V. Levin811bda62015-07-30 16:49:42 +00005 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "defs.h"
31#include <poll.h>
32
33#include "xlat/pollflags.h"
34
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000035static bool
36print_pollfd(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000037{
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000038 const struct pollfd *fds = elem_buf;
39
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000040 tprints("{fd=");
41 printfd(tcp, fds->fd);
42 if (fds->fd >= 0) {
43 tprints(", events=");
Dmitry V. Levin952d89b2016-05-16 22:03:51 +000044 printflags(pollflags, (unsigned short) fds->events, "POLL???");
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000045 }
46 tprints("}");
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000047
48 return true;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000049}
50
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000051static void
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000052decode_poll_entering(struct tcb *tcp)
Dmitry V. Levin811bda62015-07-30 16:49:42 +000053{
Elliott Hughesd35df492017-02-15 15:19:05 -080054 const kernel_ulong_t addr = tcp->u_arg[0];
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000055 const unsigned int nfds = tcp->u_arg[1];
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000056 struct pollfd fds;
Dmitry V. Levin811bda62015-07-30 16:49:42 +000057
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +000058 print_array(tcp, addr, nfds, &fds, sizeof(fds),
59 umoven_or_printaddr, print_pollfd, 0);
60 tprintf(", %u, ", nfds);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000061}
62
63static int
Elliott Hughesd35df492017-02-15 15:19:05 -080064decode_poll_exiting(struct tcb *const tcp, const kernel_ulong_t pts)
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000065{
66 struct pollfd fds;
67 const unsigned int nfds = tcp->u_arg[1];
68 const unsigned long size = sizeof(fds) * nfds;
Elliott Hughesd35df492017-02-15 15:19:05 -080069 const kernel_ulong_t start = tcp->u_arg[0];
70 const kernel_ulong_t end = start + size;
71 kernel_ulong_t cur;
72 const unsigned int max_printed =
73 abbrev(tcp) ? max_strlen : -1U;
74 unsigned int printed;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000075
76 static char outstr[1024];
77 char *outptr;
78#define end_outstr (outstr + sizeof(outstr))
79
80 if (syserror(tcp))
81 return 0;
82 if (tcp->u_rval == 0) {
83 tcp->auxstr = "Timeout";
84 return RVAL_STR;
85 }
86
87 if (!verbose(tcp) || !start || !nfds ||
88 size / sizeof(fds) != nfds || end < start)
89 return 0;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000090
91 outptr = outstr;
92
Dmitry V. Levin9f612ff2016-02-16 00:44:16 +000093 for (printed = 0, cur = start; cur < end; cur += sizeof(fds)) {
Dmitry V. Levind9fb4502015-07-30 19:46:11 +000094 if (umove(tcp, cur, &fds) < 0) {
95 if (outptr == outstr)
96 *outptr++ = '[';
97 else
98 outptr = stpcpy(outptr, ", ");
Elliott Hughesd35df492017-02-15 15:19:05 -080099 outptr += sprintf(outptr, "%#" PRI_klx, cur);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000100 break;
101 }
102 if (!fds.revents)
103 continue;
104 if (outptr == outstr)
105 *outptr++ = '[';
106 else
107 outptr = stpcpy(outptr, ", ");
Dmitry V. Levin9f612ff2016-02-16 00:44:16 +0000108 if (printed >= max_printed) {
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000109 outptr = stpcpy(outptr, "...");
110 break;
111 }
112
113 static const char fmt[] = "{fd=%d, revents=";
114 char fdstr[sizeof(fmt) + sizeof(int) * 3];
115 sprintf(fdstr, fmt, fds.fd);
116
Dmitry V. Levin6274ecc2016-05-15 14:23:06 +0000117 const char *flagstr = sprintflags("", pollflags,
118 (unsigned short) fds.revents);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000119
Dmitry V. Levin8ef98092016-02-16 00:04:37 +0000120 if (outptr + strlen(fdstr) + strlen(flagstr) + 1 >=
121 end_outstr - (2 + 2 * sizeof(long) + sizeof(", ], ..."))) {
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000122 outptr = stpcpy(outptr, "...");
123 break;
124 }
125 outptr = stpcpy(outptr, fdstr);
126 outptr = stpcpy(outptr, flagstr);
127 *outptr++ = '}';
Dmitry V. Levin9f612ff2016-02-16 00:44:16 +0000128 ++printed;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000129 }
130
131 if (outptr != outstr)
132 *outptr++ = ']';
133
134 *outptr = '\0';
135 if (pts) {
Dmitry V. Levin2950de32015-09-18 17:44:16 +0000136 const char *str = sprint_timespec(tcp, pts);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000137
Dmitry V. Levin2950de32015-09-18 17:44:16 +0000138 if (outptr + sizeof(", left ") + strlen(str) < end_outstr) {
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000139 outptr = stpcpy(outptr, outptr == outstr ? "left " : ", left ");
Dmitry V. Levin2950de32015-09-18 17:44:16 +0000140 outptr = stpcpy(outptr, str);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000141 } else {
142 outptr = stpcpy(outptr, ", ...");
143 }
144 }
145
146 if (outptr == outstr)
147 return 0;
148
149 tcp->auxstr = outstr;
150 return RVAL_STR;
151#undef end_outstr
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000152}
153
154SYS_FUNC(poll)
155{
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000156 if (entering(tcp)) {
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +0000157 decode_poll_entering(tcp);
158 int timeout = tcp->u_arg[2];
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000159
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000160#ifdef INFTIM
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +0000161 if (INFTIM == timeout)
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000162 tprints("INFTIM");
163 else
164#endif
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +0000165 tprintf("%d", timeout);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000166
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +0000167 return 0;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000168 } else {
169 return decode_poll_exiting(tcp, 0);
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000170 }
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000171}
172
173SYS_FUNC(ppoll)
174{
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000175 if (entering(tcp)) {
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +0000176 decode_poll_entering(tcp);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000177
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000178 print_timespec(tcp, tcp->u_arg[2]);
179 tprints(", ");
Elliott Hughesd35df492017-02-15 15:19:05 -0800180 /* NB: kernel requires arg[4] == NSIG_BYTES */
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000181 print_sigset_addr_len(tcp, tcp->u_arg[3], tcp->u_arg[4]);
Elliott Hughesd35df492017-02-15 15:19:05 -0800182 tprintf(", %" PRI_klu, tcp->u_arg[4]);
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000183
Dmitry V. Levinfd2d6a72016-05-07 23:09:09 +0000184 return 0;
Dmitry V. Levind9fb4502015-07-30 19:46:11 +0000185 } else {
186 return decode_poll_exiting(tcp, tcp->u_arg[2]);
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000187 }
Dmitry V. Levin811bda62015-07-30 16:49:42 +0000188}