1999-11-21 08:06:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1999 Martin Blapp
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-05-03 21:06:42 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1999-11-21 08:06:00 +00:00
|
|
|
|
|
|
|
#include <sys/syslog.h>
|
|
|
|
|
|
|
|
#include <rpc/rpc.h>
|
2009-06-24 18:42:21 +00:00
|
|
|
#include <rpcsvc/mount.h>
|
1999-11-21 08:06:00 +00:00
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2001-07-22 12:17:51 +00:00
|
|
|
#include <limits.h>
|
1999-11-21 08:06:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "mounttab.h"
|
|
|
|
|
|
|
|
struct mtablist *mtabhead;
|
|
|
|
|
2009-12-28 17:57:37 +00:00
|
|
|
static void badline(const char *field, const char *bad);
|
2001-07-22 12:17:51 +00:00
|
|
|
|
1999-11-21 08:06:00 +00:00
|
|
|
/*
|
|
|
|
* Add an entry to PATH_MOUNTTAB for each mounted NFS filesystem,
|
|
|
|
* so the client can notify the NFS server even after reboot.
|
|
|
|
*/
|
|
|
|
int
|
2009-12-30 06:36:42 +00:00
|
|
|
add_mtab(char *hostp, char *dirp)
|
|
|
|
{
|
1999-11-21 08:06:00 +00:00
|
|
|
FILE *mtabfile;
|
|
|
|
|
|
|
|
if ((mtabfile = fopen(PATH_MOUNTTAB, "a")) == NULL)
|
|
|
|
return (0);
|
|
|
|
else {
|
2000-10-08 09:24:45 +00:00
|
|
|
fprintf(mtabfile, "%ld\t%s\t%s\n",
|
2001-07-22 12:17:51 +00:00
|
|
|
(long)time(NULL), hostp, dirp);
|
1999-11-21 08:06:00 +00:00
|
|
|
fclose(mtabfile);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read mounttab line for line and return struct mtablist.
|
|
|
|
*/
|
|
|
|
int
|
2009-12-30 06:36:42 +00:00
|
|
|
read_mtab(void)
|
|
|
|
{
|
2001-07-22 12:17:51 +00:00
|
|
|
struct mtablist **mtabpp, *mtabp;
|
1999-11-21 08:06:00 +00:00
|
|
|
char *hostp, *dirp, *cp;
|
|
|
|
char str[STRSIZ];
|
2001-07-22 12:17:51 +00:00
|
|
|
char *timep, *endp;
|
2009-12-28 17:57:37 +00:00
|
|
|
time_t actiontime;
|
2001-07-22 12:17:51 +00:00
|
|
|
u_long ultmp;
|
1999-11-21 08:06:00 +00:00
|
|
|
FILE *mtabfile;
|
|
|
|
|
|
|
|
if ((mtabfile = fopen(PATH_MOUNTTAB, "r")) == NULL) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return (0);
|
|
|
|
else {
|
|
|
|
syslog(LOG_ERR, "can't open %s", PATH_MOUNTTAB);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
2009-12-28 17:57:37 +00:00
|
|
|
actiontime = 0;
|
1999-11-21 08:06:00 +00:00
|
|
|
mtabpp = &mtabhead;
|
|
|
|
while (fgets(str, STRSIZ, mtabfile) != NULL) {
|
|
|
|
cp = str;
|
|
|
|
errno = 0;
|
|
|
|
if (*cp == '#' || *cp == ' ' || *cp == '\n')
|
|
|
|
continue;
|
|
|
|
timep = strsep(&cp, " \t\n");
|
2001-07-22 12:17:51 +00:00
|
|
|
if (timep == NULL || *timep == '\0') {
|
|
|
|
badline("time", timep);
|
1999-11-21 08:06:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
hostp = strsep(&cp, " \t\n");
|
2001-07-22 12:17:51 +00:00
|
|
|
if (hostp == NULL || *hostp == '\0') {
|
|
|
|
badline("host", hostp);
|
1999-11-21 08:06:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dirp = strsep(&cp, " \t\n");
|
2001-07-22 12:17:51 +00:00
|
|
|
if (dirp == NULL || *dirp == '\0') {
|
|
|
|
badline("dir", dirp);
|
1999-11-21 08:06:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
2001-07-22 12:17:51 +00:00
|
|
|
ultmp = strtoul(timep, &endp, 10);
|
|
|
|
if (ultmp == ULONG_MAX || *endp != '\0') {
|
|
|
|
badline("time", timep);
|
1999-11-21 08:06:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-12-28 17:57:37 +00:00
|
|
|
actiontime = ultmp;
|
1999-11-21 08:06:00 +00:00
|
|
|
if ((mtabp = malloc(sizeof (struct mtablist))) == NULL) {
|
|
|
|
syslog(LOG_ERR, "malloc");
|
|
|
|
fclose(mtabfile);
|
|
|
|
return (0);
|
|
|
|
}
|
2009-12-28 17:57:37 +00:00
|
|
|
mtabp->mtab_time = actiontime;
|
2009-06-24 18:42:21 +00:00
|
|
|
memmove(mtabp->mtab_host, hostp, MNTNAMLEN);
|
|
|
|
mtabp->mtab_host[MNTNAMLEN - 1] = '\0';
|
|
|
|
memmove(mtabp->mtab_dirp, dirp, MNTPATHLEN);
|
|
|
|
mtabp->mtab_dirp[MNTPATHLEN - 1] = '\0';
|
1999-11-21 08:06:00 +00:00
|
|
|
mtabp->mtab_next = (struct mtablist *)NULL;
|
|
|
|
*mtabpp = mtabp;
|
|
|
|
mtabpp = &mtabp->mtab_next;
|
|
|
|
}
|
|
|
|
fclose(mtabfile);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rewrite PATH_MOUNTTAB from scratch and skip bad entries.
|
|
|
|
* Unlink PATH_MOUNTAB if no entry is left.
|
|
|
|
*/
|
|
|
|
int
|
2009-12-30 06:36:42 +00:00
|
|
|
write_mtab(int verbose)
|
|
|
|
{
|
2001-07-22 12:17:51 +00:00
|
|
|
struct mtablist *mtabp, *mp;
|
1999-11-21 08:06:00 +00:00
|
|
|
FILE *mtabfile;
|
|
|
|
int line;
|
|
|
|
|
|
|
|
if ((mtabfile = fopen(PATH_MOUNTTAB, "w")) == NULL) {
|
|
|
|
syslog(LOG_ERR, "can't write to %s", PATH_MOUNTTAB);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
line = 0;
|
|
|
|
for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
|
2001-07-22 12:17:51 +00:00
|
|
|
if (mtabp->mtab_host[0] == '\0')
|
|
|
|
continue;
|
|
|
|
/* Skip if a later (hence more recent) entry is identical. */
|
|
|
|
for (mp = mtabp->mtab_next; mp != NULL; mp = mp->mtab_next)
|
|
|
|
if (strcmp(mtabp->mtab_host, mp->mtab_host) == 0 &&
|
|
|
|
strcmp(mtabp->mtab_dirp, mp->mtab_dirp) == 0)
|
|
|
|
break;
|
|
|
|
if (mp != NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fprintf(mtabfile, "%ld\t%s\t%s\n",
|
|
|
|
(long)mtabp->mtab_time, mtabp->mtab_host,
|
|
|
|
mtabp->mtab_dirp);
|
|
|
|
if (verbose)
|
|
|
|
warnx("write mounttab entry %s:%s",
|
|
|
|
mtabp->mtab_host, mtabp->mtab_dirp);
|
|
|
|
line++;
|
1999-11-21 08:06:00 +00:00
|
|
|
}
|
|
|
|
fclose(mtabfile);
|
|
|
|
if (line == 0) {
|
|
|
|
if (unlink(PATH_MOUNTTAB) == -1) {
|
|
|
|
syslog(LOG_ERR, "can't remove %s", PATH_MOUNTTAB);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark the entries as clean where RPC calls have been done successfully.
|
|
|
|
*/
|
|
|
|
void
|
2009-12-30 06:36:42 +00:00
|
|
|
clean_mtab(char *hostp, char *dirp, int verbose)
|
|
|
|
{
|
1999-11-21 08:06:00 +00:00
|
|
|
struct mtablist *mtabp;
|
|
|
|
char *host;
|
|
|
|
|
2001-07-22 12:17:51 +00:00
|
|
|
/* Copy hostp in case it points to an entry that we are zeroing out. */
|
1999-11-21 08:06:00 +00:00
|
|
|
host = strdup(hostp);
|
|
|
|
for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
|
2001-08-02 21:46:21 +00:00
|
|
|
if (strcmp(mtabp->mtab_host, host) != 0)
|
2001-07-22 12:17:51 +00:00
|
|
|
continue;
|
|
|
|
if (dirp != NULL && strcmp(mtabp->mtab_dirp, dirp) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
warnx("delete mounttab entry%s %s:%s",
|
|
|
|
(dirp == NULL) ? " by host" : "",
|
|
|
|
mtabp->mtab_host, mtabp->mtab_dirp);
|
2009-06-24 18:42:21 +00:00
|
|
|
bzero(mtabp->mtab_host, MNTNAMLEN);
|
1999-11-21 08:06:00 +00:00
|
|
|
}
|
|
|
|
free(host);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free struct mtablist mtab.
|
|
|
|
*/
|
|
|
|
void
|
2009-12-30 06:36:42 +00:00
|
|
|
free_mtab(void)
|
|
|
|
{
|
1999-11-21 08:06:00 +00:00
|
|
|
struct mtablist *mtabp;
|
|
|
|
|
2001-07-22 12:17:51 +00:00
|
|
|
while ((mtabp = mtabhead) != NULL) {
|
|
|
|
mtabhead = mtabhead->mtab_next;
|
1999-11-21 08:06:00 +00:00
|
|
|
free(mtabp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print bad lines to syslog.
|
|
|
|
*/
|
2001-07-22 12:17:51 +00:00
|
|
|
static void
|
2009-12-30 06:36:42 +00:00
|
|
|
badline(const char *field, const char *bad)
|
|
|
|
{
|
2001-07-22 12:17:51 +00:00
|
|
|
syslog(LOG_ERR, "bad mounttab %s field '%s'", field,
|
|
|
|
(bad == NULL) ? "<null>" : bad);
|
1999-11-21 08:06:00 +00:00
|
|
|
}
|