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_se(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(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("/", NULL
, AT_SYMLINK_FOLLOW
) > 0);
127 assert_se(path_is_mount_point("/", NULL
, 0) > 0);
128 assert_se(path_is_mount_point("//", NULL
, AT_SYMLINK_FOLLOW
) > 0);
129 assert_se(path_is_mount_point("//", NULL
, 0) > 0);
131 assert_se(path_is_mount_point("/proc", NULL
, AT_SYMLINK_FOLLOW
) > 0);
132 assert_se(path_is_mount_point("/proc", NULL
, 0) > 0);
133 assert_se(path_is_mount_point("/proc/", NULL
, AT_SYMLINK_FOLLOW
) > 0);
134 assert_se(path_is_mount_point("/proc/", NULL
, 0) > 0);
136 assert_se(path_is_mount_point("/proc/1", NULL
, AT_SYMLINK_FOLLOW
) == 0);
137 assert_se(path_is_mount_point("/proc/1", NULL
, 0) == 0);
138 assert_se(path_is_mount_point("/proc/1/", NULL
, AT_SYMLINK_FOLLOW
) == 0);
139 assert_se(path_is_mount_point("/proc/1/", NULL
, 0) == 0);
141 assert_se(path_is_mount_point("/sys", NULL
, AT_SYMLINK_FOLLOW
) > 0);
142 assert_se(path_is_mount_point("/sys", NULL
, 0) > 0);
143 assert_se(path_is_mount_point("/sys/", NULL
, AT_SYMLINK_FOLLOW
) > 0);
144 assert_se(path_is_mount_point("/sys/", NULL
, 0) > 0);
146 /* we'll create a hierarchy of different kinds of dir/file/link
149 * <tmp>/file1, <tmp>/file2
150 * <tmp>/link1 -> file1, <tmp>/link2 -> file2
153 * <tmp>/dirlink1 -> dir1
154 * <tmp>/dirlink1file -> dirlink1/file
159 /* file mountpoints */
160 assert_se(mkdtemp(tmp_dir
) != NULL
);
161 file1
= path_join(tmp_dir
, "file1");
163 file2
= path_join(tmp_dir
, "file2");
165 fd
= open(file1
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
, 0664);
168 fd
= open(file2
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
, 0664);
171 link1
= path_join(tmp_dir
, "link1");
173 assert_se(symlink("file1", link1
) == 0);
174 link2
= path_join(tmp_dir
, "link2");
176 assert_se(symlink("file2", link2
) == 0);
178 assert_se(path_is_mount_point(file1
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
179 assert_se(path_is_mount_point(file1
, NULL
, 0) == 0);
180 assert_se(path_is_mount_point(link1
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
181 assert_se(path_is_mount_point(link1
, NULL
, 0) == 0);
183 /* directory mountpoints */
184 dir1
= path_join(tmp_dir
, "dir1");
186 assert_se(mkdir(dir1
, 0755) == 0);
187 dirlink1
= path_join(tmp_dir
, "dirlink1");
189 assert_se(symlink("dir1", dirlink1
) == 0);
190 dirlink1file
= path_join(tmp_dir
, "dirlink1file");
191 assert_se(dirlink1file
);
192 assert_se(symlink("dirlink1/file", dirlink1file
) == 0);
193 dir2
= path_join(tmp_dir
, "dir2");
195 assert_se(mkdir(dir2
, 0755) == 0);
197 assert_se(path_is_mount_point(dir1
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
198 assert_se(path_is_mount_point(dir1
, NULL
, 0) == 0);
199 assert_se(path_is_mount_point(dirlink1
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
200 assert_se(path_is_mount_point(dirlink1
, NULL
, 0) == 0);
202 /* file in subdirectory mountpoints */
203 dir1file
= path_join(dir1
, "file");
205 fd
= open(dir1file
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
, 0664);
209 assert_se(path_is_mount_point(dir1file
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
210 assert_se(path_is_mount_point(dir1file
, NULL
, 0) == 0);
211 assert_se(path_is_mount_point(dirlink1file
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
212 assert_se(path_is_mount_point(dirlink1file
, NULL
, 0) == 0);
214 /* these tests will only work as root */
215 if (mount(file1
, file2
, NULL
, MS_BIND
, NULL
) >= 0) {
216 int rf
, rt
, rdf
, rdt
, rlf
, rlt
, rl1f
, rl1t
;
220 /* capture results in vars, to avoid dangling mounts on failure */
221 log_info("%s: %s", __func__
, file2
);
222 rf
= path_is_mount_point(file2
, NULL
, 0);
223 rt
= path_is_mount_point(file2
, NULL
, AT_SYMLINK_FOLLOW
);
225 file2d
= strjoina(file2
, "/");
226 log_info("%s: %s", __func__
, file2d
);
227 rdf
= path_is_mount_point(file2d
, NULL
, 0);
228 rdt
= path_is_mount_point(file2d
, NULL
, AT_SYMLINK_FOLLOW
);
230 log_info("%s: %s", __func__
, link2
);
231 rlf
= path_is_mount_point(link2
, NULL
, 0);
232 rlt
= path_is_mount_point(link2
, NULL
, AT_SYMLINK_FOLLOW
);
234 assert_se(umount(file2
) == 0);
238 assert_se(rdf
== -ENOTDIR
);
239 assert_se(rdt
== -ENOTDIR
);
244 dir2file
= path_join(dir2
, "file");
246 fd
= open(dir2file
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
, 0664);
250 assert_se(mount(dir2
, dir1
, NULL
, MS_BIND
, NULL
) >= 0);
252 log_info("%s: %s", __func__
, dir1
);
253 rf
= path_is_mount_point(dir1
, NULL
, 0);
254 rt
= path_is_mount_point(dir1
, NULL
, AT_SYMLINK_FOLLOW
);
255 log_info("%s: %s", __func__
, dirlink1
);
256 rlf
= path_is_mount_point(dirlink1
, NULL
, 0);
257 rlt
= path_is_mount_point(dirlink1
, NULL
, AT_SYMLINK_FOLLOW
);
258 log_info("%s: %s", __func__
, dirlink1file
);
259 /* its parent is a mount point, but not /file itself */
260 rl1f
= path_is_mount_point(dirlink1file
, NULL
, 0);
261 rl1t
= path_is_mount_point(dirlink1file
, NULL
, AT_SYMLINK_FOLLOW
);
263 assert_se(umount(dir1
) == 0);
269 assert_se(rl1f
== 0);
270 assert_se(rl1t
== 0);
273 log_info("Skipping bind mount file test");
275 assert_se(rm_rf(tmp_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
278 TEST(fd_is_mount_point
) {
279 _cleanup_close_
int fd
= -EBADF
;
282 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOCTTY
);
285 /* Not allowed, since "/" is a path, not a plain filename */
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
, "./", 0) == -EINVAL
);
289 assert_se(fd_is_mount_point(fd
, "..", 0) == -EINVAL
);
290 assert_se(fd_is_mount_point(fd
, "../", 0) == -EINVAL
);
291 assert_se(fd_is_mount_point(fd
, "", 0) == -EINVAL
);
292 assert_se(fd_is_mount_point(fd
, "/proc", 0) == -EINVAL
);
293 assert_se(fd_is_mount_point(fd
, "/proc/", 0) == -EINVAL
);
294 assert_se(fd_is_mount_point(fd
, "proc/sys", 0) == -EINVAL
);
295 assert_se(fd_is_mount_point(fd
, "proc/sys/", 0) == -EINVAL
);
297 /* This one definitely is a mount point */
298 assert_se(fd_is_mount_point(fd
, "proc", 0) > 0);
299 assert_se(fd_is_mount_point(fd
, "proc/", 0) > 0);
301 /* /root's entire reason for being is to be on the root file system (i.e. not in /home/ which
302 * might be split off), so that the user can always log in, so it cannot be a mount point unless
303 * the system is borked. Let's allow for it to be missing though. */
304 assert_se(IN_SET(fd_is_mount_point(fd
, "root", 0), -ENOENT
, 0));
305 assert_se(IN_SET(fd_is_mount_point(fd
, "root/", 0), -ENOENT
, 0));
308 fd
= open("/proc", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOCTTY
);
311 assert_se(fd_is_mount_point(fd
, NULL
, 0) > 0);
312 assert_se(fd_is_mount_point(fd
, "", 0) == -EINVAL
);
313 assert_se(fd_is_mount_point(fd
, "version", 0) == 0);
316 fd
= open("/proc/version", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
319 r
= fd_is_mount_point(fd
, NULL
, 0);
320 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 */
321 assert_se(fd_is_mount_point(fd
, "", 0) == -EINVAL
);
324 static int intro(void) {
325 /* let's move into our own mount namespace with all propagation from the host turned off, so
326 * that /proc/self/mountinfo is static and constant for the whole time our test runs. */
328 if (unshare(CLONE_NEWNS
) < 0) {
329 if (!ERRNO_IS_PRIVILEGE(errno
))
330 return log_error_errno(errno
, "Failed to detach mount namespace: %m");
332 log_notice("Lacking privilege to create separate mount namespace, proceeding in originating mount namespace.");
334 assert_se(mount(NULL
, "/", NULL
, MS_PRIVATE
| MS_REC
, NULL
) >= 0);
339 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG
, intro
);