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 | * writev02.c |
| 23 | * |
| 24 | * DESCRIPTION |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 25 | * In these testcases, writev() is called with partially valid data |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | * to be written in a sparse file. |
| 27 | * |
| 28 | * ALGORITHM |
| 29 | * The file is created and write() is called with valid buffer to write |
| 30 | * at some 8k th offset. After that writev() will be called with invalid |
| 31 | * vector. This should return EFAULT. And at the same time, try looking at |
| 32 | * the 8kth offset whether the file is intact or not. |
| 33 | * |
| 34 | * USAGE: <for command-line> |
| 35 | * writev02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 36 | * where, -c n : Run n copies concurrently. |
| 37 | * -e : Turn on errno logging. |
| 38 | * -i n : Execute test n times. |
| 39 | * -I x : Execute test for x seconds. |
| 40 | * -P x : Pause for x seconds between iterations. |
| 41 | * -t : Turn on syscall timing. |
| 42 | * |
| 43 | * History |
| 44 | * 07/2001 John George |
| 45 | * -Ported |
robbiew | 4644c7e | 2002-04-26 14:33:32 +0000 | [diff] [blame] | 46 | * 04/2002 wjhuie sigset cleanups |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | * |
| 48 | * Restrictions |
| 49 | * None |
| 50 | */ |
| 51 | |
| 52 | #include <sys/types.h> |
| 53 | #include <signal.h> |
| 54 | #include <sys/uio.h> |
| 55 | #include <fcntl.h> |
| 56 | #include <memory.h> |
| 57 | #include <errno.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 58 | #include "test.h" |
robbiew | a21a8b3 | 2003-11-14 16:52:38 +0000 | [diff] [blame] | 59 | #include <sys/mman.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
| 61 | #define K_1 8192 |
| 62 | |
| 63 | #define NBUFS 2 |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 64 | #define CHUNK K_1 /* single chunk */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | #define MAX_IOVEC 2 |
| 66 | #define DATA_FILE "writev_data_file" |
| 67 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 68 | char buf1[K_1]; |
| 69 | char buf2[K_1]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 71 | char *bad_addr = 0; |
robbiew | a21a8b3 | 2003-11-14 16:52:38 +0000 | [diff] [blame] | 72 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 73 | struct iovec wr_iovec[MAX_IOVEC] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 74 | {(caddr_t) - 1, CHUNK}, |
Cyril Hrubis | cf0d626 | 2014-09-23 14:03:31 +0200 | [diff] [blame] | 75 | {NULL, 0}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 78 | char name[K_1], f_name[K_1]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 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 = "writev02"; |
| 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); |
| 90 | int fail; |
| 91 | |
robbiew | 7d5c517 | 2003-03-27 22:54:57 +0000 | [diff] [blame] | 92 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 94 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 95 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | |
| 97 | int nbytes; |
| 98 | |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 99 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 101 | |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 102 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 105 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 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 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | memset(buf_list[0], 0, K_1); |
| 112 | memset(buf_list[1], 0, K_1); |
| 113 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | fd[1] = -1; /* Invalid file descriptor */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 116 | if (signal(SIGTERM, sighandler) == SIG_ERR) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 117 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 118 | "signal(SIGTERM, ..)"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 120 | if (signal(SIGPIPE, sighandler) == SIG_ERR) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 122 | "signal(SIGPIPE, ..)"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 124 | if ((fd[0] = open(f_name, O_WRONLY | O_CREAT, 0666)) < 0) |
| 125 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 126 | "open(.., O_WRONLY|O_CREAT, ..) failed"); |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 127 | else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | l_seek(fd[0], K_1, 0); |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 129 | if ((nbytes = write(fd[0], buf_list[1], K_1)) != K_1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 130 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 131 | "write failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 134 | if (close(fd[0]) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 135 | tst_brkm(TFAIL | TERRNO, cleanup, "close failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 137 | if ((fd[0] = open(f_name, O_RDWR, 0666)) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 138 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 139 | "open(.., O_RDWR, ..) failed"); |
robbiew | 7d5c517 | 2003-03-27 22:54:57 +0000 | [diff] [blame] | 140 | //block1: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | /* |
| 142 | * In this block we are trying to call writev() with invalid |
| 143 | * vector to be written in a sparse file. This will return |
| 144 | * EFAULT. At the same time, check should be made whether |
| 145 | * the scheduled write() with valid data at 8k th offset is |
| 146 | * done correctly or not. |
| 147 | */ |
| 148 | tst_resm(TINFO, "Enter block 1"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | |
| 150 | l_seek(fd[0], 0, 0); |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 151 | TEST(writev(fd[0], wr_iovec, 2)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | if (TEST_RETURN < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | if (TEST_ERRNO == EFAULT) { |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 154 | tst_resm(TPASS, "Received EFAULT as expected"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | } else if (TEST_ERRNO != EFAULT) { |
| 156 | tst_resm(TFAIL, "Expected EFAULT, got %d", |
| 157 | TEST_ERRNO); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | } |
| 159 | l_seek(fd[0], K_1, 0); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | if ((nbytes = read(fd[0], buf_list[0], CHUNK)) != CHUNK) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | tst_resm(TFAIL, "Expected nbytes = 64, got " |
| 162 | "%d", nbytes); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | } else { |
| 164 | if (memcmp(buf_list[0], buf_list[1], CHUNK) |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 165 | != 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | tst_resm(TFAIL, "Error: writev() " |
| 167 | "over wrote %s", f_name); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | } |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 169 | } else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | tst_resm(TFAIL, "Error writev returned a positive " |
| 171 | "value"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | tst_resm(TINFO, "Exit block 1"); |
| 173 | } |
| 174 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 175 | tst_exit(); |
vapier | 6925ca3 | 2006-02-27 04:25:25 +0000 | [diff] [blame] | 176 | } |
| 177 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 178 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | { |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 180 | tst_sig(FORK, sighandler, cleanup); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 181 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | TEST_PAUSE; |
| 183 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | tst_tmpdir(); |
| 185 | |
| 186 | strcpy(name, DATA_FILE); |
| 187 | sprintf(f_name, "%s.%d", name, getpid()); |
robbiew | a21a8b3 | 2003-11-14 16:52:38 +0000 | [diff] [blame] | 188 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 189 | bad_addr = mmap(0, 1, PROT_NONE, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 190 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 191 | if (bad_addr == MAP_FAILED) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 192 | tst_brkm(TBROK | TERRNO, cleanup, "mmap failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 193 | wr_iovec[0].iov_base = bad_addr; |
robbiew | a21a8b3 | 2003-11-14 16:52:38 +0000 | [diff] [blame] | 194 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | } |
| 196 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 197 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | { |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 199 | close(fd[0]); |
| 200 | close(fd[1]); |
| 201 | |
| 202 | if (munmap(bad_addr, 1) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 203 | tst_resm(TWARN | TERRNO, "unmap failed"); |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 204 | if (unlink(f_name) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 205 | tst_resm(TWARN | TERRNO, "unlink failed"); |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 206 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 207 | tst_rmdir(); |
| 208 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 209 | } |
| 210 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 211 | void sighandler(int sig) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 212 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 213 | switch (sig) { |
| 214 | case SIGTERM: |
| 215 | break; |
| 216 | case SIGPIPE: |
| 217 | ++in_sighandler; |
| 218 | return; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 219 | default: |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 220 | tst_resm(TBROK, "sighandler received invalid signal : %d", sig); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 221 | break; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 224 | if (unlink(f_name) == -1 && errno != ENOENT) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 225 | tst_resm(TFAIL | TERRNO, "unlink failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* |
| 229 | * l_seek() |
| 230 | * Wrap around for regular lseek function for giving error message |
| 231 | */ |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 232 | void l_seek(int fdesc, off_t offset, int whence) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 233 | { |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 234 | if (lseek(fdesc, offset, whence) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 235 | tst_resm(TBROK | TERRNO, "lseek failed"); |
Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 236 | } |