Jens Axboe | e502435 | 2020-02-11 20:34:12 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: MIT */ |
Jens Axboe | 7772c81 | 2020-01-08 18:52:56 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Description: run various openat(2) tests |
| 4 | * |
| 5 | */ |
| 6 | #include <errno.h> |
| 7 | #include <stdio.h> |
| 8 | #include <unistd.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include <fcntl.h> |
| 12 | |
Zhiqiang Liu | 9749981 | 2021-02-21 22:58:12 +0800 | [diff] [blame] | 13 | #include "helpers.h" |
Jens Axboe | 7772c81 | 2020-01-08 18:52:56 -0700 | [diff] [blame] | 14 | #include "liburing.h" |
| 15 | |
Jens Axboe | e93e341 | 2020-02-07 09:01:32 -0700 | [diff] [blame] | 16 | static int test_openat2(struct io_uring *ring, const char *path, int dfd) |
Jens Axboe | 7772c81 | 2020-01-08 18:52:56 -0700 | [diff] [blame] | 17 | { |
| 18 | struct io_uring_cqe *cqe; |
| 19 | struct io_uring_sqe *sqe; |
| 20 | struct open_how how; |
| 21 | int ret; |
| 22 | |
| 23 | sqe = io_uring_get_sqe(ring); |
| 24 | if (!sqe) { |
| 25 | fprintf(stderr, "get sqe failed\n"); |
| 26 | goto err; |
| 27 | } |
| 28 | memset(&how, 0, sizeof(how)); |
| 29 | how.flags = O_RDONLY; |
Jens Axboe | e93e341 | 2020-02-07 09:01:32 -0700 | [diff] [blame] | 30 | io_uring_prep_openat2(sqe, dfd, path, &how); |
Jens Axboe | 7772c81 | 2020-01-08 18:52:56 -0700 | [diff] [blame] | 31 | |
| 32 | ret = io_uring_submit(ring); |
| 33 | if (ret <= 0) { |
| 34 | fprintf(stderr, "sqe submit failed: %d\n", ret); |
| 35 | goto err; |
| 36 | } |
| 37 | |
| 38 | ret = io_uring_wait_cqe(ring, &cqe); |
| 39 | if (ret < 0) { |
| 40 | fprintf(stderr, "wait completion %d\n", ret); |
| 41 | goto err; |
| 42 | } |
| 43 | ret = cqe->res; |
| 44 | io_uring_cqe_seen(ring, cqe); |
| 45 | return ret; |
| 46 | err: |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | int main(int argc, char *argv[]) |
| 51 | { |
| 52 | struct io_uring ring; |
Jens Axboe | e93e341 | 2020-02-07 09:01:32 -0700 | [diff] [blame] | 53 | const char *path, *path_rel; |
Jens Axboe | 7772c81 | 2020-01-08 18:52:56 -0700 | [diff] [blame] | 54 | int ret, do_unlink; |
| 55 | |
| 56 | ret = io_uring_queue_init(8, &ring, 0); |
| 57 | if (ret) { |
| 58 | fprintf(stderr, "ring setup failed\n"); |
| 59 | return 1; |
| 60 | } |
| 61 | |
| 62 | if (argc > 1) { |
Jens Axboe | e93e341 | 2020-02-07 09:01:32 -0700 | [diff] [blame] | 63 | path = "/tmp/.open.close"; |
| 64 | path_rel = argv[1]; |
Jens Axboe | 7772c81 | 2020-01-08 18:52:56 -0700 | [diff] [blame] | 65 | do_unlink = 0; |
| 66 | } else { |
Jens Axboe | e93e341 | 2020-02-07 09:01:32 -0700 | [diff] [blame] | 67 | path = "/tmp/.open.close"; |
| 68 | path_rel = ".open.close"; |
Jens Axboe | 7772c81 | 2020-01-08 18:52:56 -0700 | [diff] [blame] | 69 | do_unlink = 1; |
| 70 | } |
| 71 | |
Jens Axboe | d84d901 | 2021-02-24 07:12:47 -0700 | [diff] [blame] | 72 | t_create_file(path, 4096); |
Zhiqiang Liu | fa7ba19 | 2021-02-23 19:15:35 +0800 | [diff] [blame] | 73 | |
| 74 | if (do_unlink) |
Jens Axboe | d84d901 | 2021-02-24 07:12:47 -0700 | [diff] [blame] | 75 | t_create_file(path_rel, 4096); |
Jens Axboe | 7772c81 | 2020-01-08 18:52:56 -0700 | [diff] [blame] | 76 | |
Jens Axboe | e93e341 | 2020-02-07 09:01:32 -0700 | [diff] [blame] | 77 | ret = test_openat2(&ring, path, -1); |
Jens Axboe | 7772c81 | 2020-01-08 18:52:56 -0700 | [diff] [blame] | 78 | if (ret < 0) { |
| 79 | if (ret == -EINVAL) { |
| 80 | fprintf(stdout, "openat2 not supported, skipping\n"); |
| 81 | goto done; |
| 82 | } |
Jens Axboe | e93e341 | 2020-02-07 09:01:32 -0700 | [diff] [blame] | 83 | fprintf(stderr, "test_openat2 absolute failed: %d\n", ret); |
| 84 | goto err; |
| 85 | } |
| 86 | |
| 87 | ret = test_openat2(&ring, path_rel, AT_FDCWD); |
| 88 | if (ret < 0) { |
| 89 | fprintf(stderr, "test_openat2 relative failed: %d\n", ret); |
Jens Axboe | 7772c81 | 2020-01-08 18:52:56 -0700 | [diff] [blame] | 90 | goto err; |
| 91 | } |
| 92 | |
| 93 | done: |
Jens Axboe | e93e341 | 2020-02-07 09:01:32 -0700 | [diff] [blame] | 94 | unlink(path); |
Jens Axboe | 7772c81 | 2020-01-08 18:52:56 -0700 | [diff] [blame] | 95 | if (do_unlink) |
Jens Axboe | e93e341 | 2020-02-07 09:01:32 -0700 | [diff] [blame] | 96 | unlink(path_rel); |
Jens Axboe | 7772c81 | 2020-01-08 18:52:56 -0700 | [diff] [blame] | 97 | return 0; |
| 98 | err: |
Jens Axboe | e93e341 | 2020-02-07 09:01:32 -0700 | [diff] [blame] | 99 | unlink(path); |
Jens Axboe | 7772c81 | 2020-01-08 18:52:56 -0700 | [diff] [blame] | 100 | if (do_unlink) |
Jens Axboe | e93e341 | 2020-02-07 09:01:32 -0700 | [diff] [blame] | 101 | unlink(path_rel); |
Jens Axboe | 7772c81 | 2020-01-08 18:52:56 -0700 | [diff] [blame] | 102 | return 1; |
| 103 | } |