1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "alloc-util.h"
13 #include "mountpoint-util.h"
14 #include "path-util.h"
16 #include "string-util.h"
18 #include "tmpfile-util.h"
20 static void test_mount_propagation_flag_one(const char *name
, int ret
, unsigned long expected
) {
23 log_info("/* %s(%s) */", __func__
, strnull(name
));
25 assert_se(mount_propagation_flag_from_string(name
, &flags
) == ret
);
30 assert_se(flags
== expected
);
32 c
= mount_propagation_flag_to_string(flags
);
34 assert_se(isempty(c
));
36 ASSERT_STREQ(c
, name
);
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);
51 _cleanup_fclose_
FILE *f
= NULL
;
52 _cleanup_hashmap_free_free_ Hashmap
*h
= NULL
;
57 assert_se(f
= fopen("/proc/self/mountinfo", "re"));
58 assert_se(h
= hashmap_new(&trivial_hash_ops
));
61 _cleanup_free_
char *line
= NULL
, *path
= NULL
;
64 r
= read_line(f
, LONG_LINE_MAX
, &line
);
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
++) {
78 log_debug("mountinfo: %s → %i", path
, mnt_id
);
80 assert_se(hashmap_put(h
, INT_TO_PTR(mnt_id
), path
) >= 0);
84 HASHMAP_FOREACH_KEY(p
, k
, h
) {
85 int mnt_id
= PTR_TO_INT(k
), mnt_id2
;
88 r
= path_get_mnt_id(p
, &mnt_id2
);
90 log_debug_errno(r
, "Failed to get the mnt id of %s: %m", p
);
94 if (mnt_id
== mnt_id2
) {
95 log_debug("mnt ids of %s is %i.", p
, mnt_id
);
98 log_debug("mnt ids of %s are %i (from /proc/self/mountinfo), %i (from path_get_mnt_id()).", p
, mnt_id
, mnt_id2
);
100 /* The ids don't match? This can easily happen e.g. running with "unshare --mount-proc".
102 assert_se(q
= hashmap_get(h
, INT_TO_PTR(mnt_id2
)));
104 assert_se((r
= path_is_mount_point_full(p
, NULL
, 0)) >= 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
)));
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
));
119 TEST(path_is_mount_point
) {
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
;
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);
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);
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);
141 /* we'll create a hierarchy of different kinds of dir/file/link
144 * <tmp>/file1, <tmp>/file2
145 * <tmp>/link1 -> file1, <tmp>/link2 -> file2
148 * <tmp>/dirlink1 -> dir1
149 * <tmp>/dirlink1file -> dirlink1/file
154 /* file mountpoints */
155 ASSERT_NOT_NULL(mkdtemp(tmp_dir
));
156 file1
= path_join(tmp_dir
, "file1");
158 file2
= path_join(tmp_dir
, "file2");
160 fd
= open(file1
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
, 0664);
163 fd
= open(file2
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
, 0664);
166 link1
= path_join(tmp_dir
, "link1");
168 assert_se(symlink("file1", link1
) == 0);
169 link2
= path_join(tmp_dir
, "link2");
171 assert_se(symlink("file2", link2
) == 0);
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);
178 /* directory mountpoints */
179 dir1
= path_join(tmp_dir
, "dir1");
181 assert_se(mkdir(dir1
, 0755) == 0);
182 dirlink1
= path_join(tmp_dir
, "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");
190 assert_se(mkdir(dir2
, 0755) == 0);
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);
197 /* file in subdirectory mountpoints */
198 dir1file
= path_join(dir1
, "file");
200 fd
= open(dir1file
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
, 0664);
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);
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
;
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
);
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
);
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
);
229 assert_se(umount(file2
) == 0);
233 assert_se(rdf
== -ENOTDIR
);
234 assert_se(rdt
== -ENOTDIR
);
239 dir2file
= path_join(dir2
, "file");
241 fd
= open(dir2file
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
, 0664);
245 assert_se(mount(dir2
, dir1
, NULL
, MS_BIND
, NULL
) >= 0);
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
);
258 assert_se(umount(dir1
) == 0);
264 assert_se(rl1f
== 0);
265 assert_se(rl1t
== 0);
268 log_info("Skipping bind mount file test");
270 assert_se(rm_rf(tmp_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
273 TEST(fd_is_mount_point
) {
274 _cleanup_(rm_rf_physical_and_freep
) char *tmpdir
= NULL
;
275 _cleanup_close_
int fd
= -EBADF
;
278 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOCTTY
);
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
);
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);
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_se(fd_is_mount_point(fd
, basename(tmpdir
), 0) == 0);
303 assert_se(fd_is_mount_point(fd
, strjoina(basename(tmpdir
), "/"), 0) == 0);
306 fd
= open("/proc", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOCTTY
);
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);
314 fd
= open("/proc/version", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
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
);
322 TEST(ms_nosymfollow_supported
) {
323 log_info("MS_NOSYMFOLLOW supported: %s", yes_no(ms_nosymfollow_supported()));
326 TEST(mount_option_supported
) {
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
));
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
));
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
));
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
));
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
));
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"));
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"));
362 TEST(fstype_can_umask
) {
363 assert_se(fstype_can_umask("vfat"));
364 assert_se(!fstype_can_umask("tmpfs"));
367 TEST(path_get_mnt_id_at_null
) {
368 _cleanup_close_
int root_fd
= -EBADF
, run_fd
= -EBADF
;
371 assert_se(path_get_mnt_id_at(AT_FDCWD
, "/run/", &id1
) >= 0);
374 assert_se(path_get_mnt_id_at(AT_FDCWD
, "/run", &id2
) >= 0);
375 assert_se(id1
== id2
);
378 root_fd
= open("/", O_DIRECTORY
|O_CLOEXEC
);
379 assert_se(root_fd
>= 0);
381 assert_se(path_get_mnt_id_at(root_fd
, "/run/", &id2
) >= 0);
382 assert_se(id1
= id2
);
385 assert_se(path_get_mnt_id_at(root_fd
, "/run", &id2
) >= 0);
386 assert_se(id1
= id2
);
389 assert_se(path_get_mnt_id_at(root_fd
, "run", &id2
) >= 0);
390 assert_se(id1
= id2
);
393 assert_se(path_get_mnt_id_at(root_fd
, "run/", &id2
) >= 0);
394 assert_se(id1
= id2
);
397 run_fd
= openat(root_fd
, "run", O_DIRECTORY
|O_CLOEXEC
);
398 assert_se(run_fd
>= 0);
401 assert_se(path_get_mnt_id_at(run_fd
, "", &id2
) >= 0);
402 assert_se(id1
= id2
);
405 assert_se(path_get_mnt_id_at(run_fd
, NULL
, &id2
) >= 0);
406 assert_se(id1
= id2
);
409 assert_se(path_get_mnt_id_at(run_fd
, ".", &id2
) >= 0);
410 assert_se(id1
= id2
);
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. */
418 if (unshare(CLONE_NEWNS
) < 0) {
419 if (!ERRNO_IS_PRIVILEGE(errno
))
420 return log_error_errno(errno
, "Failed to detach mount namespace: %m");
422 log_notice("Lacking privilege to create separate mount namespace, proceeding in originating mount namespace.");
424 assert_se(mount(NULL
, "/", NULL
, MS_PRIVATE
| MS_REC
, NULL
) >= 0);
429 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG
, intro
);