blob: ceb9e2d60fcb116581826c325aa50d62885212ea [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00003 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00004 * 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.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000027 */
28
29#include "defs.h"
Dmitry V. Levind64a7e42013-11-06 01:17:05 +000030#if defined HAVE_POLL_H
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +010031# include <poll.h>
Dmitry V. Levind64a7e42013-11-06 01:17:05 +000032#elif defined HAVE_SYS_POLL_H
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +010033# include <sys/poll.h>
Pavel Machek245a6ac2000-02-01 16:12:33 +000034#endif
Wichert Akkerman42080d82001-04-10 10:32:26 +000035#ifdef HAVE_SYS_CONF_H
Denys Vlasenkoa6d91de2012-03-16 12:02:22 +010036# include <sys/conf.h>
Wichert Akkerman42080d82001-04-10 10:32:26 +000037#endif
Wichert Akkerman42080d82001-04-10 10:32:26 +000038
Denys Vlasenkocc902912013-03-05 16:50:12 +010039/* Who has STREAMS syscalls?
40 * Linux hasn't. Solaris has (had?).
41 * Just in case I miss something, retain in for Sparc...
42 */
43#if defined(SPARC) || defined(SPARC64)
44
45# ifdef HAVE_STROPTS_H
46# include <stropts.h>
47# else
48# define RS_HIPRI 1
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000049struct strbuf {
Denys Vlasenkoadedb512008-12-30 18:47:55 +000050 int maxlen; /* no. of bytes in buffer */
51 int len; /* no. of bytes returned */
Dmitry V. Levin30145dd2010-09-06 22:08:24 +000052 const char *buf; /* pointer to data */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000053};
Denys Vlasenkocc902912013-03-05 16:50:12 +010054# define MORECTL 1
55# define MOREDATA 2
56# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000057
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +000058#include "xlat/msgflags.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000059
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000060static void
Denys Vlasenko12014262011-05-30 14:00:14 +020061printstrbuf(struct tcb *tcp, struct strbuf *sbp, int getting)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000062{
63 if (sbp->maxlen == -1 && getting)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +020064 tprints("{maxlen=-1}");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000065 else {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +020066 tprints("{");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000067 if (getting)
68 tprintf("maxlen=%d, ", sbp->maxlen);
69 tprintf("len=%d, buf=", sbp->len);
Wichert Akkerman2e2553a1999-05-09 00:29:58 +000070 printstr(tcp, (unsigned long) sbp->buf, sbp->len);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +020071 tprints("}");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000072 }
73}
74
75static void
Denys Vlasenkoe7db4652013-03-05 16:17:46 +010076printstrbufarg(struct tcb *tcp, long arg, int getting)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000077{
78 struct strbuf buf;
79
80 if (arg == 0)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +020081 tprints("NULL");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000082 else if (umove(tcp, arg, &buf) < 0)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +020083 tprints("{...}");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000084 else
85 printstrbuf(tcp, &buf, getting);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +020086 tprints(", ");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000087}
88
89int
Denys Vlasenko12014262011-05-30 14:00:14 +020090sys_putmsg(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000091{
92 int i;
93
94 if (entering(tcp)) {
95 /* fd */
96 tprintf("%ld, ", tcp->u_arg[0]);
97 /* control and data */
98 for (i = 1; i < 3; i++)
99 printstrbufarg(tcp, tcp->u_arg[i], 0);
100 /* flags */
Roland McGrathb2dee132005-06-01 19:02:36 +0000101 printflags(msgflags, tcp->u_arg[3], "RS_???");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000102 }
103 return 0;
104}
105
106int
Denys Vlasenko12014262011-05-30 14:00:14 +0200107sys_getmsg(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000108{
109 int i, flags;
110
111 if (entering(tcp)) {
112 /* fd */
113 tprintf("%lu, ", tcp->u_arg[0]);
114 } else {
115 if (syserror(tcp)) {
116 tprintf("%#lx, %#lx, %#lx",
117 tcp->u_arg[1], tcp->u_arg[2], tcp->u_arg[3]);
118 return 0;
119 }
120 /* control and data */
121 for (i = 1; i < 3; i++)
122 printstrbufarg(tcp, tcp->u_arg[i], 1);
123 /* pointer to flags */
124 if (tcp->u_arg[3] == 0)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200125 tprints("NULL");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000126 else if (umove(tcp, tcp->u_arg[3], &flags) < 0)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200127 tprints("[?]");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000128 else {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200129 tprints("[");
Roland McGrathb2dee132005-06-01 19:02:36 +0000130 printflags(msgflags, flags, "RS_???");
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200131 tprints("]");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000132 }
133 /* decode return value */
134 switch (tcp->u_rval) {
135 case MORECTL:
136 tcp->auxstr = "MORECTL";
137 break;
138 case MORECTL|MOREDATA:
139 tcp->auxstr = "MORECTL|MOREDATA";
140 break;
141 case MOREDATA:
142 tcp->auxstr = "MORECTL";
143 break;
144 default:
145 tcp->auxstr = NULL;
146 break;
147 }
148 }
149 return RVAL_HEX | RVAL_STR;
150}
151
Denys Vlasenkocc902912013-03-05 16:50:12 +0100152# if defined SYS_putpmsg || defined SYS_getpmsg
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +0000153#include "xlat/pmsgflags.h"
Denys Vlasenkocc902912013-03-05 16:50:12 +0100154# ifdef SYS_putpmsg
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000155int
Denys Vlasenko12014262011-05-30 14:00:14 +0200156sys_putpmsg(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000157{
158 int i;
159
160 if (entering(tcp)) {
161 /* fd */
162 tprintf("%ld, ", tcp->u_arg[0]);
163 /* control and data */
164 for (i = 1; i < 3; i++)
165 printstrbufarg(tcp, tcp->u_arg[i], 0);
166 /* band */
167 tprintf("%ld, ", tcp->u_arg[3]);
168 /* flags */
Roland McGrathb2dee132005-06-01 19:02:36 +0000169 printflags(pmsgflags, tcp->u_arg[4], "MSG_???");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000170 }
171 return 0;
172}
Denys Vlasenkocc902912013-03-05 16:50:12 +0100173# endif
174# ifdef SYS_getpmsg
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000175int
Denys Vlasenko12014262011-05-30 14:00:14 +0200176sys_getpmsg(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000177{
178 int i, flags;
179
180 if (entering(tcp)) {
181 /* fd */
182 tprintf("%lu, ", tcp->u_arg[0]);
183 } else {
184 if (syserror(tcp)) {
185 tprintf("%#lx, %#lx, %#lx, %#lx", tcp->u_arg[1],
186 tcp->u_arg[2], tcp->u_arg[3], tcp->u_arg[4]);
187 return 0;
188 }
189 /* control and data */
190 for (i = 1; i < 3; i++)
191 printstrbufarg(tcp, tcp->u_arg[i], 1);
192 /* pointer to band */
193 printnum(tcp, tcp->u_arg[3], "%d");
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200194 tprints(", ");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000195 /* pointer to flags */
196 if (tcp->u_arg[4] == 0)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200197 tprints("NULL");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000198 else if (umove(tcp, tcp->u_arg[4], &flags) < 0)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200199 tprints("[?]");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000200 else {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200201 tprints("[");
Roland McGrathb2dee132005-06-01 19:02:36 +0000202 printflags(pmsgflags, flags, "MSG_???");
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200203 tprints("]");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000204 }
205 /* decode return value */
206 switch (tcp->u_rval) {
207 case MORECTL:
208 tcp->auxstr = "MORECTL";
209 break;
210 case MORECTL|MOREDATA:
211 tcp->auxstr = "MORECTL|MOREDATA";
212 break;
213 case MOREDATA:
214 tcp->auxstr = "MORECTL";
215 break;
216 default:
217 tcp->auxstr = NULL;
218 break;
219 }
220 }
221 return RVAL_HEX | RVAL_STR;
222}
Denys Vlasenkocc902912013-03-05 16:50:12 +0100223# endif
224# endif /* getpmsg/putpmsg */
225
226#endif /* STREAMS syscalls support */
227
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000228
Wichert Akkermanfaf72222000-02-19 23:59:03 +0000229#ifdef HAVE_SYS_POLL_H
230
Dmitry V. Levin0ed617b2014-04-25 23:30:54 +0000231#include "xlat/pollflags.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000232
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000233static int
Roland McGrathf17106e2007-11-01 21:49:49 +0000234decode_poll(struct tcb *tcp, long pts)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000235{
Roland McGrathaa524c82005-06-01 19:22:06 +0000236 struct pollfd fds;
237 unsigned nfds;
238 unsigned long size, start, cur, end, abbrev_end;
239 int failed = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000240
Roland McGrathf17106e2007-11-01 21:49:49 +0000241 if (entering(tcp)) {
242 nfds = tcp->u_arg[1];
243 size = sizeof(fds) * nfds;
244 start = tcp->u_arg[0];
245 end = start + size;
246 if (nfds == 0 || size / sizeof(fds) != nfds || end < start) {
247 tprintf("%#lx, %d, ",
248 tcp->u_arg[0], nfds);
249 return 0;
250 }
251 if (abbrev(tcp)) {
252 abbrev_end = start + max_strlen * sizeof(fds);
253 if (abbrev_end < start)
254 abbrev_end = end;
255 } else {
Roland McGrathaa524c82005-06-01 19:22:06 +0000256 abbrev_end = end;
Roland McGrathf17106e2007-11-01 21:49:49 +0000257 }
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200258 tprints("[");
Roland McGrathf17106e2007-11-01 21:49:49 +0000259 for (cur = start; cur < end; cur += sizeof(fds)) {
260 if (cur > start)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200261 tprints(", ");
Roland McGrathf17106e2007-11-01 21:49:49 +0000262 if (cur >= abbrev_end) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200263 tprints("...");
Roland McGrathf17106e2007-11-01 21:49:49 +0000264 break;
265 }
266 if (umoven(tcp, cur, sizeof fds, (char *) &fds) < 0) {
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200267 tprints("?");
Roland McGrathf17106e2007-11-01 21:49:49 +0000268 failed = 1;
269 break;
270 }
271 if (fds.fd < 0) {
272 tprintf("{fd=%d}", fds.fd);
273 continue;
274 }
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200275 tprints("{fd=");
Dmitry V. Levin31382132011-03-04 05:08:02 +0300276 printfd(tcp, fds.fd);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200277 tprints(", events=");
Roland McGrathf17106e2007-11-01 21:49:49 +0000278 printflags(pollflags, fds.events, "POLL???");
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200279 tprints("}");
Roland McGrathf17106e2007-11-01 21:49:49 +0000280 }
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200281 tprints("]");
Roland McGrathf17106e2007-11-01 21:49:49 +0000282 if (failed)
283 tprintf(" %#lx", start);
284 tprintf(", %d, ", nfds);
285 return 0;
Roland McGrathaa524c82005-06-01 19:22:06 +0000286 } else {
Roland McGrathf17106e2007-11-01 21:49:49 +0000287 static char outstr[1024];
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200288 char *outptr;
289#define end_outstr (outstr + sizeof(outstr))
Denys Vlasenkoadedb512008-12-30 18:47:55 +0000290 const char *flagstr;
Roland McGrathf17106e2007-11-01 21:49:49 +0000291
292 if (syserror(tcp))
293 return 0;
294 if (tcp->u_rval == 0) {
295 tcp->auxstr = "Timeout";
296 return RVAL_STR;
297 }
298
299 nfds = tcp->u_arg[1];
300 size = sizeof(fds) * nfds;
301 start = tcp->u_arg[0];
302 end = start + size;
303 if (nfds == 0 || size / sizeof(fds) != nfds || end < start)
304 return 0;
305 if (abbrev(tcp)) {
306 abbrev_end = start + max_strlen * sizeof(fds);
307 if (abbrev_end < start)
308 abbrev_end = end;
309 } else {
310 abbrev_end = end;
311 }
312
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200313 outptr = outstr;
Roland McGrathf17106e2007-11-01 21:49:49 +0000314
315 for (cur = start; cur < end; cur += sizeof(fds)) {
316 if (umoven(tcp, cur, sizeof fds, (char *) &fds) < 0) {
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200317 if (outptr < end_outstr - 2)
318 *outptr++ = '?';
Roland McGrathf17106e2007-11-01 21:49:49 +0000319 failed = 1;
320 break;
321 }
322 if (!fds.revents)
323 continue;
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200324 if (outptr == outstr) {
325 *outptr++ = '[';
Roland McGrathf17106e2007-11-01 21:49:49 +0000326 } else {
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200327 if (outptr < end_outstr - 3)
328 outptr = stpcpy(outptr, ", ");
Roland McGrathf17106e2007-11-01 21:49:49 +0000329 }
330 if (cur >= abbrev_end) {
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200331 if (outptr < end_outstr - 4)
332 outptr = stpcpy(outptr, "...");
Roland McGrathf17106e2007-11-01 21:49:49 +0000333 break;
334 }
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200335 if (outptr < end_outstr - (sizeof("{fd=%d, revents=") + sizeof(int)*3) + 1)
336 outptr += sprintf(outptr, "{fd=%d, revents=", fds.fd);
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200337 flagstr = sprintflags("", pollflags, fds.revents);
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200338 if (outptr < end_outstr - (strlen(flagstr) + 2)) {
339 outptr = stpcpy(outptr, flagstr);
340 *outptr++ = '}';
Roland McGrathf17106e2007-11-01 21:49:49 +0000341 }
342 }
343 if (failed)
344 return 0;
345
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200346 if (outptr != outstr /* && outptr < end_outstr - 1 (always true)*/)
347 *outptr++ = ']';
Roland McGrathf17106e2007-11-01 21:49:49 +0000348
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200349 *outptr = '\0';
Roland McGrathf17106e2007-11-01 21:49:49 +0000350 if (pts) {
Denys Vlasenkoa1d541e2012-01-20 11:04:04 +0100351 if (outptr < end_outstr - (10 + TIMESPEC_TEXT_BUFSIZE)) {
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200352 outptr = stpcpy(outptr, outptr == outstr ? "left " : ", left ");
353 sprint_timespec(outptr, tcp, pts);
354 }
Roland McGrathf17106e2007-11-01 21:49:49 +0000355 }
356
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200357 if (outptr == outstr)
Roland McGrathf17106e2007-11-01 21:49:49 +0000358 return 0;
359
360 tcp->auxstr = outstr;
361 return RVAL_STR;
Denys Vlasenko2fb4db32011-08-31 12:26:03 +0200362#undef end_outstr
Roland McGrathaa524c82005-06-01 19:22:06 +0000363 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000364}
365
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000366int
367sys_poll(struct tcb *tcp)
368{
Roland McGrathf17106e2007-11-01 21:49:49 +0000369 int rc = decode_poll(tcp, 0);
370 if (entering(tcp)) {
Denys Vlasenkocc902912013-03-05 16:50:12 +0100371# ifdef INFTIM
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000372 if (tcp->u_arg[2] == INFTIM)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200373 tprints("INFTIM");
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000374 else
Denys Vlasenkocc902912013-03-05 16:50:12 +0100375# endif
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000376 tprintf("%ld", tcp->u_arg[2]);
377 }
378 return rc;
379}
380
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000381int
382sys_ppoll(struct tcb *tcp)
383{
Roland McGrathf17106e2007-11-01 21:49:49 +0000384 int rc = decode_poll(tcp, tcp->u_arg[2]);
385 if (entering(tcp)) {
Roland McGrath6bc09da2007-11-01 21:50:54 +0000386 print_timespec(tcp, tcp->u_arg[2]);
Denys Vlasenko60fe8c12011-09-01 10:00:28 +0200387 tprints(", ");
Denys Vlasenko5e133aa2013-07-18 17:02:21 +0200388 /* NB: kernel requires arg[4] == NSIG / 8 */
389 print_sigset_addr_len(tcp, tcp->u_arg[3], tcp->u_arg[4]);
Dmitry V. Levin95ebf5a2006-10-13 20:25:12 +0000390 tprintf(", %lu", tcp->u_arg[4]);
391 }
392 return rc;
393}
Roland McGrathaa524c82005-06-01 19:22:06 +0000394
Wichert Akkermanfaf72222000-02-19 23:59:03 +0000395#else /* !HAVE_SYS_POLL_H */
396int
Denys Vlasenko12014262011-05-30 14:00:14 +0200397sys_poll(struct tcb *tcp)
Wichert Akkermanfaf72222000-02-19 23:59:03 +0000398{
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000399 return 0;
Wichert Akkermanfaf72222000-02-19 23:59:03 +0000400}
401#endif