blob: 875ecef5dbf3acbeb765a23aa517084ccfc14c52 [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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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]
subrata_modak56207ce2009-03-23 13:35:39 +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>
subrata_modak498546d2007-12-05 08:44:26 +000057#include <unistd.h>
subrata_modak923b23f2009-11-02 13:57:16 +000058#include <inttypes.h>
plars865695b2001-08-27 22:15:12 +000059#include "test.h"
60
subrata_modakb752e852007-11-28 11:20:03 +000061#ifndef OFF_T
62#define OFF_T off_t
63#endif /* Not def: OFF_T */
64
subrata_modak585950c2008-08-20 10:55:19 +000065TCID_DEFINE(sendfile02);
plars865695b2001-08-27 22:15:12 +000066int TST_TOTAL = 4;
plars865695b2001-08-27 22:15:12 +000067
68char in_file[100];
69char out_file[100];
plars16bfb442002-08-05 22:07:16 +000070int out_fd;
robbiew31f6f672003-01-28 14:50:49 +000071pid_t child_pid;
robbiew98270662003-05-14 14:17:45 +000072static int sockfd, s;
subrata_modak56207ce2009-03-23 13:35:39 +000073static struct sockaddr_in sin1; /* shared between do_child and create_server */
plars865695b2001-08-27 22:15:12 +000074
75void cleanup(void);
robbiewd34d5812005-07-11 22:28:09 +000076void do_child(void);
plars865695b2001-08-27 22:15:12 +000077void setup(void);
plars16bfb442002-08-05 22:07:16 +000078int create_server(void);
plars865695b2001-08-27 22:15:12 +000079
80struct test_case_t {
81 char *desc;
82 int offset;
83 int exp_retval;
subrata_modak498546d2007-12-05 08:44:26 +000084 int exp_updated_offset;
plars865695b2001-08-27 22:15:12 +000085} testcases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000086 {
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}
plars865695b2001-08-27 22:15:12 +000091};
92
robbiewd34d5812005-07-11 22:28:09 +000093#ifdef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +000094static char *argv0;
robbiewd34d5812005-07-11 22:28:09 +000095#endif
96
subrata_modakb752e852007-11-28 11:20:03 +000097void do_sendfile(OFF_T offset, int i)
plars865695b2001-08-27 22:15:12 +000098{
plars16bfb442002-08-05 22:07:16 +000099 int in_fd;
plars865695b2001-08-27 22:15:12 +0000100 struct stat sb;
robbiew33b77742002-10-02 14:56:00 +0000101 int wait_status;
robbiew31f6f672003-01-28 14:50:49 +0000102 int wait_stat;
subrata_modak498546d2007-12-05 08:44:26 +0000103 off_t before_pos, after_pos;
104
robbiew31f6f672003-01-28 14:50:49 +0000105 out_fd = create_server();
plars865695b2001-08-27 22:15:12 +0000106
107 if ((in_fd = open(in_file, O_RDONLY)) < 0) {
108 tst_brkm(TBROK, cleanup, "open failed: %d", errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800109 }
plars865695b2001-08-27 22:15:12 +0000110 if (stat(in_file, &sb) < 0) {
111 tst_brkm(TBROK, cleanup, "stat failed: %d", errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800112 }
subrata_modak498546d2007-12-05 08:44:26 +0000113
114 if ((before_pos = lseek(in_fd, 0, SEEK_CUR)) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000115 tst_brkm(TBROK, cleanup,
116 "lseek before invoking sendfile failed: %d", errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800117 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000118
plars865695b2001-08-27 22:15:12 +0000119 TEST(sendfile(out_fd, in_fd, &offset, sb.st_size - offset));
120
subrata_modak498546d2007-12-05 08:44:26 +0000121 if ((after_pos = lseek(in_fd, 0, SEEK_CUR)) < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000122 tst_brkm(TBROK, cleanup,
123 "lseek after invoking sendfile failed: %d", errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800124 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000125
Cyril Hrubise38b9612014-06-02 17:20:57 +0200126 /* 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);
plars865695b2001-08-27 22:15:12 +0000146 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200147 tst_resm(TPASS, "functionality of sendfile() is "
148 "correct");
149 wait_status = waitpid(-1, &wait_stat, 0);
plars865695b2001-08-27 22:15:12 +0000150 }
151
plars865695b2001-08-27 22:15:12 +0000152 close(in_fd);
153}
154
155/*
robbiewd34d5812005-07-11 22:28:09 +0000156 * do_child
157 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400158void do_child(void)
robbiewd34d5812005-07-11 22:28:09 +0000159{
160 int lc;
vapier1968aef2006-05-26 06:35:43 +0000161 socklen_t length;
robbiewd34d5812005-07-11 22:28:09 +0000162 char rbuf[4096];
163
164 for (lc = 0; TEST_LOOPING(lc); lc++) {
165 length = sizeof(sin1);
subrata_modak56207ce2009-03-23 13:35:39 +0000166 recvfrom(sockfd, rbuf, 4096, 0, (struct sockaddr *)&sin1,
167 &length);
robbiewd34d5812005-07-11 22:28:09 +0000168 }
169 exit(0);
170}
171
172/*
plars865695b2001-08-27 22:15:12 +0000173 * setup() - performs all ONE TIME setup for this test.
174 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400175void setup(void)
plars865695b2001-08-27 22:15:12 +0000176{
177 int fd;
178 char buf[100];
179
plars16bfb442002-08-05 22:07:16 +0000180 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000181
plars865695b2001-08-27 22:15:12 +0000182 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);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800190 }
plars865695b2001-08-27 22:15:12 +0000191 sprintf(buf, "abcdefghijklmnopqrstuvwxyz");
192 if (write(fd, buf, strlen(buf)) < 0) {
193 tst_brkm(TBROK, cleanup, "write failed, errno: %d", errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800194 }
plars865695b2001-08-27 22:15:12 +0000195 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 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400203void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000204{
plars865695b2001-08-27 22:15:12 +0000205
plars16bfb442002-08-05 22:07:16 +0000206 close(out_fd);
plars865695b2001-08-27 22:15:12 +0000207 /* delete the test directory created in setup() */
208 tst_rmdir();
209
plars865695b2001-08-27 22:15:12 +0000210}
211
subrata_modak56207ce2009-03-23 13:35:39 +0000212int create_server(void)
213{
214 static int count = 0;
Jan Stancek6e4e77a2014-03-07 11:39:56 +0100215 socklen_t slen = sizeof(sin1);
plars16bfb442002-08-05 22:07:16 +0000216
217 sockfd = socket(PF_INET, SOCK_DGRAM, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000218 if (sockfd < 0) {
plars16bfb442002-08-05 22:07:16 +0000219 tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000220 strerror(errno));
plars16bfb442002-08-05 22:07:16 +0000221 return -1;
222 }
robbiew31f6f672003-01-28 14:50:49 +0000223 sin1.sin_family = AF_INET;
Jan Stancek6e4e77a2014-03-07 11:39:56 +0100224 sin1.sin_port = 0; /* pick random free port */
robbiew31f6f672003-01-28 14:50:49 +0000225 sin1.sin_addr.s_addr = INADDR_ANY;
robbiewe93a3242005-08-31 20:27:12 +0000226 count++;
subrata_modak56207ce2009-03-23 13:35:39 +0000227 if (bind(sockfd, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
plars16bfb442002-08-05 22:07:16 +0000228 tst_brkm(TBROK, cleanup, "call to bind() failed: %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000229 strerror(errno));
plars16bfb442002-08-05 22:07:16 +0000230 return -1;
231 }
Jan Stancek6e4e77a2014-03-07 11:39:56 +0100232 if (getsockname(sockfd, (struct sockaddr *)&sin1, &slen) == -1)
233 tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
234
robbiewd34d5812005-07-11 22:28:09 +0000235 child_pid = FORK_OR_VFORK();
subrata_modak56207ce2009-03-23 13:35:39 +0000236 if (child_pid < 0) {
plars16bfb442002-08-05 22:07:16 +0000237 tst_brkm(TBROK, cleanup, "client/server fork failed: %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000238 strerror(errno));
plars16bfb442002-08-05 22:07:16 +0000239 return -1;
240 }
subrata_modak56207ce2009-03-23 13:35:39 +0000241 if (!child_pid) { /* child */
robbiewd34d5812005-07-11 22:28:09 +0000242#ifdef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +0000243 if (self_exec(argv0, "") < 0) {
robbiewd34d5812005-07-11 22:28:09 +0000244 tst_brkm(TBROK, cleanup, "self_exec failed");
245 return -1;
subrata_modak56207ce2009-03-23 13:35:39 +0000246
plars16bfb442002-08-05 22:07:16 +0000247 }
robbiewd34d5812005-07-11 22:28:09 +0000248#else
249 do_child();
250#endif
plars16bfb442002-08-05 22:07:16 +0000251 }
252
253 s = socket(PF_INET, SOCK_DGRAM, 0);
254 inet_aton("127.0.0.1", &sin1.sin_addr);
subrata_modak56207ce2009-03-23 13:35:39 +0000255 if (s < 0) {
plars16bfb442002-08-05 22:07:16 +0000256 tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000257 strerror(errno));
plars16bfb442002-08-05 22:07:16 +0000258 return -1;
259 }
subrata_modak56207ce2009-03-23 13:35:39 +0000260 if (connect(s, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
plars16bfb442002-08-05 22:07:16 +0000261 tst_brkm(TBROK, cleanup, "call to connect() failed: %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000262 strerror(errno));
plars16bfb442002-08-05 22:07:16 +0000263 }
264 return s;
265
266}
robbiewaa01abd2003-03-27 18:39:24 +0000267
268int main(int ac, char **av)
269{
270 int i;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200271 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200272 const char *msg; /* parse_opts() return message */
robbiewaa01abd2003-03-27 18:39:24 +0000273
Garrett Cooper45e285d2010-11-22 12:19:25 -0800274 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800275 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800276 }
robbiewd34d5812005-07-11 22:28:09 +0000277#ifdef UCLINUX
278 argv0 = av[0];
279 maybe_run_child(&do_child, "");
280#endif
281
robbiewaa01abd2003-03-27 18:39:24 +0000282 setup();
283
284 /*
285 * The following loop checks looping state if -c option given
286 */
287 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800288 tst_count = 0;
robbiewaa01abd2003-03-27 18:39:24 +0000289
290 for (i = 0; i < TST_TOTAL; ++i) {
291 do_sendfile(testcases[i].offset, i);
292 }
293 }
294 cleanup();
295
Garrett Cooper53740502010-12-16 00:04:01 -0800296 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700297}