1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Simple app that demonstrates how to setup an io_uring interface, and use it
4 * via a registered ring fd, without leaving the original fd open.
5 *
6 * gcc -Wall -O2 -D_GNU_SOURCE -o io_uring-close-test io_uring-close-test.c -luring
7 */
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include "liburing.h"
16
17 #define QD 4
18
main(int argc,char * argv[])19 int main(int argc, char *argv[])
20 {
21 struct io_uring ring;
22 int i, fd, ret, pending, done;
23 struct io_uring_sqe *sqe;
24 struct io_uring_cqe *cqe;
25 struct iovec *iovecs;
26 struct stat sb;
27 ssize_t fsize;
28 off_t offset;
29 void *buf;
30
31 if (argc < 2) {
32 printf("%s: file\n", argv[0]);
33 return 1;
34 }
35
36 ret = io_uring_queue_init(QD, &ring, 0);
37 if (ret < 0) {
38 fprintf(stderr, "queue_init: %s\n", strerror(-ret));
39 return 1;
40 }
41
42 ret = io_uring_register_ring_fd(&ring);
43 if (ret < 0) {
44 fprintf(stderr, "register_ring_fd: %s\n", strerror(-ret));
45 return 1;
46 }
47 ret = io_uring_close_ring_fd(&ring);
48 if (ret < 0) {
49 fprintf(stderr, "close_ring_fd: %s\n", strerror(-ret));
50 return 1;
51 }
52
53 fd = open(argv[1], O_RDONLY);
54 if (fd < 0) {
55 perror("open");
56 return 1;
57 }
58
59 if (fstat(fd, &sb) < 0) {
60 perror("fstat");
61 return 1;
62 }
63
64 fsize = 0;
65 iovecs = calloc(QD, sizeof(struct iovec));
66 for (i = 0; i < QD; i++) {
67 if (posix_memalign(&buf, 4096, 4096))
68 return 1;
69 iovecs[i].iov_base = buf;
70 iovecs[i].iov_len = 4096;
71 fsize += 4096;
72 }
73
74 offset = 0;
75 i = 0;
76 do {
77 sqe = io_uring_get_sqe(&ring);
78 if (!sqe)
79 break;
80 io_uring_prep_readv(sqe, fd, &iovecs[i], 1, offset);
81 offset += iovecs[i].iov_len;
82 i++;
83 if (offset > sb.st_size)
84 break;
85 } while (1);
86
87 ret = io_uring_submit(&ring);
88 if (ret < 0) {
89 fprintf(stderr, "io_uring_submit: %s\n", strerror(-ret));
90 return 1;
91 } else if (ret != i) {
92 fprintf(stderr, "io_uring_submit submitted less %d\n", ret);
93 return 1;
94 }
95
96 done = 0;
97 pending = ret;
98 fsize = 0;
99 for (i = 0; i < pending; i++) {
100 ret = io_uring_wait_cqe(&ring, &cqe);
101 if (ret < 0) {
102 fprintf(stderr, "io_uring_wait_cqe: %s\n", strerror(-ret));
103 return 1;
104 }
105
106 done++;
107 ret = 0;
108 if (cqe->res != 4096 && cqe->res + fsize != sb.st_size) {
109 fprintf(stderr, "ret=%d, wanted 4096\n", cqe->res);
110 ret = 1;
111 }
112 fsize += cqe->res;
113 io_uring_cqe_seen(&ring, cqe);
114 if (ret)
115 break;
116 }
117
118 printf("Submitted=%d, completed=%d, bytes=%lu\n", pending, done,
119 (unsigned long) fsize);
120 close(fd);
121 io_uring_queue_exit(&ring);
122 return 0;
123 }
124