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
|
/* 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
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)
{
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;
}
/* open direct descriptor */
sqe = io_uring_get_sqe(&sring);
io_uring_prep_openat_direct(sqe, AT_FDCWD, filename, 0, 0644, 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) {
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, 0, 0))
return T_EXIT_FAIL;
/* send direct descriptor to destination ring */
sqe = io_uring_get_sqe(&sring);
io_uring_prep_msg_ring(sqe, dring.ring_fd, 0, 0x89, 0);
sqe->addr = 1;
sqe->addr3 = 0;
sqe->file_index = 1;
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) {
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 != 0x89) {
fprintf(stderr, "bad user_data %ld\n", (long) cqe->res);
return T_EXIT_FAIL;
}
io_uring_cqe_seen(&dring, cqe);
/* now verify we can read the sane data from the destination ring */
if (verify_fixed_read(&dring, 0, 0))
return T_EXIT_FAIL;
/* close descriptor in source ring */
sqe = io_uring_get_sqe(&sring);
io_uring_prep_close_direct(sqe, 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) {
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, 0, 1))
return T_EXIT_FAIL;
/* check we can still read from destination ring */
if (verify_fixed_read(&dring, 0, 0))
return T_EXIT_FAIL;
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);
if (ret == T_EXIT_FAIL) {
fprintf(stderr, "test failed\n");
ret = T_EXIT_FAIL;
}
unlink(fname);
return ret;
}
|