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[] =
33 #include <sys/syslog.h>
36 #include <nfs/rpcv2.h>
48 struct mtablist *mtabhead;
51 * Add an entry to PATH_MOUNTTAB for each mounted NFS filesystem,
52 * so the client can notify the NFS server even after reboot.
55 add_mtab(char *hostp, char *dirp) {
60 if ((mtabfile = fopen(PATH_MOUNTTAB, "a")) == NULL)
63 fprintf(mtabfile, "%ld\t%s\t%s\n", time(now), hostp, dirp);
70 * Read mounttab line for line and return struct mtablist.
73 read_mtab(struct mtablist *mtabp) {
74 struct mtablist **mtabpp;
75 char *hostp, *dirp, *cp;
81 if ((mtabfile = fopen(PATH_MOUNTTAB, "r")) == NULL) {
85 syslog(LOG_ERR, "can't open %s", PATH_MOUNTTAB);
91 while (fgets(str, STRSIZ, mtabfile) != NULL) {
94 if (*cp == '#' || *cp == ' ' || *cp == '\n')
96 timep = strsep(&cp, " \t\n");
97 if (timep == NULL || *timep == ' ' || *timep == '\n') {
101 hostp = strsep(&cp, " \t\n");
102 if (hostp == NULL || *hostp == ' ' || *hostp == '\n') {
106 dirp = strsep(&cp, " \t\n");
107 if (dirp == NULL || *dirp == ' ' || *dirp == '\n') {
111 time = strtoul(timep, (char **)NULL, 10);
112 if (errno == ERANGE) {
116 if ((mtabp = malloc(sizeof (struct mtablist))) == NULL) {
117 syslog(LOG_ERR, "malloc");
121 mtabp->mtab_time = time;
122 memmove(mtabp->mtab_host, hostp, RPCMNT_NAMELEN);
123 mtabp->mtab_host[RPCMNT_NAMELEN - 1] = '\0';
124 memmove(mtabp->mtab_dirp, dirp, RPCMNT_PATHLEN);
125 mtabp->mtab_dirp[RPCMNT_PATHLEN - 1] = '\0';
126 mtabp->mtab_next = (struct mtablist *)NULL;
128 mtabpp = &mtabp->mtab_next;
135 * Rewrite PATH_MOUNTTAB from scratch and skip bad entries.
136 * Unlink PATH_MOUNTAB if no entry is left.
140 struct mtablist *mtabp;
144 if ((mtabfile = fopen(PATH_MOUNTTAB, "w")) == NULL) {
145 syslog(LOG_ERR, "can't write to %s", PATH_MOUNTTAB);
149 for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
150 if (mtabp->mtab_host != NULL &&
151 strlen(mtabp->mtab_host) > 0) {
152 fprintf(mtabfile, "%ld\t%s\t%s\n", mtabp->mtab_time,
153 mtabp->mtab_host, mtabp->mtab_dirp);
155 warnx("write entry " "%s:%s",
156 mtabp->mtab_host, mtabp->mtab_dirp);
158 clean_mtab(mtabp->mtab_host, mtabp->mtab_dirp);
164 if (unlink(PATH_MOUNTTAB) == -1) {
165 syslog(LOG_ERR, "can't remove %s", PATH_MOUNTTAB);
173 * Mark the entries as clean where RPC calls have been done successfully.
176 clean_mtab(char *hostp, char *dirp) {
177 struct mtablist *mtabp;
180 host = strdup(hostp);
181 for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
182 if (mtabp->mtab_host != NULL &&
183 strcmp(mtabp->mtab_host, host) == 0) {
186 warnx("delete entries "
189 bzero(mtabp->mtab_host, RPCMNT_NAMELEN);
191 if (strcmp(mtabp->mtab_dirp, dirp) == 0) {
193 warnx("delete entry "
194 "%s:%s", host, dirp);
196 bzero(mtabp->mtab_host, RPCMNT_NAMELEN);
205 * Free struct mtablist mtab.
209 struct mtablist *mtabp;
210 struct mtablist *mtab_next;
212 for (mtabp = mtabhead; mtabp != NULL; mtabp = mtab_next) {
213 mtab_next = mtabp->mtab_next;
220 * Print bad lines to syslog.
225 syslog(LOG_ERR, "skip bad line in mounttab with entry %s", bad);