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 write from 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 writing.
17*49cdfc7eSAndroid Build Coastguard Worker * - ESPIPE when fd is associated with a pipe.
18*49cdfc7eSAndroid Build Coastguard Worker */
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard Worker #include <sys/uio.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
25*49cdfc7eSAndroid Build Coastguard Worker #include "pwritev.h"
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker #define CHUNK 64
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker static int fd1;
30*49cdfc7eSAndroid Build Coastguard Worker static int fd2;
31*49cdfc7eSAndroid Build Coastguard Worker static int fd3 = -1;
32*49cdfc7eSAndroid Build Coastguard Worker static int fd4[2];
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker static char buf[CHUNK];
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker static struct iovec wr_iovec1[] = {
37*49cdfc7eSAndroid Build Coastguard Worker {buf, -1},
38*49cdfc7eSAndroid Build Coastguard Worker };
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker static struct iovec wr_iovec2[] = {
41*49cdfc7eSAndroid Build Coastguard Worker {buf, CHUNK},
42*49cdfc7eSAndroid Build Coastguard Worker };
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker static struct iovec wr_iovec3[] = {
45*49cdfc7eSAndroid Build Coastguard Worker {(char *)-1, CHUNK},
46*49cdfc7eSAndroid Build Coastguard Worker };
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
49*49cdfc7eSAndroid Build Coastguard Worker int *fd;
50*49cdfc7eSAndroid Build Coastguard Worker struct iovec *name;
51*49cdfc7eSAndroid Build Coastguard Worker int count;
52*49cdfc7eSAndroid Build Coastguard Worker off_t offset;
53*49cdfc7eSAndroid Build Coastguard Worker int exp_err;
54*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
55*49cdfc7eSAndroid Build Coastguard Worker {&fd1, wr_iovec1, 1, 0, EINVAL},
56*49cdfc7eSAndroid Build Coastguard Worker {&fd1, wr_iovec2, -1, 0, EINVAL},
57*49cdfc7eSAndroid Build Coastguard Worker {&fd1, wr_iovec2, 1, -1, EINVAL},
58*49cdfc7eSAndroid Build Coastguard Worker {&fd1, wr_iovec3, 1, 0, EFAULT},
59*49cdfc7eSAndroid Build Coastguard Worker {&fd3, wr_iovec2, 1, 0, EBADF},
60*49cdfc7eSAndroid Build Coastguard Worker {&fd2, wr_iovec2, 1, 0, EBADF},
61*49cdfc7eSAndroid Build Coastguard Worker {&fd4[1], wr_iovec2, 1, 0, ESPIPE}
62*49cdfc7eSAndroid Build Coastguard Worker };
63*49cdfc7eSAndroid Build Coastguard Worker
verify_pwritev(unsigned int n)64*49cdfc7eSAndroid Build Coastguard Worker static void verify_pwritev(unsigned int n)
65*49cdfc7eSAndroid Build Coastguard Worker {
66*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker TEST(pwritev(*tc->fd, tc->name, tc->count, tc->offset));
69*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == 0) {
70*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "pwritev() succeeded unexpectedly");
71*49cdfc7eSAndroid Build Coastguard Worker return;
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == tc->exp_err) {
75*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "pwritev() failed as expected");
76*49cdfc7eSAndroid Build Coastguard Worker return;
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "pwritev() failed unexpectedly, expected %s",
80*49cdfc7eSAndroid Build Coastguard Worker tst_strerrno(tc->exp_err));
81*49cdfc7eSAndroid Build Coastguard Worker }
82*49cdfc7eSAndroid Build Coastguard Worker
setup(void)83*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
84*49cdfc7eSAndroid Build Coastguard Worker {
85*49cdfc7eSAndroid Build Coastguard Worker fd1 = SAFE_OPEN("file", O_RDWR | O_CREAT, 0644);
86*49cdfc7eSAndroid Build Coastguard Worker SAFE_FTRUNCATE(fd1, getpagesize());
87*49cdfc7eSAndroid Build Coastguard Worker fd2 = SAFE_OPEN("file", O_RDONLY | O_CREAT, 0644);
88*49cdfc7eSAndroid Build Coastguard Worker SAFE_PIPE(fd4);
89*49cdfc7eSAndroid Build Coastguard Worker }
90*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)91*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
92*49cdfc7eSAndroid Build Coastguard Worker {
93*49cdfc7eSAndroid Build Coastguard Worker if (fd1 > 0)
94*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd1);
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker if (fd2 > 0)
97*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd2);
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker if (fd4[0] > 0)
100*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd4[0]);
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker if (fd4[1] > 0)
103*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd4[1]);
104*49cdfc7eSAndroid Build Coastguard Worker }
105*49cdfc7eSAndroid Build Coastguard Worker
106*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
107*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
108*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
109*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
110*49cdfc7eSAndroid Build Coastguard Worker .test = verify_pwritev,
111*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
112*49cdfc7eSAndroid Build Coastguard Worker };
113