Eryu Guan | 65248a9 | 2012-01-11 21:59:26 +0800 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
Eryu Guan | 65248a9 | 2012-01-11 21:59:26 +0800 | [diff] [blame] | 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 | |
| 37 | #include "usctest.h" |
| 38 | #include "test.h" |
| 39 | |
| 40 | #define TEST_MSG_IN "world" |
| 41 | #define TEST_MSG_OUT "hello" |
Helge Deller | 840aa55 | 2013-11-06 20:41:11 +0100 | [diff] [blame] | 42 | #define TEST_MSG_ALL (TEST_MSG_OUT TEST_MSG_IN) |
Eryu Guan | 65248a9 | 2012-01-11 21:59:26 +0800 | [diff] [blame] | 43 | |
| 44 | TCID_DEFINE(sendfile08); |
| 45 | int TST_TOTAL = 1; |
| 46 | |
| 47 | static int in_fd; |
| 48 | static int out_fd; |
| 49 | static char *in_file = "sendfile08.in"; |
| 50 | static char *out_file = "sendfile08.out"; |
| 51 | |
| 52 | static void cleanup(void); |
| 53 | static void setup(void); |
| 54 | |
| 55 | int main(int argc, char *argv[]) |
| 56 | { |
| 57 | int lc; |
| 58 | int ret; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 59 | const char *msg; |
Eryu Guan | 65248a9 | 2012-01-11 21:59:26 +0800 | [diff] [blame] | 60 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 77 | out_file); |
Eryu Guan | 65248a9 | 2012-01-11 21:59:26 +0800 | [diff] [blame] | 78 | ret = read(out_fd, buf, BUFSIZ); |
| 79 | if (ret == -1) |
| 80 | tst_brkm(TBROK | TERRNO, cleanup, "read %s failed", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 81 | out_file); |
Eryu Guan | 65248a9 | 2012-01-11 21:59:26 +0800 | [diff] [blame] | 82 | |
Helge Deller | 840aa55 | 2013-11-06 20:41:11 +0100 | [diff] [blame] | 83 | if (!strncmp(buf, TEST_MSG_ALL, strlen(TEST_MSG_ALL))) |
Eryu Guan | 65248a9 | 2012-01-11 21:59:26 +0800 | [diff] [blame] | 84 | tst_resm(TPASS, "sendfile(2) copies data correctly"); |
| 85 | else |
| 86 | tst_resm(TFAIL, "sendfile(2) copies data incorrectly." |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 87 | " Expect \"%s%s\", got \"%s\"", TEST_MSG_OUT, |
| 88 | TEST_MSG_IN, buf); |
Eryu Guan | 65248a9 | 2012-01-11 21:59:26 +0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | cleanup(); |
| 92 | tst_exit(); |
| 93 | } |
| 94 | |
| 95 | static void setup(void) |
| 96 | { |
| 97 | int ret; |
| 98 | |
Wanlong Gao | 3ca2875 | 2013-06-26 09:30:29 +0800 | [diff] [blame] | 99 | /* 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 Guan | 65248a9 | 2012-01-11 21:59:26 +0800 | [diff] [blame] | 106 | 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 | |
| 131 | static void cleanup(void) |
| 132 | { |
| 133 | close(out_fd); |
| 134 | close(in_fd); |
| 135 | |
| 136 | TEST_CLEANUP; |
| 137 | |
| 138 | tst_rmdir(); |
| 139 | } |