xref: /aosp_15_r20/external/liburing/test/hardlink.c (revision 25da2bea747f3a93b4c30fd9708b0618ef55a0e6)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Description: test io_uring linkat 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_linkat(struct io_uring * ring,const char * oldname,const char * newname)15 static int do_linkat(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_linkat(sqe, AT_FDCWD, oldname, AT_FDCWD, newname, 0);
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 
files_linked_ok(const char * fn1,const char * fn2)46 int files_linked_ok(const char* fn1, const char *fn2)
47 {
48 	struct stat s1, s2;
49 
50 	if (stat(fn1, &s1)) {
51 		fprintf(stderr, "stat(%s): %s\n", fn1, strerror(errno));
52 		return 0;
53 	}
54 	if (stat(fn2, &s2)) {
55 		fprintf(stderr, "stat(%s): %s\n", fn2, strerror(errno));
56 		return 0;
57 	}
58 	if (s1.st_dev != s2.st_dev || s1.st_ino != s2.st_ino) {
59 		fprintf(stderr, "linked files have different device / inode numbers\n");
60 		return 0;
61 	}
62 	if (s1.st_nlink != 2 || s2.st_nlink != 2) {
63 		fprintf(stderr, "linked files have unexpected links count\n");
64 		return 0;
65 	}
66 	return 1;
67 }
68 
main(int argc,char * argv[])69 int main(int argc, char *argv[])
70 {
71 	static const char target[] = "io_uring-linkat-test-target";
72 	static const char linkname[] = "io_uring-linkat-test-link";
73 	int ret;
74 	struct io_uring ring;
75 
76 	if (argc > 1)
77 		return 0;
78 
79 	ret = io_uring_queue_init(8, &ring, 0);
80 	if (ret) {
81 		fprintf(stderr, "queue init failed: %d\n", ret);
82 		return ret;
83 	}
84 
85 	ret = open(target, O_CREAT | O_RDWR | O_EXCL, 0600);
86 	if (ret < 0) {
87 		perror("open");
88 		goto err;
89 	}
90 	if (write(ret, "linktest", 8) != 8) {
91 		close(ret);
92 		goto err1;
93 	}
94 	close(ret);
95 
96 	ret = do_linkat(&ring, target, linkname);
97 	if (ret < 0) {
98 		if (ret == -EBADF || ret == -EINVAL) {
99 			fprintf(stdout, "linkat not supported, skipping\n");
100 			goto out;
101 		}
102 		fprintf(stderr, "linkat: %s\n", strerror(-ret));
103 		goto err1;
104 	} else if (ret) {
105 		goto err1;
106 	}
107 
108 	if (!files_linked_ok(linkname, target))
109 		goto err2;
110 
111 	ret = do_linkat(&ring, target, linkname);
112 	if (ret != -EEXIST) {
113 		fprintf(stderr, "test_linkat linkname already exists failed: %d\n", ret);
114 		goto err2;
115 	}
116 
117 	ret = do_linkat(&ring, target, "surely/this/does/not/exist");
118 	if (ret != -ENOENT) {
119 		fprintf(stderr, "test_linkat no parent failed: %d\n", ret);
120 		goto err2;
121 	}
122 
123 out:
124 	unlinkat(AT_FDCWD, linkname, 0);
125 	unlinkat(AT_FDCWD, target, 0);
126 	io_uring_queue_exit(&ring);
127 	return 0;
128 err2:
129 	unlinkat(AT_FDCWD, linkname, 0);
130 err1:
131 	unlinkat(AT_FDCWD, target, 0);
132 err:
133 	io_uring_queue_exit(&ring);
134 	return 1;
135 }
136 
137