Dmitry V. Levin | 7c37ce4 | 2016-07-14 22:13:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010 Andreas Schwab <schwab@linux-m68k.org> |
| 3 | * Copyright (c) 2012-2013 Denys Vlasenko <vda.linux@googlemail.com> |
| 4 | * Copyright (c) 2014 Masatake YAMATO <yamato@redhat.com> |
| 5 | * Copyright (c) 2010-2016 Dmitry V. Levin <ldv@altlinux.org> |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 6 | * Copyright (c) 2016-2018 The strace developers. |
Dmitry V. Levin | 7c37ce4 | 2016-07-14 22:13:58 +0000 | [diff] [blame] | 7 | * All rights reserved. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. The name of the author may not be used to endorse or promote products |
| 18 | * derived from this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 21 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 22 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 23 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 24 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 29 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #include "defs.h" |
| 33 | #include "msghdr.h" |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 34 | #include "xstring.h" |
Dmitry V. Levin | 0485ab5 | 2016-07-16 22:04:24 +0000 | [diff] [blame] | 35 | #include <limits.h> |
Dmitry V. Levin | 7c37ce4 | 2016-07-14 22:13:58 +0000 | [diff] [blame] | 36 | |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 37 | static bool |
| 38 | fetch_struct_mmsghdr_for_print(struct tcb *const tcp, |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 39 | const kernel_ulong_t addr, |
Dmitry V. Levin | 0485ab5 | 2016-07-16 22:04:24 +0000 | [diff] [blame] | 40 | const unsigned int len, void *const mh) |
Dmitry V. Levin | 7c37ce4 | 2016-07-14 22:13:58 +0000 | [diff] [blame] | 41 | { |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 42 | return (entering(tcp) || !syserror(tcp)) && |
| 43 | fetch_struct_mmsghdr(tcp, addr, mh); |
Dmitry V. Levin | 0485ab5 | 2016-07-16 22:04:24 +0000 | [diff] [blame] | 44 | } |
Dmitry V. Levin | 7c37ce4 | 2016-07-14 22:13:58 +0000 | [diff] [blame] | 45 | |
Dmitry V. Levin | 0485ab5 | 2016-07-16 22:04:24 +0000 | [diff] [blame] | 46 | struct print_struct_mmsghdr_config { |
| 47 | const int *p_user_msg_namelen; |
Dmitry V. Levin | 3a161d1 | 2016-07-18 16:25:25 +0000 | [diff] [blame] | 48 | unsigned int msg_len_vlen; |
Dmitry V. Levin | 0485ab5 | 2016-07-16 22:04:24 +0000 | [diff] [blame] | 49 | unsigned int count; |
| 50 | bool use_msg_len; |
| 51 | }; |
| 52 | |
| 53 | static bool |
| 54 | print_struct_mmsghdr(struct tcb *tcp, void *elem_buf, |
| 55 | size_t elem_size, void *data) |
| 56 | { |
| 57 | const struct mmsghdr *const mmsg = elem_buf; |
| 58 | struct print_struct_mmsghdr_config *const c = data; |
| 59 | |
| 60 | if (!c->count) { |
| 61 | tprints("..."); |
| 62 | return false; |
| 63 | } |
| 64 | --c->count; |
| 65 | |
| 66 | tprints("{msg_hdr="); |
| 67 | print_struct_msghdr(tcp, &mmsg->msg_hdr, c->p_user_msg_namelen, |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 68 | c->use_msg_len ? mmsg->msg_len : (kernel_ulong_t) -1); |
Dmitry V. Levin | 3a161d1 | 2016-07-18 16:25:25 +0000 | [diff] [blame] | 69 | if (c->msg_len_vlen) { |
| 70 | tprintf(", msg_len=%u", mmsg->msg_len); |
| 71 | --c->msg_len_vlen; |
| 72 | } |
| 73 | tprints("}"); |
Dmitry V. Levin | 0485ab5 | 2016-07-16 22:04:24 +0000 | [diff] [blame] | 74 | |
Dmitry V. Levin | b42f6b3 | 2016-07-17 23:51:39 +0000 | [diff] [blame] | 75 | if (c->p_user_msg_namelen) |
| 76 | ++c->p_user_msg_namelen; |
| 77 | |
Dmitry V. Levin | 0485ab5 | 2016-07-16 22:04:24 +0000 | [diff] [blame] | 78 | return true; |
Dmitry V. Levin | 7c37ce4 | 2016-07-14 22:13:58 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Dmitry V. Levin | 4de8de5 | 2016-07-14 22:20:04 +0000 | [diff] [blame] | 81 | static void |
Dmitry V. Levin | b42f6b3 | 2016-07-17 23:51:39 +0000 | [diff] [blame] | 82 | free_mmsgvec_data(void *ptr) |
| 83 | { |
| 84 | char **pstr = ptr; |
| 85 | free(*pstr); |
| 86 | *pstr = 0; |
| 87 | |
| 88 | free(ptr); |
| 89 | } |
| 90 | |
| 91 | struct mmsgvec_data { |
| 92 | char *timeout; |
| 93 | unsigned int count; |
| 94 | int namelen[IOV_MAX]; |
| 95 | }; |
| 96 | |
| 97 | static void |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 98 | save_mmsgvec_namelen(struct tcb *const tcp, kernel_ulong_t addr, |
Dmitry V. Levin | b42f6b3 | 2016-07-17 23:51:39 +0000 | [diff] [blame] | 99 | unsigned int len, const char *const timeout) |
| 100 | { |
| 101 | if (len > IOV_MAX) |
| 102 | len = IOV_MAX; |
| 103 | |
| 104 | const size_t data_size = offsetof(struct mmsgvec_data, namelen) |
| 105 | + sizeof(int) * len; |
| 106 | struct mmsgvec_data *const data = xmalloc(data_size); |
| 107 | data->timeout = xstrdup(timeout); |
| 108 | |
| 109 | unsigned int i, fetched; |
| 110 | |
| 111 | for (i = 0; i < len; ++i, addr += fetched) { |
| 112 | struct mmsghdr mh; |
| 113 | |
| 114 | fetched = fetch_struct_mmsghdr(tcp, addr, &mh); |
| 115 | if (!fetched) |
| 116 | break; |
| 117 | data->namelen[i] = mh.msg_hdr.msg_namelen; |
| 118 | } |
| 119 | data->count = i; |
| 120 | |
| 121 | set_tcb_priv_data(tcp, data, free_mmsgvec_data); |
| 122 | } |
| 123 | |
| 124 | static void |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 125 | decode_mmsgvec(struct tcb *const tcp, const kernel_ulong_t addr, |
Dmitry V. Levin | 3a161d1 | 2016-07-18 16:25:25 +0000 | [diff] [blame] | 126 | const unsigned int vlen, const unsigned int msg_len_vlen, |
| 127 | const bool use_msg_len) |
Dmitry V. Levin | 7c37ce4 | 2016-07-14 22:13:58 +0000 | [diff] [blame] | 128 | { |
Dmitry V. Levin | 0485ab5 | 2016-07-16 22:04:24 +0000 | [diff] [blame] | 129 | struct mmsghdr mmsg; |
| 130 | struct print_struct_mmsghdr_config c = { |
Dmitry V. Levin | 3a161d1 | 2016-07-18 16:25:25 +0000 | [diff] [blame] | 131 | .msg_len_vlen = msg_len_vlen, |
Dmitry V. Levin | 0485ab5 | 2016-07-16 22:04:24 +0000 | [diff] [blame] | 132 | .count = IOV_MAX, |
| 133 | .use_msg_len = use_msg_len |
| 134 | }; |
Dmitry V. Levin | b42f6b3 | 2016-07-17 23:51:39 +0000 | [diff] [blame] | 135 | const struct mmsgvec_data *const data = get_tcb_priv_data(tcp); |
| 136 | |
| 137 | if (data) { |
| 138 | if (data->count < c.count) |
| 139 | c.count = data->count; |
| 140 | c.p_user_msg_namelen = data->namelen; |
| 141 | } |
Dmitry V. Levin | 7c37ce4 | 2016-07-14 22:13:58 +0000 | [diff] [blame] | 142 | |
Dmitry V. Levin | 3a161d1 | 2016-07-18 16:25:25 +0000 | [diff] [blame] | 143 | print_array(tcp, addr, vlen, &mmsg, sizeof_struct_mmsghdr(), |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 144 | fetch_struct_mmsghdr_for_print, |
Dmitry V. Levin | 0485ab5 | 2016-07-16 22:04:24 +0000 | [diff] [blame] | 145 | print_struct_mmsghdr, &c); |
Dmitry V. Levin | 7c37ce4 | 2016-07-14 22:13:58 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 149 | dumpiov_in_mmsghdr(struct tcb *const tcp, kernel_ulong_t addr) |
Dmitry V. Levin | 7c37ce4 | 2016-07-14 22:13:58 +0000 | [diff] [blame] | 150 | { |
| 151 | unsigned int len = tcp->u_rval; |
| 152 | unsigned int i, fetched; |
| 153 | struct mmsghdr mmsg; |
| 154 | |
| 155 | for (i = 0; i < len; ++i, addr += fetched) { |
| 156 | fetched = fetch_struct_mmsghdr(tcp, addr, &mmsg); |
| 157 | if (!fetched) |
| 158 | break; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 159 | tprintf(" = %" PRI_klu " buffers in vector %u\n", |
| 160 | (kernel_ulong_t) mmsg.msg_hdr.msg_iovlen, i); |
Dmitry V. Levin | 7c37ce4 | 2016-07-14 22:13:58 +0000 | [diff] [blame] | 161 | dumpiov_upto(tcp, mmsg.msg_hdr.msg_iovlen, |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 162 | ptr_to_kulong(mmsg.msg_hdr.msg_iov), |
| 163 | mmsg.msg_len); |
Dmitry V. Levin | 7c37ce4 | 2016-07-14 22:13:58 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
Dmitry V. Levin | 4de8de5 | 2016-07-14 22:20:04 +0000 | [diff] [blame] | 166 | |
| 167 | SYS_FUNC(sendmmsg) |
| 168 | { |
| 169 | if (entering(tcp)) { |
| 170 | /* sockfd */ |
| 171 | printfd(tcp, tcp->u_arg[0]); |
| 172 | tprints(", "); |
| 173 | if (!verbose(tcp)) { |
Dmitry V. Levin | 0d88d99 | 2016-07-18 17:48:44 +0000 | [diff] [blame] | 174 | /* msgvec */ |
Dmitry V. Levin | 4de8de5 | 2016-07-14 22:20:04 +0000 | [diff] [blame] | 175 | printaddr(tcp->u_arg[1]); |
| 176 | /* vlen */ |
| 177 | tprintf(", %u, ", (unsigned int) tcp->u_arg[2]); |
| 178 | /* flags */ |
| 179 | printflags(msg_flags, tcp->u_arg[3], "MSG_???"); |
| 180 | return RVAL_DECODED; |
| 181 | } |
| 182 | } else { |
Dmitry V. Levin | 0d88d99 | 2016-07-18 17:48:44 +0000 | [diff] [blame] | 183 | const unsigned int msg_len_vlen = |
| 184 | syserror(tcp) ? 0 : tcp->u_rval; |
| 185 | /* msgvec */ |
| 186 | temporarily_clear_syserror(tcp); |
Dmitry V. Levin | 3a161d1 | 2016-07-18 16:25:25 +0000 | [diff] [blame] | 187 | decode_mmsgvec(tcp, tcp->u_arg[1], tcp->u_arg[2], |
Dmitry V. Levin | 0d88d99 | 2016-07-18 17:48:44 +0000 | [diff] [blame] | 188 | msg_len_vlen, false); |
| 189 | restore_cleared_syserror(tcp); |
Dmitry V. Levin | 4de8de5 | 2016-07-14 22:20:04 +0000 | [diff] [blame] | 190 | /* vlen */ |
| 191 | tprintf(", %u, ", (unsigned int) tcp->u_arg[2]); |
| 192 | /* flags */ |
| 193 | printflags(msg_flags, tcp->u_arg[3], "MSG_???"); |
| 194 | } |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | SYS_FUNC(recvmmsg) |
| 199 | { |
Dmitry V. Levin | 4de8de5 | 2016-07-14 22:20:04 +0000 | [diff] [blame] | 200 | if (entering(tcp)) { |
| 201 | printfd(tcp, tcp->u_arg[0]); |
| 202 | tprints(", "); |
| 203 | if (verbose(tcp)) { |
Dmitry V. Levin | b42f6b3 | 2016-07-17 23:51:39 +0000 | [diff] [blame] | 204 | save_mmsgvec_namelen(tcp, tcp->u_arg[1], tcp->u_arg[2], |
| 205 | sprint_timespec(tcp, tcp->u_arg[4])); |
Dmitry V. Levin | 4de8de5 | 2016-07-14 22:20:04 +0000 | [diff] [blame] | 206 | } else { |
Dmitry V. Levin | 0d88d99 | 2016-07-18 17:48:44 +0000 | [diff] [blame] | 207 | /* msgvec */ |
Dmitry V. Levin | 4de8de5 | 2016-07-14 22:20:04 +0000 | [diff] [blame] | 208 | printaddr(tcp->u_arg[1]); |
| 209 | /* vlen */ |
| 210 | tprintf(", %u, ", (unsigned int) tcp->u_arg[2]); |
| 211 | /* flags */ |
| 212 | printflags(msg_flags, tcp->u_arg[3], "MSG_???"); |
| 213 | tprints(", "); |
| 214 | print_timespec(tcp, tcp->u_arg[4]); |
| 215 | } |
| 216 | return 0; |
| 217 | } else { |
| 218 | if (verbose(tcp)) { |
Dmitry V. Levin | 0d88d99 | 2016-07-18 17:48:44 +0000 | [diff] [blame] | 219 | /* msgvec */ |
Dmitry V. Levin | 3a161d1 | 2016-07-18 16:25:25 +0000 | [diff] [blame] | 220 | decode_mmsgvec(tcp, tcp->u_arg[1], tcp->u_rval, |
| 221 | tcp->u_rval, true); |
Dmitry V. Levin | 4de8de5 | 2016-07-14 22:20:04 +0000 | [diff] [blame] | 222 | /* vlen */ |
| 223 | tprintf(", %u, ", (unsigned int) tcp->u_arg[2]); |
| 224 | /* flags */ |
| 225 | printflags(msg_flags, tcp->u_arg[3], "MSG_???"); |
| 226 | tprints(", "); |
| 227 | /* timeout on entrance */ |
Dmitry V. Levin | b42f6b3 | 2016-07-17 23:51:39 +0000 | [diff] [blame] | 228 | tprints(*(const char **) get_tcb_priv_data(tcp)); |
Dmitry V. Levin | 4de8de5 | 2016-07-14 22:20:04 +0000 | [diff] [blame] | 229 | } |
| 230 | if (syserror(tcp)) |
| 231 | return 0; |
| 232 | if (tcp->u_rval == 0) { |
| 233 | tcp->auxstr = "Timeout"; |
| 234 | return RVAL_STR; |
| 235 | } |
Dmitry V. Levin | 3e3b7f6 | 2016-07-18 10:34:41 +0000 | [diff] [blame] | 236 | if (!verbose(tcp) || !tcp->u_arg[4]) |
Dmitry V. Levin | 4de8de5 | 2016-07-14 22:20:04 +0000 | [diff] [blame] | 237 | return 0; |
| 238 | /* timeout on exit */ |
Dmitry V. Levin | 7f8ece1 | 2016-07-15 17:46:07 +0000 | [diff] [blame] | 239 | static char str[sizeof("left") + TIMESPEC_TEXT_BUFSIZE]; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 240 | xsprintf(str, "left %s", sprint_timespec(tcp, tcp->u_arg[4])); |
Dmitry V. Levin | 4de8de5 | 2016-07-14 22:20:04 +0000 | [diff] [blame] | 241 | tcp->auxstr = str; |
| 242 | return RVAL_STR; |
| 243 | } |
| 244 | } |