1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Test case for SQPOLL missing a 'ret' clear in case of busy.
4 *
5 * Heavily based on a test case from
6 * Xiaoguang Wang <xiaoguang.wang@linux.alibaba.com>
7 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <string.h>
13
14 #include "helpers.h"
15 #include "liburing.h"
16
17 #define FILE_SIZE (128 * 1024)
18
main(int argc,char * argv[])19 int main(int argc, char *argv[])
20 {
21 struct io_uring ring;
22 int i, fd, ret, __e;
23 struct io_uring_sqe *sqe;
24 struct io_uring_cqe *cqe;
25 struct iovec *iovecs = NULL;
26 struct io_uring_params p;
27 char *fname;
28 void *buf;
29
30 memset(&p, 0, sizeof(p));
31 p.flags = IORING_SETUP_SQPOLL;
32 ret = t_create_ring_params(16, &ring, &p);
33 if (ret == T_SETUP_SKIP)
34 return T_EXIT_SKIP;
35 else if (ret < 0)
36 return T_EXIT_FAIL;
37
38 if (argc > 1) {
39 fname = argv[1];
40 } else {
41 fname = ".sqpoll.tmp";
42 t_create_file(fname, FILE_SIZE);
43 }
44
45 fd = open(fname, O_RDONLY | O_DIRECT);
46 __e = errno;
47 if (fname != argv[1])
48 unlink(fname);
49 if (fd < 0) {
50 if (__e == EINVAL || __e == EPERM || __e == EACCES)
51 return T_EXIT_SKIP;
52 fprintf(stderr, "open: %s\n", strerror(__e));
53 goto out;
54 }
55
56 iovecs = t_calloc(10, sizeof(struct iovec));
57 for (i = 0; i < 10; i++) {
58 t_posix_memalign(&buf, 4096, 4096);
59 iovecs[i].iov_base = buf;
60 iovecs[i].iov_len = 4096;
61 }
62
63 ret = io_uring_register_files(&ring, &fd, 1);
64 if (ret < 0) {
65 fprintf(stderr, "register files %d\n", ret);
66 goto out;
67 }
68
69 for (i = 0; i < 10; i++) {
70 sqe = io_uring_get_sqe(&ring);
71 if (!sqe)
72 break;
73
74 io_uring_prep_readv(sqe, 0, &iovecs[i], 1, 0);
75 sqe->flags |= IOSQE_FIXED_FILE;
76
77 ret = io_uring_submit(&ring);
78 usleep(1000);
79 }
80
81 for (i = 0; i < 10; i++) {
82 ret = io_uring_wait_cqe(&ring, &cqe);
83 if (ret) {
84 fprintf(stderr, "wait_cqe=%d\n", ret);
85 break;
86 }
87 if (cqe->res != 4096) {
88 fprintf(stderr, "ret=%d, wanted 4096\n", cqe->res);
89 ret = 1;
90 break;
91 }
92 io_uring_cqe_seen(&ring, cqe);
93 }
94
95 close(fd);
96 out:
97 io_uring_queue_exit(&ring);
98 if (iovecs != NULL) { //
99 for (i = 0; i < 10; i++)
100 free(iovecs[i].iov_base);
101 free(iovecs);
102 }
103 return ret;
104 }
105