• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	{ &notvalidfd, &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 	if (tst_fs_type(".") == TST_NFS_MAGIC) {
56 		tst_brk(TCONF, "Cannot do splice() "
57 			"on a file located on an NFS filesystem");
58 	}
59 
60 	filefd = SAFE_OPEN(TESTFILE, O_WRONLY | O_CREAT, 0644);
61 
62 	SAFE_PIPE(pipes);
63 
64 	ivc.iov_base = buffer;
65 	ivc.iov_len = TEST_BLOCK_SIZE;
66 }
67 
vmsplice_verify(unsigned int n)68 static void vmsplice_verify(unsigned int n)
69 {
70 	struct tcase *tc = &tcases[n];
71 
72 	TEST(vmsplice(*(tc->fd), tc->iov, tc->nr_segs, 0));
73 
74 	if (TST_RET != -1) {
75 		tst_res(TFAIL, "vmsplice() returned %ld, "
76 			"expected -1, errno:%d", TST_RET,
77 			tc->exp_errno);
78 		return;
79 	}
80 
81 	if (TST_ERR != tc->exp_errno) {
82 		tst_res(TFAIL | TTERRNO,
83 			"vmsplice() failed unexpectedly; expected: %d - %s",
84 			tc->exp_errno, tst_strerrno(tc->exp_errno));
85 		return;
86 	}
87 
88 	tst_res(TPASS | TTERRNO, "vmsplice() failed as expected");
89 }
90 
cleanup(void)91 static void cleanup(void)
92 {
93 	if (filefd > 0)
94 		SAFE_CLOSE(filefd);
95 
96 	if (pipes[0] > 0)
97 		SAFE_CLOSE(pipes[0]);
98 
99 	if (pipes[1] > 0)
100 		SAFE_CLOSE(pipes[1]);
101 }
102 
103 static struct tst_test test = {
104 	.setup = setup,
105 	.cleanup = cleanup,
106 	.test = vmsplice_verify,
107 	.tcnt = ARRAY_SIZE(tcases),
108 	.needs_tmpdir = 1,
109 	.min_kver = "2.6.17",
110 };
111