xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/splice/splice05.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) 2017 Red Hat, Inc.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Boyang Xue <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * Functional test for splice(2): pipe <-> socket
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * This test case tests splice(2) from a pipe to a socket and vice versa
11*49cdfc7eSAndroid Build Coastguard Worker  */
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
14*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/splice.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include "splice.h"
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #define PIPE_MAX (64*1024)
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker static char *str_len_data;
29*49cdfc7eSAndroid Build Coastguard Worker static int num_len_data = PIPE_MAX;
30*49cdfc7eSAndroid Build Coastguard Worker static char *arr_in, *arr_out;
31*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)32*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
33*49cdfc7eSAndroid Build Coastguard Worker {
34*49cdfc7eSAndroid Build Coastguard Worker 	int i, pipe_limit;
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	pipe_limit = get_max_limit(num_len_data);
37*49cdfc7eSAndroid Build Coastguard Worker 	num_len_data = pipe_limit;
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_parse_int(str_len_data, &num_len_data, 1, pipe_limit)) {
40*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Invalid length of data: '%s', "
41*49cdfc7eSAndroid Build Coastguard Worker 			"valid value: [1, %d]", str_len_data, pipe_limit);
42*49cdfc7eSAndroid Build Coastguard Worker 	}
43*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "splice size = %d", num_len_data);
44*49cdfc7eSAndroid Build Coastguard Worker 	arr_in = SAFE_MALLOC(num_len_data);
45*49cdfc7eSAndroid Build Coastguard Worker 	arr_out = SAFE_MALLOC(num_len_data);
46*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < num_len_data; i++)
47*49cdfc7eSAndroid Build Coastguard Worker 		arr_in[i] = i & 0xff;
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker }
50*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)51*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
52*49cdfc7eSAndroid Build Coastguard Worker {
53*49cdfc7eSAndroid Build Coastguard Worker 	free(arr_in);
54*49cdfc7eSAndroid Build Coastguard Worker 	free(arr_out);
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker 
pipe_socket(void)57*49cdfc7eSAndroid Build Coastguard Worker static void pipe_socket(void)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker 	int pp1[2], pp2[2], sv[2], i, ret;
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(pp1);
62*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_PIPE(pp2);
63*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SOCKETPAIR(AF_UNIX, SOCK_STREAM, 0, sv);
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ALL, pp1[1], arr_in, num_len_data);
66*49cdfc7eSAndroid Build Coastguard Worker 	for (i = num_len_data; i > 0; i = i - ret) {
67*49cdfc7eSAndroid Build Coastguard Worker 		ret = splice(pp1[0], NULL, sv[0], 0, i, 0);
68*49cdfc7eSAndroid Build Coastguard Worker 		if (ret == -1) {
69*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL | TERRNO, "splice error");
70*49cdfc7eSAndroid Build Coastguard Worker 			goto exit;
71*49cdfc7eSAndroid Build Coastguard Worker 		}
72*49cdfc7eSAndroid Build Coastguard Worker 	}
73*49cdfc7eSAndroid Build Coastguard Worker 	for (i = num_len_data; i > 0; i = i - ret) {
74*49cdfc7eSAndroid Build Coastguard Worker 		ret = splice(sv[1], 0, pp2[1], NULL, i, 0);
75*49cdfc7eSAndroid Build Coastguard Worker 		if (ret == -1) {
76*49cdfc7eSAndroid Build Coastguard Worker 			if (errno == EINVAL) {
77*49cdfc7eSAndroid Build Coastguard Worker 				tst_res(TCONF, "splice does not support "
78*49cdfc7eSAndroid Build Coastguard Worker 					"af_unix sockets");
79*49cdfc7eSAndroid Build Coastguard Worker 			} else {
80*49cdfc7eSAndroid Build Coastguard Worker 				tst_res(TFAIL | TERRNO, "splice error");
81*49cdfc7eSAndroid Build Coastguard Worker 			}
82*49cdfc7eSAndroid Build Coastguard Worker 			goto exit;
83*49cdfc7eSAndroid Build Coastguard Worker 		}
84*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_READ(1, pp2[0], arr_out + num_len_data - i, ret);
85*49cdfc7eSAndroid Build Coastguard Worker 	}
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < num_len_data; i++) {
88*49cdfc7eSAndroid Build Coastguard Worker 		if (arr_in[i] != arr_out[i]) {
89*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL, "wrong data at %d: expected: %d, "
90*49cdfc7eSAndroid Build Coastguard Worker 				"actual: %d", i, arr_in[i], arr_out[i]);
91*49cdfc7eSAndroid Build Coastguard Worker 			goto exit;
92*49cdfc7eSAndroid Build Coastguard Worker 		}
93*49cdfc7eSAndroid Build Coastguard Worker 	}
94*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "splice(2): pipe <-> socket run pass.");
95*49cdfc7eSAndroid Build Coastguard Worker exit:
96*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < 2; i++) {
97*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(pp1[i]);
98*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(pp2[i]);
99*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(sv[i]);
100*49cdfc7eSAndroid Build Coastguard Worker 	}
101*49cdfc7eSAndroid Build Coastguard Worker }
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
104*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = pipe_socket,
105*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
106*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
107*49cdfc7eSAndroid Build Coastguard Worker 	.options = (struct tst_option[]) {
108*49cdfc7eSAndroid Build Coastguard Worker 		{"l:", &str_len_data, "Length of test data (in bytes)"},
109*49cdfc7eSAndroid Build Coastguard Worker 		{}
110*49cdfc7eSAndroid Build Coastguard Worker 	},
111*49cdfc7eSAndroid Build Coastguard Worker };
112