1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Description: run various nop tests
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 "liburing.h"
14
test_unlink(struct io_uring * ring,const char * old)15 static int test_unlink(struct io_uring *ring, const char *old)
16 {
17 struct io_uring_cqe *cqe;
18 struct io_uring_sqe *sqe;
19 int ret;
20
21 sqe = io_uring_get_sqe(ring);
22 if (!sqe) {
23 fprintf(stderr, "get sqe failed\n");
24 goto err;
25 }
26 io_uring_prep_unlinkat(sqe, AT_FDCWD, old, 0);
27
28 ret = io_uring_submit(ring);
29 if (ret <= 0) {
30 fprintf(stderr, "sqe submit failed: %d\n", ret);
31 goto err;
32 }
33
34 ret = io_uring_wait_cqe(ring, &cqe);
35 if (ret < 0) {
36 fprintf(stderr, "wait completion %d\n", ret);
37 goto err;
38 }
39 ret = cqe->res;
40 io_uring_cqe_seen(ring, cqe);
41 return ret;
42 err:
43 return 1;
44 }
45
stat_file(const char * buf)46 static int stat_file(const char *buf)
47 {
48 struct stat sb;
49
50 if (!stat(buf, &sb))
51 return 0;
52
53 return errno;
54 }
55
main(int argc,char * argv[])56 int main(int argc, char *argv[])
57 {
58 struct io_uring ring;
59 char buf[32] = "./XXXXXX";
60 int ret;
61
62 if (argc > 1)
63 return 0;
64
65 ret = io_uring_queue_init(1, &ring, 0);
66 if (ret) {
67 fprintf(stderr, "ring setup failed: %d\n", ret);
68 return 1;
69 }
70
71 ret = mkstemp(buf);
72 if (ret < 0) {
73 perror("mkstemp");
74 return 1;
75 }
76 close(ret);
77
78 if (stat_file(buf) != 0) {
79 perror("stat");
80 return 1;
81 }
82
83 ret = test_unlink(&ring, buf);
84 if (ret < 0) {
85 if (ret == -EBADF || ret == -EINVAL) {
86 fprintf(stdout, "Unlink not supported, skipping\n");
87 unlink(buf);
88 return 0;
89 }
90 fprintf(stderr, "rename: %s\n", strerror(-ret));
91 goto err;
92 } else if (ret)
93 goto err;
94
95 ret = stat_file(buf);
96 if (ret != ENOENT) {
97 fprintf(stderr, "stat got %s\n", strerror(ret));
98 return 1;
99 }
100
101 ret = test_unlink(&ring, "/3/2/3/1/z/y");
102 if (ret != -ENOENT) {
103 fprintf(stderr, "invalid unlink got %s\n", strerror(-ret));
104 return 1;
105 }
106
107 return 0;
108 err:
109 unlink(buf);
110 return 1;
111 }
112