xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/splice/splice09.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 to /dev/zero and /dev/null these two devices discard all
10  * data written to them.
11  *
12  * The support for splicing to /dev/zero was added in:
13  * 1b057bd800c3 ("drivers/char/mem: implement splice() for /dev/zero, /dev/full")
14  */
15 
16 #define _GNU_SOURCE
17 #include "tst_test.h"
18 
19 static const char *const test_devices[] = {
20 	"/dev/null",
21 	"/dev/zero",
22 };
23 
verify_splice(unsigned int n)24 static void verify_splice(unsigned int n)
25 {
26 	char buf[1024];
27 	char dev_fd;
28 	int pipefd[2];
29 
30 	memset(buf, 0xff, sizeof(buf));
31 
32 	tst_res(TINFO, "Testing %s", test_devices[n]);
33 
34 	dev_fd = SAFE_OPEN(test_devices[n], O_WRONLY);
35 
36 	SAFE_PIPE(pipefd);
37 	SAFE_WRITE(1, pipefd[1], buf, sizeof(buf));
38 
39 	TST_EXP_POSITIVE(splice(pipefd[0], NULL, dev_fd, NULL, sizeof(buf), 0));
40 
41 	if (TST_PASS && TST_RET != sizeof(buf))
42 		tst_res(TFAIL, "Wrote only part of the pipe buffer");
43 
44 	SAFE_CLOSE(pipefd[0]);
45 	SAFE_CLOSE(pipefd[1]);
46 	SAFE_CLOSE(dev_fd);
47 }
48 
49 static struct tst_test test = {
50 	.test = verify_splice,
51 	.tcnt = ARRAY_SIZE(test_devices),
52 	.min_kver = "6.7",
53 };
54