Deleted Added
full compact
mounttab.c (66814) mounttab.c (80146)
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

--- 13 unchanged lines hidden (view full) ---

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#ifndef lint
29static const char rcsid[] =
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

--- 13 unchanged lines hidden (view full) ---

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#ifndef lint
29static const char rcsid[] =
30 "$FreeBSD: head/usr.sbin/rpc.umntall/mounttab.c 66814 2000-10-08 09:24:45Z bde $";
30 "$FreeBSD: head/usr.sbin/rpc.umntall/mounttab.c 80146 2001-07-22 12:17:51Z iedowse $";
31#endif /* not lint */
32
33#include <sys/syslog.h>
34
35#include <rpc/rpc.h>
36#include <nfs/rpcv2.h>
37
38#include <err.h>
39#include <errno.h>
31#endif /* not lint */
32
33#include <sys/syslog.h>
34
35#include <rpc/rpc.h>
36#include <nfs/rpcv2.h>
37
38#include <err.h>
39#include <errno.h>
40#include <limits.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44
45#include "mounttab.h"
46
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <unistd.h>
45
46#include "mounttab.h"
47
47int verbose;
48struct mtablist *mtabhead;
49
48struct mtablist *mtabhead;
49
50static void badline(char *field, char *bad);
51
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) {
56 FILE *mtabfile;
52/*
53 * Add an entry to PATH_MOUNTTAB for each mounted NFS filesystem,
54 * so the client can notify the NFS server even after reboot.
55 */
56int
57add_mtab(char *hostp, char *dirp) {
58 FILE *mtabfile;
57 time_t *now;
58
59
59 now = NULL;
60 if ((mtabfile = fopen(PATH_MOUNTTAB, "a")) == NULL)
61 return (0);
62 else {
63 fprintf(mtabfile, "%ld\t%s\t%s\n",
60 if ((mtabfile = fopen(PATH_MOUNTTAB, "a")) == NULL)
61 return (0);
62 else {
63 fprintf(mtabfile, "%ld\t%s\t%s\n",
64 (long)time(now), hostp, dirp);
64 (long)time(NULL), hostp, dirp);
65 fclose(mtabfile);
66 return (1);
67 }
68}
69
70/*
71 * Read mounttab line for line and return struct mtablist.
72 */
73int
65 fclose(mtabfile);
66 return (1);
67 }
68}
69
70/*
71 * Read mounttab line for line and return struct mtablist.
72 */
73int
74read_mtab(struct mtablist *mtabp) {
75 struct mtablist **mtabpp;
74read_mtab() {
75 struct mtablist **mtabpp, *mtabp;
76 char *hostp, *dirp, *cp;
77 char str[STRSIZ];
76 char *hostp, *dirp, *cp;
77 char str[STRSIZ];
78 char *timep;
78 char *timep, *endp;
79 time_t time;
79 time_t time;
80 u_long ultmp;
80 FILE *mtabfile;
81
82 if ((mtabfile = fopen(PATH_MOUNTTAB, "r")) == NULL) {
83 if (errno == ENOENT)
84 return (0);
85 else {
86 syslog(LOG_ERR, "can't open %s", PATH_MOUNTTAB);
87 return (0);
88 }
89 }
90 time = 0;
91 mtabpp = &mtabhead;
92 while (fgets(str, STRSIZ, mtabfile) != NULL) {
93 cp = str;
94 errno = 0;
95 if (*cp == '#' || *cp == ' ' || *cp == '\n')
96 continue;
97 timep = strsep(&cp, " \t\n");
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 time = 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");
98 if (timep == NULL || *timep == ' ' || *timep == '\n') {
99 badline(timep);
99 if (timep == NULL || *timep == '\0') {
100 badline("time", timep);
100 continue;
101 }
102 hostp = strsep(&cp, " \t\n");
101 continue;
102 }
103 hostp = strsep(&cp, " \t\n");
103 if (hostp == NULL || *hostp == ' ' || *hostp == '\n') {
104 badline(hostp);
104 if (hostp == NULL || *hostp == '\0') {
105 badline("host", hostp);
105 continue;
106 }
107 dirp = strsep(&cp, " \t\n");
106 continue;
107 }
108 dirp = strsep(&cp, " \t\n");
108 if (dirp == NULL || *dirp == ' ' || *dirp == '\n') {
109 badline(dirp);
109 if (dirp == NULL || *dirp == '\0') {
110 badline("dir", dirp);
110 continue;
111 }
111 continue;
112 }
112 time = strtoul(timep, (char **)NULL, 10);
113 if (errno == ERANGE) {
114 badline(timep);
113 ultmp = strtoul(timep, &endp, 10);
114 if (ultmp == ULONG_MAX || *endp != '\0') {
115 badline("time", timep);
115 continue;
116 }
116 continue;
117 }
118 time = 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 = time;
123 memmove(mtabp->mtab_host, hostp, RPCMNT_NAMELEN);
124 mtabp->mtab_host[RPCMNT_NAMELEN - 1] = '\0';

--- 7 unchanged lines hidden (view full) ---

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
119 if ((mtabp = malloc(sizeof (struct mtablist))) == NULL) {
120 syslog(LOG_ERR, "malloc");
121 fclose(mtabfile);
122 return (0);
123 }
124 mtabp->mtab_time = time;
125 memmove(mtabp->mtab_host, hostp, RPCMNT_NAMELEN);
126 mtabp->mtab_host[RPCMNT_NAMELEN - 1] = '\0';

--- 7 unchanged lines hidden (view full) ---

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() {
141 struct mtablist *mtabp;
142write_mtab(int verbose) {
143 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) {
144 FILE *mtabfile;
145 int line;
146
147 if ((mtabfile = fopen(PATH_MOUNTTAB, "w")) == NULL) {
148 syslog(LOG_ERR, "can't write to %s", PATH_MOUNTTAB);
149 return (0);
150 }
151 line = 0;
152 for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
151 if (mtabp->mtab_host != NULL &&
152 strlen(mtabp->mtab_host) > 0) {
153 fprintf(mtabfile, "%ld\t%s\t%s\n",
154 (long)mtabp->mtab_time, mtabp->mtab_host,
155 mtabp->mtab_dirp);
156 if (verbose) {
157 warnx("write entry " "%s:%s",
158 mtabp->mtab_host, mtabp->mtab_dirp);
159 }
160 clean_mtab(mtabp->mtab_host, mtabp->mtab_dirp);
161 line++;
162 }
153 if (mtabp->mtab_host[0] == '\0')
154 continue;
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)
159 break;
160 if (mp != NULL)
161 continue;
162
163 fprintf(mtabfile, "%ld\t%s\t%s\n",
164 (long)mtabp->mtab_time, mtabp->mtab_host,
165 mtabp->mtab_dirp);
166 if (verbose)
167 warnx("write mounttab entry %s:%s",
168 mtabp->mtab_host, mtabp->mtab_dirp);
169 line++;
163 }
164 fclose(mtabfile);
165 if (line == 0) {
166 if (unlink(PATH_MOUNTTAB) == -1) {
167 syslog(LOG_ERR, "can't remove %s", PATH_MOUNTTAB);
168 return (0);
169 }
170 }
171 return (1);
172}
173
174/*
175 * Mark the entries as clean where RPC calls have been done successfully.
176 */
177void
170 }
171 fclose(mtabfile);
172 if (line == 0) {
173 if (unlink(PATH_MOUNTTAB) == -1) {
174 syslog(LOG_ERR, "can't remove %s", PATH_MOUNTTAB);
175 return (0);
176 }
177 }
178 return (1);
179}
180
181/*
182 * Mark the entries as clean where RPC calls have been done successfully.
183 */
184void
178clean_mtab(char *hostp, char *dirp) {
185clean_mtab(char *hostp, char *dirp, int verbose) {
179 struct mtablist *mtabp;
180 char *host;
181
186 struct mtablist *mtabp;
187 char *host;
188
189 /* Copy hostp in case it points to an entry that we are zeroing out. */
182 host = strdup(hostp);
183 for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
190 host = strdup(hostp);
191 for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
184 if (mtabp->mtab_host != NULL &&
185 strcmp(mtabp->mtab_host, host) == 0) {
186 if (dirp == NULL) {
187 if (verbose) {
188 warnx("delete entries "
189 "host %s", host);
190 }
191 bzero(mtabp->mtab_host, RPCMNT_NAMELEN);
192 } else {
193 if (strcmp(mtabp->mtab_dirp, dirp) == 0) {
194 if (verbose) {
195 warnx("delete entry "
196 "%s:%s", host, dirp);
197 }
198 bzero(mtabp->mtab_host, RPCMNT_NAMELEN);
199 }
200 }
201 }
192 if (strcmp(mtabp->mtab_host, hostp) != 0)
193 continue;
194 if (dirp != NULL && strcmp(mtabp->mtab_dirp, dirp) != 0)
195 continue;
196
197 if (verbose)
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);
202 }
203 free(host);
204}
205
206/*
207 * Free struct mtablist mtab.
208 */
209void
210free_mtab() {
211 struct mtablist *mtabp;
202 }
203 free(host);
204}
205
206/*
207 * Free struct mtablist mtab.
208 */
209void
210free_mtab() {
211 struct mtablist *mtabp;
212 struct mtablist *mtab_next;
213
212
214 for (mtabp = mtabhead; mtabp != NULL; mtabp = mtab_next) {
215 mtab_next = mtabp->mtab_next;
213 while ((mtabp = mtabhead) != NULL) {
214 mtabhead = mtabhead->mtab_next;
216 free(mtabp);
215 free(mtabp);
217 mtabp = mtab_next;
218 }
219}
220
221/*
222 * Print bad lines to syslog.
223 */
216 }
217}
218
219/*
220 * Print bad lines to syslog.
221 */
224void
225badline(char *bad) {
226
227 syslog(LOG_ERR, "skip bad line in mounttab with entry %s", bad);
222static void
223badline(char *field, char *bad) {
224 syslog(LOG_ERR, "bad mounttab %s field '%s'", field,
225 (bad == NULL) ? "<null>" : bad);
228}
226}