blob: eb53a5e9782cf72cbab2ec8b97799a04cc2ed595 [file] [log] [blame]
Dmitry V. Levind4611512016-03-30 03:54:21 +00001/*
2 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
15 *
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.
26 */
27
28#include "tests.h"
29
30#ifdef HAVE_PREADV
31
32# include <fcntl.h>
33# include <stdio.h>
34# include <sys/uio.h>
35# include <unistd.h>
36
37# define LEN 8
38
39static void
40print_iov(const struct iovec *iov)
41{
42 unsigned int i;
43 unsigned char *buf = iov->iov_base;
44
Dmitry V. Levin4ddbfcf2016-07-13 22:54:55 +000045 fputs("{iov_base=\"", stdout);
Dmitry V. Levind4611512016-03-30 03:54:21 +000046 for (i = 0; i < iov->iov_len; ++i)
47 printf("\\%d", (int) buf[i]);
Dmitry V. Levin4ddbfcf2016-07-13 22:54:55 +000048 printf("\", iov_len=%u}", (unsigned) iov->iov_len);
Dmitry V. Levind4611512016-03-30 03:54:21 +000049}
50
51static void
52print_iovec(const struct iovec *iov, unsigned int cnt)
53{
54 unsigned int i;
55 putchar('[');
56 for (i = 0; i < cnt; ++i) {
57 if (i)
58 fputs(", ", stdout);
59 print_iov(&iov[i]);
60 }
61 putchar(']');
62}
63
64int
65main(void)
66{
67 const off_t offset = 0xdefaceddeadbeefLL;
68 char *buf = tail_alloc(LEN);
69 struct iovec *iov = tail_alloc(sizeof(*iov));
70 iov->iov_base = buf;
71 iov->iov_len = LEN;
72
73 (void) close(0);
74 if (open("/dev/zero", O_RDONLY))
75 perror_msg_and_fail("open");
76
77 if (preadv(0, iov, 1, offset) != LEN)
78 perror_msg_and_fail("preadv");
79 printf("preadv(0, ");
80 print_iovec(iov, 1);
81 printf(", 1, %lld) = %u\n", (long long) offset, LEN);
82
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);
86
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");
90
Dmitry V. Levin4ce30542016-05-07 22:54:04 +000091 if (preadv(0, iov, 0, -3) != -1)
Dmitry V. Levind4611512016-03-30 03:54:21 +000092 perror_msg_and_fail("preadv");
93 printf("preadv(0, [], 0, -3) = -1 EINVAL (%m)\n");
94
Dmitry V. Levinc98ab882016-03-30 18:04:00 +000095 static const char tmp[] = "preadv-tmpfile";
96 int fd = open(tmp, O_RDWR | O_CREAT | O_TRUNC, 0600);
97 if (fd < 0)
98 perror_msg_and_fail("open");
99 if (unlink(tmp))
100 perror_msg_and_fail("unlink");
101
102 static const char w[] = "0123456789abcde";
103 if (write(fd, w, LENGTH_OF(w)) != LENGTH_OF(w))
104 perror_msg_and_fail("write");
105
106 static const char r0_c[] = "01234567";
107 static const char r1_c[] = "89abcde";
108
109 const unsigned int r_len = (LENGTH_OF(w) + 1) / 2;
110 void *r0 = tail_alloc(r_len);
111 const struct iovec r0_iov_[] = {
112 {
113 .iov_base = r0,
114 .iov_len = r_len
115 }
116 };
117 const struct iovec *r_iov = tail_memdup(r0_iov_, sizeof(r0_iov_));
118
119 long rc;
120
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",
124 r_len, rc);
Dmitry V. Levin4ddbfcf2016-07-13 22:54:55 +0000125 printf("preadv(%d, [{iov_base=\"%s\", iov_len=%u}], %u, 0) = %u\n",
Dmitry V. Levinc98ab882016-03-30 18:04:00 +0000126 fd, r0_c, r_len, ARRAY_SIZE(r0_iov_), r_len);
127
128 void *r1 = tail_alloc(r_len);
129 void *r2 = tail_alloc(LENGTH_OF(w));
130 const struct iovec r1_iov_[] = {
131 {
132 .iov_base = r1,
133 .iov_len = r_len
134 },
135 {
136 .iov_base = r2,
137 .iov_len = LENGTH_OF(w)
138 }
139 };
140 r_iov = tail_memdup(r1_iov_, sizeof(r1_iov_));
141
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);
Dmitry V. Levin4ddbfcf2016-07-13 22:54:55 +0000146 printf("preadv(%d, [{iov_base=\"%s\", iov_len=%u}"
147 ", {iov_base=\"\", iov_len=%u}], %u, %u) = %u\n",
Dmitry V. Levinc98ab882016-03-30 18:04:00 +0000148 fd, r1_c, r_len, LENGTH_OF(w), ARRAY_SIZE(r1_iov_),
149 r_len, LENGTH_OF(w) - r_len);
150
Dmitry V. Levind4611512016-03-30 03:54:21 +0000151 puts("+++ exited with 0 +++");
152 return 0;
153}
154
155#else
156
157SKIP_MAIN_UNDEFINED("HAVE_PREADV")
158
159#endif