]>
git.proxmox.com Git - ceph.git/blob - ceph/src/mount/mtab.c
3 * this code lifted from util-linux-ng, licensed GPLv2+,
5 * git://git.kernel.org/pub/scm/utils/util-linux-ng/util-linux-ng.git
7 * whoever decided that each special mount program is responsible
8 * for updating /etc/mtab should be spanked.
20 #include <sys/types.h>
26 #include "mount/canonicalize.c"
29 /* Updating mtab ----------------------------------------------*/
31 /* Flag for already existing lock file. */
32 static int we_created_lockfile
= 0;
33 static int lockfile_fd
= -1;
35 /* Flag to indicate that signals have been set up. */
36 static int signals_have_been_setup
= 0;
38 /* Ensure that the lock is released if we are interrupted. */
39 extern char *strsignal(int sig
); /* not always in <string.h> */
42 setlkw_timeout (int sig
) {
43 /* nothing, fcntl will fail anyway */
46 #define _PATH_MOUNTED "/etc/mtab"
47 #define _PATH_MOUNTED_LOCK "/etc/mtab~"
48 #define PROC_SUPER_MAGIC 0x9fa0
50 /* exit status - bits below are ORed */
51 #define EX_USAGE 1 /* incorrect invocation or permission */
52 #define EX_SYSERR 2 /* out of memory, cannot fork, ... */
53 #define EX_SOFTWARE 4 /* internal mount bug or wrong version */
54 #define EX_USER 8 /* user interrupt */
55 #define EX_FILEIO 16 /* problems writing, locking, ... mtab/fstab */
56 #define EX_FAIL 32 /* mount failure */
57 #define EX_SOMEOK 64 /* some mount succeeded */
59 int die(int err
, const char *fmt
, ...) {
63 vfprintf(stderr
, fmt
, args
);
64 fprintf(stderr
, "\n");
72 die(EX_USER
, "%s", strsignal(sig
));
75 /* Remove lock file. */
78 if (we_created_lockfile
) {
81 unlink (_PATH_MOUNTED_LOCK
);
82 we_created_lockfile
= 0;
86 /* Create the lock file.
87 The lock file will be removed if we catch a signal or when we exit. */
88 /* The old code here used flock on a lock file /etc/mtab~ and deleted
89 this lock file afterwards. However, as rgooch remarks, that has a
90 race: a second mount may be waiting on the lock and proceed as
91 soon as the lock file is deleted by the first mount, and immediately
92 afterwards a third mount comes, creates a new /etc/mtab~, applies
93 flock to that, and also proceeds, so that the second and third mount
94 now both are scribbling in /etc/mtab.
95 The new code uses a link() instead of a creat(), where we proceed
96 only if it was us that created the lock, and hence we always have
97 to delete the lock afterwards. Now the use of flock() is in principle
98 superfluous, but avoids an arbitrary sleep(). */
100 /* Where does the link point to? Obvious choices are mtab and mtab~~.
101 HJLu points out that the latter leads to races. Right now we use
102 mtab~.<pid> instead. Use 20 as upper bound for the length of %d. */
103 #define MOUNTLOCK_LINKTARGET _PATH_MOUNTED_LOCK "%d"
104 #define MOUNTLOCK_LINKTARGET_LTH (sizeof(_PATH_MOUNTED_LOCK)+20)
107 * The original mount locking code has used sleep(1) between attempts and
108 * maximal number of attemps has been 5.
110 * There was very small number of attempts and extremely long waiting (1s)
111 * that is useless on machines with large number of concurret mount processes.
113 * Now we wait few thousand microseconds between attempts and we have global
114 * time limit (30s) rather than limit for number of attempts. The advantage
115 * is that this method also counts time which we spend in fcntl(F_SETLKW) and
116 * number of attempts is not so much restricted.
118 * -- kzak@redhat.com [2007-Mar-2007]
121 /* maximum seconds between first and last attempt */
122 #define MOUNTLOCK_MAXTIME 30
124 /* sleep time (in microseconds, max=999999) between attempts */
125 #define MOUNTLOCK_WAITTIME 5000
130 struct timespec waittime
;
131 struct timeval maxtime
;
132 char linktargetfile
[MOUNTLOCK_LINKTARGET_LTH
];
134 if (!signals_have_been_setup
) {
138 sa
.sa_handler
= handler
;
140 sigfillset (&sa
.sa_mask
);
142 while (sigismember (&sa
.sa_mask
, ++sig
) != -1
145 sa
.sa_handler
= setlkw_timeout
;
147 sa
.sa_handler
= handler
;
148 sigaction (sig
, &sa
, (struct sigaction
*) 0);
150 signals_have_been_setup
= 1;
153 snprintf(linktargetfile
, sizeof(linktargetfile
), MOUNTLOCK_LINKTARGET
,
156 i
= open (linktargetfile
, O_WRONLY
|O_CREAT
, S_IRUSR
|S_IWUSR
);
159 /* linktargetfile does not exist (as a file)
160 and we cannot create it. Read-only filesystem?
161 Too many files open in the system?
163 die (EX_FILEIO
, "can't create lock file %s: %s "
164 "(use -n flag to override)",
165 linktargetfile
, strerror (errsv
));
169 gettimeofday(&maxtime
, NULL
);
170 maxtime
.tv_sec
+= MOUNTLOCK_MAXTIME
;
173 waittime
.tv_nsec
= (1000 * MOUNTLOCK_WAITTIME
);
175 /* Repeat until it was us who made the link */
176 while (!we_created_lockfile
) {
181 j
= link(linktargetfile
, _PATH_MOUNTED_LOCK
);
185 we_created_lockfile
= 1;
187 if (j
< 0 && errsv
!= EEXIST
) {
188 (void) unlink(linktargetfile
);
189 die (EX_FILEIO
, "can't link lock file %s: %s "
190 "(use -n flag to override)",
191 _PATH_MOUNTED_LOCK
, strerror (errsv
));
194 lockfile_fd
= open (_PATH_MOUNTED_LOCK
, O_WRONLY
);
196 if (lockfile_fd
< 0) {
197 /* Strange... Maybe the file was just deleted? */
199 gettimeofday(&now
, NULL
);
200 if (errno
== ENOENT
&& now
.tv_sec
< maxtime
.tv_sec
) {
201 we_created_lockfile
= 0;
204 (void) unlink(linktargetfile
);
205 die (EX_FILEIO
, "can't open lock file %s: %s "
206 "(use -n flag to override)",
207 _PATH_MOUNTED_LOCK
, strerror (errsv
));
210 flock
.l_type
= F_WRLCK
;
211 flock
.l_whence
= SEEK_SET
;
216 /* We made the link. Now claim the lock. */
217 if (fcntl (lockfile_fd
, F_SETLK
, &flock
) == -1) {
218 /* proceed, since it was us who created the lockfile anyway */
220 (void) unlink(linktargetfile
);
222 /* Someone else made the link. Wait. */
223 gettimeofday(&now
, NULL
);
224 if (now
.tv_sec
< maxtime
.tv_sec
) {
225 alarm(maxtime
.tv_sec
- now
.tv_sec
);
226 if (fcntl (lockfile_fd
, F_SETLKW
, &flock
) == -1) {
228 (void) unlink(linktargetfile
);
229 die (EX_FILEIO
, "can't lock lock file %s: %s",
230 _PATH_MOUNTED_LOCK
, (errno
== EINTR
) ?
231 "timed out" : strerror (errsv
));
235 nanosleep(&waittime
, NULL
);
237 (void) unlink(linktargetfile
);
238 die (EX_FILEIO
, "Cannot create link %s\n"
239 "Perhaps there is a stale lock file?\n",
248 update_mtab_entry(const char *spec
, const char *node
, const char *type
,
249 const char *opts
, int flags
, int freq
, int pass
) {
251 int err
= statfs(_PATH_MOUNTED
, &buf
);
253 printf("mount: can't statfs %s: %s", _PATH_MOUNTED
,
257 /* /etc/mtab is symbol link to /proc/self/mounts? */
258 if (buf
.f_type
== PROC_SUPER_MAGIC
)
265 mnt
.mnt_fsname
= strdup(spec
);
266 mnt
.mnt_dir
= canonicalize_path(node
);
267 mnt
.mnt_type
= strdup(type
);
268 mnt
.mnt_opts
= strdup(opts
);
270 mnt
.mnt_passno
= pass
;
275 fp
= setmntent(_PATH_MOUNTED
, "a+");
278 printf("mount: can't open %s: %s", _PATH_MOUNTED
,
281 if ((addmntent (fp
, &mnt
)) == 1) {
283 printf("mount: error writing %s: %s",
284 _PATH_MOUNTED
, strerror (errsv
));
290 free(mnt
.mnt_fsname
);