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