2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/mount.h>
25 #include <sys/statvfs.h>
28 #include "alloc-util.h"
34 #include "mount-util.h"
35 #include "parse-util.h"
36 #include "path-util.h"
38 #include "stdio-util.h"
39 #include "string-util.h"
41 static int fd_fdinfo_mnt_id(int fd, const char *filename, int flags, int *mnt_id) {
42 char path[strlen("/proc/self/fdinfo/") + DECIMAL_STR_MAX(int)];
43 _cleanup_free_ char *fdinfo = NULL;
44 _cleanup_close_ int subfd = -1;
48 if ((flags & AT_EMPTY_PATH) && isempty(filename))
49 xsprintf(path, "/proc/self/fdinfo/%i", fd);
51 subfd = openat(fd, filename, O_CLOEXEC|O_PATH);
55 xsprintf(path, "/proc/self/fdinfo/%i", subfd);
58 r = read_full_file(path, &fdinfo, NULL);
59 if (r == -ENOENT) /* The fdinfo directory is a relatively new addition */
64 p = startswith(fdinfo, "mnt_id:");
66 p = strstr(fdinfo, "\nmnt_id:");
67 if (!p) /* The mnt_id field is a relatively new addition */
73 p += strspn(p, WHITESPACE);
74 p[strcspn(p, WHITESPACE)] = 0;
76 return safe_atoi(p, mnt_id);
79 int fd_is_mount_point(int fd, const char *filename, int flags) {
80 union file_handle_union h = FILE_HANDLE_INIT, h_parent = FILE_HANDLE_INIT;
81 int mount_id = -1, mount_id_parent = -1;
82 bool nosupp = false, check_st_dev = true;
89 /* First we will try the name_to_handle_at() syscall, which
90 * tells us the mount id and an opaque file "handle". It is
91 * not supported everywhere though (kernel compile-time
92 * option, not all file systems are hooked up). If it works
93 * the mount id is usually good enough to tell us whether
94 * something is a mount point.
96 * If that didn't work we will try to read the mount id from
97 * /proc/self/fdinfo/<fd>. This is almost as good as
98 * name_to_handle_at(), however, does not return the
99 * opaque file handle. The opaque file handle is pretty useful
100 * to detect the root directory, which we should always
101 * consider a mount point. Hence we use this only as
102 * fallback. Exporting the mnt_id in fdinfo is a pretty recent
105 * As last fallback we do traditional fstat() based st_dev
106 * comparisons. This is how things were traditionally done,
107 * but unionfs breaks this since it exposes file
108 * systems with a variety of st_dev reported. Also, btrfs
109 * subvolumes have different st_dev, even though they aren't
110 * real mounts of their own. */
112 r = name_to_handle_at(fd, filename, &h.handle, &mount_id, flags);
114 if (IN_SET(errno, ENOSYS, EACCES, EPERM))
115 /* This kernel does not support name_to_handle_at() at all, or the syscall was blocked (maybe
116 * through seccomp, because we are running inside of a container?): fall back to simpler
118 goto fallback_fdinfo;
119 else if (errno == EOPNOTSUPP)
120 /* This kernel or file system does not support
121 * name_to_handle_at(), hence let's see if the
122 * upper fs supports it (in which case it is a
123 * mount point), otherwise fallback to the
124 * traditional stat() logic */
130 r = name_to_handle_at(fd, "", &h_parent.handle, &mount_id_parent, AT_EMPTY_PATH);
132 if (errno == EOPNOTSUPP) {
134 /* Neither parent nor child do name_to_handle_at()?
135 We have no choice but to fall back. */
136 goto fallback_fdinfo;
138 /* The parent can't do name_to_handle_at() but the
139 * directory we are interested in can?
140 * If so, it must be a mount point. */
146 /* The parent can do name_to_handle_at() but the
147 * directory we are interested in can't? If so, it
148 * must be a mount point. */
152 /* If the file handle for the directory we are
153 * interested in and its parent are identical, we
154 * assume this is the root directory, which is a mount
157 if (h.handle.handle_bytes == h_parent.handle.handle_bytes &&
158 h.handle.handle_type == h_parent.handle.handle_type &&
159 memcmp(h.handle.f_handle, h_parent.handle.f_handle, h.handle.handle_bytes) == 0)
162 return mount_id != mount_id_parent;
165 r = fd_fdinfo_mnt_id(fd, filename, flags, &mount_id);
166 if (IN_SET(r, -EOPNOTSUPP, -EACCES, -EPERM))
171 r = fd_fdinfo_mnt_id(fd, "", AT_EMPTY_PATH, &mount_id_parent);
175 if (mount_id != mount_id_parent)
178 /* Hmm, so, the mount ids are the same. This leaves one
179 * special case though for the root file system. For that,
180 * let's see if the parent directory has the same inode as we
181 * are interested in. Hence, let's also do fstat() checks now,
182 * too, but avoid the st_dev comparisons, since they aren't
183 * that useful on unionfs mounts. */
184 check_st_dev = false;
187 /* yay for fstatat() taking a different set of flags than the other
189 if (flags & AT_SYMLINK_FOLLOW)
190 flags &= ~AT_SYMLINK_FOLLOW;
192 flags |= AT_SYMLINK_NOFOLLOW;
193 if (fstatat(fd, filename, &a, flags) < 0)
196 if (fstatat(fd, "", &b, AT_EMPTY_PATH) < 0)
199 /* A directory with same device and inode as its parent? Must
200 * be the root directory */
201 if (a.st_dev == b.st_dev &&
202 a.st_ino == b.st_ino)
205 return check_st_dev && (a.st_dev != b.st_dev);
208 /* flags can be AT_SYMLINK_FOLLOW or 0 */
209 int path_is_mount_point(const char *t, const char *root, int flags) {
210 _cleanup_free_ char *canonical = NULL, *parent = NULL;
211 _cleanup_close_ int fd = -1;
216 if (path_equal(t, "/"))
219 /* we need to resolve symlinks manually, we can't just rely on
220 * fd_is_mount_point() to do that for us; if we have a structure like
221 * /bin -> /usr/bin/ and /usr is a mount point, then the parent that we
222 * look at needs to be /usr, not /. */
223 if (flags & AT_SYMLINK_FOLLOW) {
224 r = chase_symlinks(t, root, 0, &canonical);
231 parent = dirname_malloc(t);
235 fd = openat(AT_FDCWD, parent, O_DIRECTORY|O_CLOEXEC|O_PATH);
239 return fd_is_mount_point(fd, basename(t), flags);
242 #if 0 /// UNNEEDED by elogind
243 int umount_recursive(const char *prefix, int flags) {
247 /* Try to umount everything recursively below a
248 * directory. Also, take care of stacked mounts, and keep
249 * unmounting them until they are gone. */
252 _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
257 proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
258 if (!proc_self_mountinfo)
262 _cleanup_free_ char *path = NULL, *p = NULL;
265 k = fscanf(proc_self_mountinfo,
266 "%*s " /* (1) mount id */
267 "%*s " /* (2) parent id */
268 "%*s " /* (3) major:minor */
269 "%*s " /* (4) root */
270 "%ms " /* (5) mount point */
271 "%*s" /* (6) mount options */
272 "%*[^-]" /* (7) optional fields */
273 "- " /* (8) separator */
274 "%*s " /* (9) file system type */
275 "%*s" /* (10) mount source */
276 "%*s" /* (11) mount options 2 */
277 "%*[^\n]", /* some rubbish at the end */
286 r = cunescape(path, UNESCAPE_RELAX, &p);
290 if (!path_startswith(p, prefix))
293 if (umount2(p, flags) < 0) {
294 r = log_debug_errno(errno, "Failed to umount %s: %m", p);
298 log_debug("Successfully unmounted %s", p);
311 static int get_mount_flags(const char *path, unsigned long *flags) {
314 if (statvfs(path, &buf) < 0)
320 int bind_remount_recursive(const char *prefix, bool ro, char **blacklist) {
321 _cleanup_set_free_free_ Set *done = NULL;
322 _cleanup_free_ char *cleaned = NULL;
325 /* Recursively remount a directory (and all its submounts) read-only or read-write. If the directory is already
326 * mounted, we reuse the mount and simply mark it MS_BIND|MS_RDONLY (or remove the MS_RDONLY for read-write
327 * operation). If it isn't we first make it one. Afterwards we apply MS_BIND|MS_RDONLY (or remove MS_RDONLY) to
328 * all submounts we can access, too. When mounts are stacked on the same mount point we only care for each
329 * individual "top-level" mount on each point, as we cannot influence/access the underlying mounts anyway. We
330 * do not have any effect on future submounts that might get propagated, they migt be writable. This includes
331 * future submounts that have been triggered via autofs.
333 * If the "blacklist" parameter is specified it may contain a list of subtrees to exclude from the
334 * remount operation. Note that we'll ignore the blacklist for the top-level path. */
336 cleaned = strdup(prefix);
340 path_kill_slashes(cleaned);
342 done = set_new(&string_hash_ops);
347 _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
348 _cleanup_set_free_free_ Set *todo = NULL;
349 bool top_autofs = false;
351 unsigned long orig_flags;
353 todo = set_new(&string_hash_ops);
357 proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
358 if (!proc_self_mountinfo)
362 _cleanup_free_ char *path = NULL, *p = NULL, *type = NULL;
365 k = fscanf(proc_self_mountinfo,
366 "%*s " /* (1) mount id */
367 "%*s " /* (2) parent id */
368 "%*s " /* (3) major:minor */
369 "%*s " /* (4) root */
370 "%ms " /* (5) mount point */
371 "%*s" /* (6) mount options (superblock) */
372 "%*[^-]" /* (7) optional fields */
373 "- " /* (8) separator */
374 "%ms " /* (9) file system type */
375 "%*s" /* (10) mount source */
376 "%*s" /* (11) mount options (bind mount) */
377 "%*[^\n]", /* some rubbish at the end */
387 r = cunescape(path, UNESCAPE_RELAX, &p);
391 if (!path_startswith(p, cleaned))
394 /* Ignore this mount if it is blacklisted, but only if it isn't the top-level mount we shall
396 if (!path_equal(cleaned, p)) {
397 bool blacklisted = false;
400 STRV_FOREACH(i, blacklist) {
402 if (path_equal(*i, cleaned))
405 if (!path_startswith(*i, cleaned))
408 if (path_startswith(p, *i)) {
410 log_debug("Not remounting %s, because blacklisted by %s, called for %s", p, *i, cleaned);
418 /* Let's ignore autofs mounts. If they aren't
419 * triggered yet, we want to avoid triggering
420 * them, as we don't make any guarantees for
421 * future submounts anyway. If they are
422 * already triggered, then we will find
423 * another entry for this. */
424 if (streq(type, "autofs")) {
425 top_autofs = top_autofs || path_equal(cleaned, p);
429 if (!set_contains(done, p)) {
430 r = set_consume(todo, p);
439 /* If we have no submounts to process anymore and if
440 * the root is either already done, or an autofs, we
442 if (set_isempty(todo) &&
443 (top_autofs || set_contains(done, cleaned)))
446 if (!set_contains(done, cleaned) &&
447 !set_contains(todo, cleaned)) {
448 /* The prefix directory itself is not yet a mount, make it one. */
449 if (mount(cleaned, cleaned, NULL, MS_BIND|MS_REC, NULL) < 0)
453 (void) get_mount_flags(cleaned, &orig_flags);
454 orig_flags &= ~MS_RDONLY;
456 if (mount(NULL, prefix, NULL, orig_flags|MS_BIND|MS_REMOUNT|(ro ? MS_RDONLY : 0), NULL) < 0)
459 log_debug("Made top-level directory %s a mount point.", prefix);
465 r = set_consume(done, x);
470 while ((x = set_steal_first(todo))) {
472 r = set_consume(done, x);
473 if (r == -EEXIST || r == 0)
478 /* Deal with mount points that are obstructed by a later mount */
479 r = path_is_mount_point(x, NULL, 0);
480 if (r == -ENOENT || r == 0)
485 /* Try to reuse the original flag set */
487 (void) get_mount_flags(x, &orig_flags);
488 orig_flags &= ~MS_RDONLY;
490 if (mount(NULL, x, NULL, orig_flags|MS_BIND|MS_REMOUNT|(ro ? MS_RDONLY : 0), NULL) < 0)
493 log_debug("Remounted %s read-only.", x);
498 int mount_move_root(const char *path) {
504 if (mount(path, "/", NULL, MS_MOVE, NULL) < 0)
516 bool fstype_is_network(const char *fstype) {
517 static const char table[] =
529 "pvfs2\0" /* OrangeFS */
536 x = startswith(fstype, "fuse.");
540 return nulstr_contains(table, fstype);
543 int repeat_unmount(const char *path, int flags) {
548 /* If there are multiple mounts on a mount point, this
549 * removes them all */
552 if (umount2(path, flags) < 0) {
565 const char* mode_to_inaccessible_node(mode_t mode) {
566 /* This function maps a node type to the correspondent inaccessible node type.
567 * Character and block inaccessible devices may not be created (because major=0 and minor=0),
568 * in such case we map character and block devices to the inaccessible node type socket. */
569 switch(mode & S_IFMT) {
571 return "/run/systemd/inaccessible/reg";
573 return "/run/systemd/inaccessible/dir";
575 if (access("/run/systemd/inaccessible/chr", F_OK) == 0)
576 return "/run/systemd/inaccessible/chr";
577 return "/run/systemd/inaccessible/sock";
579 if (access("/run/systemd/inaccessible/blk", F_OK) == 0)
580 return "/run/systemd/inaccessible/blk";
581 return "/run/systemd/inaccessible/sock";
583 return "/run/systemd/inaccessible/fifo";
585 return "/run/systemd/inaccessible/sock";
590 #if 0 /// UNNEEDED by elogind
591 #define FLAG(name) (flags & name ? STRINGIFY(name) "|" : "")
592 static char* mount_flags_to_string(long unsigned flags) {
594 _cleanup_free_ char *y = NULL;
595 long unsigned overflow;
597 overflow = flags & ~(MS_RDONLY |
622 if (flags == 0 || overflow != 0)
623 if (asprintf(&y, "%lx", overflow) < 0)
626 x = strjoin(FLAG(MS_RDONLY),
630 FLAG(MS_SYNCHRONOUS),
648 FLAG(MS_STRICTATIME),
654 x[strlen(x) - 1] = '\0'; /* truncate the last | */
664 const char *options) {
666 _cleanup_free_ char *fl = NULL;
668 fl = mount_flags_to_string(flags);
670 if ((flags & MS_REMOUNT) && !what && !type)
671 log_debug("Remounting %s (%s \"%s\")...",
672 where, strnull(fl), strempty(options));
673 else if (!what && !type)
674 log_debug("Mounting %s (%s \"%s\")...",
675 where, strnull(fl), strempty(options));
676 else if ((flags & MS_BIND) && !type)
677 log_debug("Bind-mounting %s on %s (%s \"%s\")...",
678 what, where, strnull(fl), strempty(options));
679 else if (flags & MS_MOVE)
680 log_debug("Moving mount %s → %s (%s \"%s\")...",
681 what, where, strnull(fl), strempty(options));
683 log_debug("Mounting %s on %s (%s \"%s\")...",
684 strna(type), where, strnull(fl), strempty(options));
685 if (mount(what, where, type, flags, options) < 0)
686 return log_full_errno(error_log_level, errno,
687 "Failed to mount %s on %s (%s \"%s\"): %m",
688 strna(type), where, strnull(fl), strempty(options));
692 int umount_verbose(const char *what) {
693 log_debug("Umounting %s...", what);
694 if (umount(what) < 0)
695 return log_error_errno(errno, "Failed to unmount %s: %m", what);
699 const char *mount_propagation_flags_to_string(unsigned long flags) {
701 switch (flags & (MS_SHARED|MS_SLAVE|MS_PRIVATE)) {
716 int mount_propagation_flags_from_string(const char *name, unsigned long *ret) {
720 else if (streq(name, "shared"))
722 else if (streq(name, "slave"))
724 else if (streq(name, "private"))