1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2015-2016 Fujitsu Ltd.
4*49cdfc7eSAndroid Build Coastguard Worker * Author: Xiao Yang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Linux Test Project, 2017-2023
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * - EINVAL when iov_len is invalid.
12*49cdfc7eSAndroid Build Coastguard Worker * - EINVAL when the vector count iovcnt is less than zero.
13*49cdfc7eSAndroid Build Coastguard Worker * - EINVAL when offset is negative.
14*49cdfc7eSAndroid Build Coastguard Worker * - EFAULT when attempts to read into a invalid address.
15*49cdfc7eSAndroid Build Coastguard Worker * - EBADF when file descriptor is invalid.
16*49cdfc7eSAndroid Build Coastguard Worker * - EBADF when file descriptor is not open for reading.
17*49cdfc7eSAndroid Build Coastguard Worker * - EISDIR when fd refers to a directory.
18*49cdfc7eSAndroid Build Coastguard Worker * - ESPIPE when fd is associated with a pipe.
19*49cdfc7eSAndroid Build Coastguard Worker */
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker #include <sys/uio.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "preadv.h"
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker #define CHUNK 64
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker static int fd1;
31*49cdfc7eSAndroid Build Coastguard Worker static int fd2;
32*49cdfc7eSAndroid Build Coastguard Worker static int fd3 = -1;
33*49cdfc7eSAndroid Build Coastguard Worker static int fd4;
34*49cdfc7eSAndroid Build Coastguard Worker static int fd5[2];
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker static char buf[CHUNK];
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker static struct iovec rd_iovec1[] = {
39*49cdfc7eSAndroid Build Coastguard Worker {buf, -1},
40*49cdfc7eSAndroid Build Coastguard Worker };
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker static struct iovec rd_iovec2[] = {
43*49cdfc7eSAndroid Build Coastguard Worker {buf, CHUNK},
44*49cdfc7eSAndroid Build Coastguard Worker };
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker static struct iovec rd_iovec3[] = {
47*49cdfc7eSAndroid Build Coastguard Worker {(char *)-1, CHUNK},
48*49cdfc7eSAndroid Build Coastguard Worker };
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
51*49cdfc7eSAndroid Build Coastguard Worker int *fd;
52*49cdfc7eSAndroid Build Coastguard Worker struct iovec *name;
53*49cdfc7eSAndroid Build Coastguard Worker int count;
54*49cdfc7eSAndroid Build Coastguard Worker off_t offset;
55*49cdfc7eSAndroid Build Coastguard Worker int exp_err;
56*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
57*49cdfc7eSAndroid Build Coastguard Worker {&fd1, rd_iovec1, 1, 0, EINVAL},
58*49cdfc7eSAndroid Build Coastguard Worker {&fd1, rd_iovec2, -1, 0, EINVAL},
59*49cdfc7eSAndroid Build Coastguard Worker {&fd1, rd_iovec2, 1, -1, EINVAL},
60*49cdfc7eSAndroid Build Coastguard Worker {&fd1, rd_iovec3, 1, 0, EFAULT},
61*49cdfc7eSAndroid Build Coastguard Worker {&fd3, rd_iovec2, 1, 0, EBADF},
62*49cdfc7eSAndroid Build Coastguard Worker {&fd2, rd_iovec2, 1, 0, EBADF},
63*49cdfc7eSAndroid Build Coastguard Worker {&fd4, rd_iovec2, 1, 0, EISDIR},
64*49cdfc7eSAndroid Build Coastguard Worker {&fd5[0], rd_iovec2, 1, 0, ESPIPE}
65*49cdfc7eSAndroid Build Coastguard Worker };
66*49cdfc7eSAndroid Build Coastguard Worker
verify_preadv(unsigned int n)67*49cdfc7eSAndroid Build Coastguard Worker static void verify_preadv(unsigned int n)
68*49cdfc7eSAndroid Build Coastguard Worker {
69*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
70*49cdfc7eSAndroid Build Coastguard Worker
71*49cdfc7eSAndroid Build Coastguard Worker TEST(preadv(*tc->fd, tc->name, tc->count, tc->offset));
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == 0) {
74*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "preadv() succeeded unexpectedly");
75*49cdfc7eSAndroid Build Coastguard Worker return;
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker
78*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == tc->exp_err) {
79*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "preadv() failed as expected");
80*49cdfc7eSAndroid Build Coastguard Worker return;
81*49cdfc7eSAndroid Build Coastguard Worker }
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "preadv() failed unexpectedly, expected %s",
84*49cdfc7eSAndroid Build Coastguard Worker tst_strerrno(tc->exp_err));
85*49cdfc7eSAndroid Build Coastguard Worker }
86*49cdfc7eSAndroid Build Coastguard Worker
setup(void)87*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
88*49cdfc7eSAndroid Build Coastguard Worker {
89*49cdfc7eSAndroid Build Coastguard Worker fd1 = SAFE_OPEN("file1", O_RDWR | O_CREAT, 0644);
90*49cdfc7eSAndroid Build Coastguard Worker SAFE_FTRUNCATE(fd1, getpagesize());
91*49cdfc7eSAndroid Build Coastguard Worker fd2 = SAFE_OPEN("file2", O_WRONLY | O_CREAT, 0644);
92*49cdfc7eSAndroid Build Coastguard Worker fd4 = SAFE_OPEN(".", O_RDONLY);
93*49cdfc7eSAndroid Build Coastguard Worker SAFE_PIPE(fd5);
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)96*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
97*49cdfc7eSAndroid Build Coastguard Worker {
98*49cdfc7eSAndroid Build Coastguard Worker if (fd1 > 0)
99*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd1);
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker if (fd2 > 0)
102*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd2);
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker if (fd4 > 0)
105*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd4);
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker if (fd5[0] > 0)
108*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd5[0]);
109*49cdfc7eSAndroid Build Coastguard Worker
110*49cdfc7eSAndroid Build Coastguard Worker if (fd5[1] > 0)
111*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd5[1]);
112*49cdfc7eSAndroid Build Coastguard Worker }
113*49cdfc7eSAndroid Build Coastguard Worker
114*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
115*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
116*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
117*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
118*49cdfc7eSAndroid Build Coastguard Worker .test = verify_preadv,
119*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
120*49cdfc7eSAndroid Build Coastguard Worker };
121