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