games - backgammon - use defined names instead of magic numbers.
[dragonfly.git] / usr.sbin / rpc.umntall / mounttab.c
blobf4321db6190a7dfdc33cbe0e8e01567c1a19abfc
1 /*
2 * Copyright (c) 1999 Martin Blapp
3 * All rights reserved.
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.
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.
26 * $FreeBSD: src/usr.sbin/rpc.umntall/mounttab.c,v 1.2.2.1 2001/12/13 01:27:20 iedowse Exp $
27 * $DragonFly: src/usr.sbin/rpc.umntall/mounttab.c,v 1.4 2008/11/12 21:44:59 swildner Exp $
30 #include <sys/syslog.h>
32 #include <rpc/rpc.h>
33 #include <nfs/rpcv2.h>
35 #include <err.h>
36 #include <errno.h>
37 #include <limits.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
43 #include "mounttab.h"
45 struct mtablist *mtabhead;
47 static void badline(const char *field, const char *bad);
50 * Add an entry to PATH_MOUNTTAB for each mounted NFS filesystem,
51 * so the client can notify the NFS server even after reboot.
53 int
54 add_mtab(char *hostp, char *dirp) {
55 FILE *mtabfile;
57 if ((mtabfile = fopen(PATH_MOUNTTAB, "a")) == NULL)
58 return (0);
59 else {
60 fprintf(mtabfile, "%ld\t%s\t%s\n",
61 (long)time(NULL), hostp, dirp);
62 fclose(mtabfile);
63 return (1);
68 * Read mounttab line for line and return struct mtablist.
70 int
71 read_mtab(void) {
72 struct mtablist **mtabpp, *mtabp;
73 char *hostp, *dirp, *cp;
74 char str[STRSIZ];
75 char *timep, *endp;
76 time_t mnt_time;
77 u_long ultmp;
78 FILE *mtabfile;
80 if ((mtabfile = fopen(PATH_MOUNTTAB, "r")) == NULL) {
81 if (errno == ENOENT)
82 return (0);
83 else {
84 syslog(LOG_ERR, "can't open %s", PATH_MOUNTTAB);
85 return (0);
88 mnt_time = 0;
89 mtabpp = &mtabhead;
90 while (fgets(str, STRSIZ, mtabfile) != NULL) {
91 cp = str;
92 errno = 0;
93 if (*cp == '#' || *cp == ' ' || *cp == '\n')
94 continue;
95 timep = strsep(&cp, " \t\n");
96 if (timep == NULL || *timep == '\0') {
97 badline("time", timep);
98 continue;
100 hostp = strsep(&cp, " \t\n");
101 if (hostp == NULL || *hostp == '\0') {
102 badline("host", hostp);
103 continue;
105 dirp = strsep(&cp, " \t\n");
106 if (dirp == NULL || *dirp == '\0') {
107 badline("dir", dirp);
108 continue;
110 ultmp = strtoul(timep, &endp, 10);
111 if (ultmp == ULONG_MAX || *endp != '\0') {
112 badline("time", timep);
113 continue;
115 mnt_time = ultmp;
116 if ((mtabp = malloc(sizeof (struct mtablist))) == NULL) {
117 syslog(LOG_ERR, "malloc");
118 fclose(mtabfile);
119 return (0);
121 mtabp->mtab_time = mnt_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 = NULL;
127 *mtabpp = mtabp;
128 mtabpp = &mtabp->mtab_next;
130 fclose(mtabfile);
131 return (1);
135 * Rewrite PATH_MOUNTTAB from scratch and skip bad entries.
136 * Unlink PATH_MOUNTAB if no entry is left.
139 write_mtab(int verbose) {
140 struct mtablist *mtabp, *mp;
141 FILE *mtabfile;
142 int line;
144 if ((mtabfile = fopen(PATH_MOUNTTAB, "w")) == NULL) {
145 syslog(LOG_ERR, "can't write to %s", PATH_MOUNTTAB);
146 return (0);
148 line = 0;
149 for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
150 if (mtabp->mtab_host[0] == '\0')
151 continue;
152 /* Skip if a later (hence more recent) entry is identical. */
153 for (mp = mtabp->mtab_next; mp != NULL; mp = mp->mtab_next)
154 if (strcmp(mtabp->mtab_host, mp->mtab_host) == 0 &&
155 strcmp(mtabp->mtab_dirp, mp->mtab_dirp) == 0)
156 break;
157 if (mp != NULL)
158 continue;
160 fprintf(mtabfile, "%ld\t%s\t%s\n",
161 (long)mtabp->mtab_time, mtabp->mtab_host,
162 mtabp->mtab_dirp);
163 if (verbose)
164 warnx("write mounttab entry %s:%s",
165 mtabp->mtab_host, mtabp->mtab_dirp);
166 line++;
168 fclose(mtabfile);
169 if (line == 0) {
170 if (unlink(PATH_MOUNTTAB) == -1) {
171 syslog(LOG_ERR, "can't remove %s", PATH_MOUNTTAB);
172 return (0);
175 return (1);
179 * Mark the entries as clean where RPC calls have been done successfully.
181 void
182 clean_mtab(char *hostp, char *dirp, int verbose) {
183 struct mtablist *mtabp;
184 char *host;
186 /* Copy hostp in case it points to an entry that we are zeroing out. */
187 host = strdup(hostp);
188 for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
189 if (strcmp(mtabp->mtab_host, host) != 0)
190 continue;
191 if (dirp != NULL && strcmp(mtabp->mtab_dirp, dirp) != 0)
192 continue;
194 if (verbose)
195 warnx("delete mounttab entry%s %s:%s",
196 (dirp == NULL) ? " by host" : "",
197 mtabp->mtab_host, mtabp->mtab_dirp);
198 bzero(mtabp->mtab_host, RPCMNT_NAMELEN);
200 free(host);
204 * Free struct mtablist mtab.
206 void
207 free_mtab(void) {
208 struct mtablist *mtabp;
210 while ((mtabp = mtabhead) != NULL) {
211 mtabhead = mtabhead->mtab_next;
212 free(mtabp);
217 * Print bad lines to syslog.
219 static void
220 badline(const char *field, const char *bad) {
221 syslog(LOG_ERR, "bad mounttab %s field '%s'", field,
222 (bad == NULL) ? "<null>" : bad);