blob: 8ceae3500ecdb72496c07b2f6111e8200b21a746 [file] [log] [blame]
Dmitry V. Levin3f6ebce2016-03-31 00:01:58 +00001/*
2 * Check decoding of preadv and pwritev 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#if defined HAVE_PREADV && defined HAVE_PWRITEV
33
Dmitry V. Levinf73a3a42016-04-19 21:10:44 +000034# include <errno.h>
Dmitry V. Levin3f6ebce2016-03-31 00:01:58 +000035# include <fcntl.h>
36# include <stdio.h>
37# include <sys/uio.h>
38# include <unistd.h>
39
40int
41main(void)
42{
43 tprintf("%s", "");
44
45 static char tmp[] = "preadv-pwritev-tmpfile";
46 if (open(tmp, O_CREAT|O_RDONLY|O_TRUNC, 0600) != 0)
47 perror_msg_and_fail("creat: %s", tmp);
48 if (open(tmp, O_WRONLY) != 1)
49 perror_msg_and_fail("open: %s", tmp);
50 if (unlink(tmp))
51 perror_msg_and_fail("unlink: %s", tmp);
52
53 static const char w0_c[] = "012";
54 const char *w0_d = hexdump_strdup(w0_c);
55 void *w0 = tail_memdup(w0_c, LENGTH_OF(w0_c));
56
57 const void *efault = w0 + LENGTH_OF(w0_c);
58
59 static const char w1_c[] = "34567";
60 const char *w1_d = hexdump_strdup(w1_c);
61 void *w1 = tail_memdup(w1_c, LENGTH_OF(w1_c));
62
63 static const char w2_c[] = "89abcde";
64 const char *w2_d = hexdump_strdup(w2_c);
65 void *w2 = tail_memdup(w2_c, LENGTH_OF(w2_c));
66
67 long rc;
68
69 rc = pwritev(1, efault, 42, 0);
70 if (rc != -1)
71 perror_msg_and_fail("pwritev: expected -1, returned %ld", rc);
Dmitry V. Levinf73a3a42016-04-19 21:10:44 +000072 tprintf("pwritev(1, %p, 42, 0) = -1 %s (%m)\n",
73 efault, errno == EINVAL ? "EINVAL" : "EFAULT");
Dmitry V. Levin3f6ebce2016-03-31 00:01:58 +000074
75 rc = preadv(0, efault, 42, 0);
76 if (rc != -1)
77 perror_msg_and_fail("preadv: expected -1, returned %ld", rc);
Dmitry V. Levinf73a3a42016-04-19 21:10:44 +000078 tprintf("preadv(0, %p, 42, 0) = -1 %s (%m)\n",
79 efault, errno == EINVAL ? "EINVAL" : "EFAULT");
Dmitry V. Levin3f6ebce2016-03-31 00:01:58 +000080
81 static const char r0_c[] = "01234567";
82 const char *r0_d = hexdump_strdup(r0_c);
83 static const char r1_c[] = "89abcde";
84 const char *r1_d = hexdump_strdup(r1_c);
85
86 const struct iovec w_iov_[] = {
87 {
88 .iov_base = w0,
89 .iov_len = LENGTH_OF(w0_c)
90 }, {
91 .iov_base = w1,
92 .iov_len = LENGTH_OF(w1_c)
93 }, {
94 .iov_base = w2,
95 .iov_len = LENGTH_OF(w2_c)
96 }
97 };
98 const struct iovec *w_iov = tail_memdup(w_iov_, sizeof(w_iov_));
99
100 rc = pwritev(1, w_iov, 0, 0);
101 if (rc)
Dmitry V. Levinfe0d07a2016-04-02 01:04:57 +0000102 perror_msg_and_fail("pwritev: expected 0, returned %ld", rc);
Dmitry V. Levin3f6ebce2016-03-31 00:01:58 +0000103 tprintf("pwritev(1, [], 0, 0) = 0\n");
104
105 rc = pwritev(1, w_iov + ARRAY_SIZE(w_iov_) - 1, 2, 0);
106 if (rc != -1)
107 perror_msg_and_fail("pwritev: expected -1 EFAULT, returned %ld",
108 rc);
Dmitry V. Levinf73a3a42016-04-19 21:10:44 +0000109 tprintf("pwritev(1, [{\"%s\", %u}, %p], 2, 0) = -1 %s (%m)\n",
110 w2_c, LENGTH_OF(w2_c), w_iov + ARRAY_SIZE(w_iov_),
111 errno == EINVAL ? "EINVAL" : "EFAULT");
Dmitry V. Levin3f6ebce2016-03-31 00:01:58 +0000112
113 const unsigned int w_len =
114 LENGTH_OF(w0_c) + LENGTH_OF(w1_c) + LENGTH_OF(w2_c);
115
116 rc = pwritev(1, w_iov, ARRAY_SIZE(w_iov_), 0);
117 if (rc != (int) w_len)
118 perror_msg_and_fail("pwritev: expected %u, returned %ld",
119 w_len, rc);
120 close(1);
121 tprintf("pwritev(1, [{\"%s\", %u}, {\"%s\", %u}"
122 ", {\"%s\", %u}], %u, 0) = %u\n"
123 " * %u bytes in buffer 0\n"
124 " | 00000 %-49s %-16s |\n"
125 " * %u bytes in buffer 1\n"
126 " | 00000 %-49s %-16s |\n"
127 " * %u bytes in buffer 2\n"
128 " | 00000 %-49s %-16s |\n",
129 w0_c, LENGTH_OF(w0_c), w1_c, LENGTH_OF(w1_c),
130 w2_c, LENGTH_OF(w2_c), ARRAY_SIZE(w_iov_), w_len,
131 LENGTH_OF(w0_c), w0_d, w0_c,
132 LENGTH_OF(w1_c), w1_d, w1_c, LENGTH_OF(w2_c), w2_d, w2_c);
133
134 const unsigned int r_len = (w_len + 1) / 2;
135 void *r0 = tail_alloc(r_len);
136 const struct iovec r0_iov_[] = {
137 {
138 .iov_base = r0,
139 .iov_len = r_len
140 }
141 };
142 const struct iovec *r_iov = tail_memdup(r0_iov_, sizeof(r0_iov_));
143
144 rc = preadv(0, r_iov, ARRAY_SIZE(r0_iov_), 0);
145 if (rc != (int) r_len)
146 perror_msg_and_fail("preadv: expected %u, returned %ld",
147 r_len, rc);
148 tprintf("preadv(0, [{\"%s\", %u}], %u, 0) = %u\n"
149 " * %u bytes in buffer 0\n"
150 " | 00000 %-49s %-16s |\n",
151 r0_c, r_len, ARRAY_SIZE(r0_iov_), r_len, r_len, r0_d, r0_c);
152
153 void *r1 = tail_alloc(r_len);
154 void *r2 = tail_alloc(w_len);
155 const struct iovec r1_iov_[] = {
156 {
157 .iov_base = r1,
158 .iov_len = r_len
159 },
160 {
161 .iov_base = r2,
162 .iov_len = w_len
163 }
164 };
165 r_iov = tail_memdup(r1_iov_, sizeof(r1_iov_));
166
167 rc = preadv(0, r_iov, ARRAY_SIZE(r1_iov_), r_len);
168 if (rc != (int) w_len - r_len)
169 perror_msg_and_fail("preadv: expected %d, returned %ld",
170 (int) w_len - r_len, rc);
171 tprintf("preadv(0, [{\"%s\", %u}, {\"\", %u}], %u, %u) = %u\n"
172 " * %u bytes in buffer 0\n"
173 " | 00000 %-49s %-16s |\n",
174 r1_c, r_len, w_len, ARRAY_SIZE(r1_iov_),
175 r_len, w_len - r_len,
176 w_len - r_len, r1_d, r1_c);
177 close(0);
178
179 tprintf("+++ exited with 0 +++\n");
180 return 0;
181}
182
183#else
184
185SKIP_MAIN_UNDEFINED("HAVE_PREADV && HAVE_PWRITEV")
186
187#endif