plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 5 | * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 6 | * |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 7 | * 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 11 | * |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 12 | * 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 16 | * |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 17 | * 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 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * DESCRIPTION |
| 24 | * Testcase to check the basic functionality of the readv(2) system call. |
| 25 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | * ALGORITHM |
| 27 | * Create a IO vector, and attempt to readv() various components of it. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 28 | */ |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 29 | #include <stdlib.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | #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 Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 37 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 38 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | #define CHUNK 64 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 40 | |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 41 | char *TCID = "readv01"; |
| 42 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | |
Cyril Hrubis | 2575e38 | 2013-11-28 14:13:18 +0100 | [diff] [blame] | 44 | static char buf[CHUNK]; |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 45 | |
Cyril Hrubis | 2575e38 | 2013-11-28 14:13:18 +0100 | [diff] [blame] | 46 | static struct iovec rd_iovec[] = { |
| 47 | {buf, CHUNK}, |
| 48 | {NULL, 0}, |
| 49 | {NULL, 0}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 52 | static int fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 54 | static void setup(void); |
| 55 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
robbiew | 0c7e81c | 2003-03-27 17:44:57 +0000 | [diff] [blame] | 57 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | { |
Cyril Hrubis | 2575e38 | 2013-11-28 14:13:18 +0100 | [diff] [blame] | 59 | int lc, i, fail; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 60 | const char *msg; |
| 61 | char *vec; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 63 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 64 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | |
| 66 | setup(); |
| 67 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 69 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
Cyril Hrubis | 2575e38 | 2013-11-28 14:13:18 +0100 | [diff] [blame] | 71 | SAFE_LSEEK(cleanup, fd, 0, SEEK_SET); |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 72 | |
Cyril Hrubis | 2575e38 | 2013-11-28 14:13:18 +0100 | [diff] [blame] | 73 | if (readv(fd, rd_iovec, 0) == -1) |
| 74 | tst_resm(TFAIL | TERRNO, "readv failed unexpectedly"); |
| 75 | else |
plars | 16b0e34 | 2002-10-10 17:16:03 +0000 | [diff] [blame] | 76 | tst_resm(TPASS, "readv read 0 io vectors"); |
plars | 16b0e34 | 2002-10-10 17:16:03 +0000 | [diff] [blame] | 77 | |
Cyril Hrubis | 2575e38 | 2013-11-28 14:13:18 +0100 | [diff] [blame] | 78 | memset(rd_iovec[0].iov_base, 0x00, CHUNK); |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 79 | |
Cyril Hrubis | 2575e38 | 2013-11-28 14:13:18 +0100 | [diff] [blame] | 80 | if (readv(fd, rd_iovec, 3) != CHUNK) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | tst_resm(TFAIL, "readv failed reading %d bytes, " |
| 82 | "followed by two NULL vectors", CHUNK); |
| 83 | } else { |
Cyril Hrubis | 2575e38 | 2013-11-28 14:13:18 +0100 | [diff] [blame] | 84 | 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); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 99 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 101 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 104 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 107 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | TEST_PAUSE; |
| 109 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | tst_tmpdir(); |
| 111 | |
Cyril Hrubis | 2575e38 | 2013-11-28 14:13:18 +0100 | [diff] [blame] | 112 | memset(buf, 0x42, sizeof(buf)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 114 | fd = SAFE_OPEN(cleanup, "data_file", O_WRONLY | O_CREAT, 0666); |
Cyril Hrubis | 2575e38 | 2013-11-28 14:13:18 +0100 | [diff] [blame] | 115 | SAFE_WRITE(cleanup, 1, fd, buf, sizeof(buf)); |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 116 | SAFE_CLOSE(cleanup, fd); |
| 117 | fd = SAFE_OPEN(cleanup, "data_file", O_RDONLY); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 120 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | { |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 122 | if (fd > 0) |
| 123 | close(fd); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 124 | |
Cyril Hrubis | f025a4e | 2013-11-27 20:03:25 +0100 | [diff] [blame] | 125 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 126 | } |