• 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 	unlink("testfile");
30 	if (fd < 0) {
31 		perror("open file");
32 		return -1;
33 	}
34 
35 	buf = t_malloc(BLOCK);
36 	memset(buf, 0, BLOCK);
37 	ret = write(fd, buf, BLOCK);
38 	if (ret != BLOCK) {
39 		if (ret < 0)
40 			perror("write");
41 		else
42 			printf("Short write\n");
43 		goto err;
44 	}
45 	fsync(fd);
46 
47 	if (posix_fadvise(fd, 0, 4096, POSIX_FADV_DONTNEED)) {
48 		perror("fadvise");
49 err:
50 		close(fd);
51 		free(buf);
52 		return -1;
53 	}
54 
55 	free(buf);
56 	return fd;
57 }
58 
main(int argc,char * argv[])59 int main(int argc, char *argv[])
60 {
61 	struct io_uring ring;
62 	struct io_uring_sqe *sqe;
63 	struct io_uring_cqe *cqe;
64 	struct iovec iov;
65 	int ret, fd;
66 
67 	if (argc > 1)
68 		return T_EXIT_SKIP;
69 
70 	iov.iov_base = t_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 T_EXIT_FAIL;
77 
78 	}
79 
80 	sqe = io_uring_get_sqe(&ring);
81 	if (!sqe) {
82 		printf("get sqe failed\n");
83 		return T_EXIT_FAIL;
84 	}
85 
86 	fd = get_file_fd();
87 	if (fd < 0)
88 		return T_EXIT_FAIL;
89 
90 	io_uring_prep_readv(sqe, fd, &iov, 1, 0);
91 	sqe->rw_flags = RWF_NOWAIT;
92 
93 	ret = io_uring_submit(&ring);
94 	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 	ret = T_EXIT_PASS;
106 	if (cqe->res != -EAGAIN && cqe->res != 4096) {
107 		if (cqe->res == -EOPNOTSUPP) {
108 			ret = T_EXIT_SKIP;
109 		} else {
110 			printf("cqe error: %d\n", cqe->res);
111 			goto err;
112 		}
113 	}
114 
115 	close(fd);
116 	free(iov.iov_base);
117 	return ret;
118 err:
119 	close(fd);
120 	free(iov.iov_base);
121 	return T_EXIT_FAIL;
122 }
123