Jens Axboe | 65c90e0 | 2020-06-09 19:50:30 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: MIT |
| 2 | |
| 3 | #define _GNU_SOURCE 1 |
| 4 | #define _FILE_OFFSET_BITS 64 |
| 5 | |
| 6 | // Test program for io_uring IORING_OP_CLOSE with O_PATH file. |
| 7 | // Author: Clayton Harris <bugs@claycon.org>, 2020-06-07 |
| 8 | |
| 9 | // linux 5.6.14-300.fc32.x86_64 |
| 10 | // gcc 10.1.1-1.fc32 |
| 11 | // liburing.x86_64 0.5-1.fc32 |
| 12 | |
| 13 | // gcc -O2 -Wall -Wextra -std=c11 -o close_opath close_opath.c -luring |
| 14 | // ./close_opath testfilepath |
| 15 | |
| 16 | #include <errno.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <liburing.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | typedef struct |
| 25 | { |
| 26 | const char *const flnames; |
| 27 | const int oflags; |
| 28 | } oflgs_t; |
| 29 | |
| 30 | static int test_io_uring_close(struct io_uring *ring, int fd) |
| 31 | { |
| 32 | struct io_uring_sqe *sqe; |
| 33 | struct io_uring_cqe *cqe; |
| 34 | int ret; |
| 35 | |
| 36 | sqe = io_uring_get_sqe(ring); |
| 37 | if (!sqe) { |
| 38 | fprintf(stderr, "io_uring_get_sqe() failed\n"); |
| 39 | return -ENOENT; |
| 40 | } |
| 41 | |
| 42 | io_uring_prep_close(sqe, fd); |
| 43 | |
| 44 | ret = io_uring_submit(ring); |
| 45 | if (ret < 0) { |
| 46 | fprintf(stderr, "io_uring_submit() failed, errno %d: %s\n", |
| 47 | -ret, strerror(-ret)); |
| 48 | return ret; |
| 49 | } |
| 50 | |
| 51 | ret = io_uring_wait_cqe(ring, &cqe); |
| 52 | if (ret < 0) { |
| 53 | fprintf(stderr, "io_uring_wait_cqe() failed, errno %d: %s\n", |
| 54 | -ret, strerror(-ret)); |
| 55 | return ret; |
| 56 | } |
| 57 | |
| 58 | ret = cqe->res; |
| 59 | io_uring_cqe_seen(ring, cqe); |
| 60 | |
Jens Axboe | b8547fc | 2020-10-07 09:11:41 -0600 | [diff] [blame] | 61 | if (ret < 0 && ret != -EOPNOTSUPP && ret != -EINVAL && ret != -EBADF) { |
Jens Axboe | 65c90e0 | 2020-06-09 19:50:30 -0600 | [diff] [blame] | 62 | fprintf(stderr, "io_uring close() failed, errno %d: %s\n", |
| 63 | -ret, strerror(-ret)); |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int open_file(const char *path, const oflgs_t *oflgs) |
| 71 | { |
| 72 | int fd; |
| 73 | |
| 74 | fd = openat(AT_FDCWD, path, oflgs->oflags, 0); |
| 75 | if (fd < 0) { |
| 76 | int err = errno; |
| 77 | fprintf(stderr, "openat(%s, %s) failed, errno %d: %s\n", |
| 78 | path, oflgs->flnames, err, strerror(err)); |
| 79 | return -err; |
| 80 | } |
| 81 | |
| 82 | return fd; |
| 83 | } |
| 84 | |
| 85 | int main(int argc, char *argv[]) |
| 86 | { |
| 87 | const char *fname = "."; |
| 88 | struct io_uring ring; |
| 89 | int ret, i; |
| 90 | static const oflgs_t oflgs[] = { |
| 91 | { "O_RDONLY", O_RDONLY }, |
| 92 | { "O_PATH", O_PATH } |
| 93 | }; |
| 94 | |
| 95 | ret = io_uring_queue_init(2, &ring, 0); |
| 96 | if (ret < 0) { |
| 97 | fprintf(stderr, "io_uring_queue_init() failed, errno %d: %s\n", |
| 98 | -ret, strerror(-ret)); |
| 99 | return 0x02; |
| 100 | } |
| 101 | |
| 102 | #define OFLGS_SIZE (sizeof(oflgs) / sizeof(oflgs[0])) |
| 103 | |
| 104 | ret = 0; |
| 105 | for (i = 0; i < OFLGS_SIZE; i++) { |
| 106 | int fd; |
| 107 | |
| 108 | fd = open_file(fname, &oflgs[i]); |
| 109 | if (fd < 0) { |
| 110 | ret |= 0x02; |
| 111 | break; |
| 112 | } |
| 113 | |
| 114 | /* Should always succeed */ |
| 115 | if (test_io_uring_close(&ring, fd) < 0) |
| 116 | ret |= 0x04 << i; |
| 117 | } |
| 118 | #undef OFLGS_SIZE |
| 119 | |
| 120 | io_uring_queue_exit(&ring); |
| 121 | return ret; |
| 122 | } |