• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "safe_macros.h"
38 
39 #define TEST_MSG_IN "world"
40 #define TEST_MSG_OUT "hello"
41 #define TEST_MSG_ALL (TEST_MSG_OUT TEST_MSG_IN)
42 
43 TCID_DEFINE(sendfile08);
44 int TST_TOTAL = 1;
45 
46 static int in_fd;
47 static int out_fd;
48 static char *in_file = "sendfile08.in";
49 static char *out_file = "sendfile08.out";
50 
51 static void cleanup(void);
52 static void setup(void);
53 
main(int argc,char * argv[])54 int main(int argc, char *argv[])
55 {
56 	int lc;
57 	int ret;
58 	char buf[BUFSIZ];
59 
60 	tst_parse_opts(argc, argv, NULL, NULL);
61 
62 	setup();
63 
64 	for (lc = 0; TEST_LOOPING(lc); lc++) {
65 		TEST(sendfile(out_fd, in_fd, NULL, strlen(TEST_MSG_IN)));
66 
67 		if (TEST_RETURN == -1)
68 			tst_brkm(TBROK | TTERRNO, cleanup, "sendfile() failed");
69 
70 		ret = SAFE_LSEEK(cleanup, out_fd, 0, SEEK_SET);
71 		ret = read(out_fd, buf, BUFSIZ);
72 		if (ret == -1)
73 			tst_brkm(TBROK | TERRNO, cleanup, "read %s failed",
74 				 out_file);
75 
76 		if (!strncmp(buf, TEST_MSG_ALL, strlen(TEST_MSG_ALL)))
77 			tst_resm(TPASS, "sendfile(2) copies data correctly");
78 		else
79 			tst_resm(TFAIL, "sendfile(2) copies data incorrectly."
80 				 " Expect \"%s%s\", got \"%s\"", TEST_MSG_OUT,
81 				 TEST_MSG_IN, buf);
82 	}
83 
84 	cleanup();
85 	tst_exit();
86 }
87 
setup(void)88 static void setup(void)
89 {
90 	int ret;
91 
92 	/* Disable test if the version of the kernel is less than 2.6.33 */
93 	if ((tst_kvercmp(2, 6, 33)) < 0) {
94 		tst_resm(TCONF, "The out_fd must be socket before kernel");
95 		tst_brkm(TCONF, NULL, "2.6.33, see kernel commit cc56f7d");
96 	}
97 
98 	TEST_PAUSE;
99 
100 	tst_tmpdir();
101 
102 	in_fd = SAFE_CREAT(cleanup, in_file, 0700);
103 
104 	ret = write(in_fd, TEST_MSG_IN, strlen(TEST_MSG_IN));
105 	if (ret == -1)
106 		tst_brkm(TBROK | TERRNO, cleanup, "Write %s failed", in_file);
107 	close(in_fd);
108 
109 	in_fd = SAFE_OPEN(cleanup, in_file, O_RDONLY);
110 	out_fd = SAFE_OPEN(cleanup, out_file, O_TRUNC | O_CREAT | O_RDWR, 0777);
111 
112 	ret = write(out_fd, TEST_MSG_OUT, strlen(TEST_MSG_OUT));
113 	if (ret == -1)
114 		tst_brkm(TBROK | TERRNO, cleanup, "Write %s failed", out_file);
115 }
116 
cleanup(void)117 static void cleanup(void)
118 {
119 	close(out_fd);
120 	close(in_fd);
121 
122 	tst_rmdir();
123 }
124