--- util-linux-2.12q/mount/fstab.c +++ util-linux-2.12q/mount/fstab.c @@ -395,6 +395,7 @@ /* Flag for already existing lock file. */ static int we_created_lockfile = 0; +static int lock_file_desc = -1; /* Flag to indicate that signals have been set up. */ static int signals_have_been_setup = 0; @@ -417,6 +418,8 @@ unlock_mtab (void) { if (we_created_lockfile) { unlink (MOUNTED_LOCK); + close(lock_file_desc); + lock_file_desc = -1; we_created_lockfile = 0; } } @@ -443,6 +446,7 @@ void lock_mtab (void) { + sigset_t sigmask; int tries = 3; char linktargetfile[MOUNTLOCK_LINKTARGET_LTH]; @@ -467,6 +471,10 @@ signals_have_been_setup = 1; } + /* Allow all signals while trying to lock mtab */ + sigemptyset(&sigmask); + sigprocmask(SIG_SETMASK, &sigmask, &sigmask); + sprintf(linktargetfile, MOUNTLOCK_LINKTARGET, getpid ()); /* Repeat until it was us who made the link */ @@ -521,12 +529,21 @@ if (j == 0) { /* We made the link. Now claim the lock. */ if (fcntl (fd, F_SETLK, &flock) == -1) { + int errsv = errno; + if (!tries--) + die(EX_FILEIO, + _("Can't lock lock file %s: %s\n"), + MOUNTED_LOCK, strerror (errsv)); if (verbose) { - int errsv = errno; printf(_("Can't lock lock file %s: %s\n"), MOUNTED_LOCK, strerror (errsv)); } - /* proceed anyway */ + /* bummer - someone raced us to the file + * lock. Start all over again. */ + unlink(MOUNTED_LOCK); + } else { + we_created_lockfile = 1; + lock_file_desc = fd; } } else { static int tries = 0; @@ -551,8 +568,14 @@ } } - close(fd); + /* If we created the lock file, keep the fd else + * our POSIX lock will go away immediately */ + if (!we_created_lockfile) + close(fd); } + + /* Restore original signal mask */ + sigprocmask(SIG_SETMASK, &sigmask, NULL); } /* --- util-linux-2.12q/mount/paths.h +++ util-linux-2.12q/mount/paths.h @@ -7,4 +7,4 @@ #define MOUNTED_LOCK "/etc/mtab~" #define MOUNTED_TEMP "/etc/mtab.tmp" #endif -#define LOCK_TIMEOUT 10 +#define LOCK_TIMEOUT 30