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