blob: d56ae241938d205d7fe657aacb0a53ff25c872e4 [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
robbiew18defb52001-09-25 17:26:12 +000022 * sendfile03.c
plars865695b2001-08-27 22:15:12 +000023 *
24 * DESCRIPTION
25 * Testcase to test that sendfile(2) system call returns appropriete
26 * errnos on error.
27 *
28 * ALGORITHM
29 * 1. Call sendfile(2) with out_fd = -1, and expect EBADF to be returned.
30 * 2. Call sendfile(2) with in_fd = -1, and expect EBADF to be returned.
31 * 3. Call sendfile(2) with in_fd = out_fd = -1, and expect EBADF.
plars865695b2001-08-27 22:15:12 +000032 *
33 * USAGE: <for command-line>
robbiew18defb52001-09-25 17:26:12 +000034 * sendfile03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000035 * where, -c n : Run n copies concurrently.
36 * -e : Turn on errno logging.
37 * -i n : Execute test n times.
38 * -I x : Execute test for x seconds.
39 * -P x : Pause for x seconds between iterations.
40 * -t : Turn on syscall timing.
41 *
42 * HISTORY
43 * 07/2001 Ported by Wayne Boyer
44 *
45 * RESTRICTIONS
46 * NONE
47 */
48
49#include <stdio.h>
50#include <errno.h>
51#include <fcntl.h>
plars8b6d9bf2002-03-05 13:55:58 +000052#include <sys/sendfile.h>
plars865695b2001-08-27 22:15:12 +000053#include "test.h"
54
55#define FAILED 1
56
subrata_modakb752e852007-11-28 11:20:03 +000057#ifndef OFF_T
58#define OFF_T off_t
59#endif /* Not def: OFF_T */
60
subrata_modak585950c2008-08-20 10:55:19 +000061TCID_DEFINE(sendfile03);
robbiew43bedce2001-10-17 20:44:45 +000062int TST_TOTAL = 3;
plars865695b2001-08-27 22:15:12 +000063
64int in_fd, out_fd;
65char in_file[100], out_file[100];
66
67void cleanup(void);
68void setup(void);
69void setup_func1(void);
70
71struct test_case_t {
72 char *desc;
subrata_modak56207ce2009-03-23 13:35:39 +000073 void (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +000074 int out_fd;
75 int in_fd;
subrata_modakb752e852007-11-28 11:20:03 +000076 OFF_T *offset;
plars865695b2001-08-27 22:15:12 +000077 int count;
78 int exp_errno;
79} testcases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000080 {
Cyril Hrubis4e2bab82014-09-24 16:34:35 +020081 "Test for EBADF, in_fd = -1", NULL, 8, -1, NULL, 0, EBADF}, {
82 "Test for EBADF, out_fd = -1", NULL, -1, 7, NULL, 0, EBADF}, {
83 "Test for EBADF, in_fd = out_fd = -1", NULL, -1, -1, NULL,
subrata_modak56207ce2009-03-23 13:35:39 +000084 0, EBADF}
plars865695b2001-08-27 22:15:12 +000085};
86
robbiewaa01abd2003-03-27 18:39:24 +000087int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000088{
89 int i;
Cyril Hrubis89af32a2012-10-24 16:39:11 +020090 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020091 const char *msg; /* parse_opts() return message */
plars865695b2001-08-27 22:15:12 +000092
Garrett Cooper45e285d2010-11-22 12:19:25 -080093 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080094 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +080095 }
plars865695b2001-08-27 22:15:12 +000096
97 setup();
98
plars865695b2001-08-27 22:15:12 +000099 /*
100 * The following loop checks looping state if -c option given
101 */
102 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800103 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000104
105 for (i = 0; i < TST_TOTAL; ++i) {
106 if (testcases[i].setupfunc != NULL) {
107 testcases[i].setupfunc();
108 }
109
110 TEST(sendfile(testcases[i].out_fd, testcases[i].in_fd,
subrata_modak56207ce2009-03-23 13:35:39 +0000111 testcases[i].offset, testcases[i].count));
plars865695b2001-08-27 22:15:12 +0000112
113 if (TEST_RETURN != -1) {
114 tst_resm(TFAIL, "call succeeded unexpectedly");
115 continue;
116 }
117
plars865695b2001-08-27 22:15:12 +0000118 if (TEST_ERRNO != testcases[i].exp_errno) {
subrata_modak498546d2007-12-05 08:44:26 +0000119 tst_resm(TFAIL, "sendfile returned unexpected "
plars865695b2001-08-27 22:15:12 +0000120 "errno, expected: %d, got: %d",
121 testcases[i].exp_errno, TEST_ERRNO);
122 } else {
123 tst_resm(TPASS, "sendfile() returned %d : %s",
124 TEST_ERRNO, strerror(TEST_ERRNO));
125 }
126 }
127 }
128 cleanup();
129
Garrett Cooper53740502010-12-16 00:04:01 -0800130 tst_exit();
plars865695b2001-08-27 22:15:12 +0000131}
132
133/*
134 * setup() - performs all ONE TIME setup for this test.
135 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400136void setup(void)
plars865695b2001-08-27 22:15:12 +0000137{
138 char buf[100];
139
plars865695b2001-08-27 22:15:12 +0000140 tst_sig(NOFORK, DEF_HANDLER, cleanup);
141
plars865695b2001-08-27 22:15:12 +0000142 TEST_PAUSE;
143
144 /* make a temporary directory and cd to it */
145 tst_tmpdir();
146
147 sprintf(in_file, "in.%d", getpid());
148 if ((in_fd = creat(in_file, 00700)) < 0) {
149 tst_brkm(TBROK, cleanup, "creat failed in setup, errno: %d",
150 errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151 }
plars865695b2001-08-27 22:15:12 +0000152 sprintf(buf, "abcdefghijklmnopqrstuvwxyz");
153 if (write(in_fd, buf, strlen(buf)) < 0) {
154 tst_brkm(TBROK, cleanup, "write failed, errno: %d", errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800155 }
plars865695b2001-08-27 22:15:12 +0000156 close(in_fd);
157 if ((in_fd = open(in_file, O_RDONLY)) < 0) {
158 tst_brkm(TBROK, cleanup, "open failed, errno: %d", errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800159 }
plars865695b2001-08-27 22:15:12 +0000160 sprintf(out_file, "out.%d", getpid());
subrata_modake4cf63d2008-01-21 11:16:15 +0000161 if ((out_fd = open(out_file, O_TRUNC | O_CREAT | O_RDWR, 0777)) < 0) {
plars865695b2001-08-27 22:15:12 +0000162 tst_brkm(TBROK, cleanup, "open failed, errno: %d", errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800163 }
plars865695b2001-08-27 22:15:12 +0000164}
165
166/*
167 * cleanup() - performs all ONE TIME cleanup for this test at
168 * completion or premature exit.
169 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400170void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000171{
172 /*
173 * print timing stats if that option was specified.
174 * print errno log if that option was specified.
175 */
subrata_modak8293edd2007-08-28 06:45:50 +0000176 close(out_fd);
177 close(in_fd);
mridgeb49a0582004-08-25 16:23:35 +0000178
plars865695b2001-08-27 22:15:12 +0000179 /* delete the test directory created in setup() */
180 tst_rmdir();
181
Chris Dearmanec6edca2012-10-17 19:54:01 -0700182}