1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Description: test io_uring symlinkat handling
4 */
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <sys/stat.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11
12 #include "liburing.h"
13
14
do_symlinkat(struct io_uring * ring,const char * oldname,const char * newname)15 static int do_symlinkat(struct io_uring *ring, const char *oldname, const char *newname)
16 {
17 int ret;
18 struct io_uring_sqe *sqe;
19 struct io_uring_cqe *cqe;
20
21 sqe = io_uring_get_sqe(ring);
22 if (!sqe) {
23 fprintf(stderr, "sqe get failed\n");
24 goto err;
25 }
26 io_uring_prep_symlinkat(sqe, oldname, AT_FDCWD, newname);
27
28 ret = io_uring_submit(ring);
29 if (ret != 1) {
30 fprintf(stderr, "submit failed: %d\n", ret);
31 goto err;
32 }
33
34 ret = io_uring_wait_cqes(ring, &cqe, 1, 0, 0);
35 if (ret) {
36 fprintf(stderr, "wait_cqe failed: %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
test_link_contents(const char * linkname,const char * expected_contents)46 int test_link_contents(const char* linkname, const char *expected_contents)
47 {
48 char buf[128];
49 int ret = readlink(linkname, buf, 127);
50 if (ret < 0) {
51 perror("readlink");
52 return ret;
53 }
54 buf[ret] = 0;
55 if (strncmp(buf, expected_contents, 128)) {
56 fprintf(stderr, "link contents differs from expected: '%s' vs '%s'",
57 buf, expected_contents);
58 return -1;
59 }
60 return 0;
61 }
62
main(int argc,char * argv[])63 int main(int argc, char *argv[])
64 {
65 static const char target[] = "io_uring-symlinkat-test-target";
66 static const char linkname[] = "io_uring-symlinkat-test-link";
67 int ret;
68 struct io_uring ring;
69
70 if (argc > 1)
71 return 0;
72
73 ret = io_uring_queue_init(8, &ring, 0);
74 if (ret) {
75 fprintf(stderr, "queue init failed: %d\n", ret);
76 return ret;
77 }
78
79 ret = do_symlinkat(&ring, target, linkname);
80 if (ret < 0) {
81 if (ret == -EBADF || ret == -EINVAL) {
82 fprintf(stdout, "symlinkat not supported, skipping\n");
83 goto out;
84 }
85 fprintf(stderr, "symlinkat: %s\n", strerror(-ret));
86 goto err;
87 } else if (ret) {
88 goto err;
89 }
90
91 ret = test_link_contents(linkname, target);
92 if (ret < 0)
93 goto err1;
94
95 ret = do_symlinkat(&ring, target, linkname);
96 if (ret != -EEXIST) {
97 fprintf(stderr, "test_symlinkat linkname already exists failed: %d\n", ret);
98 goto err1;
99 }
100
101 ret = do_symlinkat(&ring, target, "surely/this/does/not/exist");
102 if (ret != -ENOENT) {
103 fprintf(stderr, "test_symlinkat no parent failed: %d\n", ret);
104 goto err1;
105 }
106
107 out:
108 unlinkat(AT_FDCWD, linkname, 0);
109 io_uring_queue_exit(&ring);
110 return 0;
111 err1:
112 unlinkat(AT_FDCWD, linkname, 0);
113 err:
114 io_uring_queue_exit(&ring);
115 return 1;
116 }
117