blob: 08ad3aed9b99e9a4fee3864b9b9f530b84ece6d4 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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]
robbiew98270662003-05-14 14:17:45 +000033 * where,
plars865695b2001-08-27 22:15:12 +000034 * -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
plars16bfb442002-08-05 22:07:16 +000042 * 08/2002 Make it use a socket so it works with 2.5 kernel
plars865695b2001-08-27 22:15:12 +000043 *
44 * RESTRICTIONS
45 * NONE
46 */
47#include <stdio.h>
48#include <errno.h>
49#include <fcntl.h>
50#include <sys/stat.h>
plars8b6d9bf2002-03-05 13:55:58 +000051#include <sys/sendfile.h>
plars16bfb442002-08-05 22:07:16 +000052#include <sys/types.h>
robbiew31f6f672003-01-28 14:50:49 +000053#include <sys/wait.h>
plars16bfb442002-08-05 22:07:16 +000054#include <sys/socket.h>
55#include <netinet/in.h>
robbiew31f6f672003-01-28 14:50:49 +000056#include <arpa/inet.h>
plars865695b2001-08-27 22:15:12 +000057#include "usctest.h"
58#include "test.h"
59
60char *TCID = "sendfile02";
61int TST_TOTAL = 4;
62extern int Tst_count;
63
64char in_file[100];
65char out_file[100];
plars16bfb442002-08-05 22:07:16 +000066int out_fd;
robbiew31f6f672003-01-28 14:50:49 +000067pid_t child_pid;
robbiew98270662003-05-14 14:17:45 +000068static int sockfd, s;
plars865695b2001-08-27 22:15:12 +000069
70void cleanup(void);
71void setup(void);
plars16bfb442002-08-05 22:07:16 +000072int create_server(void);
plars865695b2001-08-27 22:15:12 +000073
74struct test_case_t {
75 char *desc;
76 int offset;
77 int exp_retval;
78} testcases[] = {
79 { "Test sendfile(2) with offset = 0", 0, 26 },
80 { "Test sendfile(2) with offset in the middle of file", 2, 24 },
81 { "Test sendfile(2) with offset in the middle of file", 4, 22 },
82 { "Test sendfile(2) with offset in the middle of file", 6, 20 }
83};
84
robbiewaa01abd2003-03-27 18:39:24 +000085void do_sendfile(off_t offset, int i)
plars865695b2001-08-27 22:15:12 +000086{
plars16bfb442002-08-05 22:07:16 +000087 int in_fd;
plars865695b2001-08-27 22:15:12 +000088 struct stat sb;
robbiew33b77742002-10-02 14:56:00 +000089 int wait_status;
robbiew31f6f672003-01-28 14:50:49 +000090 int wait_stat;
91
92 out_fd = create_server();
plars865695b2001-08-27 22:15:12 +000093
94 if ((in_fd = open(in_file, O_RDONLY)) < 0) {
95 tst_brkm(TBROK, cleanup, "open failed: %d", errno);
96 /*NOTREACHED*/
97 }
98 if (stat(in_file, &sb) < 0) {
99 tst_brkm(TBROK, cleanup, "stat failed: %d", errno);
100 /*NOTREACHED*/
101 }
plars865695b2001-08-27 22:15:12 +0000102
103 TEST(sendfile(out_fd, in_fd, &offset, sb.st_size - offset));
104
105 if (STD_FUNCTIONAL_TEST) {
robbiew98270662003-05-14 14:17:45 +0000106 /* Close the sockets */
107 shutdown(sockfd, SHUT_RDWR);
108 shutdown(s, SHUT_RDWR);
plars865695b2001-08-27 22:15:12 +0000109 if (TEST_RETURN != testcases[i].exp_retval) {
110 tst_resm(TFAIL, "sendfile(2) failed to return "
111 "expected value, expected: %d, "
112 "got: %d", testcases[i].exp_retval,
113 TEST_RETURN);
robbiew31f6f672003-01-28 14:50:49 +0000114 kill(child_pid, SIGKILL);
plars865695b2001-08-27 22:15:12 +0000115 } else {
116 tst_resm(TPASS, "functionality of sendfile() is "
117 "correct");
robbiew31f6f672003-01-28 14:50:49 +0000118 wait_status = waitpid(-1, &wait_stat, 0);
plars865695b2001-08-27 22:15:12 +0000119 }
120 } else {
121 tst_resm(TPASS, "call succeeded");
robbiew98270662003-05-14 14:17:45 +0000122 /* Close the sockets */
123 shutdown(sockfd, SHUT_RDWR);
124 shutdown(s, SHUT_RDWR);
125 if (TEST_RETURN != testcases[i].exp_retval) {
robbiew31f6f672003-01-28 14:50:49 +0000126 kill(child_pid, SIGKILL);
robbiew98270662003-05-14 14:17:45 +0000127 } else {
robbiew31f6f672003-01-28 14:50:49 +0000128 wait_status = waitpid(-1, &wait_stat, 0);
robbiew98270662003-05-14 14:17:45 +0000129 }
plars865695b2001-08-27 22:15:12 +0000130 }
131
plars865695b2001-08-27 22:15:12 +0000132 close(in_fd);
robbiew33b77742002-10-02 14:56:00 +0000133
plars865695b2001-08-27 22:15:12 +0000134}
135
136/*
137 * setup() - performs all ONE TIME setup for this test.
138 */
139void
140setup()
141{
142 int fd;
143 char buf[100];
144
145 /* capture signals */
plars16bfb442002-08-05 22:07:16 +0000146 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000147
148 /* Pause if that option was specified */
149 TEST_PAUSE;
150
151 /* make a temporary directory and cd to it */
152 tst_tmpdir();
153 sprintf(in_file, "in.%d", getpid());
154 if ((fd = creat(in_file, 00700)) < 0) {
155 tst_brkm(TBROK, cleanup, "creat failed in setup, errno: %d",
156 errno);
157 /*NOTREACHED*/
158 }
159 sprintf(buf, "abcdefghijklmnopqrstuvwxyz");
160 if (write(fd, buf, strlen(buf)) < 0) {
161 tst_brkm(TBROK, cleanup, "write failed, errno: %d", errno);
162 /*NOTREACHED*/
163 }
164 close(fd);
165 sprintf(out_file, "out.%d", getpid());
166}
167
168/*
169 * cleanup() - performs all ONE TIME cleanup for this test at
170 * completion or premature exit.
171 */
172void
173cleanup()
174{
175 /*
176 * print timing stats if that option was specified.
177 * print errno log if that option was specified.
178 */
179 TEST_CLEANUP;
180
plars16bfb442002-08-05 22:07:16 +0000181 close(out_fd);
plars865695b2001-08-27 22:15:12 +0000182 /* delete the test directory created in setup() */
183 tst_rmdir();
184
185 /* exit with return code appropriate for results */
186 tst_exit();
187}
188
plars16bfb442002-08-05 22:07:16 +0000189int create_server(void) {
plars16bfb442002-08-05 22:07:16 +0000190 int lc;
robbiew31f6f672003-01-28 14:50:49 +0000191 int length;
plars16bfb442002-08-05 22:07:16 +0000192 char rbuf[4096];
robbiew31f6f672003-01-28 14:50:49 +0000193 struct sockaddr_in sin1;
194 static int count=0;
plars16bfb442002-08-05 22:07:16 +0000195
196 sockfd = socket(PF_INET, SOCK_DGRAM, 0);
197 if(sockfd < 0) {
198 tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
199 strerror(errno));
200 return -1;
201 }
robbiew31f6f672003-01-28 14:50:49 +0000202 sin1.sin_family = AF_INET;
203 sin1.sin_port = htons((getpid() % 32768) + 11000 + count++);
204 sin1.sin_addr.s_addr = INADDR_ANY;
plars16bfb442002-08-05 22:07:16 +0000205 if(bind(sockfd, (struct sockaddr*)&sin1, sizeof(sin1)) < 0) {
206 tst_brkm(TBROK, cleanup, "call to bind() failed: %s",
207 strerror(errno));
208 return -1;
209 }
robbiew31f6f672003-01-28 14:50:49 +0000210 child_pid = fork();
211 if(child_pid < 0) {
plars16bfb442002-08-05 22:07:16 +0000212 tst_brkm(TBROK, cleanup, "client/server fork failed: %s",
213 strerror(errno));
214 return -1;
215 }
robbiew31f6f672003-01-28 14:50:49 +0000216 if(!child_pid) {
plars16bfb442002-08-05 22:07:16 +0000217 for (lc = 0; TEST_LOOPING(lc); lc++) {
robbiew31f6f672003-01-28 14:50:49 +0000218 length = sizeof(sin1);
219 recvfrom(sockfd, rbuf, 4096, 0, (struct sockaddr*)&sin1, &length);
plars16bfb442002-08-05 22:07:16 +0000220 }
221 exit(0);
222 }
223
224 s = socket(PF_INET, SOCK_DGRAM, 0);
225 inet_aton("127.0.0.1", &sin1.sin_addr);
226 if(s < 0) {
227 tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
228 strerror(errno));
229 return -1;
230 }
231 if (connect(s, (struct sockaddr*)&sin1, sizeof(sin1)) < 0) {
232 tst_brkm(TBROK, cleanup, "call to connect() failed: %s",
233 strerror(errno));
234 }
235 return s;
236
237}
robbiewaa01abd2003-03-27 18:39:24 +0000238
239int main(int ac, char **av)
240{
241 int i;
242 int lc; /* loop counter */
243 char *msg; /* parse_opts() return message */
244
245 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
246 tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
247 /*NOTREACHED*/
248 }
249
250 setup();
251
252 /*
253 * The following loop checks looping state if -c option given
254 */
255 for (lc = 0; TEST_LOOPING(lc); lc++) {
256 Tst_count = 0;
257
258 for (i = 0; i < TST_TOTAL; ++i) {
259 do_sendfile(testcases[i].offset, i);
260 }
261 }
262 cleanup();
263
264 /*NOTREACHED*/
265 return(0);
266}
267