xref: /dragonfly/sbin/umount/umount.c (revision 7485684f)
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. 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  * @(#)umount.c	8.8 (Berkeley) 5/8/95
30  * $FreeBSD: src/sbin/umount/umount.c,v 1.28 2001/10/13 02:04:54 iedowse Exp $
31  */
32 
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/mount.h>
36 #include <sys/socket.h>
37 #include <sys/vnioctl.h>
38 
39 #include <netdb.h>
40 #include <rpc/rpc.h>
41 #include <vfs/nfs/rpcv2.h>
42 
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <fstab.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 #include "mounttab.h"
53 
54 #define ISDOT(x)	((x)[0] == '.' && (x)[1] == '\0')
55 #define ISDOTDOT(x)	((x)[0] == '.' && (x)[1] == '.' && (x)[2] == '\0')
56 
57 typedef enum { MNTON, MNTFROM, NOTHING } mntwhat;
58 typedef enum { MARK, UNMARK, NAME, COUNT, FREE } dowhat;
59 
60 struct  addrinfo *nfshost_ai = NULL;
61 int	all, dflag, fflag, vflag;
62 char   *nfshost;
63 
64 void	 checkmntlist (char *, char **, char **, char **);
65 int	 checkvfsname (const char *, char **);
66 char	*getmntname (const char *, const char *, mntwhat, char **, dowhat);
67 char 	*getrealname(char *, char *resolved_path);
68 char   **makevfslist (const char *);
69 int	 mntinfo (struct statfs **);
70 int	 namematch (struct addrinfo *);
71 int	 sacmp (struct sockaddr *, struct sockaddr *);
72 int	 umountall (char **);
73 int	 checkname (char *, char **);
74 int	 umountfs (char *, char *, char *);
75 void	 usage (void) __dead2;
76 int	 vn_detach (const char *);
77 int	 xdr_dir (XDR *, char *);
78 
79 int
main(int argc,char * argv[])80 main(int argc, char *argv[])
81 {
82 	int errs, ch, mntsize, error;
83 	char **typelist = NULL, *mntonname, *mntfromname;
84 	char *type, *mntfromnamerev, *mntonnamerev;
85 	struct statfs *mntbuf;
86 	struct addrinfo hints;
87 
88 	all = errs = 0;
89 	while ((ch = getopt(argc, argv, "AadF:fh:t:v")) != -1) {
90 		switch (ch) {
91 		case 'A':
92 			all = 2;
93 			break;
94 		case 'a':
95 			all = 1;
96 			break;
97 		case 'd':
98 			dflag = 1;
99 			break;
100 		case 'F':
101 			setfstab(optarg);
102 			break;
103 		case 'f':
104 			fflag = MNT_FORCE;
105 			break;
106 		case 'h':	/* -h implies -A. */
107 			all = 2;
108 			nfshost = optarg;
109 			break;
110 		case 't':
111 			if (typelist != NULL)
112 				err(1, "only one -t option may be specified");
113 			typelist = makevfslist(optarg);
114 			break;
115 		case 'v':
116 			vflag = 1;
117 			break;
118 		default:
119 			usage();
120 			/* NOTREACHED */
121 		}
122 	}
123 	argc -= optind;
124 	argv += optind;
125 
126 	if ((argc == 0 && !all) || (argc != 0 && all))
127 		usage();
128 
129 	/* -h implies "-t nfs" if no -t flag. */
130 	if ((nfshost != NULL) && (typelist == NULL))
131 		typelist = makevfslist("nfs");
132 
133 	if (nfshost != NULL) {
134 		memset(&hints, 0, sizeof hints);
135 		error = getaddrinfo(nfshost, NULL, &hints, &nfshost_ai);
136 		if (error)
137 			errx(1, "%s: %s", nfshost, gai_strerror(error));
138 	}
139 
140 	switch (all) {
141 	case 2:
142 		if ((mntsize = mntinfo(&mntbuf)) <= 0)
143 			break;
144 		/*
145 		 * We unmount the nfs-mounts in the reverse order
146 		 * that they were mounted.
147 		 */
148 		for (errs = 0, mntsize--; mntsize > 0; mntsize--) {
149 			if (checkvfsname(mntbuf[mntsize].f_fstypename,
150 			    typelist))
151 				continue;
152 			/*
153 			 * Check if a mountpoint is laid over by another mount.
154 			 * A warning will be printed to stderr if this is
155 			 * the case. The laid over mount remains unmounted.
156 			 */
157 			mntonname = mntbuf[mntsize].f_mntonname;
158 			mntfromname = mntbuf[mntsize].f_mntfromname;
159 			mntonnamerev = getmntname(getmntname(mntonname,
160 			    NULL, MNTFROM, &type, NAME), NULL,
161 			    MNTON, &type, NAME);
162 
163 			mntfromnamerev = getmntname(mntonnamerev,
164 			    NULL, MNTFROM, &type, NAME);
165 
166 			if (strcmp(mntonnamerev, mntonname) == 0 &&
167 			    strcmp(mntfromnamerev, mntfromname ) != 0)
168 				warnx("cannot umount %s, %s\n        "
169 				    "is mounted there, umount it first",
170 				    mntonname, mntfromnamerev);
171 
172 			if (checkname(mntbuf[mntsize].f_mntonname,
173 			    typelist) != 0)
174 				errs = 1;
175 		}
176 		free(mntbuf);
177 		break;
178 	case 1:
179 		if (setfsent() == 0)
180 			err(1, "%s", getfstab());
181 		errs = umountall(typelist);
182 		break;
183 	case 0:
184 		for (errs = 0; *argv != NULL; ++argv)
185 			if (checkname(*argv, typelist) != 0)
186 				errs = 1;
187 		break;
188 	}
189 	getmntname(NULL, NULL, NOTHING, NULL, FREE);
190 	exit(errs);
191 }
192 
193 int
umountall(char ** typelist)194 umountall(char **typelist)
195 {
196 	struct vfsconf vfc;
197 	struct fstab *fs;
198 	int rval;
199 	char *cp;
200 	static int firstcall = 1;
201 
202 	if ((fs = getfsent()) != NULL)
203 		firstcall = 0;
204 	else if (firstcall)
205 		errx(1, "fstab reading failure");
206 	else
207 		return (0);
208 	do {
209 		/* Ignore the root. */
210 		if (strcmp(fs->fs_file, "/") == 0)
211 			continue;
212 		/*
213 		 * !!!
214 		 * Historic practice: ignore unknown FSTAB_* fields.
215 		 */
216 		if (strcmp(fs->fs_type, FSTAB_RW) &&
217 		    strcmp(fs->fs_type, FSTAB_RO) &&
218 		    strcmp(fs->fs_type, FSTAB_RQ))
219 			continue;
220 		/* Ignore unknown file system types. */
221 		if (getvfsbyname(fs->fs_vfstype, &vfc) == -1)
222 			continue;
223 		if (checkvfsname(fs->fs_vfstype, typelist))
224 			continue;
225 
226 		/*
227 		 * We want to unmount the file systems in the reverse order
228 		 * that they were mounted.  So, we save off the file name
229 		 * in some allocated memory, and then call recursively.
230 		 */
231 		if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL)
232 			err(1, "malloc failed");
233 		strcpy(cp, fs->fs_file);
234 		rval = umountall(typelist);
235 		rval = checkname(cp, typelist) || rval;
236 		free(cp);
237 		return (rval);
238 	} while ((fs = getfsent()) != NULL);
239 	return (0);
240 }
241 
242 /*
243  * Do magic checks on mountpoint and device or hand over
244  * it to unmount(2) if everything fails.
245  */
246 int
checkname(char * name,char ** typelist)247 checkname(char *name, char **typelist)
248 {
249 	size_t len;
250 	int speclen;
251 	char *mntonname, *mntfromname;
252 	char *mntfromnamerev;
253 	char *resolved, realname[MAXPATHLEN];
254 	char *type, *hostp, *delimp, *origname;
255 	char none[] = "none";
256 
257 	len = 0;
258 	mntfromname = mntonname = delimp = hostp = NULL;
259 
260 	/*
261 	 * 1. Check if the name exists in the mounttable.
262 	 */
263 	checkmntlist(name, &mntfromname, &mntonname, &type);
264 	if (mntfromname == NULL && mntonname == NULL) {
265 		checkmntlist(getdevpath(name, 0), &mntfromname,
266 			     &mntonname, &type);
267 	}
268 
269 	/*
270 	 * 2. Remove trailing slashes if there are any. After that
271 	 * we look up the name in the mounttable again.
272 	 */
273 	if (mntfromname == NULL && mntonname == NULL) {
274 		speclen = strlen(name);
275 		for (speclen = strlen(name);
276 		    speclen > 1 && name[speclen - 1] == '/';
277 		    speclen--)
278 			name[speclen - 1] = '\0';
279 		checkmntlist(name, &mntfromname, &mntonname, &type);
280 		resolved = name;
281 		/* Save off original name in origname */
282 		if ((origname = strdup(name)) == NULL)
283 			err(1, "strdup");
284 		/*
285 		 * 3. Check if the deprecated nfs-syntax with an '@'
286 		 * has been used and translate it to the ':' syntax.
287 		 * Look up the name in the mounttable again.
288 		 */
289 		if (mntfromname == NULL && mntonname == NULL) {
290 			if ((delimp = strrchr(name, '@')) != NULL) {
291 				hostp = delimp + 1;
292 				if (*hostp != '\0') {
293 					/*
294 					 * Make both '@' and ':'
295 					 * notations equal
296 					 */
297 					char *host = strdup(hostp);
298 					len = strlen(hostp);
299 					if (host == NULL)
300 						err(1, "strdup");
301 					memmove(name + len + 1, name,
302 					    (size_t)(delimp - name));
303 					name[len] = ':';
304 					memmove(name, host, len);
305 					free(host);
306 				}
307 				for (speclen = strlen(name);
308 				    speclen > 1 && name[speclen - 1] == '/';
309 				    speclen--)
310 					name[speclen - 1] = '\0';
311 				name[len + speclen + 1] = '\0';
312 				checkmntlist(name, &mntfromname,
313 				    &mntonname, &type);
314 				resolved = name;
315 			}
316 			/*
317 			 * 4. Check if a relative mountpoint has been
318 			 * specified. This should happen as last check,
319 			 * the order is important. To prevent possible
320 			 * nfs-hangs, we just call realpath(3) on the
321 			 * basedir of mountpoint and add the dirname again.
322 			 * Check the name in mounttable one last time.
323 			 */
324 			if (mntfromname == NULL && mntonname == NULL) {
325 				strcpy(name, origname);
326 				if ((getrealname(name, realname)) != NULL) {
327 					checkmntlist(realname,
328 					    &mntfromname, &mntonname, &type);
329 					resolved = realname;
330 				}
331 				/*
332 				 * 5. All tests failed, just hand over the
333 				 * mountpoint to the kernel, maybe the statfs
334 				 * structure has been truncated or is not
335 				 * useful anymore because of a chroot(2).
336 				 * Please note that nfs will not be able to
337 				 * notify the nfs-server about unmounting.
338 				 * These things can change in future when the
339 				 * fstat structure get's more reliable,
340 				 * but at the moment we cannot thrust it.
341 				 */
342 				if (mntfromname == NULL && mntonname == NULL) {
343 					strcpy(name, origname);
344 					if (umountfs(NULL, origname,
345 					    none) == 0) {
346 						warnx("%s not found in "
347 						    "mount table, "
348 						    "unmounted it anyway",
349 						    origname);
350 						free(origname);
351 						return (0);
352 					} else {
353 						free(origname);
354 						return (1);
355 					}
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 	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 		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
umountfs(char * mntfromname,char * mntonname,char * type)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 		printf("%s: unmount from %s\n", mntfromname, mntonname);
452 	if (dflag) {
453 		if (vn_detach(mntfromname) != 0 && !all) {
454 			warnx("detach of %s failed", mntfromname);
455 			return (1);
456 		}
457 	}
458 
459 	/*
460 	 * Report to mountd-server which nfsname
461 	 * has been unmounted.
462 	 */
463 	if (ai != NULL && !(fflag & MNT_FORCE) && do_rpc) {
464 		clp = clnt_create(hostp, RPCPROG_MNT, RPCMNT_VER1, "udp");
465 		if (clp  == NULL) {
466 			warnx("%s: %s", hostp,
467 			    clnt_spcreateerror("RPCPROG_MNT"));
468 			return (1);
469 		}
470 		clp->cl_auth = authsys_create_default();
471 		try.tv_sec = 20;
472 		try.tv_usec = 0;
473 		clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, (xdrproc_t)xdr_dir,
474 		    nfsdirname, (xdrproc_t)xdr_void, (caddr_t)0, try);
475 		if (clnt_stat != RPC_SUCCESS) {
476 			warnx("%s: %s", hostp,
477 			    clnt_sperror(clp, "RPCMNT_UMOUNT"));
478 			return (1);
479 		}
480 		/*
481 		 * Remove the unmounted entry from /var/db/mounttab.
482 		 */
483 		if (read_mtab()) {
484 			clean_mtab(hostp, nfsdirname, vflag);
485 			if(!write_mtab(vflag))
486 				warnx("cannot remove mounttab entry %s:%s",
487 				    hostp, nfsdirname);
488 			free_mtab();
489 		}
490 		free(orignfsdirname);
491 		auth_destroy(clp->cl_auth);
492 		clnt_destroy(clp);
493 	}
494 
495 	return (0);
496 }
497 
498 int
vn_detach(const char * fromname)499 vn_detach(const char *fromname)
500 {
501 	struct vn_ioctl vnio;
502 	char *p, device[MAXPATHLEN];
503 	int fd;
504 
505 	if (strncmp(fromname, "/dev/vn", sizeof("/dev/vn") - 1) != 0) {
506 		if (!all)
507 			warnx("invalid vn device: %s", fromname);
508 		return (-1);
509 	}
510 
511 	/* Strip the slice/partition part (e.g., /dev/vn4s1a -> /dev/vn4) */
512 	snprintf(device, sizeof(device), "%s", fromname);
513 	p = device + sizeof("/dev/vn");
514 	while (*p >= '0' && *p <= '9')
515 		p++;
516 	*p = '\0';
517 
518 	fd = open(device, O_RDONLY);
519 	if (fd < 0) {
520 		warn("open: %s", device);
521 		return (-1);
522 	}
523 
524 	memset(&vnio, 0, sizeof(vnio));
525 	if (ioctl(fd, VNIOCDETACH, &vnio) < 0) {
526 		warn("VNIOCDETACH: %s", device);
527 		close(fd);
528 		return (-1);
529 	}
530 
531 	close(fd);
532 	if (vflag)
533 		printf("%s: detached\n", device);
534 
535 	return (0);
536 }
537 
538 char *
getmntname(const char * fromname,const char * onname,mntwhat what,char ** type,dowhat mark)539 getmntname(const char *fromname, const char *onname,
540     mntwhat what, char **type, dowhat mark)
541 {
542 	static struct statfs *mntbuf;
543 	static int mntsize = 0;
544 	static char *mntcheck = NULL;
545 	static char *mntcount = NULL;
546 	int i, count;
547 
548 	if (mntsize <= 0) {
549 		if ((mntsize = mntinfo(&mntbuf)) <= 0)
550 			return (NULL);
551 	}
552 	if (mntcheck == NULL) {
553 		if ((mntcheck = calloc(mntsize + 1, sizeof(int))) == NULL ||
554 		    (mntcount = calloc(mntsize + 1, sizeof(int))) == NULL)
555 			err(1, "calloc");
556 	}
557 	/*
558 	 * We want to get the file systems in the reverse order
559 	 * that they were mounted. Mounted and unmounted filesystems
560 	 * are marked or unmarked in a table called 'mntcheck'.
561 	 * Unmount(const char *dir, int flags) does only take the
562 	 * mountpoint as argument, not the destination. If we don't pay
563 	 * attention to the order, it can happen that a overlaying
564 	 * filesystem get's unmounted instead of the one the user
565 	 * has choosen.
566 	 */
567 	switch (mark) {
568 	case NAME:
569 		/* Return only the specific name */
570 		for (i = mntsize - 1; i >= 0; i--) {
571 			if (fromname != NULL && what == MNTON &&
572 			    !strcmp(mntbuf[i].f_mntfromname, fromname) &&
573 			    mntcheck[i] != 1) {
574 				if (type)
575 					*type = mntbuf[i].f_fstypename;
576 				return (mntbuf[i].f_mntonname);
577 			}
578 			if (fromname != NULL && what == MNTFROM &&
579 			    !strcmp(mntbuf[i].f_mntonname, fromname) &&
580 			    mntcheck[i] != 1) {
581 				if (type)
582 					*type = mntbuf[i].f_fstypename;
583 				return (mntbuf[i].f_mntfromname);
584 			}
585 		}
586 		return (NULL);
587 	case MARK:
588 		/* Mark current mount with '1' and return name */
589 		for (i = mntsize - 1; i >= 0; i--) {
590 			if (mntcheck[i] == 0 &&
591 			    (strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
592 			    (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
593 				mntcheck[i] = 1;
594 				return (mntbuf[i].f_mntonname);
595 			}
596 		}
597 		return (NULL);
598 	case UNMARK:
599 		/* Unmark current mount with '0' and return name */
600 		for (i = 0; i < mntsize; i++) {
601 			if (mntcheck[i] == 1 &&
602 			    (strcmp(mntbuf[i].f_mntonname, onname) == 0) &&
603 			    (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)) {
604 				mntcheck[i] = 0;
605 				return (mntbuf[i].f_mntonname);
606 			}
607 		}
608 		return (NULL);
609 	case COUNT:
610 		/* Count the equal mntfromnames */
611 		count = 0;
612 		for (i = mntsize - 1; i >= 0; i--) {
613 			if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0)
614 				count++;
615 		}
616 		/* Mark the already unmounted mounts and return
617 		 * mntfromname if count <= 1. Else return NULL.
618 		 */
619 		for (i = mntsize - 1; i >= 0; i--) {
620 			if (strcmp(mntbuf[i].f_mntfromname, fromname) == 0) {
621 				if (mntcount[i] == 1)
622 					count--;
623 				else {
624 					mntcount[i] = 1;
625 					break;
626 				}
627 			}
628 		}
629 		if (count <= 1)
630 			return (mntbuf[i].f_mntonname);
631 		else
632 			return (NULL);
633 	case FREE:
634 		free(mntbuf);
635 		free(mntcheck);
636 		free(mntcount);
637 		return (NULL);
638 	default:
639 		return (NULL);
640 	}
641 }
642 
643 int
sacmp(struct sockaddr * sa1,struct sockaddr * sa2)644 sacmp(struct sockaddr *sa1, struct sockaddr *sa2)
645 {
646 	void *p1, *p2;
647 	int len;
648 
649 	if (sa1->sa_family != sa2->sa_family)
650 		return (1);
651 
652 	switch (sa1->sa_family) {
653 	case AF_INET:
654 		p1 = &((struct sockaddr_in *)sa1)->sin_addr;
655 		p2 = &((struct sockaddr_in *)sa2)->sin_addr;
656 		len = 4;
657 		break;
658 	case AF_INET6:
659 		p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
660 		p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
661 		len = 16;
662 		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
663 		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
664 			return (1);
665 		break;
666 	default:
667 		return (1);
668 	}
669 
670 	return memcmp(p1, p2, len);
671 }
672 
673 int
namematch(struct addrinfo * ai)674 namematch(struct addrinfo *ai)
675 {
676 	struct addrinfo *aip;
677 
678 	if (nfshost == NULL || nfshost_ai == NULL)
679 		return (1);
680 
681 	while (ai != NULL) {
682 		aip = nfshost_ai;
683 		while (aip != NULL) {
684 			if (sacmp(ai->ai_addr, aip->ai_addr) == 0)
685 				return (1);
686 			aip = aip->ai_next;
687 		}
688 		ai = ai->ai_next;
689 	}
690 
691 	return (0);
692 }
693 
694 void
checkmntlist(char * name,char ** fromname,char ** onname,char ** type)695 checkmntlist(char *name, char **fromname, char **onname, char **type)
696 {
697 
698 	*fromname = getmntname(name, NULL, MNTFROM, type, NAME);
699 	if (*fromname == NULL) {
700 		*onname = getmntname(name, NULL, MNTON, type, NAME);
701 		if (*onname != NULL)
702 			*fromname = name;
703 	} else
704 		*onname = name;
705 }
706 
707 int
mntinfo(struct statfs ** mntbuf)708 mntinfo(struct statfs **mntbuf)
709 {
710 	static struct statfs *origbuf;
711 	size_t bufsize;
712 	int mntsize;
713 
714 	mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
715 	if (mntsize <= 0)
716 		return (0);
717 	bufsize = (mntsize + 1) * sizeof(struct statfs);
718 	if ((origbuf = malloc(bufsize)) == NULL)
719 		err(1, "malloc");
720 	mntsize = getfsstat(origbuf, (long)bufsize, MNT_NOWAIT);
721 	*mntbuf = origbuf;
722 	return (mntsize);
723 }
724 
725 char *
getrealname(char * name,char * realname)726 getrealname(char *name, char *realname)
727 {
728 	char *dirname;
729 	int havedir;
730 	size_t baselen;
731 	size_t dirlen;
732 
733 	dirname = NULL;
734 	havedir = 0;
735 	if (*name == '/') {
736 		if (ISDOT(name + 1) || ISDOTDOT(name + 1))
737 			strcpy(realname, "/");
738 		else {
739 			if ((dirname = strrchr(name + 1, '/')) == NULL)
740 				snprintf(realname, MAXPATHLEN, "%s", name);
741 			else
742 				havedir = 1;
743 		}
744 	} else {
745 		if (ISDOT(name) || ISDOTDOT(name))
746 			realpath(name, realname);
747 		else {
748 			if ((dirname = strrchr(name, '/')) == NULL) {
749 				if ((realpath(name, realname)) == NULL)
750 					return (NULL);
751 			} else
752 				havedir = 1;
753 		}
754 	}
755 	if (havedir) {
756 		*dirname++ = '\0';
757 		if (ISDOT(dirname)) {
758 			*dirname = '\0';
759 			if ((realpath(name, realname)) == NULL)
760 				return (NULL);
761 		} else if (ISDOTDOT(dirname)) {
762 			*--dirname = '/';
763 			if ((realpath(name, realname)) == NULL)
764 				return (NULL);
765 		} else {
766 			if ((realpath(name, realname)) == NULL)
767 				return (NULL);
768 			baselen = strlen(realname);
769 			dirlen = strlen(dirname);
770 			if (baselen + dirlen + 1 > MAXPATHLEN)
771 				return (NULL);
772 			if (realname[1] == '\0') {
773 				memmove(realname + 1, dirname, dirlen);
774 				realname[dirlen + 1] = '\0';
775 			} else {
776 				realname[baselen] = '/';
777 				memmove(realname + baselen + 1,
778 				    dirname, dirlen);
779 				realname[baselen + dirlen + 1] = '\0';
780 			}
781 		}
782 	}
783 	return (realname);
784 }
785 
786 /*
787  * xdr routines for mount rpc's
788  */
789 int
xdr_dir(XDR * xdrsp,char * dirp)790 xdr_dir(XDR *xdrsp, char *dirp)
791 {
792 
793 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
794 }
795 
796 void
usage(void)797 usage(void)
798 {
799 
800 	fprintf(stderr, "%s\n%s\n",
801 	    "usage: umount [-dfv] special | node",
802 	    "       umount -a | -A [-F fstab] [-dfv] [-h host] [-t type]");
803 	exit(1);
804 }
805