• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  *
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>
6  *
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.
11  *
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.
16  *
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
20  *
21  * DESCRIPTION
22  *	This test case will verify basic function of tee(2)
23  *	added by kernel 2.6.17 or up.
24  *
25  ***************************************************************************/
26 
27 #define _GNU_SOURCE
28 
29 #include <errno.h>
30 #include <string.h>
31 #include <signal.h>
32 #include <sys/types.h>
33 #include <fcntl.h>
34 
35 #include "tst_test.h"
36 #include "lapi/fcntl.h"
37 #include "lapi/tee.h"
38 #include "lapi/splice.h"
39 
40 #define TEST_BLOCK_SIZE 1024
41 
42 #define TESTFILE1 "tee_test_file_1"
43 #define TESTFILE2 "tee_test_file_2"
44 
45 static int fd_in, fd_out;
46 static char buffer[TEST_BLOCK_SIZE];
47 
check_file(void)48 static void check_file(void)
49 {
50 	int i;
51 	char teebuffer[TEST_BLOCK_SIZE];
52 
53 	fd_out = SAFE_OPEN(TESTFILE2, O_RDONLY);
54 	SAFE_READ(1, fd_out, teebuffer, TEST_BLOCK_SIZE);
55 
56 	for (i = 0; i < TEST_BLOCK_SIZE; i++) {
57 		if (buffer[i] != teebuffer[i])
58 			break;
59 	}
60 
61 	if (i < TEST_BLOCK_SIZE)
62 		tst_res(TFAIL, "Wrong data read from the buffer at %i", i);
63 	else
64 		tst_res(TPASS, "Written data has been read back correctly");
65 
66 	SAFE_CLOSE(fd_out);
67 }
68 
tee_test(void)69 static void tee_test(void)
70 {
71 	int pipe1[2];
72 	int pipe2[2];
73 	int ret = 0;
74 
75 	fd_in = SAFE_OPEN(TESTFILE1, O_RDONLY);
76 	fd_out = SAFE_OPEN(TESTFILE2, O_WRONLY | O_CREAT | O_TRUNC, 0777);
77 
78 	SAFE_PIPE(pipe1);
79 	SAFE_PIPE(pipe2);
80 
81 	ret = splice(fd_in, NULL, pipe1[1], NULL, TEST_BLOCK_SIZE, 0);
82 	if (ret < 0)
83 		tst_brk(TBROK | TERRNO, "splice(fd_in, pipe1) failed");
84 
85 	ret = tee(pipe1[0], pipe2[1], TEST_BLOCK_SIZE, SPLICE_F_NONBLOCK);
86 	if (ret < 0)
87 		tst_brk(TBROK | TERRNO, "tee() failed");
88 
89 	ret = splice(pipe2[0], NULL, fd_out, NULL, TEST_BLOCK_SIZE, 0);
90 	if (ret < 0)
91 		tst_brk(TBROK | TERRNO, "splice(pipe2, fd_out) failed");
92 
93 	SAFE_CLOSE(pipe2[0]);
94 	SAFE_CLOSE(pipe2[1]);
95 	SAFE_CLOSE(pipe1[0]);
96 	SAFE_CLOSE(pipe1[1]);
97 	SAFE_CLOSE(fd_out);
98 	SAFE_CLOSE(fd_in);
99 
100 	check_file();
101 }
102 
setup(void)103 static void setup(void)
104 {
105 	int i;
106 
107 	if (tst_fs_type(".") == TST_NFS_MAGIC) {
108 		if ((tst_kvercmp(2, 6, 32)) < 0)
109 			tst_brk(TCONF, "Cannot do tee on a file"
110 				" on NFS filesystem before 2.6.32");
111 	}
112 
113 	for (i = 0; i < TEST_BLOCK_SIZE; i++)
114 		buffer[i] = i & 0xff;
115 
116 	fd_in = SAFE_OPEN(TESTFILE1, O_WRONLY | O_CREAT | O_TRUNC, 0777);
117 	SAFE_WRITE(1, fd_in, buffer, TEST_BLOCK_SIZE);
118 	SAFE_CLOSE(fd_in);
119 }
120 
cleanup(void)121 static void cleanup(void)
122 {
123 	if (fd_in > 0)
124 		SAFE_CLOSE(fd_in);
125 
126 	if (fd_out > 0)
127 		SAFE_CLOSE(fd_out);
128 }
129 
130 static struct tst_test test = {
131 	.setup = setup,
132 	.cleanup = cleanup,
133 	.test_all = tee_test,
134 	.needs_tmpdir = 1,
135 	.min_kver = "2.6.17",
136 };
137