• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: MIT */
2 /*
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 
13 #include "helpers.h"
14 #include "liburing.h"
15 
16 #define BLOCK	4096
17 
18 #ifndef RWF_NOWAIT
19 #define RWF_NOWAIT	8
20 #endif
21 
get_file_fd(void)22 static int get_file_fd(void)
23 {
24 	ssize_t ret;
25 	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 
34 	buf = t_malloc(BLOCK);
35 	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 	}
43 	fsync(fd);
44 
45 	if (posix_fadvise(fd, 0, 4096, POSIX_FADV_DONTNEED)) {
46 		perror("fadvise");
47 err:
48 		close(fd);
49 		free(buf);
50 		return -1;
51 	}
52 
53 	free(buf);
54 	return fd;
55 }
56 
put_file_fd(int fd)57 static void put_file_fd(int fd)
58 {
59 	close(fd);
60 	unlink("testfile");
61 }
62 
main(int argc,char * argv[])63 int main(int argc, char *argv[])
64 {
65 	struct io_uring ring;
66 	struct io_uring_sqe *sqe;
67 	struct io_uring_cqe *cqe;
68 	struct iovec iov;
69 	int ret, fd;
70 
71 	if (argc > 1)
72 		return 0;
73 
74 	iov.iov_base = t_malloc(4096);
75 	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);
98 	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 
109 	if (cqe->res != -EAGAIN && cqe->res != 4096) {
110 		printf("cqe error: %d\n", cqe->res);
111 		goto err;
112 	}
113 
114 	put_file_fd(fd);
115 	return 0;
116 err:
117 	put_file_fd(fd);
118 	return 1;
119 }
120