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