blob: ce8b01d181966ca6461b5c0c9d07230fc576debc [file] [log] [blame]
subrata_modak498546d2007-12-05 08:44:26 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 * Copyright (c) Red Hat Inc., 2007
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
subrata_modak498546d2007-12-05 08:44:26 +000019 */
20
21/*
22 * NAME
23 * sendfile05.c
24 *
25 * DESCRIPTION
26 * Testcase to test that sendfile(2) system call returns EINVAL
27 * when passing negative offset.
28 *
29 * USAGE: <for command-line>
30 * sendfile05 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
subrata_modak56207ce2009-03-23 13:35:39 +000031 * where,
subrata_modak498546d2007-12-05 08:44:26 +000032 * -f : Turn off functionality Testing.
33 * -i n : Execute test n times.
34 * -I x : Execute test for x seconds.
35 * -P x : Pause for x seconds between iterations.
36 * -t : Turn on syscall timing.
37 *
38 * HISTORY
39 * 11/2007 Copyed from sendfile02.c by Masatake YAMATO
40 *
41 * RESTRICTIONS
42 * NONE
43 */
44#include <stdio.h>
45#include <errno.h>
46#include <fcntl.h>
47#include <sys/stat.h>
48#include <sys/sendfile.h>
49#include <sys/types.h>
50#include <sys/socket.h>
51#include <sys/mman.h>
52#include <netinet/in.h>
53#include <arpa/inet.h>
subrata_modak498546d2007-12-05 08:44:26 +000054#include "test.h"
55
56#ifndef OFF_T
57#define OFF_T off_t
58#endif /* Not def: OFF_T */
59
subrata_modak585950c2008-08-20 10:55:19 +000060TCID_DEFINE(sendfile05);
subrata_modak498546d2007-12-05 08:44:26 +000061
62char in_file[100];
63char out_file[100];
64int out_fd;
65pid_t child_pid;
66static int sockfd, s;
subrata_modak56207ce2009-03-23 13:35:39 +000067static struct sockaddr_in sin1; /* shared between do_child and create_server */
subrata_modak498546d2007-12-05 08:44:26 +000068
69void cleanup(void);
70void do_child(void);
71void setup(void);
72int create_server(void);
73
74int TST_TOTAL = 1;
75
subrata_modak498546d2007-12-05 08:44:26 +000076#ifdef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +000077static char *argv0;
subrata_modak498546d2007-12-05 08:44:26 +000078#endif
79
80void do_sendfile(void)
81{
82 OFF_T offset;
83 int in_fd;
84 struct stat sb;
85
86 out_fd = create_server();
87
88 if ((in_fd = open(in_file, O_RDONLY)) < 0) {
89 tst_brkm(TBROK, cleanup, "open failed: %d", errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +080090 }
subrata_modak498546d2007-12-05 08:44:26 +000091 if (stat(in_file, &sb) < 0) {
92 tst_brkm(TBROK, cleanup, "stat failed: %d", errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +080093 }
subrata_modak498546d2007-12-05 08:44:26 +000094
95 offset = -1;
96 TEST(sendfile(out_fd, in_fd, &offset, sb.st_size));
97
98 if (TEST_RETURN != -1) {
99 tst_resm(TFAIL, "call succeeded unexpectedly");
100 } else {
subrata_modak498546d2007-12-05 08:44:26 +0000101 if (TEST_ERRNO != EINVAL) {
102 tst_resm(TFAIL, "sendfile returned unexpected "
103 "errno, expected: %d, got: %d",
104 EINVAL, TEST_ERRNO);
105 } else {
106 tst_resm(TPASS, "sendfile() returned %d : %s",
107 TEST_ERRNO, strerror(TEST_ERRNO));
108 }
109 }
110
111 shutdown(sockfd, SHUT_RDWR);
112 shutdown(s, SHUT_RDWR);
113 kill(child_pid, SIGKILL);
114 close(in_fd);
115}
116
117/*
118 * do_child
119 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400120void do_child(void)
subrata_modak498546d2007-12-05 08:44:26 +0000121{
122 int lc;
123 socklen_t length;
124 char rbuf[4096];
125
126 for (lc = 0; TEST_LOOPING(lc); lc++) {
127 length = sizeof(sin1);
subrata_modak56207ce2009-03-23 13:35:39 +0000128 recvfrom(sockfd, rbuf, 4096, 0, (struct sockaddr *)&sin1,
129 &length);
subrata_modak498546d2007-12-05 08:44:26 +0000130 }
131 exit(0);
132}
133
134/*
135 * setup() - performs all ONE TIME setup for this test.
136 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400137void setup(void)
subrata_modak498546d2007-12-05 08:44:26 +0000138{
139 int fd;
140 char buf[100];
141
subrata_modak498546d2007-12-05 08:44:26 +0000142 tst_sig(FORK, DEF_HANDLER, cleanup);
143
subrata_modak498546d2007-12-05 08:44:26 +0000144 TEST_PAUSE;
145
146 /* make a temporary directory and cd to it */
147 tst_tmpdir();
148 sprintf(in_file, "in.%d", getpid());
149 if ((fd = creat(in_file, 00700)) < 0) {
150 tst_brkm(TBROK, cleanup, "creat failed in setup, errno: %d",
151 errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800152 }
subrata_modak498546d2007-12-05 08:44:26 +0000153 sprintf(buf, "abcdefghijklmnopqrstuvwxyz");
154 if (write(fd, buf, strlen(buf)) < 0) {
155 tst_brkm(TBROK, cleanup, "write failed, errno: %d", errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800156 }
subrata_modak498546d2007-12-05 08:44:26 +0000157 close(fd);
158 sprintf(out_file, "out.%d", getpid());
159}
160
161/*
162 * cleanup() - performs all ONE TIME cleanup for this test at
163 * completion or premature exit.
164 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400165void cleanup(void)
subrata_modak498546d2007-12-05 08:44:26 +0000166{
subrata_modak498546d2007-12-05 08:44:26 +0000167
168 close(out_fd);
169 /* delete the test directory created in setup() */
170 tst_rmdir();
171
subrata_modak498546d2007-12-05 08:44:26 +0000172}
173
subrata_modak56207ce2009-03-23 13:35:39 +0000174int create_server(void)
175{
176 static int count = 0;
Jan Stancek6e4e77a2014-03-07 11:39:56 +0100177 socklen_t slen = sizeof(sin1);
subrata_modak498546d2007-12-05 08:44:26 +0000178
179 sockfd = socket(PF_INET, SOCK_DGRAM, 0);
subrata_modak56207ce2009-03-23 13:35:39 +0000180 if (sockfd < 0) {
subrata_modak498546d2007-12-05 08:44:26 +0000181 tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000182 strerror(errno));
subrata_modak498546d2007-12-05 08:44:26 +0000183 return -1;
184 }
185 sin1.sin_family = AF_INET;
Jan Stancek6e4e77a2014-03-07 11:39:56 +0100186 sin1.sin_port = 0; /* pick random free port */
subrata_modak498546d2007-12-05 08:44:26 +0000187 sin1.sin_addr.s_addr = INADDR_ANY;
188 count++;
subrata_modak56207ce2009-03-23 13:35:39 +0000189 if (bind(sockfd, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
subrata_modak498546d2007-12-05 08:44:26 +0000190 tst_brkm(TBROK, cleanup, "call to bind() failed: %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000191 strerror(errno));
subrata_modak498546d2007-12-05 08:44:26 +0000192 return -1;
193 }
Jan Stancek6e4e77a2014-03-07 11:39:56 +0100194 if (getsockname(sockfd, (struct sockaddr *)&sin1, &slen) == -1)
195 tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
196
subrata_modak498546d2007-12-05 08:44:26 +0000197 child_pid = FORK_OR_VFORK();
subrata_modak56207ce2009-03-23 13:35:39 +0000198 if (child_pid < 0) {
subrata_modak498546d2007-12-05 08:44:26 +0000199 tst_brkm(TBROK, cleanup, "client/server fork failed: %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000200 strerror(errno));
subrata_modak498546d2007-12-05 08:44:26 +0000201 return -1;
202 }
subrata_modak56207ce2009-03-23 13:35:39 +0000203 if (!child_pid) { /* child */
subrata_modak498546d2007-12-05 08:44:26 +0000204#ifdef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +0000205 if (self_exec(argv0, "") < 0) {
subrata_modak498546d2007-12-05 08:44:26 +0000206 tst_brkm(TBROK, cleanup, "self_exec failed");
207 return -1;
subrata_modak56207ce2009-03-23 13:35:39 +0000208
subrata_modak498546d2007-12-05 08:44:26 +0000209 }
210#else
211 do_child();
212#endif
213 }
214
215 s = socket(PF_INET, SOCK_DGRAM, 0);
216 inet_aton("127.0.0.1", &sin1.sin_addr);
subrata_modak56207ce2009-03-23 13:35:39 +0000217 if (s < 0) {
subrata_modak498546d2007-12-05 08:44:26 +0000218 tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000219 strerror(errno));
subrata_modak498546d2007-12-05 08:44:26 +0000220 return -1;
221 }
subrata_modak56207ce2009-03-23 13:35:39 +0000222 if (connect(s, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
subrata_modak498546d2007-12-05 08:44:26 +0000223 tst_brkm(TBROK, cleanup, "call to connect() failed: %s",
subrata_modak56207ce2009-03-23 13:35:39 +0000224 strerror(errno));
subrata_modak498546d2007-12-05 08:44:26 +0000225 }
226 return s;
227
228}
229
230int main(int ac, char **av)
231{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200232 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200233 const char *msg; /* parse_opts() return message */
subrata_modak498546d2007-12-05 08:44:26 +0000234
Garrett Cooper45e285d2010-11-22 12:19:25 -0800235 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800236 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800237 }
subrata_modak498546d2007-12-05 08:44:26 +0000238#ifdef UCLINUX
239 argv0 = av[0];
240 maybe_run_child(&do_child, "");
241#endif
242
243 setup();
244
245 /*
246 * The following loop checks looping state if -c option given
247 */
248 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800249 tst_count = 0;
subrata_modak498546d2007-12-05 08:44:26 +0000250
251 do_sendfile();
252 }
253 cleanup();
254
Garrett Cooper53740502010-12-16 00:04:01 -0800255 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700256}