| 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 | * writev01.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to check the basic functionality of writev(2) system call. |
| 26 | * |
| 27 | * ALGORITHM |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 28 | * Create a IO vector, and attempt to writev various components of it. |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | * |
| 30 | * USAGE: <for command-line> |
| 31 | * writev01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 32 | * where, -c n : Run n copies concurrently. |
| 33 | * -e : Turn on errno logging. |
| 34 | * -i n : Execute test n times. |
| 35 | * -I x : Execute test for x seconds. |
| 36 | * -P x : Pause for x seconds between iterations. |
| 37 | * -t : Turn on syscall timing. |
| 38 | * |
| 39 | * History |
| 40 | * 07/2001 John George |
| 41 | * -Ported |
| robbiew | 4644c7e | 2002-04-26 14:33:32 +0000 | [diff] [blame] | 42 | * 04/2002 wjhuie sigset cleanups |
| robbiew | 5128100 | 2002-06-20 17:02:19 +0000 | [diff] [blame] | 43 | * 06/2002 Shaobo Li |
| 44 | * fix testcase 7, add each testcase comment. |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | * |
| 46 | * Restrictions |
| 47 | * None |
| 48 | */ |
| 49 | |
| 50 | #include <stdio.h> |
| plars | 29bcde8 | 2002-10-07 17:21:48 +0000 | [diff] [blame] | 51 | #include <sys/types.h> |
| plars | 20bc5f2 | 2002-10-10 17:37:56 +0000 | [diff] [blame] | 52 | #include <signal.h> |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | #include <sys/uio.h> |
| 54 | #include <sys/fcntl.h> |
| 55 | #include <memory.h> |
| 56 | #include <errno.h> |
| Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 57 | #include "test.h" |
| 58 | #include "usctest.h" |
| robbiew | b73a6b7 | 2003-11-17 15:21:18 +0000 | [diff] [blame] | 59 | #include <sys/mman.h> |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
| 61 | #define K_1 1024 |
| 62 | #define M_1 K_1 * K_1 |
| 63 | #define G_1 M_1 * K_1 |
| 64 | |
| 65 | #define NBUFS 4 |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 66 | #define CHUNK 64 /* single chunk */ |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | #define MAX_IOVEC 16 |
| 68 | #define DATA_FILE "writev_data_file" |
| 69 | |
| 70 | char buf1[K_1], buf2[K_1], buf3[K_1]; |
| 71 | |
| 72 | struct iovec wr_iovec[MAX_IOVEC] = { |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 73 | /* iov_base *//* iov_len */ |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | |
| 75 | /* testcase# 1 */ |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 76 | {buf1, -1}, |
| 77 | {(buf1 + CHUNK), CHUNK}, |
| 78 | {(buf1 + CHUNK * 2), CHUNK}, |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
| 80 | /* testcase# 2 */ |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | {(buf1 + CHUNK * 3), G_1}, |
| 82 | {(buf1 + CHUNK * 4), G_1}, |
| 83 | {(buf1 + CHUNK * 5), G_1}, |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
| 85 | /* testcase# 3 */ |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | {(buf1 + CHUNK * 6), CHUNK}, |
| 87 | {(caddr_t) - 1, CHUNK}, |
| 88 | {(buf1 + CHUNK * 8), CHUNK}, |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
| 90 | /* testcase# 4 */ |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | {(buf1 + CHUNK * 9), CHUNK}, |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | |
| 93 | /* testcase# 5 */ |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | {(buf1 + CHUNK * 10), CHUNK}, |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
| 96 | /* testcase# 6 */ |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | {(buf1 + CHUNK * 11), CHUNK}, |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
| 99 | /* testcase# 7 */ |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 100 | {(buf1 + CHUNK * 12), CHUNK}, |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
| 102 | /* testcase# 8 */ |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | {(buf1 + CHUNK * 13), 0}, |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | |
| robbiew | 5128100 | 2002-06-20 17:02:19 +0000 | [diff] [blame] | 105 | /* testcase# 7 */ |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | {(caddr_t) NULL, 0}, |
| 107 | {(caddr_t) NULL, 0} |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | char name[K_1], f_name[K_1]; |
| 111 | |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | char *bad_addr = 0; |
| robbiew | b73a6b7 | 2003-11-17 15:21:18 +0000 | [diff] [blame] | 113 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | /* 0 terminated list of expected errnos */ |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 115 | int exp_enos[] = { 14, 22, 32, 77, 0 }; |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | |
| 117 | int fd[4], in_sighandler; |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 118 | int pfd[2]; /* pipe fd's */ |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | char *buf_list[NBUFS]; |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | |
| 121 | void sighandler(int); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | int fill_mem(char *, int, int); |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 123 | void init_buffs(char *[]); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | void setup(void); |
| 125 | void cleanup(void); |
| 126 | |
| 127 | char *TCID = "writev01"; |
| 128 | int TST_TOTAL = 1; |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
| robbiew | 7d5c517 | 2003-03-27 22:54:57 +0000 | [diff] [blame] | 130 | int main(int argc, char **argv) |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | { |
| 132 | int nbytes, ret; |
| 133 | |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 134 | int lc; /* loop counter */ |
| 135 | char *msg; /* message returned from parse_opts */ |
| 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 ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
| Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 138 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | setup(); |
| 141 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 143 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | Tst_count = 0; |
| 145 | |
| 146 | buf_list[0] = buf1; |
| 147 | buf_list[1] = buf2; |
| 148 | buf_list[2] = buf3; |
| Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 149 | buf_list[3] = NULL; |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 151 | fd[1] = -1; /* Invalid file descriptor */ |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 153 | if (signal(SIGTERM, sighandler) == SIG_ERR) |
| 154 | tst_brkm(TBROK|TERRNO, cleanup, |
| 155 | "signal(SIGTERM, ..) failed"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 157 | if (signal(SIGPIPE, sighandler) == SIG_ERR) |
| 158 | tst_brkm(TBROK|TERRNO, cleanup, |
| 159 | "signal(SIGPIPE, ..) failed"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | |
| 161 | init_buffs(buf_list); |
| 162 | |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 163 | if ((fd[0] = open(f_name, O_WRONLY | O_CREAT, 0666)) == -1) |
| 164 | tst_brkm(TBROK|TERRNO, cleanup, |
| 165 | "open(.., O_WRONLY|O_CREAT, ..) failed"); |
| 166 | else |
| 167 | if ((nbytes = write(fd[0], buf_list[2], K_1)) != K_1) |
| 168 | tst_brkm(TBROK|TERRNO, cleanup, "write failed"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 170 | if (close(fd[0]) == -1) |
| 171 | tst_brkm(TBROK|TERRNO, cleanup, "close failed"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 173 | if ((fd[0] = open(f_name, O_RDWR, 0666)) == -1) |
| 174 | tst_brkm(TBROK|TERRNO, cleanup, "open failed"); |
| 175 | //block1: /* given vector length -1, writev return EINVAL. */ |
| 176 | tst_resm(TPASS, "Enter Block 1"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | |
| 178 | TEST(writev(fd[0], wr_iovec, 1)); |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 179 | if (TEST_RETURN == -1) { |
| 180 | if (TEST_ERRNO == EINVAL) |
| 181 | tst_resm(TPASS, "Received EINVAL as expected"); |
| 182 | else |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | tst_resm(TFAIL, "Expected errno = EINVAL, " |
| 184 | "got %d", TEST_ERRNO); |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 185 | } else |
| 186 | tst_resm(TFAIL, "writev failed to fail"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | tst_resm(TINFO, "Exit block 1"); |
| 188 | |
| robbiew | 7d5c517 | 2003-03-27 22:54:57 +0000 | [diff] [blame] | 189 | //block2: |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 190 | /* This testcases doesn't look like what it intent to do |
| 191 | * 1. it is not using the wr_iovec initialized |
| 192 | * 2. read() and following message is not consistent |
| 193 | */ |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 194 | tst_resm(TPASS, "Enter block 2"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 196 | if (lseek(fd[0], CHUNK * 6, 0) == -1) |
| 197 | tst_resm(TBROK|TERRNO, "block2: 1st lseek failed"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | |
| 199 | if ((ret = writev(fd[0], (wr_iovec + 6), 3)) == CHUNK) { |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 200 | if (lseek(fd[0], CHUNK * 6, 0) == -1) |
| 201 | tst_brkm(TBROK|TERRNO, cleanup, |
| 202 | "block2: 2nd lseek failed"); |
| 203 | if ((nbytes = read(fd[0], buf_list[0], CHUNK)) != |
| 204 | CHUNK) |
| 205 | tst_resm(TFAIL, "read failed; expected nbytes " |
| 206 | "= 1024, got = %d", nbytes); |
| 207 | else if (memcmp((buf_list[0] + CHUNK * 6), |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | (buf_list[2] + CHUNK * 6), |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 209 | CHUNK) != 0) |
| 210 | tst_resm(TFAIL, "writev over " |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 211 | "wrote %s", f_name); |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 212 | } else |
| 213 | tst_resm(TFAIL|TERRNO, "writev failed unexpectedly"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | tst_resm(TINFO, "Exit block 2"); |
| 215 | |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 216 | //block3: /* given 1 bad vector buffer with good ones, writev success */ |
| 217 | tst_resm(TPASS, "Enter block 3"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 218 | |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 219 | if (lseek(fd[0], CHUNK * 6, 0) == -1) |
| 220 | tst_brkm(TBROK|TERRNO, cleanup, |
| 221 | "block3: 1st lseek failed"); |
| 222 | if ((nbytes = writev(fd[0], (wr_iovec + 6), 3)) == -1) { |
| 223 | if (errno == EFAULT) |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 224 | tst_resm(TFAIL, "Got EFAULT"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 225 | } |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 226 | if (lseek(fd[0], 0, 0) == -1) |
| 227 | tst_brkm(TBROK, cleanup, "block3: 2nd lseek failed"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 228 | if ((nbytes = read(fd[0], buf_list[0], K_1)) != K_1) { |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 229 | tst_resm(TFAIL|TERRNO, |
| 230 | "read failed; expected nbytes = 1024, got = %d", |
| 231 | nbytes); |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 232 | } else if (memcmp((buf_list[0] + CHUNK * 6), |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 233 | (buf_list[2] + CHUNK * 6), CHUNK * 3) != 0) |
| 234 | tst_resm(TFAIL, "writev overwrote file"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 235 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 236 | tst_resm(TINFO, "Exit block 3"); |
| 237 | |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 238 | //block4: /* given bad file discriptor, writev return EBADF. */ |
| 239 | tst_resm(TPASS, "Enter block 4"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 240 | |
| 241 | TEST(writev(fd[1], (wr_iovec + 9), 1)); |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 242 | if (TEST_RETURN == -1) { |
| 243 | if (TEST_ERRNO == EBADF) |
| 244 | tst_resm(TPASS, "Received EBADF as expected"); |
| 245 | else |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 246 | tst_resm(TFAIL, "expected errno = EBADF, " |
| 247 | "got %d", TEST_ERRNO); |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 248 | } else |
| 249 | tst_resm(TFAIL, "writev returned a " |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 250 | "positive value"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 251 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 252 | tst_resm(TINFO, "Exit block 4"); |
| 253 | |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 254 | //block5: /* given invalid vector count, writev return EINVAL */ |
| 255 | tst_resm(TPASS, "Enter block 5"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 256 | |
| 257 | TEST(writev(fd[0], (wr_iovec + 10), -1)); |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 258 | if (TEST_RETURN == -1) { |
| 259 | if (TEST_ERRNO == EINVAL) |
| 260 | tst_resm(TPASS, "Received EINVAL as expected"); |
| 261 | else |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 262 | tst_resm(TFAIL, "expected errno = EINVAL, " |
| 263 | "got %d", TEST_ERRNO); |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 264 | } else |
| 265 | tst_resm(TFAIL, "writev returned a " |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 266 | "positive value"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 267 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 268 | tst_resm(TINFO, "Exit block 5"); |
| 269 | |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 270 | //block6: /* given no buffer vector, writev success */ |
| 271 | tst_resm(TPASS, "Enter block 6"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 272 | |
| 273 | TEST(writev(fd[0], (wr_iovec + 11), 0)); |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 274 | if (TEST_RETURN == -1) |
| 275 | tst_resm(TFAIL|TTERRNO, "writev failed"); |
| 276 | else |
| 277 | tst_resm(TPASS, "writev wrote 0 iovectors"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 278 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 279 | tst_resm(TINFO, "Exit block 6"); |
| 280 | |
| robbiew | 7d5c517 | 2003-03-27 22:54:57 +0000 | [diff] [blame] | 281 | //block7: |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 282 | /* given 4 vectors, 2 are NULL, 1 with 0 length and 1 with fixed length, |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 283 | * writev success writing fixed length. |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 284 | */ |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 285 | tst_resm(TPASS, "Enter block 7"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 286 | |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 287 | if (lseek(fd[0], CHUNK * 12, 0) == -1) |
| 288 | tst_resm(TBROK, "lseek failed"); |
| 289 | else if ((ret = writev(fd[0], (wr_iovec + 12), 4)) != CHUNK) |
| 290 | tst_resm(TFAIL, "writev failed writing %d bytes, " |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 291 | "followed by two NULL vectors", CHUNK); |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 292 | else |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 293 | tst_resm(TPASS, "writev passed writing %d bytes, " |
| 294 | "followed by two NULL vectors", CHUNK); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 295 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 296 | tst_resm(TINFO, "Exit block 7"); |
| 297 | |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 298 | //block8: /* try to write to a closed pipe, writev return EPIPE. */ |
| 299 | tst_resm(TPASS, "Enter block 8"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 300 | |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 301 | if (pipe(pfd) == -1) |
| 302 | tst_resm(TFAIL|TERRNO, "pipe failed"); |
| 303 | else { |
| 304 | if (close(pfd[0]) == -1) |
| 305 | tst_resm(TFAIL|TERRNO, "close failed"); |
| 306 | else if (writev(pfd[1], (wr_iovec + 12), 1) == -1 && |
| 307 | in_sighandler) { |
| 308 | if (errno == EPIPE) |
| 309 | tst_resm(TPASS, "Received EPIPE as " |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 310 | "expected"); |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 311 | else |
| 312 | tst_resm(TFAIL|TERRNO, |
| 313 | "didn't get EPIPE"); |
| 314 | } else |
| 315 | tst_resm(TFAIL, "writev returned a positive " |
| 316 | "value"); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 317 | } |
| 318 | tst_resm(TINFO, "Exit block 8"); |
| 319 | } |
| 320 | cleanup(); |
| Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 321 | tst_exit(); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 324 | void setup(void) |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 325 | { |
| Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 326 | |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 327 | tst_sig(FORK, sighandler, cleanup); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 328 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 329 | TEST_EXP_ENOS(exp_enos); |
| 330 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 331 | TEST_PAUSE; |
| 332 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 333 | tst_tmpdir(); |
| 334 | |
| 335 | strcpy(name, DATA_FILE); |
| 336 | sprintf(f_name, "%s.%d", name, getpid()); |
| robbiew | b73a6b7 | 2003-11-17 15:21:18 +0000 | [diff] [blame] | 337 | |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 338 | bad_addr = mmap(0, 1, PROT_NONE, |
| 339 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 340 | if (bad_addr == MAP_FAILED) |
| 341 | tst_brkm(TBROK|TERRNO, cleanup, "mmap failed"); |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 342 | wr_iovec[7].iov_base = bad_addr; |
| robbiew | b73a6b7 | 2003-11-17 15:21:18 +0000 | [diff] [blame] | 343 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 346 | void cleanup(void) |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 347 | { |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 348 | TEST_CLEANUP; |
| 349 | |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 350 | if (munmap(bad_addr, 1) == -1) |
| 351 | tst_resm(TBROK|TERRNO, "munmap failed"); |
| 352 | |
| 353 | close(fd[0]); |
| 354 | close(fd[1]); |
| 355 | |
| 356 | if (unlink(f_name) == -1) |
| 357 | tst_resm(TBROK|TERRNO, "unlink failed"); |
| 358 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 359 | tst_rmdir(); |
| 360 | |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 363 | void init_buffs(char *pbufs[]) |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 364 | { |
| 365 | int i; |
| 366 | |
| Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 367 | for (i = 0; pbufs[i] != NULL; i++) { |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 368 | switch (i) { |
| 369 | case 0: |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 370 | |
| 371 | case 1: |
| 372 | fill_mem(pbufs[i], 0, 1); |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 373 | break; |
| 374 | |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 375 | case 2: |
| 376 | fill_mem(pbufs[i], 1, 0); |
| 377 | break; |
| 378 | |
| 379 | default: |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 380 | tst_brkm(TBROK, cleanup, "error detected: init_buffs"); |
| 381 | } |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 382 | } |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 385 | int fill_mem(char *c_ptr, int c1, int c2) |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 386 | { |
| 387 | int count; |
| 388 | |
| 389 | for (count = 1; count <= K_1 / CHUNK; count++) { |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 390 | if (count & 0x01) { /* if odd */ |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 391 | memset(c_ptr, c1, CHUNK); |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 392 | } else { /* if even */ |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 393 | memset(c_ptr, c2, CHUNK); |
| 394 | } |
| 395 | } |
| subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame] | 396 | return 0; |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 399 | void sighandler(int sig) |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 400 | { |
| 401 | switch (sig) { |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 402 | case SIGTERM: |
| 403 | break; |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 404 | |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 405 | case SIGPIPE: |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 406 | in_sighandler++; |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 407 | return; |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 408 | |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 409 | default: |
| Garrett Cooper | 2aca6fc | 2010-12-19 03:12:40 -0800 | [diff] [blame] | 410 | tst_resm(TFAIL, "sighandler received invalid signal:%d", sig); |
| subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 411 | break; |
| plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 412 | } |
| Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 413 | } |