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;
23 struct io_uring_sqe *sqe;
24 struct io_uring_cqe *cqe;
25 struct iovec *iovecs;
26 struct io_uring_params p;
27 char *fname;
28 void *buf;
29
30 if (geteuid()) {
31 fprintf(stdout, "Test requires root, skipping\n");
32 return 0;
33 }
34
35 memset(&p, 0, sizeof(p));
36 p.flags = IORING_SETUP_SQPOLL;
37 ret = t_create_ring_params(4, &ring, &p);
38 if (ret == T_SETUP_SKIP)
39 return 0;
40 else if (ret < 0)
41 return 1;
42
43 if (argc > 1) {
44 fname = argv[1];
45 } else {
46 fname = ".sqpoll.tmp";
47 t_create_file(fname, FILE_SIZE);
48 }
49
50 fd = open(fname, O_RDONLY | O_DIRECT);
51 if (fd < 0) {
52 perror("open");
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 if (fname != argv[1])
98 unlink(fname);
99 io_uring_queue_exit(&ring);
100 return ret;
101 }
102