blob: f0f8fa33d7db88997899851ba2813ed51ed3597c [file] [log] [blame]
Dmitry V. Levin93c9d1c2016-01-20 03:26:37 +00001/*
2 * Check decoding of recvmsg and sendmsg syscalls.
3 *
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
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
32#include <assert.h>
33#include <stdio.h>
34#include <unistd.h>
35#include <sys/socket.h>
36#include <sys/uio.h>
37
38int
39main(void)
40{
41 tprintf("%s", "");
42
43 int fds[2];
44 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds))
45 perror_msg_and_skip("socketpair");
46 assert(0 == fds[0]);
47 assert(1 == fds[1]);
48
49 static const char w0_c[] = "012";
50 const char *w0_d = hexdump_strdup(w0_c);
51 void *w0 = tail_memdup(w0_c, LENGTH_OF(w0_c));
52
53 static const char w1_c[] = "34567";
54 const char *w1_d = hexdump_strdup(w1_c);
55 void *w1 = tail_memdup(w1_c, LENGTH_OF(w1_c));
56
57 static const char w2_c[] = "89abcde";
58 const char *w2_d = hexdump_strdup(w2_c);
59 void *w2 = tail_memdup(w2_c, LENGTH_OF(w2_c));
60
61 static const char r0_c[] = "01234567";
62 const char *r0_d = hexdump_strdup(r0_c);
63 static const char r1_c[] = "89abcde";
64 const char *r1_d = hexdump_strdup(r1_c);
65
66 const struct iovec w_iov_[] = {
67 {
68 .iov_base = w0,
69 .iov_len = LENGTH_OF(w0_c)
70 }, {
71 .iov_base = w1,
72 .iov_len = LENGTH_OF(w1_c)
73 }, {
74 .iov_base = w2,
75 .iov_len = LENGTH_OF(w2_c)
76 }
77 };
78 struct iovec *w_iov = tail_memdup(w_iov_, sizeof(w_iov_));
79 const unsigned int w_len =
80 LENGTH_OF(w0_c) + LENGTH_OF(w1_c) + LENGTH_OF(w2_c);
81
82 const struct msghdr w_mh_ = {
83 .msg_iov = w_iov,
84 .msg_iovlen = ARRAY_SIZE(w_iov_)
85 };
86 const struct msghdr *w_mh = tail_memdup(&w_mh_, sizeof(w_mh_));
87
88 assert(sendmsg(1, w_mh, 0) == (int) w_len);
89 close(1);
90 tprintf("sendmsg(1, {msg_name(0)=NULL, msg_iov(%u)="
91 "[{\"%s\", %u}, {\"%s\", %u}, {\"%s\", %u}]"
92 ", msg_controllen=0, msg_flags=0}, 0) = %u\n"
93 " * %u bytes in buffer 0\n"
94 " | 00000 %-49s %-16s |\n"
95 " * %u bytes in buffer 1\n"
96 " | 00000 %-49s %-16s |\n"
97 " * %u bytes in buffer 2\n"
98 " | 00000 %-49s %-16s |\n",
99 ARRAY_SIZE(w_iov_), w0_c, LENGTH_OF(w0_c),
100 w1_c, LENGTH_OF(w1_c), w2_c, LENGTH_OF(w2_c), w_len,
101 LENGTH_OF(w0_c), w0_d, w0_c, LENGTH_OF(w1_c), w1_d, w1_c,
102 LENGTH_OF(w2_c), w2_d, w2_c);
103
104 const unsigned int r_len = (w_len + 1) / 2;
105 void *r0 = tail_alloc(r_len);
106 const struct iovec r0_iov_[] = {
107 {
108 .iov_base = r0,
109 .iov_len = r_len
110 }
111 };
112 struct iovec *r_iov = tail_memdup(r0_iov_, sizeof(r0_iov_));
113
114 const struct msghdr r_mh_ = {
115 .msg_iov = r_iov,
116 .msg_iovlen = ARRAY_SIZE(r0_iov_)
117 };
118 struct msghdr *r_mh = tail_memdup(&r_mh_, sizeof(r_mh_));
119
120 assert(recvmsg(0, r_mh, 0) == (int) r_len);
121 tprintf("recvmsg(0, {msg_name(0)=NULL, msg_iov(%u)="
122 "[{\"%s\", %u}], msg_controllen=0, msg_flags=0}, 0) = %u\n"
123 " * %u bytes in buffer 0\n"
124 " | 00000 %-49s %-16s |\n",
125 ARRAY_SIZE(r0_iov_), r0_c, r_len, r_len, r_len, r0_d, r0_c);
126
127 void *r1 = tail_alloc(r_len);
128 void *r2 = tail_alloc(w_len);
129 const struct iovec r1_iov_[] = {
130 {
131 .iov_base = r1,
132 .iov_len = r_len
133 },
134 {
135 .iov_base = r2,
136 .iov_len = w_len
137 }
138 };
139 r_iov = tail_memdup(r1_iov_, sizeof(r1_iov_));
140 r_mh->msg_iov = r_iov;
141 r_mh->msg_iovlen = ARRAY_SIZE(r1_iov_);
142
143 assert(recvmsg(0, r_mh, 0) == (int) w_len - r_len);
144 tprintf("recvmsg(0, {msg_name(0)=NULL, msg_iov(%u)="
145 "[{\"%s\", %u}, {\"\", %u}], msg_controllen=0"
146 ", msg_flags=0}, 0) = %u\n"
147 " * %u bytes in buffer 0\n"
148 " | 00000 %-49s %-16s |\n",
149 ARRAY_SIZE(r1_iov_), r1_c, r_len, w_len, w_len - r_len,
150 w_len - r_len, r1_d, r1_c);
151 close(0);
152
153 tprintf("+++ exited with 0 +++\n");
154 return 0;
155}