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 | * writev03.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * The testcases are written calling writev() with partially valid data |
| 26 | * to overwrite the contents, to write in the beginning and to write in |
| 27 | * the end of the file. |
| 28 | * |
| 29 | * USAGE: <for command-line> |
| 30 | * writev03 [-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. |
| 37 | * |
| 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> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | #include <sys/uio.h> |
robbiew | 169e09b | 2005-08-30 17:24:45 +0000 | [diff] [blame] | 49 | #include <sys/mman.h> |
| 50 | #include <unistd.h> |
| 51 | #include <signal.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | #include <fcntl.h> |
| 53 | #include <memory.h> |
| 54 | #include <errno.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 55 | #include "test.h" |
| 56 | #include "usctest.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
| 58 | #define K_1 1024 |
| 59 | |
| 60 | #define NBUFS 4 |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 61 | #define CHUNK 64 /* single chunk */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | #define MAX_IOVEC 4 |
| 63 | #define DATA_FILE "writev_data_file" |
| 64 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 65 | char buf1[K_1], buf2[K_1], buf3[K_1]; |
| 66 | char *bad_addr = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
| 68 | struct iovec wr_iovec[MAX_IOVEC] = { |
| 69 | /* testcase #1 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 70 | {buf1 + (CHUNK * 6), CHUNK}, |
| 71 | {(caddr_t) - 1, CHUNK}, |
| 72 | {buf1 + (CHUNK * 8), CHUNK}, |
| 73 | {(caddr_t) NULL, 0} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /* 0 terminated list of expected errnos */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 77 | int exp_enos[] = { 0 }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | char name[K_1], f_name[K_1]; |
| 80 | int fd[2], in_sighandler; |
| 81 | char *buf_list[NBUFS]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
| 83 | char *TCID = "writev03"; |
| 84 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | |
| 86 | void sighandler(int); |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 87 | void l_seek(int, off_t, int); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | void setup(void); |
| 89 | void cleanup(void); |
vapier | 6925ca3 | 2006-02-27 04:25:25 +0000 | [diff] [blame] | 90 | |
robbiew | 7d5c517 | 2003-03-27 22:54:57 +0000 | [diff] [blame] | 91 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | int lc; /* loop counter */ |
| 94 | char *msg; /* message returned from parse_opts */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
| 96 | int nbytes; |
| 97 | |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 98 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 100 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | setup(); |
| 102 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 104 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | Tst_count = 0; |
| 106 | |
| 107 | buf_list[0] = buf1; |
| 108 | buf_list[1] = buf2; |
| 109 | buf_list[2] = buf3; |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 110 | buf_list[3] = NULL; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | fd[1] = -1; /* Invalid file descriptor */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 114 | if (signal(SIGTERM, sighandler) == SIG_ERR) |
| 115 | tst_brkm(TBROK|TERRNO, cleanup, |
| 116 | "signal(SIGTERM, ..) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 118 | if (signal(SIGPIPE, sighandler) == SIG_ERR) |
| 119 | tst_brkm(TBROK|TERRNO, cleanup, |
| 120 | "signal(SIGPIPE, ..) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | |
| 122 | memset(buf_list[0], 0, K_1); |
| 123 | memset(buf_list[1], 0, K_1); |
| 124 | |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 125 | if ((fd[0] = open(f_name, O_WRONLY|O_CREAT, 0666)) == -1) |
| 126 | tst_brkm(TBROK|TERRNO, cleanup, |
| 127 | "open(.., O_WRONLY|O_CREAT, ..) failed"); |
| 128 | else |
| 129 | if ((nbytes = write(fd[0], buf_list[1], K_1)) != K_1) |
| 130 | tst_brkm(TFAIL|TERRNO, cleanup, "write failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 132 | if (close(fd[0]) < 0) |
| 133 | tst_brkm(TBROK|TERRNO, cleanup, "close failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 135 | if ((fd[0] = open(f_name, O_RDWR, 0666)) == -1) |
| 136 | tst_brkm(TBROK|TERRNO, cleanup, |
| 137 | "open(.., O_RDWR, ..) failed"); |
robbiew | 7d5c517 | 2003-03-27 22:54:57 +0000 | [diff] [blame] | 138 | //block1: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | tst_resm(TINFO, "Enter block 1"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | /* |
| 142 | * In this block we are trying to call writev() with |
| 143 | * partially valid data. This should return the valid number |
| 144 | * of bytes written in the vector. If it returns EFAULT, it |
| 145 | * is an error. And after returning the number of valid |
| 146 | * bytes written, the check should be made to verify the |
| 147 | * contents of the first valid write() scheduled. |
| 148 | */ |
| 149 | |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 150 | if (writev(fd[0], wr_iovec, 3) == -1) { |
| 151 | if (errno == EFAULT) |
| 152 | tst_resm(TFAIL, "Got EFAULT"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | } else { |
| 154 | l_seek(fd[0], 0, 0); |
| 155 | read(fd[0], buf_list[0], CHUNK); |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 156 | if (memcmp(buf_list[0], buf_list[1], CHUNK) != 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | tst_resm(TFAIL, "writev overwrote the file"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | } |
| 159 | tst_resm(TINFO, "Exit block 1"); |
| 160 | |
robbiew | 7d5c517 | 2003-03-27 22:54:57 +0000 | [diff] [blame] | 161 | //block2: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | tst_resm(TINFO, "Enter block 2"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | |
| 164 | /* |
| 165 | * In this block we are trying to over write the contents by |
| 166 | * calling writev() with partially valid data. It should |
| 167 | * return the valid number of bytes written but not EFAULT. |
| 168 | * Also the check should be made whether the initial write() |
| 169 | * scheduled is done correctly or not. |
| 170 | */ |
| 171 | l_seek(fd[0], 0, 0); |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 172 | if (writev(fd[0], wr_iovec, 3) == -1) { |
| 173 | if (errno == EFAULT) |
| 174 | tst_resm(TFAIL, "Got EFAULT"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | } else { |
| 176 | l_seek(fd[0], 0, 0); |
| 177 | read(fd[0], buf_list[0], CHUNK); |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 178 | if (memcmp(buf_list[0], buf_list[1], CHUNK) != 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | tst_resm(TFAIL, "writev overwrote the file"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | } |
| 181 | tst_resm(TINFO, "Exit block 2"); |
| 182 | |
robbiew | 7d5c517 | 2003-03-27 22:54:57 +0000 | [diff] [blame] | 183 | //block3: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | tst_resm(TINFO, "Enter block 3"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | |
| 186 | /* |
| 187 | * In this block, we are trying to call writev() by going to |
| 188 | * some end position of the file. Here writev() is called |
| 189 | * with partially valid data, and this will return the |
| 190 | * number of valid bytes written and not EFAULT. Also, the |
| 191 | * check should be made whether the inital write() that is |
| 192 | * scheduled with valid data is done correctly done or not. |
| 193 | */ |
| 194 | |
| 195 | l_seek(fd[0], 8192, 0); |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 196 | if (writev(fd[0], wr_iovec, 3) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 197 | TEST_ERROR_LOG(errno); |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 198 | if (errno == EFAULT) |
| 199 | tst_resm(TFAIL, "Got EFAULT"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | } else { |
| 201 | l_seek(fd[0], 0, 0); |
| 202 | read(fd[0], buf_list[0], CHUNK); |
| 203 | if (memcmp(buf_list[0], buf_list[1], CHUNK) != 0) { |
| 204 | tst_resm(TFAIL, "writev overwrote the file"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | tst_resm(TINFO, "Exit block 3"); |
| 209 | } |
| 210 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 211 | tst_exit(); |
vapier | 6925ca3 | 2006-02-27 04:25:25 +0000 | [diff] [blame] | 212 | } |
| 213 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | /* |
| 215 | * setup() |
| 216 | * performs all ONE TIME setup for this test |
| 217 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 218 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 219 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 220 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 221 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 222 | |
| 223 | /* Set up the expected error numbers for -e option */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 224 | TEST_EXP_ENOS(exp_enos); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 225 | |
| 226 | /* Pause if that option was specified. |
| 227 | * TEST_PAUSE contains the code to fork the test with the -i option. |
| 228 | * You want to make sure you do this before you create your temporary |
| 229 | * directory. |
| 230 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 231 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 232 | |
| 233 | strcpy(name, DATA_FILE); |
| 234 | sprintf(f_name, "%s.%d", name, getpid()); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 235 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 236 | bad_addr = mmap(0, 1, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); |
vapier | 0722a2b | 2006-05-12 15:44:11 +0000 | [diff] [blame] | 237 | if (bad_addr == MAP_FAILED) { |
robbiew | 169e09b | 2005-08-30 17:24:45 +0000 | [diff] [blame] | 238 | tst_brkm(TBROK, cleanup, "mmap failed"); |
| 239 | } |
| 240 | wr_iovec[1].iov_base = bad_addr; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 241 | tst_tmpdir(); |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * cleanup() |
| 246 | * performs all ONE TIME cleanup for this test at |
| 247 | * completion or premature exit |
| 248 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 249 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 250 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 251 | TEST_CLEANUP; |
| 252 | |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 253 | close(fd[0]); |
| 254 | close(fd[1]); |
| 255 | |
| 256 | if (unlink(f_name) == -1) |
| 257 | tst_resm(TFAIL, "unlink failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 258 | tst_rmdir(); |
| 259 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 260 | } |
| 261 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 262 | void sighandler(int sig) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 263 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 264 | switch (sig) { |
| 265 | case SIGTERM: |
| 266 | break; |
| 267 | case SIGPIPE: |
| 268 | ++in_sighandler; |
| 269 | return; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 270 | default: |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 271 | tst_resm(TBROK, "sighandler received invalid signal : %d", sig); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 272 | break; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 273 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* |
| 277 | * l_seek() |
| 278 | * Wrap around for regular lseek function for giving error message |
| 279 | */ |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 280 | void l_seek(int fdesc, off_t offset, int whence) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 281 | { |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 282 | if (lseek(fdesc, offset, whence) == -1) |
| 283 | tst_brkm(TBROK|TERRNO, cleanup, "lseek failed"); |
| 284 | } |