blob: 78fdb1aa998592a2df3f144cce0ac7e143d57a46 [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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
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 "usctest.h"
54#include "test.h"
55
56#define FAILED 1
57
subrata_modakb752e852007-11-28 11:20:03 +000058#ifndef OFF_T
59#define OFF_T off_t
60#endif /* Not def: OFF_T */
61
subrata_modak585950c2008-08-20 10:55:19 +000062TCID_DEFINE(sendfile03);
robbiew43bedce2001-10-17 20:44:45 +000063int TST_TOTAL = 3;
plars865695b2001-08-27 22:15:12 +000064extern int Tst_count;
65
66int in_fd, out_fd;
67char in_file[100], out_file[100];
68
69void cleanup(void);
70void setup(void);
71void setup_func1(void);
72
73struct test_case_t {
74 char *desc;
subrata_modak56207ce2009-03-23 13:35:39 +000075 void (*setupfunc) ();
plars865695b2001-08-27 22:15:12 +000076 int out_fd;
77 int in_fd;
subrata_modakb752e852007-11-28 11:20:03 +000078 OFF_T *offset;
plars865695b2001-08-27 22:15:12 +000079 int count;
80 int exp_errno;
81} testcases[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000082 {
83 "Test for EBADF, in_fd = -1", NULL, 8, -1, (void *)0, 0, EBADF}, {
84 "Test for EBADF, out_fd = -1", NULL, -1, 7, (void *)0, 0, EBADF}, {
85 "Test for EBADF, in_fd = out_fd = -1", NULL, -1, -1, (void *)0,
86 0, EBADF}
plars865695b2001-08-27 22:15:12 +000087};
88
subrata_modak56207ce2009-03-23 13:35:39 +000089int exp_enos[] = { EBADF, 0 };
plars865695b2001-08-27 22:15:12 +000090
robbiewaa01abd2003-03-27 18:39:24 +000091int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000092{
93 int i;
subrata_modak56207ce2009-03-23 13:35:39 +000094 int lc; /* loop counter */
95 char *msg; /* parse_opts() return message */
plars865695b2001-08-27 22:15:12 +000096
Garrett Cooper45e285d2010-11-22 12:19:25 -080097 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -080098<<<<<<< HEAD
Garrett Cooper60fa8012010-11-22 13:50:58 -080099 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper53740502010-12-16 00:04:01 -0800100=======
101 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
102>>>>>>> master
103 }
plars865695b2001-08-27 22:15:12 +0000104
105 setup();
106
107 TEST_EXP_ENOS(exp_enos);
108
109 /*
110 * The following loop checks looping state if -c option given
111 */
112 for (lc = 0; TEST_LOOPING(lc); lc++) {
113 Tst_count = 0;
114
115 for (i = 0; i < TST_TOTAL; ++i) {
116 if (testcases[i].setupfunc != NULL) {
117 testcases[i].setupfunc();
118 }
119
120 TEST(sendfile(testcases[i].out_fd, testcases[i].in_fd,
subrata_modak56207ce2009-03-23 13:35:39 +0000121 testcases[i].offset, testcases[i].count));
plars865695b2001-08-27 22:15:12 +0000122
123 if (TEST_RETURN != -1) {
124 tst_resm(TFAIL, "call succeeded unexpectedly");
125 continue;
126 }
127
128 TEST_ERROR_LOG(TEST_ERRNO);
129
130 if (TEST_ERRNO != testcases[i].exp_errno) {
subrata_modak498546d2007-12-05 08:44:26 +0000131 tst_resm(TFAIL, "sendfile returned unexpected "
plars865695b2001-08-27 22:15:12 +0000132 "errno, expected: %d, got: %d",
133 testcases[i].exp_errno, TEST_ERRNO);
134 } else {
135 tst_resm(TPASS, "sendfile() returned %d : %s",
136 TEST_ERRNO, strerror(TEST_ERRNO));
137 }
138 }
139 }
140 cleanup();
141
Garrett Cooper53740502010-12-16 00:04:01 -0800142 tst_exit();
plars865695b2001-08-27 22:15:12 +0000143}
144
145/*
146 * setup() - performs all ONE TIME setup for this test.
147 */
subrata_modak56207ce2009-03-23 13:35:39 +0000148void setup()
plars865695b2001-08-27 22:15:12 +0000149{
150 char buf[100];
151
plars865695b2001-08-27 22:15:12 +0000152 tst_sig(NOFORK, DEF_HANDLER, cleanup);
153
plars865695b2001-08-27 22:15:12 +0000154 TEST_PAUSE;
155
156 /* make a temporary directory and cd to it */
157 tst_tmpdir();
158
159 sprintf(in_file, "in.%d", getpid());
160 if ((in_fd = creat(in_file, 00700)) < 0) {
161 tst_brkm(TBROK, cleanup, "creat failed in setup, errno: %d",
162 errno);
Garrett Cooper53740502010-12-16 00:04:01 -0800163 }
plars865695b2001-08-27 22:15:12 +0000164 sprintf(buf, "abcdefghijklmnopqrstuvwxyz");
165 if (write(in_fd, buf, strlen(buf)) < 0) {
166 tst_brkm(TBROK, cleanup, "write failed, errno: %d", errno);
Garrett Cooper53740502010-12-16 00:04:01 -0800167 }
plars865695b2001-08-27 22:15:12 +0000168 close(in_fd);
169 if ((in_fd = open(in_file, O_RDONLY)) < 0) {
170 tst_brkm(TBROK, cleanup, "open failed, errno: %d", errno);
Garrett Cooper53740502010-12-16 00:04:01 -0800171 }
plars865695b2001-08-27 22:15:12 +0000172 sprintf(out_file, "out.%d", getpid());
subrata_modake4cf63d2008-01-21 11:16:15 +0000173 if ((out_fd = open(out_file, O_TRUNC | O_CREAT | O_RDWR, 0777)) < 0) {
plars865695b2001-08-27 22:15:12 +0000174 tst_brkm(TBROK, cleanup, "open failed, errno: %d", errno);
Garrett Cooper53740502010-12-16 00:04:01 -0800175 }
plars865695b2001-08-27 22:15:12 +0000176}
177
178/*
179 * cleanup() - performs all ONE TIME cleanup for this test at
180 * completion or premature exit.
181 */
subrata_modak56207ce2009-03-23 13:35:39 +0000182void cleanup()
plars865695b2001-08-27 22:15:12 +0000183{
184 /*
185 * print timing stats if that option was specified.
186 * print errno log if that option was specified.
187 */
subrata_modak8293edd2007-08-28 06:45:50 +0000188 close(out_fd);
189 close(in_fd);
mridgeb49a0582004-08-25 16:23:35 +0000190
plars865695b2001-08-27 22:15:12 +0000191 TEST_CLEANUP;
192
193 /* delete the test directory created in setup() */
194 tst_rmdir();
195
Garrett Cooper2c282152010-12-16 00:55:50 -0800196}