]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: don't touch mtab when mount -n specified
authorKarel Zak <kzak@redhat.com>
Tue, 17 Jun 2014 13:38:14 +0000 (15:38 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 17 Jun 2014 13:38:14 +0000 (15:38 +0200)
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1109367
Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/context.c
libmount/src/context_loopdev.c
libmount/src/context_umount.c
libmount/src/mountP.h

index 1a04c1a29ee8a6c63923389e4bfe02c4dbafd844..65171eceb4b29dc17bc6b3ed9cbeea6ed743193e 100644 (file)
@@ -65,11 +65,6 @@ struct libmnt_context *mnt_new_context(void)
        DBG(CXT, ul_debugobj(cxt, "----> allocate %s",
                                cxt->restricted ? "[RESTRICTED]" : ""));
 
-       mnt_has_regular_mtab(&cxt->mtab_path, &cxt->mtab_writable);
-
-       if (!cxt->mtab_writable)
-               /* use /run/mount/utab if /etc/mtab is useless */
-               mnt_has_regular_utab(&cxt->utab_path, &cxt->utab_writable);
 
        return cxt;
 }
@@ -178,6 +173,7 @@ int mnt_reset_context(struct libmnt_context *cxt)
        cxt->flags |= (fl & MNT_FL_NOCANONICALIZE);
        cxt->flags |= (fl & MNT_FL_RDONLY_UMOUNT);
        cxt->flags |= (fl & MNT_FL_NOSWAPMATCH);
+       cxt->flags |= (fl & MNT_FL_TABPATHS_CHECKED);
        return 0;
 }
 
@@ -205,6 +201,59 @@ int mnt_context_reset_status(struct libmnt_context *cxt)
        return 0;
 }
 
+static int context_init_paths(struct libmnt_context *cxt, int writable)
+{
+       assert(cxt);
+
+       if (!cxt->mtab_path)
+               cxt->mtab_path = mnt_get_mtab_path();
+       if (!cxt->utab_path)
+               cxt->utab_path = mnt_get_utab_path();
+
+       if (!writable)
+               return 0;               /* only paths wanted */
+       if (mnt_context_is_nomtab(cxt))
+               return 0;               /* write mode overrided by mount -n */
+       if (cxt->flags & MNT_FL_TABPATHS_CHECKED)
+               return 0;
+
+       DBG(CXT, ul_debugobj(cxt, "checking for writable tab files"));
+
+       mnt_has_regular_mtab(&cxt->mtab_path, &cxt->mtab_writable);
+
+       if (!cxt->mtab_writable)
+               /* use /run/mount/utab if /etc/mtab is useless */
+               mnt_has_regular_utab(&cxt->utab_path, &cxt->utab_writable);
+
+       cxt->flags |= MNT_FL_TABPATHS_CHECKED;
+       return 0;
+}
+
+int mnt_context_mtab_writable(struct libmnt_context *cxt)
+{
+       assert(cxt);
+
+       context_init_paths(cxt, 1);
+       return cxt->mtab_writable == 1;
+}
+
+int mnt_context_utab_writable(struct libmnt_context *cxt)
+{
+       assert(cxt);
+
+       context_init_paths(cxt, 1);
+       return cxt->utab_writable == 1;
+}
+
+const char *mnt_context_get_writable_tabpath(struct libmnt_context *cxt)
+{
+       assert(cxt);
+
+       context_init_paths(cxt, 1);
+       return cxt->mtab_writable ? cxt->mtab_path : cxt->utab_path;
+}
+
+
 static int set_flag(struct libmnt_context *cxt, int flag, int enable)
 {
        assert(cxt);
@@ -978,6 +1027,8 @@ int mnt_context_get_mtab(struct libmnt_context *cxt, struct libmnt_table **tb)
        if (!cxt->mtab) {
                int rc;
 
+               context_init_paths(cxt, 0);
+
                cxt->mtab = mnt_new_table();
                if (!cxt->mtab)
                        return -ENOMEM;
@@ -1220,8 +1271,8 @@ struct libmnt_lock *mnt_context_get_lock(struct libmnt_context *cxt)
                return NULL;
 
        if (!cxt->lock) {
-               cxt->lock = mnt_new_lock(cxt->mtab_writable ?
-                               cxt->mtab_path : cxt->utab_path, 0);
+               cxt->lock = mnt_new_lock(
+                               mnt_context_get_writable_tabpath(cxt), 0);
                if (cxt->lock)
                        mnt_lock_block_signals(cxt->lock, TRUE);
        }
@@ -1740,7 +1791,7 @@ int mnt_context_prepare_update(struct libmnt_context *cxt)
                DBG(CXT, ul_debugobj(cxt, "skip update: NOMTAB flag"));
                return 0;
        }
-       if (!cxt->mtab_writable && !cxt->utab_writable) {
+       if (!mnt_context_get_writable_tabpath(cxt)) {
                DBG(CXT, ul_debugobj(cxt, "skip update: no writable destination"));
                return 0;
        }
@@ -1753,7 +1804,7 @@ int mnt_context_prepare_update(struct libmnt_context *cxt)
        }
 
        if (!cxt->update) {
-               const char *name = cxt->mtab_writable ? cxt->mtab_path : cxt->utab_path;
+               const char *name = mnt_context_get_writable_tabpath(cxt);
 
                if (cxt->action == MNT_ACT_UMOUNT && is_file_empty(name)) {
                        DBG(CXT, ul_debugobj(cxt,
@@ -1765,7 +1816,8 @@ int mnt_context_prepare_update(struct libmnt_context *cxt)
                if (!cxt->update)
                        return -ENOMEM;
 
-               mnt_update_set_filename(cxt->update, name, !cxt->mtab_writable);
+               mnt_update_set_filename(cxt->update, name,
+                               !mnt_context_mtab_writable(cxt));
        }
 
        if (cxt->action == MNT_ACT_UMOUNT)
@@ -1796,7 +1848,7 @@ int mnt_context_update_tabs(struct libmnt_context *cxt)
        /* check utab update when external helper executed */
        if (mnt_context_helper_executed(cxt)
            && mnt_context_get_helper_status(cxt) == 0
-           && cxt->utab_writable) {
+           && mnt_context_utab_writable(cxt)) {
 
                if (mnt_update_already_done(cxt->update, cxt->lock)) {
                        DBG(CXT, ul_debugobj(cxt, "don't update: error evaluate or already updated"));
index e8f637b29357da218c500d0112f73fca6325147a..f4525b3adac2d25a13710dc97b6851f13d51309e 100644 (file)
@@ -227,7 +227,7 @@ int mnt_context_setup_loopdev(struct libmnt_context *cxt)
         * because kernel provides the name in /sys.
         */
        if (get_linux_version() >= KERNEL_VERSION(2, 6, 37) ||
-           !cxt->mtab_writable) {
+           !mnt_context_mtab_writable(cxt)) {
                DBG(CXT, ul_debugobj(cxt, "enabling AUTOCLEAR flag"));
                lo_flags |= LO_FLAGS_AUTOCLEAR;
        }
index 7df4c2cca22f3bc28fcacfdffbf2b325c3691c3c..220f28bc6d291ee05915681ee4eb8ebf317ebfa9 100644 (file)
@@ -92,7 +92,7 @@ int mnt_context_find_umount_fs(struct libmnt_context *cxt,
         * where LABEL, UUID or symlinks are canonicalized. It means that
         * it's usable only for canonicalized stuff (e.g. kernel mountinfo).
         */
-       if (!cxt->mtab_writable && *tgt == '/' &&
+       if (!mnt_context_mtab_writable(cxt) && *tgt == '/' &&
            !mnt_context_is_force(cxt) && !mnt_context_is_lazy(cxt)) {
 
                struct stat st;
@@ -259,7 +259,7 @@ static int lookup_umount_fs(struct libmnt_context *cxt)
        if (!mnt_context_is_restricted(cxt)
            && *tgt == '/'
            && !(cxt->flags & MNT_FL_HELPER)
-           && !cxt->mtab_writable
+           && !mnt_context_mtab_writable(cxt)
            && !mnt_context_is_force(cxt)
            && !mnt_context_is_lazy(cxt)
            && stat(tgt, &st) == 0 && S_ISDIR(st.st_mode)
@@ -267,7 +267,7 @@ static int lookup_umount_fs(struct libmnt_context *cxt)
 
                const char *type = mnt_fs_get_fstype(cxt->fs);
 
-               /* !cxt->mtab_writable && has_utab_entry() verified that there
+               /* !mnt_context_mtab_writable(cxt) && has_utab_entry() verified that there
                 * is no stuff in utab, so disable all mtab/utab related actions */
                mnt_context_disable_mtab(cxt, TRUE);
 
@@ -865,7 +865,7 @@ int mnt_context_do_umount(struct libmnt_context *cxt)
                    && (cxt->mountflags & MS_REMOUNT)) {
 
                        /* use "remount" instead of "umount" in /etc/mtab */
-                       if (!rc && cxt->update && cxt->mtab_writable)
+                       if (!rc && cxt->update && mnt_context_mtab_writable(cxt))
                                rc = mnt_update_set_fs(cxt->update,
                                                       cxt->mountflags, NULL, cxt->fs);
                }
index d85a078defbed0caf2248a86ea7778cc214ce134..f2660e057459fa43de8cdbdcf6e0d28af045927c 100644 (file)
@@ -340,6 +340,7 @@ struct libmnt_context
 #define MNT_FL_HELPER          (1 << 25)       /* [u]mount.<type> */
 #define MNT_FL_LOOPDEV_READY   (1 << 26)       /* /dev/loop<N> initialized by the library */
 #define MNT_FL_MOUNTOPTS_FIXED  (1 << 27)
+#define MNT_FL_TABPATHS_CHECKED        (1 << 28)
 
 /* default flags */
 #define MNT_FL_DEFAULT         0
@@ -371,6 +372,10 @@ extern int __mnt_fs_set_fstype_ptr(struct libmnt_fs *fs, char *fstype)
                        __attribute__((nonnull(1)));
 
 /* context.c */
+extern int mnt_context_mtab_writable(struct libmnt_context *cxt);
+extern int mnt_context_utab_writable(struct libmnt_context *cxt);
+extern const char *mnt_context_get_writable_tabpath(struct libmnt_context *cxt);
+
 extern int mnt_context_prepare_srcpath(struct libmnt_context *cxt);
 extern int mnt_context_prepare_target(struct libmnt_context *cxt);
 extern int mnt_context_guess_fstype(struct libmnt_context *cxt);