1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2012 Red Hat, Inc.
4 */
5
6 /*\
7 * [Description]
8 *
9 * Bug in the splice code has caused the file position on the write side
10 * of the sendfile system call to be incorrectly set to the read side file
11 * position. This can result in the data being written to an incorrect offset.
12 *
13 * This is a regression test for kernel commit 2cb4b05e76478.
14 */
15
16 #include <stdio.h>
17 #include <string.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 #define TEST_MSG_IN "world"
26 #define TEST_MSG_OUT "hello"
27 #define TEST_MSG_ALL (TEST_MSG_OUT TEST_MSG_IN)
28
29 static int in_fd;
30 static int out_fd;
31
run(void)32 static void run(void)
33 {
34 TEST(sendfile(out_fd, in_fd, NULL, strlen(TEST_MSG_IN)));
35 if (TST_RET == -1)
36 tst_brk(TBROK | TTERRNO, "sendfile() failed");
37
38 char buf[BUFSIZ];
39
40 SAFE_LSEEK(out_fd, 0, SEEK_SET);
41 SAFE_READ(0, out_fd, buf, BUFSIZ);
42
43 if (!strncmp(buf, TEST_MSG_ALL, strlen(TEST_MSG_ALL))) {
44 tst_res(TPASS, "sendfile() copies data correctly");
45 return;
46 }
47
48 tst_res(TFAIL, "sendfile() copies data incorrectly: '%s' expected: '%s%s'",
49 buf, TEST_MSG_OUT, TEST_MSG_IN);
50 }
51
setup(void)52 static void setup(void)
53 {
54 in_fd = SAFE_CREAT(IN_FILE, 0700);
55 SAFE_WRITE(SAFE_WRITE_ALL, in_fd, TEST_MSG_IN, strlen(TEST_MSG_IN));
56 SAFE_CLOSE(in_fd);
57 in_fd = SAFE_OPEN(IN_FILE, O_RDONLY);
58
59 out_fd = SAFE_OPEN(OUT_FILE, O_TRUNC | O_CREAT | O_RDWR, 0777);
60 SAFE_WRITE(SAFE_WRITE_ALL, out_fd, TEST_MSG_OUT, strlen(TEST_MSG_OUT));
61 }
62
cleanup(void)63 static void cleanup(void)
64 {
65 SAFE_CLOSE(in_fd);
66 SAFE_CLOSE(out_fd);
67 }
68
69 static struct tst_test test = {
70 .needs_tmpdir = 1,
71 .setup = setup,
72 .cleanup = cleanup,
73 .test_all = run,
74 .tags = (const struct tst_tag[]) {
75 {"linux-git", "2cb4b05e76478"},
76 {}
77 }
78 };
79