xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/splice/splice08.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2024 Cyril Hrubis <[email protected]>
4  */
5 
6 /*\
7  * [Description]
8  *
9  * Test for splicing from /dev/zero and /dev/full.
10  *
11  * The support for splicing from /dev/zero and /dev/full was removed in:
12  * c6585011bc1d ("splice: Remove generic_file_splice_read()")
13  *
14  * And added back in:
15  * 1b057bd800c3 ("drivers/char/mem: implement splice() for /dev/zero, /dev/full")
16  */
17 
18 #define _GNU_SOURCE
19 #include "tst_test.h"
20 
21 static int fd_zero;
22 static int fd_full;
23 
test_splice(unsigned int bytes,int dev_fd)24 static void test_splice(unsigned int bytes, int dev_fd)
25 {
26 	int pipefd[2];
27 	char buf[bytes];
28 	size_t i;
29 	int fail = 0;
30 
31 	memset(buf, 0xff, sizeof(buf));
32 
33 	SAFE_PIPE(pipefd);
34 
35 	TST_EXP_POSITIVE(splice(dev_fd, NULL, pipefd[1], NULL, sizeof(buf), 0));
36 
37 	if (!TST_PASS)
38 		goto ret;
39 
40 	if ((size_t)TST_RET != sizeof(buf)) {
41 		tst_res(TFAIL, "Only part %lu bytes of %u were spliced",
42 			TST_RET, bytes);
43 		goto ret;
44 	}
45 
46 	SAFE_READ(1, pipefd[0], buf, sizeof(buf));
47 
48 	for (i = 0; i < sizeof(buf); i++) {
49 		if (buf[i])
50 			fail++;
51 	}
52 
53 	if (fail)
54 		tst_res(TFAIL, "%i non-zero bytes spliced from /dev/zero", fail);
55 	else
56 		tst_res(TPASS, "All bytes spliced from /dev/zero are zeroed");
57 
58 ret:
59 	SAFE_CLOSE(pipefd[0]);
60 	SAFE_CLOSE(pipefd[1]);
61 }
62 
verify_splice(unsigned int n)63 static void verify_splice(unsigned int n)
64 {
65 	unsigned int bytes = 1009 * n;
66 
67 	tst_res(TINFO, "Splicing %u bytes from /dev/zero", bytes);
68 	test_splice(bytes, fd_zero);
69 	tst_res(TINFO, "Splicing %u bytes from /dev/full", bytes);
70 	test_splice(bytes, fd_full);
71 }
72 
setup(void)73 static void setup(void)
74 {
75 	fd_zero = SAFE_OPEN("/dev/zero", O_RDONLY);
76 	fd_full = SAFE_OPEN("/dev/full", O_RDONLY);
77 }
78 
cleanup(void)79 static void cleanup(void)
80 {
81 	if (fd_zero > 0)
82 		SAFE_CLOSE(fd_zero);
83 
84 	if (fd_full > 0)
85 		SAFE_CLOSE(fd_full);
86 }
87 
88 static struct tst_test test = {
89 	.test = verify_splice,
90 	.tcnt = 9,
91 	.setup = setup,
92 	.cleanup = cleanup,
93 	.min_kver = "6.7",
94 };
95