blob: 29d2191d1d0977a001c8dba5e90fa4a1d083cdd6 [file] [log] [blame]
Dmitry V. Levin13c21732015-08-26 12:49:07 +00001/*
Dmitry V. Levin9ad44092016-01-06 11:27:15 +00002 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
Dmitry V. Levin13c21732015-08-26 12:49:07 +00003 * 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. Levin0c8853c2016-01-02 13:28:43 +000028#include "tests.h"
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000029#include <assert.h>
30#include <errno.h>
Dmitry V. Levinc36270a2016-01-11 02:20:04 +000031#include <fcntl.h>
Dmitry V. Levin13c21732015-08-26 12:49:07 +000032#include <inttypes.h>
33#include <stdio.h>
Dmitry V. Levind1c663a2015-12-16 02:00:01 +000034#include <time.h>
Dmitry V. Levin13c21732015-08-26 12:49:07 +000035#include <unistd.h>
Dmitry V. Levin13c21732015-08-26 12:49:07 +000036#include <sys/syscall.h>
37
38#if defined __NR_io_setup \
39 && defined __NR_io_submit \
40 && defined __NR_io_getevents \
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000041 && defined __NR_io_cancel \
Dmitry V. Levin13c21732015-08-26 12:49:07 +000042 && defined __NR_io_destroy
43# include <linux/aio_abi.h>
44
Dmitry V. Levin9ad44092016-01-06 11:27:15 +000045# define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000046
Dmitry V. Levin13c21732015-08-26 12:49:07 +000047int
48main(void)
49{
Dmitry V. Levin68f7a662016-01-13 22:06:18 +000050 const unsigned int sizeof_data0 = 4096;
51 const unsigned int sizeof_data1 = 8192;
52 void *data0 = tail_alloc(sizeof_data0);
53 void *data1 = tail_alloc(sizeof_data1);
Dmitry V. Levin13c21732015-08-26 12:49:07 +000054
Dmitry V. Levin68f7a662016-01-13 22:06:18 +000055 const struct iocb proto_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. Levin68f7a662016-01-13 22:06:18 +000061 .aio_nbytes = sizeof_data0
Dmitry V. Levin13c21732015-08-26 12:49:07 +000062 },
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. Levin68f7a662016-01-13 22:06:18 +000068 .aio_nbytes = sizeof_data1
Dmitry V. Levin13c21732015-08-26 12:49:07 +000069 }
70 };
Dmitry V. Levin68f7a662016-01-13 22:06:18 +000071 const struct iocb *cb = tail_memdup(proto_cb, sizeof(proto_cb));
72
73 const struct iovec proto_iov0[] = {
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000074 {
75 .iov_base = data0,
Dmitry V. Levin68f7a662016-01-13 22:06:18 +000076 .iov_len = sizeof_data0 / 4
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000077 },
78 {
Dmitry V. Levin68f7a662016-01-13 22:06:18 +000079 .iov_base = data0 + sizeof_data0 / 4,
80 .iov_len = sizeof_data0 / 4 * 3
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000081 },
82 };
Dmitry V. Levin68f7a662016-01-13 22:06:18 +000083 const struct iovec *iov0 = tail_memdup(proto_iov0, sizeof(proto_iov0));
84
85 const struct iovec proto_iov1[] = {
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000086 {
87 .iov_base = data1,
Dmitry V. Levin68f7a662016-01-13 22:06:18 +000088 .iov_len = sizeof_data1 / 4
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000089 },
90 {
Dmitry V. Levin68f7a662016-01-13 22:06:18 +000091 .iov_base = data1 + sizeof_data1 / 4,
92 .iov_len = sizeof_data1 / 4 * 3
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000093 },
94 };
Dmitry V. Levin68f7a662016-01-13 22:06:18 +000095 const struct iovec *iov1 = tail_memdup(proto_iov1, sizeof(proto_iov1));
96
97 const struct iocb proto_cbv[] = {
Dmitry V. Levinf56046e2015-08-26 17:48:40 +000098 {
99 .aio_data = 0xfeed11111111face,
100 .aio_lio_opcode = 7,
101 .aio_reqprio = 111,
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000102 .aio_buf = (unsigned long) iov0,
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000103 .aio_offset = 0xdeface1facefeed,
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000104 .aio_nbytes = ARRAY_SIZE(proto_iov0)
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000105 },
106 {
107 .aio_data = 0xfeed22222222face,
108 .aio_lio_opcode = 7,
109 .aio_reqprio = 222,
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000110 .aio_buf = (unsigned long) iov1,
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000111 .aio_offset = 0xdeface2cafef00d,
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000112 .aio_nbytes = ARRAY_SIZE(proto_iov1)
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000113 }
114 };
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000115 const struct iocb *cbv = tail_memdup(proto_cbv, sizeof(proto_cbv));
116
117 const struct iocb proto_cbc = {
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000118 .aio_data = 0xdeadbeefbadc0ded,
119 .aio_reqprio = 99,
120 .aio_fildes = -42
121 };
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000122 const struct iocb *cbc = tail_memdup(&proto_cbc, sizeof(proto_cbc));
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000123
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000124 const long proto_cbs[] = {
125 (long) &cb[0], (long) &cb[1]
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000126 };
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000127 const long *cbs = tail_memdup(proto_cbs, sizeof(proto_cbs));
128
129 const long proto_cbvs[] = {
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000130 (long) &cbv[0], (long) &cbv[1],
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000131 };
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000132 const long *cbvs = tail_memdup(proto_cbvs, sizeof(proto_cbvs));
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000133
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000134 unsigned long *ctx = tail_alloc(sizeof(unsigned long));
135 *ctx = 0;
136
137 const unsigned int nr = ARRAY_SIZE(proto_cb);
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000138 const unsigned long lnr = (unsigned long) (0xdeadbeef00000000ULL | nr);
139
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000140 const struct io_event *ev = tail_alloc(nr * sizeof(struct io_event));
141 const struct timespec proto_ts = { .tv_nsec = 123456789 };
142 const struct timespec *ts = tail_memdup(&proto_ts, sizeof(proto_ts));
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000143
144 (void) close(0);
145 if (open("/dev/zero", O_RDONLY))
Dmitry V. Levin9ad44092016-01-06 11:27:15 +0000146 perror_msg_and_skip("open: %s", "/dev/zero");
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000147
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000148 if (syscall(__NR_io_setup, lnr, ctx))
Dmitry V. Levin9ad44092016-01-06 11:27:15 +0000149 perror_msg_and_skip("io_setup");
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000150 printf("io_setup(%u, [%lu]) = 0\n", nr, *ctx);
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000151
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000152 if (syscall(__NR_io_submit, *ctx, nr, cbs) != (long) nr)
Dmitry V. Levin9ad44092016-01-06 11:27:15 +0000153 perror_msg_and_skip("io_submit");
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000154 printf("io_submit(%lu, %u, ["
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000155 "{data=%#llx, pread, reqprio=11, fildes=0, "
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000156 "buf=%p, nbytes=%u, offset=%lld}, "
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000157 "{data=%#llx, pread, reqprio=22, fildes=0, "
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000158 "buf=%p, nbytes=%u, offset=%lld}"
159 "]) = %u\n",
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000160 *ctx, nr,
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000161 (unsigned long long) cb[0].aio_data, data0,
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000162 sizeof_data0, (long long) cb[0].aio_offset,
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000163 (unsigned long long) cb[1].aio_data, data1,
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000164 sizeof_data1, (long long) cb[1].aio_offset,
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000165 nr);
166
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000167 assert(syscall(__NR_io_getevents, *ctx, nr, nr + 1, ev, ts) == (long) nr);
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000168 printf("io_getevents(%lu, %u, %u, ["
169 "{data=%#llx, obj=%p, res=%u, res2=0}, "
170 "{data=%#llx, obj=%p, res=%u, res2=0}"
171 "], {0, 123456789}) = %u\n",
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000172 *ctx, nr, nr + 1,
173 (unsigned long long) cb[0].aio_data, &cb[0], sizeof_data0,
174 (unsigned long long) cb[1].aio_data, &cb[1], sizeof_data1,
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000175 nr);
176
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000177 assert(syscall(__NR_io_cancel, *ctx, cbc, ev) == -1 && EINVAL == errno);
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000178 printf("io_cancel(%lu, {data=%#llx, pread, reqprio=99, fildes=-42}, %p) "
Dmitry V. Levin9ad44092016-01-06 11:27:15 +0000179 "= -1 EINVAL (%m)\n",
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000180 *ctx, (unsigned long long) cbc->aio_data, ev);
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000181
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000182 if (syscall(__NR_io_submit, *ctx, nr, cbvs) != (long) nr)
Dmitry V. Levin9ad44092016-01-06 11:27:15 +0000183 perror_msg_and_skip("io_submit");
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000184 printf("io_submit(%lu, %u, ["
185 "{data=%#llx, preadv, reqprio=%hd, fildes=0, "
186 "iovec=[{%p, %u}, {%p, %u}], offset=%lld}, "
187 "{data=%#llx, preadv, reqprio=%hd, fildes=0, "
188 "iovec=[{%p, %u}, {%p, %u}], offset=%lld}"
189 "]) = %u\n",
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000190 *ctx, nr,
Dmitry V. Levinf56046e2015-08-26 17:48:40 +0000191 (unsigned long long) cbv[0].aio_data, cbv[0].aio_reqprio,
192 iov0[0].iov_base, (unsigned int) iov0[0].iov_len,
193 iov0[1].iov_base, (unsigned int) iov0[1].iov_len,
194 (long long) cbv[0].aio_offset,
195 (unsigned long long) cbv[1].aio_data, cbv[1].aio_reqprio,
196 iov1[0].iov_base, (unsigned int) iov1[0].iov_len,
197 iov1[1].iov_base, (unsigned int) iov1[1].iov_len,
198 (long long) cbv[1].aio_offset,
199 nr);
200
Dmitry V. Levin68f7a662016-01-13 22:06:18 +0000201 assert(syscall(__NR_io_destroy, *ctx) == 0);
202 printf("io_destroy(%lu) = 0\n", *ctx);
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000203
204 puts("+++ exited with 0 +++");
205 return 0;
206}
207
208#else
209
Dmitry V. Levin9ad44092016-01-06 11:27:15 +0000210SKIP_MAIN_UNDEFINED("__NR_io_*")
Dmitry V. Levin13c21732015-08-26 12:49:07 +0000211
212#endif