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/param.h>
34 #include <sys/ucred.h>
35 #include <sys/mount.h>
38 #include <nfs/rpcv2.h>
51 static int do_umount (char *, char *);
52 static int do_umntall (char *);
53 static int is_mounted (char *, char *);
54 static void usage (void);
55 int xdr_dir (XDR *, char *);
58 main(int argc, char **argv) {
59 int ch, keep, success, pathlen;
62 struct mtablist *mtab;
66 success = keep = verbose = 0;
67 while ((ch = getopt(argc, argv, "h:kp:ve:")) != -1)
73 expire = atoi(optarg);
92 /* Default expiretime is one day */
97 /* Read PATH_MOUNTTAB. */
100 warnx("no mounttab entries (%s does not exist)",
105 if (host == NULL && path == NULL) {
106 /* Check each entry and do any necessary unmount RPCs. */
107 for (mtab = mtabhead; mtab != NULL; mtab = mtab->mtab_next) {
108 if (*mtab->mtab_host == '\0')
110 if (mtab->mtab_time + expire < now) {
111 /* Clear expired entry. */
113 warnx("remove expired entry %s:%s",
114 mtab->mtab_host, mtab->mtab_dirp);
115 bzero(mtab->mtab_host,
116 sizeof(mtab->mtab_host));
119 if (keep && is_mounted(mtab->mtab_host,
122 warnx("skip entry %s:%s",
123 mtab->mtab_host, mtab->mtab_dirp);
126 if (do_umount(mtab->mtab_host, mtab->mtab_dirp)) {
128 warnx("umount RPC for %s:%s succeeded",
129 mtab->mtab_host, mtab->mtab_dirp);
130 /* Remove all entries for this host + path. */
131 clean_mtab(mtab->mtab_host, mtab->mtab_dirp,
137 if (host == NULL && path != NULL)
138 /* Missing hostname. */
141 /* Do a RPC UMNTALL for this specific host */
142 success = do_umntall(host);
143 if (verbose && success)
144 warnx("umntall RPC for %s succeeded", host);
146 /* Do a RPC UMNTALL for this specific mount */
147 for (pathlen = strlen(path);
148 pathlen > 1 && path[pathlen - 1] == '/'; pathlen--)
149 path[pathlen - 1] = '\0';
150 success = do_umount(host, path);
151 if (verbose && success)
152 warnx("umount RPC for %s:%s succeeded", host,
155 /* If successful, remove any corresponding mounttab entries. */
157 clean_mtab(host, path, verbose);
159 /* Write and unlink PATH_MOUNTTAB if necessary */
161 success = write_mtab(verbose);
163 exit (success ? 0 : 1);
167 * Send a RPC_MNT UMNTALL request to hostname.
168 * XXX This works for all mountd implementations,
169 * but produces a RPC IOERR on non FreeBSD systems.
172 do_umntall(char *hostname) {
173 enum clnt_stat clnt_stat;
179 clp = clnt_create_timed(hostname, RPCPROG_MNT, RPCMNT_VER1, "udp",
182 warnx("%s: %s", hostname, clnt_spcreateerror("RPCPROG_MNT"));
185 clp->cl_auth = authunix_create_default();
186 clnt_stat = clnt_call(clp, RPCMNT_UMNTALL,
187 (xdrproc_t)xdr_void, (caddr_t)0,
188 (xdrproc_t)xdr_void, (caddr_t)0, try);
189 if (clnt_stat != RPC_SUCCESS)
190 warnx("%s: %s", hostname, clnt_sperror(clp, "RPCMNT_UMNTALL"));
191 auth_destroy(clp->cl_auth);
193 return (clnt_stat == RPC_SUCCESS);
197 * Send a RPC_MNT UMOUNT request for dirp to hostname.
200 do_umount(char *hostname, char *dirp) {
201 enum clnt_stat clnt_stat;
207 clp = clnt_create_timed(hostname, RPCPROG_MNT, RPCMNT_VER1, "udp",
210 warnx("%s: %s", hostname, clnt_spcreateerror("RPCPROG_MNT"));
213 clp->cl_auth = authsys_create_default();
214 clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, (xdrproc_t)xdr_dir, dirp,
215 (xdrproc_t)xdr_void, (caddr_t)0, try);
216 if (clnt_stat != RPC_SUCCESS)
217 warnx("%s: %s", hostname, clnt_sperror(clp, "RPCMNT_UMOUNT"));
218 auth_destroy(clp->cl_auth);
220 return (clnt_stat == RPC_SUCCESS);
224 * Check if the entry is still/already mounted.
227 is_mounted(char *hostname, char *dirp) {
228 struct statfs *mntbuf;
229 char name[MNAMELEN + 1];
233 if (strlen(hostname) + strlen(dirp) >= MNAMELEN)
235 snprintf(name, sizeof(name), "%s:%s", hostname, dirp);
236 mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
239 bufsize = (mntsize + 1) * sizeof(struct statfs);
240 if ((mntbuf = malloc(bufsize)) == NULL)
242 mntsize = getfsstat(mntbuf, (long)bufsize, MNT_NOWAIT);
243 for (i = mntsize - 1; i >= 0; i--) {
244 if (strcmp(mntbuf[i].f_mntfromname, name) == 0) {
254 * xdr routines for mount rpc's
257 xdr_dir(XDR *xdrsp, char *dirp) {
258 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
263 (void)fprintf(stderr, "%s\n",
264 "usage: rpc.umntall [-kv] [-e expire] [-h host] [-p path]");