From: Karel Zak Date: Tue, 16 Feb 2016 14:44:31 +0000 (+0100) Subject: libmount: fix mnt_table_is_fs_mounted() loopdev use X-Git-Tag: v2.28-rc1~96 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dc7c3d658782f6c2a409c6e76e32db3afc86c4ed;p=thirdparty%2Futil-linux.git libmount: fix mnt_table_is_fs_mounted() loopdev use The function does not detect already mounted loop devices on systems with regular /etc/mtab file. The patch also improves test_is_mounted() to be useful with mtab. Reported-by: Ruediger Meier Signed-off-by: Karel Zak --- diff --git a/libmount/src/tab.c b/libmount/src/tab.c index 729a99fc08..f9d8b064f8 100644 --- a/libmount/src/tab.c +++ b/libmount/src/tab.c @@ -1485,7 +1485,7 @@ int mnt_table_is_fs_mounted(struct libmnt_table *tb, struct libmnt_fs *fstab_fs) int rc = 0; dev_t devno = 0; - DBG(FS, ul_debugobj(fstab_fs, "is FS mounted? [target=%s, source=%s]", + DBG(FS, ul_debugobj(fstab_fs, "mnt_table_is_fs_mounted: target=%s, source=%s", mnt_fs_get_target(fstab_fs), mnt_fs_get_source(fstab_fs))); @@ -1529,7 +1529,7 @@ int mnt_table_is_fs_mounted(struct libmnt_table *tb, struct libmnt_fs *fstab_fs) } mnt_reset_iter(&itr, MNT_ITER_FORWARD); - DBG(FS, ul_debugobj(fstab_fs, "is mounted: src=%s, tgt=%s, root=%s", src, tgt, root)); + DBG(FS, ul_debugobj(fstab_fs, "mnt_table_is_fs_mounted: src=%s, tgt=%s, root=%s", src, tgt, root)); while (mnt_table_next_fs(tb, &itr, &fs) == 0) { @@ -1545,19 +1545,19 @@ int mnt_table_is_fs_mounted(struct libmnt_table *tb, struct libmnt_fs *fstab_fs) uint64_t offset = 0; char *val; size_t len; - int flags; + int flags = 0; - if (!mnt_fs_is_kernel(fs) || - !mnt_fs_get_srcpath(fs) || + if (!mnt_fs_get_srcpath(fs) || !startswith(mnt_fs_get_srcpath(fs), "/dev/loop")) continue; /* does not look like loopdev */ - if (mnt_fs_get_option(fstab_fs, "offset", &val, &len) == 0 && - mnt_parse_offset(val, len, &offset)) { - DBG(FS, ul_debugobj(fstab_fs, "failed to parse offset=")); - continue; - } else + if (mnt_fs_get_option(fstab_fs, "offset", &val, &len) == 0) { + if (mnt_parse_offset(val, len, &offset)) { + DBG(FS, ul_debugobj(fstab_fs, "failed to parse offset=")); + continue; + } flags = LOOPDEV_FL_OFFSET; + } DBG(FS, ul_debugobj(fs, "checking for loop: src=%s", mnt_fs_get_srcpath(fs))); #if __linux__ @@ -1808,9 +1808,14 @@ static int test_is_mounted(struct libmnt_test *ts, int argc, char *argv[]) struct libmnt_fs *fs; struct libmnt_iter *itr = NULL; struct libmnt_cache *mpc = NULL; - int rc; + int rc, writable = 0; + const char *path = NULL; + + if (mnt_has_regular_mtab(&path, &writable) == 1 && writable == 0) + tb = mnt_new_table_from_file(path); + else + tb = mnt_new_table_from_file("/proc/self/mountinfo"); - tb = mnt_new_table_from_file("/proc/self/mountinfo"); if (!tb) { fprintf(stderr, "failed to parse mountinfo\n"); return -1; @@ -1830,7 +1835,7 @@ static int test_is_mounted(struct libmnt_test *ts, int argc, char *argv[]) mnt_table_set_cache(tb, mpc); mnt_unref_cache(mpc); - while(mnt_table_next_fs(fstab, itr, &fs) == 0) { + while (mnt_table_next_fs(fstab, itr, &fs) == 0) { if (mnt_table_is_fs_mounted(tb, fs)) printf("%s already mounted on %s\n", mnt_fs_get_source(fs), @@ -1900,7 +1905,7 @@ int main(int argc, char *argv[]) { "--find-pair", test_find_pair, " " }, { "--find-mountpoint", test_find_mountpoint, "" }, { "--copy-fs", test_copy_fs, " copy root FS from the file" }, - { "--is-mounted", test_is_mounted, " check what from are already mounted" }, + { "--is-mounted", test_is_mounted, " check what from fstab is already mounted" }, { NULL } }; diff --git a/libmount/src/tab_parse.c b/libmount/src/tab_parse.c index 4c473905f2..c67479b3b9 100644 --- a/libmount/src/tab_parse.c +++ b/libmount/src/tab_parse.c @@ -843,6 +843,7 @@ struct libmnt_table *__mnt_new_table_from_file(const char *filename, int fmt) return NULL; tb = mnt_new_table(); if (tb) { + DBG(TAB, ul_debugobj(tb, "new tab for file: %s", filename)); tb->fmt = fmt; if (mnt_table_parse_file(tb, filename) != 0) { mnt_unref_table(tb); diff --git a/libmount/src/utils.c b/libmount/src/utils.c index 39f6c851e4..50cca0671d 100644 --- a/libmount/src/utils.c +++ b/libmount/src/utils.c @@ -785,6 +785,7 @@ int mnt_has_regular_mtab(const char **mtab, int *writable) if (S_ISREG(st.st_mode)) { if (writable) *writable = !try_write(filename); + DBG(UTILS, ul_debug("%s: writable", filename)); return 1; } goto done; @@ -793,8 +794,10 @@ int mnt_has_regular_mtab(const char **mtab, int *writable) /* try to create the file */ if (writable) { *writable = !try_write(filename); - if (*writable) + if (*writable) { + DBG(UTILS, ul_debug("%s: writable", filename)); return 1; + } } done: