| 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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 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 "usctest.h" |
| 54 | #include "test.h" |
| 55 | |
| 56 | #define FAILED 1 |
| 57 | |
| subrata_modak | b752e85 | 2007-11-28 11:20:03 +0000 | [diff] [blame] | 58 | #ifndef OFF_T |
| 59 | #define OFF_T off_t |
| 60 | #endif /* Not def: OFF_T */ |
| 61 | |
| subrata_modak | 585950c | 2008-08-20 10:55:19 +0000 | [diff] [blame] | 62 | TCID_DEFINE(sendfile03); |
| robbiew | 43bedce | 2001-10-17 20:44:45 +0000 | [diff] [blame] | 63 | int TST_TOTAL = 3; |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | extern int Tst_count; |
| 65 | |
| 66 | int in_fd, out_fd; |
| 67 | char in_file[100], out_file[100]; |
| 68 | |
| 69 | void cleanup(void); |
| 70 | void setup(void); |
| 71 | void setup_func1(void); |
| 72 | |
| 73 | struct test_case_t { |
| 74 | char *desc; |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 75 | void (*setupfunc) (); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | int out_fd; |
| 77 | int in_fd; |
| subrata_modak | b752e85 | 2007-11-28 11:20:03 +0000 | [diff] [blame] | 78 | OFF_T *offset; |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | int count; |
| 80 | int exp_errno; |
| 81 | } testcases[] = { |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 82 | { |
| 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} |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | int exp_enos[] = { EBADF, 0 }; |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
| robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 91 | int main(int ac, char **av) |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | { |
| 93 | int i; |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | int lc; /* loop counter */ |
| 95 | char *msg; /* parse_opts() return message */ |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | |
| Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 97 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
| Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 98 | <<<<<<< HEAD |
| Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 99 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 100 | ======= |
| 101 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 102 | >>>>>>> master |
| 103 | } |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 121 | testcases[i].offset, testcases[i].count)); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | |
| 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_modak | 498546d | 2007-12-05 08:44:26 +0000 | [diff] [blame] | 131 | tst_resm(TFAIL, "sendfile returned unexpected " |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | "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 Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 142 | tst_exit(); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* |
| 146 | * setup() - performs all ONE TIME setup for this test. |
| 147 | */ |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 148 | void setup() |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | { |
| 150 | char buf[100]; |
| 151 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 153 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | 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 Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 163 | } |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | sprintf(buf, "abcdefghijklmnopqrstuvwxyz"); |
| 165 | if (write(in_fd, buf, strlen(buf)) < 0) { |
| 166 | tst_brkm(TBROK, cleanup, "write failed, errno: %d", errno); |
| Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 167 | } |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | close(in_fd); |
| 169 | if ((in_fd = open(in_file, O_RDONLY)) < 0) { |
| 170 | tst_brkm(TBROK, cleanup, "open failed, errno: %d", errno); |
| Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 171 | } |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | sprintf(out_file, "out.%d", getpid()); |
| subrata_modak | e4cf63d | 2008-01-21 11:16:15 +0000 | [diff] [blame] | 173 | 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] | 174 | tst_brkm(TBROK, cleanup, "open failed, errno: %d", errno); |
| Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 175 | } |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* |
| 179 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 180 | * completion or premature exit. |
| 181 | */ |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 182 | void cleanup() |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | { |
| 184 | /* |
| 185 | * print timing stats if that option was specified. |
| 186 | * print errno log if that option was specified. |
| 187 | */ |
| subrata_modak | 8293edd | 2007-08-28 06:45:50 +0000 | [diff] [blame] | 188 | close(out_fd); |
| 189 | close(in_fd); |
| mridge | b49a058 | 2004-08-25 16:23:35 +0000 | [diff] [blame] | 190 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | TEST_CLEANUP; |
| 192 | |
| 193 | /* delete the test directory created in setup() */ |
| 194 | tst_rmdir(); |
| 195 | |
| Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 196 | } |