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
61#ifndef OFF_T
62#define OFF_T off_t
63#endif /* Not def: OFF_T */
64
65TCID_DEFINE(sendfile02);
66int TST_TOTAL = 4;
67
68char in_file[100];
69char out_file[100];
70int out_fd;
71pid_t child_pid;
72static int sockfd, s;
73static struct sockaddr_in sin1;	/* shared between do_child and create_server */
74
75void cleanup(void);
76void do_child(void);
77void setup(void);
78int create_server(void);
79
80struct test_case_t {
81	char *desc;
82	int offset;
83	int exp_retval;
84	int exp_updated_offset;
85} testcases[] = {
86	{
87	"Test sendfile(2) with offset = 0", 0, 26, 26}, {
88	"Test sendfile(2) with offset in the middle of file", 2, 24, 26}, {
89	"Test sendfile(2) with offset in the middle of file", 4, 22, 26}, {
90	"Test sendfile(2) with offset in the middle of file", 6, 20, 26}
91};
92
93#ifdef UCLINUX
94static char *argv0;
95#endif
96
97void do_sendfile(OFF_T offset, int i)
98{
99	int in_fd;
100	struct stat sb;
101	int wait_status;
102	int wait_stat;
103	off_t before_pos, after_pos;
104
105	out_fd = create_server();
106
107	if ((in_fd = open(in_file, O_RDONLY)) < 0) {
108		tst_brkm(TBROK, cleanup, "open failed: %d", errno);
109	}
110	if (stat(in_file, &sb) < 0) {
111		tst_brkm(TBROK, cleanup, "stat failed: %d", errno);
112	}
113
114	if ((before_pos = lseek(in_fd, 0, SEEK_CUR)) < 0) {
115		tst_brkm(TBROK, cleanup,
116			 "lseek before invoking sendfile failed: %d", errno);
117	}
118
119	TEST(sendfile(out_fd, in_fd, &offset, sb.st_size - offset));
120
121	if ((after_pos = lseek(in_fd, 0, SEEK_CUR)) < 0) {
122		tst_brkm(TBROK, cleanup,
123			 "lseek after invoking sendfile failed: %d", errno);
124	}
125
126	/* Close the sockets */
127	shutdown(sockfd, SHUT_RDWR);
128	shutdown(s, SHUT_RDWR);
129	if (TEST_RETURN != testcases[i].exp_retval) {
130		tst_resm(TFAIL, "sendfile(2) failed to return "
131			 "expected value, expected: %d, "
132			 "got: %ld", testcases[i].exp_retval,
133			 TEST_RETURN);
134		kill(child_pid, SIGKILL);
135	} else if (offset != testcases[i].exp_updated_offset) {
136		tst_resm(TFAIL, "sendfile(2) failed to update "
137			 "OFFSET parameter to expected value, "
138			 "expected: %d, got: %" PRId64,
139			 testcases[i].exp_updated_offset,
140			 (int64_t) offset);
141	} else if (before_pos != after_pos) {
142		tst_resm(TFAIL, "sendfile(2) updated the file position "
143			 " of in_fd unexpectedly, expected file position: %"
144			 PRId64 ", " " actual file position %" PRId64,
145			 (int64_t) before_pos, (int64_t) after_pos);
146	} else {
147		tst_resm(TPASS, "functionality of sendfile() is "
148			 "correct");
149		wait_status = waitpid(-1, &wait_stat, 0);
150	}
151
152	close(in_fd);
153}
154
155/*
156 * do_child
157 */
158void do_child(void)
159{
160	int lc;
161	socklen_t length;
162	char rbuf[4096];
163
164	for (lc = 0; TEST_LOOPING(lc); lc++) {
165		length = sizeof(sin1);
166		recvfrom(sockfd, rbuf, 4096, 0, (struct sockaddr *)&sin1,
167			 &length);
168	}
169	exit(0);
170}
171
172/*
173 * setup() - performs all ONE TIME setup for this test.
174 */
175void setup(void)
176{
177	int fd;
178	char buf[100];
179
180	tst_sig(FORK, DEF_HANDLER, cleanup);
181
182	TEST_PAUSE;
183
184	/* make a temporary directory and cd to it */
185	tst_tmpdir();
186	sprintf(in_file, "in.%d", getpid());
187	if ((fd = creat(in_file, 00700)) < 0) {
188		tst_brkm(TBROK, cleanup, "creat failed in setup, errno: %d",
189			 errno);
190	}
191	sprintf(buf, "abcdefghijklmnopqrstuvwxyz");
192	if (write(fd, buf, strlen(buf)) < 0) {
193		tst_brkm(TBROK, cleanup, "write failed, errno: %d", errno);
194	}
195	close(fd);
196	sprintf(out_file, "out.%d", getpid());
197}
198
199/*
200 * cleanup() - performs all ONE TIME cleanup for this test at
201 *	       completion or premature exit.
202 */
203void cleanup(void)
204{
205
206	close(out_fd);
207	/* delete the test directory created in setup() */
208	tst_rmdir();
209
210}
211
212int create_server(void)
213{
214	static int count = 0;
215	socklen_t slen = sizeof(sin1);
216
217	sockfd = socket(PF_INET, SOCK_DGRAM, 0);
218	if (sockfd < 0) {
219		tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
220			 strerror(errno));
221		return -1;
222	}
223	sin1.sin_family = AF_INET;
224	sin1.sin_port = 0; /* pick random free port */
225	sin1.sin_addr.s_addr = INADDR_ANY;
226	count++;
227	if (bind(sockfd, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
228		tst_brkm(TBROK, cleanup, "call to bind() failed: %s",
229			 strerror(errno));
230		return -1;
231	}
232	if (getsockname(sockfd, (struct sockaddr *)&sin1, &slen) == -1)
233		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
234
235	child_pid = FORK_OR_VFORK();
236	if (child_pid < 0) {
237		tst_brkm(TBROK, cleanup, "client/server fork failed: %s",
238			 strerror(errno));
239		return -1;
240	}
241	if (!child_pid) {	/* child */
242#ifdef UCLINUX
243		if (self_exec(argv0, "") < 0) {
244			tst_brkm(TBROK, cleanup, "self_exec failed");
245			return -1;
246
247		}
248#else
249		do_child();
250#endif
251	}
252
253	s = socket(PF_INET, SOCK_DGRAM, 0);
254	inet_aton("127.0.0.1", &sin1.sin_addr);
255	if (s < 0) {
256		tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
257			 strerror(errno));
258		return -1;
259	}
260	if (connect(s, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
261		tst_brkm(TBROK, cleanup, "call to connect() failed: %s",
262			 strerror(errno));
263	}
264	return s;
265
266}
267
268int main(int ac, char **av)
269{
270	int i;
271	int lc;
272
273	tst_parse_opts(ac, av, NULL, NULL);
274#ifdef UCLINUX
275	argv0 = av[0];
276	maybe_run_child(&do_child, "");
277#endif
278
279	setup();
280
281	/*
282	 * The following loop checks looping state if -c option given
283	 */
284	for (lc = 0; TEST_LOOPING(lc); lc++) {
285		tst_count = 0;
286
287		for (i = 0; i < TST_TOTAL; ++i) {
288			do_sendfile(testcases[i].offset, i);
289		}
290	}
291	cleanup();
292
293	tst_exit();
294}
295