[a86b1b85] | 1 | #define _GNU_SOURCE |
---|
| 2 | |
---|
[3a3b2b9] | 3 | #include <fcntl.h> |
---|
| 4 | #include <liburing.h> |
---|
| 5 | #include <stdio.h> |
---|
[a86b1b85] | 6 | #include <string.h> |
---|
[3a3b2b9] | 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 | |
---|
[a86b1b85] | 17 | int fd = open(argv[1], O_DIRECT); |
---|
[3a3b2b9] | 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 */ |
---|
[a86b1b85] | 28 | io_uring_queue_init(256, &ring, IORING_SETUP_IOPOLL); |
---|
[3a3b2b9] | 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 | |
---|
[f100a83] | 38 | sqe->user_data = data; |
---|
| 39 | |
---|
[3a3b2b9] | 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); |
---|
[a86b1b85] | 45 | |
---|
[3a3b2b9] | 46 | /* read and process cqe event */ |
---|
[f100a83] | 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 | } |
---|
[a86b1b85] | 55 | else if( len < 0 ) { |
---|
| 56 | fprintf(stderr, "readv returned error : %s\n", strerror(-len)); |
---|
| 57 | } |
---|
[f100a83] | 58 | } |
---|
| 59 | else { |
---|
| 60 | printf("%d\n", ret); |
---|
| 61 | io_uring_cqe_seen(&ring, cqe); |
---|
| 62 | } |
---|
[3a3b2b9] | 63 | |
---|
| 64 | io_uring_queue_exit(&ring); |
---|
| 65 | |
---|
| 66 | close(fd); |
---|
| 67 | } |
---|