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