]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - libmount/src/context.c
libmount: do not call umount helper on --fake
[thirdparty/util-linux.git] / libmount / src / context.c
index 1620e9c8e954c8e10dd8bd97185e20f1a948aee2..ec04191fccb20f56f36b382ebe4ef0d525a9da46 100644 (file)
@@ -191,7 +191,6 @@ int mnt_reset_context(struct libmnt_context *cxt)
  */
 int mnt_context_reset_status(struct libmnt_context *cxt)
 {
-       assert(cxt);
        if (!cxt)
                return -EINVAL;
 
@@ -219,7 +218,11 @@ static int context_init_paths(struct libmnt_context *cxt, int writable)
 
        DBG(CXT, ul_debugobj(cxt, "checking for writable tab files"));
 
+#ifdef USE_LIBMOUNT_FORCE_MOUNTINFO
+       cxt->mtab_writable = 0;
+#else
        mnt_has_regular_mtab(&cxt->mtab_path, &cxt->mtab_writable);
+#endif
 
        if (!cxt->mtab_writable)
                /* use /run/mount/utab if /etc/mtab is useless */
@@ -319,7 +322,6 @@ int mnt_context_is_restricted(struct libmnt_context *cxt)
  */
 int mnt_context_set_optsmode(struct libmnt_context *cxt, int mode)
 {
-       assert(cxt);
        if (!cxt)
                return -EINVAL;
        cxt->optsmode = mode;
@@ -335,7 +337,6 @@ int mnt_context_set_optsmode(struct libmnt_context *cxt, int mode)
 
 int mnt_context_get_optsmode(struct libmnt_context *cxt)
 {
-       assert(cxt);
        return cxt->optsmode;
 }
 
@@ -717,7 +718,6 @@ int mnt_context_set_fs(struct libmnt_context *cxt, struct libmnt_fs *fs)
  */
 struct libmnt_fs *mnt_context_get_fs(struct libmnt_context *cxt)
 {
-       assert(cxt);
        if (!cxt)
                return NULL;
        if (!cxt->fs)
@@ -733,7 +733,6 @@ struct libmnt_fs *mnt_context_get_fs(struct libmnt_context *cxt)
  */
 void *mnt_context_get_fs_userdata(struct libmnt_context *cxt)
 {
-       assert(cxt);
        return cxt->fs ? mnt_fs_get_userdata(cxt->fs) : NULL;
 }
 
@@ -745,7 +744,6 @@ void *mnt_context_get_fs_userdata(struct libmnt_context *cxt)
  */
 void *mnt_context_get_fstab_userdata(struct libmnt_context *cxt)
 {
-       assert(cxt);
        return cxt->fstab ? mnt_table_get_userdata(cxt->fstab) : NULL;
 }
 
@@ -757,7 +755,6 @@ void *mnt_context_get_fstab_userdata(struct libmnt_context *cxt)
  */
 void *mnt_context_get_mtab_userdata(struct libmnt_context *cxt)
 {
-       assert(cxt);
        return cxt->mtab ? mnt_table_get_userdata(cxt->mtab) : NULL;
 }
 
@@ -775,7 +772,6 @@ void *mnt_context_get_mtab_userdata(struct libmnt_context *cxt)
  */
 int mnt_context_set_source(struct libmnt_context *cxt, const char *source)
 {
-       assert(cxt);
        return mnt_fs_set_source(mnt_context_get_fs(cxt), source);
 }
 
@@ -787,7 +783,6 @@ int mnt_context_set_source(struct libmnt_context *cxt, const char *source)
  */
 const char *mnt_context_get_source(struct libmnt_context *cxt)
 {
-       assert(cxt);
        return mnt_fs_get_source(mnt_context_get_fs(cxt));
 }
 
@@ -800,7 +795,6 @@ const char *mnt_context_get_source(struct libmnt_context *cxt)
  */
 int mnt_context_set_target(struct libmnt_context *cxt, const char *target)
 {
-       assert(cxt);
        return mnt_fs_set_target(mnt_context_get_fs(cxt), target);
 }
 
@@ -812,7 +806,6 @@ int mnt_context_set_target(struct libmnt_context *cxt, const char *target)
  */
 const char *mnt_context_get_target(struct libmnt_context *cxt)
 {
-       assert(cxt);
        return mnt_fs_get_target(mnt_context_get_fs(cxt));
 }
 
@@ -829,7 +822,6 @@ const char *mnt_context_get_target(struct libmnt_context *cxt)
  */
 int mnt_context_set_fstype(struct libmnt_context *cxt, const char *fstype)
 {
-       assert(cxt);
        return mnt_fs_set_fstype(mnt_context_get_fs(cxt), fstype);
 }
 
@@ -841,7 +833,6 @@ int mnt_context_set_fstype(struct libmnt_context *cxt, const char *fstype)
  */
 const char *mnt_context_get_fstype(struct libmnt_context *cxt)
 {
-       assert(cxt);
        return mnt_fs_get_fstype(mnt_context_get_fs(cxt));
 }
 
@@ -854,7 +845,6 @@ const char *mnt_context_get_fstype(struct libmnt_context *cxt)
  */
 int mnt_context_set_options(struct libmnt_context *cxt, const char *optstr)
 {
-       assert(cxt);
        return mnt_fs_set_options(mnt_context_get_fs(cxt), optstr);
 }
 
@@ -867,7 +857,6 @@ int mnt_context_set_options(struct libmnt_context *cxt, const char *optstr)
  */
 int mnt_context_append_options(struct libmnt_context *cxt, const char *optstr)
 {
-       assert(cxt);
        return mnt_fs_append_options(mnt_context_get_fs(cxt), optstr);
 }
 
@@ -886,7 +875,6 @@ int mnt_context_append_options(struct libmnt_context *cxt, const char *optstr)
  */
 const char *mnt_context_get_options(struct libmnt_context *cxt)
 {
-       assert(cxt);
        return mnt_fs_get_options(mnt_context_get_fs(cxt));
 }
 
@@ -903,7 +891,6 @@ int mnt_context_set_fstype_pattern(struct libmnt_context *cxt, const char *patte
 {
        char *p = NULL;
 
-       assert(cxt);
        if (!cxt)
                return -EINVAL;
        if (pattern) {
@@ -929,7 +916,6 @@ int mnt_context_set_options_pattern(struct libmnt_context *cxt, const char *patt
 {
        char *p = NULL;
 
-       assert(cxt);
        if (!cxt)
                return -EINVAL;
        if (pattern) {
@@ -965,7 +951,6 @@ int mnt_context_set_options_pattern(struct libmnt_context *cxt, const char *patt
  */
 int mnt_context_set_fstab(struct libmnt_context *cxt, struct libmnt_table *tb)
 {
-       assert(cxt);
        if (!cxt)
                return -EINVAL;
 
@@ -987,7 +972,6 @@ int mnt_context_set_fstab(struct libmnt_context *cxt, struct libmnt_table *tb)
  */
 int mnt_context_get_fstab(struct libmnt_context *cxt, struct libmnt_table **tb)
 {
-       assert(cxt);
        if (!cxt)
                return -EINVAL;
        if (!cxt->fstab) {
@@ -1021,7 +1005,6 @@ int mnt_context_get_fstab(struct libmnt_context *cxt, struct libmnt_table **tb)
  */
 int mnt_context_get_mtab(struct libmnt_context *cxt, struct libmnt_table **tb)
 {
-       assert(cxt);
        if (!cxt)
                return -EINVAL;
        if (!cxt->mtab) {
@@ -1113,7 +1096,6 @@ int mnt_context_set_tabfilter(struct libmnt_context *cxt,
                              int (*fltr)(struct libmnt_fs *, void *),
                              void *data)
 {
-       assert(cxt);
        if (!cxt)
                return -EINVAL;
 
@@ -1153,8 +1135,6 @@ int mnt_context_get_table(struct libmnt_context *cxt,
 {
        int rc;
 
-       assert(cxt);
-       assert(tb);
        if (!cxt || !tb)
                return -EINVAL;
 
@@ -1192,7 +1172,6 @@ int mnt_context_get_table(struct libmnt_context *cxt,
 int mnt_context_set_tables_errcb(struct libmnt_context *cxt,
        int (*cb)(struct libmnt_table *tb, const char *filename, int line))
 {
-       assert(cxt);
        if (!cxt)
                return -EINVAL;
 
@@ -1250,7 +1229,6 @@ int mnt_context_set_cache(struct libmnt_context *cxt, struct libmnt_cache *cache
  */
 struct libmnt_cache *mnt_context_get_cache(struct libmnt_context *cxt)
 {
-       assert(cxt);
        if (!cxt || mnt_context_is_nocanonicalize(cxt))
                return NULL;
 
@@ -1277,7 +1255,6 @@ int mnt_context_set_passwd_cb(struct libmnt_context *cxt,
                              char *(*get)(struct libmnt_context *),
                              void (*release)(struct libmnt_context *, char *))
 {
-       assert(cxt);
        if (!cxt)
                return -EINVAL;
        cxt->pwd_get_cb = get;
@@ -1307,7 +1284,6 @@ int mnt_context_set_passwd_cb(struct libmnt_context *cxt,
  */
 struct libmnt_lock *mnt_context_get_lock(struct libmnt_context *cxt)
 {
-       assert(cxt);
        /*
         * DON'T call this function within libmount, it will always allocate
         * the lock. The mnt_update_* functions are able to allocate the lock
@@ -1347,7 +1323,6 @@ struct libmnt_lock *mnt_context_get_lock(struct libmnt_context *cxt)
  */
 int mnt_context_set_mflags(struct libmnt_context *cxt, unsigned long flags)
 {
-       assert(cxt);
        if (!cxt)
                return -EINVAL;
 
@@ -1380,8 +1355,6 @@ int mnt_context_get_mflags(struct libmnt_context *cxt, unsigned long *flags)
        int rc = 0;
        struct list_head *p;
 
-       assert(cxt);
-       assert(flags);
        if (!cxt || !flags)
                return -EINVAL;
 
@@ -1439,8 +1412,6 @@ int mnt_context_get_user_mflags(struct libmnt_context *cxt, unsigned long *flags
 {
        int rc = 0;
 
-       assert(cxt);
-       assert(flags);
        if (!cxt || !flags)
                return -EINVAL;
 
@@ -1472,7 +1443,6 @@ int mnt_context_get_user_mflags(struct libmnt_context *cxt, unsigned long *flags
  */
 int mnt_context_set_mountdata(struct libmnt_context *cxt, void *data)
 {
-       assert(cxt);
        if (!cxt)
                return -EINVAL;
        cxt->mountdata = data;
@@ -1647,8 +1617,9 @@ int mnt_context_prepare_target(struct libmnt_context *cxt)
        return 0;
 }
 
-/* Guess type, but not set to cxt->fs, use free() for the result. It's no error
- * when we're not able to guess a filesystem type.
+/* Guess type, but not set to cxt->fs, always use free() for the result. It's
+ * no error when we're not able to guess a filesystem type. Note that error
+ * does not mean that result in @type is NULL.
  */
 int mnt_context_guess_srcpath_fstype(struct libmnt_context *cxt, char **type)
 {
@@ -1694,6 +1665,8 @@ int mnt_context_guess_fstype(struct libmnt_context *cxt)
        assert(cxt->fs);
        assert((cxt->flags & MNT_FL_MOUNTFLAGS_MERGED));
 
+       DBG(CXT, ul_debugobj(cxt, "preparing fstype"));
+
        if (!cxt || !cxt->fs)
                return -EINVAL;
 
@@ -1709,7 +1682,7 @@ int mnt_context_guess_fstype(struct libmnt_context *cxt)
 
        if (type)
                goto done;
-       if (cxt->flags & MS_REMOUNT)
+       if (cxt->mountflags & MS_REMOUNT)
                goto none;
        if (cxt->fstype_pattern)
                goto done;
@@ -1843,10 +1816,10 @@ int mnt_context_prepare_update(struct libmnt_context *cxt)
 
        target = mnt_fs_get_target(cxt->fs);
 
-       if (cxt->action == MNT_ACT_UMOUNT && target && !strcmp(target, "/"))
-               /* Don't try to touch mtab if umounting root FS */
+       if (cxt->action == MNT_ACT_UMOUNT && target && !strcmp(target, "/")) {
+               DBG(CXT, ul_debugobj(cxt, "root umount: setting NOMTAB"));
                mnt_context_disable_mtab(cxt, TRUE);
-
+       }
        if (mnt_context_is_nomtab(cxt)) {
                DBG(CXT, ul_debugobj(cxt, "skip update: NOMTAB flag"));
                return 0;
@@ -1993,6 +1966,9 @@ static int apply_table(struct libmnt_context *cxt, struct libmnt_table *tb,
        if (!rc && !mnt_fs_get_fstype(cxt->fs))
                rc = mnt_fs_set_fstype(cxt->fs, mnt_fs_get_fstype(fs));
 
+       if (!rc && !mnt_fs_get_root(cxt->fs) && mnt_fs_get_root(fs))
+               rc = mnt_fs_set_root(cxt->fs, mnt_fs_get_root(fs));
+
        if (rc)
                return rc;
 
@@ -2022,15 +1998,12 @@ static int apply_table(struct libmnt_context *cxt, struct libmnt_table *tb,
  */
 int mnt_context_apply_fstab(struct libmnt_context *cxt)
 {
-       int rc = -1;
+       int rc = -1, isremount = 0;
        struct libmnt_table *tab = NULL;
        const char *src = NULL, *tgt = NULL;
        unsigned long mflags = 0;
 
-       assert(cxt);
-       assert(cxt->fs);
-
-       if (!cxt)
+       if (!cxt || !cxt->fs)
                return -EINVAL;
 
        if (mnt_context_tab_applied(cxt))       /* already applied */
@@ -2048,12 +2021,8 @@ int mnt_context_apply_fstab(struct libmnt_context *cxt)
                cxt->optsmode &= ~MNT_OMODE_FORCE;
        }
 
-       if (mnt_context_get_mflags(cxt, &mflags) == 0 && mflags & MS_REMOUNT) {
-               /* preffer mtab on remount */
-               DBG(CXT, ul_debugobj(cxt, "force mtab parsing on remount"));
-               cxt->optsmode |= MNT_OMODE_MTAB;
-               cxt->optsmode &= ~MNT_OMODE_FSTAB;
-       }
+       if (mnt_context_get_mflags(cxt, &mflags) == 0 && mflags & MS_REMOUNT)
+               isremount = 1;
 
        if (cxt->fs) {
                src = mnt_fs_get_source(cxt->fs);
@@ -2096,7 +2065,8 @@ int mnt_context_apply_fstab(struct libmnt_context *cxt)
        }
 
        /* try mtab */
-       if (rc < 0 && (cxt->optsmode & MNT_OMODE_MTAB)) {
+       if (rc < 0 && (cxt->optsmode & MNT_OMODE_MTAB)
+           && (isremount || cxt->action == MNT_ACT_UMOUNT)) {
                DBG(CXT, ul_debugobj(cxt, "trying to apply mtab (src=%s, target=%s)", src, tgt));
                if (tgt)
                        rc = mnt_context_get_mtab_for_target(cxt, &tab, tgt);
@@ -2106,6 +2076,13 @@ int mnt_context_apply_fstab(struct libmnt_context *cxt)
                        rc = apply_table(cxt, tab, MNT_ITER_BACKWARD);
        }
        if (rc) {
+               if (!mnt_context_is_restricted(cxt)
+                   && tgt && !src
+                   && isremount) {
+                       DBG(CXT, ul_debugobj(cxt, "only target; ignore missing mtab entry on remount"));
+                       return 0;
+               }
+
                DBG(CXT, ul_debugobj(cxt, "failed to find entry in fstab/mtab [rc=%d]: %m", rc));
 
                /* force to "not found in fstab/mtab" error, the details why
@@ -2124,7 +2101,6 @@ int mnt_context_apply_fstab(struct libmnt_context *cxt)
  */
 int mnt_context_tab_applied(struct libmnt_context *cxt)
 {
-       assert(cxt);
        return cxt->flags & MNT_FL_TAB_APPLIED;
 }
 
@@ -2135,9 +2111,6 @@ int mnt_context_tab_applied(struct libmnt_context *cxt)
  */
 int mnt_context_propagation_only(struct libmnt_context *cxt)
 {
-       assert(cxt);
-       assert(cxt->fs);
-
        if (cxt->action != MNT_ACT_MOUNT)
                return 0;
 
@@ -2166,7 +2139,6 @@ int mnt_context_propagation_only(struct libmnt_context *cxt)
  */
 int mnt_context_get_status(struct libmnt_context *cxt)
 {
-       assert(cxt);
        return !cxt->syscall_status || !cxt->helper_exec_status;
 }
 
@@ -2178,7 +2150,6 @@ int mnt_context_get_status(struct libmnt_context *cxt)
  */
 int mnt_context_helper_executed(struct libmnt_context *cxt)
 {
-       assert(cxt);
        return cxt->helper_exec_status != 1;
 }
 
@@ -2191,7 +2162,6 @@ int mnt_context_helper_executed(struct libmnt_context *cxt)
  */
 int mnt_context_get_helper_status(struct libmnt_context *cxt)
 {
-       assert(cxt);
        return cxt->helper_status;
 }
 
@@ -2203,7 +2173,6 @@ int mnt_context_get_helper_status(struct libmnt_context *cxt)
  */
 int mnt_context_syscall_called(struct libmnt_context *cxt)
 {
-       assert(cxt);
        return cxt->syscall_status != 1;
 }
 
@@ -2218,7 +2187,6 @@ int mnt_context_syscall_called(struct libmnt_context *cxt)
  */
 int mnt_context_get_syscall_errno(struct libmnt_context *cxt)
 {
-       assert(cxt);
        if (cxt->syscall_status < 0)
                return -cxt->syscall_status;
        return 0;
@@ -2238,7 +2206,6 @@ int mnt_context_get_syscall_errno(struct libmnt_context *cxt)
  */
 int mnt_context_set_syscall_status(struct libmnt_context *cxt, int status)
 {
-       assert(cxt);
        if (!cxt)
                return -EINVAL;
 
@@ -2287,7 +2254,8 @@ int mnt_context_init_helper(struct libmnt_context *cxt, int action,
 {
        int rc;
 
-       assert(cxt);
+       if (!cxt)
+               return -EINVAL;
 
        rc = mnt_context_disable_helpers(cxt, TRUE);
        if (!rc)
@@ -2340,7 +2308,6 @@ int mnt_context_is_fs_mounted(struct libmnt_context *cxt,
        struct libmnt_table *mtab;
        int rc;
 
-       assert(cxt);
        if (!cxt || !fs || !mounted)
                return -EINVAL;
 
@@ -2356,7 +2323,6 @@ static int mnt_context_add_child(struct libmnt_context *cxt, pid_t pid)
 {
        pid_t *pids;
 
-       assert(cxt);
        if (!cxt)
                return -EINVAL;
 
@@ -2410,7 +2376,6 @@ int mnt_context_wait_for_children(struct libmnt_context *cxt,
 {
        int i;
 
-       assert(cxt);
        if (!cxt)
                return -EINVAL;