1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Description: run various fixed file fd passing 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 "liburing.h"
14 #include "helpers.h"
15
16 #define FSIZE 128
17 #define PAT 0x9a
18 #define USER_DATA 0x89
19
20 static int no_fd_pass;
21
verify_fixed_read(struct io_uring * ring,int fixed_fd,int fail)22 static int verify_fixed_read(struct io_uring *ring, int fixed_fd, int fail)
23 {
24 struct io_uring_sqe *sqe;
25 struct io_uring_cqe *cqe;
26 unsigned char buf[FSIZE];
27 int i;
28
29 sqe = io_uring_get_sqe(ring);
30 io_uring_prep_read(sqe, fixed_fd, buf, FSIZE, 0);
31 sqe->flags |= IOSQE_FIXED_FILE;
32 io_uring_submit(ring);
33
34 io_uring_wait_cqe(ring, &cqe);
35 if (cqe->res != FSIZE) {
36 if (fail && cqe->res == -EBADF)
37 return 0;
38 fprintf(stderr, "Read: %d\n", cqe->res);
39 return 1;
40 }
41 io_uring_cqe_seen(ring, cqe);
42
43 for (i = 0; i < FSIZE; i++) {
44 if (buf[i] != PAT) {
45 fprintf(stderr, "got %x, wanted %x\n", buf[i], PAT);
46 return 1;
47 }
48 }
49
50 return 0;
51 }
52
test(const char * filename,int source_fd,int target_fd)53 static int test(const char *filename, int source_fd, int target_fd)
54 {
55 struct io_uring sring, dring;
56 struct io_uring_sqe *sqe;
57 struct io_uring_cqe *cqe;
58 int ret;
59
60 ret = io_uring_queue_init(8, &sring, 0);
61 if (ret) {
62 fprintf(stderr, "ring setup failed: %d\n", ret);
63 return T_EXIT_FAIL;
64 }
65 ret = io_uring_queue_init(8, &dring, 0);
66 if (ret) {
67 fprintf(stderr, "ring setup failed: %d\n", ret);
68 return T_EXIT_FAIL;
69 }
70
71 ret = io_uring_register_files_sparse(&sring, 8);
72 if (ret) {
73 if (ret == -EINVAL)
74 return T_EXIT_SKIP;
75 fprintf(stderr, "register files failed %d\n", ret);
76 return T_EXIT_FAIL;
77 }
78 ret = io_uring_register_files_sparse(&dring, 8);
79 if (ret) {
80 fprintf(stderr, "register files failed %d\n", ret);
81 return T_EXIT_FAIL;
82 }
83 if (target_fd == IORING_FILE_INDEX_ALLOC) {
84 /* we want to test installing into a non-zero slot */
85 ret = io_uring_register_file_alloc_range(&dring, 1, 1);
86 if (ret) {
87 fprintf(stderr, "io_uring_register_file_alloc_range %d\n", ret);
88 return T_EXIT_FAIL;
89 }
90 }
91
92 /* open direct descriptor */
93 sqe = io_uring_get_sqe(&sring);
94 io_uring_prep_openat_direct(sqe, AT_FDCWD, filename, 0, 0644, source_fd);
95 io_uring_submit(&sring);
96 ret = io_uring_wait_cqe(&sring, &cqe);
97 if (ret) {
98 fprintf(stderr, "wait cqe failed %d\n", ret);
99 return T_EXIT_FAIL;
100 }
101 if (cqe->res) {
102 fprintf(stderr, "cqe res %d\n", cqe->res);
103 return T_EXIT_FAIL;
104 }
105 io_uring_cqe_seen(&sring, cqe);
106
107 /* verify data is sane for source ring */
108 if (verify_fixed_read(&sring, source_fd, 0))
109 return T_EXIT_FAIL;
110
111 /* send direct descriptor to destination ring */
112 sqe = io_uring_get_sqe(&sring);
113 if (target_fd == IORING_FILE_INDEX_ALLOC) {
114 io_uring_prep_msg_ring_fd_alloc(sqe, dring.ring_fd, source_fd,
115 USER_DATA, 0);
116 } else {
117
118 io_uring_prep_msg_ring_fd(sqe, dring.ring_fd, source_fd,
119 target_fd, USER_DATA, 0);
120 }
121 io_uring_submit(&sring);
122
123 ret = io_uring_wait_cqe(&sring, &cqe);
124 if (ret) {
125 fprintf(stderr, "wait cqe failed %d\n", ret);
126 return T_EXIT_FAIL;
127 }
128 if (cqe->res < 0) {
129 if (cqe->res == -EINVAL && !no_fd_pass) {
130 no_fd_pass = 1;
131 return T_EXIT_SKIP;
132 }
133 fprintf(stderr, "msg_ring failed %d\n", cqe->res);
134 return T_EXIT_FAIL;
135 }
136 io_uring_cqe_seen(&sring, cqe);
137
138 /* get posted completion for the passing */
139 ret = io_uring_wait_cqe(&dring, &cqe);
140 if (ret) {
141 fprintf(stderr, "wait cqe failed %d\n", ret);
142 return T_EXIT_FAIL;
143 }
144 if (cqe->user_data != USER_DATA) {
145 fprintf(stderr, "bad user_data %ld\n", (long) cqe->res);
146 return T_EXIT_FAIL;
147 }
148 if (cqe->res < 0) {
149 fprintf(stderr, "bad result %i\n", cqe->res);
150 return T_EXIT_FAIL;
151 }
152 if (target_fd == IORING_FILE_INDEX_ALLOC) {
153 if (cqe->res != 1) {
154 fprintf(stderr, "invalid allocated index %i\n", cqe->res);
155 return T_EXIT_FAIL;
156 }
157 target_fd = cqe->res;
158 }
159 io_uring_cqe_seen(&dring, cqe);
160
161 /* now verify we can read the sane data from the destination ring */
162 if (verify_fixed_read(&dring, target_fd, 0))
163 return T_EXIT_FAIL;
164
165 /* close descriptor in source ring */
166 sqe = io_uring_get_sqe(&sring);
167 io_uring_prep_close_direct(sqe, source_fd);
168 io_uring_submit(&sring);
169
170 ret = io_uring_wait_cqe(&sring, &cqe);
171 if (ret) {
172 fprintf(stderr, "wait cqe failed %d\n", ret);
173 return T_EXIT_FAIL;
174 }
175 if (cqe->res) {
176 fprintf(stderr, "direct close failed %d\n", cqe->res);
177 return T_EXIT_FAIL;
178 }
179 io_uring_cqe_seen(&sring, cqe);
180
181 /* check that source ring fails after close */
182 if (verify_fixed_read(&sring, source_fd, 1))
183 return T_EXIT_FAIL;
184
185 /* check we can still read from destination ring */
186 if (verify_fixed_read(&dring, target_fd, 0))
187 return T_EXIT_FAIL;
188
189 io_uring_queue_exit(&sring);
190 io_uring_queue_exit(&dring);
191 return T_EXIT_PASS;
192 }
193
main(int argc,char * argv[])194 int main(int argc, char *argv[])
195 {
196 char fname[80];
197 int ret;
198
199 if (argc > 1)
200 return T_EXIT_SKIP;
201
202 sprintf(fname, ".fd-pass.%d", getpid());
203 t_create_file_pattern(fname, FSIZE, PAT);
204
205 ret = test(fname, 0, 1);
206 if (ret == T_EXIT_FAIL) {
207 fprintf(stderr, "test failed 0 1\n");
208 ret = T_EXIT_FAIL;
209 }
210
211 ret = test(fname, 0, 2);
212 if (ret == T_EXIT_FAIL) {
213 fprintf(stderr, "test failed 0 2\n");
214 ret = T_EXIT_FAIL;
215 }
216
217 ret = test(fname, 1, 1);
218 if (ret == T_EXIT_FAIL) {
219 fprintf(stderr, "test failed 1 1\n");
220 ret = T_EXIT_FAIL;
221 }
222
223 ret = test(fname, 1, 0);
224 if (ret == T_EXIT_FAIL) {
225 fprintf(stderr, "test failed 1 0\n");
226 ret = T_EXIT_FAIL;
227 }
228
229 ret = test(fname, 1, IORING_FILE_INDEX_ALLOC);
230 if (ret == T_EXIT_FAIL) {
231 fprintf(stderr, "test failed 1 ALLOC\n");
232 ret = T_EXIT_FAIL;
233 }
234
235 unlink(fname);
236 return ret;
237 }
238