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