2 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 print_iov(const struct iovec *iov)
43 unsigned char *buf = iov->iov_base;
46 for (i = 0; i < iov->iov_len; ++i)
47 printf("\\%d", (int) buf[i]);
48 printf("\", %u}", (unsigned) iov->iov_len);
52 print_iovec(const struct iovec *iov, unsigned int cnt)
56 for (i = 0; i < cnt; ++i) {
67 const off_t offset = 0xdefaceddeadbeefLL;
68 char *buf = tail_alloc(LEN);
69 struct iovec *iov = tail_alloc(sizeof(*iov));
74 if (open("/dev/zero", O_RDONLY))
75 perror_msg_and_fail("open");
77 if (preadv(0, iov, 1, offset) != LEN)
78 perror_msg_and_fail("preadv");
81 printf(", 1, %lld) = %u\n", (long long) offset, LEN);
83 if (preadv(0, iov, 1, -1) != -1)
84 perror_msg_and_fail("preadv");
85 printf("preadv(0, %p, 1, -1) = -1 EINVAL (%m)\n", iov);
87 if (preadv(0, NULL, 1, -2) != -1)
88 perror_msg_and_fail("preadv");
89 printf("preadv(0, NULL, 1, -2) = -1 EINVAL (%m)\n");
91 if (preadv(0, iov, 0, -3) != -1)
92 perror_msg_and_fail("preadv");
93 printf("preadv(0, [], 0, -3) = -1 EINVAL (%m)\n");
95 static const char tmp[] = "preadv-tmpfile";
96 int fd = open(tmp, O_RDWR | O_CREAT | O_TRUNC, 0600);
98 perror_msg_and_fail("open");
100 perror_msg_and_fail("unlink");
102 static const char w[] = "0123456789abcde";
103 if (write(fd, w, LENGTH_OF(w)) != LENGTH_OF(w))
104 perror_msg_and_fail("write");
106 static const char r0_c[] = "01234567";
107 static const char r1_c[] = "89abcde";
109 const unsigned int r_len = (LENGTH_OF(w) + 1) / 2;
110 void *r0 = tail_alloc(r_len);
111 const struct iovec r0_iov_[] = {
117 const struct iovec *r_iov = tail_memdup(r0_iov_, sizeof(r0_iov_));
121 rc = preadv(fd, r_iov, ARRAY_SIZE(r0_iov_), 0);
122 if (rc != (int) r_len)
123 perror_msg_and_fail("preadv: expected %u, returned %ld",
125 printf("preadv(%d, [{\"%s\", %u}], %u, 0) = %u\n",
126 fd, r0_c, r_len, ARRAY_SIZE(r0_iov_), r_len);
128 void *r1 = tail_alloc(r_len);
129 void *r2 = tail_alloc(LENGTH_OF(w));
130 const struct iovec r1_iov_[] = {
137 .iov_len = LENGTH_OF(w)
140 r_iov = tail_memdup(r1_iov_, sizeof(r1_iov_));
142 rc = preadv(fd, r_iov, ARRAY_SIZE(r1_iov_), r_len);
143 if (rc != (int) LENGTH_OF(w) - r_len)
144 perror_msg_and_fail("preadv: expected %d, returned %ld",
145 (int) LENGTH_OF(w) - r_len, rc);
146 printf("preadv(%d, [{\"%s\", %u}, {\"\", %u}], %u, %u) = %u\n",
147 fd, r1_c, r_len, LENGTH_OF(w), ARRAY_SIZE(r1_iov_),
148 r_len, LENGTH_OF(w) - r_len);
150 puts("+++ exited with 0 +++");
156 SKIP_MAIN_UNDEFINED("HAVE_PREADV")