1 /*
2 * Copyright (C) 2012 Red Hat, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /*
20 * Bug in the splice code has caused the file position on the write side
21 * of the sendfile system call to be incorrectly set to the read side file
22 * position. This can result in the data being written to an incorrect offset.
23 *
24 * This is a regression test for kernel commit
25 * 2cb4b05e7647891b46b91c07c9a60304803d1688
26 */
27
28 #include <sys/sendfile.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include "test.h"
37
38 #define TEST_MSG_IN "world"
39 #define TEST_MSG_OUT "hello"
40 #define TEST_MSG_ALL (TEST_MSG_OUT TEST_MSG_IN)
41
42 TCID_DEFINE(sendfile08);
43 int TST_TOTAL = 1;
44
45 static int in_fd;
46 static int out_fd;
47 static char *in_file = "sendfile08.in";
48 static char *out_file = "sendfile08.out";
49
50 static void cleanup(void);
51 static void setup(void);
52
main(int argc,char * argv[])53 int main(int argc, char *argv[])
54 {
55 int lc;
56 int ret;
57 char buf[BUFSIZ];
58
59 tst_parse_opts(argc, argv, NULL, NULL);
60
61 setup();
62
63 for (lc = 0; TEST_LOOPING(lc); lc++) {
64 TEST(sendfile(out_fd, in_fd, NULL, strlen(TEST_MSG_IN)));
65
66 if (TEST_RETURN == -1)
67 tst_brkm(TBROK | TTERRNO, cleanup, "sendfile() failed");
68
69 ret = lseek(out_fd, 0, SEEK_SET);
70 if (ret == -1)
71 tst_brkm(TBROK | TERRNO, cleanup, "lseek %s failed",
72 out_file);
73 ret = read(out_fd, buf, BUFSIZ);
74 if (ret == -1)
75 tst_brkm(TBROK | TERRNO, cleanup, "read %s failed",
76 out_file);
77
78 if (!strncmp(buf, TEST_MSG_ALL, strlen(TEST_MSG_ALL)))
79 tst_resm(TPASS, "sendfile(2) copies data correctly");
80 else
81 tst_resm(TFAIL, "sendfile(2) copies data incorrectly."
82 " Expect \"%s%s\", got \"%s\"", TEST_MSG_OUT,
83 TEST_MSG_IN, buf);
84 }
85
86 cleanup();
87 tst_exit();
88 }
89
setup(void)90 static void setup(void)
91 {
92 int ret;
93
94 /* Disable test if the version of the kernel is less than 2.6.33 */
95 if ((tst_kvercmp(2, 6, 33)) < 0) {
96 tst_resm(TCONF, "The out_fd must be socket before kernel");
97 tst_brkm(TCONF, NULL, "2.6.33, see kernel commit cc56f7d");
98 }
99
100 TEST_PAUSE;
101
102 tst_tmpdir();
103
104 in_fd = creat(in_file, 0700);
105 if (in_fd == -1)
106 tst_brkm(TBROK | TERRNO, cleanup, "Create %s failed", in_file);
107
108 ret = write(in_fd, TEST_MSG_IN, strlen(TEST_MSG_IN));
109 if (ret == -1)
110 tst_brkm(TBROK | TERRNO, cleanup, "Write %s failed", in_file);
111 close(in_fd);
112
113 in_fd = open(in_file, O_RDONLY);
114 if (in_fd == -1)
115 tst_brkm(TBROK | TERRNO, cleanup, "Open %s failed", in_file);
116
117 out_fd = open(out_file, O_TRUNC | O_CREAT | O_RDWR, 0777);
118 if (out_fd == -1)
119 tst_brkm(TBROK | TERRNO, cleanup, "Open %s failed", out_file);
120 ret = write(out_fd, TEST_MSG_OUT, strlen(TEST_MSG_OUT));
121 if (ret == -1)
122 tst_brkm(TBROK | TERRNO, cleanup, "Write %s failed", out_file);
123 }
124
cleanup(void)125 static void cleanup(void)
126 {
127 close(out_fd);
128 close(in_fd);
129
130 tst_rmdir();
131 }
132