xref: /freebsd/sbin/umount/umount.c (revision 5965373e69ca208cedd8ede9716ef0d58e6c3434)
1 /*-
2  * Copyright (c) 1980, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
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.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1989, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)umount.c	8.8 (Berkeley) 5/8/95";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47 
48 #include <sys/param.h>
49 #include <sys/mount.h>
50 #include <sys/socket.h>
51 
52 #include <netdb.h>
53 #include <rpc/rpc.h>
54 #include <nfs/rpcv2.h>
55 
56 #include <err.h>
57 #include <fstab.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 
63 #include "mounttab.h"
64 
65 #define ISDOT(x)	((x)[0] == '.' && (x)[1] == '\0')
66 #define ISDOTDOT(x)	((x)[0] == '.' && (x)[1] == '.' && (x)[2] == '\0')
67 
68 typedef enum { MNTON, MNTFROM, NOTHING } mntwhat;
69 typedef enum { MARK, UNMARK, NAME, COUNT, FREE } dowhat;
70 
71 struct  addrinfo *nfshost_ai = NULL;
72 int	fflag, vflag;
73 char   *nfshost;
74 
75 void	 checkmntlist (char *, char **, char **, char **);
76 int	 checkvfsname (const char *, char **);
77 char	*getmntname (const char *, const char *,
78 	 mntwhat, char **, dowhat);
79 char 	*getrealname(char *, char *resolved_path);
80 char   **makevfslist (const char *);
81 size_t	 mntinfo (struct statfs **);
82 int	 namematch (struct addrinfo *);
83 int	 sacmp (struct sockaddr *, struct sockaddr *);
84 int	 umountall (char **);
85 int	 checkname (char *, char **);
86 int	 umountfs (char *, char *, char *);
87 void	 usage (void);
88 int	 xdr_dir (XDR *, char *);
89 
90 int
91 main(int argc, char *argv[])
92 {
93 	int all, errs, ch, mntsize, error;
94 	char **typelist = NULL, *mntonname, *mntfromname;
95 	char *type, *mntfromnamerev, *mntonnamerev;
96 	struct statfs *mntbuf;
97 	struct addrinfo hints;
98 
99 	/* Start disks transferring immediately. */
100 	sync();
101 
102 	all = errs = 0;
103 	while ((ch = getopt(argc, argv, "Aafh:t:v")) != -1)
104 		switch (ch) {
105 		case 'A':
106 			all = 2;
107 			break;
108 		case 'a':
109 			all = 1;
110 			break;
111 		case 'f':
112 			fflag = MNT_FORCE;
113 			break;
114 		case 'h':	/* -h implies -A. */
115 			all = 2;
116 			nfshost = optarg;
117 			break;
118 		case 't':
119 			if (typelist != NULL)
120 				err(1, "only one -t option may be specified");
121 			typelist = makevfslist(optarg);
122 			break;
123 		case 'v':
124 			vflag = 1;
125 			break;
126 		default:
127 			usage();
128 			/* NOTREACHED */
129 		}
130 	argc -= optind;
131 	argv += optind;
132 
133 	if ((argc == 0 && !all) || (argc != 0 && all))
134 		usage();
135 
136 	/* -h implies "-t nfs" if no -t flag. */
137 	if ((nfshost != NULL) && (typelist == NULL))
138 		typelist = makevfslist("nfs");
139 
140 	if (nfshost != NULL) {
141 		memset(&hints, 0, sizeof hints);
142 		error = getaddrinfo(nfshost, NULL, &hints, &nfshost_ai);
143 		if (error)
144 			errx(1, "%s: %s", nfshost, gai_strerror(error));
145 	}
146 
147 	switch (all) {
148 	case 2:
149 		if ((mntsize = mntinfo(&mntbuf)) <= 0)
150 			break;
151 		/*
152 		 * We unmount the nfs-mounts in the reverse order
153 		 * that they were mounted.
154 		 */
155 		for (errs = 0, mntsize--; mntsize > 0; mntsize--) {
156 			if (checkvfsname(mntbuf[mntsize].f_fstypename,
157 			    typelist))
158 				continue;
159 			/*
160 			 * Check if a mountpoint is laid over by another mount.
161 			 * A warning will be printed to stderr if this is
162 			 * the case. The laid over mount remains unmounted.
163 			 */
164 			mntonname = mntbuf[mntsize].f_mntonname;
165 			mntfromname = mntbuf[mntsize].f_mntfromname;
166 			mntonnamerev = getmntname(getmntname(mntonname,
167 			    NULL, MNTFROM, &type, NAME), NULL,
168 			    MNTON, &type, NAME);
169 
170 			mntfromnamerev = getmntname(mntonnamerev,
171 			    NULL, MNTFROM, &type, NAME);
172 
173 			if (strcmp(mntonnamerev, mntonname) == 0 &&
174 			    strcmp(mntfromnamerev, mntfromname ) != 0)
175 				warnx("cannot umount %s, %s\n        "
176 				    "is mounted there, umount it first",
177 				    mntonname, mntfromnamerev);
178 
179 			if (checkname(mntbuf[mntsize].f_mntonname,
180 			    typelist) != 0)
181 				errs = 1;
182 		}
183 		free(mntbuf);
184 		break;
185 	case 1:
186 		if (setfsent() == 0)
187 			err(1, "%s", _PATH_FSTAB);
188 		errs = umountall(typelist);
189 		break;
190 	case 0:
191 		for (errs = 0; *argv != NULL; ++argv)
192 			if (checkname(*argv, typelist) != 0)
193 				errs = 1;
194 		break;
195 	}
196 	(void)getmntname(NULL, NULL, NOTHING, NULL, FREE);
197 	exit(errs);
198 }
199 
200 int
201 umountall(char **typelist)
202 {
203 	struct xvfsconf vfc;
204 	struct fstab *fs;
205 	int rval;
206 	char *cp;
207 	static int firstcall = 1;
208 
209 	if ((fs = getfsent()) != NULL)
210 		firstcall = 0;
211 	else if (firstcall)
212 		errx(1, "fstab reading failure");
213 	else
214 		return (0);
215 	do {
216 		/* Ignore the root. */
217 		if (strcmp(fs->fs_file, "/") == 0)
218 			continue;
219 		/*
220 		 * !!!
221 		 * Historic practice: ignore unknown FSTAB_* fields.
222 		 */
223 		if (strcmp(fs->fs_type, FSTAB_RW) &&
224 		    strcmp(fs->fs_type, FSTAB_RO) &&
225 		    strcmp(fs->fs_type, FSTAB_RQ))
226 			continue;
227 		/* Ignore unknown filesystem types. */
228 		if (getvfsbyname(fs->fs_vfstype, &vfc) == -1)
229 			continue;
230 		if (checkvfsname(fs->fs_vfstype, typelist))
231 			continue;
232 
233 		/*
234 		 * We want to unmount the filesystems in the reverse order
235 		 * that they were mounted.  So, we save off the file name
236 		 * in some allocated memory, and then call recursively.
237 		 */
238 		if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL)
239 			err(1, "malloc failed");
240 		(void)strcpy(cp, fs->fs_file);
241 		rval = umountall(typelist);
242 		rval = checkname(cp, typelist) || rval;
243 		free(cp);
244 		return (rval);
245 	} while ((fs = getfsent()) != NULL);
246 	return (0);
247 }
248 
249 /*
250  * Do magic checks on mountpoint and device or hand over
251  * it to unmount(2) if everything fails.
252  */
253 int
254 checkname(char *name, char **typelist)
255 {
256 	size_t len;
257 	int speclen;
258 	char *mntonname, *mntfromname;
259 	char *mntfromnamerev;
260 	char *resolved, realname[MAXPATHLEN];
261 	char *type, *hostp, *delimp, *origname;
262 
263 	len = 0;
264 	mntfromname = mntonname = delimp = hostp = NULL;
265 
266 	/*
267 	 * 1. Check if the name exists in the mounttable.
268 	 */
269 	(void)checkmntlist(name, &mntfromname, &mntonname, &type);
270 	/*
271 	 * 2. Remove trailing slashes if there are any. After that
272 	 * we look up the name in the mounttable again.
273 	 */
274 	if (mntfromname == NULL && mntonname == NULL) {
275 		speclen = strlen(name);
276 		for (speclen = strlen(name);
277 		    speclen > 1 && name[speclen - 1] == '/';
278 		    speclen--)
279 			name[speclen - 1] = '\0';
280 		(void)checkmntlist(name, &mntfromname, &mntonname, &type);
281 		resolved = name;
282 		/* Save off original name in origname */
283 		if ((origname = strdup(name)) == NULL)
284 			err(1, "strdup");
285 		/*
286 		 * 3. Check if the deprecated nfs-syntax with an '@'
287 		 * has been used and translate it to the ':' syntax.
288 		 * Look up the name in the mounttable again.
289 		 */
290 		if (mntfromname == NULL && mntonname == NULL) {
291 			if ((delimp = strrchr(name, '@')) != NULL) {
292 				hostp = delimp + 1;
293 				if (*hostp != '\0') {
294 					/*
295 					 * Make both '@' and ':'
296 					 * notations equal
297 					 */
298 					char *host = strdup(hostp);
299 					len = strlen(hostp);
300 					if (host == NULL)
301 						err(1, "strdup");
302 					memmove(name + len + 1, name,
303 					    (size_t)(delimp - name));
304 					name[len] = ':';
305 					memmove(name, host, len);
306 					free(host);
307 				}
308 				for (speclen = strlen(name);
309 				    speclen > 1 && name[speclen - 1] == '/';
310 				    speclen--)
311 					name[speclen - 1] = '\0';
312 				name[len + speclen + 1] = '\0';
313 				(void)checkmntlist(name, &mntfromname,
314 				    &mntonname, &type);
315 				resolved = name;
316 			}
317 			/*
318 			 * 4. Check if a relative mountpoint has been
319 			 * specified. This should happen as last check,
320 			 * the order is important. To prevent possible
321 			 * nfs-hangs, we just call realpath(3) on the
322 			 * basedir of mountpoint and add the dirname again.
323 			 * Check the name in mounttable one last time.
324 			 */
325 			if (mntfromname == NULL && mntonname == NULL) {
326 				(void)strcpy(name, origname);
327 				if ((getrealname(name, realname)) != NULL) {
328 					(void)checkmntlist(realname,
329 					    &mntfromname, &mntonname, &type);
330 					resolved = realname;
331 				}
332 				/*
333 				 * 5. All tests failed, just hand over the
334 				 * mountpoint to the kernel, maybe the statfs
335 				 * structure has been truncated or is not
336 				 * useful anymore because of a chroot(2).
337 				 * Please note that nfs will not be able to
338 				 * notify the nfs-server about unmounting.
339 				 * These things can change in future when the
340 				 * fstat structure get's more reliable,
341 				 * but at the moment we cannot thrust it.
342 				 */
343 				if (mntfromname == NULL && mntonname == NULL) {
344 					(void)strcpy(name, origname);
345 					if (umountfs(NULL, origname,
346 					    "none") == 0) {;
347 						warnx("%s not found in "
348 						    "mount table, "
349 						    "unmounted it anyway",
350 						    origname);
351 						free(origname);
352 						return (0);
353 					} else
354 						free(origname);
355 						return (1);
356 				}
357 			}
358 		}
359 		free(origname);
360 	} else
361 		resolved = name;
362 
363 	if (checkvfsname(type, typelist))
364 		return (1);
365 
366 	/*
367 	 * Check if the reverse entrys of the mounttable are really the
368 	 * same as the normal ones.
369 	 */
370 	if ((mntfromnamerev = strdup(getmntname(getmntname(mntfromname,
371 	    NULL, MNTON, &type, NAME), NULL, MNTFROM, &type, NAME))) == NULL)
372 		err(1, "strdup");
373 	/*
374 	 * Mark the uppermost mount as unmounted.
375 	 */
376 	(void)getmntname(mntfromname, mntonname, NOTHING, &type, MARK);
377 	/*
378 	 * If several equal mounts are in the mounttable, check the order
379 	 * and warn the user if necessary.
380 	 */
381 	if (strcmp(mntfromnamerev, mntfromname ) != 0 &&
382 	    strcmp(resolved, mntonname) != 0) {
383 		warnx("cannot umount %s, %s\n        "
384 		    "is mounted there, umount it first",
385 		    mntonname, mntfromnamerev);
386 
387 		/* call getmntname again to set mntcheck[i] to 0 */
388 		(void)getmntname(mntfromname, mntonname,
389 		    NOTHING, &type, UNMARK);
390 		return (1);
391 	}
392 	free(mntfromnamerev);
393 	return (umountfs(mntfromname, mntonname, type));
394 }
395 
396 /*
397  * NFS stuff and unmount(2) call
398  */
399 int
400 umountfs(char *mntfromname, char *mntonname, char *type)
401 {
402 	enum clnt_stat clnt_stat;
403 	struct timeval try;
404 	struct addrinfo *ai, hints;
405 	int do_rpc;
406 	CLIENT *clp;
407 	char *nfsdirname, *orignfsdirname;
408 	char *hostp, *delimp;
409 
410 	ai = NULL;
411 	do_rpc = 0;
412 	hostp = NULL;
413 	nfsdirname = delimp = orignfsdirname = NULL;
414 	memset(&hints, 0, sizeof hints);
415 
416 	if (strcmp(type, "nfs") == 0) {
417 		if ((nfsdirname = strdup(mntfromname)) == NULL)
418 			err(1, "strdup");
419 		orignfsdirname = nfsdirname;
420 		if ((delimp = strrchr(nfsdirname, ':')) != NULL) {
421 			*delimp = '\0';
422 			hostp = nfsdirname;
423 			getaddrinfo(hostp, NULL, &hints, &ai);
424 			if (ai == NULL) {
425 				warnx("can't get net id for host");
426 			}
427 			nfsdirname = delimp + 1;
428 		}
429 
430 		/*
431 		 * Check if we have to start the rpc-call later.
432 		 * If there are still identical nfs-names mounted,
433 		 * we skip the rpc-call. Obviously this has to
434 		 * happen before unmount(2), but it should happen
435 		 * after the previous namecheck.
436 		 * A non-NULL return means that this is the last
437 		 * mount from mntfromname that is still mounted.
438 		 */
439 		if (getmntname(mntfromname, NULL, NOTHING, &type, COUNT)
440 		     != NULL)
441 			do_rpc = 1;
442 	}
443 
444 	if (!namematch(ai))
445 		return (1);
446 	if (unmount(mntonname, fflag) != 0 ) {
447 		warn("unmount of %s failed", mntonname);
448 		return (1);
449 	}
450 	if (vflag)
451 		(void)printf("%s: unmount from %s\n", mntfromname, mntonname);
452 	/*
453 	 * Report to mountd-server which nfsname
454 	 * has been unmounted.
455 	 */
456 	if (ai != NULL && !(fflag & MNT_FORCE) && do_rpc) {
457 		clp = clnt_create(hostp, RPCPROG_MNT, RPCMNT_VER1, "udp");
458 		if (clp  == NULL) {
459 			warnx("%s: %s", hostp,
460 			    clnt_spcreateerror("RPCPROG_MNT"));
461 			return (1);
462 		}
463 		clp->cl_auth = authsys_create_default();
464 		try.tv_sec = 20;
465 		try.tv_usec = 0;
466 		clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir,
467 		    nfsdirname, xdr_void, (caddr_t)0, try);
468 		if (clnt_stat != RPC_SUCCESS) {
469 			warnx("%s: %s", hostp,
470 			    clnt_sperror(clp, "RPCMNT_UMOUNT"));
471 			return (1);
472 		}
473 		/*
474 		 * Remove the unmounted entry from /var/db/mounttab.
475 		 */
476 		if (read_mtab()) {
477 			clean_mtab(hostp, nfsdirname, vflag);
478 			if(!write_mtab(vflag))
479 				warnx("cannot remove mounttab entry %s:%s",
480 				    hostp, nfsdirname);
481 			free_mtab();
482 		}
483 		free(orignfsdirname);
484 		auth_destroy(clp->cl_auth);
485 		clnt_destroy(clp);
486 	}
487 	return (0);
488 }
489 
490 char *
491 getmntname(const char *fromname, const char *onname,
492     mntwhat what, char **type, dowhat mark)
493 {
494 	static struct statfs *mntbuf;
495 	static size_t mntsize = 0;
496 	static char *mntcheck = NULL;
497 	static char *mntcount = NULL;
498 	int i, count;
499 
500 	if (mntsize <= 0) {
501 		if ((mntsize = mntinfo(&mntbuf)) <= 0)
502 			return (NULL);
503 	}
504 	if (mntcheck == NULL) {
505 		if ((mntcheck = calloc(mntsize + 1, sizeof(int))) == NULL ||
506 		    (mntcount = calloc(mntsize + 1, sizeof(int))) == NULL)
507 			err(1, "calloc");
508 	}
509 	/*
510 	 * We want to get the filesystems in the reverse order
511 	 * that they were mounted. Mounted and unmounted filesystems
512 	 * are marked or unmarked in a table called 'mntcheck'.
513 	 * Unmount(const char *dir, int flags) does only take the
514 	 * mountpoint as argument, not the destination. If we don't pay
515 	 * attention to the order, it can happen that a overlaying
516 	 * filesystem get's unmounted instead of the one the user
517 	 * has choosen.
518 	 */
519 	switch (mark) {
520 	case NAME:
521 		/* Return only the specific name */
522 		for (i = mntsize - 1; i >= 0; i--) {
523 			if (fromname != NULL && what == MNTON &&
524 			    !strcmp(mntbuf[i].f_mntfromname, fromname) &&
525 			    mntcheck[i] != 1) {
526 				if (type)
527 					*type = mntbuf[i].f_fstypename;
528 				return (mntbuf[i].f_mntonname);
529 			}
530 			if (fromname != NULL && what == MNTFROM &&
531 			    !strcmp(mntbuf[i].f_mntonname, fromname) &&
532 			    mntcheck[i] != 1) {
533 				if (type)
534 					*type = mntbuf[i].f_fstypename;
535 				return (mntbuf[i].f_mntfromname);
536 			}
537 		}
538 		return (NULL);
539 	case MARK:
540 		/* Mark current mount with '1' and return name */
541 		for (i = mntsize - 1; i >= 0; i--) {
542 			if (mntcheck[i] == 0 &&
543 			    (strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
544 			    (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
545 				mntcheck[i] = 1;
546 				return (mntbuf[i].f_mntonname);
547 			}
548 		}
549 		return (NULL);
550 	case UNMARK:
551 		/* Unmark current mount with '0' and return name */
552 		for (i = 0; i < mntsize; i++) {
553 			if (mntcheck[i] == 1 &&
554 			    (strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
555 			    (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
556 				mntcheck[i] = 0;
557 				return (mntbuf[i].f_mntonname);
558 			}
559 		}
560 		return (NULL);
561 	case COUNT:
562 		/* Count the equal mntfromnames */
563 		count = 0;
564 		for (i = mntsize - 1; i >= 0; i--) {
565 			if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)
566 				count++;
567 		}
568 		/* Mark the already unmounted mounts and return
569 		 * mntfromname if count <= 1. Else return NULL.
570 		 */
571 		for (i = mntsize - 1; i >= 0; i--) {
572 			if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0) {
573 				if (mntcount[i] == 1)
574 					count--;
575 				else {
576 					mntcount[i] = 1;
577 					break;
578 				}
579 			}
580 		}
581 		if (count <= 1)
582 			return (mntbuf[i].f_mntonname);
583 		else
584 			return (NULL);
585 	case FREE:
586 		free(mntbuf);
587 		free(mntcheck);
588 		free(mntcount);
589 		return (NULL);
590 	default:
591 		return (NULL);
592 	}
593 }
594 
595 int
596 sacmp(struct sockaddr *sa1, struct sockaddr *sa2)
597 {
598 	void *p1, *p2;
599 	int len;
600 
601 	if (sa1->sa_family != sa2->sa_family)
602 		return (1);
603 
604 	switch (sa1->sa_family) {
605 	case AF_INET:
606 		p1 = &((struct sockaddr_in *)sa1)->sin_addr;
607 		p2 = &((struct sockaddr_in *)sa2)->sin_addr;
608 		len = 4;
609 		break;
610 	case AF_INET6:
611 		p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
612 		p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
613 		len = 16;
614 		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
615 		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
616 			return (1);
617 		break;
618 	default:
619 		return (1);
620 	}
621 
622 	return memcmp(p1, p2, len);
623 }
624 
625 int
626 namematch(struct addrinfo *ai)
627 {
628 	struct addrinfo *aip;
629 
630 	if (nfshost == NULL || nfshost_ai == NULL)
631 		return (1);
632 
633 	while (ai != NULL) {
634 		aip = nfshost_ai;
635 		while (aip != NULL) {
636 			if (sacmp(ai->ai_addr, aip->ai_addr) == 0)
637 				return (1);
638 			aip = aip->ai_next;
639 		}
640 		ai = ai->ai_next;
641 	}
642 
643 	return (0);
644 }
645 
646 void
647 checkmntlist(char *name, char **fromname, char **onname, char **type)
648 {
649 
650 	*fromname = getmntname(name, NULL, MNTFROM, type, NAME);
651 	if (*fromname == NULL) {
652 		*onname = getmntname(name, NULL, MNTON, type, NAME);
653 		if (*onname != NULL)
654 			*fromname = name;
655 	} else
656 		*onname = name;
657 }
658 
659 size_t
660 mntinfo(struct statfs **mntbuf)
661 {
662 	static struct statfs *origbuf;
663 	size_t bufsize;
664 	int mntsize;
665 
666 	mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
667 	if (mntsize <= 0)
668 		return (0);
669 	bufsize = (mntsize + 1) * sizeof(struct statfs);
670 	if ((origbuf = malloc(bufsize)) == NULL)
671 		err(1, "malloc");
672 	mntsize = getfsstat(origbuf, (long)bufsize, MNT_NOWAIT);
673 	*mntbuf = origbuf;
674 	return (mntsize);
675 }
676 
677 char *
678 getrealname(char *name, char *realname)
679 {
680 	char *dirname;
681 	int havedir;
682 	size_t baselen;
683 	size_t dirlen;
684 
685 	dirname = '\0';
686 	havedir = 0;
687 	if (*name == '/') {
688 		if (ISDOT(name + 1) || ISDOTDOT(name + 1))
689 			strcpy(realname, "/");
690 		else {
691 			if ((dirname = strrchr(name + 1, '/')) == NULL)
692 				snprintf(realname, MAXPATHLEN, "%s", name);
693 			else
694 				havedir = 1;
695 		}
696 	} else {
697 		if (ISDOT(name) || ISDOTDOT(name))
698 			(void)realpath(name, realname);
699 		else {
700 			if ((dirname = strrchr(name, '/')) == NULL) {
701 				if ((realpath(name, realname)) == NULL)
702 					return (NULL);
703 			} else
704 				havedir = 1;
705 		}
706 	}
707 	if (havedir) {
708 		*dirname++ = '\0';
709 		if (ISDOT(dirname)) {
710 			*dirname = '\0';
711 			if ((realpath(name, realname)) == NULL)
712 				return (NULL);
713 		} else if (ISDOTDOT(dirname)) {
714 			*--dirname = '/';
715 			if ((realpath(name, realname)) == NULL)
716 				return (NULL);
717 		} else {
718 			if ((realpath(name, realname)) == NULL)
719 				return (NULL);
720 			baselen = strlen(realname);
721 			dirlen = strlen(dirname);
722 			if (baselen + dirlen + 1 > MAXPATHLEN)
723 				return (NULL);
724 			if (realname[1] == '\0') {
725 				memmove(realname + 1, dirname, dirlen);
726 				realname[dirlen + 1] = '\0';
727 			} else {
728 				realname[baselen] = '/';
729 				memmove(realname + baselen + 1,
730 				    dirname, dirlen);
731 				realname[baselen + dirlen + 1] = '\0';
732 			}
733 		}
734 	}
735 	return (realname);
736 }
737 
738 /*
739  * xdr routines for mount rpc's
740  */
741 int
742 xdr_dir(XDR *xdrsp, char *dirp)
743 {
744 
745 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
746 }
747 
748 void
749 usage()
750 {
751 
752 	(void)fprintf(stderr, "%s\n%s\n",
753 	    "usage: umount [-fv] special | node",
754 	    "       umount -a | -A [-fv] [-h host] [-t type]");
755 	exit(1);
756 }
757