4 * Copyright (c) 1997-2009 Erez Zadok
5 * Copyright (c) 1990 Jan-Simon Pendry
6 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
7 * Copyright (c) 1990 The Regents of the University of California.
10 * This code is derived from software contributed to Berkeley by
11 * Jan-Simon Pendry at Imperial College, London.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgment:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * File: am-utils/conf/mtab/mtab_linux.c
46 /* This file was adapted by Red Hat for Linux from mtab_file.c */
49 * The locking code must be kept in sync with that used
50 * by the mount command in util-linux, otherwise you'll
51 * end with with race conditions leading to a corrupt
52 * /etc/mtab, particularly when AutoFS is used on same
58 #endif /* HAVE_CONFIG_H */
62 #define NFILE_RETRIES 10 /* number of retries (seconds) */
63 #define LOCK_TIMEOUT 10
65 #ifdef MOUNT_TABLE_ON_FILE
67 # define PROC_MOUNTS "/proc/mounts"
69 static FILE *mnt_file
= NULL
;
70 /* Information about mtab. ------------------------------------*/
71 static int have_mtab_info
= 0;
72 static int var_mtab_does_not_exist
= 0;
73 static int var_mtab_is_a_symlink
= 0;
74 /* Flag for already existing lock file. */
75 static int we_created_lockfile
= 0;
76 static int lockfile_fd
= -1;
82 struct stat mtab_stat
;
84 if (!have_mtab_info
) {
85 if (lstat(MOUNTED
, &mtab_stat
))
86 var_mtab_does_not_exist
= 1;
87 else if (S_ISLNK(mtab_stat
.st_mode
))
88 var_mtab_is_a_symlink
= 1;
95 mtab_is_a_symlink(void)
98 return var_mtab_is_a_symlink
;
108 * Should we write to /etc/mtab upon an update? Probably not if it is a
109 * symlink to /proc/mounts, since that would create a file /proc/mounts in
110 * case the proc filesystem is not mounted.
112 if (mtab_is_a_symlink())
116 int fd
= open(MOUNTED
, O_RDWR
| O_CREAT
, 0644);
128 setlkw_timeout(int sig
)
130 /* nothing, fcntl will fail anyway */
135 * Create the lock file.
136 * The lock file will be removed if we catch a signal or when we exit.
138 * The old code here used flock on a lock file /etc/mtab~ and deleted
139 * this lock file afterwards. However, as rgooch remarks, that has a
140 * race: a second mount may be waiting on the lock and proceed as
141 * soon as the lock file is deleted by the first mount, and immediately
142 * afterwards a third mount comes, creates a new /etc/mtab~, applies
143 * flock to that, and also proceeds, so that the second and third mount
144 * now both are scribbling in /etc/mtab.
145 * The new code uses a link() instead of a creat(), where we proceed
146 * only if it was us that created the lock, and hence we always have
147 * to delete the lock afterwards. Now the use of flock() is in principle
148 * superfluous, but avoids an arbitrary sleep().
152 * Where does the link point to? Obvious choices are mtab and mtab~~.
153 * HJLu points out that the latter leads to races. Right now we use
154 * mtab~.<pid> instead.
156 #define MOUNTED_LOCK "/etc/mtab~"
157 #define MOUNTLOCK_LINKTARGET MOUNTED_LOCK "%d"
162 int tries
= 100000, i
;
163 char *linktargetfile
;
167 * Redhat's original code set a signal handler called "handler()" for all
168 * non-ALRM signals. The handler called unlock_mntlist(), plog'ed the
169 * signal name, and then exit(1)! Never, ever, exit() from inside a
170 * utility function. This messed up Amd's careful signal-handling code,
171 * and caused Amd to abort uncleanly only any other "innocent" signal
172 * (even simple SIGUSR1), leaving behind a hung Amd mnt point. That code
173 * should have at least restored the signal handlers' states upon a
174 * successful mtab unlocking. Anyway, that handler was unnecessary,
175 * because will call unlock_mntlist() properly anyway on exit.
177 setup_sighandler(SIGALRM
, setlkw_timeout
);
179 /* somewhat clumsy, but some ancient systems do not have snprintf() */
180 /* use 20 as upper bound for the length of %d output */
181 l
= strlen(MOUNTLOCK_LINKTARGET
) + 20;
182 linktargetfile
= xmalloc(l
);
183 xsnprintf(linktargetfile
, l
, MOUNTLOCK_LINKTARGET
, getpid());
185 i
= open(linktargetfile
, O_WRONLY
|O_CREAT
, 0);
189 * linktargetfile does not exist (as a file) and we cannot create
190 * it. Read-only filesystem? Too many files open in the system?
193 plog(XLOG_ERROR
, "can't create lock file %s: %s (use -n flag to override)",
194 linktargetfile
, strerror(errsv
));
199 /* Repeat until it was us who made the link */
200 while (!we_created_lockfile
) {
204 j
= link(linktargetfile
, MOUNTED_LOCK
);
207 if (j
< 0 && errsv
!= EEXIST
) {
208 (void) unlink(linktargetfile
);
209 plog(XLOG_ERROR
, "can't link lock file %s: %s ",
210 MOUNTED_LOCK
, strerror(errsv
));
214 lockfile_fd
= open(MOUNTED_LOCK
, O_WRONLY
);
215 if (lockfile_fd
< 0) {
217 /* Strange... Maybe the file was just deleted? */
218 if (errno
== ENOENT
&& tries
-- > 0) {
219 if (tries
% 200 == 0)
223 (void) unlink(linktargetfile
);
224 plog(XLOG_ERROR
,"can't open lock file %s: %s ",
225 MOUNTED_LOCK
, strerror(errsv
));
229 flock
.l_type
= F_WRLCK
;
230 flock
.l_whence
= SEEK_SET
;
235 /* We made the link. Now claim the lock. */
236 if (fcntl(lockfile_fd
, F_SETLK
, &flock
) == -1) {
238 plog(XLOG_ERROR
, "Can't lock lock file %s: %s",
239 MOUNTED_LOCK
, strerror(errsv
));
240 /* proceed, since it was us who created the lockfile anyway */
242 we_created_lockfile
= 1;
243 (void) unlink(linktargetfile
);
245 static int tries
= 0;
247 /* Someone else made the link. Wait. */
250 if (fcntl(lockfile_fd
, F_SETLKW
, &flock
) == -1) {
252 (void) unlink(linktargetfile
);
253 plog(XLOG_ERROR
, "can't lock lock file %s: %s",
254 MOUNTED_LOCK
, (errno
== EINTR
) ?
255 "timed out" : strerror(errsv
));
260 * Limit the number of iterations - maybe there
261 * still is some old /etc/mtab~
264 if (tries
% 200 == 0)
266 if (tries
> 100000) {
267 (void) unlink(linktargetfile
);
270 "Cannot create link %s; Perhaps there is a stale lock file?",
281 open_locked_mtab(const char *mnttabname
, char *mode
, char *fs
)
286 dlog("Forced close on %s in read_mtab", mnttabname
);
291 if (!mtab_is_a_symlink() &&
293 plog(XLOG_ERROR
, "Couldn't lock mtab");
297 mfp
= setmntent((char *)mnttabname
, mode
);
299 plog(XLOG_ERROR
, "setmntent(\"%s\", \"%s\"): %m", mnttabname
, mode
);
307 * Unlock the mount table
312 if (mnt_file
|| we_created_lockfile
)
313 dlog("unlock_mntlist: releasing");
318 if (we_created_lockfile
) {
321 unlink(MOUNTED_LOCK
);
322 we_created_lockfile
= 0;
328 * Write out a mount list
331 rewrite_mtab(mntlist
*mp
, const char *mnttabname
)
341 if (!mtab_is_writable()) {
346 * Concoct a temporary name in the same directory as the target mount
347 * table so that rename() will work.
349 xstrlcpy(mcp
, mnttabname
, sizeof(mcp
));
350 cp
= strrchr(mcp
, '/');
352 memmove(tmpname
, mcp
, cp
- mcp
);
353 tmpname
[cp
- mcp
] = '\0';
355 plog(XLOG_WARNING
, "No '/' in mtab (%s), using \".\" as tmp directory", mnttabname
);
359 xstrlcat(tmpname
, "/mtabXXXXXX", sizeof(tmpname
));
363 tmpfd
= mkstemp(tmpname
);
365 #else /* not HAVE_MKSTEMP */
367 tmpfd
= open(tmpname
, O_RDWR
| O_CREAT
| O_TRUNC
, 0644);
368 #endif /* not HAVE_MKSTEMP */
370 if (errno
== ENFILE
&& retries
++ < NFILE_RETRIES
) {
374 plog(XLOG_ERROR
, "%s: open: %m", tmpname
);
377 if (close(tmpfd
) < 0)
378 plog(XLOG_ERROR
, "Couldn't close tmp file descriptor: %m");
382 mfp
= setmntent(tmpname
, "w");
384 if (errno
== ENFILE
&& retries
++ < NFILE_RETRIES
) {
388 plog(XLOG_ERROR
, "setmntent(\"%s\", \"w\"): %m", tmpname
);
394 if (addmntent(mfp
, mp
->mnt
)) {
395 plog(XLOG_ERROR
, "Can't write entry to %s", tmpname
);
404 * SunOS 4.1 manuals say that the return code from entmntent()
405 * is always 1 and to treat as a void. That means we need to
406 * call fflush() to make sure the new mtab file got written.
409 plog(XLOG_ERROR
, "flush new mtab file: %m");
413 (void) endmntent(mfp
);
416 * Rename temporary mtab to real mtab
418 if (rename(tmpname
, mnttabname
) < 0) {
419 plog(XLOG_ERROR
, "rename %s to %s: %m", tmpname
, mnttabname
);
425 (void) unlink(tmpname
);
430 mtab_stripnl(char *s
)
441 * Append a mntent structure to the
442 * current mount table.
445 write_mntent(mntent_t
*mp
, const char *mnttabname
)
450 if (!mtab_is_writable()) {
455 mfp
= open_locked_mtab(mnttabname
, "a", mp
->mnt_dir
);
457 mtab_stripnl(mp
->mnt_opts
);
458 if (addmntent(mfp
, mp
))
459 plog(XLOG_ERROR
, "Couldn't write %s: %m", mnttabname
);
461 plog(XLOG_ERROR
, "Couldn't flush %s: %m", mnttabname
);
462 (void) endmntent(mfp
);
464 if (errno
== ENFILE
&& retries
< NFILE_RETRIES
) {
468 plog(XLOG_ERROR
, "setmntent(\"%s\", \"a\"): %m", mnttabname
);
474 #endif /* MOUNT_TABLE_ON_FILE */
478 mnt_dup(mntent_t
*mp
)
480 mntent_t
*new_mp
= ALLOC(mntent_t
);
482 new_mp
->mnt_fsname
= strdup(mp
->mnt_fsname
);
483 new_mp
->mnt_dir
= strdup(mp
->mnt_dir
);
484 new_mp
->mnt_type
= strdup(mp
->mnt_type
);
485 new_mp
->mnt_opts
= strdup(mp
->mnt_opts
);
487 new_mp
->mnt_freq
= mp
->mnt_freq
;
488 new_mp
->mnt_passno
= mp
->mnt_passno
;
490 #ifdef HAVE_MNTENT_T_MNT_TIME
491 # ifdef HAVE_MNTENT_T_MNT_TIME_STRING
492 new_mp
->mnt_time
= strdup(mp
->mnt_time
);
493 # else /* not HAVE_MNTENT_T_MNT_TIME_STRING */
494 new_mp
->mnt_time
= mp
->mnt_time
;
495 # endif /* not HAVE_MNTENT_T_MNT_TIME_STRING */
496 #endif /* HAVE_MNTENT_T_MNT_TIME */
498 #ifdef HAVE_MNTENT_T_MNT_CNODE
499 new_mp
->mnt_cnode
= mp
->mnt_cnode
;
500 #endif /* HAVE_MNTENT_T_MNT_CNODE */
507 * Read a mount table into memory
510 read_mtab(char *fs
, const char *mnttabname
)
516 FILE *mfp
= open_locked_mtab(mnttabname
, "r+", fs
);
524 * XXX - In SunOS 4 there is (yet another) memory leak
525 * which loses 1K the first time getmntent is called.
528 while ((mep
= getmntent(mfp
))) {
530 * Allocate a new slot
532 *mpp
= ALLOC(struct mntlist
);
535 * Copy the data returned by getmntent
537 (*mpp
)->mnt
= mnt_dup(mep
);
540 * Move to next pointer
542 mpp
= &(*mpp
)->mnext
;
546 #ifdef MOUNT_TABLE_ON_FILE
548 * If we are not updating the mount table then we
549 * can free the resources held here, otherwise they
550 * must be held until the mount table update is complete
553 #else /* not MOUNT_TABLE_ON_FILE */
555 #endif /* not MOUNT_TABLE_ON_FILE */