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