plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
robbiew | 18defb5 | 2001-09-25 17:26:12 +0000 | [diff] [blame] | 22 | * sendfile03.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | * |
| 33 | * USAGE: <for command-line> |
robbiew | 18defb5 | 2001-09-25 17:26:12 +0000 | [diff] [blame] | 34 | * sendfile03 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 35 | * 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> |
plars | 8b6d9bf | 2002-03-05 13:55:58 +0000 | [diff] [blame] | 52 | #include <sys/sendfile.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | #include "test.h" |
| 54 | |
| 55 | #define FAILED 1 |
| 56 | |
subrata_modak | b752e85 | 2007-11-28 11:20:03 +0000 | [diff] [blame] | 57 | #ifndef OFF_T |
| 58 | #define OFF_T off_t |
| 59 | #endif /* Not def: OFF_T */ |
| 60 | |
subrata_modak | 585950c | 2008-08-20 10:55:19 +0000 | [diff] [blame] | 61 | TCID_DEFINE(sendfile03); |
robbiew | 43bedce | 2001-10-17 20:44:45 +0000 | [diff] [blame] | 62 | int TST_TOTAL = 3; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | |
| 64 | int in_fd, out_fd; |
| 65 | char in_file[100], out_file[100]; |
| 66 | |
| 67 | void cleanup(void); |
| 68 | void setup(void); |
| 69 | void setup_func1(void); |
| 70 | |
| 71 | struct test_case_t { |
| 72 | char *desc; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 73 | void (*setupfunc) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | int out_fd; |
| 75 | int in_fd; |
subrata_modak | b752e85 | 2007-11-28 11:20:03 +0000 | [diff] [blame] | 76 | OFF_T *offset; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | int count; |
| 78 | int exp_errno; |
| 79 | } testcases[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 80 | { |
Cyril Hrubis | 4e2bab8 | 2014-09-24 16:34:35 +0200 | [diff] [blame] | 81 | "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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 84 | 0, EBADF} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 87 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | { |
| 89 | int i; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 90 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 91 | const char *msg; /* parse_opts() return message */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 93 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 94 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 95 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | |
| 97 | setup(); |
| 98 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | /* |
| 100 | * The following loop checks looping state if -c option given |
| 101 | */ |
| 102 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 103 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 111 | testcases[i].offset, testcases[i].count)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | |
| 113 | if (TEST_RETURN != -1) { |
| 114 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 115 | continue; |
| 116 | } |
| 117 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | if (TEST_ERRNO != testcases[i].exp_errno) { |
subrata_modak | 498546d | 2007-12-05 08:44:26 +0000 | [diff] [blame] | 119 | tst_resm(TFAIL, "sendfile returned unexpected " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | "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 Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 130 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* |
| 134 | * setup() - performs all ONE TIME setup for this test. |
| 135 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 136 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | { |
| 138 | char buf[100]; |
| 139 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 141 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 151 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | sprintf(buf, "abcdefghijklmnopqrstuvwxyz"); |
| 153 | if (write(in_fd, buf, strlen(buf)) < 0) { |
| 154 | tst_brkm(TBROK, cleanup, "write failed, errno: %d", errno); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 155 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | close(in_fd); |
| 157 | if ((in_fd = open(in_file, O_RDONLY)) < 0) { |
| 158 | tst_brkm(TBROK, cleanup, "open failed, errno: %d", errno); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 159 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | sprintf(out_file, "out.%d", getpid()); |
subrata_modak | e4cf63d | 2008-01-21 11:16:15 +0000 | [diff] [blame] | 161 | if ((out_fd = open(out_file, O_TRUNC | O_CREAT | O_RDWR, 0777)) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | tst_brkm(TBROK, cleanup, "open failed, errno: %d", errno); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 163 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* |
| 167 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 168 | * completion or premature exit. |
| 169 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 170 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | { |
| 172 | /* |
| 173 | * print timing stats if that option was specified. |
| 174 | * print errno log if that option was specified. |
| 175 | */ |
subrata_modak | 8293edd | 2007-08-28 06:45:50 +0000 | [diff] [blame] | 176 | close(out_fd); |
| 177 | close(in_fd); |
mridge | b49a058 | 2004-08-25 16:23:35 +0000 | [diff] [blame] | 178 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | /* delete the test directory created in setup() */ |
| 180 | tst_rmdir(); |
| 181 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 182 | } |