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