blob: f0f122c234191e08ff3a9ce1a100a0fd89a1ee31 [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
Jens Axboea2141fc2020-05-19 17:36:19 -060070 if (argc > 1)
71 return 0;
72
Jens Axboecb1e01a2019-04-25 09:58:55 -060073 iov.iov_base = malloc(4096);
74 iov.iov_len = 4096;
75
76 ret = io_uring_queue_init(2, &ring, 0);
77 if (ret) {
78 printf("ring setup failed\n");
79 return 1;
80
81 }
82
83 sqe = io_uring_get_sqe(&ring);
84 if (!sqe) {
85 printf("get sqe failed\n");
86 return 1;
87 }
88
89 fd = get_file_fd();
90 if (fd < 0)
91 return 1;
92
93 io_uring_prep_readv(sqe, fd, &iov, 1, 0);
94 sqe->rw_flags = RWF_NOWAIT;
95
96 ret = io_uring_submit(&ring);
Jens Axboece8e2bc2019-04-30 14:50:36 -060097 if (ret != 1) {
98 printf("Got submit %d, expected 1\n", ret);
99 goto err;
100 }
101
102 ret = io_uring_peek_cqe(&ring, &cqe);
103 if (ret) {
104 printf("Ring peek got %d\n", ret);
105 goto err;
106 }
107
Jens Axboe9e1d69e2020-07-30 10:44:10 -0600108 if (cqe->res != -EAGAIN && cqe->res != 4096) {
Jens Axboece8e2bc2019-04-30 14:50:36 -0600109 printf("cqe error: %d\n", cqe->res);
Jens Axboecb1e01a2019-04-25 09:58:55 -0600110 goto err;
111 }
112
113 put_file_fd(fd);
114 return 0;
115err:
116 put_file_fd(fd);
117 return 1;
118}