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
26 * $FreeBSD: src/usr.sbin/rpc.umntall/rpc.umntall.c,v 1.3.2.1 2001/12/13 01:27:20 iedowse Exp $
27 * $DragonFly: src/usr.sbin/rpc.umntall/rpc.umntall.c,v 1.2 2003/06/17 04:30:02 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/ucred.h>
32 #include <sys/mount.h>
35 #include <nfs/rpcv2.h>
48 static int do_umount (char *, char *);
49 static int do_umntall (char *);
50 static int is_mounted (char *, char *);
51 static void usage (void);
52 int xdr_dir (XDR *, char *);
55 main(int argc, char **argv) {
56 int ch, keep, success, pathlen;
59 struct mtablist *mtab;
63 success = keep = verbose = 0;
64 while ((ch = getopt(argc, argv, "h:kp:ve:")) != -1)
70 expire = atoi(optarg);
88 /* Default expiretime is one day */
93 /* Read PATH_MOUNTTAB. */
96 warnx("no mounttab entries (%s does not exist)",
101 if (host == NULL && path == NULL) {
102 /* Check each entry and do any necessary unmount RPCs. */
103 for (mtab = mtabhead; mtab != NULL; mtab = mtab->mtab_next) {
104 if (*mtab->mtab_host == '\0')
106 if (mtab->mtab_time + expire < now) {
107 /* Clear expired entry. */
109 warnx("remove expired entry %s:%s",
110 mtab->mtab_host, mtab->mtab_dirp);
111 bzero(mtab->mtab_host,
112 sizeof(mtab->mtab_host));
115 if (keep && is_mounted(mtab->mtab_host,
118 warnx("skip entry %s:%s",
119 mtab->mtab_host, mtab->mtab_dirp);
122 if (do_umount(mtab->mtab_host, mtab->mtab_dirp)) {
124 warnx("umount RPC for %s:%s succeeded",
125 mtab->mtab_host, mtab->mtab_dirp);
126 /* Remove all entries for this host + path. */
127 clean_mtab(mtab->mtab_host, mtab->mtab_dirp,
133 if (host == NULL && path != NULL)
134 /* Missing hostname. */
137 /* Do a RPC UMNTALL for this specific host */
138 success = do_umntall(host);
139 if (verbose && success)
140 warnx("umntall RPC for %s succeeded", host);
142 /* Do a RPC UMNTALL for this specific mount */
143 for (pathlen = strlen(path);
144 pathlen > 1 && path[pathlen - 1] == '/'; pathlen--)
145 path[pathlen - 1] = '\0';
146 success = do_umount(host, path);
147 if (verbose && success)
148 warnx("umount RPC for %s:%s succeeded", host,
151 /* If successful, remove any corresponding mounttab entries. */
153 clean_mtab(host, path, verbose);
155 /* Write and unlink PATH_MOUNTTAB if necessary */
157 success = write_mtab(verbose);
159 exit (success ? 0 : 1);
163 * Send a RPC_MNT UMNTALL request to hostname.
164 * XXX This works for all mountd implementations,
165 * but produces a RPC IOERR on non FreeBSD systems.
168 do_umntall(char *hostname) {
169 enum clnt_stat clnt_stat;
171 struct sockaddr_in saddr;
172 struct timeval pertry, try;
176 if ((hp = gethostbyname(hostname)) == NULL) {
177 warnx("gethostbyname(%s) failed", hostname);
180 memset(&saddr, 0, sizeof(saddr));
181 saddr.sin_family = AF_INET;
183 memmove(&saddr.sin_addr, hp->h_addr, MIN(hp->h_length,
184 sizeof(saddr.sin_addr)));
188 clp = clntudp_create(&saddr, RPCPROG_MNT, RPCMNT_VER1, pertry, &so);
190 warnx("%s: %s", hostname, clnt_spcreateerror("RPCPROG_MNT"));
193 clp->cl_auth = authunix_create_default();
196 clnt_stat = clnt_call(clp, RPCMNT_UMNTALL, xdr_void, (caddr_t)0,
197 xdr_void, (caddr_t)0, try);
198 if (clnt_stat != RPC_SUCCESS)
199 warnx("%s: %s", hostname, clnt_sperror(clp, "RPCMNT_UMNTALL"));
200 auth_destroy(clp->cl_auth);
202 return (clnt_stat == RPC_SUCCESS);
206 * Send a RPC_MNT UMOUNT request for dirp to hostname.
209 do_umount(char *hostname, char *dirp) {
210 enum clnt_stat clnt_stat;
212 struct sockaddr_in saddr;
213 struct timeval pertry, try;
217 if ((hp = gethostbyname(hostname)) == NULL) {
218 warnx("gethostbyname(%s) failed", hostname);
221 memset(&saddr, 0, sizeof(saddr));
222 saddr.sin_family = AF_INET;
224 memmove(&saddr.sin_addr, hp->h_addr, MIN(hp->h_length,
225 sizeof(saddr.sin_addr)));
229 clp = clntudp_create(&saddr, RPCPROG_MNT, RPCMNT_VER1, pertry, &so);
231 warnx("%s: %s", hostname, clnt_spcreateerror("RPCPROG_MNT"));
234 clp->cl_auth = authunix_create_default();
237 clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir, dirp,
238 xdr_void, (caddr_t)0, try);
239 if (clnt_stat != RPC_SUCCESS)
240 warnx("%s: %s", hostname, clnt_sperror(clp, "RPCMNT_UMOUNT"));
241 auth_destroy(clp->cl_auth);
243 return (clnt_stat == RPC_SUCCESS);
247 * Check if the entry is still/already mounted.
250 is_mounted(char *hostname, char *dirp) {
251 struct statfs *mntbuf;
252 char name[MNAMELEN + 1];
256 if (strlen(hostname) + strlen(dirp) >= MNAMELEN)
258 snprintf(name, sizeof(name), "%s:%s", hostname, dirp);
259 mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
262 bufsize = (mntsize + 1) * sizeof(struct statfs);
263 if ((mntbuf = malloc(bufsize)) == NULL)
265 mntsize = getfsstat(mntbuf, (long)bufsize, MNT_NOWAIT);
266 for (i = mntsize - 1; i >= 0; i--) {
267 if (strcmp(mntbuf[i].f_mntfromname, name) == 0) {
277 * xdr routines for mount rpc's
280 xdr_dir(XDR *xdrsp, char *dirp) {
281 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
286 (void)fprintf(stderr, "%s\n",
287 "usage: rpc.umntall [-kv] [-e expire] [-h host] [-p path]");