diff options
author | Jens Axboe <axboe@kernel.dk> | 2020-03-03 16:50:05 -0700 |
---|---|---|
committer | Jens Axboe <axboe@kernel.dk> | 2020-03-04 08:54:54 -0700 |
commit | 92fb56243b1bca86fbd387007e9044a111728208 (patch) | |
tree | 1db75f4f5c3b0e3ed276f6a6d680ae34231ab1ca | |
parent | 044e585027d2094d27379872acb8d06b13175522 (diff) | |
download | liburing-92fb56243b1bca86fbd387007e9044a111728208.tar.gz liburing-92fb56243b1bca86fbd387007e9044a111728208.tar.bz2 |
Add sample open-read-close chain
Signed-off-by: Jens Axboe <axboe@kernel.dk>
-rwxr-xr-x | configure | 2 | ||||
-rw-r--r-- | test/Makefile | 5 | ||||
-rw-r--r-- | test/orc.c | 98 |
3 files changed, 103 insertions, 2 deletions
@@ -297,6 +297,8 @@ struct open_how { uint64_t flags; uint64_t mode; uint64_t resolve; + int32_t fd; + uint32_t pad; }; EOF diff --git a/test/Makefile b/test/Makefile index 8437f31..7bdba3c 100644 --- a/test/Makefile +++ b/test/Makefile @@ -20,7 +20,8 @@ all_targets += poll poll-cancel ring-leak fsync io_uring_setup io_uring_register connect 7ad0e4b2f83c-test submit-reuse fallocate open-close \ file-update statx accept-reuse poll-v-poll fadvise madvise \ short-read openat2 probe shared-wq personality eventfd \ - send_recv eventfd-ring across-fork sq-poll-kthread splice + send_recv eventfd-ring across-fork sq-poll-kthread splice \ + orc include ../Makefile.quiet @@ -48,7 +49,7 @@ test_srcs := poll.c poll-cancel.c ring-leak.c fsync.c io_uring_setup.c \ file-update.c statx.c accept-reuse.c poll-v-poll.c fadvise.c \ madvise.c short-read.c openat2.c probe.c shared-wq.c \ personality.c eventfd.c eventfd-ring.c across-fork.c sq-poll-kthread.c \ - splice.c + splice.c orc.c test_objs := $(patsubst %.c,%.ol,$(test_srcs)) diff --git a/test/orc.c b/test/orc.c new file mode 100644 index 0000000..eda5b7a --- /dev/null +++ b/test/orc.c @@ -0,0 +1,98 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Description: open+read+close link sequence with fd passing + * + */ +#include <errno.h> +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> +#include <fcntl.h> + +#include "liburing.h" + +#ifndef O_SPECIFIC_FD +#define O_SPECIFIC_FD 01000000000 +#endif + +#define USE_FD 89 + +static int test_orc(struct io_uring *ring, const char *fname) +{ + struct io_uring_cqe *cqe; + struct io_uring_sqe *sqe; + struct open_how how; + char buf[32]; + int ret, i; + int nr = 0; + + how.flags = O_RDONLY | O_SPECIFIC_FD; + how.mode = 0; + how.resolve = 0; + how.fd = USE_FD; + sqe = io_uring_get_sqe(ring); + io_uring_prep_openat2(sqe, AT_FDCWD, fname, &how); + sqe->flags |= IOSQE_IO_LINK; + sqe->user_data = IORING_OP_OPENAT2; + nr++; + + sqe = io_uring_get_sqe(ring); + io_uring_prep_read(sqe, USE_FD, buf, sizeof(buf), 0); + sqe->flags |= IOSQE_IO_LINK; + sqe->user_data = IORING_OP_READ; + nr++; + + sqe = io_uring_get_sqe(ring); + io_uring_prep_close(sqe, USE_FD); + sqe->user_data = IORING_OP_CLOSE; + nr++; + + ret = io_uring_submit(ring); + if (ret != nr) { + fprintf(stderr, "sqe submit failed: %d\n", ret); + goto err; + } + + for (i = 0; i < nr; i++) { + ret = io_uring_wait_cqe(ring, &cqe); + if (ret < 0) { + fprintf(stderr, "wait completion %d\n", ret); + goto err; + } + + printf("%d: op=%u, res=%d\n", i, (unsigned) cqe->user_data, cqe->res); + if (cqe->user_data == IORING_OP_OPENAT2 && cqe->res != USE_FD) + printf("openat2 got fd %d, wanted %d\n", cqe->res, USE_FD); + io_uring_cqe_seen(ring, cqe); + } + + return 0; +err: + return 1; +} + +int main(int argc, char *argv[]) +{ + struct io_uring ring; + int ret; + + if (argc < 2) { + fprintf(stderr, "%s: <file>\n", argv[0]); + return 1; + } + + ret = io_uring_queue_init(8, &ring, 0); + if (ret) { + fprintf(stderr, "ring setup failed: %d\n", ret); + return 1; + } + + ret = test_orc(&ring, argv[1]); + if (ret) { + fprintf(stderr, "test_orc failed\n"); + return ret; + } + + return 0; +} |