1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "alloc-util.h"
14 #include "mountpoint-util.h"
15 #include "path-util.h"
17 #include "stat-util.h"
18 #include "string-util.h"
20 #include "tmpfile-util.h"
22 static void test_mount_propagation_flag_one(const char *name
, int ret
, unsigned long expected
) {
25 log_info("/* %s(%s) */", __func__
, strnull(name
));
27 assert_se(mount_propagation_flag_from_string(name
, &flags
) == ret
);
32 assert_se(flags
== expected
);
34 c
= mount_propagation_flag_to_string(flags
);
36 assert_se(isempty(c
));
38 ASSERT_STREQ(c
, name
);
42 TEST(mount_propagation_flag
) {
43 test_mount_propagation_flag_one("shared", 0, MS_SHARED
);
44 test_mount_propagation_flag_one("slave", 0, MS_SLAVE
);
45 test_mount_propagation_flag_one("private", 0, MS_PRIVATE
);
46 test_mount_propagation_flag_one(NULL
, 0, 0);
47 test_mount_propagation_flag_one("", 0, 0);
48 test_mount_propagation_flag_one("xxxx", -EINVAL
, 0);
49 test_mount_propagation_flag_one(" ", -EINVAL
, 0);
53 _cleanup_fclose_
FILE *f
= NULL
;
54 _cleanup_hashmap_free_ Hashmap
*h
= NULL
;
59 assert_se(f
= fopen("/proc/self/mountinfo", "re"));
60 assert_se(h
= hashmap_new(&trivial_hash_ops_value_free
));
63 _cleanup_free_
char *line
= NULL
, *path
= NULL
;
66 r
= read_line(f
, LONG_LINE_MAX
, &line
);
71 assert_se(sscanf(line
, "%i %*s %*s %*s %ms", &mnt_id
, &path
) == 2);
72 #if HAS_FEATURE_MEMORY_SANITIZER
73 /* We don't know the length of the string, so we need to unpoison it one char at a time */
74 for (const char *c
= path
; ; c
++) {
80 log_debug("mountinfo: %s → %i", path
, mnt_id
);
82 assert_se(hashmap_put(h
, INT_TO_PTR(mnt_id
), path
) >= 0);
86 HASHMAP_FOREACH_KEY(p
, k
, h
) {
87 int mnt_id
= PTR_TO_INT(k
), mnt_id2
;
90 r
= path_get_mnt_id(p
, &mnt_id2
);
92 log_debug_errno(r
, "Failed to get the mnt id of %s: %m", p
);
96 if (mnt_id
== mnt_id2
) {
97 log_debug("mnt ids of %s is %i.", p
, mnt_id
);
100 log_debug("mnt ids of %s are %i (from /proc/self/mountinfo), %i (from path_get_mnt_id()).", p
, mnt_id
, mnt_id2
);
102 /* The ids don't match? This can easily happen e.g. running with "unshare --mount-proc".
104 assert_se(q
= hashmap_get(h
, INT_TO_PTR(mnt_id2
)));
106 assert_se((r
= path_is_mount_point_full(p
, NULL
, 0)) >= 0);
108 /* If the path is not a mount point anymore, then it must be a sub directory of
109 * the path corresponds to mnt_id2. */
110 log_debug("The path %s for mnt id %i is not a mount point.", p
, mnt_id2
);
111 assert_se(!isempty(path_startswith(p
, q
)));
113 /* If the path is still a mount point, then it must be equivalent to the path
114 * corresponds to mnt_id2 */
115 log_debug("There are multiple mounts on the same path %s.", p
);
116 assert_se(path_equal(p
, q
));
121 TEST(path_is_mount_point
) {
123 char tmp_dir
[] = "/tmp/test-path-is-mount-point-XXXXXX";
124 _cleanup_free_
char *file1
= NULL
, *file2
= NULL
, *link1
= NULL
, *link2
= NULL
;
125 _cleanup_free_
char *dir1
= NULL
, *dir1file
= NULL
, *dirlink1
= NULL
, *dirlink1file
= NULL
;
126 _cleanup_free_
char *dir2
= NULL
, *dir2file
= NULL
;
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 assert_se(path_is_mount_point_full("//", NULL
, AT_SYMLINK_FOLLOW
) > 0);
131 assert_se(path_is_mount_point_full("//", 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 assert_se(path_is_mount_point_full("/proc/", NULL
, AT_SYMLINK_FOLLOW
) > 0);
136 assert_se(path_is_mount_point_full("/proc/", 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 assert_se(path_is_mount_point_full("/proc/1/", NULL
, AT_SYMLINK_FOLLOW
) == 0);
141 assert_se(path_is_mount_point_full("/proc/1/", NULL
, 0) == 0);
143 /* we'll create a hierarchy of different kinds of dir/file/link
146 * <tmp>/file1, <tmp>/file2
147 * <tmp>/link1 -> file1, <tmp>/link2 -> file2
150 * <tmp>/dirlink1 -> dir1
151 * <tmp>/dirlink1file -> dirlink1/file
156 /* file mountpoints */
157 ASSERT_NOT_NULL(mkdtemp(tmp_dir
));
158 file1
= path_join(tmp_dir
, "file1");
160 file2
= path_join(tmp_dir
, "file2");
162 fd
= open(file1
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
, 0664);
165 fd
= open(file2
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
, 0664);
168 link1
= path_join(tmp_dir
, "link1");
170 assert_se(symlink("file1", link1
) == 0);
171 link2
= path_join(tmp_dir
, "link2");
173 assert_se(symlink("file2", link2
) == 0);
175 assert_se(path_is_mount_point_full(file1
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
176 assert_se(path_is_mount_point_full(file1
, NULL
, 0) == 0);
177 assert_se(path_is_mount_point_full(link1
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
178 assert_se(path_is_mount_point_full(link1
, NULL
, 0) == 0);
180 /* directory mountpoints */
181 dir1
= path_join(tmp_dir
, "dir1");
183 assert_se(mkdir(dir1
, 0755) == 0);
184 dirlink1
= path_join(tmp_dir
, "dirlink1");
186 assert_se(symlink("dir1", dirlink1
) == 0);
187 dirlink1file
= path_join(tmp_dir
, "dirlink1file");
188 assert_se(dirlink1file
);
189 assert_se(symlink("dirlink1/file", dirlink1file
) == 0);
190 dir2
= path_join(tmp_dir
, "dir2");
192 assert_se(mkdir(dir2
, 0755) == 0);
194 assert_se(path_is_mount_point_full(dir1
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
195 assert_se(path_is_mount_point_full(dir1
, NULL
, 0) == 0);
196 assert_se(path_is_mount_point_full(dirlink1
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
197 assert_se(path_is_mount_point_full(dirlink1
, NULL
, 0) == 0);
199 /* file in subdirectory mountpoints */
200 dir1file
= path_join(dir1
, "file");
202 fd
= open(dir1file
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
, 0664);
206 assert_se(path_is_mount_point_full(dir1file
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
207 assert_se(path_is_mount_point_full(dir1file
, NULL
, 0) == 0);
208 assert_se(path_is_mount_point_full(dirlink1file
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
209 assert_se(path_is_mount_point_full(dirlink1file
, NULL
, 0) == 0);
211 /* these tests will only work as root */
212 if (mount(file1
, file2
, NULL
, MS_BIND
, NULL
) >= 0) {
213 int rf
, rt
, rdf
, rdt
, rlf
, rlt
, rl1f
, rl1t
;
217 /* capture results in vars, to avoid dangling mounts on failure */
218 log_info("%s: %s", __func__
, file2
);
219 rf
= path_is_mount_point_full(file2
, NULL
, 0);
220 rt
= path_is_mount_point_full(file2
, NULL
, AT_SYMLINK_FOLLOW
);
222 file2d
= strjoina(file2
, "/");
223 log_info("%s: %s", __func__
, file2d
);
224 rdf
= path_is_mount_point_full(file2d
, NULL
, 0);
225 rdt
= path_is_mount_point_full(file2d
, NULL
, AT_SYMLINK_FOLLOW
);
227 log_info("%s: %s", __func__
, link2
);
228 rlf
= path_is_mount_point_full(link2
, NULL
, 0);
229 rlt
= path_is_mount_point_full(link2
, NULL
, AT_SYMLINK_FOLLOW
);
231 assert_se(umount(file2
) == 0);
235 assert_se(rdf
== -ENOTDIR
);
236 assert_se(rdt
== -ENOTDIR
);
241 dir2file
= path_join(dir2
, "file");
243 fd
= open(dir2file
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
, 0664);
247 assert_se(mount(dir2
, dir1
, NULL
, MS_BIND
, NULL
) >= 0);
249 log_info("%s: %s", __func__
, dir1
);
250 rf
= path_is_mount_point_full(dir1
, NULL
, 0);
251 rt
= path_is_mount_point_full(dir1
, NULL
, AT_SYMLINK_FOLLOW
);
252 log_info("%s: %s", __func__
, dirlink1
);
253 rlf
= path_is_mount_point_full(dirlink1
, NULL
, 0);
254 rlt
= path_is_mount_point_full(dirlink1
, NULL
, AT_SYMLINK_FOLLOW
);
255 log_info("%s: %s", __func__
, dirlink1file
);
256 /* its parent is a mount point, but not /file itself */
257 rl1f
= path_is_mount_point_full(dirlink1file
, NULL
, 0);
258 rl1t
= path_is_mount_point_full(dirlink1file
, NULL
, AT_SYMLINK_FOLLOW
);
260 assert_se(umount(dir1
) == 0);
266 assert_se(rl1f
== 0);
267 assert_se(rl1t
== 0);
270 log_info("Skipping bind mount file test");
272 assert_se(rm_rf(tmp_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
275 TEST(is_mount_point_at
) {
276 _cleanup_(rm_rf_physical_and_freep
) char *tmpdir
= NULL
;
277 _cleanup_free_
char *pwd
= NULL
, *tmpdir_basename
= NULL
;
278 _cleanup_close_
int fd
= -EBADF
;
281 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOCTTY
);
284 /* Not allowed, since "/" is a path, not a plain filename */
285 assert_se(is_mount_point_at(fd
, "/", 0) == -EINVAL
);
286 assert_se(is_mount_point_at(fd
, "..", 0) == -EINVAL
);
287 assert_se(is_mount_point_at(fd
, "../", 0) == -EINVAL
);
288 assert_se(is_mount_point_at(fd
, "/proc", 0) == -EINVAL
);
289 assert_se(is_mount_point_at(fd
, "/proc/", 0) == -EINVAL
);
290 assert_se(is_mount_point_at(fd
, "proc/sys", 0) == -EINVAL
);
291 assert_se(is_mount_point_at(fd
, "proc/sys/", 0) == -EINVAL
);
293 /* This one definitely is a mount point */
294 assert_se(is_mount_point_at(fd
, "proc", 0) > 0);
295 assert_se(is_mount_point_at(fd
, "proc/", 0) > 0);
298 fd
= open("/tmp", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOCTTY
);
301 assert_se(mkdtemp_malloc("/tmp/not-mounted-XXXXXX", &tmpdir
) >= 0);
302 ASSERT_OK(path_extract_filename(tmpdir
, &tmpdir_basename
));
303 ASSERT_OK_ZERO(is_mount_point_at(fd
, tmpdir_basename
, 0));
304 ASSERT_OK_ZERO(is_mount_point_at(fd
, strjoina(tmpdir_basename
, "/"), 0));
307 fd
= open("/proc", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOCTTY
);
310 ASSERT_OK_POSITIVE(is_mount_point_at(fd
, NULL
, 0));
311 ASSERT_OK_POSITIVE(is_mount_point_at(fd
, "", 0));
312 ASSERT_OK_POSITIVE(is_mount_point_at(fd
, ".", 0));
313 ASSERT_OK_POSITIVE(is_mount_point_at(fd
, "./", 0));
314 ASSERT_OK_ZERO(is_mount_point_at(fd
, "version", 0));
316 ASSERT_OK(safe_getcwd(&pwd
));
317 ASSERT_OK_ERRNO(fchdir(fd
));
319 ASSERT_OK_POSITIVE(is_mount_point_at(AT_FDCWD
, NULL
, 0));
320 ASSERT_OK_POSITIVE(is_mount_point_at(AT_FDCWD
, "", 0));
321 ASSERT_OK_POSITIVE(is_mount_point_at(AT_FDCWD
, "./", 0));
323 ASSERT_OK_ERRNO(chdir(pwd
));
326 fd
= open("/proc/version", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
329 r
= is_mount_point_at(fd
, NULL
, 0);
330 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 */
332 if (!mount_new_api_supported())
335 /* Symlinks can be mount points with new mount API */
336 _cleanup_close_
int mfd
= -EBADF
, rfd
= -EBADF
;
337 _cleanup_free_
char *t
= NULL
;
341 ASSERT_OK_ERRNO(fd
= open(tmpdir
, O_DIRECTORY
|O_PATH
|O_CLOEXEC
));
343 ASSERT_OK_ERRNO(symlinkat("/usr", fd
, "symlink"));
345 mfd
= open_tree(fd
, "symlink", AT_SYMLINK_NOFOLLOW
|OPEN_TREE_CLONE
|OPEN_TREE_CLOEXEC
);
346 if (mfd
< 0 && ERRNO_IS_PRIVILEGE(errno
))
348 ASSERT_OK_ERRNO(mfd
);
350 ASSERT_OK_ERRNO(rfd
= openat(fd
, "regular", O_CLOEXEC
|O_CREAT
|O_EXCL
, 0644));
352 ASSERT_OK_ERRNO(move_mount(mfd
, "", rfd
, "", MOVE_MOUNT_F_EMPTY_PATH
|MOVE_MOUNT_T_EMPTY_PATH
));
354 ASSERT_OK_ERRNO(fstatat(fd
, "regular", &st
, AT_SYMLINK_NOFOLLOW
));
355 ASSERT_OK(stat_verify_symlink(&st
));
356 ASSERT_OK(readlinkat_malloc(fd
, "regular", &t
));
357 ASSERT_STREQ(t
, "/usr");
359 ASSERT_OK(is_mount_point_at(fd
, "regular", 0));
362 TEST(ms_nosymfollow_supported
) {
363 log_info("MS_NOSYMFOLLOW supported: %s", yes_no(ms_nosymfollow_supported()));
366 TEST(mount_option_supported
) {
369 r
= mount_option_supported("tmpfs", "size", "64M");
370 log_info("tmpfs supports size=64M: %s (%i)", r
< 0 ? "don't know" : yes_no(r
), r
);
371 assert_se(r
> 0 || r
== -EAGAIN
|| ERRNO_IS_NEG_PRIVILEGE(r
));
373 r
= mount_option_supported("ext4", "discard", NULL
);
374 log_info("ext4 supports discard: %s (%i)", r
< 0 ? "don't know" : yes_no(r
), r
);
375 assert_se(r
> 0 || r
== -EAGAIN
|| ERRNO_IS_NEG_PRIVILEGE(r
));
377 r
= mount_option_supported("tmpfs", "idontexist", "64M");
378 log_info("tmpfs supports idontexist: %s (%i)", r
< 0 ? "don't know" : yes_no(r
), r
);
379 assert_se(IN_SET(r
, 0, -EAGAIN
) || ERRNO_IS_NEG_PRIVILEGE(r
));
381 r
= mount_option_supported("tmpfs", "ialsodontexist", NULL
);
382 log_info("tmpfs supports ialsodontexist: %s (%i)", r
< 0 ? "don't know" : yes_no(r
), r
);
383 assert_se(IN_SET(r
, 0, -EAGAIN
) || ERRNO_IS_NEG_PRIVILEGE(r
));
385 r
= mount_option_supported("proc", "hidepid", "1");
386 log_info("proc supports hidepid=1: %s (%i)", r
< 0 ? "don't know" : yes_no(r
), r
);
387 assert_se(r
>= 0 || r
== -EAGAIN
|| ERRNO_IS_NEG_PRIVILEGE(r
));
390 TEST(fstype_can_discard
) {
391 assert_se(fstype_can_discard("ext4"));
392 assert_se(!fstype_can_discard("squashfs"));
393 assert_se(!fstype_can_discard("iso9660"));
396 TEST(fstype_can_norecovery
) {
397 ASSERT_STREQ(fstype_norecovery_option("ext4"), "norecovery");
398 ASSERT_NULL(fstype_norecovery_option("vfat"));
399 ASSERT_NULL(fstype_norecovery_option("tmpfs"));
402 TEST(fstype_can_fmask_dmask
) {
403 assert_se(fstype_can_fmask_dmask("vfat"));
404 assert_se(!fstype_can_fmask_dmask("tmpfs"));
407 TEST(path_get_mnt_id_at_null
) {
408 _cleanup_close_
int root_fd
= -EBADF
, run_fd
= -EBADF
;
411 assert_se(path_get_mnt_id_at(AT_FDCWD
, "/run/", &id1
) >= 0);
414 assert_se(path_get_mnt_id_at(AT_FDCWD
, "/run", &id2
) >= 0);
415 assert_se(id1
== id2
);
418 root_fd
= open("/", O_DIRECTORY
|O_CLOEXEC
);
419 assert_se(root_fd
>= 0);
421 assert_se(path_get_mnt_id_at(root_fd
, "/run/", &id2
) >= 0);
422 assert_se(id1
= id2
);
425 assert_se(path_get_mnt_id_at(root_fd
, "/run", &id2
) >= 0);
426 assert_se(id1
= id2
);
429 assert_se(path_get_mnt_id_at(root_fd
, "run", &id2
) >= 0);
430 assert_se(id1
= id2
);
433 assert_se(path_get_mnt_id_at(root_fd
, "run/", &id2
) >= 0);
434 assert_se(id1
= id2
);
437 run_fd
= openat(root_fd
, "run", O_DIRECTORY
|O_CLOEXEC
);
438 assert_se(run_fd
>= 0);
441 assert_se(path_get_mnt_id_at(run_fd
, "", &id2
) >= 0);
442 assert_se(id1
= id2
);
445 assert_se(path_get_mnt_id_at(run_fd
, NULL
, &id2
) >= 0);
446 assert_se(id1
= id2
);
449 assert_se(path_get_mnt_id_at(run_fd
, ".", &id2
) >= 0);
450 assert_se(id1
= id2
);
454 static int intro(void) {
455 /* let's move into our own mount namespace with all propagation from the host turned off, so
456 * that /proc/self/mountinfo is static and constant for the whole time our test runs. */
458 if (unshare(CLONE_NEWNS
) < 0) {
459 if (!ERRNO_IS_PRIVILEGE(errno
))
460 return log_error_errno(errno
, "Failed to detach mount namespace: %m");
462 log_notice("Lacking privilege to create separate mount namespace, proceeding in originating mount namespace.");
464 assert_se(mount(NULL
, "/", NULL
, MS_PRIVATE
| MS_REC
, NULL
) >= 0);
469 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG
, intro
);