blob: 65f81b12a5277d08ffaf6f8c5913be91c42131eb [file] [log] [blame]
Jens Axboee5024352020-02-11 20:34:12 -07001/* SPDX-License-Identifier: MIT */
Jens Axboe7772c812020-01-08 18:52:56 -07002/*
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 Liu97499812021-02-21 22:58:12 +080013#include "helpers.h"
Jens Axboe7772c812020-01-08 18:52:56 -070014#include "liburing.h"
15
Jens Axboee93e3412020-02-07 09:01:32 -070016static int test_openat2(struct io_uring *ring, const char *path, int dfd)
Jens Axboe7772c812020-01-08 18:52:56 -070017{
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 Axboee93e3412020-02-07 09:01:32 -070030 io_uring_prep_openat2(sqe, dfd, path, &how);
Jens Axboe7772c812020-01-08 18:52:56 -070031
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;
46err:
47 return -1;
48}
49
50int main(int argc, char *argv[])
51{
52 struct io_uring ring;
Jens Axboee93e3412020-02-07 09:01:32 -070053 const char *path, *path_rel;
Jens Axboe7772c812020-01-08 18:52:56 -070054 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 Axboee93e3412020-02-07 09:01:32 -070063 path = "/tmp/.open.close";
64 path_rel = argv[1];
Jens Axboe7772c812020-01-08 18:52:56 -070065 do_unlink = 0;
66 } else {
Jens Axboee93e3412020-02-07 09:01:32 -070067 path = "/tmp/.open.close";
68 path_rel = ".open.close";
Jens Axboe7772c812020-01-08 18:52:56 -070069 do_unlink = 1;
70 }
71
Jens Axboed84d9012021-02-24 07:12:47 -070072 t_create_file(path, 4096);
Zhiqiang Liufa7ba192021-02-23 19:15:35 +080073
74 if (do_unlink)
Jens Axboed84d9012021-02-24 07:12:47 -070075 t_create_file(path_rel, 4096);
Jens Axboe7772c812020-01-08 18:52:56 -070076
Jens Axboee93e3412020-02-07 09:01:32 -070077 ret = test_openat2(&ring, path, -1);
Jens Axboe7772c812020-01-08 18:52:56 -070078 if (ret < 0) {
79 if (ret == -EINVAL) {
80 fprintf(stdout, "openat2 not supported, skipping\n");
81 goto done;
82 }
Jens Axboee93e3412020-02-07 09:01:32 -070083 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 Axboe7772c812020-01-08 18:52:56 -070090 goto err;
91 }
92
93done:
Jens Axboee93e3412020-02-07 09:01:32 -070094 unlink(path);
Jens Axboe7772c812020-01-08 18:52:56 -070095 if (do_unlink)
Jens Axboee93e3412020-02-07 09:01:32 -070096 unlink(path_rel);
Jens Axboe7772c812020-01-08 18:52:56 -070097 return 0;
98err:
Jens Axboee93e3412020-02-07 09:01:32 -070099 unlink(path);
Jens Axboe7772c812020-01-08 18:52:56 -0700100 if (do_unlink)
Jens Axboee93e3412020-02-07 09:01:32 -0700101 unlink(path_rel);
Jens Axboe7772c812020-01-08 18:52:56 -0700102 return 1;
103}