Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Masatake YAMATO <yamato@redhat.com> |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org> |
Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 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 | |
Dmitry V. Levin | 0c8853c | 2016-01-02 13:28:43 +0000 | [diff] [blame] | 29 | #include "tests.h" |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 30 | # include <sys/syscall.h> |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 31 | |
Dmitry V. Levin | eb4649a | 2016-01-12 04:37:06 +0000 | [diff] [blame] | 32 | #if (defined __NR_sendmmsg || defined HAVE_SENDMMSG) \ |
| 33 | && (defined __NR_recvmmsg || defined HAVE_RECVMMSG) |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 34 | |
| 35 | # include <assert.h> |
| 36 | # include <errno.h> |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 37 | # include <stdio.h> |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 38 | # include <unistd.h> |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 39 | |
Dmitry V. Levin | 4b38ce9 | 2016-06-27 00:02:41 +0000 | [diff] [blame^] | 40 | # include "msghdr.h" |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 41 | |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 42 | static int |
| 43 | send_mmsg(int fd, struct mmsghdr *vec, unsigned int vlen, unsigned int flags) |
| 44 | { |
| 45 | int rc; |
| 46 | #ifdef __NR_sendmmsg |
| 47 | rc = syscall(__NR_sendmmsg, (long) fd, vec, (unsigned long) vlen, |
| 48 | (unsigned long) flags); |
| 49 | if (rc >= 0 || ENOSYS != errno) |
| 50 | return rc; |
Dmitry V. Levin | 536a035 | 2016-01-20 02:06:59 +0000 | [diff] [blame] | 51 | tprintf("sendmmsg(%d, %p, %u, MSG_DONTROUTE|MSG_NOSIGNAL)" |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 52 | " = -1 ENOSYS (%m)\n", fd, vec, vlen); |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 53 | #endif |
| 54 | #ifdef HAVE_SENDMMSG |
| 55 | rc = sendmmsg(fd, vec, vlen, flags); |
| 56 | #endif |
| 57 | return rc; |
| 58 | } |
| 59 | |
| 60 | static int |
| 61 | recv_mmsg(int fd, struct mmsghdr *vec, unsigned int vlen, unsigned int flags, |
| 62 | struct timespec *timeout) |
| 63 | { |
| 64 | int rc; |
Dmitry V. Levin | eb4649a | 2016-01-12 04:37:06 +0000 | [diff] [blame] | 65 | #ifdef __NR_recvmmsg |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 66 | rc = syscall(__NR_recvmmsg, (long) fd, vec, (unsigned long) vlen, |
| 67 | (unsigned long) flags, timeout); |
| 68 | if (rc >= 0 || ENOSYS != errno) |
| 69 | return rc; |
Dmitry V. Levin | 536a035 | 2016-01-20 02:06:59 +0000 | [diff] [blame] | 70 | tprintf("recvmmsg(%d, %p, %u, MSG_DONTWAIT, NULL)" |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 71 | " = -1 ENOSYS (%m)\n", fd, vec, vlen); |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 72 | #endif |
Dmitry V. Levin | eb4649a | 2016-01-12 04:37:06 +0000 | [diff] [blame] | 73 | #ifdef HAVE_RECVMMSG |
Dmitry V. Levin | 6e815ce | 2016-01-11 00:16:39 +0000 | [diff] [blame] | 74 | rc = recvmmsg(fd, vec, vlen, flags, timeout); |
| 75 | #endif |
| 76 | return rc; |
| 77 | } |
| 78 | |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 79 | int |
| 80 | main(void) |
| 81 | { |
Dmitry V. Levin | 536a035 | 2016-01-20 02:06:59 +0000 | [diff] [blame] | 82 | tprintf("%s", ""); |
| 83 | |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 84 | int fds[2]; |
| 85 | if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 86 | perror_msg_and_skip("socketpair"); |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 87 | assert(0 == fds[0]); |
| 88 | assert(1 == fds[1]); |
Dmitry V. Levin | 35eb03f | 2016-01-11 00:17:36 +0000 | [diff] [blame] | 89 | |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 90 | static const char w0_c[] = "012"; |
| 91 | const char *w0_d = hexdump_strdup(w0_c); |
| 92 | void *w0 = tail_memdup(w0_c, LENGTH_OF(w0_c)); |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 93 | |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 94 | static const char w1_c[] = "34567"; |
| 95 | const char *w1_d = hexdump_strdup(w1_c); |
| 96 | void *w1 = tail_memdup(w1_c, LENGTH_OF(w1_c)); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 97 | |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 98 | static const char w2_c[] = "89abcde"; |
| 99 | const char *w2_d = hexdump_strdup(w2_c); |
| 100 | void *w2 = tail_memdup(w2_c, LENGTH_OF(w2_c)); |
| 101 | |
| 102 | const struct iovec w0_iov_[] = { |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 103 | { |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 104 | .iov_base = w0, |
| 105 | .iov_len = LENGTH_OF(w0_c) |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 106 | }, { |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 107 | .iov_base = w1, |
| 108 | .iov_len = LENGTH_OF(w1_c) |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 109 | } |
| 110 | }; |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 111 | struct iovec *w0_iov = tail_memdup(w0_iov_, sizeof(w0_iov_)); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 112 | |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 113 | const struct iovec w1_iov_[] = { |
| 114 | { |
| 115 | .iov_base = w2, |
| 116 | .iov_len = LENGTH_OF(w2_c) |
| 117 | } |
| 118 | }; |
| 119 | struct iovec *w1_iov = tail_memdup(w1_iov_, sizeof(w1_iov_)); |
| 120 | |
| 121 | const struct mmsghdr w_mmh_[] = { |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 122 | { |
| 123 | .msg_hdr = { |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 124 | .msg_iov = w0_iov, |
| 125 | .msg_iovlen = ARRAY_SIZE(w0_iov_), |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 126 | } |
| 127 | }, { |
| 128 | .msg_hdr = { |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 129 | .msg_iov = w1_iov, |
| 130 | .msg_iovlen = ARRAY_SIZE(w1_iov_), |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | }; |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 134 | void *w_mmh = tail_memdup(w_mmh_, sizeof(w_mmh_)); |
| 135 | const unsigned int n_w_mmh = ARRAY_SIZE(w_mmh_); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 136 | |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 137 | int r = send_mmsg(1, w_mmh, n_w_mmh, MSG_DONTROUTE | MSG_NOSIGNAL); |
Dmitry V. Levin | 95cbf6e | 2015-01-14 12:47:45 +0000 | [diff] [blame] | 138 | if (r < 0 && errno == ENOSYS) |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 139 | perror_msg_and_skip("sendmmsg"); |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 140 | assert(r == (int) n_w_mmh); |
| 141 | assert(close(1) == 0); |
Dmitry V. Levin | 26f90af | 2016-06-26 23:57:39 +0000 | [diff] [blame] | 142 | tprintf("sendmmsg(1, {{{msg_name=NULL, msg_namelen=0" |
| 143 | ", msg_iov=[{\"%s\", %u}, {\"%s\", %u}], msg_iovlen=%u" |
| 144 | ", msg_controllen=0, msg_flags=0}, %u}" |
| 145 | ", {{msg_name=NULL, msg_namelen=0" |
| 146 | ", msg_iov=[{\"%s\", %u}], msg_iovlen=%u" |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 147 | ", msg_controllen=0, msg_flags=0}, %u}}, %u" |
| 148 | ", MSG_DONTROUTE|MSG_NOSIGNAL) = %d\n" |
| 149 | " = %u buffers in vector 0\n" |
| 150 | " * %u bytes in buffer 0\n" |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 151 | " | 00000 %-49s %-16s |\n" |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 152 | " * %u bytes in buffer 1\n" |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 153 | " | 00000 %-49s %-16s |\n" |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 154 | " = %u buffers in vector 1\n" |
| 155 | " * %u bytes in buffer 0\n" |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 156 | " | 00000 %-49s %-16s |\n", |
Dmitry V. Levin | 26f90af | 2016-06-26 23:57:39 +0000 | [diff] [blame] | 157 | w0_c, LENGTH_OF(w0_c), |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 158 | w1_c, LENGTH_OF(w1_c), |
Dmitry V. Levin | 26f90af | 2016-06-26 23:57:39 +0000 | [diff] [blame] | 159 | ARRAY_SIZE(w0_iov_), |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 160 | LENGTH_OF(w0_c) + LENGTH_OF(w1_c), |
Dmitry V. Levin | 26f90af | 2016-06-26 23:57:39 +0000 | [diff] [blame] | 161 | w2_c, LENGTH_OF(w2_c), ARRAY_SIZE(w1_iov_), |
| 162 | LENGTH_OF(w2_c), |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 163 | n_w_mmh, r, |
| 164 | ARRAY_SIZE(w0_iov_), LENGTH_OF(w0_c), w0_d, w0_c, |
| 165 | LENGTH_OF(w1_c), w1_d, w1_c, |
| 166 | ARRAY_SIZE(w1_iov_), LENGTH_OF(w2_c), w2_d, w2_c); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 167 | |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 168 | const unsigned int w_len = |
| 169 | LENGTH_OF(w0_c) + LENGTH_OF(w1_c) + LENGTH_OF(w2_c); |
| 170 | const unsigned int r_len = (w_len + 1) / 2; |
| 171 | void *r0 = tail_alloc(r_len); |
| 172 | void *r1 = tail_alloc(r_len); |
| 173 | void *r2 = tail_alloc(r_len); |
| 174 | const struct iovec r0_iov_[] = { |
| 175 | { |
| 176 | .iov_base = r0, |
| 177 | .iov_len = r_len |
| 178 | } |
| 179 | }; |
| 180 | struct iovec *r0_iov = tail_memdup(r0_iov_, sizeof(r0_iov_)); |
| 181 | const struct iovec r1_iov_[] = { |
| 182 | { |
| 183 | .iov_base = r1, |
| 184 | .iov_len = r_len |
| 185 | }, |
| 186 | { |
| 187 | .iov_base = r2, |
| 188 | .iov_len = r_len |
| 189 | } |
| 190 | }; |
| 191 | struct iovec *r1_iov = tail_memdup(r1_iov_, sizeof(r1_iov_)); |
| 192 | |
| 193 | const struct mmsghdr r_mmh_[] = { |
| 194 | { |
| 195 | .msg_hdr = { |
| 196 | .msg_iov = r0_iov, |
| 197 | .msg_iovlen = ARRAY_SIZE(r0_iov_), |
| 198 | } |
| 199 | }, { |
| 200 | .msg_hdr = { |
| 201 | .msg_iov = r1_iov, |
| 202 | .msg_iovlen = ARRAY_SIZE(r1_iov_), |
| 203 | } |
| 204 | } |
| 205 | }; |
| 206 | void *r_mmh = tail_memdup(r_mmh_, sizeof(r_mmh_)); |
| 207 | const unsigned int n_r_mmh = ARRAY_SIZE(r_mmh_); |
| 208 | |
| 209 | static const char r0_c[] = "01234567"; |
| 210 | const char *r0_d = hexdump_strdup(r0_c); |
| 211 | static const char r1_c[] = "89abcde"; |
| 212 | const char *r1_d = hexdump_strdup(r1_c); |
| 213 | |
| 214 | assert(recv_mmsg(0, r_mmh, n_r_mmh, MSG_DONTWAIT, NULL) == (int) n_r_mmh); |
| 215 | assert(close(0) == 0); |
Dmitry V. Levin | 26f90af | 2016-06-26 23:57:39 +0000 | [diff] [blame] | 216 | tprintf("recvmmsg(0, {{{msg_name=NULL, msg_namelen=0" |
| 217 | ", msg_iov=[{\"%s\", %u}], msg_iovlen=%u" |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 218 | ", msg_controllen=0, msg_flags=0}, %u}" |
Dmitry V. Levin | 26f90af | 2016-06-26 23:57:39 +0000 | [diff] [blame] | 219 | ", {{msg_name=NULL, msg_namelen=0" |
| 220 | ", msg_iov=[{\"%s\", %u}, {\"\", %u}], msg_iovlen=%u" |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 221 | ", msg_controllen=0, msg_flags=0}, %u}}, %u" |
| 222 | ", MSG_DONTWAIT, NULL) = %d (left NULL)\n" |
| 223 | " = %u buffers in vector 0\n" |
| 224 | " * %u bytes in buffer 0\n" |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 225 | " | 00000 %-49s %-16s |\n" |
Dmitry V. Levin | 19d10f8 | 2016-01-14 00:06:20 +0000 | [diff] [blame] | 226 | " = %u buffers in vector 1\n" |
| 227 | " * %u bytes in buffer 0\n" |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 228 | " | 00000 %-49s %-16s |\n", |
Dmitry V. Levin | 26f90af | 2016-06-26 23:57:39 +0000 | [diff] [blame] | 229 | r0_c, r_len, ARRAY_SIZE(r0_iov_), LENGTH_OF(r0_c), |
| 230 | r1_c, r_len, r_len, ARRAY_SIZE(r1_iov_), LENGTH_OF(r1_c), |
Dmitry V. Levin | d44707d | 2016-01-20 04:56:25 +0000 | [diff] [blame] | 231 | n_r_mmh, r, |
| 232 | ARRAY_SIZE(r0_iov_), LENGTH_OF(r0_c), r0_d, r0_c, |
| 233 | ARRAY_SIZE(r1_iov_), LENGTH_OF(r1_c), r1_d, r1_c); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 234 | |
Dmitry V. Levin | 536a035 | 2016-01-20 02:06:59 +0000 | [diff] [blame] | 235 | tprintf("+++ exited with 0 +++\n"); |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 236 | return 0; |
Masatake YAMATO | 993198d | 2014-11-07 01:23:27 +0900 | [diff] [blame] | 237 | } |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 238 | |
| 239 | #else |
| 240 | |
Dmitry V. Levin | eb4649a | 2016-01-12 04:37:06 +0000 | [diff] [blame] | 241 | SKIP_MAIN_UNDEFINED("(__NR_sendmmsg || HAVE_SENDMMSG) && (__NR_recvmmsg || HAVE_RECVMMSG)") |
Dmitry V. Levin | 4e66672 | 2016-01-06 11:43:08 +0000 | [diff] [blame] | 242 | |
| 243 | #endif |