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