2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Martin Blapp
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char rcsid[] =
35 #include <sys/param.h>
36 #include <sys/ucred.h>
37 #include <sys/mount.h>
40 #include <rpcsvc/mount.h>
53 static int do_umount (char *, char *);
54 static int do_umntall (char *);
55 static int is_mounted (char *, char *);
56 static void usage (void);
57 int xdr_dir (XDR *, char *);
60 main(int argc, char **argv) {
61 int ch, keep, success, pathlen;
64 struct mtablist *mtab;
68 success = keep = verbose = 0;
69 while ((ch = getopt(argc, argv, "h:kp:ve:")) != -1)
75 expire = atoi(optarg);
94 /* Default expiretime is one day */
99 /* Read PATH_MOUNTTAB. */
102 warnx("no mounttab entries (%s does not exist)",
107 if (host == NULL && path == NULL) {
108 /* Check each entry and do any necessary unmount RPCs. */
109 for (mtab = mtabhead; mtab != NULL; mtab = mtab->mtab_next) {
110 if (*mtab->mtab_host == '\0')
112 if (mtab->mtab_time + expire < now) {
113 /* Clear expired entry. */
115 warnx("remove expired entry %s:%s",
116 mtab->mtab_host, mtab->mtab_dirp);
117 bzero(mtab->mtab_host,
118 sizeof(mtab->mtab_host));
121 if (keep && is_mounted(mtab->mtab_host,
124 warnx("skip entry %s:%s",
125 mtab->mtab_host, mtab->mtab_dirp);
128 if (do_umount(mtab->mtab_host, mtab->mtab_dirp)) {
130 warnx("umount RPC for %s:%s succeeded",
131 mtab->mtab_host, mtab->mtab_dirp);
132 /* Remove all entries for this host + path. */
133 clean_mtab(mtab->mtab_host, mtab->mtab_dirp,
139 if (host == NULL && path != NULL)
140 /* Missing hostname. */
143 /* Do a RPC UMNTALL for this specific host */
144 success = do_umntall(host);
145 if (verbose && success)
146 warnx("umntall RPC for %s succeeded", host);
148 /* Do a RPC UMNTALL for this specific mount */
149 for (pathlen = strlen(path);
150 pathlen > 1 && path[pathlen - 1] == '/'; pathlen--)
151 path[pathlen - 1] = '\0';
152 success = do_umount(host, path);
153 if (verbose && success)
154 warnx("umount RPC for %s:%s succeeded", host,
157 /* If successful, remove any corresponding mounttab entries. */
159 clean_mtab(host, path, verbose);
161 /* Write and unlink PATH_MOUNTTAB if necessary */
163 success = write_mtab(verbose);
165 exit (success ? 0 : 1);
169 * Send a RPC_MNT UMNTALL request to hostname.
170 * XXX This works for all mountd implementations,
171 * but produces a RPC IOERR on non FreeBSD systems.
174 do_umntall(char *hostname) {
175 enum clnt_stat clnt_stat;
181 clp = clnt_create_timed(hostname, MOUNTPROG, MOUNTVERS, "udp",
184 warnx("%s: %s", hostname, clnt_spcreateerror("MOUNTPROG"));
187 clp->cl_auth = authunix_create_default();
188 clnt_stat = clnt_call(clp, MOUNTPROC_UMNTALL,
189 (xdrproc_t)xdr_void, (caddr_t)0,
190 (xdrproc_t)xdr_void, (caddr_t)0, try);
191 if (clnt_stat != RPC_SUCCESS)
192 warnx("%s: %s", hostname, clnt_sperror(clp, "MOUNTPROC_UMNTALL"));
193 auth_destroy(clp->cl_auth);
195 return (clnt_stat == RPC_SUCCESS);
199 * Send a RPC_MNT UMOUNT request for dirp to hostname.
202 do_umount(char *hostname, char *dirp) {
203 enum clnt_stat clnt_stat;
209 clp = clnt_create_timed(hostname, MOUNTPROG, MOUNTVERS, "udp",
212 warnx("%s: %s", hostname, clnt_spcreateerror("MOUNTPROG"));
215 clp->cl_auth = authsys_create_default();
216 clnt_stat = clnt_call(clp, MOUNTPROC_UMNT, (xdrproc_t)xdr_dir, dirp,
217 (xdrproc_t)xdr_void, (caddr_t)0, try);
218 if (clnt_stat != RPC_SUCCESS)
219 warnx("%s: %s", hostname, clnt_sperror(clp, "MOUNTPROC_UMNT"));
220 auth_destroy(clp->cl_auth);
222 return (clnt_stat == RPC_SUCCESS);
226 * Check if the entry is still/already mounted.
229 is_mounted(char *hostname, char *dirp) {
230 struct statfs *mntbuf;
231 char name[MNAMELEN + 1];
235 if (strlen(hostname) + strlen(dirp) >= MNAMELEN)
237 snprintf(name, sizeof(name), "%s:%s", hostname, dirp);
238 mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
241 bufsize = (mntsize + 1) * sizeof(struct statfs);
242 if ((mntbuf = malloc(bufsize)) == NULL)
244 mntsize = getfsstat(mntbuf, (long)bufsize, MNT_NOWAIT);
245 for (i = mntsize - 1; i >= 0; i--) {
246 if (strcmp(mntbuf[i].f_mntfromname, name) == 0) {
256 * xdr routines for mount rpc's
259 xdr_dir(XDR *xdrsp, char *dirp) {
260 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
265 (void)fprintf(stderr, "%s\n",
266 "usage: rpc.umntall [-kv] [-e expire] [-h host] [-p path]");