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) 2019 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker * Author: Jinhui Huang <[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 *
10*49cdfc7eSAndroid Build Coastguard Worker * Check various errnos for pwritev2(2).
11*49cdfc7eSAndroid Build Coastguard Worker *
12*49cdfc7eSAndroid Build Coastguard Worker * - pwritev2() fails and sets errno to EINVAL if iov_len is invalid.
13*49cdfc7eSAndroid Build Coastguard Worker * - pwritev2() fails and sets errno to EINVAL if the vector count iovcnt is
14*49cdfc7eSAndroid Build Coastguard Worker * less than zero.
15*49cdfc7eSAndroid Build Coastguard Worker * - pwritev2() fails and sets errno to EOPNOTSUPP if flag is invalid.
16*49cdfc7eSAndroid Build Coastguard Worker * - pwritev2() fails and sets errno to EFAULT when writing data from invalid
17*49cdfc7eSAndroid Build Coastguard Worker * address.
18*49cdfc7eSAndroid Build Coastguard Worker * - pwritev2() fails and sets errno to EBADF if file descriptor is invalid.
19*49cdfc7eSAndroid Build Coastguard Worker * - pwritev2() fails and sets errno to EBADF if file descriptor is open for
20*49cdfc7eSAndroid Build Coastguard Worker * reading.
21*49cdfc7eSAndroid Build Coastguard Worker * - pwritev2() fails and sets errno to ESPIPE if fd is associated with a pipe.
22*49cdfc7eSAndroid Build Coastguard Worker */
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
25*49cdfc7eSAndroid Build Coastguard Worker #include <sys/uio.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
29*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/pwritev2.h"
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker #define CHUNK 64
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker static int fd1;
34*49cdfc7eSAndroid Build Coastguard Worker static int fd2;
35*49cdfc7eSAndroid Build Coastguard Worker static int fd3 = -1;
36*49cdfc7eSAndroid Build Coastguard Worker static int fd4[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 wr_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 wr_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 wr_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, wr_iovec1, 1, 0, 0, EINVAL},
61*49cdfc7eSAndroid Build Coastguard Worker {&fd1, wr_iovec2, -1, 0, 0, EINVAL},
62*49cdfc7eSAndroid Build Coastguard Worker {&fd1, wr_iovec2, 1, 1, -1, EOPNOTSUPP},
63*49cdfc7eSAndroid Build Coastguard Worker {&fd1, wr_iovec3, 1, 0, 0, EFAULT},
64*49cdfc7eSAndroid Build Coastguard Worker {&fd3, wr_iovec2, 1, 0, 0, EBADF},
65*49cdfc7eSAndroid Build Coastguard Worker {&fd2, wr_iovec2, 1, 0, 0, EBADF},
66*49cdfc7eSAndroid Build Coastguard Worker {&fd4[0], wr_iovec2, 1, 0, 0, ESPIPE},
67*49cdfc7eSAndroid Build Coastguard Worker };
68*49cdfc7eSAndroid Build Coastguard Worker
verify_pwritev2(unsigned int n)69*49cdfc7eSAndroid Build Coastguard Worker static void verify_pwritev2(unsigned int n)
70*49cdfc7eSAndroid Build Coastguard Worker {
71*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker TEST(pwritev2(*tc->fd, tc->name, tc->count, tc->offset, tc->flag));
74*49cdfc7eSAndroid Build Coastguard Worker
75*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == 0) {
76*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "pwritev2() succeeded unexpectedly");
77*49cdfc7eSAndroid Build Coastguard Worker return;
78*49cdfc7eSAndroid Build Coastguard Worker }
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == tc->exp_err) {
81*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "pwritev2() failed as expected");
82*49cdfc7eSAndroid Build Coastguard Worker return;
83*49cdfc7eSAndroid Build Coastguard Worker }
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "pwritev2() failed unexpectedly, expected %s",
86*49cdfc7eSAndroid Build Coastguard Worker tst_strerrno(tc->exp_err));
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker
setup(void)89*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
90*49cdfc7eSAndroid Build Coastguard Worker {
91*49cdfc7eSAndroid Build Coastguard Worker fd1 = SAFE_OPEN("file1", O_RDWR | O_CREAT, 0644);
92*49cdfc7eSAndroid Build Coastguard Worker SAFE_FTRUNCATE(fd1, getpagesize());
93*49cdfc7eSAndroid Build Coastguard Worker fd2 = SAFE_OPEN("file2", O_RDONLY | O_CREAT, 0644);
94*49cdfc7eSAndroid Build Coastguard Worker SAFE_PIPE(fd4);
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker wr_iovec3[0].iov_base = tst_get_bad_addr(NULL);
97*49cdfc7eSAndroid Build Coastguard Worker }
98*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)99*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
100*49cdfc7eSAndroid Build Coastguard Worker {
101*49cdfc7eSAndroid Build Coastguard Worker if (fd1 > 0)
102*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd1);
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker if (fd2 > 0)
105*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd2);
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker if (fd4[0] > 0)
108*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd4[0]);
109*49cdfc7eSAndroid Build Coastguard Worker
110*49cdfc7eSAndroid Build Coastguard Worker if (fd4[1] > 0)
111*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd4[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_pwritev2,
119*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
120*49cdfc7eSAndroid Build Coastguard Worker };
121