1 /* SPDX-License-Identifier: MIT */
2 #include <errno.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <sys/types.h>
9
10 #include "helpers.h"
11 #include "liburing.h"
12
13 #define IOVECS_LEN 2
14
main(int argc,char * argv[])15 int main(int argc, char *argv[])
16 {
17 struct iovec iovecs[IOVECS_LEN];
18 struct io_uring ring;
19 int i, fd, ret;
20
21 if (argc > 1)
22 return 0;
23
24 fd = open("/dev/zero", O_RDONLY);
25 if (fd < 0) {
26 fprintf(stderr, "Failed to open /dev/zero\n");
27 return 1;
28 }
29
30 if (io_uring_queue_init(32, &ring, 0) < 0) {
31 fprintf(stderr, "Faild to init io_uring\n");
32 close(fd);
33 return 1;
34 }
35
36 for (i = 0; i < IOVECS_LEN; ++i) {
37 iovecs[i].iov_base = t_malloc(64);
38 iovecs[i].iov_len = 64;
39 };
40
41 ret = io_uring_register_buffers(&ring, iovecs, IOVECS_LEN);
42 if (ret) {
43 fprintf(stderr, "Failed to register buffers\n");
44 return 1;
45 }
46
47 for (i = 0; i < IOVECS_LEN; ++i) {
48 struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);
49 const char *str = "#include <errno.h>";
50
51 iovecs[i].iov_len = strlen(str);
52 io_uring_prep_read_fixed(sqe, fd, iovecs[i].iov_base, strlen(str), 0, i);
53 if (i == 0)
54 io_uring_sqe_set_flags(sqe, IOSQE_IO_LINK);
55 io_uring_sqe_set_data(sqe, (void *)str);
56 }
57
58 ret = io_uring_submit_and_wait(&ring, IOVECS_LEN);
59 if (ret < 0) {
60 fprintf(stderr, "Failed to submit IO\n");
61 return 1;
62 } else if (ret < 2) {
63 fprintf(stderr, "Submitted %d, wanted %d\n", ret, IOVECS_LEN);
64 return 1;
65 }
66
67 for (i = 0; i < IOVECS_LEN; i++) {
68 struct io_uring_cqe *cqe;
69
70 ret = io_uring_wait_cqe(&ring, &cqe);
71 if (ret) {
72 fprintf(stderr, "wait_cqe=%d\n", ret);
73 return 1;
74 }
75 if (cqe->res != iovecs[i].iov_len) {
76 fprintf(stderr, "read: wanted %ld, got %d\n",
77 (long) iovecs[i].iov_len, cqe->res);
78 return 1;
79 }
80 io_uring_cqe_seen(&ring, cqe);
81 }
82
83 close(fd);
84 io_uring_queue_exit(&ring);
85
86 for (i = 0; i < IOVECS_LEN; ++i)
87 free(iovecs[i].iov_base);
88
89 return 0;
90 }
91