1 /* 2 * Copyright (c) 2015 Dmitry V. Levin <ldv (at) altlinux.org> 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 28 #ifdef HAVE_CONFIG_H 29 # include "config.h" 30 #endif 31 32 #include <assert.h> 33 #include <errno.h> 34 #include <inttypes.h> 35 #include <stdio.h> 36 #include <time.h> 37 #include <unistd.h> 38 #include <sys/fcntl.h> 39 #include <sys/syscall.h> 40 41 #if defined __NR_io_setup \ 42 && defined __NR_io_submit \ 43 && defined __NR_io_getevents \ 44 && defined __NR_io_cancel \ 45 && defined __NR_io_destroy 46 # include <linux/aio_abi.h> 47 48 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 49 50 int 51 main(void) 52 { 53 static char data0[4096]; 54 static char data1[8192]; 55 56 const struct iocb cb[] = { 57 { 58 .aio_data = 0xfeedface11111111, 59 .aio_reqprio = 11, 60 .aio_buf = (unsigned long) data0, 61 .aio_offset = 0xdeface1facefeed, 62 .aio_nbytes = sizeof(data0) 63 }, 64 { 65 .aio_data = 0xfeedface22222222, 66 .aio_reqprio = 22, 67 .aio_buf = (unsigned long) data1, 68 .aio_offset = 0xdeface2cafef00d, 69 .aio_nbytes = sizeof(data1) 70 } 71 }; 72 const struct iovec iov0[] = { 73 { 74 .iov_base = data0, 75 .iov_len = sizeof(data0) / 4 76 }, 77 { 78 .iov_base = data0 + sizeof(data0) / 4, 79 .iov_len = sizeof(data0) / 4 * 3 80 }, 81 }; 82 const struct iovec iov1[] = { 83 { 84 .iov_base = data1, 85 .iov_len = sizeof(data1) / 4 86 }, 87 { 88 .iov_base = data1 + sizeof(data1) / 4, 89 .iov_len = sizeof(data1) / 4 * 3 90 }, 91 }; 92 const struct iocb cbv[] = { 93 { 94 .aio_data = 0xfeed11111111face, 95 .aio_lio_opcode = 7, 96 .aio_reqprio = 111, 97 .aio_buf = (unsigned long) &iov0, 98 .aio_offset = 0xdeface1facefeed, 99 .aio_nbytes = ARRAY_SIZE(iov0) 100 }, 101 { 102 .aio_data = 0xfeed22222222face, 103 .aio_lio_opcode = 7, 104 .aio_reqprio = 222, 105 .aio_buf = (unsigned long) &iov1, 106 .aio_offset = 0xdeface2cafef00d, 107 .aio_nbytes = ARRAY_SIZE(iov1) 108 } 109 }; 110 struct iocb cbc = { 111 .aio_data = 0xdeadbeefbadc0ded, 112 .aio_reqprio = 99, 113 .aio_fildes = -42 114 }; 115 116 const long cbs[ARRAY_SIZE(cb) + 2] = { 117 (long) &cb[0], (long) &cb[1], 118 0xdeadbeef, 0xbadc0ded 119 }; 120 const long cbvs[ARRAY_SIZE(cb) + 2] = { 121 (long) &cbv[0], (long) &cbv[1], 122 0xdeadbeef, 0xbadc0ded 123 }; 124 125 unsigned long ctx = 0; 126 const unsigned int nr = ARRAY_SIZE(cb); 127 const unsigned long lnr = (unsigned long) (0xdeadbeef00000000ULL | nr); 128 129 struct io_event ev[nr]; 130 const struct timespec ts = { .tv_nsec = 123456789 }; 131 132 (void) close(0); 133 if (open("/dev/zero", O_RDONLY)) 134 return 77; 135 136 if (syscall(__NR_io_setup, lnr, &ctx)) 137 return 77; 138 printf("io_setup(%u, [%lu]) = 0\n", nr, ctx); 139 140 if (syscall(__NR_io_submit, ctx, nr, cbs) != (long) nr) 141 return 77; 142 printf("io_submit(%lu, %u, [" 143 "{data=%#llx, pread, reqprio=11, fildes=0, " 144 "buf=%p, nbytes=%u, offset=%lld}, " 145 "{data=%#llx, pread, reqprio=22, fildes=0, " 146 "buf=%p, nbytes=%u, offset=%lld}" 147 "]) = %u\n", 148 ctx, nr, 149 (unsigned long long) cb[0].aio_data, data0, 150 (unsigned int) sizeof(data0), (long long) cb[0].aio_offset, 151 (unsigned long long) cb[1].aio_data, data1, 152 (unsigned int) sizeof(data1), (long long) cb[1].aio_offset, 153 nr); 154 155 assert(syscall(__NR_io_getevents, ctx, nr, nr + 1, ev, &ts) == (long) nr); 156 printf("io_getevents(%lu, %u, %u, [" 157 "{data=%#llx, obj=%p, res=%u, res2=0}, " 158 "{data=%#llx, obj=%p, res=%u, res2=0}" 159 "], {0, 123456789}) = %u\n", 160 ctx, nr, nr + 1, 161 (unsigned long long) cb[0].aio_data, &cb[0], 162 (unsigned int) sizeof(data0), 163 (unsigned long long) cb[1].aio_data, &cb[1], 164 (unsigned int) sizeof(data1), 165 nr); 166 167 assert(syscall(__NR_io_cancel, ctx, &cbc, ev) == -1 && EINVAL == errno); 168 printf("io_cancel(%lu, {data=%#llx, pread, reqprio=99, fildes=-42}, %p) " 169 "= -1 EINVAL (Invalid argument)\n", 170 ctx, (unsigned long long) cbc.aio_data, ev); 171 172 if (syscall(__NR_io_submit, ctx, nr, cbvs) != (long) nr) 173 return 77; 174 printf("io_submit(%lu, %u, [" 175 "{data=%#llx, preadv, reqprio=%hd, fildes=0, " 176 "iovec=[{%p, %u}, {%p, %u}], offset=%lld}, " 177 "{data=%#llx, preadv, reqprio=%hd, fildes=0, " 178 "iovec=[{%p, %u}, {%p, %u}], offset=%lld}" 179 "]) = %u\n", 180 ctx, nr, 181 (unsigned long long) cbv[0].aio_data, cbv[0].aio_reqprio, 182 iov0[0].iov_base, (unsigned int) iov0[0].iov_len, 183 iov0[1].iov_base, (unsigned int) iov0[1].iov_len, 184 (long long) cbv[0].aio_offset, 185 (unsigned long long) cbv[1].aio_data, cbv[1].aio_reqprio, 186 iov1[0].iov_base, (unsigned int) iov1[0].iov_len, 187 iov1[1].iov_base, (unsigned int) iov1[1].iov_len, 188 (long long) cbv[1].aio_offset, 189 nr); 190 191 assert(syscall(__NR_io_destroy, ctx) == 0); 192 printf("io_destroy(%lu) = 0\n", ctx); 193 194 puts("+++ exited with 0 +++"); 195 return 0; 196 } 197 198 #else 199 200 int 201 main(void) 202 { 203 return 77; 204 } 205 206 #endif 207