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 |
| 22 | * writev05.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * These testcases are written to test writev() on sparse files. This |
| 26 | * is same as writev02.c. But the initial write() with valid data is |
| 27 | * done at the beginning of the file. |
| 28 | * |
| 29 | * USAGE: <for command-line> |
| 30 | * writev05 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 31 | * where, -c n : Run n copies concurrently. |
| 32 | * -e : Turn on errno logging. |
| 33 | * -i n : Execute test n times. |
| 34 | * -I x : Execute test for x seconds. |
| 35 | * -P x : Pause for x seconds between iterations. |
| 36 | * -t : Turn on syscall timing. |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 37 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 38 | * History |
| 39 | * 07/2001 John George |
| 40 | * -Ported |
robbiew | 4644c7e | 2002-04-26 14:33:32 +0000 | [diff] [blame] | 41 | * 04/2002 wjhuie sigset cleanups |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | * |
| 43 | * Restrictions |
| 44 | * NONE |
| 45 | */ |
| 46 | |
| 47 | #include <sys/types.h> |
| 48 | #include <signal.h> |
| 49 | #include <sys/uio.h> |
| 50 | #include <fcntl.h> |
| 51 | #include <memory.h> |
| 52 | #include <errno.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 53 | #include "test.h" |
robbiew | 37d19bd | 2003-11-14 16:57:49 +0000 | [diff] [blame] | 54 | #include <sys/mman.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
| 56 | #define K_1 8192 |
| 57 | |
| 58 | #define NBUFS 2 |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 59 | #define CHUNK K_1 /* single chunk */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | #define MAX_IOVEC 2 |
| 61 | #define DATA_FILE "writev_data_file" |
| 62 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 63 | char buf1[K_1]; |
| 64 | char buf2[K_1]; |
| 65 | char buf3[K_1]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 67 | char *bad_addr = 0; |
robbiew | 37d19bd | 2003-11-14 16:57:49 +0000 | [diff] [blame] | 68 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | struct iovec wr_iovec[MAX_IOVEC] = { |
| 70 | {(caddr_t) - 1, CHUNK}, |
Cyril Hrubis | cf0d626 | 2014-09-23 14:03:31 +0200 | [diff] [blame] | 71 | {NULL, 0} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 74 | char name[K_1], f_name[K_1]; |
| 75 | int fd[2], in_sighandler; |
| 76 | char *buf_list[NBUFS]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
| 78 | char *TCID = "writev05"; |
| 79 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
| 81 | void sighandler(int); |
| 82 | long l_seek(int, long, int); |
| 83 | void setup(void); |
| 84 | void cleanup(void); |
| 85 | int fail; |
| 86 | |
vapier | 6925ca3 | 2006-02-27 04:25:25 +0000 | [diff] [blame] | 87 | #if !defined(UCLINUX) |
| 88 | |
robbiew | 7d5c517 | 2003-03-27 22:54:57 +0000 | [diff] [blame] | 89 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 91 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 92 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | |
| 94 | int nbytes; |
| 95 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 96 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 98 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 100 | setup(); /* set "tstdir", and "testfile" vars */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
| 102 | /* The following loop checks looping state if -i option given */ |
| 103 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 104 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 105 | /* reset tst_count in case we are looping */ |
| 106 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | |
| 108 | buf_list[0] = buf1; |
| 109 | buf_list[1] = buf2; |
| 110 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 111 | fd[1] = -1; /* Invalid file descriptor */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | |
robbiew | 4644c7e | 2002-04-26 14:33:32 +0000 | [diff] [blame] | 113 | if (signal(SIGTERM, sighandler) == SIG_ERR) { |
| 114 | perror("signal"); |
| 115 | tst_resm(TFAIL, "signal() SIGTERM FAILED"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 117 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | |
robbiew | 4644c7e | 2002-04-26 14:33:32 +0000 | [diff] [blame] | 119 | if (signal(SIGPIPE, sighandler) == SIG_ERR) { |
| 120 | perror("signal"); |
| 121 | tst_resm(TFAIL, "signal() SIGPIPE FAILED"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 123 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | |
| 125 | /* Fill the buf_list[0] and buf_list[1] with 0 zeros */ |
| 126 | memset(buf_list[0], 0, K_1); |
| 127 | memset(buf_list[1], 0, K_1); |
| 128 | |
| 129 | if ((fd[0] = open(f_name, O_WRONLY | O_CREAT, 0666)) < 0) { |
| 130 | tst_resm(TFAIL, "open(2) failed: fname = %s, " |
| 131 | "errno = %d", f_name, errno); |
| 132 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 133 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 134 | if ((nbytes = write(fd[0], buf_list[1], K_1)) != K_1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | tst_resm(TFAIL, "write(2) failed: nbytes " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 136 | "= %d, errno = %d", nbytes, errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 138 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | if (close(fd[0]) < 0) { |
| 142 | tst_resm(TFAIL, "close failed: errno = %d", errno); |
| 143 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 144 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | |
| 146 | if ((fd[0] = open(f_name, O_RDWR, 0666)) < 0) { |
| 147 | tst_resm(TFAIL, "open failed: fname = %s, errno = %d", |
| 148 | f_name, errno); |
| 149 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 150 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * In this block we are trying to call writev() with invalid |
| 154 | * vector to be written in a sparse file. This will return |
| 155 | * EFAULT. At the same time, check should be made whether |
| 156 | * the scheduled write() with valid data is done correctly |
| 157 | * or not. |
| 158 | */ |
robbiew | 7d5c517 | 2003-03-27 22:54:57 +0000 | [diff] [blame] | 159 | //block1: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | tst_resm(TINFO, "Enter block 1"); |
| 161 | fail = 0; |
| 162 | |
| 163 | l_seek(fd[0], 0, 0); |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 164 | TEST(writev(fd[0], wr_iovec, 2)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | if (TEST_RETURN < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | if (TEST_ERRNO == EFAULT) { |
| 167 | tst_resm(TINFO, "Received EFAULT as expected"); |
| 168 | } else { |
| 169 | tst_resm(TFAIL, "Expected EFAULT, got %d", |
| 170 | TEST_ERRNO); |
| 171 | fail = 1; |
| 172 | } |
| 173 | l_seek(fd[0], K_1, 0); |
| 174 | if ((nbytes = read(fd[0], buf_list[0], CHUNK)) != 0) { |
| 175 | tst_resm(TFAIL, "Expected nbytes = 0, got " |
| 176 | "%d", nbytes); |
| 177 | fail = 1; |
| 178 | } |
| 179 | } else { |
| 180 | tst_resm(TFAIL, "Error writev returned a positive " |
| 181 | "value"); |
| 182 | fail = 1; |
| 183 | } |
| 184 | if (fail) { |
| 185 | tst_resm(TINFO, "block 1 FAILED"); |
| 186 | } else { |
| 187 | tst_resm(TINFO, "block 1 PASSED"); |
| 188 | } |
| 189 | tst_resm(TINFO, "Exit block 1"); |
| 190 | } |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 191 | close(fd[0]); |
| 192 | close(fd[1]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 193 | cleanup(); |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 194 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 195 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | } |
| 197 | |
vapier | 6925ca3 | 2006-02-27 04:25:25 +0000 | [diff] [blame] | 198 | #else |
| 199 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 200 | int main(void) |
vapier | 6925ca3 | 2006-02-27 04:25:25 +0000 | [diff] [blame] | 201 | { |
| 202 | tst_resm(TINFO, "test is not available on uClinux"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 203 | tst_exit(); |
vapier | 6925ca3 | 2006-02-27 04:25:25 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | #endif /* if !defined(UCLINUX) */ |
| 207 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | /* |
| 209 | * setup() |
| 210 | * performs all ONE TIME setup for this test |
| 211 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 212 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 213 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 214 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 215 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 216 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 217 | /* Pause if that option was specified. |
| 218 | * TEST_PAUSE contains the code to fork the test with the -i option. |
| 219 | * You want to make sure you do this before you create your temporary |
| 220 | * directory. |
| 221 | */ |
| 222 | TEST_PAUSE; |
| 223 | |
| 224 | /* Create a unique temporary directory and chdir() to it. */ |
| 225 | tst_tmpdir(); |
| 226 | |
| 227 | strcpy(name, DATA_FILE); |
| 228 | sprintf(f_name, "%s.%d", name, getpid()); |
| 229 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 230 | bad_addr = mmap(0, 1, PROT_NONE, |
| 231 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
| 232 | if (bad_addr == MAP_FAILED) { |
| 233 | printf("mmap failed\n"); |
| 234 | } |
| 235 | wr_iovec[0].iov_base = bad_addr; |
robbiew | 37d19bd | 2003-11-14 16:57:49 +0000 | [diff] [blame] | 236 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* |
| 240 | * cleanup() |
| 241 | * performs all ONE TIME cleanup for this test at |
| 242 | * completion or premature exit |
| 243 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 244 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 245 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 246 | |
| 247 | if (unlink(f_name) < 0) { |
| 248 | tst_resm(TFAIL, "unlink Failed--file = %s, errno = %d", |
| 249 | f_name, errno); |
| 250 | } |
| 251 | tst_rmdir(); |
| 252 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* |
| 256 | * sighandler() |
| 257 | * Signal handler for SIGTERM and SIGPIPE |
| 258 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 259 | void sighandler(int sig) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 260 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 261 | switch (sig) { |
| 262 | case SIGTERM: |
| 263 | break; |
| 264 | case SIGPIPE: |
| 265 | ++in_sighandler; |
| 266 | return; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 267 | default: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 268 | tst_resm(TFAIL, "sighandler() received invalid signal " |
| 269 | ": %d", sig); |
| 270 | break; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | if ((unlink(f_name) < 0) && (errno != ENOENT)) { |
| 274 | tst_resm(TFAIL, "unlink Failed--file = %s, errno = %d", |
| 275 | f_name, errno); |
| 276 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 277 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 278 | exit(sig); |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * l_seek() |
| 283 | * Wrap around for regular lseek() to give error message on failure |
| 284 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 285 | long l_seek(int fdesc, long offset, int whence) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 286 | { |
| 287 | if (lseek(fdesc, offset, whence) < 0) { |
| 288 | tst_resm(TFAIL, "lseek Failed : errno = %d", errno); |
| 289 | fail = 1; |
| 290 | } |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame] | 291 | return 0; |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 292 | } |