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