Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 1 | /* |
Dmitry V. Levin | 9ad4409 | 2016-01-06 11:27:15 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org> |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
Dmitry V. Levin | 0c8853c | 2016-01-02 13:28:43 +0000 | [diff] [blame] | 28 | #include "tests.h" |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 29 | #include <assert.h> |
Dmitry V. Levin | c36270a | 2016-01-11 02:20:04 +0000 | [diff] [blame] | 30 | #include <fcntl.h> |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 31 | #include <inttypes.h> |
| 32 | #include <stdio.h> |
Dmitry V. Levin | d1c663a | 2015-12-16 02:00:01 +0000 | [diff] [blame] | 33 | #include <time.h> |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 34 | #include <unistd.h> |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 35 | #include <sys/syscall.h> |
| 36 | |
| 37 | #if defined __NR_io_setup \ |
| 38 | && defined __NR_io_submit \ |
| 39 | && defined __NR_io_getevents \ |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 40 | && defined __NR_io_cancel \ |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 41 | && defined __NR_io_destroy |
| 42 | # include <linux/aio_abi.h> |
| 43 | |
| 44 | int |
| 45 | main(void) |
| 46 | { |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 47 | const unsigned int sizeof_data0 = 4096; |
| 48 | const unsigned int sizeof_data1 = 8192; |
| 49 | void *data0 = tail_alloc(sizeof_data0); |
| 50 | void *data1 = tail_alloc(sizeof_data1); |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 51 | |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 52 | const struct iocb proto_cb[] = { |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 53 | { |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 54 | .aio_data = 0xfeedface11111111, |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 55 | .aio_reqprio = 11, |
| 56 | .aio_buf = (unsigned long) data0, |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 57 | .aio_offset = 0xdeface1facefeed, |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 58 | .aio_nbytes = sizeof_data0 |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 59 | }, |
| 60 | { |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 61 | .aio_data = 0xfeedface22222222, |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 62 | .aio_reqprio = 22, |
| 63 | .aio_buf = (unsigned long) data1, |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 64 | .aio_offset = 0xdeface2cafef00d, |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 65 | .aio_nbytes = sizeof_data1 |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 66 | } |
| 67 | }; |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 68 | const struct iocb *cb = tail_memdup(proto_cb, sizeof(proto_cb)); |
| 69 | |
| 70 | const struct iovec proto_iov0[] = { |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 71 | { |
| 72 | .iov_base = data0, |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 73 | .iov_len = sizeof_data0 / 4 |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 74 | }, |
| 75 | { |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 76 | .iov_base = data0 + sizeof_data0 / 4, |
| 77 | .iov_len = sizeof_data0 / 4 * 3 |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 78 | }, |
| 79 | }; |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 80 | const struct iovec *iov0 = tail_memdup(proto_iov0, sizeof(proto_iov0)); |
| 81 | |
| 82 | const struct iovec proto_iov1[] = { |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 83 | { |
| 84 | .iov_base = data1, |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 85 | .iov_len = sizeof_data1 / 4 |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 86 | }, |
| 87 | { |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 88 | .iov_base = data1 + sizeof_data1 / 4, |
| 89 | .iov_len = sizeof_data1 / 4 * 3 |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 90 | }, |
| 91 | }; |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 92 | const struct iovec *iov1 = tail_memdup(proto_iov1, sizeof(proto_iov1)); |
| 93 | |
| 94 | const struct iocb proto_cbv[] = { |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 95 | { |
| 96 | .aio_data = 0xfeed11111111face, |
| 97 | .aio_lio_opcode = 7, |
| 98 | .aio_reqprio = 111, |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 99 | .aio_buf = (unsigned long) iov0, |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 100 | .aio_offset = 0xdeface1facefeed, |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 101 | .aio_nbytes = ARRAY_SIZE(proto_iov0) |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 102 | }, |
| 103 | { |
| 104 | .aio_data = 0xfeed22222222face, |
| 105 | .aio_lio_opcode = 7, |
| 106 | .aio_reqprio = 222, |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 107 | .aio_buf = (unsigned long) iov1, |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 108 | .aio_offset = 0xdeface2cafef00d, |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 109 | .aio_nbytes = ARRAY_SIZE(proto_iov1) |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 110 | } |
| 111 | }; |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 112 | const struct iocb *cbv = tail_memdup(proto_cbv, sizeof(proto_cbv)); |
| 113 | |
| 114 | const struct iocb proto_cbc = { |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 115 | .aio_data = 0xdeadbeefbadc0ded, |
| 116 | .aio_reqprio = 99, |
| 117 | .aio_fildes = -42 |
| 118 | }; |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 119 | const struct iocb *cbc = tail_memdup(&proto_cbc, sizeof(proto_cbc)); |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 120 | |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 121 | const long proto_cbs[] = { |
| 122 | (long) &cb[0], (long) &cb[1] |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 123 | }; |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 124 | const long *cbs = tail_memdup(proto_cbs, sizeof(proto_cbs)); |
| 125 | |
| 126 | const long proto_cbvs[] = { |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 127 | (long) &cbv[0], (long) &cbv[1], |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 128 | }; |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 129 | const long *cbvs = tail_memdup(proto_cbvs, sizeof(proto_cbvs)); |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 130 | |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 131 | unsigned long *ctx = tail_alloc(sizeof(unsigned long)); |
| 132 | *ctx = 0; |
| 133 | |
| 134 | const unsigned int nr = ARRAY_SIZE(proto_cb); |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 135 | const unsigned long lnr = (unsigned long) (0xdeadbeef00000000ULL | nr); |
| 136 | |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 137 | const struct io_event *ev = tail_alloc(nr * sizeof(struct io_event)); |
| 138 | const struct timespec proto_ts = { .tv_nsec = 123456789 }; |
| 139 | const struct timespec *ts = tail_memdup(&proto_ts, sizeof(proto_ts)); |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 140 | |
| 141 | (void) close(0); |
| 142 | if (open("/dev/zero", O_RDONLY)) |
Dmitry V. Levin | 9ad4409 | 2016-01-06 11:27:15 +0000 | [diff] [blame] | 143 | perror_msg_and_skip("open: %s", "/dev/zero"); |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 144 | |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 145 | if (syscall(__NR_io_setup, lnr, ctx)) |
Dmitry V. Levin | 9ad4409 | 2016-01-06 11:27:15 +0000 | [diff] [blame] | 146 | perror_msg_and_skip("io_setup"); |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 147 | printf("io_setup(%u, [%lu]) = 0\n", nr, *ctx); |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 148 | |
Dmitry V. Levin | 2868e2b | 2016-05-07 22:40:06 +0000 | [diff] [blame] | 149 | assert(syscall(__NR_io_submit, *ctx, -1L, cbs) == -1); |
| 150 | printf("io_submit(%lu, -1, %p) = -1 %s (%m)\n", |
| 151 | *ctx, cbs, errno2name()); |
| 152 | |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 153 | if (syscall(__NR_io_submit, *ctx, nr, cbs) != (long) nr) |
Dmitry V. Levin | 9ad4409 | 2016-01-06 11:27:15 +0000 | [diff] [blame] | 154 | perror_msg_and_skip("io_submit"); |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 155 | printf("io_submit(%lu, %u, [" |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 156 | "{data=%#llx, pread, reqprio=11, fildes=0, " |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 157 | "buf=%p, nbytes=%u, offset=%lld}, " |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 158 | "{data=%#llx, pread, reqprio=22, fildes=0, " |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 159 | "buf=%p, nbytes=%u, offset=%lld}" |
| 160 | "]) = %u\n", |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 161 | *ctx, nr, |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 162 | (unsigned long long) cb[0].aio_data, data0, |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 163 | sizeof_data0, (long long) cb[0].aio_offset, |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 164 | (unsigned long long) cb[1].aio_data, data1, |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 165 | sizeof_data1, (long long) cb[1].aio_offset, |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 166 | nr); |
| 167 | |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 168 | assert(syscall(__NR_io_getevents, *ctx, nr, nr + 1, ev, ts) == (long) nr); |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 169 | printf("io_getevents(%lu, %u, %u, [" |
| 170 | "{data=%#llx, obj=%p, res=%u, res2=0}, " |
| 171 | "{data=%#llx, obj=%p, res=%u, res2=0}" |
| 172 | "], {0, 123456789}) = %u\n", |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 173 | *ctx, nr, nr + 1, |
| 174 | (unsigned long long) cb[0].aio_data, &cb[0], sizeof_data0, |
| 175 | (unsigned long long) cb[1].aio_data, &cb[1], sizeof_data1, |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 176 | nr); |
| 177 | |
Dmitry V. Levin | 5327e9c | 2016-04-21 20:29:29 +0000 | [diff] [blame] | 178 | assert(syscall(__NR_io_cancel, *ctx, cbc, ev) == -1); |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 179 | printf("io_cancel(%lu, {data=%#llx, pread, reqprio=99, fildes=-42}, %p) " |
Dmitry V. Levin | 5327e9c | 2016-04-21 20:29:29 +0000 | [diff] [blame] | 180 | "= -1 %s (%m)\n", |
| 181 | *ctx, (unsigned long long) cbc->aio_data, ev, errno2name()); |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 182 | |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 183 | if (syscall(__NR_io_submit, *ctx, nr, cbvs) != (long) nr) |
Dmitry V. Levin | 9ad4409 | 2016-01-06 11:27:15 +0000 | [diff] [blame] | 184 | perror_msg_and_skip("io_submit"); |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 185 | printf("io_submit(%lu, %u, [" |
| 186 | "{data=%#llx, preadv, reqprio=%hd, fildes=0, " |
| 187 | "iovec=[{%p, %u}, {%p, %u}], offset=%lld}, " |
| 188 | "{data=%#llx, preadv, reqprio=%hd, fildes=0, " |
| 189 | "iovec=[{%p, %u}, {%p, %u}], offset=%lld}" |
| 190 | "]) = %u\n", |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 191 | *ctx, nr, |
Dmitry V. Levin | f56046e | 2015-08-26 17:48:40 +0000 | [diff] [blame] | 192 | (unsigned long long) cbv[0].aio_data, cbv[0].aio_reqprio, |
| 193 | iov0[0].iov_base, (unsigned int) iov0[0].iov_len, |
| 194 | iov0[1].iov_base, (unsigned int) iov0[1].iov_len, |
| 195 | (long long) cbv[0].aio_offset, |
| 196 | (unsigned long long) cbv[1].aio_data, cbv[1].aio_reqprio, |
| 197 | iov1[0].iov_base, (unsigned int) iov1[0].iov_len, |
| 198 | iov1[1].iov_base, (unsigned int) iov1[1].iov_len, |
| 199 | (long long) cbv[1].aio_offset, |
| 200 | nr); |
| 201 | |
Dmitry V. Levin | 68f7a66 | 2016-01-13 22:06:18 +0000 | [diff] [blame] | 202 | assert(syscall(__NR_io_destroy, *ctx) == 0); |
| 203 | printf("io_destroy(%lu) = 0\n", *ctx); |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 204 | |
| 205 | puts("+++ exited with 0 +++"); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | #else |
| 210 | |
Dmitry V. Levin | 9ad4409 | 2016-01-06 11:27:15 +0000 | [diff] [blame] | 211 | SKIP_MAIN_UNDEFINED("__NR_io_*") |
Dmitry V. Levin | 13c2173 | 2015-08-26 12:49:07 +0000 | [diff] [blame] | 212 | |
| 213 | #endif |