• 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 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 	ret = io_uring_queue_init(8, &ring, 0);
71 	if (ret) {
72 		fprintf(stderr, "queue init failed: %d\n", ret);
73 		return ret;
74 	}
75 
76 	ret = do_symlinkat(&ring, target, linkname);
77 	if (ret < 0) {
78 		if (ret == -EBADF || ret == -EINVAL) {
79 			fprintf(stdout, "symlinkat not supported, skipping\n");
80 			goto out;
81 		}
82 		fprintf(stderr, "symlinkat: %s\n", strerror(-ret));
83 		goto err;
84 	} else if (ret) {
85 		goto err;
86 	}
87 
88 	ret = test_link_contents(linkname, target);
89 	if (ret < 0)
90 		goto err1;
91 
92 	ret = do_symlinkat(&ring, target, linkname);
93 	if (ret != -EEXIST) {
94 		fprintf(stderr, "test_symlinkat linkname already exists failed: %d\n", ret);
95 		goto err1;
96 	}
97 
98 	ret = do_symlinkat(&ring, target, "surely/this/does/not/exist");
99 	if (ret != -ENOENT) {
100 		fprintf(stderr, "test_symlinkat no parent failed: %d\n", ret);
101 		goto err1;
102 	}
103 
104 out:
105 	unlinkat(AT_FDCWD, linkname, 0);
106 	io_uring_queue_exit(&ring);
107 	return 0;
108 err1:
109 	unlinkat(AT_FDCWD, linkname, 0);
110 err:
111 	io_uring_queue_exit(&ring);
112 	return 1;
113 }
114