subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) Crackerjack Project., 2007 |
Cyril Hrubis | d8a90be | 2011-03-16 18:13:38 +0100 | [diff] [blame] | 4 | * Copyright (c) 2011 Cyril Hrubis <chrubis@suse.cz> |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 14 | * the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | /* Porting from Crackerjack to LTP is done |
| 22 | by Masatake YAMATO <yamato@redhat.com> */ |
| 23 | |
subrata_modak | 79cefc1 | 2009-01-20 10:48:41 +0000 | [diff] [blame] | 24 | #include "config.h" |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 25 | #include "test.h" |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 26 | |
Cyril Hrubis | d8a90be | 2011-03-16 18:13:38 +0100 | [diff] [blame] | 27 | char *TCID = "io_submit01"; |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 28 | |
subrata_modak | 18dc0c2 | 2008-07-21 10:55:34 +0000 | [diff] [blame] | 29 | int TST_TOTAL = 3; |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 30 | |
subrata_modak | 79cefc1 | 2009-01-20 10:48:41 +0000 | [diff] [blame] | 31 | #ifdef HAVE_LIBAIO_H |
| 32 | #include <libaio.h> |
| 33 | #include <errno.h> |
| 34 | #include <string.h> |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 35 | #include <fcntl.h> |
| 36 | |
| 37 | #define TESTFILE "testfile" |
subrata_modak | 79cefc1 | 2009-01-20 10:48:41 +0000 | [diff] [blame] | 38 | |
Cyril Hrubis | d8a90be | 2011-03-16 18:13:38 +0100 | [diff] [blame] | 39 | static void cleanup(void) |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 40 | { |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 41 | tst_rmdir(); |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Cyril Hrubis | d8a90be | 2011-03-16 18:13:38 +0100 | [diff] [blame] | 44 | static void setup(void) |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 45 | { |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 46 | int fd; |
| 47 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 48 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 49 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 50 | TEST_PAUSE; |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 51 | |
| 52 | tst_tmpdir(); |
| 53 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 54 | fd = open(TESTFILE, O_CREAT | O_RDWR, 0755); |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 55 | if (fd == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 56 | tst_brkm(TBROK | TERRNO, cleanup, "open"); |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 57 | if (close(fd) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 58 | tst_brkm(TBROK | TERRNO, cleanup, "close"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 59 | } |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 60 | |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 61 | static void check_result(long exp, long act) |
| 62 | { |
| 63 | if (exp >= 0) { |
| 64 | if (act == exp) |
| 65 | tst_resm(TPASS, "expected success - " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 66 | "returned value = %ld", act); |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 67 | else |
| 68 | tst_resm(TFAIL, "unexpected failure - " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 69 | "returned value = %ld : %s", |
| 70 | act, strerror(-1 * act)); |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 71 | return; |
| 72 | } |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 73 | |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 74 | /* if return value is expected to be < 0 */ |
| 75 | if (act == exp) |
| 76 | tst_resm(TPASS, "expected failure - " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 77 | "returned value = %ld : %s", act, strerror(-1 * act)); |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 78 | else if (act == 0) |
| 79 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 80 | else |
| 81 | tst_resm(TFAIL, "unexpected failure - " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 82 | "returned value = %ld : %s, " |
| 83 | "expected value = %ld : %s", |
| 84 | act, strerror(-1 * act), exp, strerror(-1 * exp)); |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 85 | } |
Cyril Hrubis | d8a90be | 2011-03-16 18:13:38 +0100 | [diff] [blame] | 86 | |
Cyril Hrubis | d8a90be | 2011-03-16 18:13:38 +0100 | [diff] [blame] | 87 | int main(int argc, char *argv[]) |
| 88 | { |
| 89 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 90 | const char *msg; |
Cyril Hrubis | d8a90be | 2011-03-16 18:13:38 +0100 | [diff] [blame] | 91 | |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 92 | int rval, fd; |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 93 | char buf[256]; |
| 94 | struct iocb iocb; |
| 95 | struct iocb *iocbs[1]; |
Cyril Hrubis | d8a90be | 2011-03-16 18:13:38 +0100 | [diff] [blame] | 96 | io_context_t ctx; |
| 97 | |
Garrett Cooper | 54587dd | 2010-12-21 11:37:16 -0800 | [diff] [blame] | 98 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 99 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 100 | |
| 101 | setup(); |
| 102 | |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 103 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 104 | tst_count = 0; |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 105 | |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 106 | /* 1 - EINVAL */ |
| 107 | /* 1.1 - EINVAL: invalid ctx */ |
Jan Stancek | f1fd767 | 2014-05-06 13:13:25 +0200 | [diff] [blame] | 108 | memset(&ctx, 0, sizeof(ctx)); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | TEST(io_submit(ctx, 0, NULL)); |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 110 | check_result(-EINVAL, TEST_RETURN); |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 111 | |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 112 | /* 1.2 - EINVAL: invalid nr */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | rval = io_setup(1, &ctx); |
Garrett Cooper | 54587dd | 2010-12-21 11:37:16 -0800 | [diff] [blame] | 114 | if (rval != 0) |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 115 | tst_brkm(TBROK, cleanup, "io_setup failed: %d", rval); |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 116 | TEST(io_submit(ctx, -1, NULL)); |
| 117 | check_result(-EINVAL, TEST_RETURN); |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 118 | |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 119 | /* 1.3 - EINVAL: uninitialized iocb */ |
| 120 | iocbs[0] = &iocb; |
Jan Stancek | 42c5898 | 2014-05-06 13:07:20 +0200 | [diff] [blame] | 121 | |
| 122 | /* There are multiple checks we can hit with uninitialized |
| 123 | * iocb, but with "random" data it's not 100%. Make sure we |
| 124 | * fail eventually in opcode check. */ |
| 125 | iocb.aio_lio_opcode = -1; |
| 126 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | TEST(io_submit(ctx, 1, iocbs)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 128 | switch (TEST_RETURN) { |
Jan Stancek | 07694a1 | 2012-03-15 10:15:55 +0100 | [diff] [blame] | 129 | case -EINVAL: |
| 130 | case -EBADF: |
| 131 | case -EFAULT: |
| 132 | tst_resm(TPASS, "expected failure - " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 133 | "returned value = %ld : %s", |
| 134 | TEST_RETURN, strerror(-1 * TEST_RETURN)); |
Jan Stancek | 07694a1 | 2012-03-15 10:15:55 +0100 | [diff] [blame] | 135 | break; |
| 136 | default: |
| 137 | tst_resm(TFAIL, "unexpected failure - " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 138 | "returned value = %ld : %s, " |
| 139 | "expected one of -EINVAL, -EBADF, -EFAULT", |
| 140 | TEST_RETURN, strerror(-1 * TEST_RETURN)); |
Jan Stancek | 07694a1 | 2012-03-15 10:15:55 +0100 | [diff] [blame] | 141 | } |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 142 | |
| 143 | /* 2 - EFAULT: iocb points to invalid data */ |
| 144 | TEST(io_submit(ctx, 1, (struct iocb **)-1)); |
| 145 | check_result(-EFAULT, TEST_RETURN); |
| 146 | |
| 147 | /* |
| 148 | * 3 - Special case EFAULT or EINVAL (indetermination) |
| 149 | * |
| 150 | * The errno depends on the per architecture implementation |
| 151 | * of io_submit. On the architecture using compat_sys_io_submit |
| 152 | * as its implementation, errno is set to -EINVAL. |
| 153 | */ |
| 154 | TEST(io_submit(ctx, -1, (struct iocb **)-1)); |
Garrett Cooper | 54587dd | 2010-12-21 11:37:16 -0800 | [diff] [blame] | 155 | if (TEST_RETURN == 0) |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 156 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 157 | else if (TEST_RETURN == -EFAULT || TEST_RETURN == -EINVAL) |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 158 | tst_resm(TPASS, "expected failure - " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 159 | "returned value = %ld : %s", |
| 160 | TEST_RETURN, strerror(-1 * TEST_RETURN)); |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 161 | else |
| 162 | tst_resm(TFAIL, "unexpected failure - " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 163 | "returned value = %ld : %s, " |
| 164 | "expected = %d : %s or %d : %s", |
| 165 | TEST_RETURN, strerror(-1 * TEST_RETURN), |
| 166 | -EFAULT, strerror(EFAULT), |
| 167 | -EINVAL, strerror(EINVAL)); |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 168 | |
| 169 | /* |
| 170 | * 4 - EBADF: fd in iocb is invalid |
| 171 | */ |
| 172 | io_prep_pread(&iocb, -1, buf, sizeof(buf), 0); |
| 173 | iocbs[0] = &iocb; |
| 174 | TEST(io_submit(ctx, 1, iocbs)); |
| 175 | check_result(-EBADF, TEST_RETURN); |
| 176 | |
| 177 | /* 5 - Positive test: nr == 0 */ |
| 178 | TEST(io_submit(ctx, 0, NULL)); |
| 179 | check_result(0, TEST_RETURN); |
| 180 | |
| 181 | /* 6 - Positive test: valid fd */ |
| 182 | fd = open(TESTFILE, O_RDONLY); |
| 183 | if (fd == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 184 | tst_resm(TBROK | TERRNO, "open"); |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 185 | io_prep_pread(&iocb, fd, buf, sizeof(buf), 0); |
| 186 | iocbs[0] = &iocb; |
| 187 | TEST(io_submit(ctx, 1, iocbs)); |
| 188 | check_result(1, TEST_RETURN); |
| 189 | if (close(fd) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 190 | tst_resm(TBROK | TERRNO, "close"); |
Caspar Zhang | adfd02e | 2011-04-14 22:28:46 +0800 | [diff] [blame] | 191 | |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 192 | } |
| 193 | cleanup(); |
| 194 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 195 | tst_exit(); |
subrata_modak | c5792c9 | 2008-07-11 20:21:42 +0000 | [diff] [blame] | 196 | } |
subrata_modak | 79cefc1 | 2009-01-20 10:48:41 +0000 | [diff] [blame] | 197 | #else |
Cyril Hrubis | d8a90be | 2011-03-16 18:13:38 +0100 | [diff] [blame] | 198 | int main(int argc, char *argv[]) |
subrata_modak | 79cefc1 | 2009-01-20 10:48:41 +0000 | [diff] [blame] | 199 | { |
Garrett Cooper | 54587dd | 2010-12-21 11:37:16 -0800 | [diff] [blame] | 200 | tst_brkm(TCONF, NULL, "System doesn't support execution of the test"); |
subrata_modak | 79cefc1 | 2009-01-20 10:48:41 +0000 | [diff] [blame] | 201 | } |
Garrett Cooper | 54587dd | 2010-12-21 11:37:16 -0800 | [diff] [blame] | 202 | #endif |