]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-mountpoint-util.c
ASSERT_STREQ for simple cases
[thirdparty/systemd.git] / src / test / test-mountpoint-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sched.h>
4 #include <sys/mount.h>
5 #include <unistd.h>
6
7 #include "alloc-util.h"
8 #include "constants.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "hashmap.h"
12 #include "log.h"
13 #include "mountpoint-util.h"
14 #include "path-util.h"
15 #include "rm-rf.h"
16 #include "string-util.h"
17 #include "tests.h"
18 #include "tmpfile-util.h"
19
20 static void test_mount_propagation_flag_one(const char *name, int ret, unsigned long expected) {
21 unsigned long flags;
22
23 log_info("/* %s(%s) */", __func__, strnull(name));
24
25 assert_se(mount_propagation_flag_from_string(name, &flags) == ret);
26
27 if (ret >= 0) {
28 const char *c;
29
30 assert_se(flags == expected);
31
32 c = mount_propagation_flag_to_string(flags);
33 if (isempty(name))
34 assert_se(isempty(c));
35 else
36 ASSERT_STREQ(c, name);
37 }
38 }
39
40 TEST(mount_propagation_flag) {
41 test_mount_propagation_flag_one("shared", 0, MS_SHARED);
42 test_mount_propagation_flag_one("slave", 0, MS_SLAVE);
43 test_mount_propagation_flag_one("private", 0, MS_PRIVATE);
44 test_mount_propagation_flag_one(NULL, 0, 0);
45 test_mount_propagation_flag_one("", 0, 0);
46 test_mount_propagation_flag_one("xxxx", -EINVAL, 0);
47 test_mount_propagation_flag_one(" ", -EINVAL, 0);
48 }
49
50 TEST(mnt_id) {
51 _cleanup_fclose_ FILE *f = NULL;
52 _cleanup_hashmap_free_free_ Hashmap *h = NULL;
53 char *p;
54 void *k;
55 int r;
56
57 assert_se(f = fopen("/proc/self/mountinfo", "re"));
58 assert_se(h = hashmap_new(&trivial_hash_ops));
59
60 for (;;) {
61 _cleanup_free_ char *line = NULL, *path = NULL;
62 int mnt_id;
63
64 r = read_line(f, LONG_LINE_MAX, &line);
65 if (r == 0)
66 break;
67 assert_se(r > 0);
68
69 assert_se(sscanf(line, "%i %*s %*s %*s %ms", &mnt_id, &path) == 2);
70 #if HAS_FEATURE_MEMORY_SANITIZER
71 /* We don't know the length of the string, so we need to unpoison it one char at a time */
72 for (const char *c = path; ; c++) {
73 msan_unpoison(c, 1);
74 if (!*c)
75 break;
76 }
77 #endif
78 log_debug("mountinfo: %s → %i", path, mnt_id);
79
80 assert_se(hashmap_put(h, INT_TO_PTR(mnt_id), path) >= 0);
81 path = NULL;
82 }
83
84 HASHMAP_FOREACH_KEY(p, k, h) {
85 int mnt_id = PTR_TO_INT(k), mnt_id2;
86 const char *q;
87
88 r = path_get_mnt_id(p, &mnt_id2);
89 if (r < 0) {
90 log_debug_errno(r, "Failed to get the mnt id of %s: %m", p);
91 continue;
92 }
93
94 if (mnt_id == mnt_id2) {
95 log_debug("mnt ids of %s is %i.", p, mnt_id);
96 continue;
97 } else
98 log_debug("mnt ids of %s are %i (from /proc/self/mountinfo), %i (from path_get_mnt_id()).", p, mnt_id, mnt_id2);
99
100 /* The ids don't match? This can easily happen e.g. running with "unshare --mount-proc".
101 * See #11505. */
102 assert_se(q = hashmap_get(h, INT_TO_PTR(mnt_id2)));
103
104 assert_se((r = path_is_mount_point_full(p, NULL, 0)) >= 0);
105 if (r == 0) {
106 /* If the path is not a mount point anymore, then it must be a sub directory of
107 * the path corresponds to mnt_id2. */
108 log_debug("The path %s for mnt id %i is not a mount point.", p, mnt_id2);
109 assert_se(!isempty(path_startswith(p, q)));
110 } else {
111 /* If the path is still a mount point, then it must be equivalent to the path
112 * corresponds to mnt_id2 */
113 log_debug("There are multiple mounts on the same path %s.", p);
114 assert_se(path_equal(p, q));
115 }
116 }
117 }
118
119 TEST(path_is_mount_point) {
120 int fd;
121 char tmp_dir[] = "/tmp/test-path-is-mount-point-XXXXXX";
122 _cleanup_free_ char *file1 = NULL, *file2 = NULL, *link1 = NULL, *link2 = NULL;
123 _cleanup_free_ char *dir1 = NULL, *dir1file = NULL, *dirlink1 = NULL, *dirlink1file = NULL;
124 _cleanup_free_ char *dir2 = NULL, *dir2file = NULL;
125
126 assert_se(path_is_mount_point_full("/", NULL, AT_SYMLINK_FOLLOW) > 0);
127 assert_se(path_is_mount_point_full("/", NULL, 0) > 0);
128 assert_se(path_is_mount_point_full("//", NULL, AT_SYMLINK_FOLLOW) > 0);
129 assert_se(path_is_mount_point_full("//", NULL, 0) > 0);
130
131 assert_se(path_is_mount_point_full("/proc", NULL, AT_SYMLINK_FOLLOW) > 0);
132 assert_se(path_is_mount_point_full("/proc", NULL, 0) > 0);
133 assert_se(path_is_mount_point_full("/proc/", NULL, AT_SYMLINK_FOLLOW) > 0);
134 assert_se(path_is_mount_point_full("/proc/", NULL, 0) > 0);
135
136 assert_se(path_is_mount_point_full("/proc/1", NULL, AT_SYMLINK_FOLLOW) == 0);
137 assert_se(path_is_mount_point_full("/proc/1", NULL, 0) == 0);
138 assert_se(path_is_mount_point_full("/proc/1/", NULL, AT_SYMLINK_FOLLOW) == 0);
139 assert_se(path_is_mount_point_full("/proc/1/", NULL, 0) == 0);
140
141 /* we'll create a hierarchy of different kinds of dir/file/link
142 * layouts:
143 *
144 * <tmp>/file1, <tmp>/file2
145 * <tmp>/link1 -> file1, <tmp>/link2 -> file2
146 * <tmp>/dir1/
147 * <tmp>/dir1/file
148 * <tmp>/dirlink1 -> dir1
149 * <tmp>/dirlink1file -> dirlink1/file
150 * <tmp>/dir2/
151 * <tmp>/dir2/file
152 */
153
154 /* file mountpoints */
155 ASSERT_NOT_NULL(mkdtemp(tmp_dir));
156 file1 = path_join(tmp_dir, "file1");
157 assert_se(file1);
158 file2 = path_join(tmp_dir, "file2");
159 assert_se(file2);
160 fd = open(file1, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
161 assert_se(fd > 0);
162 close(fd);
163 fd = open(file2, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
164 assert_se(fd > 0);
165 close(fd);
166 link1 = path_join(tmp_dir, "link1");
167 assert_se(link1);
168 assert_se(symlink("file1", link1) == 0);
169 link2 = path_join(tmp_dir, "link2");
170 assert_se(link1);
171 assert_se(symlink("file2", link2) == 0);
172
173 assert_se(path_is_mount_point_full(file1, NULL, AT_SYMLINK_FOLLOW) == 0);
174 assert_se(path_is_mount_point_full(file1, NULL, 0) == 0);
175 assert_se(path_is_mount_point_full(link1, NULL, AT_SYMLINK_FOLLOW) == 0);
176 assert_se(path_is_mount_point_full(link1, NULL, 0) == 0);
177
178 /* directory mountpoints */
179 dir1 = path_join(tmp_dir, "dir1");
180 assert_se(dir1);
181 assert_se(mkdir(dir1, 0755) == 0);
182 dirlink1 = path_join(tmp_dir, "dirlink1");
183 assert_se(dirlink1);
184 assert_se(symlink("dir1", dirlink1) == 0);
185 dirlink1file = path_join(tmp_dir, "dirlink1file");
186 assert_se(dirlink1file);
187 assert_se(symlink("dirlink1/file", dirlink1file) == 0);
188 dir2 = path_join(tmp_dir, "dir2");
189 assert_se(dir2);
190 assert_se(mkdir(dir2, 0755) == 0);
191
192 assert_se(path_is_mount_point_full(dir1, NULL, AT_SYMLINK_FOLLOW) == 0);
193 assert_se(path_is_mount_point_full(dir1, NULL, 0) == 0);
194 assert_se(path_is_mount_point_full(dirlink1, NULL, AT_SYMLINK_FOLLOW) == 0);
195 assert_se(path_is_mount_point_full(dirlink1, NULL, 0) == 0);
196
197 /* file in subdirectory mountpoints */
198 dir1file = path_join(dir1, "file");
199 assert_se(dir1file);
200 fd = open(dir1file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
201 assert_se(fd > 0);
202 close(fd);
203
204 assert_se(path_is_mount_point_full(dir1file, NULL, AT_SYMLINK_FOLLOW) == 0);
205 assert_se(path_is_mount_point_full(dir1file, NULL, 0) == 0);
206 assert_se(path_is_mount_point_full(dirlink1file, NULL, AT_SYMLINK_FOLLOW) == 0);
207 assert_se(path_is_mount_point_full(dirlink1file, NULL, 0) == 0);
208
209 /* these tests will only work as root */
210 if (mount(file1, file2, NULL, MS_BIND, NULL) >= 0) {
211 int rf, rt, rdf, rdt, rlf, rlt, rl1f, rl1t;
212 const char *file2d;
213
214 /* files */
215 /* capture results in vars, to avoid dangling mounts on failure */
216 log_info("%s: %s", __func__, file2);
217 rf = path_is_mount_point_full(file2, NULL, 0);
218 rt = path_is_mount_point_full(file2, NULL, AT_SYMLINK_FOLLOW);
219
220 file2d = strjoina(file2, "/");
221 log_info("%s: %s", __func__, file2d);
222 rdf = path_is_mount_point_full(file2d, NULL, 0);
223 rdt = path_is_mount_point_full(file2d, NULL, AT_SYMLINK_FOLLOW);
224
225 log_info("%s: %s", __func__, link2);
226 rlf = path_is_mount_point_full(link2, NULL, 0);
227 rlt = path_is_mount_point_full(link2, NULL, AT_SYMLINK_FOLLOW);
228
229 assert_se(umount(file2) == 0);
230
231 assert_se(rf == 1);
232 assert_se(rt == 1);
233 assert_se(rdf == -ENOTDIR);
234 assert_se(rdt == -ENOTDIR);
235 assert_se(rlf == 0);
236 assert_se(rlt == 1);
237
238 /* dirs */
239 dir2file = path_join(dir2, "file");
240 assert_se(dir2file);
241 fd = open(dir2file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
242 assert_se(fd > 0);
243 close(fd);
244
245 assert_se(mount(dir2, dir1, NULL, MS_BIND, NULL) >= 0);
246
247 log_info("%s: %s", __func__, dir1);
248 rf = path_is_mount_point_full(dir1, NULL, 0);
249 rt = path_is_mount_point_full(dir1, NULL, AT_SYMLINK_FOLLOW);
250 log_info("%s: %s", __func__, dirlink1);
251 rlf = path_is_mount_point_full(dirlink1, NULL, 0);
252 rlt = path_is_mount_point_full(dirlink1, NULL, AT_SYMLINK_FOLLOW);
253 log_info("%s: %s", __func__, dirlink1file);
254 /* its parent is a mount point, but not /file itself */
255 rl1f = path_is_mount_point_full(dirlink1file, NULL, 0);
256 rl1t = path_is_mount_point_full(dirlink1file, NULL, AT_SYMLINK_FOLLOW);
257
258 assert_se(umount(dir1) == 0);
259
260 assert_se(rf == 1);
261 assert_se(rt == 1);
262 assert_se(rlf == 0);
263 assert_se(rlt == 1);
264 assert_se(rl1f == 0);
265 assert_se(rl1t == 0);
266
267 } else
268 log_info("Skipping bind mount file test");
269
270 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
271 }
272
273 TEST(fd_is_mount_point) {
274 _cleanup_(rm_rf_physical_and_freep) char *tmpdir = NULL;
275 _cleanup_close_ int fd = -EBADF;
276 int r;
277
278 fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
279 assert_se(fd >= 0);
280
281 /* Not allowed, since "/" is a path, not a plain filename */
282 assert_se(fd_is_mount_point(fd, "/", 0) == -EINVAL);
283 assert_se(fd_is_mount_point(fd, ".", 0) == -EINVAL);
284 assert_se(fd_is_mount_point(fd, "./", 0) == -EINVAL);
285 assert_se(fd_is_mount_point(fd, "..", 0) == -EINVAL);
286 assert_se(fd_is_mount_point(fd, "../", 0) == -EINVAL);
287 assert_se(fd_is_mount_point(fd, "", 0) == -EINVAL);
288 assert_se(fd_is_mount_point(fd, "/proc", 0) == -EINVAL);
289 assert_se(fd_is_mount_point(fd, "/proc/", 0) == -EINVAL);
290 assert_se(fd_is_mount_point(fd, "proc/sys", 0) == -EINVAL);
291 assert_se(fd_is_mount_point(fd, "proc/sys/", 0) == -EINVAL);
292
293 /* This one definitely is a mount point */
294 assert_se(fd_is_mount_point(fd, "proc", 0) > 0);
295 assert_se(fd_is_mount_point(fd, "proc/", 0) > 0);
296
297 safe_close(fd);
298 fd = open("/tmp", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
299 assert_se(fd >= 0);
300
301 assert_se(mkdtemp_malloc("/tmp/not-mounted-XXXXXX", &tmpdir) >= 0);
302 assert_se(fd_is_mount_point(fd, basename(tmpdir), 0) == 0);
303 assert_se(fd_is_mount_point(fd, strjoina(basename(tmpdir), "/"), 0) == 0);
304
305 safe_close(fd);
306 fd = open("/proc", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
307 assert_se(fd >= 0);
308
309 assert_se(fd_is_mount_point(fd, NULL, 0) > 0);
310 assert_se(fd_is_mount_point(fd, "", 0) == -EINVAL);
311 assert_se(fd_is_mount_point(fd, "version", 0) == 0);
312
313 safe_close(fd);
314 fd = open("/proc/version", O_RDONLY|O_CLOEXEC|O_NOCTTY);
315 assert_se(fd >= 0);
316
317 r = fd_is_mount_point(fd, NULL, 0);
318 assert_se(IN_SET(r, 0, -ENOTDIR)); /* on old kernels we can't determine if regular files are mount points if we have no directory fd */
319 assert_se(fd_is_mount_point(fd, "", 0) == -EINVAL);
320 }
321
322 TEST(ms_nosymfollow_supported) {
323 log_info("MS_NOSYMFOLLOW supported: %s", yes_no(ms_nosymfollow_supported()));
324 }
325
326 TEST(mount_option_supported) {
327 int r;
328
329 r = mount_option_supported("tmpfs", "size", "64M");
330 log_info("tmpfs supports size=64M: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
331 assert_se(r > 0 || r == -EAGAIN || ERRNO_IS_NEG_PRIVILEGE(r));
332
333 r = mount_option_supported("ext4", "discard", NULL);
334 log_info("ext4 supports discard: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
335 assert_se(r > 0 || r == -EAGAIN || ERRNO_IS_NEG_PRIVILEGE(r));
336
337 r = mount_option_supported("tmpfs", "idontexist", "64M");
338 log_info("tmpfs supports idontexist: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
339 assert_se(IN_SET(r, 0, -EAGAIN) || ERRNO_IS_NEG_PRIVILEGE(r));
340
341 r = mount_option_supported("tmpfs", "ialsodontexist", NULL);
342 log_info("tmpfs supports ialsodontexist: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
343 assert_se(IN_SET(r, 0, -EAGAIN) || ERRNO_IS_NEG_PRIVILEGE(r));
344
345 r = mount_option_supported("proc", "hidepid", "1");
346 log_info("proc supports hidepid=1: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
347 assert_se(r >= 0 || r == -EAGAIN || ERRNO_IS_NEG_PRIVILEGE(r));
348 }
349
350 TEST(fstype_can_discard) {
351 assert_se(fstype_can_discard("ext4"));
352 assert_se(!fstype_can_discard("squashfs"));
353 assert_se(!fstype_can_discard("iso9660"));
354 }
355
356 TEST(fstype_can_norecovery) {
357 assert_se(fstype_can_norecovery("ext4"));
358 assert_se(!fstype_can_norecovery("vfat"));
359 assert_se(!fstype_can_norecovery("tmpfs"));
360 }
361
362 TEST(fstype_can_umask) {
363 assert_se(fstype_can_umask("vfat"));
364 assert_se(!fstype_can_umask("tmpfs"));
365 }
366
367 TEST(path_get_mnt_id_at_null) {
368 _cleanup_close_ int root_fd = -EBADF, run_fd = -EBADF;
369 int id1, id2;
370
371 assert_se(path_get_mnt_id_at(AT_FDCWD, "/run/", &id1) >= 0);
372 assert_se(id1 > 0);
373
374 assert_se(path_get_mnt_id_at(AT_FDCWD, "/run", &id2) >= 0);
375 assert_se(id1 == id2);
376 id2 = -1;
377
378 root_fd = open("/", O_DIRECTORY|O_CLOEXEC);
379 assert_se(root_fd >= 0);
380
381 assert_se(path_get_mnt_id_at(root_fd, "/run/", &id2) >= 0);
382 assert_se(id1 = id2);
383 id2 = -1;
384
385 assert_se(path_get_mnt_id_at(root_fd, "/run", &id2) >= 0);
386 assert_se(id1 = id2);
387 id2 = -1;
388
389 assert_se(path_get_mnt_id_at(root_fd, "run", &id2) >= 0);
390 assert_se(id1 = id2);
391 id2 = -1;
392
393 assert_se(path_get_mnt_id_at(root_fd, "run/", &id2) >= 0);
394 assert_se(id1 = id2);
395 id2 = -1;
396
397 run_fd = openat(root_fd, "run", O_DIRECTORY|O_CLOEXEC);
398 assert_se(run_fd >= 0);
399
400 id2 = -1;
401 assert_se(path_get_mnt_id_at(run_fd, "", &id2) >= 0);
402 assert_se(id1 = id2);
403 id2 = -1;
404
405 assert_se(path_get_mnt_id_at(run_fd, NULL, &id2) >= 0);
406 assert_se(id1 = id2);
407 id2 = -1;
408
409 assert_se(path_get_mnt_id_at(run_fd, ".", &id2) >= 0);
410 assert_se(id1 = id2);
411 id2 = -1;
412 }
413
414 static int intro(void) {
415 /* let's move into our own mount namespace with all propagation from the host turned off, so
416 * that /proc/self/mountinfo is static and constant for the whole time our test runs. */
417
418 if (unshare(CLONE_NEWNS) < 0) {
419 if (!ERRNO_IS_PRIVILEGE(errno))
420 return log_error_errno(errno, "Failed to detach mount namespace: %m");
421
422 log_notice("Lacking privilege to create separate mount namespace, proceeding in originating mount namespace.");
423 } else
424 assert_se(mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL) >= 0);
425
426 return EXIT_SUCCESS;
427 }
428
429 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);