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