2 * Copyright (c) 1999 Martin Blapp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 static const char rcsid[] =
30 "$FreeBSD: src/usr.sbin/rpc.umntall/mounttab.c,v 1.2.2.1 2001/12/13 01:27:20 iedowse Exp $";
33 #include <sys/syslog.h>
36 #include <nfs/rpcv2.h>
48 struct mtablist *mtabhead;
50 static void badline(char *field, char *bad);
53 * Add an entry to PATH_MOUNTTAB for each mounted NFS filesystem,
54 * so the client can notify the NFS server even after reboot.
57 add_mtab(char *hostp, char *dirp) {
60 if ((mtabfile = fopen(PATH_MOUNTTAB, "a")) == NULL)
63 fprintf(mtabfile, "%ld\t%s\t%s\n",
64 (long)time(NULL), hostp, dirp);
71 * Read mounttab line for line and return struct mtablist.
75 struct mtablist **mtabpp, *mtabp;
76 char *hostp, *dirp, *cp;
83 if ((mtabfile = fopen(PATH_MOUNTTAB, "r")) == NULL) {
87 syslog(LOG_ERR, "can't open %s", PATH_MOUNTTAB);
93 while (fgets(str, STRSIZ, mtabfile) != NULL) {
96 if (*cp == '#' || *cp == ' ' || *cp == '\n')
98 timep = strsep(&cp, " \t\n");
99 if (timep == NULL || *timep == '\0') {
100 badline("time", timep);
103 hostp = strsep(&cp, " \t\n");
104 if (hostp == NULL || *hostp == '\0') {
105 badline("host", hostp);
108 dirp = strsep(&cp, " \t\n");
109 if (dirp == NULL || *dirp == '\0') {
110 badline("dir", dirp);
113 ultmp = strtoul(timep, &endp, 10);
114 if (ultmp == ULONG_MAX || *endp != '\0') {
115 badline("time", timep);
119 if ((mtabp = malloc(sizeof (struct mtablist))) == NULL) {
120 syslog(LOG_ERR, "malloc");
124 mtabp->mtab_time = time;
125 memmove(mtabp->mtab_host, hostp, RPCMNT_NAMELEN);
126 mtabp->mtab_host[RPCMNT_NAMELEN - 1] = '\0';
127 memmove(mtabp->mtab_dirp, dirp, RPCMNT_PATHLEN);
128 mtabp->mtab_dirp[RPCMNT_PATHLEN - 1] = '\0';
129 mtabp->mtab_next = (struct mtablist *)NULL;
131 mtabpp = &mtabp->mtab_next;
138 * Rewrite PATH_MOUNTTAB from scratch and skip bad entries.
139 * Unlink PATH_MOUNTAB if no entry is left.
142 write_mtab(int verbose) {
143 struct mtablist *mtabp, *mp;
147 if ((mtabfile = fopen(PATH_MOUNTTAB, "w")) == NULL) {
148 syslog(LOG_ERR, "can't write to %s", PATH_MOUNTTAB);
152 for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
153 if (mtabp->mtab_host[0] == '\0')
155 /* Skip if a later (hence more recent) entry is identical. */
156 for (mp = mtabp->mtab_next; mp != NULL; mp = mp->mtab_next)
157 if (strcmp(mtabp->mtab_host, mp->mtab_host) == 0 &&
158 strcmp(mtabp->mtab_dirp, mp->mtab_dirp) == 0)
163 fprintf(mtabfile, "%ld\t%s\t%s\n",
164 (long)mtabp->mtab_time, mtabp->mtab_host,
167 warnx("write mounttab entry %s:%s",
168 mtabp->mtab_host, mtabp->mtab_dirp);
173 if (unlink(PATH_MOUNTTAB) == -1) {
174 syslog(LOG_ERR, "can't remove %s", PATH_MOUNTTAB);
182 * Mark the entries as clean where RPC calls have been done successfully.
185 clean_mtab(char *hostp, char *dirp, int verbose) {
186 struct mtablist *mtabp;
189 /* Copy hostp in case it points to an entry that we are zeroing out. */
190 host = strdup(hostp);
191 for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
192 if (strcmp(mtabp->mtab_host, host) != 0)
194 if (dirp != NULL && strcmp(mtabp->mtab_dirp, dirp) != 0)
198 warnx("delete mounttab entry%s %s:%s",
199 (dirp == NULL) ? " by host" : "",
200 mtabp->mtab_host, mtabp->mtab_dirp);
201 bzero(mtabp->mtab_host, RPCMNT_NAMELEN);
207 * Free struct mtablist mtab.
211 struct mtablist *mtabp;
213 while ((mtabp = mtabhead) != NULL) {
214 mtabhead = mtabhead->mtab_next;
220 * Print bad lines to syslog.
223 badline(char *field, char *bad) {
224 syslog(LOG_ERR, "bad mounttab %s field '%s'", field,
225 (bad == NULL) ? "<null>" : bad);