]>
Commit | Line | Data |
---|---|---|
5545a732 JR |
1 | - Improved /etc/mtab lock |
2 | - fix for the mount command and am-utils using different mtab locking | |
3 | ||
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 | |
6 | @@ -395,6 +395,7 @@ | |
7 | ||
8 | /* Flag for already existing lock file. */ | |
9 | static int we_created_lockfile = 0; | |
10 | +static int lockfile_fd = -1; | |
11 | ||
12 | /* Flag to indicate that signals have been set up. */ | |
13 | static int signals_have_been_setup = 0; | |
14 | @@ -416,6 +417,8 @@ | |
15 | void | |
16 | unlock_mtab (void) { | |
17 | if (we_created_lockfile) { | |
18 | + close(lockfile_fd); | |
19 | + lockfile_fd = -1; | |
20 | unlink (MOUNTED_LOCK); | |
21 | we_created_lockfile = 0; | |
22 | } | |
23 | @@ -443,7 +446,7 @@ | |
24 | ||
25 | void | |
26 | lock_mtab (void) { | |
27 | - int tries = 3; | |
28 | + int tries = 100000, i; | |
29 | char linktargetfile[MOUNTLOCK_LINKTARGET_LTH]; | |
30 | ||
31 | at_die = unlock_mtab; | |
32 | @@ -469,45 +472,48 @@ | |
33 | ||
34 | sprintf(linktargetfile, MOUNTLOCK_LINKTARGET, getpid ()); | |
35 | ||
36 | + i = open (linktargetfile, O_WRONLY|O_CREAT, 0); | |
37 | + if (i < 0) { | |
38 | + int errsv = errno; | |
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? | |
42 | + Filesystem full? */ | |
43 | + die (EX_FILEIO, _("can't create lock file %s: %s " | |
44 | + "(use -n flag to override)"), | |
45 | + linktargetfile, strerror (errsv)); | |
46 | + } | |
47 | + close(i); | |
48 | + | |
49 | /* Repeat until it was us who made the link */ | |
50 | while (!we_created_lockfile) { | |
51 | struct flock flock; | |
52 | - int errsv, fd, i, j; | |
53 | - | |
54 | - i = open (linktargetfile, O_WRONLY|O_CREAT, 0); | |
55 | - if (i < 0) { | |
56 | - int errsv = errno; | |
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? | |
60 | - Filesystem full? */ | |
61 | - die (EX_FILEIO, _("can't create lock file %s: %s " | |
62 | - "(use -n flag to override)"), | |
63 | - linktargetfile, strerror (errsv)); | |
64 | - } | |
65 | - close(i); | |
66 | + int errsv, j; | |
67 | ||
68 | j = link(linktargetfile, MOUNTED_LOCK); | |
69 | errsv = errno; | |
70 | ||
71 | - (void) unlink(linktargetfile); | |
72 | - | |
73 | if (j == 0) | |
74 | we_created_lockfile = 1; | |
75 | ||
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)); | |
81 | } | |
82 | ||
83 | - fd = open (MOUNTED_LOCK, O_WRONLY); | |
84 | + lockfile_fd = open (MOUNTED_LOCK, O_WRONLY); | |
85 | ||
86 | - if (fd < 0) { | |
87 | + if (lockfile_fd < 0) { | |
88 | int errsv = errno; | |
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) | |
93 | + usleep(30); | |
94 | continue; | |
95 | + } | |
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)); | |
100 | @@ -520,7 +526,7 @@ | |
101 | ||
102 | if (j == 0) { | |
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) { | |
106 | if (verbose) { | |
107 | int errsv = errno; | |
108 | printf(_("Can't lock lock file %s: %s\n"), | |
109 | @@ -528,13 +534,15 @@ | |
110 | } | |
111 | /* proceed anyway */ | |
112 | } | |
113 | + (void) unlink(linktargetfile); | |
114 | } else { | |
115 | static int tries = 0; | |
116 | ||
117 | /* Someone else made the link. Wait. */ | |
118 | alarm(LOCK_TIMEOUT); | |
119 | - if (fcntl (fd, F_SETLKW, &flock) == -1) { | |
120 | + if (fcntl (lockfile_fd, F_SETLKW, &flock) == -1) { | |
121 | int errsv = errno; | |
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 @@ | |
127 | alarm(0); | |
128 | /* Limit the number of iterations - maybe there | |
129 | still is some old /etc/mtab~ */ | |
130 | - if (tries++ > 3) { | |
131 | - if (tries > 5) | |
132 | - die (EX_FILEIO, _("Cannot create link %s\n" | |
133 | - "Perhaps there is a stale lock file?\n"), | |
134 | - MOUNTED_LOCK); | |
135 | - sleep(1); | |
136 | - } | |
137 | + ++tries; | |
138 | + if (tries % 200 == 0) | |
139 | + usleep(30); | |
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"), | |
145 | + MOUNTED_LOCK); | |
146 | + } | |
147 | + close(lockfile_fd); | |
148 | } | |
149 | - | |
150 | - close(fd); | |
151 | } | |
152 | } | |
153 |