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> |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 4 | * Copyright (c) 1999-2018 The strace developers. |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 5 | * 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> |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 32 | #include "xstring.h" |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 33 | |
| 34 | #include "xlat/pollflags.h" |
| 35 | |
Dmitry V. Levin | fd2d6a7 | 2016-05-07 23:09:09 +0000 | [diff] [blame] | 36 | static bool |
| 37 | print_pollfd(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data) |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 38 | { |
Dmitry V. Levin | fd2d6a7 | 2016-05-07 23:09:09 +0000 | [diff] [blame] | 39 | const struct pollfd *fds = elem_buf; |
| 40 | |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 41 | tprints("{fd="); |
| 42 | printfd(tcp, fds->fd); |
| 43 | if (fds->fd >= 0) { |
| 44 | tprints(", events="); |
Dmitry V. Levin | 952d89b | 2016-05-16 22:03:51 +0000 | [diff] [blame] | 45 | printflags(pollflags, (unsigned short) fds->events, "POLL???"); |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 46 | } |
| 47 | tprints("}"); |
Dmitry V. Levin | fd2d6a7 | 2016-05-07 23:09:09 +0000 | [diff] [blame] | 48 | |
| 49 | return true; |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Dmitry V. Levin | fd2d6a7 | 2016-05-07 23:09:09 +0000 | [diff] [blame] | 52 | static void |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 53 | decode_poll_entering(struct tcb *tcp) |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 54 | { |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 55 | const kernel_ulong_t addr = tcp->u_arg[0]; |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 56 | const unsigned int nfds = tcp->u_arg[1]; |
Dmitry V. Levin | fd2d6a7 | 2016-05-07 23:09:09 +0000 | [diff] [blame] | 57 | struct pollfd fds; |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 58 | |
Dmitry V. Levin | fd2d6a7 | 2016-05-07 23:09:09 +0000 | [diff] [blame] | 59 | print_array(tcp, addr, nfds, &fds, sizeof(fds), |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 60 | tfetch_mem, print_pollfd, 0); |
Dmitry V. Levin | fd2d6a7 | 2016-05-07 23:09:09 +0000 | [diff] [blame] | 61 | tprintf(", %u, ", nfds); |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static int |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 65 | decode_poll_exiting(struct tcb *const tcp, const kernel_ulong_t pts) |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 66 | { |
| 67 | struct pollfd fds; |
| 68 | const unsigned int nfds = tcp->u_arg[1]; |
| 69 | const unsigned long size = sizeof(fds) * nfds; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 70 | const kernel_ulong_t start = tcp->u_arg[0]; |
| 71 | const kernel_ulong_t end = start + size; |
| 72 | kernel_ulong_t cur; |
| 73 | const unsigned int max_printed = |
| 74 | abbrev(tcp) ? max_strlen : -1U; |
| 75 | unsigned int printed; |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 76 | |
| 77 | static char outstr[1024]; |
| 78 | char *outptr; |
| 79 | #define end_outstr (outstr + sizeof(outstr)) |
| 80 | |
| 81 | if (syserror(tcp)) |
| 82 | return 0; |
| 83 | if (tcp->u_rval == 0) { |
| 84 | tcp->auxstr = "Timeout"; |
| 85 | return RVAL_STR; |
| 86 | } |
| 87 | |
| 88 | if (!verbose(tcp) || !start || !nfds || |
| 89 | size / sizeof(fds) != nfds || end < start) |
| 90 | return 0; |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 91 | |
| 92 | outptr = outstr; |
| 93 | |
Dmitry V. Levin | 9f612ff | 2016-02-16 00:44:16 +0000 | [diff] [blame] | 94 | for (printed = 0, cur = start; cur < end; cur += sizeof(fds)) { |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 95 | if (umove(tcp, cur, &fds) < 0) { |
| 96 | if (outptr == outstr) |
| 97 | *outptr++ = '['; |
| 98 | else |
| 99 | outptr = stpcpy(outptr, ", "); |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 100 | outptr = xappendstr(outstr, outptr, "%#" PRI_klx, cur); |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 101 | break; |
| 102 | } |
| 103 | if (!fds.revents) |
| 104 | continue; |
| 105 | if (outptr == outstr) |
| 106 | *outptr++ = '['; |
| 107 | else |
| 108 | outptr = stpcpy(outptr, ", "); |
Dmitry V. Levin | 9f612ff | 2016-02-16 00:44:16 +0000 | [diff] [blame] | 109 | if (printed >= max_printed) { |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 110 | outptr = stpcpy(outptr, "..."); |
| 111 | break; |
| 112 | } |
| 113 | |
| 114 | static const char fmt[] = "{fd=%d, revents="; |
| 115 | char fdstr[sizeof(fmt) + sizeof(int) * 3]; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 116 | xsprintf(fdstr, fmt, fds.fd); |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 117 | |
Dmitry V. Levin | 6274ecc | 2016-05-15 14:23:06 +0000 | [diff] [blame] | 118 | const char *flagstr = sprintflags("", pollflags, |
| 119 | (unsigned short) fds.revents); |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 120 | |
Dmitry V. Levin | 8ef9809 | 2016-02-16 00:04:37 +0000 | [diff] [blame] | 121 | if (outptr + strlen(fdstr) + strlen(flagstr) + 1 >= |
| 122 | end_outstr - (2 + 2 * sizeof(long) + sizeof(", ], ..."))) { |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 123 | outptr = stpcpy(outptr, "..."); |
| 124 | break; |
| 125 | } |
| 126 | outptr = stpcpy(outptr, fdstr); |
| 127 | outptr = stpcpy(outptr, flagstr); |
| 128 | *outptr++ = '}'; |
Dmitry V. Levin | 9f612ff | 2016-02-16 00:44:16 +0000 | [diff] [blame] | 129 | ++printed; |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | if (outptr != outstr) |
| 133 | *outptr++ = ']'; |
| 134 | |
| 135 | *outptr = '\0'; |
| 136 | if (pts) { |
Dmitry V. Levin | 2950de3 | 2015-09-18 17:44:16 +0000 | [diff] [blame] | 137 | const char *str = sprint_timespec(tcp, pts); |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 138 | |
Dmitry V. Levin | 2950de3 | 2015-09-18 17:44:16 +0000 | [diff] [blame] | 139 | if (outptr + sizeof(", left ") + strlen(str) < end_outstr) { |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 140 | outptr = stpcpy(outptr, outptr == outstr ? "left " : ", left "); |
Dmitry V. Levin | 2950de3 | 2015-09-18 17:44:16 +0000 | [diff] [blame] | 141 | outptr = stpcpy(outptr, str); |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 142 | } else { |
| 143 | outptr = stpcpy(outptr, ", ..."); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (outptr == outstr) |
| 148 | return 0; |
| 149 | |
| 150 | tcp->auxstr = outstr; |
| 151 | return RVAL_STR; |
| 152 | #undef end_outstr |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | SYS_FUNC(poll) |
| 156 | { |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 157 | if (entering(tcp)) { |
Dmitry V. Levin | fd2d6a7 | 2016-05-07 23:09:09 +0000 | [diff] [blame] | 158 | decode_poll_entering(tcp); |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 159 | tprintf("%d", (int) tcp->u_arg[2]); |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 160 | |
Dmitry V. Levin | fd2d6a7 | 2016-05-07 23:09:09 +0000 | [diff] [blame] | 161 | return 0; |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 162 | } else { |
| 163 | return decode_poll_exiting(tcp, 0); |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 164 | } |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | SYS_FUNC(ppoll) |
| 168 | { |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 169 | if (entering(tcp)) { |
Dmitry V. Levin | fd2d6a7 | 2016-05-07 23:09:09 +0000 | [diff] [blame] | 170 | decode_poll_entering(tcp); |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 171 | |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 172 | print_timespec(tcp, tcp->u_arg[2]); |
| 173 | tprints(", "); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 174 | /* NB: kernel requires arg[4] == NSIG_BYTES */ |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 175 | print_sigset_addr_len(tcp, tcp->u_arg[3], tcp->u_arg[4]); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 176 | tprintf(", %" PRI_klu, tcp->u_arg[4]); |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 177 | |
Dmitry V. Levin | fd2d6a7 | 2016-05-07 23:09:09 +0000 | [diff] [blame] | 178 | return 0; |
Dmitry V. Levin | d9fb450 | 2015-07-30 19:46:11 +0000 | [diff] [blame] | 179 | } else { |
| 180 | return decode_poll_exiting(tcp, tcp->u_arg[2]); |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 181 | } |
Dmitry V. Levin | 811bda6 | 2015-07-30 16:49:42 +0000 | [diff] [blame] | 182 | } |