99cbc7
From 691b33e397a7bd726160bc74202c8d0fdd944892 Mon Sep 17 00:00:00 2001
99cbc7
Message-Id: <691b33e397a7bd726160bc74202c8d0fdd944892@dist-git>
99cbc7
From: Michal Privoznik <mprivozn@redhat.com>
99cbc7
Date: Wed, 10 Oct 2018 17:25:55 +0200
99cbc7
Subject: [PATCH] virFileIsSharedFSType: Detect direct mount points
99cbc7
99cbc7
RHEL-7.7: https://bugzilla.redhat.com/show_bug.cgi?id=1632711
99cbc7
RHEL-8.0: https://bugzilla.redhat.com/show_bug.cgi?id=1634782
99cbc7
RHEL-7.6.z: https://bugzilla.redhat.com/show_bug.cgi?id=1635705
99cbc7
99cbc7
If the given path is already a mount point (e.g. a bind mount of
99cbc7
a file, or simply a direct mount point of a FS), then our code
99cbc7
fails to detect that because the first thing it does is cutting
99cbc7
off part after last slash '/'.
99cbc7
99cbc7
Conflicts:
99cbc7
  src/util/virfile.c - VIR_AUTOFREE() stuff
99cbc7
99cbc7
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
99cbc7
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
99cbc7
(cherry picked from commit 98ca1d52a2a871e1c068504450b4dc15db063ef4)
99cbc7
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
99cbc7
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
99cbc7
---
99cbc7
 src/util/virfile.c  | 9 +++++----
99cbc7
 tests/virfiletest.c | 3 +--
99cbc7
 2 files changed, 6 insertions(+), 6 deletions(-)
99cbc7
99cbc7
diff --git a/src/util/virfile.c b/src/util/virfile.c
99cbc7
index 46466ed136..05ecf7bf21 100644
99cbc7
--- a/src/util/virfile.c
99cbc7
+++ b/src/util/virfile.c
99cbc7
@@ -3609,7 +3609,8 @@ int
99cbc7
 virFileIsSharedFSType(const char *path,
99cbc7
                       int fstypes)
99cbc7
 {
99cbc7
-    char *dirpath, *p;
99cbc7
+    char *dirpath;
99cbc7
+    char *p = NULL;
99cbc7
     struct statfs sb;
99cbc7
     int statfs_ret;
99cbc7
     long long f_type = 0;
99cbc7
@@ -3617,8 +3618,9 @@ virFileIsSharedFSType(const char *path,
99cbc7
     if (VIR_STRDUP(dirpath, path) < 0)
99cbc7
         return -1;
99cbc7
 
99cbc7
-    do {
99cbc7
+    statfs_ret = statfs(dirpath, &sb);
99cbc7
 
99cbc7
+    while ((statfs_ret < 0) && (p != dirpath)) {
99cbc7
         /* Try less and less of the path until we get to a
99cbc7
          * directory we can stat. Even if we don't have 'x'
99cbc7
          * permission on any directory in the path on the NFS
99cbc7
@@ -3640,8 +3642,7 @@ virFileIsSharedFSType(const char *path,
99cbc7
             *p = '\0';
99cbc7
 
99cbc7
         statfs_ret = statfs(dirpath, &sb);
99cbc7
-
99cbc7
-    } while ((statfs_ret < 0) && (p != dirpath));
99cbc7
+    }
99cbc7
 
99cbc7
     VIR_FREE(dirpath);
99cbc7
 
99cbc7
diff --git a/tests/virfiletest.c b/tests/virfiletest.c
99cbc7
index 85f22063fe..80ea34bfa4 100644
99cbc7
--- a/tests/virfiletest.c
99cbc7
+++ b/tests/virfiletest.c
99cbc7
@@ -454,8 +454,7 @@ mymain(void)
99cbc7
     DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts1.txt", "/boot/vmlinuz", false);
99cbc7
     DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts2.txt", "/run/user/501/gvfs/some/file", false);
99cbc7
     DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/file", true);
99cbc7
-    /* TODO Detect bind mounts */
99cbc7
-    DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/blah", true);
99cbc7
+    DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/blah", false);
99cbc7
 
99cbc7
     return ret != 0 ? EXIT_FAILURE : EXIT_SUCCESS;
99cbc7
 }
99cbc7
-- 
99cbc7
2.21.0
99cbc7