xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/vmsplice/vmsplice03.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) 2019 SUSE LLC
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Jorik Cronenberg <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  *
6*49cdfc7eSAndroid Build Coastguard Worker  * Test vmsplice() from a pipe into user memory
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
10*49cdfc7eSAndroid Build Coastguard Worker 
11*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
12*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
13*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/vmsplice.h"
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #define TEST_BLOCK_SIZE (64*1024)	/* 64K */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker static char buffer[TEST_BLOCK_SIZE];
19*49cdfc7eSAndroid Build Coastguard Worker static struct iovec *iov;
20*49cdfc7eSAndroid Build Coastguard Worker 
vmsplice_test(void)21*49cdfc7eSAndroid Build Coastguard Worker static void vmsplice_test(void)
22*49cdfc7eSAndroid Build Coastguard Worker {
23*49cdfc7eSAndroid Build Coastguard Worker 	int written, i;
24*49cdfc7eSAndroid Build Coastguard Worker 	int pipes[2];
25*49cdfc7eSAndroid Build Coastguard Worker 	char *arr_write = iov->iov_base;
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker 	memset(iov->iov_base, 0, iov->iov_len);
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(pipes);
30*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ALL, pipes[1], buffer, TEST_BLOCK_SIZE);
31*49cdfc7eSAndroid Build Coastguard Worker 	written = vmsplice(pipes[0], iov, 1, 0);
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	if (written < 0)
34*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "vmsplice() failed");
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	if (written == 0) {
37*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "vmsplice() didn't write anything");
38*49cdfc7eSAndroid Build Coastguard Worker 	} else {
39*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < TEST_BLOCK_SIZE; i++) {
40*49cdfc7eSAndroid Build Coastguard Worker 			if (arr_write[i] != buffer[i]) {
41*49cdfc7eSAndroid Build Coastguard Worker 				tst_res(TFAIL,
42*49cdfc7eSAndroid Build Coastguard Worker 					"Wrong data in user memory at %i", i);
43*49cdfc7eSAndroid Build Coastguard Worker 				break;
44*49cdfc7eSAndroid Build Coastguard Worker 			}
45*49cdfc7eSAndroid Build Coastguard Worker 		}
46*49cdfc7eSAndroid Build Coastguard Worker 		if (i == written)
47*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TPASS, "Spliced correctly into user memory");
48*49cdfc7eSAndroid Build Coastguard Worker 	}
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(pipes[1]);
51*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(pipes[0]);
52*49cdfc7eSAndroid Build Coastguard Worker }
53*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)54*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
55*49cdfc7eSAndroid Build Coastguard Worker {
56*49cdfc7eSAndroid Build Coastguard Worker 	int i;
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < TEST_BLOCK_SIZE; i++)
59*49cdfc7eSAndroid Build Coastguard Worker 		buffer[i] = i & 0xff;
60*49cdfc7eSAndroid Build Coastguard Worker }
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
63*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
64*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = vmsplice_test,
65*49cdfc7eSAndroid Build Coastguard Worker 	.bufs = (struct tst_buffers []) {
66*49cdfc7eSAndroid Build Coastguard Worker 		{&iov, .iov_sizes = (int[]){TEST_BLOCK_SIZE, -1}},
67*49cdfc7eSAndroid Build Coastguard Worker 		{}
68*49cdfc7eSAndroid Build Coastguard Worker 	}
69*49cdfc7eSAndroid Build Coastguard Worker };
70