blob: 17018a23890e076fe8dd7684a486619b44151c2e [file] [log] [blame]
Jens Axboecb1e01a2019-04-25 09:58:55 -06001/*
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 Axboe60b33052019-05-01 10:07:37 -060014#define BLOCK 4096
15
Jens Axboecb1e01a2019-04-25 09:58:55 -060016static int get_file_fd(void)
17{
Jens Axboe60b33052019-05-01 10:07:37 -060018 ssize_t ret;
Jens Axboecb1e01a2019-04-25 09:58:55 -060019 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 Axboe60b33052019-05-01 10:07:37 -060028 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 Axboecb1e01a2019-04-25 09:58:55 -060037 fsync(fd);
38
39 if (posix_fadvise(fd, 0, 4096, POSIX_FADV_DONTNEED)) {
40 perror("fadvise");
Jens Axboe60b33052019-05-01 10:07:37 -060041err:
Jens Axboecb1e01a2019-04-25 09:58:55 -060042 close(fd);
43 free(buf);
44 return -1;
45 }
46
47 free(buf);
48 return fd;
49}
50
51static void put_file_fd(int fd)
52{
53 close(fd);
54 unlink("testfile");
55}
56
57int main(int argc, char *argv[])
58{
59 struct io_uring ring;
60 struct io_uring_sqe *sqe;
Jens Axboece8e2bc2019-04-30 14:50:36 -060061 struct io_uring_cqe *cqe;
Jens Axboecb1e01a2019-04-25 09:58:55 -060062 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 Axboece8e2bc2019-04-30 14:50:36 -060089 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 Axboecb1e01a2019-04-25 09:58:55 -0600102 goto err;
103 }
104
105 put_file_fd(fd);
106 return 0;
107err:
108 put_file_fd(fd);
109 return 1;
110}