From: Karel Zak Date: Sun, 19 Dec 2010 20:22:51 +0000 (+0100) Subject: mount: use libmount for mtab/utab updates X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c9cf41e4f2b263bbf8e0ed0fbc90163988740a3f;p=util-linux mount: use libmount for mtab/utab updates Signed-off-by: Karel Zak --- diff --git a/mount/fstab.c b/mount/fstab.c index 0156017e..74cbf21b 100644 --- a/mount/fstab.c +++ b/mount/fstab.c @@ -564,6 +564,21 @@ setlkw_timeout (int sig) { /* nothing, fcntl will fail anyway */ } +#ifdef HAVE_LIBMOUNT_MOUNT +static mnt_lock *libmount_lock; + +mnt_lock * +init_libmount_lock(const char *filename) +{ + if (filename) + return libmount_lock = mnt_new_lock(filename, 0); + if (libmount_lock) + mnt_free_lock(libmount_lock); + libmount_lock = NULL; + return NULL; +} +#endif + /* Remove lock file. */ void unlock_mtab (void) { @@ -573,6 +588,10 @@ unlock_mtab (void) { unlink (_PATH_MOUNTED_LOCK); we_created_lockfile = 0; } +#ifdef HAVE_LIBMOUNT_MOUNT + if (libmount_lock) + mnt_unlock_file(libmount_lock); +#endif } /* Create the lock file. diff --git a/mount/fstab.h b/mount/fstab.h index e9855064..f8ec4ef2 100644 --- a/mount/fstab.h +++ b/mount/fstab.h @@ -2,6 +2,13 @@ #define MOUNT_FSTAB_H #include "mount_mntent.h" + +#ifdef HAVE_LIBMOUNT_MOUNT +#define USE_UNSTABLE_LIBMOUNT_API +#include /* libmount */ +extern mnt_lock *init_libmount_lock(const char *filename); +#endif + int mtab_is_writable(void); int mtab_is_a_symlink(void); int mtab_does_not_exist(void); diff --git a/mount/mount.c b/mount/mount.c index 8c30263c..c4955ffd 100644 --- a/mount/mount.c +++ b/mount/mount.c @@ -1302,6 +1302,50 @@ loop_check(const char **spec, const char **type, int *flags, return 0; } +#ifdef HAVE_LIBMOUNT_MOUNT +static mnt_update * +prepare_mtab_entry(const char *spec, const char *node, const char *type, + const char *opts, int flags) +{ + mnt_update *upd = mnt_new_update(); + mnt_fs *fs = mnt_new_fs(); + + if (!upd || !fs) + goto nothing; + + mnt_fs_set_source(fs, spec); + mnt_fs_set_target(fs, node); + mnt_fs_set_fstype(fs, type); + mnt_fs_set_options(fs, opts); + + if (mnt_update_set_fs(upd, flags, NULL, fs)) + goto nothing; + + mnt_free_fs(fs); + return upd; +nothing: + mnt_free_update(upd); + mnt_free_fs(fs); + return NULL; +} + +static void update_mtab_entry(mnt_update *upd, int flags) +{ + unsigned long fl = mnt_update_get_mountflags(upd); + mnt_lock *lc; + + if ((flags & MS_RDONLY) != (fl & MS_RDONLY)) + mnt_update_force_rdonly(upd, flags & MS_RDONLY); + + lc = init_libmount_lock( mnt_update_get_filename(upd) ); + + mnt_update_tab(upd, lc); + + init_libmount_lock(NULL); + mnt_free_update(upd); +} + +#else /*!HAVE_LIBMOUNT_MOUNT */ static void update_mtab_entry(const char *spec, const char *node, const char *type, const char *opts, int flags, int freq, int pass) { @@ -1354,6 +1398,7 @@ update_mtab_entry(const char *spec, const char *node, const char *type, my_free(mnt.mnt_fsname); my_free(mnt.mnt_dir); } +#endif /* !HAVE_LIBMOUNT_MOUNT */ static void set_pfd(char *s) { @@ -1438,7 +1483,9 @@ try_mount_one (const char *spec0, const char *node0, const char *types0, int loop = 0; const char *loopdev = 0, *loopfile = 0; struct stat statbuf; - +#ifdef HAVE_LIBMOUNT_MOUNT + mnt_update *upd = NULL; +#endif /* copies for freeing on exit */ const char *opts1, *spec1, *node1, *types1, *extra_opts1; @@ -1506,6 +1553,9 @@ try_mount_one (const char *spec0, const char *node0, const char *types0, res = status; goto out; } +#ifdef HAVE_LIBMOUNT_MOUNT + upd = prepare_mtab_entry(spec, node, types, opts, flags); +#endif block_signals (SIG_BLOCK); @@ -1545,7 +1595,10 @@ try_mount_one (const char *spec0, const char *node0, const char *types0, if (fake || mnt5_res == 0) { /* Mount succeeded, report this (if verbose) and write mtab entry. */ - +#ifdef HAVE_LIBMOUNT_MOUNT + if (upd) + update_mtab_entry(upd, flags); +#else if (!(mounttype & MS_PROPAGATION)) { char *mtab_opts = fix_opts_string (flags & ~MS_NOMTAB, extra_opts, user); update_mtab_entry(loop ? loopfile : spec, @@ -1557,7 +1610,7 @@ try_mount_one (const char *spec0, const char *node0, const char *types0, pass); free (mtab_opts); } - +#endif block_signals (SIG_UNBLOCK); res = 0; goto out; diff --git a/shlibs/mount/src/tab_update.c b/shlibs/mount/src/tab_update.c index 5afa0d49..603b6689 100644 --- a/shlibs/mount/src/tab_update.c +++ b/shlibs/mount/src/tab_update.c @@ -184,7 +184,6 @@ int mnt_update_set_fs(mnt_update *upd, unsigned long mountflags, if (mountflags & MS_PROPAGATION) return 1; - upd->mountflags = mountflags; rc = mnt_update_set_filename(upd, NULL, 0);