1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/*
* Copyright (c) 1999 Martin Blapp
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#ifndef lint
static const char rcsid[] =
"$FreeBSD$";
#endif /* not lint */
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#include <rpc/rpc.h>
#include <nfs/rpcv2.h>
#include <err.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mounttab.h"
int verbose;
static int do_umount (char *, char *);
static int do_umntall (char *);
static int is_mounted (char *, char *);
static void usage (void);
int xdr_dir (XDR *, char *);
int
main(int argc, char **argv) {
int ch, keep, success, pathlen;
time_t expire, now;
char *host, *path;
struct mtablist *mtab;
expire = 0;
host = path = NULL;
success = keep = verbose = 0;
while ((ch = getopt(argc, argv, "h:kp:ve:")) != -1)
switch (ch) {
case 'h':
host = optarg;
break;
case 'e':
expire = atoi(optarg);
break;
case 'k':
keep = 1;
break;
case 'p':
path = optarg;
break;
case 'v':
verbose = 1;
break;
case '?':
usage();
default:
}
argc -= optind;
argv += optind;
/* Ignore SIGINT and SIGQUIT during shutdown */
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
/* Default expiretime is one day */
if (expire == 0)
expire = 86400;
time(&now);
/* Read PATH_MOUNTTAB. */
if (!read_mtab()) {
if (verbose)
warnx("no mounttab entries (%s does not exist)",
PATH_MOUNTTAB);
mtabhead = NULL;
}
if (host == NULL && path == NULL) {
/* Check each entry and do any necessary unmount RPCs. */
for (mtab = mtabhead; mtab != NULL; mtab = mtab->mtab_next) {
if (*mtab->mtab_host == '\0')
continue;
if (mtab->mtab_time + expire < now) {
/* Clear expired entry. */
if (verbose)
warnx("remove expired entry %s:%s",
mtab->mtab_host, mtab->mtab_dirp);
bzero(mtab->mtab_host,
sizeof(mtab->mtab_host));
continue;
}
if (keep && is_mounted(mtab->mtab_host,
mtab->mtab_dirp)) {
if (verbose)
warnx("skip entry %s:%s",
mtab->mtab_host, mtab->mtab_dirp);
continue;
}
if (do_umount(mtab->mtab_host, mtab->mtab_dirp)) {
if (verbose)
warnx("umount RPC for %s:%s succeeded",
mtab->mtab_host, mtab->mtab_dirp);
/* Remove all entries for this host + path. */
clean_mtab(mtab->mtab_host, mtab->mtab_dirp,
verbose);
}
}
success = 1;
} else {
if (host == NULL && path != NULL)
/* Missing hostname. */
usage();
if (path == NULL) {
/* Do a RPC UMNTALL for this specific host */
success = do_umntall(host);
if (verbose && success)
warnx("umntall RPC for %s succeeded", host);
} else {
/* Do a RPC UMNTALL for this specific mount */
for (pathlen = strlen(path);
pathlen > 1 && path[pathlen - 1] == '/'; pathlen--)
path[pathlen - 1] = '\0';
success = do_umount(host, path);
if (verbose && success)
warnx("umount RPC for %s:%s succeeded", host,
path);
}
/* If successful, remove any corresponding mounttab entries. */
if (success)
clean_mtab(host, path, verbose);
}
/* Write and unlink PATH_MOUNTTAB if necessary */
if (success)
success = write_mtab(verbose);
free_mtab();
exit (success ? 0 : 1);
}
/*
* Send a RPC_MNT UMNTALL request to hostname.
* XXX This works for all mountd implementations,
* but produces a RPC IOERR on non FreeBSD systems.
*/
int
do_umntall(char *hostname) {
enum clnt_stat clnt_stat;
struct timeval try;
CLIENT *clp;
clp = clnt_create(hostname, RPCPROG_MNT, RPCMNT_VER1, "udp");
if (clp == NULL) {
warnx("%s: %s", hostname, clnt_spcreateerror("RPCPROG_MNT"));
return (0);
}
clp->cl_auth = authunix_create_default();
try.tv_sec = 3;
try.tv_usec = 0;
clnt_stat = clnt_call(clp, RPCMNT_UMNTALL, xdr_void, (caddr_t)0,
xdr_void, (caddr_t)0, try);
if (clnt_stat != RPC_SUCCESS)
warnx("%s: %s", hostname, clnt_sperror(clp, "RPCMNT_UMNTALL"));
auth_destroy(clp->cl_auth);
clnt_destroy(clp);
return (clnt_stat == RPC_SUCCESS);
}
/*
* Send a RPC_MNT UMOUNT request for dirp to hostname.
*/
int
do_umount(char *hostname, char *dirp) {
enum clnt_stat clnt_stat;
struct timeval try;
CLIENT *clp;
clp = clnt_create(hostname, RPCPROG_MNT, RPCMNT_VER1, "udp");
if (clp == NULL) {
warnx("%s: %s", hostname, clnt_spcreateerror("RPCPROG_MNT"));
return (0);
}
clp->cl_auth = authsys_create_default();
try.tv_sec = 3;
try.tv_usec = 0;
clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir, dirp,
xdr_void, (caddr_t)0, try);
if (clnt_stat != RPC_SUCCESS)
warnx("%s: %s", hostname, clnt_sperror(clp, "RPCMNT_UMOUNT"));
auth_destroy(clp->cl_auth);
clnt_destroy(clp);
return (clnt_stat == RPC_SUCCESS);
}
/*
* Check if the entry is still/already mounted.
*/
int
is_mounted(char *hostname, char *dirp) {
struct statfs *mntbuf;
char name[MNAMELEN + 1];
size_t bufsize;
int mntsize, i;
if (strlen(hostname) + strlen(dirp) >= MNAMELEN)
return (0);
snprintf(name, sizeof(name), "%s:%s", hostname, dirp);
mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
if (mntsize <= 0)
return (0);
bufsize = (mntsize + 1) * sizeof(struct statfs);
if ((mntbuf = malloc(bufsize)) == NULL)
err(1, "malloc");
mntsize = getfsstat(mntbuf, (long)bufsize, MNT_NOWAIT);
for (i = mntsize - 1; i >= 0; i--) {
if (strcmp(mntbuf[i].f_mntfromname, name) == 0) {
free(mntbuf);
return (1);
}
}
free(mntbuf);
return (0);
}
/*
* xdr routines for mount rpc's
*/
int
xdr_dir(XDR *xdrsp, char *dirp) {
return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
}
static void
usage() {
(void)fprintf(stderr, "%s\n",
"usage: rpc.umntall [-kv] [-e expire] [-h host] [-p path]");
exit(1);
}
|