]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-mountpoint-util.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / test / test-mountpoint-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <sys/mount.h>
4
5 #include "alloc-util.h"
6 #include "def.h"
7 #include "fd-util.h"
8 #include "fileio.h"
9 #include "hashmap.h"
10 #include "log.h"
11 #include "mountpoint-util.h"
12 #include "path-util.h"
13 #include "rm-rf.h"
14 #include "string-util.h"
15 #include "tests.h"
16
17 static void test_mount_propagation_flags(const char *name, int ret, unsigned long expected) {
18 long unsigned flags;
19
20 log_info("/* %s(%s) */", __func__, name);
21
22 assert_se(mount_propagation_flags_from_string(name, &flags) == ret);
23
24 if (ret >= 0) {
25 const char *c;
26
27 assert_se(flags == expected);
28
29 c = mount_propagation_flags_to_string(flags);
30 if (isempty(name))
31 assert_se(isempty(c));
32 else
33 assert_se(streq(c, name));
34 }
35 }
36
37 static void test_mnt_id(void) {
38 _cleanup_fclose_ FILE *f = NULL;
39 _cleanup_hashmap_free_free_ Hashmap *h = NULL;
40 Iterator i;
41 char *p;
42 void *k;
43 int r;
44
45 log_info("/* %s */", __func__);
46
47 assert_se(f = fopen("/proc/self/mountinfo", "re"));
48 assert_se(h = hashmap_new(&trivial_hash_ops));
49
50 for (;;) {
51 _cleanup_free_ char *line = NULL, *path = NULL;
52 int mnt_id;
53
54 r = read_line(f, LONG_LINE_MAX, &line);
55 if (r == 0)
56 break;
57 assert_se(r > 0);
58
59 assert_se(sscanf(line, "%i %*s %*s %*s %ms", &mnt_id, &path) == 2);
60 #if HAS_FEATURE_MEMORY_SANITIZER
61 /* We don't know the length of the string, so we need to unpoison it one char at a time */
62 for (const char *c = path; ;c++) {
63 msan_unpoison(c, 1);
64 if (!*c)
65 break;
66 }
67 #endif
68 log_debug("mountinfo: %s → %i", path, mnt_id);
69
70 assert_se(hashmap_put(h, INT_TO_PTR(mnt_id), path) >= 0);
71 path = NULL;
72 }
73
74 HASHMAP_FOREACH_KEY(p, k, h, i) {
75 int mnt_id = PTR_TO_INT(k), mnt_id2;
76
77 r = path_get_mnt_id(p, &mnt_id2);
78 if (r < 0) {
79 log_debug_errno(r, "Failed to get the mnt id of %s: %m\n", p);
80 continue;
81 }
82
83 log_debug("mnt ids of %s are %i, %i\n", p, mnt_id, mnt_id2);
84
85 if (mnt_id == mnt_id2)
86 continue;
87
88 /* The ids don't match? If so, then there are two mounts on the same path, let's check if
89 * that's really the case */
90 char *t = hashmap_get(h, INT_TO_PTR(mnt_id2));
91 log_debug("the other path for mnt id %i is %s\n", mnt_id2, t);
92 assert_se(path_equal(p, t));
93 }
94 }
95
96 static void test_path_is_mount_point(void) {
97 int fd;
98 char tmp_dir[] = "/tmp/test-path-is-mount-point-XXXXXX";
99 _cleanup_free_ char *file1 = NULL, *file2 = NULL, *link1 = NULL, *link2 = NULL;
100 _cleanup_free_ char *dir1 = NULL, *dir1file = NULL, *dirlink1 = NULL, *dirlink1file = NULL;
101 _cleanup_free_ char *dir2 = NULL, *dir2file = NULL;
102
103 log_info("/* %s */", __func__);
104
105 assert_se(path_is_mount_point("/", NULL, AT_SYMLINK_FOLLOW) > 0);
106 assert_se(path_is_mount_point("/", NULL, 0) > 0);
107 assert_se(path_is_mount_point("//", NULL, AT_SYMLINK_FOLLOW) > 0);
108 assert_se(path_is_mount_point("//", NULL, 0) > 0);
109
110 assert_se(path_is_mount_point("/proc", NULL, AT_SYMLINK_FOLLOW) > 0);
111 assert_se(path_is_mount_point("/proc", NULL, 0) > 0);
112 assert_se(path_is_mount_point("/proc/", NULL, AT_SYMLINK_FOLLOW) > 0);
113 assert_se(path_is_mount_point("/proc/", NULL, 0) > 0);
114
115 assert_se(path_is_mount_point("/proc/1", NULL, AT_SYMLINK_FOLLOW) == 0);
116 assert_se(path_is_mount_point("/proc/1", NULL, 0) == 0);
117 assert_se(path_is_mount_point("/proc/1/", NULL, AT_SYMLINK_FOLLOW) == 0);
118 assert_se(path_is_mount_point("/proc/1/", NULL, 0) == 0);
119
120 assert_se(path_is_mount_point("/sys", NULL, AT_SYMLINK_FOLLOW) > 0);
121 assert_se(path_is_mount_point("/sys", NULL, 0) > 0);
122 assert_se(path_is_mount_point("/sys/", NULL, AT_SYMLINK_FOLLOW) > 0);
123 assert_se(path_is_mount_point("/sys/", NULL, 0) > 0);
124
125 /* we'll create a hierarchy of different kinds of dir/file/link
126 * layouts:
127 *
128 * <tmp>/file1, <tmp>/file2
129 * <tmp>/link1 -> file1, <tmp>/link2 -> file2
130 * <tmp>/dir1/
131 * <tmp>/dir1/file
132 * <tmp>/dirlink1 -> dir1
133 * <tmp>/dirlink1file -> dirlink1/file
134 * <tmp>/dir2/
135 * <tmp>/dir2/file
136 */
137
138 /* file mountpoints */
139 assert_se(mkdtemp(tmp_dir) != NULL);
140 file1 = path_join(tmp_dir, "file1");
141 assert_se(file1);
142 file2 = path_join(tmp_dir, "file2");
143 assert_se(file2);
144 fd = open(file1, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
145 assert_se(fd > 0);
146 close(fd);
147 fd = open(file2, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
148 assert_se(fd > 0);
149 close(fd);
150 link1 = path_join(tmp_dir, "link1");
151 assert_se(link1);
152 assert_se(symlink("file1", link1) == 0);
153 link2 = path_join(tmp_dir, "link2");
154 assert_se(link1);
155 assert_se(symlink("file2", link2) == 0);
156
157 assert_se(path_is_mount_point(file1, NULL, AT_SYMLINK_FOLLOW) == 0);
158 assert_se(path_is_mount_point(file1, NULL, 0) == 0);
159 assert_se(path_is_mount_point(link1, NULL, AT_SYMLINK_FOLLOW) == 0);
160 assert_se(path_is_mount_point(link1, NULL, 0) == 0);
161
162 /* directory mountpoints */
163 dir1 = path_join(tmp_dir, "dir1");
164 assert_se(dir1);
165 assert_se(mkdir(dir1, 0755) == 0);
166 dirlink1 = path_join(tmp_dir, "dirlink1");
167 assert_se(dirlink1);
168 assert_se(symlink("dir1", dirlink1) == 0);
169 dirlink1file = path_join(tmp_dir, "dirlink1file");
170 assert_se(dirlink1file);
171 assert_se(symlink("dirlink1/file", dirlink1file) == 0);
172 dir2 = path_join(tmp_dir, "dir2");
173 assert_se(dir2);
174 assert_se(mkdir(dir2, 0755) == 0);
175
176 assert_se(path_is_mount_point(dir1, NULL, AT_SYMLINK_FOLLOW) == 0);
177 assert_se(path_is_mount_point(dir1, NULL, 0) == 0);
178 assert_se(path_is_mount_point(dirlink1, NULL, AT_SYMLINK_FOLLOW) == 0);
179 assert_se(path_is_mount_point(dirlink1, NULL, 0) == 0);
180
181 /* file in subdirectory mountpoints */
182 dir1file = path_join(dir1, "file");
183 assert_se(dir1file);
184 fd = open(dir1file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
185 assert_se(fd > 0);
186 close(fd);
187
188 assert_se(path_is_mount_point(dir1file, NULL, AT_SYMLINK_FOLLOW) == 0);
189 assert_se(path_is_mount_point(dir1file, NULL, 0) == 0);
190 assert_se(path_is_mount_point(dirlink1file, NULL, AT_SYMLINK_FOLLOW) == 0);
191 assert_se(path_is_mount_point(dirlink1file, NULL, 0) == 0);
192
193 /* these tests will only work as root */
194 if (mount(file1, file2, NULL, MS_BIND, NULL) >= 0) {
195 int rf, rt, rdf, rdt, rlf, rlt, rl1f, rl1t;
196 const char *file2d;
197
198 /* files */
199 /* capture results in vars, to avoid dangling mounts on failure */
200 log_info("%s: %s", __func__, file2);
201 rf = path_is_mount_point(file2, NULL, 0);
202 rt = path_is_mount_point(file2, NULL, AT_SYMLINK_FOLLOW);
203
204 file2d = strjoina(file2, "/");
205 log_info("%s: %s", __func__, file2d);
206 rdf = path_is_mount_point(file2d, NULL, 0);
207 rdt = path_is_mount_point(file2d, NULL, AT_SYMLINK_FOLLOW);
208
209 log_info("%s: %s", __func__, link2);
210 rlf = path_is_mount_point(link2, NULL, 0);
211 rlt = path_is_mount_point(link2, NULL, AT_SYMLINK_FOLLOW);
212
213 assert_se(umount(file2) == 0);
214
215 assert_se(rf == 1);
216 assert_se(rt == 1);
217 assert_se(rdf == -ENOTDIR);
218 assert_se(rdt == -ENOTDIR);
219 assert_se(rlf == 0);
220 assert_se(rlt == 1);
221
222 /* dirs */
223 dir2file = path_join(dir2, "file");
224 assert_se(dir2file);
225 fd = open(dir2file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
226 assert_se(fd > 0);
227 close(fd);
228
229 assert_se(mount(dir2, dir1, NULL, MS_BIND, NULL) >= 0);
230
231 log_info("%s: %s", __func__, dir1);
232 rf = path_is_mount_point(dir1, NULL, 0);
233 rt = path_is_mount_point(dir1, NULL, AT_SYMLINK_FOLLOW);
234 log_info("%s: %s", __func__, dirlink1);
235 rlf = path_is_mount_point(dirlink1, NULL, 0);
236 rlt = path_is_mount_point(dirlink1, NULL, AT_SYMLINK_FOLLOW);
237 log_info("%s: %s", __func__, dirlink1file);
238 /* its parent is a mount point, but not /file itself */
239 rl1f = path_is_mount_point(dirlink1file, NULL, 0);
240 rl1t = path_is_mount_point(dirlink1file, NULL, AT_SYMLINK_FOLLOW);
241
242 assert_se(umount(dir1) == 0);
243
244 assert_se(rf == 1);
245 assert_se(rt == 1);
246 assert_se(rlf == 0);
247 assert_se(rlt == 1);
248 assert_se(rl1f == 0);
249 assert_se(rl1t == 0);
250
251 } else
252 printf("Skipping bind mount file test: %m\n");
253
254 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
255 }
256
257 int main(int argc, char *argv[]) {
258 test_setup_logging(LOG_DEBUG);
259
260 test_mount_propagation_flags("shared", 0, MS_SHARED);
261 test_mount_propagation_flags("slave", 0, MS_SLAVE);
262 test_mount_propagation_flags("private", 0, MS_PRIVATE);
263 test_mount_propagation_flags(NULL, 0, 0);
264 test_mount_propagation_flags("", 0, 0);
265 test_mount_propagation_flags("xxxx", -EINVAL, 0);
266 test_mount_propagation_flags(" ", -EINVAL, 0);
267
268 test_mnt_id();
269 test_path_is_mount_point();
270
271 return 0;
272 }