1 - Improved /etc/mtab lock
2 - fix for the mount command and am-utils using different mtab locking
4 --- util-linux-2.12p/mount/fstab.c.mtab-lock 2005-03-22 14:05:22.481297072 +0100
5 +++ util-linux-2.12p/mount/fstab.c 2005-03-22 14:50:55.719781664 +0100
8 /* Flag for already existing lock file. */
9 static int we_created_lockfile = 0;
10 +static int lockfile_fd = -1;
12 /* Flag to indicate that signals have been set up. */
13 static int signals_have_been_setup = 0;
17 if (we_created_lockfile) {
20 unlink (MOUNTED_LOCK);
21 we_created_lockfile = 0;
28 + int tries = 100000, i;
29 char linktargetfile[MOUNTLOCK_LINKTARGET_LTH];
34 sprintf(linktargetfile, MOUNTLOCK_LINKTARGET, getpid ());
36 + i = open (linktargetfile, O_WRONLY|O_CREAT, 0);
39 + /* linktargetfile does not exist (as a file)
40 + and we cannot create it. Read-only filesystem?
41 + Too many files open in the system?
43 + die (EX_FILEIO, _("can't create lock file %s: %s "
44 + "(use -n flag to override)"),
45 + linktargetfile, strerror (errsv));
49 /* Repeat until it was us who made the link */
50 while (!we_created_lockfile) {
52 - int errsv, fd, i, j;
54 - i = open (linktargetfile, O_WRONLY|O_CREAT, 0);
57 - /* linktargetfile does not exist (as a file)
58 - and we cannot create it. Read-only filesystem?
59 - Too many files open in the system?
61 - die (EX_FILEIO, _("can't create lock file %s: %s "
62 - "(use -n flag to override)"),
63 - linktargetfile, strerror (errsv));
68 j = link(linktargetfile, MOUNTED_LOCK);
71 - (void) unlink(linktargetfile);
74 we_created_lockfile = 1;
76 if (j < 0 && errsv != EEXIST) {
77 + (void) unlink(linktargetfile);
78 die (EX_FILEIO, _("can't link lock file %s: %s "
79 "(use -n flag to override)"),
80 MOUNTED_LOCK, strerror (errsv));
83 - fd = open (MOUNTED_LOCK, O_WRONLY);
84 + lockfile_fd = open (MOUNTED_LOCK, O_WRONLY);
87 + if (lockfile_fd < 0) {
89 /* Strange... Maybe the file was just deleted? */
90 - if (errno == ENOENT && tries-- > 0)
91 + if (errno == ENOENT && tries-- > 0) {
92 + if (tries % 200 == 0)
96 + (void) unlink(linktargetfile);
97 die (EX_FILEIO, _("can't open lock file %s: %s "
98 "(use -n flag to override)"),
99 MOUNTED_LOCK, strerror (errsv));
103 /* We made the link. Now claim the lock. */
104 - if (fcntl (fd, F_SETLK, &flock) == -1) {
105 + if (fcntl (lockfile_fd, F_SETLK, &flock) == -1) {
108 printf(_("Can't lock lock file %s: %s\n"),
109 @@ -528,13 +534,15 @@
113 + (void) unlink(linktargetfile);
115 static int tries = 0;
117 /* Someone else made the link. Wait. */
119 - if (fcntl (fd, F_SETLKW, &flock) == -1) {
120 + if (fcntl (lockfile_fd, F_SETLKW, &flock) == -1) {
122 + (void) unlink(linktargetfile);
123 die (EX_FILEIO, _("can't lock lock file %s: %s"),
124 MOUNTED_LOCK, (errno == EINTR) ?
125 _("timed out") : strerror (errsv));
126 @@ -542,16 +550,18 @@
128 /* Limit the number of iterations - maybe there
129 still is some old /etc/mtab~ */
132 - die (EX_FILEIO, _("Cannot create link %s\n"
133 - "Perhaps there is a stale lock file?\n"),
138 + if (tries % 200 == 0)
140 + if (tries > 100000) {
141 + (void) unlink(linktargetfile);
142 + close(lockfile_fd);
143 + die (EX_FILEIO, _("Cannot create link %s\n"
144 + "Perhaps there is a stale lock file?\n"),
147 + close(lockfile_fd);