cdown / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

Blame SOURCES/0072-libmount-cleanup-fs-root-detection-code.patch

05ad79
From bb3483bf21b9cbe462caaa74fbc03d2eb7845d74 Mon Sep 17 00:00:00 2001
05ad79
From: Karel Zak <kzak@redhat.com>
05ad79
Date: Tue, 10 Mar 2015 13:35:56 +0100
05ad79
Subject: [PATCH 72/84] libmount: cleanup fs root detection code
05ad79
05ad79
Upstream: http://github.com/karelzak/util-linux/commit/cc06a01ec551ed2bcd397a5097165b4434179b34
05ad79
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=587393
05ad79
Signed-off-by: Karel Zak <kzak@redhat.com>
05ad79
---
05ad79
 libmount/src/tab.c                            | 32 ++++++++++++++++++++-----
05ad79
 libmount/src/utils.c                          | 34 ---------------------------
05ad79
 tests/expected/libmount/utils-fs-root         |  1 -
05ad79
 tests/expected/libmount/utils-fs-root-subdir  |  1 -
05ad79
 tests/expected/libmount/utils-fs-root-subdir2 |  1 -
05ad79
 tests/ts/libmount/utils                       | 12 ----------
05ad79
 6 files changed, 26 insertions(+), 55 deletions(-)
05ad79
 delete mode 100644 tests/expected/libmount/utils-fs-root
05ad79
 delete mode 100644 tests/expected/libmount/utils-fs-root-subdir
05ad79
 delete mode 100644 tests/expected/libmount/utils-fs-root-subdir2
05ad79
05ad79
diff --git a/libmount/src/tab.c b/libmount/src/tab.c
05ad79
index 1ba1eec..10ee7ce 100644
05ad79
--- a/libmount/src/tab.c
05ad79
+++ b/libmount/src/tab.c
05ad79
@@ -46,6 +46,8 @@
05ad79
 #include "mountP.h"
05ad79
 #include "strutils.h"
05ad79
 #include "loopdev.h"
05ad79
+#include "fileutils.h"
05ad79
+#include "canonicalize.h"
05ad79
 
05ad79
 static int is_mountinfo(struct libmnt_table *tb);
05ad79
 
05ad79
@@ -900,6 +902,20 @@ struct libmnt_fs *mnt_table_find_devno(struct libmnt_table *tb,
05ad79
 	return NULL;
05ad79
 }
05ad79
 
05ad79
+static char *remove_mountpoint_from_path(const char *path, const char *mnt)
05ad79
+{
05ad79
+        char *res;
05ad79
+	const char *p;
05ad79
+	size_t sz;
05ad79
+
05ad79
+	sz = strlen(mnt);
05ad79
+	p = sz > 1 ? path + sz : path;
05ad79
+
05ad79
+	res = *p ? strdup(p) : strdup("/");
05ad79
+	DBG(UTILS, mnt_debug("%s fs-root is %s", path, res));
05ad79
+	return res;
05ad79
+}
05ad79
+
05ad79
 /*
05ad79
  * tb: /proc/self/mountinfo
05ad79
  * fs: filesystem
05ad79
@@ -919,7 +935,8 @@ struct libmnt_fs *mnt_table_get_fs_root(struct libmnt_table *tb,
05ad79
 					unsigned long mountflags,
05ad79
 					char **fsroot)
05ad79
 {
05ad79
-	char *root = NULL, *mnt = NULL;
05ad79
+	char *root = NULL;
05ad79
+	const char *mnt = NULL;
05ad79
 	const char *fstype;
05ad79
 	struct libmnt_fs *src_fs = NULL;
05ad79
 
05ad79
@@ -937,10 +954,15 @@ struct libmnt_fs *mnt_table_get_fs_root(struct libmnt_table *tb,
05ad79
 		DBG(TAB, mnt_debug("fs-root for bind"));
05ad79
 
05ad79
 		src = xsrc = mnt_resolve_spec(mnt_fs_get_source(fs), tb->cache);
05ad79
-		if (src)
05ad79
-			mnt = mnt_get_mountpoint(src);
05ad79
+		if (src) {
05ad79
+			struct libmnt_fs *fs = mnt_table_find_mountpoint(tb,
05ad79
+							src, MNT_ITER_BACKWARD);
05ad79
+			if (fs)
05ad79
+				mnt = mnt_fs_get_target(fs);
05ad79
+		}
05ad79
+
05ad79
 		if (mnt)
05ad79
-			root = mnt_get_fs_root(src, mnt);
05ad79
+			root = remove_mountpoint_from_path(src, mnt);
05ad79
 
05ad79
 		if (xsrc && !tb->cache) {
05ad79
 			free(xsrc);
05ad79
@@ -1007,11 +1029,9 @@ dflt:
05ad79
 
05ad79
 	DBG(TAB, mnt_debug("FS root result: %s", root));
05ad79
 
05ad79
-	free(mnt);
05ad79
 	return src_fs;
05ad79
 err:
05ad79
 	free(root);
05ad79
-	free(mnt);
05ad79
 	return NULL;
05ad79
 }
05ad79
 
05ad79
diff --git a/libmount/src/utils.c b/libmount/src/utils.c
05ad79
index 5783d88..2151ff9 100644
05ad79
--- a/libmount/src/utils.c
05ad79
+++ b/libmount/src/utils.c
05ad79
@@ -908,28 +908,6 @@ err:
05ad79
 	return NULL;
05ad79
 }
05ad79
 
05ad79
-char *mnt_get_fs_root(const char *path, const char *mnt)
05ad79
-{
05ad79
-	char *m = (char *) mnt, *res;
05ad79
-	const char *p;
05ad79
-	size_t sz;
05ad79
-
05ad79
-	if (!m)
05ad79
-		m = mnt_get_mountpoint(path);
05ad79
-	if (!m)
05ad79
-		return NULL;
05ad79
-
05ad79
-	sz = strlen(m);
05ad79
-	p = sz > 1 ? path + sz : path;
05ad79
-
05ad79
-	if (m != mnt)
05ad79
-		free(m);
05ad79
-
05ad79
-	res = *p ? strdup(p) : strdup("/");
05ad79
-	DBG(UTILS, mnt_debug("%s fs-root is %s", path, res));
05ad79
-	return res;
05ad79
-}
05ad79
-
05ad79
 /*
05ad79
  * Search for @name kernel command parametr.
05ad79
  *
05ad79
@@ -1085,17 +1063,6 @@ int test_mountpoint(struct libmnt_test *ts, int argc, char *argv[])
05ad79
 	return 0;
05ad79
 }
05ad79
 
05ad79
-int test_fsroot(struct libmnt_test *ts, int argc, char *argv[])
05ad79
-{
05ad79
-	char *path = canonicalize_path(argv[1]),
05ad79
-	     *mnt = path ? mnt_get_fs_root(path, NULL) : NULL;
05ad79
-
05ad79
-	printf("%s: %s\n", argv[1], mnt ? : "unknown");
05ad79
-	free(mnt);
05ad79
-	free(path);
05ad79
-	return 0;
05ad79
-}
05ad79
-
05ad79
 int test_filesystems(struct libmnt_test *ts, int argc, char *argv[])
05ad79
 {
05ad79
 	char **filesystems = NULL;
05ad79
@@ -1170,7 +1137,6 @@ int main(int argc, char *argv[])
05ad79
 	{ "--starts-with",   test_startswith,      "<string> <prefix>" },
05ad79
 	{ "--ends-with",     test_endswith,        "<string> <prefix>" },
05ad79
 	{ "--mountpoint",    test_mountpoint,      "<path>" },
05ad79
-	{ "--fs-root",       test_fsroot,          "<path>" },
05ad79
 	{ "--cd-parent",     test_chdir,           "<path>" },
05ad79
 	{ "--kernel-cmdline",test_kernel_cmdline,  "<option> | <option>=" },
05ad79
 	{ "--mkdir",         test_mkdir,           "<path>" },
05ad79
diff --git a/tests/expected/libmount/utils-fs-root b/tests/expected/libmount/utils-fs-root
05ad79
deleted file mode 100644
05ad79
index 7746b28..0000000
05ad79
--- a/tests/expected/libmount/utils-fs-root
05ad79
+++ /dev/null
05ad79
@@ -1 +0,0 @@
05ad79
-/proc: /
05ad79
diff --git a/tests/expected/libmount/utils-fs-root-subdir b/tests/expected/libmount/utils-fs-root-subdir
05ad79
deleted file mode 100644
05ad79
index 09cdb8d..0000000
05ad79
--- a/tests/expected/libmount/utils-fs-root-subdir
05ad79
+++ /dev/null
05ad79
@@ -1 +0,0 @@
05ad79
-/proc/sys/kernel: /sys/kernel
05ad79
diff --git a/tests/expected/libmount/utils-fs-root-subdir2 b/tests/expected/libmount/utils-fs-root-subdir2
05ad79
deleted file mode 100644
05ad79
index 2e8b89a..0000000
05ad79
--- a/tests/expected/libmount/utils-fs-root-subdir2
05ad79
+++ /dev/null
05ad79
@@ -1 +0,0 @@
05ad79
-/etc: /etc
05ad79
diff --git a/tests/ts/libmount/utils b/tests/ts/libmount/utils
05ad79
index 6facaad..89ecf10 100755
05ad79
--- a/tests/ts/libmount/utils
05ad79
+++ b/tests/ts/libmount/utils
05ad79
@@ -64,18 +64,6 @@ ts_init_subtest "mountpoint-root"
05ad79
 ts_valgrind $TESTPROG --mountpoint / &> $TS_OUTPUT
05ad79
 ts_finalize_subtest
05ad79
 
05ad79
-ts_init_subtest "fs-root"
05ad79
-ts_valgrind $TESTPROG --fs-root /proc &> $TS_OUTPUT
05ad79
-ts_finalize_subtest
05ad79
-
05ad79
-ts_init_subtest "fs-root-subdir"
05ad79
-ts_valgrind $TESTPROG --fs-root /proc/sys/kernel &> $TS_OUTPUT
05ad79
-ts_finalize_subtest
05ad79
-
05ad79
-ts_init_subtest "fs-root-subdir2"
05ad79
-ts_valgrind $TESTPROG --fs-root /etc &> $TS_OUTPUT
05ad79
-ts_finalize_subtest
05ad79
-
05ad79
 ts_init_subtest "kernel-cmdline"
05ad79
 export LIBMOUNT_KERNEL_CMDLINE="$TS_SELF/files/kernel_cmdline"
05ad79
 ts_valgrind $TESTPROG --kernel-cmdline selinux= &>> $TS_OUTPUT
05ad79
-- 
05ad79
2.7.4
05ad79