1 /* 2 * Copyright (c) 2014 Masatake YAMATO <yamato (at) redhat.com> 3 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv (at) altlinux.org> 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 "tests.h" 30 #include <assert.h> 31 #include <unistd.h> 32 33 #include "msghdr.h" 34 35 int 36 main(void) 37 { 38 tprintf("%s", ""); 39 40 int fds[2]; 41 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) 42 perror_msg_and_skip("socketpair"); 43 assert(0 == fds[0]); 44 assert(1 == fds[1]); 45 46 static const char w0_c[] = "012"; 47 const char *w0_d = hexdump_strdup(w0_c); 48 void *w0 = tail_memdup(w0_c, LENGTH_OF(w0_c)); 49 50 static const char w1_c[] = "34567"; 51 const char *w1_d = hexdump_strdup(w1_c); 52 void *w1 = tail_memdup(w1_c, LENGTH_OF(w1_c)); 53 54 static const char w2_c[] = "89abcde"; 55 const char *w2_d = hexdump_strdup(w2_c); 56 void *w2 = tail_memdup(w2_c, LENGTH_OF(w2_c)); 57 58 const struct iovec w0_iov_[] = { 59 { 60 .iov_base = w0, 61 .iov_len = LENGTH_OF(w0_c) 62 }, { 63 .iov_base = w1, 64 .iov_len = LENGTH_OF(w1_c) 65 } 66 }; 67 struct iovec *w0_iov = tail_memdup(w0_iov_, sizeof(w0_iov_)); 68 69 const struct iovec w1_iov_[] = { 70 { 71 .iov_base = w2, 72 .iov_len = LENGTH_OF(w2_c) 73 } 74 }; 75 struct iovec *w1_iov = tail_memdup(w1_iov_, sizeof(w1_iov_)); 76 77 const struct mmsghdr w_mmh_[] = { 78 { 79 .msg_hdr = { 80 .msg_iov = w0_iov, 81 .msg_iovlen = ARRAY_SIZE(w0_iov_), 82 } 83 }, { 84 .msg_hdr = { 85 .msg_iov = w1_iov, 86 .msg_iovlen = ARRAY_SIZE(w1_iov_), 87 } 88 } 89 }; 90 void *w_mmh = tail_memdup(w_mmh_, sizeof(w_mmh_)); 91 const unsigned int n_w_mmh = ARRAY_SIZE(w_mmh_); 92 93 int r = send_mmsg(1, w_mmh, n_w_mmh, MSG_DONTROUTE | MSG_NOSIGNAL); 94 if (r < 0) 95 perror_msg_and_skip("sendmmsg"); 96 assert(r == (int) n_w_mmh); 97 assert(close(1) == 0); 98 tprintf("sendmmsg(1, [{msg_hdr={msg_name=NULL, msg_namelen=0" 99 ", msg_iov=[{iov_base=\"%s\", iov_len=%u}" 100 ", {iov_base=\"%s\", iov_len=%u}], msg_iovlen=%u" 101 ", msg_controllen=0, msg_flags=0}, msg_len=%u}" 102 ", {msg_hdr={msg_name=NULL, msg_namelen=0" 103 ", msg_iov=[{iov_base=\"%s\", iov_len=%u}], msg_iovlen=%u" 104 ", msg_controllen=0, msg_flags=0}, msg_len=%u}], %u" 105 ", MSG_DONTROUTE|MSG_NOSIGNAL) = %d\n" 106 " = %u buffers in vector 0\n" 107 " * %u bytes in buffer 0\n" 108 " | 00000 %-49s %-16s |\n" 109 " * %u bytes in buffer 1\n" 110 " | 00000 %-49s %-16s |\n" 111 " = %u buffers in vector 1\n" 112 " * %u bytes in buffer 0\n" 113 " | 00000 %-49s %-16s |\n", 114 w0_c, LENGTH_OF(w0_c), 115 w1_c, LENGTH_OF(w1_c), 116 (unsigned int) ARRAY_SIZE(w0_iov_), 117 LENGTH_OF(w0_c) + LENGTH_OF(w1_c), 118 w2_c, LENGTH_OF(w2_c), (unsigned int) ARRAY_SIZE(w1_iov_), 119 LENGTH_OF(w2_c), 120 n_w_mmh, r, 121 (unsigned int) ARRAY_SIZE(w0_iov_), 122 LENGTH_OF(w0_c), w0_d, w0_c, 123 LENGTH_OF(w1_c), w1_d, w1_c, 124 (unsigned int) ARRAY_SIZE(w1_iov_), 125 LENGTH_OF(w2_c), w2_d, w2_c); 126 127 const unsigned int w_len = 128 LENGTH_OF(w0_c) + LENGTH_OF(w1_c) + LENGTH_OF(w2_c); 129 const unsigned int r_len = (w_len + 1) / 2; 130 void *r0 = tail_alloc(r_len); 131 void *r1 = tail_alloc(r_len); 132 void *r2 = tail_alloc(r_len); 133 const struct iovec r0_iov_[] = { 134 { 135 .iov_base = r0, 136 .iov_len = r_len 137 } 138 }; 139 struct iovec *r0_iov = tail_memdup(r0_iov_, sizeof(r0_iov_)); 140 const struct iovec r1_iov_[] = { 141 { 142 .iov_base = r1, 143 .iov_len = r_len 144 }, 145 { 146 .iov_base = r2, 147 .iov_len = r_len 148 } 149 }; 150 struct iovec *r1_iov = tail_memdup(r1_iov_, sizeof(r1_iov_)); 151 152 const struct mmsghdr r_mmh_[] = { 153 { 154 .msg_hdr = { 155 .msg_iov = r0_iov, 156 .msg_iovlen = ARRAY_SIZE(r0_iov_), 157 } 158 }, { 159 .msg_hdr = { 160 .msg_iov = r1_iov, 161 .msg_iovlen = ARRAY_SIZE(r1_iov_), 162 } 163 } 164 }; 165 void *r_mmh = tail_memdup(r_mmh_, sizeof(r_mmh_)); 166 const unsigned int n_r_mmh = ARRAY_SIZE(r_mmh_); 167 168 static const char r0_c[] = "01234567"; 169 const char *r0_d = hexdump_strdup(r0_c); 170 static const char r1_c[] = "89abcde"; 171 const char *r1_d = hexdump_strdup(r1_c); 172 173 assert(recv_mmsg(0, r_mmh, n_r_mmh, MSG_DONTWAIT, NULL) == (int) n_r_mmh); 174 assert(close(0) == 0); 175 tprintf("recvmmsg(0, [{msg_hdr={msg_name=NULL, msg_namelen=0" 176 ", msg_iov=[{iov_base=\"%s\", iov_len=%u}], msg_iovlen=%u" 177 ", msg_controllen=0, msg_flags=0}, msg_len=%u}" 178 ", {msg_hdr={msg_name=NULL, msg_namelen=0" 179 ", msg_iov=[{iov_base=\"%s\", iov_len=%u}" 180 ", {iov_base=\"\", iov_len=%u}], msg_iovlen=%u" 181 ", msg_controllen=0, msg_flags=0}, msg_len=%u}], %u" 182 ", MSG_DONTWAIT, NULL) = %d\n" 183 " = %u buffers in vector 0\n" 184 " * %u bytes in buffer 0\n" 185 " | 00000 %-49s %-16s |\n" 186 " = %u buffers in vector 1\n" 187 " * %u bytes in buffer 0\n" 188 " | 00000 %-49s %-16s |\n", 189 r0_c, r_len, (unsigned int) ARRAY_SIZE(r0_iov_), 190 LENGTH_OF(r0_c), r1_c, r_len, r_len, 191 (unsigned int) ARRAY_SIZE(r1_iov_), LENGTH_OF(r1_c), 192 n_r_mmh, r, 193 (unsigned int) ARRAY_SIZE(r0_iov_), LENGTH_OF(r0_c), 194 r0_d, r0_c, 195 (unsigned int) ARRAY_SIZE(r1_iov_), LENGTH_OF(r1_c), 196 r1_d, r1_c); 197 198 tprintf("+++ exited with 0 +++\n"); 199 return 0; 200 } 201