xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/sendfile/sendfile02.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) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  * 07/2001 Ported by Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker  * 08/2002 Make it use a socket so it works with 2.5 kernel
6*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2021 Xie Ziyao <[email protected]>
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker /*\
10*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Test the basic functionality of the sendfile() system call:
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * 1. Call sendfile() with offset = 0.
15*49cdfc7eSAndroid Build Coastguard Worker  * 2. Call sendfile() with offset in the middle of the file.
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <inttypes.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/sendfile.h>
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #define IN_FILE "in_file"
25*49cdfc7eSAndroid Build Coastguard Worker #define OUT_FILE "out_file"
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker #define OFFSET_DESC(x) .desc = "with offset = "#x, .offset = x
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t {
30*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
31*49cdfc7eSAndroid Build Coastguard Worker 	off_t offset;
32*49cdfc7eSAndroid Build Coastguard Worker 	int64_t count;
33*49cdfc7eSAndroid Build Coastguard Worker 	int64_t exp_retval;
34*49cdfc7eSAndroid Build Coastguard Worker 	int64_t exp_updated_offset;
35*49cdfc7eSAndroid Build Coastguard Worker } tc[] = {
36*49cdfc7eSAndroid Build Coastguard Worker 	{ OFFSET_DESC(0), 26, 26, 26 },
37*49cdfc7eSAndroid Build Coastguard Worker 	{ OFFSET_DESC(2), 24, 24, 26 },
38*49cdfc7eSAndroid Build Coastguard Worker };
39*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)40*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
43*49cdfc7eSAndroid Build Coastguard Worker 	char buf[27];
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_CREAT(IN_FILE, 00700);
46*49cdfc7eSAndroid Build Coastguard Worker 	sprintf(buf, "abcdefghijklmnopqrstuvwxyz");
47*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, strlen(buf));
48*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_CREAT(OUT_FILE, 00700);
51*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
52*49cdfc7eSAndroid Build Coastguard Worker }
53*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int i)54*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int i)
55*49cdfc7eSAndroid Build Coastguard Worker {
56*49cdfc7eSAndroid Build Coastguard Worker 	int in_fd = SAFE_OPEN(IN_FILE, O_RDONLY);
57*49cdfc7eSAndroid Build Coastguard Worker 	int out_fd = SAFE_OPEN(OUT_FILE, O_WRONLY);
58*49cdfc7eSAndroid Build Coastguard Worker 	off_t offset = tc[i].offset;
59*49cdfc7eSAndroid Build Coastguard Worker 	off_t before_pos, after_pos;
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	before_pos = SAFE_LSEEK(in_fd, 0, SEEK_CUR);
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	TEST(sendfile(out_fd, in_fd, &offset, tc[i].count));
64*49cdfc7eSAndroid Build Coastguard Worker 	after_pos = SAFE_LSEEK(in_fd, 0, SEEK_CUR);
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	if (tc[i].exp_retval != TST_RET)
67*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "sendfile() failed to return expected value, "
68*49cdfc7eSAndroid Build Coastguard Worker 			       "expected: %" PRId64 ", got: %ld",
69*49cdfc7eSAndroid Build Coastguard Worker 			tc[i].exp_retval, TST_RET);
70*49cdfc7eSAndroid Build Coastguard Worker 	else if (offset != tc[i].exp_updated_offset)
71*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "sendfile() failed to update OFFSET parameter to "
72*49cdfc7eSAndroid Build Coastguard Worker 			       "expected value, expected: %" PRId64 ", got: %" PRId64,
73*49cdfc7eSAndroid Build Coastguard Worker 			tc[i].exp_updated_offset, (int64_t)(offset));
74*49cdfc7eSAndroid Build Coastguard Worker 	else if (before_pos != after_pos)
75*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "sendfile() updated the file position of in_fd "
76*49cdfc7eSAndroid Build Coastguard Worker 			       "unexpectedly, expected file position: %" PRId64
77*49cdfc7eSAndroid Build Coastguard Worker 			       ", actual file position %" PRId64,
78*49cdfc7eSAndroid Build Coastguard Worker 			(int64_t)(before_pos), (int64_t)(after_pos));
79*49cdfc7eSAndroid Build Coastguard Worker 	else
80*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "sendfile() with %s", tc[i].desc);
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(in_fd);
83*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(out_fd);
84*49cdfc7eSAndroid Build Coastguard Worker }
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
87*49cdfc7eSAndroid Build Coastguard Worker 		.needs_tmpdir = 1,
88*49cdfc7eSAndroid Build Coastguard Worker 		.setup = setup,
89*49cdfc7eSAndroid Build Coastguard Worker 		.test = run,
90*49cdfc7eSAndroid Build Coastguard Worker 		.tcnt = ARRAY_SIZE(tc),
91*49cdfc7eSAndroid Build Coastguard Worker };
92