blob: 428b42bcba538afa55037f35e158776aa29218a5 [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
14static int get_file_fd(void)
15{
16 char *buf;
17 int fd;
18
19 fd = open("testfile", O_RDWR | O_CREAT, 0644);
20 if (fd < 0) {
21 perror("open file");
22 return -1;
23 }
24
25 buf = malloc(4096);
26 write(fd, buf, 4096);
27 fsync(fd);
28
29 if (posix_fadvise(fd, 0, 4096, POSIX_FADV_DONTNEED)) {
30 perror("fadvise");
31 close(fd);
32 free(buf);
33 return -1;
34 }
35
36 free(buf);
37 return fd;
38}
39
40static void put_file_fd(int fd)
41{
42 close(fd);
43 unlink("testfile");
44}
45
46int main(int argc, char *argv[])
47{
48 struct io_uring ring;
49 struct io_uring_sqe *sqe;
50 struct iovec iov;
51 int ret, fd;
52
53 iov.iov_base = malloc(4096);
54 iov.iov_len = 4096;
55
56 ret = io_uring_queue_init(2, &ring, 0);
57 if (ret) {
58 printf("ring setup failed\n");
59 return 1;
60
61 }
62
63 sqe = io_uring_get_sqe(&ring);
64 if (!sqe) {
65 printf("get sqe failed\n");
66 return 1;
67 }
68
69 fd = get_file_fd();
70 if (fd < 0)
71 return 1;
72
73 io_uring_prep_readv(sqe, fd, &iov, 1, 0);
74 sqe->rw_flags = RWF_NOWAIT;
75
76 ret = io_uring_submit(&ring);
77 if (ret != -EAGAIN) {
78 printf("Got submit %d, expected EAGAIN\n", ret);
79 goto err;
80 }
81
82 put_file_fd(fd);
83 return 0;
84err:
85 put_file_fd(fd);
86 return 1;
87}