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