blob: b6e27ccdfdda708d174434030d7e7150698818e1 [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
Zhiqiang Liu97499812021-02-21 22:58:12 +080013#include "helpers.h"
Stefan Hajnoczic31c7ec2019-07-24 09:24:50 +010014#include "liburing.h"
Jens Axboecb1e01a2019-04-25 09:58:55 -060015
Jens Axboe60b33052019-05-01 10:07:37 -060016#define BLOCK 4096
17
Jens Axboe7b989f32019-05-01 16:12:06 -060018#ifndef RWF_NOWAIT
19#define RWF_NOWAIT 8
20#endif
21
Jens Axboecb1e01a2019-04-25 09:58:55 -060022static int get_file_fd(void)
23{
Jens Axboe60b33052019-05-01 10:07:37 -060024 ssize_t ret;
Jens Axboecb1e01a2019-04-25 09:58:55 -060025 char *buf;
26 int fd;
27
28 fd = open("testfile", O_RDWR | O_CREAT, 0644);
29 if (fd < 0) {
30 perror("open file");
31 return -1;
32 }
33
Jens Axboed84d9012021-02-24 07:12:47 -070034 buf = t_malloc(BLOCK);
Jens Axboe60b33052019-05-01 10:07:37 -060035 ret = write(fd, buf, BLOCK);
36 if (ret != BLOCK) {
37 if (ret < 0)
38 perror("write");
39 else
40 printf("Short write\n");
41 goto err;
42 }
Jens Axboecb1e01a2019-04-25 09:58:55 -060043 fsync(fd);
44
45 if (posix_fadvise(fd, 0, 4096, POSIX_FADV_DONTNEED)) {
46 perror("fadvise");
Jens Axboe60b33052019-05-01 10:07:37 -060047err:
Jens Axboecb1e01a2019-04-25 09:58:55 -060048 close(fd);
49 free(buf);
50 return -1;
51 }
52
53 free(buf);
54 return fd;
55}
56
57static void put_file_fd(int fd)
58{
59 close(fd);
60 unlink("testfile");
61}
62
63int main(int argc, char *argv[])
64{
65 struct io_uring ring;
66 struct io_uring_sqe *sqe;
Jens Axboece8e2bc2019-04-30 14:50:36 -060067 struct io_uring_cqe *cqe;
Jens Axboecb1e01a2019-04-25 09:58:55 -060068 struct iovec iov;
69 int ret, fd;
70
Jens Axboea2141fc2020-05-19 17:36:19 -060071 if (argc > 1)
72 return 0;
73
Jens Axboed84d9012021-02-24 07:12:47 -070074 iov.iov_base = t_malloc(4096);
Jens Axboecb1e01a2019-04-25 09:58:55 -060075 iov.iov_len = 4096;
76
77 ret = io_uring_queue_init(2, &ring, 0);
78 if (ret) {
79 printf("ring setup failed\n");
80 return 1;
81
82 }
83
84 sqe = io_uring_get_sqe(&ring);
85 if (!sqe) {
86 printf("get sqe failed\n");
87 return 1;
88 }
89
90 fd = get_file_fd();
91 if (fd < 0)
92 return 1;
93
94 io_uring_prep_readv(sqe, fd, &iov, 1, 0);
95 sqe->rw_flags = RWF_NOWAIT;
96
97 ret = io_uring_submit(&ring);
Jens Axboece8e2bc2019-04-30 14:50:36 -060098 if (ret != 1) {
99 printf("Got submit %d, expected 1\n", ret);
100 goto err;
101 }
102
103 ret = io_uring_peek_cqe(&ring, &cqe);
104 if (ret) {
105 printf("Ring peek got %d\n", ret);
106 goto err;
107 }
108
Jens Axboe9e1d69e2020-07-30 10:44:10 -0600109 if (cqe->res != -EAGAIN && cqe->res != 4096) {
Jens Axboece8e2bc2019-04-30 14:50:36 -0600110 printf("cqe error: %d\n", cqe->res);
Jens Axboecb1e01a2019-04-25 09:58:55 -0600111 goto err;
112 }
113
114 put_file_fd(fd);
115 return 0;
116err:
117 put_file_fd(fd);
118 return 1;
119}