sendfile07.c revision bdbaec51a423e715c2b03ed9e497e9a1fba6103e
1/*
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
64
65TCID_DEFINE(sendfile07);
66int TST_TOTAL = 1;
67extern int Tst_count;
68
69int in_fd, out_fd = 0, ignored_fd = 0;
70char in_file[100];
71
72/* To make out_fd overflow, write much chars
73 to out_fd. MAX_FILL_DATA_LENGTH defines the `much'. */
74#define MAX_FILL_DATA_LENGTH 0xFFFFFFF
75
76void cleanup(void);
77void setup(void);
78
79int main(int ac, char **av)
80{
81	int lc;				/* loop counter */
82	char *msg;			/* parse_opts() return message */
83
84	if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
85		tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
86		/*NOTREACHED*/
87	}
88
89	setup();
90
91	/*
92	 * The following loop checks looping state if -c option given
93	 */
94	for (lc = 0; TEST_LOOPING(lc); lc++) {
95		Tst_count = 0;
96
97		TEST(sendfile(out_fd, in_fd, NULL, 1));
98
99		if (TEST_RETURN != -1) {
100			tst_resm(TFAIL, "call succeeded unexpectedly");
101			continue;
102		}
103
104		TEST_ERROR_LOG(TEST_ERRNO);
105
106		if (TEST_ERRNO != EAGAIN) {
107			tst_resm(TFAIL, "sendfile returned unexpected "
108				 "errno, expected: %d, got: %d",
109				 EAGAIN, TEST_ERRNO);
110		} else {
111			tst_resm(TPASS, "sendfile() returned %d : %s",
112				 TEST_ERRNO, strerror(TEST_ERRNO));
113		}
114	}
115
116	cleanup();
117
118	/*NOTREACHED*/
119	return 0;
120}
121
122/*
123 * setup() - performs all ONE TIME setup for this test.
124 */
125void
126setup()
127{
128	char buf[100];
129	int  p[2];
130	int  i, r;
131
132	/* capture signals */
133	tst_sig(NOFORK, DEF_HANDLER, cleanup);
134
135	/* Pause if that option was specified */
136	TEST_PAUSE;
137
138	/* make a temporary directory and cd to it */
139	tst_tmpdir();
140
141	sprintf(in_file, "in.%d", getpid());
142	if ((in_fd = creat(in_file, 00700)) < 0) {
143		tst_brkm(TBROK, cleanup, "creat failed in setup, errno: %d",
144			 errno);
145		/*NOTREACHED*/
146	}
147	sprintf(buf, "abcdefghijklmnopqrstuvwxyz");
148	if (write(in_fd, buf, strlen(buf)) < 0) {
149		tst_brkm(TBROK, cleanup, "write failed, errno: %d", errno);
150		/*NOTREACHED*/
151	}
152	close(in_fd);
153	if ((in_fd = open(in_file, O_RDONLY)) < 0) {
154		tst_brkm(TBROK, cleanup, "open failed, errno: %d", errno);
155		/*NOTREACHED*/
156	}
157
158	/* Make fulfilled out_fd. */
159	if (socketpair(PF_UNIX, SOCK_DGRAM, 0, p) < 0) {
160		tst_brkm(TBROK, cleanup, "socketpair failed, errno: %d", errno);
161		/*NOTREACHED*/
162	}
163
164	/* Don't close.
165	   You cannot write nothing on out_fd if ignored_fd is closed.*/
166	ignored_fd = p[0];
167	out_fd = p[1];
168	if (fcntl(out_fd, F_SETFL, O_WRONLY|O_NONBLOCK) < 0) {
169		tst_brkm(TBROK, cleanup, "fcntl failed, errno: %d", errno);
170	}
171
172	i = MAX_FILL_DATA_LENGTH;
173	while (i > 0) {
174		r = write(out_fd, buf, 1);
175		if (r < 0) {
176			if (errno == EAGAIN) {
177				break;
178			} else {
179				tst_brkm(TBROK, cleanup,
180					 "write failed to fill out_fd, errno: %d",
181					 errno);
182			}
183		}
184		i--;
185	}
186	if (i == 0) {
187		tst_brkm(TBROK, cleanup,
188			 "fail to fill out_fd, write %d bytes but EAGAIN it not returned.",
189			 MAX_FILL_DATA_LENGTH);
190	}
191}
192
193/*
194 * cleanup() - performs all ONE TIME cleanup for this test at
195 *	       completion or premature exit.
196 */
197void
198cleanup()
199{
200	/*
201	 * print timing stats if that option was specified.
202	 * print errno log if that option was specified.
203	 */
204	if (out_fd)
205	  close(out_fd);
206	if (ignored_fd)
207	  close(ignored_fd);
208	close(in_fd);
209
210	TEST_CLEANUP;
211
212	/* delete the test directory created in setup() */
213	tst_rmdir();
214
215	/* exit with return code appropriate for results */
216	tst_exit();
217}
218