vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 3 | * Copyright (c) International Business Machines Corp., 2006 |
| 4 | * Author Yi Yang <yyangcdl@cn.ibm.com> |
| 5 | * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz> |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 6 | * |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 11 | * |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 15 | * the GNU General Public License for more details. |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 16 | * |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 20 | * |
| 21 | * DESCRIPTION |
| 22 | * This test case will verify basic function of vmsplice |
| 23 | * added by kernel 2.6.17 or up. |
| 24 | * |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 25 | *****************************************************************************/ |
| 26 | |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 27 | #define _GNU_SOURCE |
| 28 | |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 29 | #include <errno.h> |
| 30 | #include <string.h> |
| 31 | #include <signal.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <fcntl.h> |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 34 | #include <sys/poll.h> |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 35 | |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 36 | #include "test.h" |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 37 | #include "linux_syscall_numbers.h" |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 38 | #include "tst_fs_type.h" |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 39 | #include "safe_macros.h" |
| 40 | #include "lapi/fcntl.h" |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 41 | |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 42 | #define TEST_BLOCK_SIZE (1<<17) /* 128K */ |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 43 | |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 44 | static void vmsplice_test(void); |
| 45 | static void setup(void); |
| 46 | static void cleanup(void); |
| 47 | |
| 48 | #define TESTFILE "vmsplice_test_file" |
| 49 | |
| 50 | static int fd_out; |
| 51 | static char buffer[TEST_BLOCK_SIZE]; |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 52 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 53 | char *TCID = "vmsplice01"; |
| 54 | int TST_TOTAL = 1; |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 55 | |
| 56 | int main(int ac, char **av) |
| 57 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 58 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 59 | const char *msg; |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 60 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 61 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 62 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 63 | |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 64 | setup(); |
| 65 | |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 66 | for (lc = 0; TEST_LOOPING(lc); lc++) |
| 67 | vmsplice_test(); |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 68 | |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 69 | cleanup(); |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 70 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 71 | } |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 72 | |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 73 | static void check_file(void) |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 74 | { |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 75 | int i; |
| 76 | char vmsplicebuffer[TEST_BLOCK_SIZE]; |
| 77 | |
| 78 | fd_out = SAFE_OPEN(cleanup, TESTFILE, O_RDONLY); |
| 79 | SAFE_READ(cleanup, 1, fd_out, vmsplicebuffer, TEST_BLOCK_SIZE); |
| 80 | |
| 81 | for (i = 0; i < TEST_BLOCK_SIZE; i++) { |
| 82 | if (buffer[i] != vmsplicebuffer[i]) |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | if (i < TEST_BLOCK_SIZE) |
| 87 | tst_resm(TFAIL, "Wrong data read from the buffer at %i", i); |
| 88 | else |
| 89 | tst_resm(TPASS, "Written data has been read back correctly"); |
| 90 | |
| 91 | close(fd_out); |
| 92 | fd_out = 0; |
| 93 | } |
| 94 | |
| 95 | static void vmsplice_test(void) |
| 96 | { |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 97 | int pipes[2]; |
| 98 | long written; |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 99 | int ret; |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 100 | int fd_out; |
| 101 | struct iovec v; |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 102 | loff_t offset; |
| 103 | |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 104 | v.iov_base = buffer; |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 105 | v.iov_len = TEST_BLOCK_SIZE; |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 106 | |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 107 | SAFE_PIPE(cleanup, pipes); |
| 108 | fd_out = SAFE_OPEN(cleanup, TESTFILE, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 109 | |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 110 | struct pollfd pfd = {.fd = pipes[1], .events = POLLOUT}; |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 111 | offset = 0; |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 112 | |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 113 | while (v.iov_len) { |
| 114 | /* |
| 115 | * in a real app you'd be more clever with poll of course, |
| 116 | * here we are basically just blocking on output room and |
| 117 | * not using the free time for anything interesting. |
| 118 | */ |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 119 | if (poll(&pfd, 1, -1) < 0) |
| 120 | tst_brkm(TBROK | TERRNO, cleanup, "poll() failed"); |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 121 | |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 122 | written = vmsplice(pipes[1], &v, 1, 0); |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 123 | if (written < 0) { |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 124 | tst_brkm(TBROK | TERRNO, cleanup, "vmsplice() failed"); |
| 125 | } else { |
| 126 | if (written == 0) { |
| 127 | break; |
| 128 | } else { |
| 129 | v.iov_base += written; |
| 130 | v.iov_len -= written; |
| 131 | } |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 134 | ret = splice(pipes[0], NULL, fd_out, &offset, written, 0); |
| 135 | if (ret < 0) |
| 136 | tst_brkm(TBROK | TERRNO, cleanup, "splice() failed"); |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 137 | //printf("offset = %lld\n", (long long)offset); |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 138 | } |
| 139 | |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 140 | close(pipes[0]); |
| 141 | close(pipes[1]); |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 142 | close(fd_out); |
| 143 | fd_out = 0; |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 144 | |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 145 | check_file(); |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 148 | static void setup(void) |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 149 | { |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 150 | int i; |
| 151 | |
| 152 | if ((tst_kvercmp(2, 6, 17)) < 0) { |
| 153 | tst_brkm(TCONF, NULL, |
| 154 | "The vmsplice is supported 2.6.17 and newer"); |
| 155 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 156 | |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 157 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 158 | |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 159 | tst_tmpdir(); |
| 160 | |
| 161 | if (tst_fs_type(cleanup, ".") == TST_NFS_MAGIC) { |
| 162 | tst_brkm(TCONF, cleanup, "Cannot do splice() " |
| 163 | "on a file located on an NFS filesystem"); |
| 164 | } |
| 165 | |
| 166 | for (i = 0; i < TEST_BLOCK_SIZE; i++) |
| 167 | buffer[i] = i & 0xff; |
| 168 | |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 169 | TEST_PAUSE; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 170 | } |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 171 | |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 172 | static void cleanup(void) |
vapier | 0b10879 | 2006-09-10 10:12:42 +0000 | [diff] [blame] | 173 | { |
Cyril Hrubis | d458410 | 2014-06-03 12:27:03 +0200 | [diff] [blame] | 174 | if (fd_out > 0 && close(fd_out)) |
| 175 | tst_resm(TWARN, "Failed to close fd_out"); |
| 176 | |
| 177 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 178 | } |