From 5944e4e6c693d0d388c3ff9a3f534daa4bdf3767 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 29 Sep 2010 12:23:59 +0200 Subject: [PATCH] libmount: add mtab managment to context API Signed-off-by: Karel Zak --- shlibs/mount/src/context.c | 79 ++++++++++++++++++++++++++++++++--- shlibs/mount/src/mount.h.in | 2 +- shlibs/mount/src/tab_update.c | 3 +- 3 files changed, 77 insertions(+), 7 deletions(-) diff --git a/shlibs/mount/src/context.c b/shlibs/mount/src/context.c index 664ef918..23593ac5 100644 --- a/shlibs/mount/src/context.c +++ b/shlibs/mount/src/context.c @@ -61,6 +61,7 @@ struct _mnt_context #define MNT_FL_LAZY (1 << 7) #define MNT_FL_FORCE (1 << 8) #define MNT_FL_NOCANONICALIZE (1 << 9) +#define MNT_FL_NOLOCK (1 << 10) /* don't lock mtab file */ #define MNT_FL_EXTERN_FS (1 << 15) /* cxt->fs is not private */ #define MNT_FL_EXTERN_FSTAB (1 << 16) /* cxt->fstab is not private */ @@ -339,11 +340,25 @@ int mnt_context_enable_fake(mnt_context *cxt, int enable) * * Returns: 0 on success, negative number in case of error. */ -int mnt_context_disable_nomtab(mnt_context *cxt, int disable) +int mnt_context_disable_mtab(mnt_context *cxt, int disable) { return mnt_context_set_flag(cxt, MNT_FL_NOMTAB, disable); } +/** + * mnt_context_disable_lock: + * @cxt: mount context + * @disable: TRUE or FALSE + * + * Disable/enable mtab lock. + * + * Returns: 0 on success, negative number in case of error. + */ +int mnt_context_disable_lock(mnt_context *cxt, int disable) +{ + return mnt_context_set_flag(cxt, MNT_FL_NOLOCK, disable); +} + /** * mnt_context_enable_force: * @cxt: mount context @@ -1121,7 +1136,6 @@ static int mnt_context_detect_fstype(mnt_context *cxt) mnt_fs_set_fstype(cxt->fs, type); if (!cache) free(type); /* type is not cached */ - return 0; } return 0; @@ -1147,6 +1161,61 @@ static int mnt_context_merge_mountflags(mnt_context *cxt) return 0; } +static int mnt_context_prepare_update(mnt_context *cxt, int act) +{ + int rc; + + if (cxt->update) { + mnt_free_update(cxt->update); + cxt->update = NULL; + } + + if (cxt->flags & MNT_FL_NOMTAB) + return 0; + + cxt->update = mnt_new_update(act, cxt->mountflags, cxt->fs); + if (!cxt->update) + return -ENOMEM; + + if (cxt->flags & MNT_FL_NOLOCK) + mnt_update_disable_lock(cxt->update, TRUE); + + rc = mnt_prepare_update(cxt->update); + + if (rc == 1) + /* mtab update is unnecessary for this system */ + rc = 0; + + return rc; +} + +/** + * mnt_context_get_lock: + * @cxt: mount context + * + * The lock is available after mnt_context_prepare_mount() or + * mnt_context_prepare_umount(). + * + * The application that uses libmount context does not have to care about + * mtab locking, but with a small exceptions: the application has to be able to + * remove the lock file when interrupted by signal. It means that properly written + * mount(8)-like application has to call mnt_unlock_file() from a signal handler. + * + * See also mnt_unlock_file(), mnt_context_disable_lock() and + * mnt_context_disable_mtab(). + * + * It's not error if this function returns NULL (it usually means that the + * context is not prepared yet, or mtab update is unnecessary). + * + * Returns: pointer to lock struct. + */ +mnt_lock *mnt_context_get_lock(mnt_context *cxt) +{ + if (!cxt || !cxt->update || (cxt->flags & (MNT_FL_NOMTAB | MNT_FL_NOLOCK))) + return NULL; + return mnt_update_get_lock(cxt->update); +} + /** * mnt_context_prepare_mount: * @cxt: mount context @@ -1199,10 +1268,10 @@ int mnt_context_prepare_mount(mnt_context *cxt) if (rc) goto err; + rc = mnt_context_prepare_update(cxt, MNT_ACT_MOUNT); + if (rc) + goto err; - /* TODO: prepare mtab update */ - - /* TODO: replace generic optstr with fs_optstr */ DBG(CXT, mnt_debug_h(cxt, "sucessfully prepared")); return 0; diff --git a/shlibs/mount/src/mount.h.in b/shlibs/mount/src/mount.h.in index faa86bfe..041be62e 100644 --- a/shlibs/mount/src/mount.h.in +++ b/shlibs/mount/src/mount.h.in @@ -293,7 +293,7 @@ extern int mnt_update_disable_lock(mnt_update *upd, int disable); extern int mnt_update_set_old_target(mnt_update *upd, const char *target); extern int mnt_update_set_fs(mnt_update *upd, const mnt_fs *fs); -extern int mnt_update_prepare_update(mnt_update *upd); +extern int mnt_prepare_update(mnt_update *upd); extern int mnt_update_mtab(mnt_update *upd); /* diff --git a/shlibs/mount/src/tab_update.c b/shlibs/mount/src/tab_update.c index 3cf67621..1573b58b 100644 --- a/shlibs/mount/src/tab_update.c +++ b/shlibs/mount/src/tab_update.c @@ -229,7 +229,8 @@ int mnt_update_set_mountflags(mnt_update *upd, unsigned long flags) * Note that after mnt_update_disable_lock(mt, TRUE) or after mnt_free_update() * the lock will be automatically deallocated. * - * Returns: libmount lock handler or NULL if locking is disabled. + * Returns: libmount lock handler or NULL if locking is disabled or update is + * not prepared yet. */ mnt_lock *mnt_update_get_lock(mnt_update *upd) { -- 2.39.5