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_file.c
48 #endif /* HAVE_CONFIG_H */
52 #define NFILE_RETRIES 10 /* number of retries (seconds) */
54 #ifdef MOUNT_TABLE_ON_FILE
56 static FILE *mnt_file
;
60 * If the system is being trashed by something, then
61 * opening mtab may fail with ENFILE. So, go to sleep
62 * for a second and try again. (Yes - this has happened to me.)
64 * Note that this *may* block the automounter, oh well.
65 * If we get to this state then things are badly wrong anyway...
67 * Give the system 10 seconds to recover but then give up.
68 * Hopefully something else will exit and free up some file
69 * table slots in that time.
72 # define lock(fd) (flock((fd), LOCK_EX))
73 # else /* not HAVE_FLOCK */
86 rc
= fcntl(fd
, F_SETLKW
, (caddr_t
) & lk
);
87 if (rc
< 0 && (errno
== EACCES
|| errno
== EAGAIN
)) {
89 dlog("Blocked, trying to obtain exclusive mtab lock");
96 # endif /* not HAVE_FLOCK */
100 open_locked_mtab(const char *mnttabname
, char *mode
, char *fs
)
105 * There is a possible race condition if two processes enter
106 * this routine at the same time. One will be blocked by the
107 * exclusive lock below (or by the shared lock in setmntent)
108 * and by the time the second process has the exclusive lock
109 * it will be on the wrong underlying object. To check for this
110 * the mtab file is stat'ed before and after all the locking
111 * sequence, and if it is a different file then we assume that
112 * it may be the wrong file (only "may", since there is another
113 * race between the initial stat and the setmntent).
115 * Simpler solutions to this problem are invited...
120 struct stat st_before
, st_after
;
124 dlog("Forced close on %s in read_mtab", mnttabname
);
134 if (stat(mnttabname
, &st_before
) < 0) {
135 plog(XLOG_ERROR
, "%s: stat: %m", mnttabname
);
136 if (errno
== ESTALE
) {
137 /* happens occasionally */
142 * If 'mnttabname' file does not exist give setmntent() a
143 * chance to create it (depending on the mode).
144 * Otherwise, bail out.
146 else if (errno
!= ENOENT
) {
152 mfp
= setmntent((char *)mnttabname
, mode
);
155 * Since setmntent locks the descriptor, it
156 * is possible it can fail... so retry if
159 if (errno
== EACCES
|| errno
== EAGAIN
) {
161 dlog("Blocked, trying to obtain exclusive mtab lock");
164 } else if (errno
== ENFILE
&& retries
++ < NFILE_RETRIES
) {
168 plog(XLOG_ERROR
, "setmntent(\"%s\", \"%s\"): %m", mnttabname
, mode
);
172 * At this point we have an exclusive lock on the mount list,
173 * but it may be the wrong one so...
177 * Need to get an exclusive lock on the current
178 * mount table until we have a new copy written
179 * out, when the lock is released in free_mntlist.
180 * flock is good enough since the mount table is
181 * not shared between machines.
184 rc
= lock(fileno(mfp
));
185 while (rc
< 0 && errno
== EINTR
);
187 plog(XLOG_ERROR
, "Couldn't lock %s: %m", mnttabname
);
192 * Now check whether the mtab file has changed under our feet
194 if (stat(mnttabname
, &st_after
) < 0) {
195 plog(XLOG_ERROR
, "%s: stat: %m", mnttabname
);
198 if (st_before
.st_dev
!= st_after
.st_dev
||
199 st_before
.st_ino
!= st_after
.st_ino
) {
202 /* Sometimes print a warning */
204 "Possible mount table race - retrying %s", fs
);
206 racing
= (racing
+ 1) & 3;
208 * Take a nap. From: Doug Kingston <dpk@morgan.com>
211 tv
.tv_usec
= (am_mypid
& 0x07) << 17;
213 if (select(0, (voidp
) 0, (voidp
) 0, (voidp
) 0, &tv
) < 0)
214 plog(XLOG_WARNING
, "mtab nap failed: %m");
224 * Unlock the mount table
230 * Release file lock, by closing the file
233 dlog("unlock_mntlist: releasing");
241 * Write out a mount list
244 rewrite_mtab(mntlist
*mp
, const char *mnttabname
)
250 * Concoct a temporary name in the same directory as the target mount
251 * table so that rename() will work.
259 xstrlcpy(mcp
, mnttabname
, sizeof(mcp
));
260 cp
= strrchr(mcp
, '/');
262 memmove(tmpname
, mcp
, cp
- mcp
);
263 tmpname
[cp
- mcp
] = '\0';
265 plog(XLOG_WARNING
, "No '/' in mtab (%s), using \".\" as tmp directory", mnttabname
);
269 xstrlcat(tmpname
, "/mtabXXXXXX", sizeof(tmpname
));
273 tmpfd
= mkstemp(tmpname
);
275 #else /* not HAVE_MKSTEMP */
277 tmpfd
= open(tmpname
, O_RDWR
| O_CREAT
| O_TRUNC
, 0644);
278 #endif /* not HAVE_MKSTEMP */
280 if (errno
== ENFILE
&& retries
++ < NFILE_RETRIES
) {
284 plog(XLOG_ERROR
, "%s: open: %m", tmpname
);
287 if (close(tmpfd
) < 0)
288 plog(XLOG_ERROR
, "Couldn't close tmp file descriptor: %m");
292 mfp
= setmntent(tmpname
, "w");
294 if (errno
== ENFILE
&& retries
++ < NFILE_RETRIES
) {
298 plog(XLOG_ERROR
, "setmntent(\"%s\", \"w\"): %m", tmpname
);
304 if (addmntent(mfp
, mp
->mnt
)) {
305 plog(XLOG_ERROR
, "Can't write entry to %s", tmpname
);
314 * SunOS 4.1 manuals say that the return code from entmntent()
315 * is always 1 and to treat as a void. That means we need to
316 * call fflush() to make sure the new mtab file got written.
319 plog(XLOG_ERROR
, "flush new mtab file: %m");
323 (void) endmntent(mfp
);
326 * Rename temporary mtab to real mtab
328 if (rename(tmpname
, mnttabname
) < 0) {
329 plog(XLOG_ERROR
, "rename %s to %s: %m", tmpname
, mnttabname
);
335 (void) unlink(tmpname
);
340 mtab_stripnl(char *s
)
351 * Append a mntent structure to the
352 * current mount table.
355 write_mntent(mntent_t
*mp
, const char *mnttabname
)
360 mfp
= open_locked_mtab(mnttabname
, "a", mp
->mnt_dir
);
362 mtab_stripnl(mp
->mnt_opts
);
363 if (addmntent(mfp
, mp
))
364 plog(XLOG_ERROR
, "Couldn't write %s: %m", mnttabname
);
366 plog(XLOG_ERROR
, "Couldn't flush %s: %m", mnttabname
);
367 (void) endmntent(mfp
);
369 if (errno
== ENFILE
&& retries
< NFILE_RETRIES
) {
373 plog(XLOG_ERROR
, "setmntent(\"%s\", \"a\"): %m", mnttabname
);
377 #endif /* MOUNT_TABLE_ON_FILE */
381 mnt_dup(mntent_t
*mp
)
383 mntent_t
*new_mp
= ALLOC(mntent_t
);
385 new_mp
->mnt_fsname
= strdup(mp
->mnt_fsname
);
386 new_mp
->mnt_dir
= strdup(mp
->mnt_dir
);
387 new_mp
->mnt_type
= strdup(mp
->mnt_type
);
388 new_mp
->mnt_opts
= strdup(mp
->mnt_opts
);
390 new_mp
->mnt_freq
= mp
->mnt_freq
;
391 new_mp
->mnt_passno
= mp
->mnt_passno
;
393 #ifdef HAVE_MNTENT_T_MNT_TIME
394 # ifdef HAVE_MNTENT_T_MNT_TIME_STRING
395 new_mp
->mnt_time
= strdup(mp
->mnt_time
);
396 # else /* not HAVE_MNTENT_T_MNT_TIME_STRING */
397 new_mp
->mnt_time
= mp
->mnt_time
;
398 # endif /* not HAVE_MNTENT_T_MNT_TIME_STRING */
399 #endif /* HAVE_MNTENT_T_MNT_TIME */
401 #ifdef HAVE_MNTENT_T_MNT_CNODE
402 new_mp
->mnt_cnode
= mp
->mnt_cnode
;
403 #endif /* HAVE_MNTENT_T_MNT_CNODE */
410 * Read a mount table into memory
413 read_mtab(char *fs
, const char *mnttabname
)
418 FILE *mfp
= open_locked_mtab(mnttabname
, "r+", fs
);
426 * XXX - In SunOS 4 there is (yet another) memory leak
427 * which loses 1K the first time getmntent is called.
430 while ((mep
= getmntent(mfp
))) {
432 * Allocate a new slot
434 *mpp
= ALLOC(struct mntlist
);
437 * Copy the data returned by getmntent
439 (*mpp
)->mnt
= mnt_dup(mep
);
442 * Move to next pointer
444 mpp
= &(*mpp
)->mnext
;
448 #ifdef MOUNT_TABLE_ON_FILE
450 * If we are not updating the mount table then we
451 * can free the resources held here, otherwise they
452 * must be held until the mount table update is complete
455 #else /* not MOUNT_TABLE_ON_FILE */
457 #endif /* not MOUNT_TABLE_ON_FILE */