2 * mtab locking routines for use with mount.cifs and umount.cifs
3 * Copyright (C) 2008 Jeff Layton (jlayton@samba.org)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * This code was copied from the util-linux-ng sources and modified:
22 * git://git.kernel.org/pub/scm/utils/util-linux-ng/util-linux-ng.git
24 * ...specifically from mount/fstab.c. That file has no explicit license. The
25 * "default" license for anything in that tree is apparently GPLv2+, so I
26 * believe we're OK to copy it here.
28 * Jeff Layton <jlayton@samba.org>
45 /* Updating mtab ----------------------------------------------*/
47 /* Flag for already existing lock file. */
48 static int we_created_lockfile = 0;
49 static int lockfile_fd = -1;
51 /* Flag to indicate that signals have been set up. */
52 static int signals_have_been_setup = 0;
55 handler (int sig __attribute__((unused))) {
60 setlkw_timeout (int sig __attribute__((unused))) {
61 /* nothing, fcntl will fail anyway */
64 /* use monotonic time for timeouts */
68 #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
70 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
71 ret.tv_sec = ts.tv_sec;
72 ret.tv_usec = ts.tv_nsec/1000;
76 gettimeofday(&ret,NULL);
81 * See if mtab is present and whether it's a symlink. Returns errno from stat()
82 * call or EMLINK if it's a symlink.
89 if(lstat(_PATH_MOUNTED, &mstat))
91 else if (S_ISLNK(mstat.st_mode))
96 /* Remove lock file. */
99 if (we_created_lockfile) {
102 unlink (_PATH_MOUNTED_LOCK);
103 we_created_lockfile = 0;
107 /* Create the lock file.
108 The lock file will be removed if we catch a signal or when we exit. */
109 /* The old code here used flock on a lock file /etc/mtab~ and deleted
110 this lock file afterwards. However, as rgooch remarks, that has a
111 race: a second mount may be waiting on the lock and proceed as
112 soon as the lock file is deleted by the first mount, and immediately
113 afterwards a third mount comes, creates a new /etc/mtab~, applies
114 flock to that, and also proceeds, so that the second and third mount
115 now both are scribbling in /etc/mtab.
116 The new code uses a link() instead of a creat(), where we proceed
117 only if it was us that created the lock, and hence we always have
118 to delete the lock afterwards. Now the use of flock() is in principle
119 superfluous, but avoids an arbitrary sleep(). */
121 /* Where does the link point to? Obvious choices are mtab and mtab~~.
122 HJLu points out that the latter leads to races. Right now we use
123 mtab~.<pid> instead. Use 20 as upper bound for the length of %d. */
124 #define MOUNTLOCK_LINKTARGET _PATH_MOUNTED_LOCK "%d"
125 #define MOUNTLOCK_LINKTARGET_LTH (sizeof(_PATH_MOUNTED_LOCK)+20)
128 * The original mount locking code has used sleep(1) between attempts and
129 * maximal number of attemps has been 5.
131 * There was very small number of attempts and extremely long waiting (1s)
132 * that is useless on machines with large number of concurret mount processes.
134 * Now we wait few thousand microseconds between attempts and we have global
135 * time limit (30s) rather than limit for number of attempts. The advantage
136 * is that this method also counts time which we spend in fcntl(F_SETLKW) and
137 * number of attempts is not so much restricted.
139 * -- kzak@redhat.com [2007-Mar-2007]
142 /* maximum seconds between first and last attempt */
143 #define MOUNTLOCK_MAXTIME 30
145 /* sleep time (in microseconds, max=999999) between attempts */
146 #define MOUNTLOCK_WAITTIME 5000
151 struct timespec waittime;
152 struct timeval maxtime;
153 char linktargetfile[MOUNTLOCK_LINKTARGET_LTH];
155 if (!signals_have_been_setup) {
159 sa.sa_handler = handler;
161 sigfillset (&sa.sa_mask);
163 while (sigismember (&sa.sa_mask, ++sig) != -1
166 sa.sa_handler = setlkw_timeout;
168 sa.sa_handler = handler;
169 sigaction (sig, &sa, (struct sigaction *) 0);
171 signals_have_been_setup = 1;
174 sprintf(linktargetfile, MOUNTLOCK_LINKTARGET, getpid ());
176 i = open (linktargetfile, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
178 /* linktargetfile does not exist (as a file)
179 and we cannot create it. Read-only filesystem?
180 Too many files open in the system?
186 maxtime = mono_time();
187 maxtime.tv_sec += MOUNTLOCK_MAXTIME;
190 waittime.tv_nsec = (1000 * MOUNTLOCK_WAITTIME);
192 /* Repeat until it was us who made the link */
193 while (!we_created_lockfile) {
198 j = link(linktargetfile, _PATH_MOUNTED_LOCK);
202 we_created_lockfile = 1;
204 if (j < 0 && errsv != EEXIST) {
205 (void) unlink(linktargetfile);
209 lockfile_fd = open (_PATH_MOUNTED_LOCK, O_WRONLY);
211 if (lockfile_fd < 0) {
212 /* Strange... Maybe the file was just deleted? */
214 if (errno == ENOENT && now.tv_sec < maxtime.tv_sec) {
215 we_created_lockfile = 0;
218 (void) unlink(linktargetfile);
222 flock.l_type = F_WRLCK;
223 flock.l_whence = SEEK_SET;
228 /* We made the link. Now claim the lock. If we can't
229 * get it, continue anyway
231 fcntl (lockfile_fd, F_SETLK, &flock);
232 (void) unlink(linktargetfile);
234 /* Someone else made the link. Wait. */
236 if (now.tv_sec < maxtime.tv_sec) {
237 alarm(maxtime.tv_sec - now.tv_sec);
238 if (fcntl (lockfile_fd, F_SETLKW, &flock) == -1) {
239 (void) unlink(linktargetfile);
243 nanosleep(&waittime, NULL);
245 (void) unlink(linktargetfile);
255 * Call fflush and fsync on the mtab, and then endmntent. If either fflush
256 * or fsync fails, then truncate the file back to "size". endmntent is called
257 * unconditionally, and the errno (if any) from fflush and fsync are returned.
260 my_endmntent(FILE *stream, off_t size)
272 /* truncate file back to "size" -- best effort here */
274 int ignore __attribute__((unused));
277 ignore = ftruncate(fd, size);