|
|
4391fdc |
--- util-linux-2.12p/mount/fstab.c.mtab-lock 2005-03-22 14:05:22.481297072 +0100
|
|
|
4391fdc |
+++ util-linux-2.12p/mount/fstab.c 2005-03-22 14:50:55.719781664 +0100
|
|
|
4391fdc |
@@ -395,6 +395,7 @@
|
|
|
4391fdc |
|
|
|
4391fdc |
/* Flag for already existing lock file. */
|
|
|
4391fdc |
static int we_created_lockfile = 0;
|
|
|
4391fdc |
+static int lockfile_fd = -1;
|
|
|
4391fdc |
|
|
|
4391fdc |
/* Flag to indicate that signals have been set up. */
|
|
|
4391fdc |
static int signals_have_been_setup = 0;
|
|
|
4391fdc |
@@ -416,6 +417,8 @@
|
|
|
4391fdc |
void
|
|
|
4391fdc |
unlock_mtab (void) {
|
|
|
4391fdc |
if (we_created_lockfile) {
|
|
|
4391fdc |
+ close(lockfile_fd);
|
|
|
4391fdc |
+ lockfile_fd = -1;
|
|
|
4391fdc |
unlink (MOUNTED_LOCK);
|
|
|
4391fdc |
we_created_lockfile = 0;
|
|
|
4391fdc |
}
|
|
|
4391fdc |
@@ -443,7 +446,7 @@
|
|
|
4391fdc |
|
|
|
4391fdc |
void
|
|
|
4391fdc |
lock_mtab (void) {
|
|
|
4391fdc |
- int tries = 3;
|
|
|
4391fdc |
+ int tries = 100000, i;
|
|
|
4391fdc |
char linktargetfile[MOUNTLOCK_LINKTARGET_LTH];
|
|
|
4391fdc |
|
|
|
4391fdc |
at_die = unlock_mtab;
|
|
|
4391fdc |
@@ -469,45 +472,48 @@
|
|
|
4391fdc |
|
|
|
4391fdc |
sprintf(linktargetfile, MOUNTLOCK_LINKTARGET, getpid ());
|
|
|
4391fdc |
|
|
|
4391fdc |
+ i = open (linktargetfile, O_WRONLY|O_CREAT, 0);
|
|
|
4391fdc |
+ if (i < 0) {
|
|
|
4391fdc |
+ int errsv = errno;
|
|
|
4391fdc |
+ /* linktargetfile does not exist (as a file)
|
|
|
4391fdc |
+ and we cannot create it. Read-only filesystem?
|
|
|
4391fdc |
+ Too many files open in the system?
|
|
|
4391fdc |
+ Filesystem full? */
|
|
|
4391fdc |
+ die (EX_FILEIO, _("can't create lock file %s: %s "
|
|
|
4391fdc |
+ "(use -n flag to override)"),
|
|
|
4391fdc |
+ linktargetfile, strerror (errsv));
|
|
|
4391fdc |
+ }
|
|
|
4391fdc |
+ close(i);
|
|
|
4391fdc |
+
|
|
|
4391fdc |
/* Repeat until it was us who made the link */
|
|
|
4391fdc |
while (!we_created_lockfile) {
|
|
|
4391fdc |
struct flock flock;
|
|
|
4391fdc |
- int errsv, fd, i, j;
|
|
|
4391fdc |
-
|
|
|
4391fdc |
- i = open (linktargetfile, O_WRONLY|O_CREAT, 0);
|
|
|
4391fdc |
- if (i < 0) {
|
|
|
4391fdc |
- int errsv = errno;
|
|
|
4391fdc |
- /* linktargetfile does not exist (as a file)
|
|
|
4391fdc |
- and we cannot create it. Read-only filesystem?
|
|
|
4391fdc |
- Too many files open in the system?
|
|
|
4391fdc |
- Filesystem full? */
|
|
|
4391fdc |
- die (EX_FILEIO, _("can't create lock file %s: %s "
|
|
|
4391fdc |
- "(use -n flag to override)"),
|
|
|
4391fdc |
- linktargetfile, strerror (errsv));
|
|
|
4391fdc |
- }
|
|
|
4391fdc |
- close(i);
|
|
|
4391fdc |
+ int errsv, j;
|
|
|
4391fdc |
|
|
|
4391fdc |
j = link(linktargetfile, MOUNTED_LOCK);
|
|
|
4391fdc |
errsv = errno;
|
|
|
4391fdc |
|
|
|
4391fdc |
- (void) unlink(linktargetfile);
|
|
|
4391fdc |
-
|
|
|
4391fdc |
if (j == 0)
|
|
|
4391fdc |
we_created_lockfile = 1;
|
|
|
4391fdc |
|
|
|
4391fdc |
if (j < 0 && errsv != EEXIST) {
|
|
|
4391fdc |
+ (void) unlink(linktargetfile);
|
|
|
4391fdc |
die (EX_FILEIO, _("can't link lock file %s: %s "
|
|
|
4391fdc |
"(use -n flag to override)"),
|
|
|
4391fdc |
MOUNTED_LOCK, strerror (errsv));
|
|
|
4391fdc |
}
|
|
|
4391fdc |
|
|
|
4391fdc |
- fd = open (MOUNTED_LOCK, O_WRONLY);
|
|
|
4391fdc |
+ lockfile_fd = open (MOUNTED_LOCK, O_WRONLY);
|
|
|
4391fdc |
|
|
|
4391fdc |
- if (fd < 0) {
|
|
|
4391fdc |
+ if (lockfile_fd < 0) {
|
|
|
4391fdc |
int errsv = errno;
|
|
|
4391fdc |
/* Strange... Maybe the file was just deleted? */
|
|
|
4391fdc |
- if (errno == ENOENT && tries-- > 0)
|
|
|
4391fdc |
+ if (errno == ENOENT && tries-- > 0) {
|
|
|
4391fdc |
+ if (tries % 200 == 0)
|
|
|
4391fdc |
+ usleep(30);
|
|
|
4391fdc |
continue;
|
|
|
4391fdc |
+ }
|
|
|
4391fdc |
+ (void) unlink(linktargetfile);
|
|
|
4391fdc |
die (EX_FILEIO, _("can't open lock file %s: %s "
|
|
|
4391fdc |
"(use -n flag to override)"),
|
|
|
4391fdc |
MOUNTED_LOCK, strerror (errsv));
|
|
|
4391fdc |
@@ -520,7 +526,7 @@
|
|
|
4391fdc |
|
|
|
4391fdc |
if (j == 0) {
|
|
|
4391fdc |
/* We made the link. Now claim the lock. */
|
|
|
4391fdc |
- if (fcntl (fd, F_SETLK, &flock) == -1) {
|
|
|
4391fdc |
+ if (fcntl (lockfile_fd, F_SETLK, &flock) == -1) {
|
|
|
4391fdc |
if (verbose) {
|
|
|
4391fdc |
int errsv = errno;
|
|
|
4391fdc |
printf(_("Can't lock lock file %s: %s\n"),
|
|
|
4391fdc |
@@ -528,13 +534,15 @@
|
|
|
4391fdc |
}
|
|
|
4391fdc |
/* proceed anyway */
|
|
|
4391fdc |
}
|
|
|
4391fdc |
+ (void) unlink(linktargetfile);
|
|
|
4391fdc |
} else {
|
|
|
4391fdc |
static int tries = 0;
|
|
|
4391fdc |
|
|
|
4391fdc |
/* Someone else made the link. Wait. */
|
|
|
4391fdc |
alarm(LOCK_TIMEOUT);
|
|
|
4391fdc |
- if (fcntl (fd, F_SETLKW, &flock) == -1) {
|
|
|
4391fdc |
+ if (fcntl (lockfile_fd, F_SETLKW, &flock) == -1) {
|
|
|
4391fdc |
int errsv = errno;
|
|
|
4391fdc |
+ (void) unlink(linktargetfile);
|
|
|
4391fdc |
die (EX_FILEIO, _("can't lock lock file %s: %s"),
|
|
|
4391fdc |
MOUNTED_LOCK, (errno == EINTR) ?
|
|
|
4391fdc |
_("timed out") : strerror (errsv));
|
|
|
4391fdc |
@@ -542,16 +550,18 @@
|
|
|
4391fdc |
alarm(0);
|
|
|
4391fdc |
/* Limit the number of iterations - maybe there
|
|
|
4391fdc |
still is some old /etc/mtab~ */
|
|
|
4391fdc |
- if (tries++ > 3) {
|
|
|
4391fdc |
- if (tries > 5)
|
|
|
4391fdc |
- die (EX_FILEIO, _("Cannot create link %s\n"
|
|
|
4391fdc |
- "Perhaps there is a stale lock file?\n"),
|
|
|
4391fdc |
- MOUNTED_LOCK);
|
|
|
4391fdc |
- sleep(1);
|
|
|
4391fdc |
- }
|
|
|
4391fdc |
+ ++tries;
|
|
|
4391fdc |
+ if (tries % 200 == 0)
|
|
|
4391fdc |
+ usleep(30);
|
|
|
4391fdc |
+ if (tries > 100000) {
|
|
|
4391fdc |
+ (void) unlink(linktargetfile);
|
|
|
4391fdc |
+ close(lockfile_fd);
|
|
|
4391fdc |
+ die (EX_FILEIO, _("Cannot create link %s\n"
|
|
|
4391fdc |
+ "Perhaps there is a stale lock file?\n"),
|
|
|
4391fdc |
+ MOUNTED_LOCK);
|
|
|
4391fdc |
+ }
|
|
|
4391fdc |
+ close(lockfile_fd);
|
|
|
4391fdc |
}
|
|
|
4391fdc |
-
|
|
|
4391fdc |
- close(fd);
|
|
|
4391fdc |
}
|
|
|
4391fdc |
}
|
|
|
4391fdc |
|