1 /*
2  * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2016-2017 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "tests.h"
30 
31 #ifdef HAVE_PREADV
32 
33 # include <fcntl.h>
34 # include <stdio.h>
35 # include <sys/uio.h>
36 # include <unistd.h>
37 
38 # define LEN 8
39 
40 static void
print_iov(const struct iovec * iov)41 print_iov(const struct iovec *iov)
42 {
43 	unsigned int i;
44 	unsigned char *buf = iov->iov_base;
45 
46 	fputs("{iov_base=\"", stdout);
47 	for (i = 0; i < iov->iov_len; ++i)
48 		printf("\\%d", (int) buf[i]);
49 	printf("\", iov_len=%u}", (unsigned) iov->iov_len);
50 }
51 
52 static void
print_iovec(const struct iovec * iov,unsigned int cnt)53 print_iovec(const struct iovec *iov, unsigned int cnt)
54 {
55 	unsigned int i;
56 	putchar('[');
57 	for (i = 0; i < cnt; ++i) {
58 		if (i)
59 			fputs(", ", stdout);
60 		print_iov(&iov[i]);
61 	}
62 	putchar(']');
63 }
64 
65 int
main(void)66 main(void)
67 {
68 	const off_t offset = 0xdefaceddeadbeefLL;
69 	char *buf = tail_alloc(LEN);
70 	TAIL_ALLOC_OBJECT_CONST_PTR(struct iovec, iov);
71 	iov->iov_base = buf;
72 	iov->iov_len = LEN;
73 
74 	(void) close(0);
75 	if (open("/dev/zero", O_RDONLY))
76 		perror_msg_and_fail("open");
77 
78 	if (preadv(0, iov, 1, offset) != LEN)
79 		perror_msg_and_fail("preadv");
80 	printf("preadv(0, ");
81 	print_iovec(iov, 1);
82 	printf(", 1, %lld) = %u\n", (long long) offset, LEN);
83 
84 	if (preadv(0, iov, 1, -1) != -1)
85 		perror_msg_and_fail("preadv");
86 	printf("preadv(0, [{iov_base=%p, iov_len=%zu}], 1, -1) = "
87 	       "-1 EINVAL (%m)\n", iov->iov_base, iov->iov_len);
88 
89 	if (preadv(0, NULL, 1, -2) != -1)
90 		perror_msg_and_fail("preadv");
91 	printf("preadv(0, NULL, 1, -2) = -1 EINVAL (%m)\n");
92 
93 	if (preadv(0, iov, 0, -3) != -1)
94 		perror_msg_and_fail("preadv");
95 	printf("preadv(0, [], 0, -3) = -1 EINVAL (%m)\n");
96 
97 	static const char tmp[] = "preadv-tmpfile";
98 	int fd = open(tmp, O_RDWR | O_CREAT | O_TRUNC, 0600);
99 	if (fd < 0)
100 		perror_msg_and_fail("open");
101 	if (unlink(tmp))
102 		perror_msg_and_fail("unlink");
103 
104 	static const char w[] = "0123456789abcde";
105 	if (write(fd, w, LENGTH_OF(w)) != LENGTH_OF(w))
106 		perror_msg_and_fail("write");
107 
108 	static const char r0_c[] = "01234567";
109 	static const char r1_c[] = "89abcde";
110 
111 	const unsigned int r_len = (LENGTH_OF(w) + 1) / 2;
112 	void *r0 = tail_alloc(r_len);
113 	const struct iovec r0_iov_[] = {
114 		{
115 			.iov_base = r0,
116 			.iov_len = r_len
117 		}
118 	};
119 	const struct iovec *r_iov = tail_memdup(r0_iov_, sizeof(r0_iov_));
120 
121 	long rc;
122 
123 	rc = preadv(fd, r_iov, ARRAY_SIZE(r0_iov_), 0);
124 	if (rc != (int) r_len)
125 		perror_msg_and_fail("preadv: expected %u, returned %ld",
126 				    r_len, rc);
127 	printf("preadv(%d, [{iov_base=\"%s\", iov_len=%u}], %u, 0) = %u\n",
128 	       fd, r0_c, r_len, (unsigned int) ARRAY_SIZE(r0_iov_), r_len);
129 
130 	void *r1 = tail_alloc(r_len);
131 	void *r2 = tail_alloc(LENGTH_OF(w));
132 	const struct iovec r1_iov_[] = {
133 		{
134 			.iov_base = r1,
135 			.iov_len = r_len
136 		},
137 		{
138 			.iov_base = r2,
139 			.iov_len = LENGTH_OF(w)
140 		}
141 	};
142 	r_iov = tail_memdup(r1_iov_, sizeof(r1_iov_));
143 
144 	rc = preadv(fd, r_iov, ARRAY_SIZE(r1_iov_), r_len);
145 	if (rc != (int) LENGTH_OF(w) - (int) r_len)
146 		perror_msg_and_fail("preadv: expected %d, returned %ld",
147 				    (int) LENGTH_OF(w) - r_len, rc);
148 	printf("preadv(%d, [{iov_base=\"%s\", iov_len=%u}"
149 	       ", {iov_base=\"\", iov_len=%u}], %u, %u) = %u\n",
150 	       fd, r1_c, r_len, LENGTH_OF(w),
151 	       (unsigned int) ARRAY_SIZE(r1_iov_),
152 	       r_len, LENGTH_OF(w) - r_len);
153 
154 	puts("+++ exited with 0 +++");
155 	return 0;
156 }
157 
158 #else
159 
160 SKIP_MAIN_UNDEFINED("HAVE_PREADV")
161 
162 #endif
163