blob: 9083d3e448bcf267d809a3eb11816e6c15cb2f85 [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 Axboe7b989f32019-05-01 16:12:06 -060016#ifndef RWF_NOWAIT
17#define RWF_NOWAIT 8
18#endif
19
Jens Axboecb1e01a2019-04-25 09:58:55 -060020static int get_file_fd(void)
21{
Jens Axboe60b33052019-05-01 10:07:37 -060022 ssize_t ret;
Jens Axboecb1e01a2019-04-25 09:58:55 -060023 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 Axboe60b33052019-05-01 10:07:37 -060032 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 Axboecb1e01a2019-04-25 09:58:55 -060041 fsync(fd);
42
43 if (posix_fadvise(fd, 0, 4096, POSIX_FADV_DONTNEED)) {
44 perror("fadvise");
Jens Axboe60b33052019-05-01 10:07:37 -060045err:
Jens Axboecb1e01a2019-04-25 09:58:55 -060046 close(fd);
47 free(buf);
48 return -1;
49 }
50
51 free(buf);
52 return fd;
53}
54
55static void put_file_fd(int fd)
56{
57 close(fd);
58 unlink("testfile");
59}
60
61int main(int argc, char *argv[])
62{
63 struct io_uring ring;
64 struct io_uring_sqe *sqe;
Jens Axboece8e2bc2019-04-30 14:50:36 -060065 struct io_uring_cqe *cqe;
Jens Axboecb1e01a2019-04-25 09:58:55 -060066 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 Axboece8e2bc2019-04-30 14:50:36 -060093 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 Axboecb1e01a2019-04-25 09:58:55 -0600106 goto err;
107 }
108
109 put_file_fd(fd);
110 return 0;
111err:
112 put_file_fd(fd);
113 return 1;
114}