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) 2014 Fujitsu Ltd.
4*49cdfc7eSAndroid Build Coastguard Worker * Author: Xing Gu <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker /*
7*49cdfc7eSAndroid Build Coastguard Worker * Description:
8*49cdfc7eSAndroid Build Coastguard Worker * Verify that,
9*49cdfc7eSAndroid Build Coastguard Worker * 1) vmsplice() returns -1 and sets errno to EBADF if fd
10*49cdfc7eSAndroid Build Coastguard Worker * is not valid.
11*49cdfc7eSAndroid Build Coastguard Worker * 2) vmsplice() returns -1 and sets errno to EBADF if fd
12*49cdfc7eSAndroid Build Coastguard Worker * doesn't refer to a pipe.
13*49cdfc7eSAndroid Build Coastguard Worker * 3) vmsplice() returns -1 and sets errno to EINVAL if
14*49cdfc7eSAndroid Build Coastguard Worker * nr_segs is greater than IOV_MAX.
15*49cdfc7eSAndroid Build Coastguard Worker */
16*49cdfc7eSAndroid Build Coastguard Worker
17*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <sys/uio.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/vmsplice.h"
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE "testfile"
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker #define TEST_BLOCK_SIZE 128
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker static char buffer[TEST_BLOCK_SIZE];
35*49cdfc7eSAndroid Build Coastguard Worker static int notvalidfd = -1;
36*49cdfc7eSAndroid Build Coastguard Worker static int filefd;
37*49cdfc7eSAndroid Build Coastguard Worker static int pipes[2];
38*49cdfc7eSAndroid Build Coastguard Worker static struct iovec ivc;
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
41*49cdfc7eSAndroid Build Coastguard Worker int *fd;
42*49cdfc7eSAndroid Build Coastguard Worker const struct iovec *iov;
43*49cdfc7eSAndroid Build Coastguard Worker unsigned long nr_segs;
44*49cdfc7eSAndroid Build Coastguard Worker int exp_errno;
45*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
46*49cdfc7eSAndroid Build Coastguard Worker { ¬validfd, &ivc, 1, EBADF },
47*49cdfc7eSAndroid Build Coastguard Worker { &filefd, &ivc, 1, EBADF },
48*49cdfc7eSAndroid Build Coastguard Worker { &pipes[1], &ivc, IOV_MAX + 1, EINVAL },
49*49cdfc7eSAndroid Build Coastguard Worker };
50*49cdfc7eSAndroid Build Coastguard Worker
setup(void)51*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
52*49cdfc7eSAndroid Build Coastguard Worker {
53*49cdfc7eSAndroid Build Coastguard Worker filefd = SAFE_OPEN(TESTFILE, O_WRONLY | O_CREAT, 0644);
54*49cdfc7eSAndroid Build Coastguard Worker
55*49cdfc7eSAndroid Build Coastguard Worker SAFE_PIPE(pipes);
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker ivc.iov_base = buffer;
58*49cdfc7eSAndroid Build Coastguard Worker ivc.iov_len = TEST_BLOCK_SIZE;
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker
vmsplice_verify(unsigned int n)61*49cdfc7eSAndroid Build Coastguard Worker static void vmsplice_verify(unsigned int n)
62*49cdfc7eSAndroid Build Coastguard Worker {
63*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker TEST(vmsplice(*(tc->fd), tc->iov, tc->nr_segs, 0));
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != -1) {
68*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "vmsplice() returned %ld, "
69*49cdfc7eSAndroid Build Coastguard Worker "expected -1, errno:%d", TST_RET,
70*49cdfc7eSAndroid Build Coastguard Worker tc->exp_errno);
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_errno) {
75*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO,
76*49cdfc7eSAndroid Build Coastguard Worker "vmsplice() failed unexpectedly; expected: %d - %s",
77*49cdfc7eSAndroid Build Coastguard Worker tc->exp_errno, tst_strerrno(tc->exp_errno));
78*49cdfc7eSAndroid Build Coastguard Worker return;
79*49cdfc7eSAndroid Build Coastguard Worker }
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "vmsplice() failed as expected");
82*49cdfc7eSAndroid Build Coastguard Worker }
83*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)84*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
85*49cdfc7eSAndroid Build Coastguard Worker {
86*49cdfc7eSAndroid Build Coastguard Worker if (filefd > 0)
87*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(filefd);
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker if (pipes[0] > 0)
90*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(pipes[0]);
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker if (pipes[1] > 0)
93*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(pipes[1]);
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
97*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
98*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
99*49cdfc7eSAndroid Build Coastguard Worker .test = vmsplice_verify,
100*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
101*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
102*49cdfc7eSAndroid Build Coastguard Worker .skip_filesystems = (const char *const []) {
103*49cdfc7eSAndroid Build Coastguard Worker "nfs",
104*49cdfc7eSAndroid Build Coastguard Worker NULL
105*49cdfc7eSAndroid Build Coastguard Worker },
106*49cdfc7eSAndroid Build Coastguard Worker };
107