blob: 08974e1b5ac6e342081eb480c39f1cac80813fe9 [file] [log] [blame]
Dmitry V. Levin13c21732015-08-26 12:49:07 +00001/*
2 * Copyright (c) 2015 Dmitry V. Levin <ldv@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
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000032#include <assert.h>
33#include <errno.h>
Dmitry V. Levin13c21732015-08-26 12:49:07 +000034#include <inttypes.h>
35#include <stdio.h>
36#include <unistd.h>
37#include <sys/fcntl.h>
38#include <sys/syscall.h>
39
40#if defined __NR_io_setup \
41 && defined __NR_io_submit \
42 && defined __NR_io_getevents \
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000043 && defined __NR_io_cancel \
Dmitry V. Levin13c21732015-08-26 12:49:07 +000044 && defined __NR_io_destroy
45# include <linux/aio_abi.h>
46
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000047#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
48
Dmitry V. Levin13c21732015-08-26 12:49:07 +000049int
50main(void)
51{
52 static char data0[4096];
53 static char data1[8192];
54
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000055 const struct iocb cb[] = {
Dmitry V. Levin13c21732015-08-26 12:49:07 +000056 {
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000057 .aio_data = 0xfeedface11111111,
Dmitry V. Levin13c21732015-08-26 12:49:07 +000058 .aio_reqprio = 11,
59 .aio_buf = (unsigned long) data0,
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000060 .aio_offset = 0xdeface1facefeed,
Dmitry V. Levin13c21732015-08-26 12:49:07 +000061 .aio_nbytes = sizeof(data0)
62 },
63 {
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000064 .aio_data = 0xfeedface22222222,
Dmitry V. Levin13c21732015-08-26 12:49:07 +000065 .aio_reqprio = 22,
66 .aio_buf = (unsigned long) data1,
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000067 .aio_offset = 0xdeface2cafef00d,
Dmitry V. Levin13c21732015-08-26 12:49:07 +000068 .aio_nbytes = sizeof(data1)
69 }
70 };
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000071 const struct iovec iov0[] = {
72 {
73 .iov_base = data0,
74 .iov_len = sizeof(data0) / 4
75 },
76 {
77 .iov_base = data0 + sizeof(data0) / 4,
78 .iov_len = sizeof(data0) / 4 * 3
79 },
80 };
81 const struct iovec iov1[] = {
82 {
83 .iov_base = data1,
84 .iov_len = sizeof(data1) / 4
85 },
86 {
87 .iov_base = data1 + sizeof(data1) / 4,
88 .iov_len = sizeof(data1) / 4 * 3
89 },
90 };
91 const struct iocb cbv[] = {
92 {
93 .aio_data = 0xfeed11111111face,
94 .aio_lio_opcode = 7,
95 .aio_reqprio = 111,
96 .aio_buf = (unsigned long) &iov0,
97 .aio_offset = 0xdeface1facefeed,
98 .aio_nbytes = ARRAY_SIZE(iov0)
99 },
100 {
101 .aio_data = 0xfeed22222222face,
102 .aio_lio_opcode = 7,
103 .aio_reqprio = 222,
104 .aio_buf = (unsigned long) &iov1,
105 .aio_offset = 0xdeface2cafef00d,
106 .aio_nbytes = ARRAY_SIZE(iov1)
107 }
108 };
109 struct iocb cbc = {
110 .aio_data = 0xdeadbeefbadc0ded,
111 .aio_reqprio = 99,
112 .aio_fildes = -42
113 };
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000114
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000115 const long cbs[ARRAY_SIZE(cb) + 2] = {
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000116 (long) &cb[0], (long) &cb[1],
117 0xdeadbeef, 0xbadc0ded
118 };
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000119 const long cbvs[ARRAY_SIZE(cb) + 2] = {
120 (long) &cbv[0], (long) &cbv[1],
121 0xdeadbeef, 0xbadc0ded
122 };
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000123
124 unsigned long ctx = 0;
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000125 const unsigned int nr = ARRAY_SIZE(cb);
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000126 const unsigned long lnr = (unsigned long) (0xdeadbeef00000000ULL | nr);
127
128 struct io_event ev[nr];
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000129 const struct timespec ts = { .tv_nsec = 123456789 };
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000130
131 (void) close(0);
132 if (open("/dev/zero", O_RDONLY))
133 return 77;
134
135 if (syscall(__NR_io_setup, lnr, &ctx))
136 return 77;
137 printf("io_setup(%u, [%lu]) = 0\n", nr, ctx);
138
139 if (syscall(__NR_io_submit, ctx, nr, cbs) != (long) nr)
140 return 77;
141 printf("io_submit(%lu, %u, ["
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000142 "{data=%#llx, pread, reqprio=11, fildes=0, "
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000143 "buf=%p, nbytes=%u, offset=%lld}, "
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000144 "{data=%#llx, pread, reqprio=22, fildes=0, "
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000145 "buf=%p, nbytes=%u, offset=%lld}"
146 "]) = %u\n",
147 ctx, nr,
148 (unsigned long long) cb[0].aio_data, data0,
149 (unsigned int) sizeof(data0), (long long) cb[0].aio_offset,
150 (unsigned long long) cb[1].aio_data, data1,
151 (unsigned int) sizeof(data1), (long long) cb[1].aio_offset,
152 nr);
153
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000154 assert(syscall(__NR_io_getevents, ctx, nr, nr + 1, ev, &ts) == (long) nr);
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000155 printf("io_getevents(%lu, %u, %u, ["
156 "{data=%#llx, obj=%p, res=%u, res2=0}, "
157 "{data=%#llx, obj=%p, res=%u, res2=0}"
158 "], {0, 123456789}) = %u\n",
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000159 ctx, nr, nr + 1,
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000160 (unsigned long long) cb[0].aio_data, &cb[0],
161 (unsigned int) sizeof(data0),
162 (unsigned long long) cb[1].aio_data, &cb[1],
163 (unsigned int) sizeof(data1),
164 nr);
165
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000166 assert(syscall(__NR_io_cancel, ctx, &cbc, ev) == -1 && EINVAL == errno);
167 printf("io_cancel(%lu, {data=%#llx, pread, reqprio=99, fildes=-42}, %p) "
168 "= -1 EINVAL (Invalid argument)\n",
169 ctx, (unsigned long long) cbc.aio_data, ev);
170
171 if (syscall(__NR_io_submit, ctx, nr, cbvs) != (long) nr)
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000172 return 77;
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000173 printf("io_submit(%lu, %u, ["
174 "{data=%#llx, preadv, reqprio=%hd, fildes=0, "
175 "iovec=[{%p, %u}, {%p, %u}], offset=%lld}, "
176 "{data=%#llx, preadv, reqprio=%hd, fildes=0, "
177 "iovec=[{%p, %u}, {%p, %u}], offset=%lld}"
178 "]) = %u\n",
179 ctx, nr,
180 (unsigned long long) cbv[0].aio_data, cbv[0].aio_reqprio,
181 iov0[0].iov_base, (unsigned int) iov0[0].iov_len,
182 iov0[1].iov_base, (unsigned int) iov0[1].iov_len,
183 (long long) cbv[0].aio_offset,
184 (unsigned long long) cbv[1].aio_data, cbv[1].aio_reqprio,
185 iov1[0].iov_base, (unsigned int) iov1[0].iov_len,
186 iov1[1].iov_base, (unsigned int) iov1[1].iov_len,
187 (long long) cbv[1].aio_offset,
188 nr);
189
190 assert(syscall(__NR_io_destroy, ctx) == 0);
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000191 printf("io_destroy(%lu) = 0\n", ctx);
192
193 puts("+++ exited with 0 +++");
194 return 0;
195}
196
197#else
198
199int
200main(void)
201{
202 return 77;
203}
204
205#endif