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