1 /* SPDX-License-Identifier: MIT */
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
13 #include "helpers.h"
14 #include "liburing.h"
15
test_openat2(struct io_uring * ring,const char * path,int dfd)16 static int test_openat2(struct io_uring *ring, const char *path, int dfd)
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;
30 io_uring_prep_openat2(sqe, dfd, path, &how);
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
main(int argc,char * argv[])50 int main(int argc, char *argv[])
51 {
52 struct io_uring ring;
53 const char *path, *path_rel;
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) {
63 path = "/tmp/.open.close";
64 path_rel = argv[1];
65 do_unlink = 0;
66 } else {
67 path = "/tmp/.open.close";
68 path_rel = ".open.close";
69 do_unlink = 1;
70 }
71
72 t_create_file(path, 4096);
73
74 if (do_unlink)
75 t_create_file(path_rel, 4096);
76
77 ret = test_openat2(&ring, path, -1);
78 if (ret < 0) {
79 if (ret == -EINVAL) {
80 fprintf(stdout, "openat2 not supported, skipping\n");
81 goto done;
82 }
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);
90 goto err;
91 }
92
93 done:
94 unlink(path);
95 if (do_unlink)
96 unlink(path_rel);
97 return 0;
98 err:
99 unlink(path);
100 if (do_unlink)
101 unlink(path_rel);
102 return 1;
103 }
104