vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2006 |
| 3 | * Author Yi Yang <yyangcdl@cn.ibm.com> |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 4 | * |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 9 | * |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 14 | * |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | /* |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 20 | * DESCRIPTION |
| 21 | * This test case will verify basic function of splice |
| 22 | * added by kernel 2.6.17 or up. |
| 23 | * |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
Xing Gu | cf90050 | 2014-05-30 16:05:25 +0800 | [diff] [blame] | 26 | #define _GNU_SOURCE |
| 27 | |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 28 | #include <errno.h> |
| 29 | #include <string.h> |
| 30 | #include <signal.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <sys/syscall.h> |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 34 | |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 35 | #include "test.h" |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 36 | #include "safe_macros.h" |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 37 | #include "tst_fs_type.h" |
Cyril Hrubis | 6bd56e6 | 2014-06-03 11:09:35 +0200 | [diff] [blame] | 38 | #include "lapi/splice.h" |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 39 | |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 40 | #define TEST_BLOCK_SIZE 1024 |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 41 | |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 42 | #define TESTFILE1 "splice_testfile_1" |
| 43 | #define TESTFILE2 "splice_testfile_2" |
| 44 | |
| 45 | static void splice_test(void); |
| 46 | static void setup(void); |
| 47 | static void cleanup(void); |
| 48 | static char buffer[TEST_BLOCK_SIZE]; |
| 49 | static int fd_in, fd_out; |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 50 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 51 | char *TCID = "splice01"; |
| 52 | int TST_TOTAL = 1; |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 53 | |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 54 | int main(int ac, char **av) |
| 55 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 56 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 57 | const char *msg; |
mreed10 | 380d3e6 | 2006-09-06 15:38:25 +0000 | [diff] [blame] | 58 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 59 | if ((msg = parse_opts(ac, av, NULL, NULL))) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 60 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 61 | |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 62 | setup(); |
| 63 | |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 64 | for (lc = 0; TEST_LOOPING(lc); lc++) |
| 65 | splice_test(); |
subrata_modak | 5387ba0 | 2008-05-05 08:49:34 +0000 | [diff] [blame] | 66 | |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 67 | cleanup(); |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 68 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 69 | } |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 70 | |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 71 | static void check_file(void) |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 72 | { |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 73 | int i; |
| 74 | char splicebuffer[TEST_BLOCK_SIZE]; |
| 75 | |
| 76 | fd_out = SAFE_OPEN(cleanup, TESTFILE2, O_RDONLY); |
| 77 | SAFE_READ(cleanup, 1, fd_out, splicebuffer, TEST_BLOCK_SIZE); |
| 78 | |
| 79 | for (i = 0; i < TEST_BLOCK_SIZE; i++) { |
| 80 | if (buffer[i] != splicebuffer[i]) |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | if (i < TEST_BLOCK_SIZE) |
| 85 | tst_resm(TFAIL, "Wrong data read from the buffer at %i", i); |
| 86 | else |
| 87 | tst_resm(TPASS, "Written data has been read back correctly"); |
| 88 | |
| 89 | close(fd_out); |
| 90 | fd_out = 0; |
| 91 | } |
| 92 | |
| 93 | static void splice_test(void) |
| 94 | { |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 95 | int pipes[2]; |
| 96 | int ret; |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 97 | |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 98 | fd_in = SAFE_OPEN(cleanup, TESTFILE1, O_RDONLY); |
| 99 | SAFE_PIPE(cleanup, pipes); |
| 100 | fd_out = SAFE_OPEN(cleanup, TESTFILE2, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
subrata_modak | 6a8f17a | 2007-11-14 12:55:07 +0000 | [diff] [blame] | 101 | |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 102 | ret = splice(fd_in, NULL, pipes[1], NULL, TEST_BLOCK_SIZE, 0); |
| 103 | if (ret < 0) |
| 104 | tst_brkm(TBROK | TERRNO, cleanup, "splice(fd_in, pipe) failed"); |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 105 | |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 106 | ret = splice(pipes[0], NULL, fd_out, NULL, TEST_BLOCK_SIZE, 0); |
| 107 | if (ret < 0) |
| 108 | tst_brkm(TBROK | TERRNO, cleanup, "splice(pipe, fd_out) failed"); |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 109 | |
| 110 | close(fd_in); |
| 111 | close(fd_out); |
| 112 | close(pipes[0]); |
| 113 | close(pipes[1]); |
| 114 | |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 115 | fd_out = 0; |
| 116 | fd_in = 0; |
| 117 | |
| 118 | check_file(); |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 121 | static void setup(void) |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 122 | { |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 123 | int i; |
| 124 | |
| 125 | if ((tst_kvercmp(2, 6, 17)) < 0) { |
| 126 | tst_brkm(TCONF, NULL, |
| 127 | "The splice is supported 2.6.17 and newer"); |
| 128 | } |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 129 | |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 130 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 131 | |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 132 | TEST_PAUSE; |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 133 | |
| 134 | tst_tmpdir(); |
| 135 | |
| 136 | if (tst_fs_type(cleanup, ".") == TST_NFS_MAGIC) { |
Xiong Zhou | d623e2c | 2014-09-25 03:24:19 -0400 | [diff] [blame] | 137 | if (tst_kvercmp(2, 6, 32) < 0) |
| 138 | tst_brkm(TCONF, cleanup, "Cannot do splice on a file" |
| 139 | " on NFS filesystem before 2.6.32"); |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | for (i = 0; i < TEST_BLOCK_SIZE; i++) |
| 143 | buffer[i] = i & 0xff; |
| 144 | |
| 145 | fd_in = SAFE_OPEN(cleanup, TESTFILE1, O_WRONLY | O_CREAT | O_TRUNC, 0777); |
| 146 | SAFE_WRITE(cleanup, 1, fd_in, buffer, TEST_BLOCK_SIZE); |
| 147 | SAFE_CLOSE(cleanup, fd_in); |
| 148 | fd_in = 0; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 149 | } |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 150 | |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 151 | static void cleanup(void) |
vapier | c033f49 | 2006-08-21 07:44:43 +0000 | [diff] [blame] | 152 | { |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 153 | if (fd_in > 0 && close(fd_in)) |
| 154 | tst_resm(TWARN, "Failed to close fd_in"); |
subrata_modak | 6a8f17a | 2007-11-14 12:55:07 +0000 | [diff] [blame] | 155 | |
Cyril Hrubis | 3cbe541 | 2014-06-03 14:00:39 +0200 | [diff] [blame] | 156 | if (fd_out > 0 && close(fd_out)) |
| 157 | tst_resm(TWARN, "Failed to close fd_out"); |
| 158 | |
| 159 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 160 | } |