xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/sendfile/sendfile05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  * Copyright (c) Red Hat Inc., 2007
5  * 11/2007 Copyed from sendfile02.c by Masatake YAMATO
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Testcase to test that sendfile(2) system call returns EINVAL when passing
12  * negative offset.
13  *
14  * [Algorithm]
15  *
16  * Call sendfile with offset = -1.
17  */
18 
19 #include <sys/sendfile.h>
20 #include "tst_test.h"
21 
22 static int in_fd;
23 static int out_fd;
24 
setup(void)25 static void setup(void)
26 {
27 	in_fd = SAFE_OPEN("in_file", O_CREAT | O_RDWR, 0600);
28 	out_fd = SAFE_CREAT("out_file", 0600);
29 }
30 
cleanup(void)31 static void cleanup(void)
32 {
33 	SAFE_CLOSE(in_fd);
34 	SAFE_CLOSE(out_fd);
35 }
36 
run(void)37 static void run(void)
38 {
39 	off_t offset = -1;
40 
41 	TST_EXP_FAIL(sendfile(out_fd, in_fd, &offset, 1), EINVAL,
42 		     "sendfile(out, in, &offset, ..) with offset=%lld",
43 		     (long long int)offset);
44 }
45 
46 static struct tst_test test = {
47 	.needs_tmpdir = 1,
48 	.cleanup = cleanup,
49 	.setup = setup,
50 	.test_all = run,
51 };
52