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 #include <limits.h>
13
14 #include "helpers.h"
15 #include "liburing.h"
16
17 #define FDS 800
18
19 static int no_direct_pick;
20
submit_wait(struct io_uring * ring)21 static int submit_wait(struct io_uring *ring)
22 {
23 struct io_uring_cqe *cqe;
24 int ret;
25
26 ret = io_uring_submit(ring);
27 if (ret <= 0) {
28 fprintf(stderr, "sqe submit failed: %d\n", ret);
29 return 1;
30 }
31 ret = io_uring_wait_cqe(ring, &cqe);
32 if (ret < 0) {
33 fprintf(stderr, "wait completion %d\n", ret);
34 return 1;
35 }
36
37 ret = cqe->res;
38 io_uring_cqe_seen(ring, cqe);
39 return ret;
40 }
41
try_close(struct io_uring * ring,int slot)42 static inline int try_close(struct io_uring *ring, int slot)
43 {
44 struct io_uring_sqe *sqe;
45
46 sqe = io_uring_get_sqe(ring);
47 io_uring_prep_close_direct(sqe, slot);
48 return submit_wait(ring);
49 }
50
do_opens(struct io_uring * ring,const char * path,int nr,int expect_enfile)51 static int do_opens(struct io_uring *ring, const char *path, int nr,
52 int expect_enfile)
53 {
54 struct io_uring_cqe *cqe;
55 struct io_uring_sqe *sqe;
56 int i, ret;
57
58 for (i = 0; i < nr; i++) {
59 sqe = io_uring_get_sqe(ring);
60 if (!sqe) {
61 fprintf(stderr, "get sqe failed\n");
62 goto err;
63 }
64 io_uring_prep_openat_direct(sqe, -1, path, O_RDONLY, 0, 0);
65 sqe->file_index = UINT_MAX;
66
67 ret = io_uring_submit(ring);
68 if (ret <= 0) {
69 fprintf(stderr, "sqe submit failed: %d\n", ret);
70 goto err;
71 }
72 }
73
74 for (i = 0; i < nr; i++) {
75 ret = io_uring_wait_cqe(ring, &cqe);
76 if (ret < 0) {
77 fprintf(stderr, "wait completion %d\n", ret);
78 goto err;
79 }
80 ret = cqe->res;
81 if (ret < 0) {
82 if (!expect_enfile || ret != -ENFILE) {
83 printf("open=%d, %d\n", cqe->res, i);
84 goto err;
85 }
86 if (!i && ret == -EINVAL) {
87 no_direct_pick = 1;
88 return 0;
89 }
90 }
91 io_uring_cqe_seen(ring, cqe);
92 }
93 return 0;
94 err:
95 return 1;
96 }
97
test_openat(struct io_uring * ring,const char * path)98 static int test_openat(struct io_uring *ring, const char *path)
99 {
100 int ret, i;
101
102 /* open all */
103 ret = do_opens(ring, path, FDS, 0);
104 if (ret)
105 goto err;
106 if (no_direct_pick)
107 return 0;
108
109 /* now close 100 randomly */
110 for (i = 0; i < 100; i++) {
111 do {
112 int slot = rand() % FDS;
113 ret = try_close(ring, slot);
114 if (ret == -EBADF)
115 continue;
116 break;
117 } while (1);
118 }
119
120 /* opening 100 should work, we closed 100 */
121 ret = do_opens(ring, path, 100, 0);
122 if (ret)
123 goto err;
124
125 /* we should be full now, expect -ENFILE */
126 ret = do_opens(ring, path, 1, 1);
127 if (ret)
128 goto err;
129
130 return ret;
131 err:
132 fprintf(stderr,"%s: err=%d\n", __FUNCTION__, ret);
133 return -1;
134 }
135
main(int argc,char * argv[])136 int main(int argc, char *argv[])
137 {
138 struct io_uring ring;
139 const char *path;
140 int ret;
141
142 if (argc > 1)
143 return 0;
144
145 ret = io_uring_queue_init(8, &ring, 0);
146 if (ret) {
147 fprintf(stderr, "ring setup failed\n");
148 return 1;
149 }
150
151 ret = io_uring_register_files_sparse(&ring, FDS);
152 if (ret ) {
153 if (ret != -EINVAL) {
154 fprintf(stderr, "Sparse file registration failed\n");
155 return 1;
156 }
157 /* skip, kernel doesn't support sparse file array */
158 return 0;
159 }
160
161 path = "/tmp/.open.close";
162 t_create_file(path, 4096);
163
164 ret = test_openat(&ring, path);
165 if (ret < 0) {
166 if (ret == -EINVAL) {
167 fprintf(stdout, "Open not supported, skipping\n");
168 goto done;
169 }
170 fprintf(stderr, "test_openat absolute failed: %d\n", ret);
171 goto err;
172 }
173
174 done:
175 unlink(path);
176 return 0;
177 err:
178 unlink(path);
179 return 1;
180 }
181