anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone

Blame SOURCES/0519-path-util-make-use-of-mnt_id-field-exported-in-proc-.patch

923a60
From f63b66b6347a8d8e5e6930a939d1997bfd8e2e7c Mon Sep 17 00:00:00 2001
923a60
From: Jan Synacek <jsynacek@redhat.com>
923a60
Date: Fri, 28 Jul 2017 15:31:50 +0200
923a60
Subject: [PATCH] path-util: make use of "mnt_id" field exported in
923a60
 /proc/self/fdinfo/<fd>
923a60
923a60
This commit is not a backport of a specific commit. It includes parts of
923a60
several upstream commits (3f72b427b44f39a1aec6806dad6f6b57103ae9ed,
923a60
5d409034017e9f9f8c4392157d95511fc2e05d87 and others).
923a60
923a60
The main goal was to bring path_is_mount_point() up to date, which meant
923a60
introducing fd_fdinfo_mnt_id() and fd_is_mount_point(). These were
923a60
needed mainly because we need to determine mount points based on
923a60
/proc/self/fdinfo/<fd> in containers. Also, there are more places in the
923a60
code where checks for mount points are performed, which would benefit from
923a60
this fix as well. Additionally, corresponding tests has been added.
923a60
923a60
Resolves: #1472439
923a60
---
923a60
 src/core/automount.c                        |   2 +-
923a60
 src/core/machine-id-setup.c                 |   2 +-
923a60
 src/core/mount-setup.c                      |   2 +-
923a60
 src/efi-boot-generator/efi-boot-generator.c |   2 +-
923a60
 src/gpt-auto-generator/gpt-auto-generator.c |   2 +-
923a60
 src/login/logind-user.c                     |   2 +-
923a60
 src/nspawn/nspawn.c                         |  10 +-
923a60
 src/shared/cgroup-util.c                    |   2 +-
923a60
 src/shared/condition.c                      |   2 +-
923a60
 src/shared/path-util.c                      | 209 +++++++++++++++-----
923a60
 src/shared/path-util.h                      |   3 +-
923a60
 src/test/test-path-util.c                   |  66 ++++++-
923a60
 12 files changed, 242 insertions(+), 62 deletions(-)
923a60
923a60
diff --git a/src/core/automount.c b/src/core/automount.c
923a60
index 4e066613d7..eedd9b8243 100644
923a60
--- a/src/core/automount.c
923a60
+++ b/src/core/automount.c
923a60
@@ -749,7 +749,7 @@ static int automount_start(Unit *u) {
923a60
         assert(a);
923a60
         assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_FAILED);
923a60
 
923a60
-        if (path_is_mount_point(a->where, false)) {
923a60
+        if (path_is_mount_point(a->where, 0)) {
923a60
                 log_unit_error(u->id,
923a60
                                "Path %s is already a mount point, refusing start for %s",
923a60
                                a->where, u->id);
923a60
diff --git a/src/core/machine-id-setup.c b/src/core/machine-id-setup.c
923a60
index d00a53246f..1121d373fa 100644
923a60
--- a/src/core/machine-id-setup.c
923a60
+++ b/src/core/machine-id-setup.c
923a60
@@ -203,7 +203,7 @@ int machine_id_commit(const char *root) {
923a60
                 etc_machine_id = path_kill_slashes(x);
923a60
         }
923a60
 
923a60
-        r = path_is_mount_point(etc_machine_id, false);
923a60
+        r = path_is_mount_point(etc_machine_id, 0);
923a60
         if (r < 0)
923a60
                 return log_error_errno(r, "Failed to determine whether %s is a mount point: %m", etc_machine_id);
923a60
         if (r == 0) {
923a60
diff --git a/src/core/mount-setup.c b/src/core/mount-setup.c
923a60
index 521545e5ce..2b8fbab1a3 100644
923a60
--- a/src/core/mount-setup.c
923a60
+++ b/src/core/mount-setup.c
923a60
@@ -160,7 +160,7 @@ static int mount_one(const MountPoint *p, bool relabel) {
923a60
         if (relabel)
923a60
                 label_fix(p->where, true, true);
923a60
 
923a60
-        r = path_is_mount_point(p->where, true);
923a60
+        r = path_is_mount_point(p->where, AT_SYMLINK_FOLLOW);
923a60
         if (r < 0)
923a60
                 return r;
923a60
 
923a60
diff --git a/src/efi-boot-generator/efi-boot-generator.c b/src/efi-boot-generator/efi-boot-generator.c
923a60
index b3ff3a8b78..5492b19946 100644
923a60
--- a/src/efi-boot-generator/efi-boot-generator.c
923a60
+++ b/src/efi-boot-generator/efi-boot-generator.c
923a60
@@ -69,7 +69,7 @@ int main(int argc, char *argv[]) {
923a60
                 return EXIT_SUCCESS;
923a60
         }
923a60
 
923a60
-        if (path_is_mount_point("/boot", true) <= 0 &&
923a60
+        if (path_is_mount_point("/boot", AT_SYMLINK_FOLLOW) <= 0 &&
923a60
             dir_is_empty("/boot") <= 0) {
923a60
                 log_debug("/boot already populated, exiting.");
923a60
                 return EXIT_SUCCESS;
923a60
diff --git a/src/gpt-auto-generator/gpt-auto-generator.c b/src/gpt-auto-generator/gpt-auto-generator.c
923a60
index 00a2141a58..d7b047118d 100644
923a60
--- a/src/gpt-auto-generator/gpt-auto-generator.c
923a60
+++ b/src/gpt-auto-generator/gpt-auto-generator.c
923a60
@@ -299,7 +299,7 @@ static int probe_and_add_mount(
923a60
         assert(where);
923a60
         assert(description);
923a60
 
923a60
-        if (path_is_mount_point(where, true) <= 0 &&
923a60
+        if (path_is_mount_point(where, AT_SYMLINK_FOLLOW) <= 0 &&
923a60
             dir_is_empty(where) <= 0) {
923a60
                 log_debug("%s already populated, ignoring.", where);
923a60
                 return 0;
923a60
diff --git a/src/login/logind-user.c b/src/login/logind-user.c
923a60
index 4298704cea..912c50ebde 100644
923a60
--- a/src/login/logind-user.c
923a60
+++ b/src/login/logind-user.c
923a60
@@ -320,7 +320,7 @@ static int user_mkdir_runtime_path(User *u) {
923a60
         } else
923a60
                 p = u->runtime_path;
923a60
 
923a60
-        if (path_is_mount_point(p, false) <= 0) {
923a60
+        if (path_is_mount_point(p, 0) <= 0) {
923a60
                 _cleanup_free_ char *t = NULL;
923a60
 
923a60
                 (void) mkdir(p, 0700);
923a60
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
923a60
index ea365b3f9b..a90a3a5d75 100644
923a60
--- a/src/nspawn/nspawn.c
923a60
+++ b/src/nspawn/nspawn.c
923a60
@@ -863,7 +863,7 @@ static int mount_all(const char *dest) {
923a60
                 if (!where)
923a60
                         return log_oom();
923a60
 
923a60
-                t = path_is_mount_point(where, true);
923a60
+                t = path_is_mount_point(where, AT_SYMLINK_FOLLOW);
923a60
                 if (t < 0) {
923a60
                         log_error_errno(t, "Failed to detect whether %s is a mount point: %m", where);
923a60
 
923a60
@@ -989,7 +989,7 @@ static int mount_cgroup_hierarchy(const char *dest, const char *controller, cons
923a60
 
923a60
         to = strjoina(dest, "/sys/fs/cgroup/", hierarchy);
923a60
 
923a60
-        r = path_is_mount_point(to, false);
923a60
+        r = path_is_mount_point(to, 0);
923a60
         if (r < 0)
923a60
                 return log_error_errno(r, "Failed to determine if %s is mounted already: %m", to);
923a60
         if (r > 0)
923a60
@@ -1787,7 +1787,7 @@ static int setup_journal(const char *directory) {
923a60
         if (!p || !q)
923a60
                 return log_oom();
923a60
 
923a60
-        if (path_is_mount_point(p, false) > 0) {
923a60
+        if (path_is_mount_point(p, 0) > 0) {
923a60
                 if (arg_link_journal != LINK_AUTO) {
923a60
                         log_error("%s: already a mount point, refusing to use for journal", p);
923a60
                         return -EEXIST;
923a60
@@ -1796,7 +1796,7 @@ static int setup_journal(const char *directory) {
923a60
                 return 0;
923a60
         }
923a60
 
923a60
-        if (path_is_mount_point(q, false) > 0) {
923a60
+        if (path_is_mount_point(q, 0) > 0) {
923a60
                 if (arg_link_journal != LINK_AUTO) {
923a60
                         log_error("%s: already a mount point, refusing to use for journal", q);
923a60
                         return -EEXIST;
923a60
@@ -3665,7 +3665,7 @@ int main(int argc, char *argv[]) {
923a60
                          * the specified is not a mount point we
923a60
                          * create the new snapshot in the parent
923a60
                          * directory, just next to it. */
923a60
-                        r = path_is_mount_point(arg_directory, false);
923a60
+                        r = path_is_mount_point(arg_directory, 0);
923a60
                         if (r < 0) {
923a60
                                 log_error_errno(r, "Failed to determine whether directory %s is mount point: %m", arg_directory);
923a60
                                 goto finish;
923a60
diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c
923a60
index c5d9e4bb58..cf085cb5ff 100644
923a60
--- a/src/shared/cgroup-util.c
923a60
+++ b/src/shared/cgroup-util.c
923a60
@@ -488,7 +488,7 @@ int cg_get_path(const char *controller, const char *path, const char *suffix, ch
923a60
         if (_unlikely_(!good)) {
923a60
                 int r;
923a60
 
923a60
-                r = path_is_mount_point("/sys/fs/cgroup", false);
923a60
+                r = path_is_mount_point("/sys/fs/cgroup", 0);
923a60
                 if (r <= 0)
923a60
                         return r < 0 ? r : -ENOENT;
923a60
 
923a60
diff --git a/src/shared/condition.c b/src/shared/condition.c
923a60
index 796cc520d7..0d2cd2bc3a 100644
923a60
--- a/src/shared/condition.c
923a60
+++ b/src/shared/condition.c
923a60
@@ -350,7 +350,7 @@ static int condition_test_path_is_mount_point(Condition *c) {
923a60
         assert(c->parameter);
923a60
         assert(c->type == CONDITION_PATH_IS_MOUNT_POINT);
923a60
 
923a60
-        return path_is_mount_point(c->parameter, true) > 0;
923a60
+        return path_is_mount_point(c->parameter, AT_SYMLINK_FOLLOW) > 0;
923a60
 }
923a60
 
923a60
 static int condition_test_path_is_read_write(Condition *c) {
923a60
diff --git a/src/shared/path-util.c b/src/shared/path-util.c
923a60
index 1181ffb9d4..0f252ec262 100644
923a60
--- a/src/shared/path-util.c
923a60
+++ b/src/shared/path-util.c
923a60
@@ -36,6 +36,7 @@
923a60
 #include "strv.h"
923a60
 #include "path-util.h"
923a60
 #include "missing.h"
923a60
+#include "fileio.h"
923a60
 
923a60
 bool path_is_absolute(const char *p) {
923a60
         return p[0] == '/';
923a60
@@ -473,87 +474,203 @@ char* path_join(const char *root, const char *path, const char *rest) {
923a60
                                NULL);
923a60
 }
923a60
 
923a60
-int path_is_mount_point(const char *t, bool allow_symlink) {
923a60
+static int fd_fdinfo_mnt_id(int fd, const char *filename, int flags, int *mnt_id) {
923a60
+        char path[strlen("/proc/self/fdinfo/") + DECIMAL_STR_MAX(int)];
923a60
+        _cleanup_free_ char *fdinfo = NULL;
923a60
+        _cleanup_close_ int subfd = -1;
923a60
+        char *p;
923a60
+        int r;
923a60
+
923a60
+        if ((flags & AT_EMPTY_PATH) && isempty(filename))
923a60
+                xsprintf(path, "/proc/self/fdinfo/%i", fd);
923a60
+        else {
923a60
+                subfd = openat(fd, filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_PATH);
923a60
+                if (subfd < 0)
923a60
+                        return -errno;
923a60
+
923a60
+                xsprintf(path, "/proc/self/fdinfo/%i", subfd);
923a60
+        }
923a60
+
923a60
+        r = read_full_file(path, &fdinfo, NULL);
923a60
+        if (r == -ENOENT) /* The fdinfo directory is a relatively new addition */
923a60
+                return -EOPNOTSUPP;
923a60
+        if (r < 0)
923a60
+                return -errno;
923a60
+
923a60
+        p = startswith(fdinfo, "mnt_id:");
923a60
+        if (!p) {
923a60
+                p = strstr(fdinfo, "\nmnt_id:");
923a60
+                if (!p) /* The mnt_id field is a relatively new addition */
923a60
+                        return -EOPNOTSUPP;
923a60
+
923a60
+                p += 8;
923a60
+        }
923a60
 
923a60
-        union file_handle_union h = FILE_HANDLE_INIT;
923a60
+        p += strspn(p, WHITESPACE);
923a60
+        p[strcspn(p, WHITESPACE)] = 0;
923a60
+
923a60
+        return safe_atoi(p, mnt_id);
923a60
+}
923a60
+
923a60
+int fd_is_mount_point(int fd, const char *filename, int flags) {
923a60
+        union file_handle_union h = FILE_HANDLE_INIT, h_parent = FILE_HANDLE_INIT;
923a60
         int mount_id = -1, mount_id_parent = -1;
923a60
-        _cleanup_free_ char *parent = NULL;
923a60
+        bool nosupp = false, check_st_dev = true;
923a60
         struct stat a, b;
923a60
         int r;
923a60
-        bool nosupp = false;
923a60
 
923a60
-        /* We are not actually interested in the file handles, but
923a60
-         * name_to_handle_at() also passes us the mount ID, hence use
923a60
-         * it but throw the handle away */
923a60
+        assert(fd >= 0);
923a60
+        assert(filename);
923a60
 
923a60
-        if (path_equal(t, "/"))
923a60
-                return 1;
923a60
-
923a60
-        r = name_to_handle_at(AT_FDCWD, t, &h.handle, &mount_id, allow_symlink ? AT_SYMLINK_FOLLOW : 0);
923a60
+        /* First we will try the name_to_handle_at() syscall, which
923a60
+         * tells us the mount id and an opaque file "handle". It is
923a60
+         * not supported everywhere though (kernel compile-time
923a60
+         * option, not all file systems are hooked up). If it works
923a60
+         * the mount id is usually good enough to tell us whether
923a60
+         * something is a mount point.
923a60
+         *
923a60
+         * If that didn't work we will try to read the mount id from
923a60
+         * /proc/self/fdinfo/<fd>. This is almost as good as
923a60
+         * name_to_handle_at(), however, does not return the
923a60
+         * opaque file handle. The opaque file handle is pretty useful
923a60
+         * to detect the root directory, which we should always
923a60
+         * consider a mount point. Hence we use this only as
923a60
+         * fallback. Exporting the mnt_id in fdinfo is a pretty recent
923a60
+         * kernel addition.
923a60
+         *
923a60
+         * As last fallback we do traditional fstat() based st_dev
923a60
+         * comparisons. This is how things were traditionally done,
923a60
+         * but unionfs breaks breaks this since it exposes file
923a60
+         * systems with a variety of st_dev reported. Also, btrfs
923a60
+         * subvolumes have different st_dev, even though they aren't
923a60
+         * real mounts of their own. */
923a60
+
923a60
+        r = name_to_handle_at(fd, filename, &h.handle, &mount_id, flags);
923a60
         if (r < 0) {
923a60
                 if (errno == ENOSYS)
923a60
                         /* This kernel does not support name_to_handle_at()
923a60
-                         * fall back to the traditional stat() logic. */
923a60
-                        goto fallback;
923a60
+                         * fall back to simpler logic. */
923a60
+                        goto fallback_fdinfo;
923a60
                 else if (errno == EOPNOTSUPP)
923a60
                         /* This kernel or file system does not support
923a60
-                         * name_to_handle_at(), hence fallback to the
923a60
+                         * name_to_handle_at(), hence let's see if the
923a60
+                         * upper fs supports it (in which case it is a
923a60
+                         * mount point), otherwise fallback to the
923a60
                          * traditional stat() logic */
923a60
                         nosupp = true;
923a60
-                else if (errno == ENOENT)
923a60
-                        return 0;
923a60
                 else
923a60
                         return -errno;
923a60
         }
923a60
 
923a60
-        r = path_get_parent(t, &parent);
923a60
-        if (r < 0)
923a60
-                return r;
923a60
-
923a60
-        h.handle.handle_bytes = MAX_HANDLE_SZ;
923a60
-        r = name_to_handle_at(AT_FDCWD, parent, &h.handle, &mount_id_parent, AT_SYMLINK_FOLLOW);
923a60
-        if (r < 0)
923a60
-                if (errno == EOPNOTSUPP)
923a60
+        r = name_to_handle_at(fd, "", &h_parent.handle, &mount_id_parent, AT_EMPTY_PATH);
923a60
+        if (r < 0) {
923a60
+                if (errno == EOPNOTSUPP) {
923a60
                         if (nosupp)
923a60
                                 /* Neither parent nor child do name_to_handle_at()?
923a60
                                    We have no choice but to fall back. */
923a60
-                                goto fallback;
923a60
+                                goto fallback_fdinfo;
923a60
                         else
923a60
-                                /* The parent can't do name_to_handle_at() but
923a60
-                                 * the directory we are interested in can?
923a60
-                                 * Or the other way around?
923a60
+                                /* The parent can't do name_to_handle_at() but the
923a60
+                                 * directory we are interested in can?
923a60
                                  * If so, it must be a mount point. */
923a60
                                 return 1;
923a60
-                else
923a60
+                } else
923a60
                         return -errno;
923a60
-        else
923a60
-                return mount_id != mount_id_parent;
923a60
+        }
923a60
 
923a60
-fallback:
923a60
-        if (allow_symlink)
923a60
-                r = stat(t, &a);
923a60
-        else
923a60
-                r = lstat(t, &a);
923a60
+        /* The parent can do name_to_handle_at() but the
923a60
+         * directory we are interested in can't? If so, it
923a60
+         * must be a mount point. */
923a60
+        if (nosupp)
923a60
+                return 1;
923a60
 
923a60
-        if (r < 0) {
923a60
-                if (errno == ENOENT)
923a60
-                        return 0;
923a60
+        /* If the file handle for the directory we are
923a60
+         * interested in and its parent are identical, we
923a60
+         * assume this is the root directory, which is a mount
923a60
+         * point. */
923a60
 
923a60
-                return -errno;
923a60
-        }
923a60
+        if (h.handle.handle_bytes == h_parent.handle.handle_bytes &&
923a60
+            h.handle.handle_type == h_parent.handle.handle_type &&
923a60
+            memcmp(h.handle.f_handle, h_parent.handle.f_handle, h.handle.handle_bytes) == 0)
923a60
+                return 1;
923a60
 
923a60
-        free(parent);
923a60
-        parent = NULL;
923a60
+        return mount_id != mount_id_parent;
923a60
 
923a60
-        r = path_get_parent(t, &parent);
923a60
+fallback_fdinfo:
923a60
+        r = fd_fdinfo_mnt_id(fd, filename, flags, &mount_id);
923a60
+        if (r == -EOPNOTSUPP)
923a60
+                goto fallback_fstat;
923a60
         if (r < 0)
923a60
                 return r;
923a60
 
923a60
-        r = stat(parent, &b);
923a60
+        r = fd_fdinfo_mnt_id(fd, "", AT_EMPTY_PATH, &mount_id_parent);
923a60
         if (r < 0)
923a60
+                return r;
923a60
+
923a60
+        if (mount_id != mount_id_parent)
923a60
+                return 1;
923a60
+
923a60
+        /* Hmm, so, the mount ids are the same. This leaves one
923a60
+         * special case though for the root file system. For that,
923a60
+         * let's see if the parent directory has the same inode as we
923a60
+         * are interested in. Hence, let's also do fstat() checks now,
923a60
+         * too, but avoid the st_dev comparisons, since they aren't
923a60
+         * that useful on unionfs mounts. */
923a60
+        check_st_dev = false;
923a60
+
923a60
+fallback_fstat:
923a60
+        /* yay for fstatat() taking a different set of flags than the other
923a60
+         * _at() above */
923a60
+        if (flags & AT_SYMLINK_FOLLOW)
923a60
+                flags &= ~AT_SYMLINK_FOLLOW;
923a60
+        else
923a60
+                flags |= AT_SYMLINK_NOFOLLOW;
923a60
+        if (fstatat(fd, filename, &a, flags) < 0)
923a60
+                return -errno;
923a60
+
923a60
+        if (fstatat(fd, "", &b, AT_EMPTY_PATH) < 0)
923a60
+                return -errno;
923a60
+
923a60
+        /* A directory with same device and inode as its parent? Must
923a60
+         * be the root directory */
923a60
+        if (a.st_dev == b.st_dev &&
923a60
+            a.st_ino == b.st_ino)
923a60
+                return 1;
923a60
+
923a60
+        return check_st_dev && (a.st_dev != b.st_dev);
923a60
+}
923a60
+
923a60
+/* flags can be AT_SYMLINK_FOLLOW or 0 */
923a60
+int path_is_mount_point(const char *t, int flags) {
923a60
+        _cleanup_close_ int fd = -1;
923a60
+        _cleanup_free_ char *canonical = NULL, *parent = NULL;
923a60
+
923a60
+        assert(t);
923a60
+
923a60
+        if (path_equal(t, "/"))
923a60
+                return 1;
923a60
+
923a60
+        /* we need to resolve symlinks manually, we can't just rely on
923a60
+         * fd_is_mount_point() to do that for us; if we have a structure like
923a60
+         * /bin -> /usr/bin/ and /usr is a mount point, then the parent that we
923a60
+         * look at needs to be /usr, not /. */
923a60
+        if (flags & AT_SYMLINK_FOLLOW) {
923a60
+                canonical = canonicalize_file_name(t);
923a60
+                if (!canonical)
923a60
+                        return -errno;
923a60
+
923a60
+                t = canonical;
923a60
+        }
923a60
+
923a60
+        parent = dirname_malloc(t);
923a60
+        if (!parent)
923a60
+                return -ENOMEM;
923a60
+
923a60
+        fd = openat(AT_FDCWD, parent, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_PATH);
923a60
+        if (fd < 0)
923a60
                 return -errno;
923a60
 
923a60
-        return a.st_dev != b.st_dev;
923a60
+        return fd_is_mount_point(fd, basename(t), flags);
923a60
 }
923a60
 
923a60
 int path_is_read_only_fs(const char *path) {
923a60
diff --git a/src/shared/path-util.h b/src/shared/path-util.h
923a60
index 71bb740e98..e16484087f 100644
923a60
--- a/src/shared/path-util.h
923a60
+++ b/src/shared/path-util.h
923a60
@@ -53,7 +53,8 @@ char** path_strv_make_absolute_cwd(char **l);
923a60
 char** path_strv_resolve(char **l, const char *prefix);
923a60
 char** path_strv_resolve_uniq(char **l, const char *prefix);
923a60
 
923a60
-int path_is_mount_point(const char *path, bool allow_symlink);
923a60
+int fd_is_mount_point(int fd, const char *filename, int flags);
923a60
+int path_is_mount_point(const char *path, int flags);
923a60
 int path_is_read_only_fs(const char *path);
923a60
 int path_is_os_tree(const char *path);
923a60
 
923a60
diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c
923a60
index 6396fcb398..8870f178a1 100644
923a60
--- a/src/test/test-path-util.c
923a60
+++ b/src/test/test-path-util.c
923a60
@@ -21,6 +21,7 @@
923a60
 
923a60
 #include <stdio.h>
923a60
 #include <unistd.h>
923a60
+#include <sys/mount.h>
923a60
 
923a60
 #include "path-util.h"
923a60
 #include "util.h"
923a60
@@ -85,8 +86,8 @@ static void test_path(void) {
923a60
         test_parent("/aa///file...", "/aa///");
923a60
         test_parent("file.../", NULL);
923a60
 
923a60
-        assert_se(path_is_mount_point("/", true));
923a60
-        assert_se(path_is_mount_point("/", false));
923a60
+        assert_se(path_is_mount_point("/", AT_SYMLINK_FOLLOW));
923a60
+        assert_se(path_is_mount_point("/", 0));
923a60
 
923a60
         {
923a60
                 char p1[] = "aaa/bbb////ccc";
923a60
@@ -99,6 +100,66 @@ static void test_path(void) {
923a60
         }
923a60
 }
923a60
 
923a60
+static void test_path_is_mount_point(void) {
923a60
+        int fd, rt, rf, rlt, rlf;
923a60
+        char tmp_dir[] = "/tmp/test-path-is-mount-point-XXXXXX";
923a60
+        _cleanup_free_ char *file1 = NULL, *file2 = NULL, *link1 = NULL, *link2 = NULL;
923a60
+
923a60
+        assert_se(path_is_mount_point("/", AT_SYMLINK_FOLLOW) > 0);
923a60
+        assert_se(path_is_mount_point("/", 0) > 0);
923a60
+
923a60
+        assert_se(path_is_mount_point("/proc", AT_SYMLINK_FOLLOW) > 0);
923a60
+        assert_se(path_is_mount_point("/proc", 0) > 0);
923a60
+
923a60
+        assert_se(path_is_mount_point("/proc/1", AT_SYMLINK_FOLLOW) == 0);
923a60
+        assert_se(path_is_mount_point("/proc/1", 0) == 0);
923a60
+
923a60
+        assert_se(path_is_mount_point("/sys", AT_SYMLINK_FOLLOW) > 0);
923a60
+        assert_se(path_is_mount_point("/sys", 0) > 0);
923a60
+
923a60
+        /* file mountpoints */
923a60
+        assert_se(mkdtemp(tmp_dir) != NULL);
923a60
+        file1 = path_join(NULL, tmp_dir, "file1");
923a60
+        assert_se(file1);
923a60
+        file2 = path_join(NULL, tmp_dir, "file2");
923a60
+        assert_se(file2);
923a60
+        fd = open(file1, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
923a60
+        assert_se(fd > 0);
923a60
+        close(fd);
923a60
+        fd = open(file2, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
923a60
+        assert_se(fd > 0);
923a60
+        close(fd);
923a60
+        link1 = path_join(NULL, tmp_dir, "link1");
923a60
+        assert_se(link1);
923a60
+        assert_se(symlink("file1", link1) == 0);
923a60
+        link2 = path_join(NULL, tmp_dir, "link2");
923a60
+        assert_se(link1);
923a60
+        assert_se(symlink("file2", link2) == 0);
923a60
+
923a60
+        assert_se(path_is_mount_point(file1, AT_SYMLINK_FOLLOW) == 0);
923a60
+        assert_se(path_is_mount_point(file1, 0) == 0);
923a60
+        assert_se(path_is_mount_point(link1, AT_SYMLINK_FOLLOW) == 0);
923a60
+        assert_se(path_is_mount_point(link1, 0) == 0);
923a60
+
923a60
+        /* this test will only work as root */
923a60
+        if (mount(file1, file2, NULL, MS_BIND, NULL) >= 0) {
923a60
+                rf = path_is_mount_point(file2, 0);
923a60
+                rt = path_is_mount_point(file2, AT_SYMLINK_FOLLOW);
923a60
+                rlf = path_is_mount_point(link2, 0);
923a60
+                rlt = path_is_mount_point(link2, AT_SYMLINK_FOLLOW);
923a60
+
923a60
+                assert_se(umount(file2) == 0);
923a60
+
923a60
+                assert_se(rf == 1);
923a60
+                assert_se(rt == 1);
923a60
+                assert_se(rlf == 0);
923a60
+                assert_se(rlt == 1);
923a60
+        } else
923a60
+                printf("Skipping bind mount file test: %m\n");
923a60
+
923a60
+        assert_se(rm_rf(tmp_dir, false, true, false) == 0);
923a60
+}
923a60
+
923a60
 static void test_find_binary(const char *self, bool local) {
923a60
         char *p;
923a60
 
923a60
@@ -288,6 +349,7 @@ int main(int argc, char **argv) {
923a60
         test_make_relative();
923a60
         test_strv_resolve();
923a60
         test_path_startswith();
923a60
+        test_path_is_mount_point();
923a60
 
923a60
         return 0;
923a60
 }