Deleted Added
full compact
mounttab.c (201135) mounttab.c (201252)
1/*
2 * Copyright (c) 1999 Martin Blapp
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 */
27
28#include <sys/cdefs.h>
1/*
2 * Copyright (c) 1999 Martin Blapp
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD: head/usr.sbin/rpc.umntall/mounttab.c 201135 2009-12-28 17:57:37Z delphij $");
29__FBSDID("$FreeBSD: head/usr.sbin/rpc.umntall/mounttab.c 201252 2009-12-30 06:36:42Z ed $");
30
31#include <sys/syslog.h>
32
33#include <rpc/rpc.h>
34#include <rpcsvc/mount.h>
35
36#include <err.h>
37#include <errno.h>
38#include <limits.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
43
44#include "mounttab.h"
45
46struct mtablist *mtabhead;
47
48static void badline(const char *field, const char *bad);
49
50/*
51 * Add an entry to PATH_MOUNTTAB for each mounted NFS filesystem,
52 * so the client can notify the NFS server even after reboot.
53 */
54int
30
31#include <sys/syslog.h>
32
33#include <rpc/rpc.h>
34#include <rpcsvc/mount.h>
35
36#include <err.h>
37#include <errno.h>
38#include <limits.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
43
44#include "mounttab.h"
45
46struct mtablist *mtabhead;
47
48static void badline(const char *field, const char *bad);
49
50/*
51 * Add an entry to PATH_MOUNTTAB for each mounted NFS filesystem,
52 * so the client can notify the NFS server even after reboot.
53 */
54int
55add_mtab(char *hostp, char *dirp) {
55add_mtab(char *hostp, char *dirp)
56{
56 FILE *mtabfile;
57
58 if ((mtabfile = fopen(PATH_MOUNTTAB, "a")) == NULL)
59 return (0);
60 else {
61 fprintf(mtabfile, "%ld\t%s\t%s\n",
62 (long)time(NULL), hostp, dirp);
63 fclose(mtabfile);
64 return (1);
65 }
66}
67
68/*
69 * Read mounttab line for line and return struct mtablist.
70 */
71int
57 FILE *mtabfile;
58
59 if ((mtabfile = fopen(PATH_MOUNTTAB, "a")) == NULL)
60 return (0);
61 else {
62 fprintf(mtabfile, "%ld\t%s\t%s\n",
63 (long)time(NULL), hostp, dirp);
64 fclose(mtabfile);
65 return (1);
66 }
67}
68
69/*
70 * Read mounttab line for line and return struct mtablist.
71 */
72int
72read_mtab(void) {
73read_mtab(void)
74{
73 struct mtablist **mtabpp, *mtabp;
74 char *hostp, *dirp, *cp;
75 char str[STRSIZ];
76 char *timep, *endp;
77 time_t actiontime;
78 u_long ultmp;
79 FILE *mtabfile;
80
81 if ((mtabfile = fopen(PATH_MOUNTTAB, "r")) == NULL) {
82 if (errno == ENOENT)
83 return (0);
84 else {
85 syslog(LOG_ERR, "can't open %s", PATH_MOUNTTAB);
86 return (0);
87 }
88 }
89 actiontime = 0;
90 mtabpp = &mtabhead;
91 while (fgets(str, STRSIZ, mtabfile) != NULL) {
92 cp = str;
93 errno = 0;
94 if (*cp == '#' || *cp == ' ' || *cp == '\n')
95 continue;
96 timep = strsep(&cp, " \t\n");
97 if (timep == NULL || *timep == '\0') {
98 badline("time", timep);
99 continue;
100 }
101 hostp = strsep(&cp, " \t\n");
102 if (hostp == NULL || *hostp == '\0') {
103 badline("host", hostp);
104 continue;
105 }
106 dirp = strsep(&cp, " \t\n");
107 if (dirp == NULL || *dirp == '\0') {
108 badline("dir", dirp);
109 continue;
110 }
111 ultmp = strtoul(timep, &endp, 10);
112 if (ultmp == ULONG_MAX || *endp != '\0') {
113 badline("time", timep);
114 continue;
115 }
116 actiontime = ultmp;
117 if ((mtabp = malloc(sizeof (struct mtablist))) == NULL) {
118 syslog(LOG_ERR, "malloc");
119 fclose(mtabfile);
120 return (0);
121 }
122 mtabp->mtab_time = actiontime;
123 memmove(mtabp->mtab_host, hostp, MNTNAMLEN);
124 mtabp->mtab_host[MNTNAMLEN - 1] = '\0';
125 memmove(mtabp->mtab_dirp, dirp, MNTPATHLEN);
126 mtabp->mtab_dirp[MNTPATHLEN - 1] = '\0';
127 mtabp->mtab_next = (struct mtablist *)NULL;
128 *mtabpp = mtabp;
129 mtabpp = &mtabp->mtab_next;
130 }
131 fclose(mtabfile);
132 return (1);
133}
134
135/*
136 * Rewrite PATH_MOUNTTAB from scratch and skip bad entries.
137 * Unlink PATH_MOUNTAB if no entry is left.
138 */
139int
75 struct mtablist **mtabpp, *mtabp;
76 char *hostp, *dirp, *cp;
77 char str[STRSIZ];
78 char *timep, *endp;
79 time_t actiontime;
80 u_long ultmp;
81 FILE *mtabfile;
82
83 if ((mtabfile = fopen(PATH_MOUNTTAB, "r")) == NULL) {
84 if (errno == ENOENT)
85 return (0);
86 else {
87 syslog(LOG_ERR, "can't open %s", PATH_MOUNTTAB);
88 return (0);
89 }
90 }
91 actiontime = 0;
92 mtabpp = &mtabhead;
93 while (fgets(str, STRSIZ, mtabfile) != NULL) {
94 cp = str;
95 errno = 0;
96 if (*cp == '#' || *cp == ' ' || *cp == '\n')
97 continue;
98 timep = strsep(&cp, " \t\n");
99 if (timep == NULL || *timep == '\0') {
100 badline("time", timep);
101 continue;
102 }
103 hostp = strsep(&cp, " \t\n");
104 if (hostp == NULL || *hostp == '\0') {
105 badline("host", hostp);
106 continue;
107 }
108 dirp = strsep(&cp, " \t\n");
109 if (dirp == NULL || *dirp == '\0') {
110 badline("dir", dirp);
111 continue;
112 }
113 ultmp = strtoul(timep, &endp, 10);
114 if (ultmp == ULONG_MAX || *endp != '\0') {
115 badline("time", timep);
116 continue;
117 }
118 actiontime = ultmp;
119 if ((mtabp = malloc(sizeof (struct mtablist))) == NULL) {
120 syslog(LOG_ERR, "malloc");
121 fclose(mtabfile);
122 return (0);
123 }
124 mtabp->mtab_time = actiontime;
125 memmove(mtabp->mtab_host, hostp, MNTNAMLEN);
126 mtabp->mtab_host[MNTNAMLEN - 1] = '\0';
127 memmove(mtabp->mtab_dirp, dirp, MNTPATHLEN);
128 mtabp->mtab_dirp[MNTPATHLEN - 1] = '\0';
129 mtabp->mtab_next = (struct mtablist *)NULL;
130 *mtabpp = mtabp;
131 mtabpp = &mtabp->mtab_next;
132 }
133 fclose(mtabfile);
134 return (1);
135}
136
137/*
138 * Rewrite PATH_MOUNTTAB from scratch and skip bad entries.
139 * Unlink PATH_MOUNTAB if no entry is left.
140 */
141int
140write_mtab(int verbose) {
142write_mtab(int verbose)
143{
141 struct mtablist *mtabp, *mp;
142 FILE *mtabfile;
143 int line;
144
145 if ((mtabfile = fopen(PATH_MOUNTTAB, "w")) == NULL) {
146 syslog(LOG_ERR, "can't write to %s", PATH_MOUNTTAB);
147 return (0);
148 }
149 line = 0;
150 for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
151 if (mtabp->mtab_host[0] == '\0')
152 continue;
153 /* Skip if a later (hence more recent) entry is identical. */
154 for (mp = mtabp->mtab_next; mp != NULL; mp = mp->mtab_next)
155 if (strcmp(mtabp->mtab_host, mp->mtab_host) == 0 &&
156 strcmp(mtabp->mtab_dirp, mp->mtab_dirp) == 0)
157 break;
158 if (mp != NULL)
159 continue;
160
161 fprintf(mtabfile, "%ld\t%s\t%s\n",
162 (long)mtabp->mtab_time, mtabp->mtab_host,
163 mtabp->mtab_dirp);
164 if (verbose)
165 warnx("write mounttab entry %s:%s",
166 mtabp->mtab_host, mtabp->mtab_dirp);
167 line++;
168 }
169 fclose(mtabfile);
170 if (line == 0) {
171 if (unlink(PATH_MOUNTTAB) == -1) {
172 syslog(LOG_ERR, "can't remove %s", PATH_MOUNTTAB);
173 return (0);
174 }
175 }
176 return (1);
177}
178
179/*
180 * Mark the entries as clean where RPC calls have been done successfully.
181 */
182void
144 struct mtablist *mtabp, *mp;
145 FILE *mtabfile;
146 int line;
147
148 if ((mtabfile = fopen(PATH_MOUNTTAB, "w")) == NULL) {
149 syslog(LOG_ERR, "can't write to %s", PATH_MOUNTTAB);
150 return (0);
151 }
152 line = 0;
153 for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
154 if (mtabp->mtab_host[0] == '\0')
155 continue;
156 /* Skip if a later (hence more recent) entry is identical. */
157 for (mp = mtabp->mtab_next; mp != NULL; mp = mp->mtab_next)
158 if (strcmp(mtabp->mtab_host, mp->mtab_host) == 0 &&
159 strcmp(mtabp->mtab_dirp, mp->mtab_dirp) == 0)
160 break;
161 if (mp != NULL)
162 continue;
163
164 fprintf(mtabfile, "%ld\t%s\t%s\n",
165 (long)mtabp->mtab_time, mtabp->mtab_host,
166 mtabp->mtab_dirp);
167 if (verbose)
168 warnx("write mounttab entry %s:%s",
169 mtabp->mtab_host, mtabp->mtab_dirp);
170 line++;
171 }
172 fclose(mtabfile);
173 if (line == 0) {
174 if (unlink(PATH_MOUNTTAB) == -1) {
175 syslog(LOG_ERR, "can't remove %s", PATH_MOUNTTAB);
176 return (0);
177 }
178 }
179 return (1);
180}
181
182/*
183 * Mark the entries as clean where RPC calls have been done successfully.
184 */
185void
183clean_mtab(char *hostp, char *dirp, int verbose) {
186clean_mtab(char *hostp, char *dirp, int verbose)
187{
184 struct mtablist *mtabp;
185 char *host;
186
187 /* Copy hostp in case it points to an entry that we are zeroing out. */
188 host = strdup(hostp);
189 for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
190 if (strcmp(mtabp->mtab_host, host) != 0)
191 continue;
192 if (dirp != NULL && strcmp(mtabp->mtab_dirp, dirp) != 0)
193 continue;
194
195 if (verbose)
196 warnx("delete mounttab entry%s %s:%s",
197 (dirp == NULL) ? " by host" : "",
198 mtabp->mtab_host, mtabp->mtab_dirp);
199 bzero(mtabp->mtab_host, MNTNAMLEN);
200 }
201 free(host);
202}
203
204/*
205 * Free struct mtablist mtab.
206 */
207void
188 struct mtablist *mtabp;
189 char *host;
190
191 /* Copy hostp in case it points to an entry that we are zeroing out. */
192 host = strdup(hostp);
193 for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
194 if (strcmp(mtabp->mtab_host, host) != 0)
195 continue;
196 if (dirp != NULL && strcmp(mtabp->mtab_dirp, dirp) != 0)
197 continue;
198
199 if (verbose)
200 warnx("delete mounttab entry%s %s:%s",
201 (dirp == NULL) ? " by host" : "",
202 mtabp->mtab_host, mtabp->mtab_dirp);
203 bzero(mtabp->mtab_host, MNTNAMLEN);
204 }
205 free(host);
206}
207
208/*
209 * Free struct mtablist mtab.
210 */
211void
208free_mtab() {
212free_mtab(void)
213{
209 struct mtablist *mtabp;
210
211 while ((mtabp = mtabhead) != NULL) {
212 mtabhead = mtabhead->mtab_next;
213 free(mtabp);
214 }
215}
216
217/*
218 * Print bad lines to syslog.
219 */
220static void
214 struct mtablist *mtabp;
215
216 while ((mtabp = mtabhead) != NULL) {
217 mtabhead = mtabhead->mtab_next;
218 free(mtabp);
219 }
220}
221
222/*
223 * Print bad lines to syslog.
224 */
225static void
221badline(const char *field, const char *bad) {
226badline(const char *field, const char *bad)
227{
222 syslog(LOG_ERR, "bad mounttab %s field '%s'", field,
223 (bad == NULL) ? "<null>" : bad);
224}
228 syslog(LOG_ERR, "bad mounttab %s field '%s'", field,
229 (bad == NULL) ? "<null>" : bad);
230}