Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 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 | |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 34 | static void |
| 35 | print_pollfd(struct tcb *tcp, const struct pollfd *fds) |
| 36 | { |
| 37 | tprints("{fd="); |
| 38 | printfd(tcp, fds->fd); |
| 39 | if (fds->fd >= 0) { |
| 40 | tprints(", events="); |
| 41 | printflags(pollflags, fds->events, "POLL???"); |
| 42 | } |
| 43 | tprints("}"); |
| 44 | } |
| 45 | |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 46 | static int |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 47 | decode_poll_entering(struct tcb *tcp) |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 48 | { |
| 49 | struct pollfd fds; |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 50 | const unsigned int nfds = tcp->u_arg[1]; |
| 51 | const unsigned long size = sizeof(fds) * nfds; |
| 52 | const unsigned long start = tcp->u_arg[0]; |
| 53 | const unsigned long end = start + size; |
Dmitry V. Levin | 9f612ff | 2016-02-16 00:44:16 +0000 | [diff] [blame] | 54 | const unsigned long max_printed = |
| 55 | abbrev(tcp) ? max_strlen : (unsigned int) -1; |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 56 | |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 57 | if (!verbose(tcp) || !start || !nfds || |
| 58 | size / sizeof(fds) != nfds || end < start) { |
| 59 | printaddr(start); |
| 60 | tprintf(", %u, ", nfds); |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 61 | return 0; |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 62 | } |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 63 | |
Dmitry V. Levin | 9f612ff | 2016-02-16 00:44:16 +0000 | [diff] [blame] | 64 | if (umove(tcp, start, &fds) < 0) { |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 65 | printaddr(start); |
| 66 | tprintf(", %u, ", nfds); |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | tprints("["); |
Dmitry V. Levin | 9f612ff | 2016-02-16 00:44:16 +0000 | [diff] [blame] | 71 | if (max_printed) { |
| 72 | unsigned long printed = 1; |
| 73 | unsigned long cur = start + sizeof(fds); |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 74 | |
Dmitry V. Levin | 9f612ff | 2016-02-16 00:44:16 +0000 | [diff] [blame] | 75 | print_pollfd(tcp, &fds); |
| 76 | for (; cur < end; ++printed, cur += sizeof(fds)) { |
| 77 | tprints(", "); |
| 78 | if (printed >= max_printed) { |
| 79 | tprints("..."); |
| 80 | break; |
| 81 | } |
| 82 | if (umove_or_printaddr(tcp, cur, &fds)) |
| 83 | break; |
| 84 | print_pollfd(tcp, &fds); |
| 85 | |
| 86 | } |
| 87 | } else { |
| 88 | tprints("..."); |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 89 | } |
| 90 | tprintf("], %u, ", nfds); |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int |
| 96 | decode_poll_exiting(struct tcb *tcp, const long pts) |
| 97 | { |
| 98 | struct pollfd fds; |
| 99 | const unsigned int nfds = tcp->u_arg[1]; |
| 100 | const unsigned long size = sizeof(fds) * nfds; |
| 101 | const unsigned long start = tcp->u_arg[0]; |
| 102 | const unsigned long end = start + size; |
Dmitry V. Levin | 9f612ff | 2016-02-16 00:44:16 +0000 | [diff] [blame] | 103 | const unsigned long max_printed = |
| 104 | abbrev(tcp) ? max_strlen : (unsigned int) -1; |
| 105 | unsigned long printed, cur; |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 106 | |
| 107 | static char outstr[1024]; |
| 108 | char *outptr; |
| 109 | #define end_outstr (outstr + sizeof(outstr)) |
| 110 | |
| 111 | if (syserror(tcp)) |
| 112 | return 0; |
| 113 | if (tcp->u_rval == 0) { |
| 114 | tcp->auxstr = "Timeout"; |
| 115 | return RVAL_STR; |
| 116 | } |
| 117 | |
| 118 | if (!verbose(tcp) || !start || !nfds || |
| 119 | size / sizeof(fds) != nfds || end < start) |
| 120 | return 0; |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 121 | |
| 122 | outptr = outstr; |
| 123 | |
Dmitry V. Levin | 9f612ff | 2016-02-16 00:44:16 +0000 | [diff] [blame] | 124 | for (printed = 0, cur = start; cur < end; cur += sizeof(fds)) { |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 125 | if (umove(tcp, cur, &fds) < 0) { |
| 126 | if (outptr == outstr) |
| 127 | *outptr++ = '['; |
| 128 | else |
| 129 | outptr = stpcpy(outptr, ", "); |
Dmitry V. Levin | 8ef9809 | 2016-02-16 00:04:37 +0000 | [diff] [blame] | 130 | outptr += sprintf(outptr, "%#lx", cur); |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 131 | break; |
| 132 | } |
| 133 | if (!fds.revents) |
| 134 | continue; |
| 135 | if (outptr == outstr) |
| 136 | *outptr++ = '['; |
| 137 | else |
| 138 | outptr = stpcpy(outptr, ", "); |
Dmitry V. Levin | 9f612ff | 2016-02-16 00:44:16 +0000 | [diff] [blame] | 139 | if (printed >= max_printed) { |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 140 | outptr = stpcpy(outptr, "..."); |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | static const char fmt[] = "{fd=%d, revents="; |
| 145 | char fdstr[sizeof(fmt) + sizeof(int) * 3]; |
| 146 | sprintf(fdstr, fmt, fds.fd); |
| 147 | |
| 148 | const char *flagstr = sprintflags("", pollflags, fds.revents); |
| 149 | |
Dmitry V. Levin | 8ef9809 | 2016-02-16 00:04:37 +0000 | [diff] [blame] | 150 | if (outptr + strlen(fdstr) + strlen(flagstr) + 1 >= |
| 151 | end_outstr - (2 + 2 * sizeof(long) + sizeof(", ], ..."))) { |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 152 | outptr = stpcpy(outptr, "..."); |
| 153 | break; |
| 154 | } |
| 155 | outptr = stpcpy(outptr, fdstr); |
| 156 | outptr = stpcpy(outptr, flagstr); |
| 157 | *outptr++ = '}'; |
Dmitry V. Levin | 9f612ff | 2016-02-16 00:44:16 +0000 | [diff] [blame] | 158 | ++printed; |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | if (outptr != outstr) |
| 162 | *outptr++ = ']'; |
| 163 | |
| 164 | *outptr = '\0'; |
| 165 | if (pts) { |
Dmitry V. Levin | 2950de3 | 2015-09-18 17:44:16 +0000 | [diff] [blame] | 166 | const char *str = sprint_timespec(tcp, pts); |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 167 | |
Dmitry V. Levin | 2950de3 | 2015-09-18 17:44:16 +0000 | [diff] [blame] | 168 | if (outptr + sizeof(", left ") + strlen(str) < end_outstr) { |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 169 | outptr = stpcpy(outptr, outptr == outstr ? "left " : ", left "); |
Dmitry V. Levin | 2950de3 | 2015-09-18 17:44:16 +0000 | [diff] [blame] | 170 | outptr = stpcpy(outptr, str); |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 171 | } else { |
| 172 | outptr = stpcpy(outptr, ", ..."); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if (outptr == outstr) |
| 177 | return 0; |
| 178 | |
| 179 | tcp->auxstr = outstr; |
| 180 | return RVAL_STR; |
| 181 | #undef end_outstr |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | SYS_FUNC(poll) |
| 185 | { |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 186 | if (entering(tcp)) { |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 187 | int rc = decode_poll_entering(tcp); |
| 188 | |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 189 | #ifdef INFTIM |
Dmitry V. Levin | c18b002 | 2015-07-30 16:54:31 +0000 | [diff] [blame] | 190 | if (INFTIM == (int) tcp->u_arg[2]) |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 191 | tprints("INFTIM"); |
| 192 | else |
| 193 | #endif |
Dmitry V. Levin | c18b002 | 2015-07-30 16:54:31 +0000 | [diff] [blame] | 194 | tprintf("%d", (int) tcp->u_arg[2]); |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 195 | |
| 196 | return rc; |
| 197 | } else { |
| 198 | return decode_poll_exiting(tcp, 0); |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 199 | } |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | SYS_FUNC(ppoll) |
| 203 | { |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 204 | if (entering(tcp)) { |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 205 | int rc = decode_poll_entering(tcp); |
| 206 | |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 207 | print_timespec(tcp, tcp->u_arg[2]); |
| 208 | tprints(", "); |
| 209 | /* NB: kernel requires arg[4] == NSIG / 8 */ |
| 210 | print_sigset_addr_len(tcp, tcp->u_arg[3], tcp->u_arg[4]); |
| 211 | tprintf(", %lu", tcp->u_arg[4]); |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 212 | |
| 213 | return rc; |
| 214 | } else { |
| 215 | return decode_poll_exiting(tcp, tcp->u_arg[2]); |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 216 | } |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 217 | } |