Jens Axboe | cb1e01a | 2019-04-25 09:58:55 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Description: -EAGAIN handling |
| 3 | * |
| 4 | */ |
| 5 | #include <errno.h> |
| 6 | #include <stdio.h> |
| 7 | #include <unistd.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <fcntl.h> |
| 11 | |
Stefan Hajnoczi | c31c7ec | 2019-07-24 09:24:50 +0100 | [diff] [blame^] | 12 | #include "liburing.h" |
Jens Axboe | cb1e01a | 2019-04-25 09:58:55 -0600 | [diff] [blame] | 13 | |
Jens Axboe | 60b3305 | 2019-05-01 10:07:37 -0600 | [diff] [blame] | 14 | #define BLOCK 4096 |
| 15 | |
Jens Axboe | 7b989f3 | 2019-05-01 16:12:06 -0600 | [diff] [blame] | 16 | #ifndef RWF_NOWAIT |
| 17 | #define RWF_NOWAIT 8 |
| 18 | #endif |
| 19 | |
Jens Axboe | cb1e01a | 2019-04-25 09:58:55 -0600 | [diff] [blame] | 20 | static int get_file_fd(void) |
| 21 | { |
Jens Axboe | 60b3305 | 2019-05-01 10:07:37 -0600 | [diff] [blame] | 22 | ssize_t ret; |
Jens Axboe | cb1e01a | 2019-04-25 09:58:55 -0600 | [diff] [blame] | 23 | char *buf; |
| 24 | int fd; |
| 25 | |
| 26 | fd = open("testfile", O_RDWR | O_CREAT, 0644); |
| 27 | if (fd < 0) { |
| 28 | perror("open file"); |
| 29 | return -1; |
| 30 | } |
| 31 | |
Jens Axboe | 60b3305 | 2019-05-01 10:07:37 -0600 | [diff] [blame] | 32 | buf = malloc(BLOCK); |
| 33 | ret = write(fd, buf, BLOCK); |
| 34 | if (ret != BLOCK) { |
| 35 | if (ret < 0) |
| 36 | perror("write"); |
| 37 | else |
| 38 | printf("Short write\n"); |
| 39 | goto err; |
| 40 | } |
Jens Axboe | cb1e01a | 2019-04-25 09:58:55 -0600 | [diff] [blame] | 41 | fsync(fd); |
| 42 | |
| 43 | if (posix_fadvise(fd, 0, 4096, POSIX_FADV_DONTNEED)) { |
| 44 | perror("fadvise"); |
Jens Axboe | 60b3305 | 2019-05-01 10:07:37 -0600 | [diff] [blame] | 45 | err: |
Jens Axboe | cb1e01a | 2019-04-25 09:58:55 -0600 | [diff] [blame] | 46 | close(fd); |
| 47 | free(buf); |
| 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | free(buf); |
| 52 | return fd; |
| 53 | } |
| 54 | |
| 55 | static void put_file_fd(int fd) |
| 56 | { |
| 57 | close(fd); |
| 58 | unlink("testfile"); |
| 59 | } |
| 60 | |
| 61 | int main(int argc, char *argv[]) |
| 62 | { |
| 63 | struct io_uring ring; |
| 64 | struct io_uring_sqe *sqe; |
Jens Axboe | ce8e2bc | 2019-04-30 14:50:36 -0600 | [diff] [blame] | 65 | struct io_uring_cqe *cqe; |
Jens Axboe | cb1e01a | 2019-04-25 09:58:55 -0600 | [diff] [blame] | 66 | struct iovec iov; |
| 67 | int ret, fd; |
| 68 | |
| 69 | iov.iov_base = malloc(4096); |
| 70 | iov.iov_len = 4096; |
| 71 | |
| 72 | ret = io_uring_queue_init(2, &ring, 0); |
| 73 | if (ret) { |
| 74 | printf("ring setup failed\n"); |
| 75 | return 1; |
| 76 | |
| 77 | } |
| 78 | |
| 79 | sqe = io_uring_get_sqe(&ring); |
| 80 | if (!sqe) { |
| 81 | printf("get sqe failed\n"); |
| 82 | return 1; |
| 83 | } |
| 84 | |
| 85 | fd = get_file_fd(); |
| 86 | if (fd < 0) |
| 87 | return 1; |
| 88 | |
| 89 | io_uring_prep_readv(sqe, fd, &iov, 1, 0); |
| 90 | sqe->rw_flags = RWF_NOWAIT; |
| 91 | |
| 92 | ret = io_uring_submit(&ring); |
Jens Axboe | ce8e2bc | 2019-04-30 14:50:36 -0600 | [diff] [blame] | 93 | if (ret != 1) { |
| 94 | printf("Got submit %d, expected 1\n", ret); |
| 95 | goto err; |
| 96 | } |
| 97 | |
| 98 | ret = io_uring_peek_cqe(&ring, &cqe); |
| 99 | if (ret) { |
| 100 | printf("Ring peek got %d\n", ret); |
| 101 | goto err; |
| 102 | } |
| 103 | |
| 104 | if (cqe->res != -EAGAIN) { |
| 105 | printf("cqe error: %d\n", cqe->res); |
Jens Axboe | cb1e01a | 2019-04-25 09:58:55 -0600 | [diff] [blame] | 106 | goto err; |
| 107 | } |
| 108 | |
| 109 | put_file_fd(fd); |
| 110 | return 0; |
| 111 | err: |
| 112 | put_file_fd(fd); |
| 113 | return 1; |
| 114 | } |