xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/sendfile/sendfile09.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2014
4  * Copyright (c) Linux Test Project, 2013-2023
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Testcase copied from sendfile02.c to test the basic functionality of
11  * the sendfile() system call on large file. There is a kernel bug which
12  * introduced by commit 8f9c0119d7ba9 and fixed by commit 5d73320a96fcc.
13  *
14  * Only supports 64bit systems.
15  *
16  * [Algorithm]
17  *
18  * 1. Call sendfile() with offset at 0.
19  * 2. Call sendfile() with offset at 3GB.
20  */
21 
22 #include <inttypes.h>
23 #include <sys/sendfile.h>
24 #include "tst_test.h"
25 
26 #define ONE_GB		(INT64_C(1) << 30)
27 #define IN_FILE		"in_file"
28 #define OUT_FILE	"out_file"
29 
30 static struct test_case_t {
31 	char *desc;
32 	off_t offset;
33 	int64_t count;
34 	int64_t exp_retval;
35 	int64_t exp_updated_offset;
36 } tc[] = {
37 	{ "offset at 0", 0, ONE_GB, ONE_GB, ONE_GB },
38 	{ "offset at 3GB", 3 * ONE_GB, ONE_GB, ONE_GB, 4 * ONE_GB }
39 };
40 
setup(void)41 static void setup(void)
42 {
43 	int i, fd;
44 
45 	if (!tst_fs_has_free(".", 5, TST_GB))
46 		tst_brk(TCONF, "Test on large file needs 5G free space");
47 
48 	fd = SAFE_CREAT(IN_FILE, 00700);
49 	for (i = 1; i <= (4 * 1024); ++i) {
50 		SAFE_LSEEK(fd, 1024 * 1024 - 1, SEEK_CUR);
51 		SAFE_WRITE(SAFE_WRITE_ALL, fd, "C", 1);
52 	}
53 	SAFE_CLOSE(fd);
54 
55 	fd = SAFE_CREAT(OUT_FILE, 00700);
56 	SAFE_CLOSE(fd);
57 }
58 
run(unsigned int i)59 static void run(unsigned int i)
60 {
61 	int in_fd = SAFE_OPEN(IN_FILE, O_RDONLY);
62 	int out_fd = SAFE_OPEN(OUT_FILE, O_WRONLY);
63 	off_t offset = tc[i].offset;
64 
65 	off_t before_pos, after_pos;
66 	before_pos = SAFE_LSEEK(in_fd, 0, SEEK_CUR);
67 
68 	TEST(sendfile(out_fd, in_fd, &offset, tc[i].count));
69 	after_pos = SAFE_LSEEK(in_fd, 0, SEEK_CUR);
70 
71 	if (TST_RET != tc[i].exp_retval)
72 		tst_res(TFAIL, "sendfile() failed to return expected value, "
73 			       "expected: %" PRId64 ", got: %ld",
74 			tc[i].exp_retval, TST_RET);
75 	else if (offset != tc[i].exp_updated_offset)
76 		tst_res(TFAIL, "sendfile() failed to update OFFSET parameter to "
77 			       "expected value, expected: %" PRId64 ", got: %" PRId64,
78 			tc[i].exp_updated_offset, (int64_t)(offset));
79 	else if (before_pos != after_pos)
80 		tst_res(TFAIL, "sendfile() updated the file position of in_fd "
81 			       "unexpectedly, expected file position: %" PRId64
82 			       ", actual file position %" PRId64,
83 			(int64_t)(before_pos), (int64_t)(after_pos));
84 	else
85 		tst_res(TPASS, "sendfile() with %s", tc[i].desc);
86 
87 	SAFE_CLOSE(in_fd);
88 	SAFE_CLOSE(out_fd);
89 }
90 
91 static struct tst_test test = {
92 	.needs_tmpdir = 1,
93 	.setup = setup,
94 	.test = run,
95 	.tcnt = ARRAY_SIZE(tc),
96 	.max_runtime = 120,
97 	.skip_in_compat = 1,
98 	.tags = (const struct tst_tag[]) {
99 		{"linux-git", "5d73320a96fcc"},
100 		{}
101 	}
102 };
103