xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/vmsplice/vmsplice02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2014 Fujitsu Ltd.
4  * Author: Xing Gu <[email protected]>
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 <unistd.h>
22 #include <sys/uio.h>
23 #include <limits.h>
24 
25 #include "tst_test.h"
26 #include "lapi/syscalls.h"
27 #include "lapi/fcntl.h"
28 #include "lapi/vmsplice.h"
29 
30 #define TESTFILE "testfile"
31 
32 #define TEST_BLOCK_SIZE 128
33 
34 static char buffer[TEST_BLOCK_SIZE];
35 static int notvalidfd = -1;
36 static int filefd;
37 static int pipes[2];
38 static struct iovec ivc;
39 
40 static struct tcase {
41 	int *fd;
42 	const struct iovec *iov;
43 	unsigned long nr_segs;
44 	int exp_errno;
45 } tcases[] = {
46 	{ &notvalidfd, &ivc, 1, EBADF },
47 	{ &filefd, &ivc, 1, EBADF },
48 	{ &pipes[1], &ivc, IOV_MAX + 1, EINVAL },
49 };
50 
setup(void)51 static void setup(void)
52 {
53 	filefd = SAFE_OPEN(TESTFILE, O_WRONLY | O_CREAT, 0644);
54 
55 	SAFE_PIPE(pipes);
56 
57 	ivc.iov_base = buffer;
58 	ivc.iov_len = TEST_BLOCK_SIZE;
59 }
60 
vmsplice_verify(unsigned int n)61 static void vmsplice_verify(unsigned int n)
62 {
63 	struct tcase *tc = &tcases[n];
64 
65 	TEST(vmsplice(*(tc->fd), tc->iov, tc->nr_segs, 0));
66 
67 	if (TST_RET != -1) {
68 		tst_res(TFAIL, "vmsplice() returned %ld, "
69 			"expected -1, errno:%d", TST_RET,
70 			tc->exp_errno);
71 		return;
72 	}
73 
74 	if (TST_ERR != tc->exp_errno) {
75 		tst_res(TFAIL | TTERRNO,
76 			"vmsplice() failed unexpectedly; expected: %d - %s",
77 			tc->exp_errno, tst_strerrno(tc->exp_errno));
78 		return;
79 	}
80 
81 	tst_res(TPASS | TTERRNO, "vmsplice() failed as expected");
82 }
83 
cleanup(void)84 static void cleanup(void)
85 {
86 	if (filefd > 0)
87 		SAFE_CLOSE(filefd);
88 
89 	if (pipes[0] > 0)
90 		SAFE_CLOSE(pipes[0]);
91 
92 	if (pipes[1] > 0)
93 		SAFE_CLOSE(pipes[1]);
94 }
95 
96 static struct tst_test test = {
97 	.setup = setup,
98 	.cleanup = cleanup,
99 	.test = vmsplice_verify,
100 	.tcnt = ARRAY_SIZE(tcases),
101 	.needs_tmpdir = 1,
102 	.skip_filesystems = (const char *const []) {
103 		"nfs",
104 		NULL
105 	},
106 };
107