blob: 42c45fab1800302549b6593827411ceedcada393 [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
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/*
22 * NAME
23 * sendfile07.c
24 *
25 * DESCRIPTION
26 * Testcase to test that sendfile(2) system call returns EAGAIN
27 * when passing blocked out_fd. Here out_fd is opend with O_NONBLOCK.
28 *
29 * ALGORITHM
30 * 1. Make sockets with socketpair(&p). Use p[1] as out_fd.
31 * 2. Set O_NONBLOCK flag of out_fd on.
32 * 3. Write much datum to out_fd till write() returns EAGAIN.
33 * 4. Call sendfile with out_fd, and expect EAGAIN.
34 *
35 * USAGE: <for command-line>
36 * sendfile07 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
37 * where, -c n : Run n copies concurrently.
38 * -e : Turn on errno logging.
39 * -i n : Execute test n times.
40 * -I x : Execute test for x seconds.
41 * -P x : Pause for x seconds between iterations.
42 * -t : Turn on syscall timing.
43 *
44 * HISTORY
45 * 12/2007 Copyed from sendfile03.c by Masatake YAMATO
46 *
47 * RESTRICTIONS
48 * NONE
49 */
50
51#include <stdio.h>
52#include <errno.h>
53#include <fcntl.h>
54#include <sys/sendfile.h>
55#include <sys/socket.h>
56
57#include "usctest.h"
58#include "test.h"
59
60#ifndef OFF_T
61#define OFF_T off_t
62#endif /* Not def: OFF_T */
63
subrata_modak585950c2008-08-20 10:55:19 +000064TCID_DEFINE(sendfile07);
subrata_modak498546d2007-12-05 08:44:26 +000065int TST_TOTAL = 1;
66extern int Tst_count;
67
68int in_fd, out_fd = 0, ignored_fd = 0;
69char in_file[100];
70
71/* To make out_fd overflow, write much chars
72 to out_fd. MAX_FILL_DATA_LENGTH defines the `much'. */
73#define MAX_FILL_DATA_LENGTH 0xFFFFFFF
74
75void cleanup(void);
76void setup(void);
77
78int main(int ac, char **av)
79{
subrata_modak56207ce2009-03-23 13:35:39 +000080 int lc; /* loop counter */
81 char *msg; /* parse_opts() return message */
subrata_modak498546d2007-12-05 08:44:26 +000082
Garrett Coopere1f008e2010-12-14 00:21:59 -080083 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
subrata_modak498546d2007-12-05 08:44:26 +000084 tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
subrata_modak56207ce2009-03-23 13:35:39 +000085 /*NOTREACHED*/}
subrata_modak498546d2007-12-05 08:44:26 +000086
87 setup();
88
89 /*
90 * The following loop checks looping state if -c option given
91 */
92 for (lc = 0; TEST_LOOPING(lc); lc++) {
93 Tst_count = 0;
94
95 TEST(sendfile(out_fd, in_fd, NULL, 1));
96
97 if (TEST_RETURN != -1) {
98 tst_resm(TFAIL, "call succeeded unexpectedly");
99 continue;
100 }
101
102 TEST_ERROR_LOG(TEST_ERRNO);
103
104 if (TEST_ERRNO != EAGAIN) {
105 tst_resm(TFAIL, "sendfile returned unexpected "
106 "errno, expected: %d, got: %d",
107 EAGAIN, TEST_ERRNO);
108 } else {
109 tst_resm(TPASS, "sendfile() returned %d : %s",
110 TEST_ERRNO, strerror(TEST_ERRNO));
111 }
112 }
113
114 cleanup();
115
subrata_modak56207ce2009-03-23 13:35:39 +0000116 /*NOTREACHED*/ return 0;
subrata_modak498546d2007-12-05 08:44:26 +0000117}
118
119/*
120 * setup() - performs all ONE TIME setup for this test.
121 */
subrata_modak56207ce2009-03-23 13:35:39 +0000122void setup()
subrata_modak498546d2007-12-05 08:44:26 +0000123{
124 char buf[100];
subrata_modak56207ce2009-03-23 13:35:39 +0000125 int p[2];
126 int i, r;
subrata_modakbdbaec52009-02-26 12:14:51 +0000127
subrata_modak498546d2007-12-05 08:44:26 +0000128 /* capture signals */
129 tst_sig(NOFORK, DEF_HANDLER, cleanup);
130
131 /* Pause if that option was specified */
132 TEST_PAUSE;
133
134 /* make a temporary directory and cd to it */
135 tst_tmpdir();
136
137 sprintf(in_file, "in.%d", getpid());
138 if ((in_fd = creat(in_file, 00700)) < 0) {
139 tst_brkm(TBROK, cleanup, "creat failed in setup, errno: %d",
140 errno);
subrata_modak56207ce2009-03-23 13:35:39 +0000141 /*NOTREACHED*/}
subrata_modak498546d2007-12-05 08:44:26 +0000142 sprintf(buf, "abcdefghijklmnopqrstuvwxyz");
143 if (write(in_fd, buf, strlen(buf)) < 0) {
144 tst_brkm(TBROK, cleanup, "write failed, errno: %d", errno);
subrata_modak56207ce2009-03-23 13:35:39 +0000145 /*NOTREACHED*/}
subrata_modak498546d2007-12-05 08:44:26 +0000146 close(in_fd);
147 if ((in_fd = open(in_file, O_RDONLY)) < 0) {
148 tst_brkm(TBROK, cleanup, "open failed, errno: %d", errno);
subrata_modak56207ce2009-03-23 13:35:39 +0000149 /*NOTREACHED*/}
subrata_modakbdbaec52009-02-26 12:14:51 +0000150
subrata_modak498546d2007-12-05 08:44:26 +0000151 /* Make fulfilled out_fd. */
152 if (socketpair(PF_UNIX, SOCK_DGRAM, 0, p) < 0) {
153 tst_brkm(TBROK, cleanup, "socketpair failed, errno: %d", errno);
subrata_modak56207ce2009-03-23 13:35:39 +0000154 /*NOTREACHED*/}
subrata_modakbdbaec52009-02-26 12:14:51 +0000155
subrata_modak498546d2007-12-05 08:44:26 +0000156 /* Don't close.
subrata_modak56207ce2009-03-23 13:35:39 +0000157 You cannot write nothing on out_fd if ignored_fd is closed. */
subrata_modak498546d2007-12-05 08:44:26 +0000158 ignored_fd = p[0];
159 out_fd = p[1];
subrata_modak56207ce2009-03-23 13:35:39 +0000160 if (fcntl(out_fd, F_SETFL, O_WRONLY | O_NONBLOCK) < 0) {
subrata_modak498546d2007-12-05 08:44:26 +0000161 tst_brkm(TBROK, cleanup, "fcntl failed, errno: %d", errno);
162 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000163
subrata_modak498546d2007-12-05 08:44:26 +0000164 i = MAX_FILL_DATA_LENGTH;
165 while (i > 0) {
166 r = write(out_fd, buf, 1);
167 if (r < 0) {
168 if (errno == EAGAIN) {
169 break;
170 } else {
subrata_modak4bb656a2009-02-26 12:02:09 +0000171 tst_brkm(TBROK, cleanup,
172 "write failed to fill out_fd, errno: %d",
subrata_modak498546d2007-12-05 08:44:26 +0000173 errno);
174 }
175 }
176 i--;
177 }
178 if (i == 0) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000179 tst_brkm(TBROK, cleanup,
180 "fail to fill out_fd, write %d bytes but EAGAIN it not returned.",
subrata_modak498546d2007-12-05 08:44:26 +0000181 MAX_FILL_DATA_LENGTH);
182 }
183}
184
185/*
186 * cleanup() - performs all ONE TIME cleanup for this test at
187 * completion or premature exit.
188 */
subrata_modak56207ce2009-03-23 13:35:39 +0000189void cleanup()
subrata_modak498546d2007-12-05 08:44:26 +0000190{
191 /*
192 * print timing stats if that option was specified.
193 * print errno log if that option was specified.
194 */
195 if (out_fd)
subrata_modak56207ce2009-03-23 13:35:39 +0000196 close(out_fd);
subrata_modak498546d2007-12-05 08:44:26 +0000197 if (ignored_fd)
subrata_modak56207ce2009-03-23 13:35:39 +0000198 close(ignored_fd);
subrata_modak498546d2007-12-05 08:44:26 +0000199 close(in_fd);
200
201 TEST_CLEANUP;
202
203 /* delete the test directory created in setup() */
204 tst_rmdir();
205
206 /* exit with return code appropriate for results */
207 tst_exit();
208}