blob: 86fca8d3ef99575ca5da60470470feb69369c529 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
22 * writev04.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. This is same as writev03.c, but the length of
28 * buffer used here is 8192 bytes.
29 *
30 * USAGE: <for command-line>
31 * writev04 [-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
robbiew4644c7e2002-04-26 14:33:32 +000042 * 04/2002 wjhuie sigset cleanups
plars865695b2001-08-27 22:15:12 +000043 *
44 * Restrictions
45 * NONE
46 */
plars865695b2001-08-27 22:15:12 +000047#include <sys/types.h>
plars865695b2001-08-27 22:15:12 +000048#include <sys/uio.h>
robbiew37549972005-08-30 19:32:44 +000049#include <sys/mman.h>
50#include <unistd.h>
51#include <signal.h>
plars865695b2001-08-27 22:15:12 +000052#include <fcntl.h>
53#include <memory.h>
54#include <errno.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080055#include "test.h"
plars865695b2001-08-27 22:15:12 +000056
57#define K_1 8192
58
59#define NBUFS 4
subrata_modak56207ce2009-03-23 13:35:39 +000060#define CHUNK 64 /* single chunk */
plars865695b2001-08-27 22:15:12 +000061#define MAX_IOVEC 4
62#define DATA_FILE "writev_data_file"
63
subrata_modak56207ce2009-03-23 13:35:39 +000064char buf1[K_1], buf2[K_1], buf3[K_1];
65char *bad_addr = 0;
plars865695b2001-08-27 22:15:12 +000066
67struct iovec wr_iovec[MAX_IOVEC] = {
68 /* testcase #1 */
subrata_modak56207ce2009-03-23 13:35:39 +000069 {buf1 + (CHUNK * 6), CHUNK},
70 {(caddr_t) - 1, CHUNK},
71 {buf1 + (CHUNK * 8), CHUNK},
Cyril Hrubiscf0d6262014-09-23 14:03:31 +020072 {NULL, 0}
plars865695b2001-08-27 22:15:12 +000073};
74
subrata_modak56207ce2009-03-23 13:35:39 +000075char name[K_1], f_name[K_1];
76int fd[2], in_sighandler;
77char *buf_list[NBUFS];
plars865695b2001-08-27 22:15:12 +000078
79char *TCID = "writev04";
80int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000081
82void sighandler(int);
83long l_seek(int, long, int);
84void setup(void);
85void cleanup(void);
86int fail;
87
vapier6925ca32006-02-27 04:25:25 +000088#if !defined(UCLINUX)
89
robbiew7d5c5172003-03-27 22:54:57 +000090int main(int argc, char **argv)
plars865695b2001-08-27 22:15:12 +000091{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020092 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020093 const char *msg;
plars865695b2001-08-27 22:15:12 +000094
95 int nbytes;
96
Wanlong Gao354ebb42012-12-07 10:10:04 +080097 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
plars865695b2001-08-27 22:15:12 +000098 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080099
Wanlong Gao354ebb42012-12-07 10:10:04 +0800100 }
plars865695b2001-08-27 22:15:12 +0000101
subrata_modak56207ce2009-03-23 13:35:39 +0000102 setup(); /* set "tstdir", and "testfile" vars */
plars865695b2001-08-27 22:15:12 +0000103
104 /* The following loop checks looping state if -i option given */
105 for (lc = 0; TEST_LOOPING(lc); lc++) {
106
Caspar Zhangd59a6592013-03-07 14:59:12 +0800107 /* reset tst_count in case we are looping */
108 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000109
110 buf_list[0] = buf1;
111 buf_list[1] = buf2;
112 buf_list[2] = buf3;
Garrett Cooper45e285d2010-11-22 12:19:25 -0800113 buf_list[3] = NULL;
plars865695b2001-08-27 22:15:12 +0000114
subrata_modak56207ce2009-03-23 13:35:39 +0000115 fd[1] = -1; /* Invalid file descriptor */
plars865695b2001-08-27 22:15:12 +0000116
robbiew4c53b8f2003-04-04 16:46:57 +0000117 if (signal(SIGTERM, sighandler) == SIG_ERR) {
robbiew4644c7e2002-04-26 14:33:32 +0000118 perror("signal");
119 tst_resm(TFAIL, "signal() SIGTERM FAILED");
plars865695b2001-08-27 22:15:12 +0000120 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800121 }
plars865695b2001-08-27 22:15:12 +0000122
robbiew4c53b8f2003-04-04 16:46:57 +0000123 if (signal(SIGPIPE, sighandler) == SIG_ERR) {
robbiew4644c7e2002-04-26 14:33:32 +0000124 perror("signal");
125 tst_resm(TFAIL, "signal() SIGPIPE FAILED");
plars865695b2001-08-27 22:15:12 +0000126 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127 }
plars865695b2001-08-27 22:15:12 +0000128
129 memset(buf_list[0], 0, K_1);
130 memset(buf_list[1], 0, K_1);
131
132 if ((fd[0] = open(f_name, O_WRONLY | O_CREAT, 0666)) < 0) {
133 tst_resm(TFAIL, "open(2) failed: fname = %s, "
134 "errno = %d", f_name, errno);
135 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800136 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000137 if ((nbytes = write(fd[0], buf_list[1], K_1)) != K_1) {
plars865695b2001-08-27 22:15:12 +0000138 tst_resm(TFAIL, "write(2) failed: nbytes "
139 "= %d, errno = %d", nbytes, errno);
140 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 }
plars865695b2001-08-27 22:15:12 +0000142 }
143
144 if (close(fd[0]) < 0) {
145 tst_resm(TFAIL, "close failed: errno = %d", errno);
146 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800147 }
plars865695b2001-08-27 22:15:12 +0000148
149 if ((fd[0] = open(f_name, O_RDWR, 0666)) < 0) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100150 tst_brkm(TFAIL, cleanup, "open failed: fname = %s, errno = %d",
plars865695b2001-08-27 22:15:12 +0000151 f_name, errno);
plars865695b2001-08-27 22:15:12 +0000152 }
robbiew7d5c5172003-03-27 22:54:57 +0000153//block1:
plars865695b2001-08-27 22:15:12 +0000154 tst_resm(TINFO, "Enter block 1");
155 fail = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000156
plars865695b2001-08-27 22:15:12 +0000157 /*
158 * In this block we are trying to call writev() with
159 * partially valid data. This should return the valid number
160 * of bytes written in the vector. If it returns EFAULT, it
161 * is an error. And after returning the number of valid
162 * bytes written, the check should be made to verify the
163 * contents of the first valid write() scheduled.
164 */
165 if (writev(fd[0], wr_iovec, 3) < 0) {
plars865695b2001-08-27 22:15:12 +0000166 fail = 1;
167 if (errno == EFAULT) {
168 tst_resm(TFAIL, "Got error EFAULT");
169 } else {
170 tst_resm(TFAIL, "Received unexpected error: %d",
subrata_modak56207ce2009-03-23 13:35:39 +0000171 errno);
plars865695b2001-08-27 22:15:12 +0000172 }
173 } else {
174 l_seek(fd[0], 0, 0);
175 read(fd[0], buf_list[0], CHUNK);
176 if (memcmp(buf_list[0], buf_list[1], CHUNK) != 0) {
177 tst_resm(TFAIL, "writev overwrote the file");
178 fail = 1;
179 }
180 }
181
182 if (fail) {
183 tst_resm(TINFO, "block 1 FAILED");
184 } else {
185 tst_resm(TINFO, "block 1 PASSED");
186 }
187 tst_resm(TINFO, "Exit block 1");
188
robbiew7d5c5172003-03-27 22:54:57 +0000189//block2:
plars865695b2001-08-27 22:15:12 +0000190 tst_resm(TINFO, "Enter block 2");
191 fail = 0;
192
193 /*
194 * In this block we are trying to over write the contents by
195 * calling writev() with partially valid data. It should
196 * return the valid number of bytes written but not EFAULT.
197 * Also the check should be made whether the initial write()
198 * scheduled is done correctly or not.
199 */
200 l_seek(fd[0], 0, 0);
201 if (writev(fd[0], wr_iovec, 3) < 0) {
plars865695b2001-08-27 22:15:12 +0000202 fail = 1;
203 if (errno == EFAULT) {
204 tst_resm(TFAIL, "Got error EFAULT");
205 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000206 tst_resm(TFAIL, "Received unexpected error: %d",
207 errno);
plars865695b2001-08-27 22:15:12 +0000208 }
209 } else {
210 l_seek(fd[0], 0, 0);
211 read(fd[0], buf_list[0], CHUNK);
212 if (memcmp(buf_list[0], buf_list[1], CHUNK) != 0) {
213 tst_resm(TFAIL, "writev overwrote the file");
214 fail = 1;
215 }
216 }
217
218 if (fail) {
219 tst_resm(TINFO, "block 2 FAILED");
220 } else {
221 tst_resm(TINFO, "block 2 PASSED");
222 }
223 tst_resm(TINFO, "Exit block 2");
224
robbiew7d5c5172003-03-27 22:54:57 +0000225//block3:
plars865695b2001-08-27 22:15:12 +0000226 tst_resm(TINFO, "Enter block 3");
227 fail = 0;
228
229 /*
230 * In this block, we are trying to call writev() by going to
231 * some end position of the file. Here writev() is called
232 * with partially valid data, and this will return the
233 * number of valid bytes written and not EFAULT. Also, the
234 * check should be made whether the inital write() that is
235 * scheduled with valid data is done correctly.
236 */
237
238 l_seek(fd[0], 8192, 0);
239 if (writev(fd[0], wr_iovec, 3) < 0) {
plars865695b2001-08-27 22:15:12 +0000240 fail = 1;
241 if (errno == EFAULT) {
242 tst_resm(TFAIL, "Got error EFAULT");
243 } else {
244 tst_resm(TFAIL, "Received unexpected error: %d",
subrata_modak56207ce2009-03-23 13:35:39 +0000245 errno);
plars865695b2001-08-27 22:15:12 +0000246 }
247 } else {
248 l_seek(fd[0], 0, 0);
249 read(fd[0], buf_list[0], CHUNK);
250 if (memcmp(buf_list[0], buf_list[1], CHUNK) != 0) {
251 tst_resm(TFAIL, "writev overwrote the file");
252 fail = 1;
253 }
254 }
255
256 if (fail) {
257 tst_resm(TINFO, "block 3 FAILED");
258 } else {
259 tst_resm(TINFO, "block 3 PASSED");
260 }
261 tst_resm(TINFO, "Exit block 3");
262 }
subrata_modakdad6e1a2007-10-30 10:46:58 +0000263 close(fd[0]);
264 close(fd[1]);
plars865695b2001-08-27 22:15:12 +0000265 cleanup();
Garrett Cooper2aca6fc2010-12-19 03:12:40 -0800266 tst_exit();
plars865695b2001-08-27 22:15:12 +0000267}
268
vapier6925ca32006-02-27 04:25:25 +0000269#else
270
Mike Frysingerc57fba52014-04-09 18:56:30 -0400271int main(void)
vapier6925ca32006-02-27 04:25:25 +0000272{
273 tst_resm(TINFO, "test is not available on uClinux");
Garrett Cooper2c282152010-12-16 00:55:50 -0800274 tst_exit();
vapier6925ca32006-02-27 04:25:25 +0000275}
276
277#endif /* if !defined(UCLINUX) */
278
plars865695b2001-08-27 22:15:12 +0000279/*
280 * setup()
281 * performs all ONE TIME setup for this test
282 */
subrata_modak56207ce2009-03-23 13:35:39 +0000283void setup(void)
plars865695b2001-08-27 22:15:12 +0000284{
Garrett Cooper2c282152010-12-16 00:55:50 -0800285
plars865695b2001-08-27 22:15:12 +0000286 tst_sig(FORK, DEF_HANDLER, cleanup);
287
plars865695b2001-08-27 22:15:12 +0000288 TEST_PAUSE;
289
290 /* Create a unique temporary directory and chdir() to it. */
291 tst_tmpdir();
292
293 strcpy(name, DATA_FILE);
294 sprintf(f_name, "%s.%d", name, getpid());
subrata_modakbdbaec52009-02-26 12:14:51 +0000295
subrata_modak56207ce2009-03-23 13:35:39 +0000296 bad_addr = mmap(0, 1, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
297 if (bad_addr == MAP_FAILED) {
298 tst_brkm(TBROK, cleanup, "mmap failed");
299 }
300 wr_iovec[1].iov_base = bad_addr;
plars865695b2001-08-27 22:15:12 +0000301
302}
303
304/*
305 * cleanup()
306 * performs all ONE TIME cleanup for this test at
307 * completion or premature exit
308 */
subrata_modak56207ce2009-03-23 13:35:39 +0000309void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000310{
plars865695b2001-08-27 22:15:12 +0000311
312 if (unlink(f_name) < 0) {
313 tst_resm(TFAIL, "unlink Failed--file = %s, errno = %d",
314 f_name, errno);
315 }
316 tst_rmdir();
317
plars865695b2001-08-27 22:15:12 +0000318}
319
320/*
321 * sighandler()
322 * Signal handler function for SIGTERM and SIGPIPE
323 */
subrata_modak56207ce2009-03-23 13:35:39 +0000324void sighandler(int sig)
plars865695b2001-08-27 22:15:12 +0000325{
subrata_modak56207ce2009-03-23 13:35:39 +0000326 switch (sig) {
327 case SIGTERM:
328 break;
329 case SIGPIPE:
330 ++in_sighandler;
331 return;
plars865695b2001-08-27 22:15:12 +0000332 default:
subrata_modak56207ce2009-03-23 13:35:39 +0000333 tst_resm(TFAIL, "sighandler() received invalid signal "
334 ": %d", sig);
335 break;
plars865695b2001-08-27 22:15:12 +0000336 }
337
338 if ((unlink(f_name) < 0) && (errno != ENOENT)) {
339 tst_resm(TFAIL, "unlink Failed--file = %s, errno = %d",
340 f_name, errno);
341 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800342 }
plars865695b2001-08-27 22:15:12 +0000343 exit(sig);
344}
345
346/*
347 * l_seek()
348 * Wrap around for regular lseek function for giving error message
349 */
subrata_modak56207ce2009-03-23 13:35:39 +0000350long l_seek(int fdesc, long offset, int whence)
plars865695b2001-08-27 22:15:12 +0000351{
352 if (lseek(fdesc, offset, whence) < 0) {
353 tst_resm(TFAIL, "lseek Failed : errno = %d", errno);
354 fail = 1;
355 }
subrata_modak43337a32009-02-26 11:43:51 +0000356 return 0;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700357}