• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
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
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * NAME
22  *	sendfile02.c
23  *
24  * DESCRIPTION
25  *	Testcase to test the basic functionality of the sendfile(2) system call.
26  *
27  * ALGORITHM
28  *	1. call sendfile(2) with offset = 0
29  *	2. call sendfile(2) with offset in the middle of the file
30  *
31  * USAGE:  <for command-line>
32  *  sendfile02 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
33  *     where,
34  *             -f   : Turn off functionality Testing.
35  *             -i n : Execute test n times.
36  *             -I x : Execute test for x seconds.
37  *             -P x : Pause for x seconds between iterations.
38  *             -t   : Turn on syscall timing.
39  *
40  * HISTORY
41  *	07/2001 Ported by Wayne Boyer
42  *	08/2002 Make it use a socket so it works with 2.5 kernel
43  *
44  * RESTRICTIONS
45  *	NONE
46  */
47 #include <stdio.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <sys/stat.h>
51 #include <sys/sendfile.h>
52 #include <sys/types.h>
53 #include <sys/wait.h>
54 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
57 #include <unistd.h>
58 #include <inttypes.h>
59 #include "test.h"
60 #include "safe_macros.h"
61 
62 #ifndef OFF_T
63 #define OFF_T off_t
64 #endif /* Not def: OFF_T */
65 
66 TCID_DEFINE(sendfile02);
67 int TST_TOTAL = 4;
68 
69 char in_file[100];
70 char out_file[100];
71 int out_fd;
72 pid_t child_pid;
73 static int sockfd, s;
74 static struct sockaddr_in sin1;	/* shared between do_child and create_server */
75 
76 void cleanup(void);
77 void do_child(void);
78 void setup(void);
79 int create_server(void);
80 
81 struct test_case_t {
82 	char *desc;
83 	int offset;
84 	int exp_retval;
85 	int exp_updated_offset;
86 } testcases[] = {
87 	{
88 	"Test sendfile(2) with offset = 0", 0, 26, 26}, {
89 	"Test sendfile(2) with offset in the middle of file", 2, 24, 26}, {
90 	"Test sendfile(2) with offset in the middle of file", 4, 22, 26}, {
91 	"Test sendfile(2) with offset in the middle of file", 6, 20, 26}
92 };
93 
94 #ifdef UCLINUX
95 static char *argv0;
96 #endif
97 
do_sendfile(OFF_T offset,int i)98 void do_sendfile(OFF_T offset, int i)
99 {
100 	int in_fd;
101 	struct stat sb;
102 	int wait_status;
103 	int wait_stat;
104 	off_t before_pos, after_pos;
105 
106 	out_fd = create_server();
107 
108 	if ((in_fd = open(in_file, O_RDONLY)) < 0) {
109 		tst_brkm(TBROK, cleanup, "open failed: %d", errno);
110 	}
111 	SAFE_STAT(cleanup, in_file, &sb);
112 
113 	if ((before_pos = lseek(in_fd, 0, SEEK_CUR)) < 0) {
114 		tst_brkm(TBROK, cleanup,
115 			 "lseek before invoking sendfile failed: %d", errno);
116 	}
117 
118 	TEST(sendfile(out_fd, in_fd, &offset, sb.st_size - offset));
119 
120 	if ((after_pos = lseek(in_fd, 0, SEEK_CUR)) < 0) {
121 		tst_brkm(TBROK, cleanup,
122 			 "lseek after invoking sendfile failed: %d", errno);
123 	}
124 
125 	/* Close the sockets */
126 	shutdown(sockfd, SHUT_RDWR);
127 	shutdown(s, SHUT_RDWR);
128 	if (TEST_RETURN != testcases[i].exp_retval) {
129 		tst_resm(TFAIL, "sendfile(2) failed to return "
130 			 "expected value, expected: %d, "
131 			 "got: %ld", testcases[i].exp_retval,
132 			 TEST_RETURN);
133 		kill(child_pid, SIGKILL);
134 	} else if (offset != testcases[i].exp_updated_offset) {
135 		tst_resm(TFAIL, "sendfile(2) failed to update "
136 			 "OFFSET parameter to expected value, "
137 			 "expected: %d, got: %" PRId64,
138 			 testcases[i].exp_updated_offset,
139 			 (int64_t) offset);
140 	} else if (before_pos != after_pos) {
141 		tst_resm(TFAIL, "sendfile(2) updated the file position "
142 			 " of in_fd unexpectedly, expected file position: %"
143 			 PRId64 ", " " actual file position %" PRId64,
144 			 (int64_t) before_pos, (int64_t) after_pos);
145 	} else {
146 		tst_resm(TPASS, "functionality of sendfile() is "
147 			 "correct");
148 		wait_status = waitpid(-1, &wait_stat, 0);
149 	}
150 
151 	close(in_fd);
152 }
153 
154 /*
155  * do_child
156  */
do_child(void)157 void do_child(void)
158 {
159 	int lc;
160 	socklen_t length;
161 	char rbuf[4096];
162 
163 	for (lc = 0; TEST_LOOPING(lc); lc++) {
164 		length = sizeof(sin1);
165 		recvfrom(sockfd, rbuf, 4096, 0, (struct sockaddr *)&sin1,
166 			 &length);
167 	}
168 	exit(0);
169 }
170 
171 /*
172  * setup() - performs all ONE TIME setup for this test.
173  */
setup(void)174 void setup(void)
175 {
176 	int fd;
177 	char buf[100];
178 
179 	tst_sig(FORK, DEF_HANDLER, cleanup);
180 
181 	TEST_PAUSE;
182 
183 	/* make a temporary directory and cd to it */
184 	tst_tmpdir();
185 	sprintf(in_file, "in.%d", getpid());
186 	if ((fd = creat(in_file, 00700)) < 0) {
187 		tst_brkm(TBROK, cleanup, "creat failed in setup, errno: %d",
188 			 errno);
189 	}
190 	sprintf(buf, "abcdefghijklmnopqrstuvwxyz");
191 	if (write(fd, buf, strlen(buf)) < 0) {
192 		tst_brkm(TBROK, cleanup, "write failed, errno: %d", errno);
193 	}
194 	close(fd);
195 	sprintf(out_file, "out.%d", getpid());
196 }
197 
198 /*
199  * cleanup() - performs all ONE TIME cleanup for this test at
200  *	       completion or premature exit.
201  */
cleanup(void)202 void cleanup(void)
203 {
204 
205 	close(out_fd);
206 	/* delete the test directory created in setup() */
207 	tst_rmdir();
208 
209 }
210 
create_server(void)211 int create_server(void)
212 {
213 	static int count = 0;
214 	socklen_t slen = sizeof(sin1);
215 
216 	sockfd = socket(PF_INET, SOCK_DGRAM, 0);
217 	if (sockfd < 0) {
218 		tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
219 			 strerror(errno));
220 		return -1;
221 	}
222 	sin1.sin_family = AF_INET;
223 	sin1.sin_port = 0; /* pick random free port */
224 	sin1.sin_addr.s_addr = INADDR_ANY;
225 	count++;
226 	if (bind(sockfd, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
227 		tst_brkm(TBROK, cleanup, "call to bind() failed: %s",
228 			 strerror(errno));
229 		return -1;
230 	}
231 	SAFE_GETSOCKNAME(cleanup, sockfd, (struct sockaddr *)&sin1, &slen);
232 
233 	child_pid = FORK_OR_VFORK();
234 	if (child_pid < 0) {
235 		tst_brkm(TBROK, cleanup, "client/server fork failed: %s",
236 			 strerror(errno));
237 		return -1;
238 	}
239 	if (!child_pid) {	/* child */
240 #ifdef UCLINUX
241 		if (self_exec(argv0, "") < 0) {
242 			tst_brkm(TBROK, cleanup, "self_exec failed");
243 			return -1;
244 
245 		}
246 #else
247 		do_child();
248 #endif
249 	}
250 
251 	s = socket(PF_INET, SOCK_DGRAM, 0);
252 	inet_aton("127.0.0.1", &sin1.sin_addr);
253 	if (s < 0) {
254 		tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
255 			 strerror(errno));
256 		return -1;
257 	}
258 	SAFE_CONNECT(cleanup, s, (struct sockaddr *)&sin1, sizeof(sin1));
259 	return s;
260 
261 }
262 
main(int ac,char ** av)263 int main(int ac, char **av)
264 {
265 	int i;
266 	int lc;
267 
268 	tst_parse_opts(ac, av, NULL, NULL);
269 #ifdef UCLINUX
270 	argv0 = av[0];
271 	maybe_run_child(&do_child, "");
272 #endif
273 
274 	setup();
275 
276 	/*
277 	 * The following loop checks looping state if -c option given
278 	 */
279 	for (lc = 0; TEST_LOOPING(lc); lc++) {
280 		tst_count = 0;
281 
282 		for (i = 0; i < TST_TOTAL; ++i) {
283 			do_sendfile(testcases[i].offset, i);
284 		}
285 	}
286 	cleanup();
287 
288 	tst_exit();
289 }
290