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 * 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 that sendfile() system call updates file position of in_fd correctly
13*49cdfc7eSAndroid Build Coastguard Worker * when passing NULL as offset.
14*49cdfc7eSAndroid Build Coastguard Worker */
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <inttypes.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/sendfile.h>
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard Worker #define IN_FILE "in_file"
23*49cdfc7eSAndroid Build Coastguard Worker #define OUT_FILE "out_file"
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker static struct stat sb;
26*49cdfc7eSAndroid Build Coastguard Worker
setup(void)27*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
28*49cdfc7eSAndroid Build Coastguard Worker {
29*49cdfc7eSAndroid Build Coastguard Worker int fd;
30*49cdfc7eSAndroid Build Coastguard Worker char buf[27];
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_CREAT(IN_FILE, 00700);
33*49cdfc7eSAndroid Build Coastguard Worker sprintf(buf, "abcdefghijklmnopqrstuvwxyz");
34*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, strlen(buf));
35*49cdfc7eSAndroid Build Coastguard Worker SAFE_FSTAT(fd, &sb);
36*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_CREAT(OUT_FILE, 00700);
39*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
40*49cdfc7eSAndroid Build Coastguard Worker }
41*49cdfc7eSAndroid Build Coastguard Worker
run(void)42*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
43*49cdfc7eSAndroid Build Coastguard Worker {
44*49cdfc7eSAndroid Build Coastguard Worker off_t after_pos;
45*49cdfc7eSAndroid Build Coastguard Worker int in_fd = SAFE_OPEN(IN_FILE, O_RDONLY);
46*49cdfc7eSAndroid Build Coastguard Worker int out_fd = SAFE_OPEN(OUT_FILE, O_WRONLY);
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker TEST(sendfile(out_fd, in_fd, NULL, sb.st_size));
49*49cdfc7eSAndroid Build Coastguard Worker after_pos = SAFE_LSEEK(in_fd, 0, SEEK_CUR);
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker if (sb.st_size != TST_RET)
52*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "sendfile() failed to return expected value, expected: %"
53*49cdfc7eSAndroid Build Coastguard Worker PRId64 ", got: %ld",
54*49cdfc7eSAndroid Build Coastguard Worker sb.st_size, TST_RET);
55*49cdfc7eSAndroid Build Coastguard Worker else if (after_pos != sb.st_size)
56*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "sendfile() updated the file position of in_fd unexpectedly,"
57*49cdfc7eSAndroid Build Coastguard Worker " expected file position: %" PRId64
58*49cdfc7eSAndroid Build Coastguard Worker " actual file position %" PRId64,
59*49cdfc7eSAndroid Build Coastguard Worker (int64_t)(sb.st_size), (int64_t)(after_pos));
60*49cdfc7eSAndroid Build Coastguard Worker else
61*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "sendfile() with offset=NULL");
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(in_fd);
64*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(out_fd);
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
68*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
69*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
70*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
71*49cdfc7eSAndroid Build Coastguard Worker };
72