1 /*
2 * Copyright (c) International Business Machines Corp., 2006
3 * Author Yi Yang <yyangcdl@cn.ibm.com>
4 *
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.
9 *
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.
14 *
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 /*
20 * DESCRIPTION
21 * This test case will verify basic function of splice
22 * added by kernel 2.6.17 or up.
23 *
24 */
25
26 #define _GNU_SOURCE
27
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>
34
35 #include "test.h"
36 #include "safe_macros.h"
37 #include "lapi/splice.h"
38
39 #define TEST_BLOCK_SIZE 1024
40
41 #define TESTFILE1 "splice_testfile_1"
42 #define TESTFILE2 "splice_testfile_2"
43
44 static void splice_test(void);
45 static void setup(void);
46 static void cleanup(void);
47 static char buffer[TEST_BLOCK_SIZE];
48 static int fd_in, fd_out;
49
50 char *TCID = "splice01";
51 int TST_TOTAL = 1;
52
main(int ac,char ** av)53 int main(int ac, char **av)
54 {
55 int lc;
56
57 tst_parse_opts(ac, av, NULL, NULL);
58
59 setup();
60
61 for (lc = 0; TEST_LOOPING(lc); lc++)
62 splice_test();
63
64 cleanup();
65 tst_exit();
66 }
67
check_file(void)68 static void check_file(void)
69 {
70 int i;
71 char splicebuffer[TEST_BLOCK_SIZE];
72
73 fd_out = SAFE_OPEN(cleanup, TESTFILE2, O_RDONLY);
74 SAFE_READ(cleanup, 1, fd_out, splicebuffer, TEST_BLOCK_SIZE);
75
76 for (i = 0; i < TEST_BLOCK_SIZE; i++) {
77 if (buffer[i] != splicebuffer[i])
78 break;
79 }
80
81 if (i < TEST_BLOCK_SIZE)
82 tst_resm(TFAIL, "Wrong data read from the buffer at %i", i);
83 else
84 tst_resm(TPASS, "Written data has been read back correctly");
85
86 close(fd_out);
87 fd_out = 0;
88 }
89
splice_test(void)90 static void splice_test(void)
91 {
92 int pipes[2];
93 int ret;
94
95 fd_in = SAFE_OPEN(cleanup, TESTFILE1, O_RDONLY);
96 SAFE_PIPE(cleanup, pipes);
97 fd_out = SAFE_OPEN(cleanup, TESTFILE2, O_WRONLY | O_CREAT | O_TRUNC, 0666);
98
99 ret = splice(fd_in, NULL, pipes[1], NULL, TEST_BLOCK_SIZE, 0);
100 if (ret < 0)
101 tst_brkm(TBROK | TERRNO, cleanup, "splice(fd_in, pipe) failed");
102
103 ret = splice(pipes[0], NULL, fd_out, NULL, TEST_BLOCK_SIZE, 0);
104 if (ret < 0)
105 tst_brkm(TBROK | TERRNO, cleanup, "splice(pipe, fd_out) failed");
106
107 close(fd_in);
108 close(fd_out);
109 close(pipes[0]);
110 close(pipes[1]);
111
112 fd_out = 0;
113 fd_in = 0;
114
115 check_file();
116 }
117
setup(void)118 static void setup(void)
119 {
120 int i;
121
122 if ((tst_kvercmp(2, 6, 17)) < 0) {
123 tst_brkm(TCONF, NULL,
124 "The splice is supported 2.6.17 and newer");
125 }
126
127 tst_sig(NOFORK, DEF_HANDLER, cleanup);
128
129 TEST_PAUSE;
130
131 tst_tmpdir();
132
133 if (tst_fs_type(cleanup, ".") == TST_NFS_MAGIC) {
134 if (tst_kvercmp(2, 6, 32) < 0)
135 tst_brkm(TCONF, cleanup, "Cannot do splice on a file"
136 " on NFS filesystem before 2.6.32");
137 }
138
139 for (i = 0; i < TEST_BLOCK_SIZE; i++)
140 buffer[i] = i & 0xff;
141
142 fd_in = SAFE_OPEN(cleanup, TESTFILE1, O_WRONLY | O_CREAT | O_TRUNC, 0777);
143 SAFE_WRITE(cleanup, 1, fd_in, buffer, TEST_BLOCK_SIZE);
144 SAFE_CLOSE(cleanup, fd_in);
145 fd_in = 0;
146 }
147
cleanup(void)148 static void cleanup(void)
149 {
150 if (fd_in > 0 && close(fd_in))
151 tst_resm(TWARN, "Failed to close fd_in");
152
153 if (fd_out > 0 && close(fd_out))
154 tst_resm(TWARN, "Failed to close fd_out");
155
156 tst_rmdir();
157 }
158