From: Karel Zak Date: Thu, 12 Aug 2021 08:58:18 +0000 (+0200) Subject: libmount: use /run/mount/tmptgt rather than /tmp/mount/mount. X-Git-Tag: v2.38-rc1~305 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7ae195affb0ea5fb9b777a7a2e89f4d2a69bd578;p=thirdparty%2Futil-linux.git libmount: use /run/mount/tmptgt rather than /tmp/mount/mount. The unshared namespace is per-process, so we can use the same directory in all mount(8) instances. It's unnecessary to create and remove process specific directory. Signed-off-by: Karel Zak --- diff --git a/libmount/src/context.c b/libmount/src/context.c index 8d1250f7c9..942f7b4e22 100644 --- a/libmount/src/context.c +++ b/libmount/src/context.c @@ -154,7 +154,6 @@ int mnt_reset_context(struct libmnt_context *cxt) free(cxt->helper); free(cxt->orig_user); free(cxt->subdir); - free(cxt->tmptgt); cxt->fs = NULL; cxt->mtab = NULL; @@ -164,7 +163,6 @@ int mnt_reset_context(struct libmnt_context *cxt) cxt->mountflags = 0; cxt->user_mountflags = 0; cxt->mountdata = NULL; - cxt->tmptgt = NULL; cxt->subdir = NULL; cxt->flags = MNT_FL_DEFAULT; @@ -298,8 +296,6 @@ struct libmnt_context *mnt_copy_context(struct libmnt_context *o) goto failed; if (strdup_between_structs(n, o, subdir)) goto failed; - if (strdup_between_structs(n, o, tmptgt)) - goto failed; n->mountflags = o->mountflags; n->mountdata = o->mountdata; @@ -1887,8 +1883,6 @@ static int is_subdir_required(struct libmnt_context *cxt, int *rc) cxt->subdir = strndup(dir, sz); if (!cxt->subdir) *rc = -ENOMEM; - else if (asprintf(&cxt->tmptgt, "%s/mount.%d", MNT_TMPDIR, getpid()) < 0) - *rc = -ENOMEM; return *rc == 0; } @@ -2013,8 +2007,7 @@ int mnt_context_prepare_target(struct libmnt_context *cxt) && (cxt->user_mountflags & MNT_MS_XFSTABCOMM) && is_subdir_required(cxt, &rc)) { - DBG(CXT, ul_debugobj(cxt, "subdir %s required, temporary target: %s", - cxt->subdir, cxt->tmptgt)); + DBG(CXT, ul_debugobj(cxt, "subdir %s required", cxt->subdir)); } diff --git a/libmount/src/context_mount.c b/libmount/src/context_mount.c index 78e5aa87aa..a0f1efa319 100644 --- a/libmount/src/context_mount.c +++ b/libmount/src/context_mount.c @@ -855,11 +855,10 @@ static int do_mount(struct libmnt_context *cxt, const char *try_type) /* create unhared temporary target */ if (cxt->subdir) { - rc = mnt_unshared_mkdir(cxt->tmptgt, - S_IRWXU, &old_ns_fd); + rc = mnt_tmptgt_unshare(&old_ns_fd); if (rc) return rc; - target = cxt->tmptgt; + target = MNT_PATH_TMPTGT; } DBG(CXT, ul_debugobj(cxt, "mount(2) " @@ -894,10 +893,10 @@ static int do_mount(struct libmnt_context *cxt, const char *try_type) */ if (cxt->subdir) { target = mnt_fs_get_target(cxt->fs); - rc = do_mount_subdir(cxt, cxt->tmptgt, cxt->subdir, target); + rc = do_mount_subdir(cxt, MNT_PATH_TMPTGT, cxt->subdir, target); if (rc) goto done; - mnt_unshared_rmdir(cxt->tmptgt, old_ns_fd); + mnt_tmptgt_cleanup(old_ns_fd); old_ns_fd = -1; } } @@ -910,7 +909,7 @@ static int do_mount(struct libmnt_context *cxt, const char *try_type) done: if (old_ns_fd >= 0) - mnt_unshared_rmdir(cxt->tmptgt, old_ns_fd); + mnt_tmptgt_cleanup(old_ns_fd); return rc; } diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h index a2b74b372b..b47640daac 100644 --- a/libmount/src/mountP.h +++ b/libmount/src/mountP.h @@ -74,10 +74,11 @@ UL_DEBUG_DECLARE_MASK(libmount); #define MNT_MNTTABDIR_EXT ".fstab" /* library private paths */ -#define MNT_TMPDIR "/tmp/mount" #define MNT_RUNTIME_TOPDIR "/run" - +/* private userspace mount table */ #define MNT_PATH_UTAB MNT_RUNTIME_TOPDIR "/mount/utab" +/* temporary mount target */ +#define MNT_PATH_TMPTGT MNT_RUNTIME_TOPDIR "/mount/tmptgt" #define MNT_UTAB_HEADER "# libmount utab file\n" @@ -122,8 +123,8 @@ extern char *mnt_get_kernel_cmdline_option(const char *name); extern int mnt_stat_mountpoint(const char *target, struct stat *st); extern int mnt_lstat_mountpoint(const char *target, struct stat *st); -extern int mnt_unshared_mkdir(const char *path, mode_t mode, int *old_ns_fd); -extern int mnt_unshared_rmdir(const char *path, int old_ns_fd); +extern int mnt_tmptgt_unshare(int *old_ns_fd); +extern int mnt_tmptgt_cleanup(int old_ns_fd); /* tab.c */ extern int is_mountinfo(struct libmnt_table *tb); @@ -292,7 +293,6 @@ struct libmnt_context char *optstr_pattern; /* for mnt_match_options() */ char *subdir; /* X-mount.subdir= */ - char *tmptgt; /* (unshared) private mount target */ struct libmnt_fs *fs; /* filesystem description (type, mountpoint, device, ...) */ struct libmnt_fs *fs_template; /* used for @fs on mnt_reset_context() */ diff --git a/libmount/src/utils.c b/libmount/src/utils.c index 7da7a5093b..1a8a294636 100644 --- a/libmount/src/utils.c +++ b/libmount/src/utils.c @@ -1136,20 +1136,19 @@ done: } /* - * like ul_mkdir_p(), but create a new namespace and mark (bind mount) - * the directory as private. + * Initialize MNT_PATH_TMPTGT; mkdir, create a new namespace and + * mark (bind mount) the directory as private. */ -int mnt_unshared_mkdir(const char *path, mode_t mode, int *old_ns_fd) +int mnt_tmptgt_unshare(int *old_ns_fd) { int rc = 0, fd = -1, mounted = 0; - *old_ns_fd = -1; + assert(old_ns_fd); - if (!path || !old_ns_fd) - return -EINVAL; + *old_ns_fd = -1; /* create directory */ - rc = ul_mkdir_p(path, mode); + rc = ul_mkdir_p(MNT_PATH_TMPTGT, S_IRWXU); if (rc) goto fail; @@ -1163,46 +1162,41 @@ int mnt_unshared_mkdir(const char *path, mode_t mode, int *old_ns_fd) goto fail; /* make the directory private */ - mounted = mount(path, path, "none", MS_BIND, NULL) == 0; + mounted = mount(MNT_PATH_TMPTGT, MNT_PATH_TMPTGT, "none", MS_BIND, NULL) == 0; if (!mounted) goto fail; - if (mount("none", path, NULL, MS_PRIVATE, NULL) != 0) + if (mount("none", MNT_PATH_TMPTGT, NULL, MS_PRIVATE, NULL) != 0) goto fail; - DBG(UTILS, ul_debug(" %s unshared", path)); + DBG(UTILS, ul_debug(MNT_PATH_TMPTGT " unshared")); *old_ns_fd = fd; return 0; fail: if (rc == 0) rc = errno ? -errno : -EINVAL; if (mounted) - umount(path); + umount(MNT_PATH_TMPTGT); if (fd >= 0) { setns(fd, CLONE_NEWNS); /* restore original NS */ close(fd); } - rmdir(path); - DBG(UTILS, ul_debug(" %s unshare failed", path)); + DBG(UTILS, ul_debug(MNT_PATH_TMPTGT " unshare failed")); return rc; } /* - * umount, rmdir and switch back to old namespace + * Clean up MNT_PATH_TMPTGT; umount and switch back to old namespace */ -int mnt_unshared_rmdir(const char *path, int old_ns_fd) +int mnt_tmptgt_cleanup(int old_ns_fd) { - if (!path) - return -EINVAL; - - umount(path); - rmdir(path); + umount(MNT_PATH_TMPTGT); if (old_ns_fd >= 0) { setns(old_ns_fd, CLONE_NEWNS); close(old_ns_fd); } - DBG(UTILS, ul_debug(" %s removed", path)); + DBG(UTILS, ul_debug(MNT_PATH_TMPTGT " cleanup done")); return 0; }