From 6f5788c5926741fb3c44bb33fe511c555f155a86 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 30 Mar 2011 13:58:04 +0200 Subject: [PATCH] libmount: small lock code cleanup Signed-off-by: Karel Zak --- shlibs/mount/src/lock.c | 29 +++++++++++++++-------------- shlibs/mount/src/tab_update.c | 21 ++++++++++++--------- shlibs/mount/src/utils.c | 4 ++-- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/shlibs/mount/src/lock.c b/shlibs/mount/src/lock.c index 55dcf73b2c..3c70da30a5 100644 --- a/shlibs/mount/src/lock.c +++ b/shlibs/mount/src/lock.c @@ -41,8 +41,8 @@ struct libmnt_lock { char *linkfile; /* path to link file (e.g. /etc/mtab~.) */ int lockfile_fd; /* lock file descriptor */ - int locked :1; /* do we own the lock? */ - int sigblock: 1; /* block signals when locked */ + int locked : 1; /* do we own the lock? */ + int sigblock : 1; /* block signals when locked */ sigset_t oldsigmask; }; @@ -100,7 +100,7 @@ void mnt_free_lock(struct libmnt_lock *ml) { if (!ml) return; - DBG(LOCKS, mnt_debug_h(ml, "free")); + DBG(LOCKS, mnt_debug_h(ml, "free%s", ml->locked ? " !!! LOCKED !!!" : "")); free(ml->lockfile); free(ml->linkfile); free(ml); @@ -120,7 +120,8 @@ int mnt_lock_block_signals(struct libmnt_lock *ml, int enable) { if (!ml) return -EINVAL; - ml->sigblock = enable; + DBG(LOCKS, mnt_debug_h(ml, "signals: %s", enable ? "BLOCKED" : "UNBLOCKED")); + ml->sigblock = enable ? 1 : 0; return 0; } @@ -248,8 +249,7 @@ void mnt_unlock_file(struct libmnt_lock *ml) if (!ml) return; - - if (ml->locked == 0 && ml->lockfile && ml->linkfile) + if (!ml->locked && ml->lockfile && ml->linkfile) { /* We have (probably) all files, but we don't own the lock, * Really? Check it! Maybe ml->locked wasn't set properly @@ -271,15 +271,17 @@ void mnt_unlock_file(struct libmnt_lock *ml) unlink(ml->linkfile); if (ml->lockfile_fd >= 0) close(ml->lockfile_fd); - if (ml->locked == 1 && ml->lockfile) + if (ml->locked && ml->lockfile) { unlink(ml->lockfile); - + DBG(LOCKS, mnt_debug_h(ml, "unlink %s", ml->lockfile)); + } ml->locked = 0; ml->lockfile_fd = -1; - if (ml->sigblock) + if (ml->sigblock) { + DBG(LOCKS, mnt_debug_h(ml, "restoring sigmask")); sigprocmask(SIG_SETMASK, &ml->oldsigmask, NULL); - + } ml->sigblock = 0; } @@ -396,7 +398,7 @@ int mnt_lock_file(struct libmnt_lock *ml) waittime.tv_nsec = (1000 * MOUNTLOCK_WAITTIME); /* Repeat until it was us who made the link */ - while (ml->locked == 0) { + while (!ml->locked) { struct timeval now; struct flock flock; int j; @@ -459,9 +461,8 @@ int mnt_lock_file(struct libmnt_lock *ml) ml->lockfile_fd = -1; } } - DBG(LOCKS, mnt_debug_h(ml, - "%s: (%d) successfully locked\n", - lockfile, getpid())); + DBG(LOCKS, mnt_debug_h(ml, "%s: (%d) successfully locked", + lockfile, getpid())); unlink(linkfile); return 0; diff --git a/shlibs/mount/src/tab_update.c b/shlibs/mount/src/tab_update.c index f1fbdc0f30..71988f8a86 100644 --- a/shlibs/mount/src/tab_update.c +++ b/shlibs/mount/src/tab_update.c @@ -83,7 +83,7 @@ void mnt_free_update(struct libmnt_update *upd) } /* - * Returns 0 on success, 1 if not file available, -1 in case of error. + * Returns 0 on success, <0 in case of error. */ int mnt_update_set_filename(struct libmnt_update *upd, const char *filename, int userspace_only) @@ -114,7 +114,7 @@ int mnt_update_set_filename(struct libmnt_update *upd, const char *filename, path = NULL; mnt_has_regular_utab(&path, &rw); if (!rw) - return 1; + return -EACCES; upd->userspace_only = TRUE; } upd->filename = strdup(path); @@ -157,7 +157,7 @@ int mnt_update_is_ready(struct libmnt_update *upd) * @target: umount target, must be num for mount * @fs: mount filesystem description, must be NULL for umount * - * Returns: -1 in case on error, 0 on success, 1 if update is unnecessary. + * Returns: <0 in case on error, 0 on success, 1 if update is unnecessary. */ int mnt_update_set_fs(struct libmnt_update *upd, unsigned long mountflags, const char *target, struct libmnt_fs *fs) @@ -195,9 +195,10 @@ int mnt_update_set_fs(struct libmnt_update *upd, unsigned long mountflags, upd->mountflags = mountflags; rc = mnt_update_set_filename(upd, NULL, 0); - if (rc) + if (rc) { + DBG(UPDATE, mnt_debug_h(upd, "no writable file available [rc=%d]", rc)); return rc; /* error or no file available (rc = 1) */ - + } if (target) { upd->target = strdup(target); if (!upd->target) @@ -693,12 +694,13 @@ static int update_remove_entry(struct libmnt_update *upd, struct libmnt_lock *lc rc = update_table(upd, tb); mnt_free_fs(rem); } - mnt_free_table(tb); } if (lc) mnt_unlock_file(lc); else if (u_lc != -1) utab_unlock(u_lc); + + mnt_free_table(tb); return rc; } @@ -724,12 +726,13 @@ static int update_modify_target(struct libmnt_update *upd, struct libmnt_lock *l if (!rc) rc = update_table(upd, tb); } - mnt_free_table(tb); } if (lc) mnt_unlock_file(lc); else if (u_lc != -1) utab_unlock(u_lc); + + mnt_free_table(tb); return rc; } @@ -765,13 +768,14 @@ static int update_modify_options(struct libmnt_update *upd, struct libmnt_lock * if (!rc) rc = update_table(upd, tb); } - mnt_free_table(tb); } if (lc) mnt_unlock_file(lc); else if (u_lc != -1) utab_unlock(u_lc); + + mnt_free_table(tb); return rc; } @@ -805,7 +809,6 @@ int mnt_update_table(struct libmnt_update *upd, struct libmnt_lock *lc) if (upd->fs) { DBG(UPDATE, mnt_fs_print_debug(upd->fs, stderr)); } - if (!lc && !upd->userspace_only) { lc = mnt_new_lock(upd->filename, 0); if (lc) diff --git a/shlibs/mount/src/utils.c b/shlibs/mount/src/utils.c index 1261a2934a..356c610696 100644 --- a/shlibs/mount/src/utils.c +++ b/shlibs/mount/src/utils.c @@ -602,8 +602,8 @@ done: * If the file does not exist and @writable argument is not NULL then it will * try to create the directory (e.g. /dev/.mount) and the file. * - * Returns: 1 if /etc/utab is a reqular file, and 0 in case of error (check - * errno for more details). + * Returns: 1 if /dev/.mount/utab is a reqular file, and 0 in case of + * error (check errno for more details). */ int mnt_has_regular_utab(const char **utab, int *writable) -- 2.47.2