]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: small lock code cleanup
authorKarel Zak <kzak@redhat.com>
Wed, 30 Mar 2011 11:58:04 +0000 (13:58 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 19 Apr 2011 11:09:53 +0000 (13:09 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
shlibs/mount/src/lock.c
shlibs/mount/src/tab_update.c
shlibs/mount/src/utils.c

index 0481d63def0d01c511eee1737a27108655d96e7b..c4059ce69243748528e6b1be186f27c221cf1faa 100644 (file)
@@ -40,8 +40,8 @@ struct libmnt_lock {
        char    *linkfile;      /* path to link file (e.g. /etc/mtab~.<id>) */
        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;
 };
@@ -99,7 +99,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);
@@ -119,7 +119,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;
 }
 
@@ -247,8 +248,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
@@ -270,15 +270,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;
 }
 
@@ -395,7 +397,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;
@@ -458,9 +460,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;
 
index 55f023557f0b63eff7913861dbae9cd85288997e..ecd5b199568ee9739f5eb94b80cd052f8b9f324c 100644 (file)
@@ -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;
 }
 
@@ -770,13 +773,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;
 }
 
@@ -810,7 +814,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)
index b02b6fe4d2d0d85afb0d3fb263d90888099f2bc9..1dc5025acf2666f3b2ea6ec241e32a6ca49853b8 100644 (file)
@@ -550,8 +550,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)