blob: dd0d94774a11125b5c900a3c8621a5b0a71223d4 [file] [log] [blame]
Jens Axboee5024352020-02-11 20:34:12 -07001/* SPDX-License-Identifier: MIT */
Jens Axboecb1e01a2019-04-25 09:58:55 -06002/*
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 Hajnoczic31c7ec2019-07-24 09:24:50 +010013#include "liburing.h"
Jens Axboecb1e01a2019-04-25 09:58:55 -060014
Jens Axboe60b33052019-05-01 10:07:37 -060015#define BLOCK 4096
16
Jens Axboe7b989f32019-05-01 16:12:06 -060017#ifndef RWF_NOWAIT
18#define RWF_NOWAIT 8
19#endif
20
Jens Axboecb1e01a2019-04-25 09:58:55 -060021static int get_file_fd(void)
22{
Jens Axboe60b33052019-05-01 10:07:37 -060023 ssize_t ret;
Jens Axboecb1e01a2019-04-25 09:58:55 -060024 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 Axboe60b33052019-05-01 10:07:37 -060033 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 Axboecb1e01a2019-04-25 09:58:55 -060042 fsync(fd);
43
44 if (posix_fadvise(fd, 0, 4096, POSIX_FADV_DONTNEED)) {
45 perror("fadvise");
Jens Axboe60b33052019-05-01 10:07:37 -060046err:
Jens Axboecb1e01a2019-04-25 09:58:55 -060047 close(fd);
48 free(buf);
49 return -1;
50 }
51
52 free(buf);
53 return fd;
54}
55
56static void put_file_fd(int fd)
57{
58 close(fd);
59 unlink("testfile");
60}
61
62int main(int argc, char *argv[])
63{
64 struct io_uring ring;
65 struct io_uring_sqe *sqe;
Jens Axboece8e2bc2019-04-30 14:50:36 -060066 struct io_uring_cqe *cqe;
Jens Axboecb1e01a2019-04-25 09:58:55 -060067 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 Axboece8e2bc2019-04-30 14:50:36 -060094 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 Axboecb1e01a2019-04-25 09:58:55 -0600107 goto err;
108 }
109
110 put_file_fd(fd);
111 return 0;
112err:
113 put_file_fd(fd);
114 return 1;
115}