xref: /aosp_15_r20/external/liburing/test/500f9fbadef8.c (revision 25da2bea747f3a93b4c30fd9708b0618ef55a0e6)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Description: Single depth submit+wait poll hang test
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 BLOCKS	4096
17 
main(int argc,char * argv[])18 int main(int argc, char *argv[])
19 {
20 	struct io_uring ring;
21 	struct io_uring_sqe *sqe;
22 	struct io_uring_cqe *cqe;
23 	struct iovec iov;
24 	char buf[32];
25 	off_t offset;
26 	unsigned blocks;
27 	int ret, fd;
28 
29 	if (argc > 1)
30 		return 0;
31 
32 	t_posix_memalign(&iov.iov_base, 4096, 4096);
33 	iov.iov_len = 4096;
34 
35 	ret = io_uring_queue_init(1, &ring, IORING_SETUP_IOPOLL);
36 	if (ret) {
37 		fprintf(stderr, "ring setup failed\n");
38 		return 1;
39 
40 	}
41 
42 	sprintf(buf, "./XXXXXX");
43 	fd = mkostemp(buf, O_WRONLY | O_DIRECT | O_CREAT);
44 	if (fd < 0) {
45 		perror("mkostemp");
46 		return 1;
47 	}
48 
49 	offset = 0;
50 	blocks = BLOCKS;
51 	do {
52 		sqe = io_uring_get_sqe(&ring);
53 		if (!sqe) {
54 			fprintf(stderr, "get sqe failed\n");
55 			goto err;
56 		}
57 		io_uring_prep_writev(sqe, fd, &iov, 1, offset);
58 		ret = io_uring_submit_and_wait(&ring, 1);
59 		if (ret < 0) {
60 			fprintf(stderr, "submit_and_wait: %d\n", ret);
61 			goto err;
62 		}
63 		ret = io_uring_wait_cqe(&ring, &cqe);
64 		if (ret < 0) {
65 			fprintf(stderr, "wait completion: %d\n", ret);
66 			goto err;
67 		}
68 		if (cqe->res != 4096) {
69 			if (cqe->res == -EOPNOTSUPP)
70 				goto skipped;
71 			goto err;
72 		}
73 		io_uring_cqe_seen(&ring, cqe);
74 		offset += 4096;
75 	} while (--blocks);
76 
77 	close(fd);
78 	unlink(buf);
79 	return 0;
80 err:
81 	close(fd);
82 	unlink(buf);
83 	return 1;
84 skipped:
85 	fprintf(stderr, "Polling not supported in current dir, test skipped\n");
86 	close(fd);
87 	unlink(buf);
88 	return 0;
89 }
90