• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static int test_link_contents(const char* linkname,
47 			      const char *expected_contents)
48 {
49 	char buf[128];
50 	int ret = readlink(linkname, buf, 127);
51 	if (ret < 0) {
52 		perror("readlink");
53 		return ret;
54 	}
55 	buf[ret] = 0;
56 	if (strncmp(buf, expected_contents, 128)) {
57 		fprintf(stderr, "link contents differs from expected: '%s' vs '%s'",
58 			buf, expected_contents);
59 		return -1;
60 	}
61 	return 0;
62 }
63 
main(int argc,char * argv[])64 int main(int argc, char *argv[])
65 {
66 	static const char target[] = "io_uring-symlinkat-test-target";
67 	static const char linkname[] = "io_uring-symlinkat-test-link";
68 	int ret;
69 	struct io_uring ring;
70 
71 	if (argc > 1)
72 		return 0;
73 
74 	ret = io_uring_queue_init(8, &ring, 0);
75 	if (ret) {
76 		fprintf(stderr, "queue init failed: %d\n", ret);
77 		return ret;
78 	}
79 
80 	ret = do_symlinkat(&ring, target, linkname);
81 	if (ret < 0) {
82 		if (ret == -EBADF || ret == -EINVAL) {
83 			fprintf(stdout, "symlinkat not supported, skipping\n");
84 			goto out;
85 		}
86 		fprintf(stderr, "symlinkat: %s\n", strerror(-ret));
87 		goto err;
88 	} else if (ret) {
89 		goto err;
90 	}
91 
92 	ret = test_link_contents(linkname, target);
93 	if (ret < 0)
94 		goto err1;
95 
96 	ret = do_symlinkat(&ring, target, linkname);
97 	if (ret != -EEXIST) {
98 		fprintf(stderr, "test_symlinkat linkname already exists failed: %d\n", ret);
99 		goto err1;
100 	}
101 
102 	ret = do_symlinkat(&ring, target, "surely/this/does/not/exist");
103 	if (ret != -ENOENT) {
104 		fprintf(stderr, "test_symlinkat no parent failed: %d\n", ret);
105 		goto err1;
106 	}
107 
108 out:
109 	unlinkat(AT_FDCWD, linkname, 0);
110 	io_uring_queue_exit(&ring);
111 	return 0;
112 err1:
113 	unlinkat(AT_FDCWD, linkname, 0);
114 err:
115 	io_uring_queue_exit(&ring);
116 	return 1;
117 }
118