blob: bbf29135c956fca6790e843e9fdfb1370793c663 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubisf025a4e2013-11-27 20:03:25 +01002 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +00004 *
Cyril Hrubisf025a4e2013-11-27 20:03:25 +01005 * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
plars865695b2001-08-27 22:15:12 +00006 *
Cyril Hrubisf025a4e2013-11-27 20:03:25 +01007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
plars865695b2001-08-27 22:15:12 +000011 *
Cyril Hrubisf025a4e2013-11-27 20:03:25 +010012 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU General Public License for more details.
plars865695b2001-08-27 22:15:12 +000016 *
Cyril Hrubisf025a4e2013-11-27 20:03:25 +010017 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000020 */
21
22/*
plars865695b2001-08-27 22:15:12 +000023 * DESCRIPTION
24 * Testcase to check the basic functionality of the readv(2) system call.
25 *
plars865695b2001-08-27 22:15:12 +000026 * ALGORITHM
27 * Create a IO vector, and attempt to readv() various components of it.
plars865695b2001-08-27 22:15:12 +000028 */
Cyril Hrubisf025a4e2013-11-27 20:03:25 +010029#include <stdlib.h>
plars865695b2001-08-27 22:15:12 +000030#include <sys/types.h>
31#include <sys/uio.h>
32#include <sys/fcntl.h>
33#include <memory.h>
34#include <errno.h>
35
36#include "test.h"
Cyril Hrubisf025a4e2013-11-27 20:03:25 +010037#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000038
plars865695b2001-08-27 22:15:12 +000039#define CHUNK 64
plars865695b2001-08-27 22:15:12 +000040
Cyril Hrubisf025a4e2013-11-27 20:03:25 +010041char *TCID = "readv01";
42int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000043
Cyril Hrubis2575e382013-11-28 14:13:18 +010044static char buf[CHUNK];
Cyril Hrubisf025a4e2013-11-27 20:03:25 +010045
Cyril Hrubis2575e382013-11-28 14:13:18 +010046static struct iovec rd_iovec[] = {
47 {buf, CHUNK},
48 {NULL, 0},
49 {NULL, 0},
plars865695b2001-08-27 22:15:12 +000050};
51
Cyril Hrubisf025a4e2013-11-27 20:03:25 +010052static int fd;
plars865695b2001-08-27 22:15:12 +000053
Cyril Hrubisf025a4e2013-11-27 20:03:25 +010054static void setup(void);
55static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000056
robbiew0c7e81c2003-03-27 17:44:57 +000057int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000058{
Cyril Hrubis2575e382013-11-28 14:13:18 +010059 int lc, i, fail;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020060 const char *msg;
61 char *vec;
plars865695b2001-08-27 22:15:12 +000062
Cyril Hrubisf025a4e2013-11-27 20:03:25 +010063 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080064 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000065
66 setup();
67
plars865695b2001-08-27 22:15:12 +000068 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080069 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000070
Cyril Hrubis2575e382013-11-28 14:13:18 +010071 SAFE_LSEEK(cleanup, fd, 0, SEEK_SET);
Cyril Hrubisf025a4e2013-11-27 20:03:25 +010072
Cyril Hrubis2575e382013-11-28 14:13:18 +010073 if (readv(fd, rd_iovec, 0) == -1)
74 tst_resm(TFAIL | TERRNO, "readv failed unexpectedly");
75 else
plars16b0e342002-10-10 17:16:03 +000076 tst_resm(TPASS, "readv read 0 io vectors");
plars16b0e342002-10-10 17:16:03 +000077
Cyril Hrubis2575e382013-11-28 14:13:18 +010078 memset(rd_iovec[0].iov_base, 0x00, CHUNK);
Cyril Hrubisf025a4e2013-11-27 20:03:25 +010079
Cyril Hrubis2575e382013-11-28 14:13:18 +010080 if (readv(fd, rd_iovec, 3) != CHUNK) {
plars865695b2001-08-27 22:15:12 +000081 tst_resm(TFAIL, "readv failed reading %d bytes, "
82 "followed by two NULL vectors", CHUNK);
83 } else {
Cyril Hrubis2575e382013-11-28 14:13:18 +010084 fail = 0;
85 vec = rd_iovec[0].iov_base;
86
87 for (i = 0; i < CHUNK; i++) {
88 if (vec[i] != 0x42)
89 fail++;
90 }
91
92 if (fail)
93 tst_resm(TFAIL, "Wrong buffer content");
94 else
95 tst_resm(TPASS, "readv passed reading %d bytes "
96 "followed by two NULL vectors", CHUNK);
plars865695b2001-08-27 22:15:12 +000097 }
98 }
Cyril Hrubisf025a4e2013-11-27 20:03:25 +010099
plars865695b2001-08-27 22:15:12 +0000100 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800101 tst_exit();
plars865695b2001-08-27 22:15:12 +0000102}
103
Cyril Hrubisf025a4e2013-11-27 20:03:25 +0100104static void setup(void)
plars865695b2001-08-27 22:15:12 +0000105{
plars865695b2001-08-27 22:15:12 +0000106 tst_sig(NOFORK, DEF_HANDLER, cleanup);
107
plars865695b2001-08-27 22:15:12 +0000108 TEST_PAUSE;
109
plars865695b2001-08-27 22:15:12 +0000110 tst_tmpdir();
111
Cyril Hrubis2575e382013-11-28 14:13:18 +0100112 memset(buf, 0x42, sizeof(buf));
plars865695b2001-08-27 22:15:12 +0000113
Cyril Hrubisf025a4e2013-11-27 20:03:25 +0100114 fd = SAFE_OPEN(cleanup, "data_file", O_WRONLY | O_CREAT, 0666);
Cyril Hrubis2575e382013-11-28 14:13:18 +0100115 SAFE_WRITE(cleanup, 1, fd, buf, sizeof(buf));
Cyril Hrubisf025a4e2013-11-27 20:03:25 +0100116 SAFE_CLOSE(cleanup, fd);
117 fd = SAFE_OPEN(cleanup, "data_file", O_RDONLY);
plars865695b2001-08-27 22:15:12 +0000118}
119
Cyril Hrubisf025a4e2013-11-27 20:03:25 +0100120static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000121{
Cyril Hrubisf025a4e2013-11-27 20:03:25 +0100122 if (fd > 0)
123 close(fd);
Garrett Cooper2c282152010-12-16 00:55:50 -0800124
Cyril Hrubisf025a4e2013-11-27 20:03:25 +0100125 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700126}