| 1 | #define _GNU_SOURCE
|
|---|
| 2 |
|
|---|
| 3 | #include <fcntl.h>
|
|---|
| 4 | #include <liburing.h>
|
|---|
| 5 | #include <stdio.h>
|
|---|
| 6 | #include <string.h>
|
|---|
| 7 | #include <unistd.h>
|
|---|
| 8 |
|
|---|
| 9 | struct io_uring ring;
|
|---|
| 10 |
|
|---|
| 11 | int main(int argc, char * argv[]) {
|
|---|
| 12 | if(argc != 2) {
|
|---|
| 13 | printf("usage: %s FILE - prints file to console.\n", argv[0]);
|
|---|
| 14 | return 1;
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | int fd = open(argv[1], O_DIRECT);
|
|---|
| 18 | if(fd < 0) {
|
|---|
| 19 | printf("Could not open file %s.\n", argv[1]);
|
|---|
| 20 | return 2;
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | /* prep the array */
|
|---|
| 24 | char data[512];
|
|---|
| 25 | struct iovec iov = { data, 512 };
|
|---|
| 26 |
|
|---|
| 27 | /* init liburing */
|
|---|
| 28 | io_uring_queue_init(256, &ring, IORING_SETUP_IOPOLL);
|
|---|
| 29 |
|
|---|
| 30 | /* declare required structs */
|
|---|
| 31 | struct io_uring_sqe * sqe;
|
|---|
| 32 | struct io_uring_cqe * cqe;
|
|---|
| 33 |
|
|---|
| 34 | /* get an sqe and fill in a READV operation */
|
|---|
| 35 | sqe = io_uring_get_sqe(&ring);
|
|---|
| 36 | io_uring_prep_readv(sqe, fd, &iov, 1, 0);
|
|---|
| 37 |
|
|---|
| 38 | sqe->user_data = data;
|
|---|
| 39 |
|
|---|
| 40 | /* tell the kernel we have an sqe ready for consumption */
|
|---|
| 41 | io_uring_submit(&ring);
|
|---|
| 42 |
|
|---|
| 43 | /* wait for the sqe to complete */
|
|---|
| 44 | int ret = io_uring_wait_cqe(&ring, &cqe);
|
|---|
| 45 |
|
|---|
| 46 | /* read and process cqe event */
|
|---|
| 47 | if(ret == 0) {
|
|---|
| 48 | char * out = cqe->user_data;
|
|---|
| 49 | signed int len = cqe->res;
|
|---|
| 50 | io_uring_cqe_seen(&ring, cqe);
|
|---|
| 51 |
|
|---|
| 52 | if(len > 0) {
|
|---|
| 53 | printf("%.*s", len, out);
|
|---|
| 54 | }
|
|---|
| 55 | else if( len < 0 ) {
|
|---|
| 56 | fprintf(stderr, "readv returned error : %s\n", strerror(-len));
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 | else {
|
|---|
| 60 | printf("%d\n", ret);
|
|---|
| 61 | io_uring_cqe_seen(&ring, cqe);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | io_uring_queue_exit(&ring);
|
|---|
| 65 |
|
|---|
| 66 | close(fd);
|
|---|
| 67 | }
|
|---|