xref: /aosp_15_r20/external/liburing/test/8a9973408177.c (revision 25da2bea747f3a93b4c30fd9708b0618ef55a0e6)
1 /* SPDX-License-Identifier: MIT */
2 #include <errno.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <fcntl.h>
8 
9 #include "liburing.h"
10 
register_file(struct io_uring * ring)11 static int register_file(struct io_uring *ring)
12 {
13 	char buf[32];
14 	int ret, fd;
15 
16 	sprintf(buf, "./XXXXXX");
17 	fd = mkstemp(buf);
18 	if (fd < 0) {
19 		perror("open");
20 		return 1;
21 	}
22 
23 	ret = io_uring_register_files(ring, &fd, 1);
24 	if (ret) {
25 		fprintf(stderr, "file register %d\n", ret);
26 		return 1;
27 	}
28 
29 	ret = io_uring_unregister_files(ring);
30 	if (ret) {
31 		fprintf(stderr, "file register %d\n", ret);
32 		return 1;
33 	}
34 
35 	unlink(buf);
36 	close(fd);
37 	return 0;
38 }
39 
test_single_fsync(struct io_uring * ring)40 static int test_single_fsync(struct io_uring *ring)
41 {
42 	struct io_uring_cqe *cqe;
43 	struct io_uring_sqe *sqe;
44 	char buf[32];
45 	int fd, ret;
46 
47 	sprintf(buf, "./XXXXXX");
48 	fd = mkstemp(buf);
49 	if (fd < 0) {
50 		perror("open");
51 		return 1;
52 	}
53 
54 	sqe = io_uring_get_sqe(ring);
55 	if (!sqe) {
56 		printf("get sqe failed\n");
57 		goto err;
58 	}
59 
60 	io_uring_prep_fsync(sqe, fd, 0);
61 
62 	ret = io_uring_submit(ring);
63 	if (ret <= 0) {
64 		printf("sqe submit failed: %d\n", ret);
65 		goto err;
66 	}
67 
68 	ret = io_uring_wait_cqe(ring, &cqe);
69 	if (ret < 0) {
70 		printf("wait completion %d\n", ret);
71 		goto err;
72 	}
73 
74 	io_uring_cqe_seen(ring, cqe);
75 	unlink(buf);
76 	return 0;
77 err:
78 	unlink(buf);
79 	return 1;
80 }
81 
main(int argc,char * argv[])82 int main(int argc, char *argv[])
83 {
84 	struct io_uring ring;
85 	int ret;
86 
87 	if (argc > 1)
88 		return 0;
89 
90 	ret = io_uring_queue_init(8, &ring, 0);
91 	if (ret) {
92 		printf("ring setup failed\n");
93 		return 1;
94 	}
95 
96 	ret = register_file(&ring);
97 	if (ret)
98 		return ret;
99 	ret = test_single_fsync(&ring);
100 	if (ret) {
101 		printf("test_single_fsync failed\n");
102 		return ret;
103 	}
104 
105 	return 0;
106 }
107