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