blob: ab886c10dffbd8fd80540253213f34ef7dbe4fbf [file] [log] [blame]
Eryu Guan65248a92012-01-11 21:59:26 +08001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Eryu Guan65248a92012-01-11 21:59:26 +080017 */
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
37#include "usctest.h"
38#include "test.h"
39
40#define TEST_MSG_IN "world"
41#define TEST_MSG_OUT "hello"
Helge Deller840aa552013-11-06 20:41:11 +010042#define TEST_MSG_ALL (TEST_MSG_OUT TEST_MSG_IN)
Eryu Guan65248a92012-01-11 21:59:26 +080043
44TCID_DEFINE(sendfile08);
45int TST_TOTAL = 1;
46
47static int in_fd;
48static int out_fd;
49static char *in_file = "sendfile08.in";
50static char *out_file = "sendfile08.out";
51
52static void cleanup(void);
53static void setup(void);
54
55int main(int argc, char *argv[])
56{
57 int lc;
58 int ret;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020059 const char *msg;
Eryu Guan65248a92012-01-11 21:59:26 +080060 char buf[BUFSIZ];
61
62 msg = parse_opts(argc, argv, NULL, NULL);
63 if (msg != NULL)
64 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
65
66 setup();
67
68 for (lc = 0; TEST_LOOPING(lc); lc++) {
69 TEST(sendfile(out_fd, in_fd, NULL, strlen(TEST_MSG_IN)));
70
71 if (TEST_RETURN == -1)
72 tst_brkm(TBROK | TTERRNO, cleanup, "sendfile() failed");
73
74 ret = lseek(out_fd, 0, SEEK_SET);
75 if (ret == -1)
76 tst_brkm(TBROK | TERRNO, cleanup, "lseek %s failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +080077 out_file);
Eryu Guan65248a92012-01-11 21:59:26 +080078 ret = read(out_fd, buf, BUFSIZ);
79 if (ret == -1)
80 tst_brkm(TBROK | TERRNO, cleanup, "read %s failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +080081 out_file);
Eryu Guan65248a92012-01-11 21:59:26 +080082
Helge Deller840aa552013-11-06 20:41:11 +010083 if (!strncmp(buf, TEST_MSG_ALL, strlen(TEST_MSG_ALL)))
Eryu Guan65248a92012-01-11 21:59:26 +080084 tst_resm(TPASS, "sendfile(2) copies data correctly");
85 else
86 tst_resm(TFAIL, "sendfile(2) copies data incorrectly."
Wanlong Gao354ebb42012-12-07 10:10:04 +080087 " Expect \"%s%s\", got \"%s\"", TEST_MSG_OUT,
88 TEST_MSG_IN, buf);
Eryu Guan65248a92012-01-11 21:59:26 +080089 }
90
91 cleanup();
92 tst_exit();
93}
94
95static void setup(void)
96{
97 int ret;
98
Wanlong Gao3ca28752013-06-26 09:30:29 +080099 /* Disable test if the version of the kernel is less than 2.6.33 */
100 if ((tst_kvercmp(2, 6, 33)) < 0) {
101 tst_resm(TCONF, "The out_fd must be socket before kernel");
102 tst_resm(TCONF, "2.6.33, see kernel commit cc56f7d");
103 tst_exit();
104 }
105
Eryu Guan65248a92012-01-11 21:59:26 +0800106 TEST_PAUSE;
107
108 tst_tmpdir();
109
110 in_fd = creat(in_file, 0700);
111 if (in_fd == -1)
112 tst_brkm(TBROK | TERRNO, cleanup, "Create %s failed", in_file);
113
114 ret = write(in_fd, TEST_MSG_IN, strlen(TEST_MSG_IN));
115 if (ret == -1)
116 tst_brkm(TBROK | TERRNO, cleanup, "Write %s failed", in_file);
117 close(in_fd);
118
119 in_fd = open(in_file, O_RDONLY);
120 if (in_fd == -1)
121 tst_brkm(TBROK | TERRNO, cleanup, "Open %s failed", in_file);
122
123 out_fd = open(out_file, O_TRUNC | O_CREAT | O_RDWR, 0777);
124 if (out_fd == -1)
125 tst_brkm(TBROK | TERRNO, cleanup, "Open %s failed", out_file);
126 ret = write(out_fd, TEST_MSG_OUT, strlen(TEST_MSG_OUT));
127 if (ret == -1)
128 tst_brkm(TBROK | TERRNO, cleanup, "Write %s failed", out_file);
129}
130
131static void cleanup(void)
132{
133 close(out_fd);
134 close(in_fd);
135
136 TEST_CLEANUP;
137
138 tst_rmdir();
139}