xref: /freebsd-14-stable/sbin/umount/umount.c (revision 4de324578f4f7d08ef5b2d3dbebe4f253c9511d3)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1989, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n";
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)umount.c	8.8 (Berkeley) 5/8/95";
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/mount.h>
46 #include <sys/socket.h>
47 #include <sys/stat.h>
48 
49 #include <netdb.h>
50 #include <rpc/rpc.h>
51 #include <rpcsvc/mount.h>
52 #include <nfs/nfssvc.h>
53 
54 #include <ctype.h>
55 #include <err.h>
56 #include <errno.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 /* used by md_detach() */
66 #include <sys/ioctl.h>
67 #include <sys/mdioctl.h>
68 #include <fcntl.h>
69 #include <paths.h>
70 
71 #define DEV_MD     _PATH_DEV   MD_NAME
72 #define DEV_MDCTL   _PATH_DEV  MDCTL_NAME
73 
74 typedef enum { FIND, REMOVE, CHECKUNIQUE } dowhat;
75 
76 static struct addrinfo *nfshost_ai = NULL;
77 static int	dflag, fflag, vflag;
78 static int	all;
79 static char	*nfshost;
80 
81 struct statfs *checkmntlist(char *);
82 int	 checkvfsname (const char *, char **);
83 struct statfs *getmntentry(const char *fromname, const char *onname,
84 	     fsid_t *fsid, dowhat what);
85 char   **makevfslist (const char *);
86 size_t	 mntinfo (struct statfs **);
87 int	 namematch (struct addrinfo *);
88 int	 parsehexfsid(const char *hex, fsid_t *fsid);
89 int	 sacmp (void *, void *);
90 int	 umountall (char **);
91 int	 checkname (char *, char **);
92 int	 umountfs(struct statfs *sfs);
93 void	 usage (void);
94 int	 xdr_dir (XDR *, char *);
95 int	 md_detach(const char *);
96 
97 int
main(int argc,char * argv[])98 main(int argc, char *argv[])
99 {
100 	int errs, ch, mntsize, error, nfsforce, ret;
101 	char **typelist = NULL;
102 	struct statfs *mntbuf, *sfs;
103 	struct addrinfo hints;
104 
105 	nfsforce = all = errs = 0;
106 	while ((ch = getopt(argc, argv, "AadF:fh:Nnt:v")) != -1)
107 		switch (ch) {
108 		case 'A':
109 			all = 2;
110 			break;
111 		case 'a':
112 			all = 1;
113 			break;
114 		case 'd':
115 			dflag = 1;
116 			break;
117 		case 'F':
118 			setfstab(optarg);
119 			break;
120 		case 'f':
121 			fflag |= MNT_FORCE;
122 			break;
123 		case 'h':	/* -h implies -A. */
124 			all = 2;
125 			nfshost = optarg;
126 			break;
127 		case 'N':
128 			nfsforce = 1;
129 			break;
130 		case 'n':
131 			fflag |= MNT_NONBUSY;
132 			break;
133 		case 't':
134 			if (typelist != NULL)
135 				err(1, "only one -t option may be specified");
136 			typelist = makevfslist(optarg);
137 			break;
138 		case 'v':
139 			vflag = 1;
140 			break;
141 		default:
142 			usage();
143 			/* NOTREACHED */
144 		}
145 	argc -= optind;
146 	argv += optind;
147 
148 	if ((fflag & MNT_FORCE) != 0 && (fflag & MNT_NONBUSY) != 0)
149 		err(1, "-f and -n are mutually exclusive");
150 
151 	if ((argc == 0 && !all) || (argc != 0 && all))
152 		usage();
153 
154 	if (nfsforce != 0 && (argc == 0 || nfshost != NULL || typelist != NULL))
155 		usage();
156 
157 	/* -h implies "-t nfs" if no -t flag. */
158 	if ((nfshost != NULL) && (typelist == NULL))
159 		typelist = makevfslist("nfs");
160 
161 	if (nfshost != NULL) {
162 		memset(&hints, 0, sizeof hints);
163 		error = getaddrinfo(nfshost, NULL, &hints, &nfshost_ai);
164 		if (error)
165 			errx(1, "%s: %s", nfshost, gai_strerror(error));
166 	}
167 
168 	switch (all) {
169 	case 2:
170 		if ((mntsize = mntinfo(&mntbuf)) <= 0)
171 			break;
172 		/*
173 		 * We unmount the nfs-mounts in the reverse order
174 		 * that they were mounted.
175 		 */
176 		for (errs = 0, mntsize--; mntsize > 0; mntsize--) {
177 			sfs = &mntbuf[mntsize];
178 			if (checkvfsname(sfs->f_fstypename, typelist))
179 				continue;
180 			if (strcmp(sfs->f_mntonname, "/dev") == 0)
181 				continue;
182 			if (umountfs(sfs) != 0)
183 				errs = 1;
184 		}
185 		free(mntbuf);
186 		break;
187 	case 1:
188 		if (setfsent() == 0)
189 			err(1, "%s", getfstab());
190 		errs = umountall(typelist);
191 		break;
192 	case 0:
193 		for (errs = 0; *argv != NULL; ++argv)
194 			if (nfsforce != 0) {
195 				/*
196 				 * First do the nfssvc() syscall to shut down
197 				 * the mount point and then do the forced
198 				 * dismount.
199 				 */
200 				ret = nfssvc(NFSSVC_FORCEDISM, *argv);
201 				if (ret >= 0)
202 					ret = unmount(*argv, MNT_FORCE);
203 				if (ret < 0) {
204 					warn("%s", *argv);
205 					errs = 1;
206 				}
207 			} else if (checkname(*argv, typelist) != 0)
208 				errs = 1;
209 		break;
210 	}
211 	exit(errs);
212 }
213 
214 int
umountall(char ** typelist)215 umountall(char **typelist)
216 {
217 	struct xvfsconf vfc;
218 	struct fstab *fs;
219 	int rval;
220 	char *cp;
221 	static int firstcall = 1;
222 
223 	if ((fs = getfsent()) != NULL)
224 		firstcall = 0;
225 	else if (firstcall)
226 		errx(1, "fstab reading failure");
227 	else
228 		return (0);
229 	do {
230 		/* Ignore the root. */
231 		if (strcmp(fs->fs_file, "/") == 0)
232 			continue;
233 		/*
234 		 * !!!
235 		 * Historic practice: ignore unknown FSTAB_* fields.
236 		 */
237 		if (strcmp(fs->fs_type, FSTAB_RW) &&
238 		    strcmp(fs->fs_type, FSTAB_RO) &&
239 		    strcmp(fs->fs_type, FSTAB_RQ))
240 			continue;
241 		/* Ignore unknown file system types. */
242 		if (getvfsbyname(fs->fs_vfstype, &vfc) == -1)
243 			continue;
244 		if (checkvfsname(fs->fs_vfstype, typelist))
245 			continue;
246 
247 		/*
248 		 * We want to unmount the file systems in the reverse order
249 		 * that they were mounted.  So, we save off the file name
250 		 * in some allocated memory, and then call recursively.
251 		 */
252 		if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL)
253 			err(1, "malloc failed");
254 		(void)strcpy(cp, fs->fs_file);
255 		rval = umountall(typelist);
256 		rval = checkname(cp, typelist) || rval;
257 		free(cp);
258 		return (rval);
259 	} while ((fs = getfsent()) != NULL);
260 	return (0);
261 }
262 
263 /*
264  * Do magic checks on mountpoint/device/fsid, and then call unmount(2).
265  */
266 int
checkname(char * mntname,char ** typelist)267 checkname(char *mntname, char **typelist)
268 {
269 	char buf[MAXPATHLEN];
270 	struct statfs sfsbuf;
271 	struct stat sb;
272 	struct statfs *sfs;
273 	char *delimp;
274 	dev_t dev;
275 	int len;
276 
277 	/*
278 	 * 1. Check if the name exists in the mounttable.
279 	 */
280 	sfs = checkmntlist(mntname);
281 	/*
282 	 * 2. Remove trailing slashes if there are any. After that
283 	 * we look up the name in the mounttable again.
284 	 */
285 	if (sfs == NULL) {
286 		len = strlen(mntname);
287 		while (len > 1 && mntname[len - 1] == '/')
288 			mntname[--len] = '\0';
289 		sfs = checkmntlist(mntname);
290 	}
291 	/*
292 	 * 3. Check if the deprecated NFS syntax with an '@' has been used
293 	 * and translate it to the ':' syntax. Look up the name in the
294 	 * mount table again.
295 	 */
296 	if (sfs == NULL && (delimp = strrchr(mntname, '@')) != NULL) {
297 		snprintf(buf, sizeof(buf), "%s:%.*s", delimp + 1,
298 		    (int)(delimp - mntname), mntname);
299 		len = strlen(buf);
300 		while (len > 1 && buf[len - 1] == '/')
301 			buf[--len] = '\0';
302 		sfs = checkmntlist(buf);
303 	}
304 	/*
305 	 * 4. Resort to a statfs(2) call. This is the last check so that
306 	 * hung NFS filesystems for example can be unmounted without
307 	 * potentially blocking forever in statfs() as long as the
308 	 * filesystem is specified unambiguously. This covers all the
309 	 * hard cases such as symlinks and mismatches between the
310 	 * mount list and reality.
311 	 * We also do this if an ambiguous mount point was specified.
312 	 */
313 	if (sfs == NULL || (getmntentry(NULL, mntname, NULL, FIND) != NULL &&
314 	    getmntentry(NULL, mntname, NULL, CHECKUNIQUE) == NULL)) {
315 		if (statfs(mntname, &sfsbuf) != 0) {
316 			warn("%s: statfs", mntname);
317 		} else if (stat(mntname, &sb) != 0) {
318 			warn("%s: stat", mntname);
319 		} else if (S_ISDIR(sb.st_mode)) {
320 			/* Check that `mntname' is the root directory. */
321 			dev = sb.st_dev;
322 			snprintf(buf, sizeof(buf), "%s/..", mntname);
323 			if (stat(buf, &sb) != 0) {
324 				warn("%s: stat", buf);
325 			} else if (sb.st_dev == dev) {
326 				warnx("%s: not a file system root directory",
327 				    mntname);
328 				return (1);
329 			} else
330 				sfs = &sfsbuf;
331 		}
332 	}
333 	if (sfs == NULL) {
334 		warnx("%s: unknown file system", mntname);
335 		return (1);
336 	}
337 	if (checkvfsname(sfs->f_fstypename, typelist))
338 		return (1);
339 	return (umountfs(sfs));
340 }
341 
342 /*
343  * NFS stuff and unmount(2) call
344  */
345 int
umountfs(struct statfs * sfs)346 umountfs(struct statfs *sfs)
347 {
348 	char fsidbuf[64];
349 	enum clnt_stat clnt_stat;
350 	struct timeval try;
351 	struct addrinfo *ai, hints;
352 	int do_rpc;
353 	CLIENT *clp;
354 	char *nfsdirname, *orignfsdirname;
355 	char *hostp, *delimp;
356 	char buf[1024];
357 	struct nfscl_dumpmntopts dumpmntopts;
358 	const char *proto_ptr = NULL;
359 
360 	ai = NULL;
361 	do_rpc = 0;
362 	hostp = NULL;
363 	nfsdirname = delimp = orignfsdirname = NULL;
364 	memset(&hints, 0, sizeof hints);
365 
366 	if (strcmp(sfs->f_fstypename, "nfs") == 0) {
367 		if ((nfsdirname = strdup(sfs->f_mntfromname)) == NULL)
368 			err(1, "strdup");
369 		orignfsdirname = nfsdirname;
370 		if (*nfsdirname == '[' &&
371 		    (delimp = strchr(nfsdirname + 1, ']')) != NULL &&
372 		    *(delimp + 1) == ':') {
373 			hostp = nfsdirname + 1;
374 			nfsdirname = delimp + 2;
375 		} else if ((delimp = strrchr(nfsdirname, ':')) != NULL) {
376 			hostp = nfsdirname;
377 			nfsdirname = delimp + 1;
378 		}
379 		if (hostp != NULL) {
380 			*delimp = '\0';
381 			getaddrinfo(hostp, NULL, &hints, &ai);
382 			if (ai == NULL) {
383 				warnx("can't get net id for host");
384 			}
385 		}
386 
387 		/*
388 		 * Check if we have to start the rpc-call later.
389 		 * If there are still identical nfs-names mounted,
390 		 * we skip the rpc-call. Obviously this has to
391 		 * happen before unmount(2), but it should happen
392 		 * after the previous namecheck.
393 		 * A non-NULL return means that this is the last
394 		 * mount from mntfromname that is still mounted.
395 		 */
396 		if (getmntentry(sfs->f_mntfromname, NULL, NULL,
397 		    CHECKUNIQUE) != NULL) {
398 			do_rpc = 1;
399 			proto_ptr = "udp";
400 			/*
401 			 * Try and find out whether this NFS mount is NFSv4 and
402 			 * what protocol is being used. If this fails, the
403 			 * default is NFSv2,3 and use UDP for the Unmount RPC.
404 			 */
405 			dumpmntopts.ndmnt_fname = sfs->f_mntonname;
406 			dumpmntopts.ndmnt_buf = buf;
407 			dumpmntopts.ndmnt_blen = sizeof(buf);
408 			if (nfssvc(NFSSVC_DUMPMNTOPTS, &dumpmntopts) >= 0) {
409 				if (strstr(buf, "nfsv4,") != NULL)
410 					do_rpc = 0;
411 				else if (strstr(buf, ",tcp,") != NULL)
412 					proto_ptr = "tcp";
413 			}
414 		}
415 	}
416 
417 	if (!namematch(ai)) {
418 		free(orignfsdirname);
419 		return (1);
420 	}
421 	/* First try to unmount using the file system ID. */
422 	snprintf(fsidbuf, sizeof(fsidbuf), "FSID:%d:%d", sfs->f_fsid.val[0],
423 	    sfs->f_fsid.val[1]);
424 	if (unmount(fsidbuf, fflag | MNT_BYFSID) != 0) {
425 		/* XXX, non-root users get a zero fsid, so don't warn. */
426 		if (errno != ENOENT || sfs->f_fsid.val[0] != 0 ||
427 		    sfs->f_fsid.val[1] != 0)
428 			warn("unmount of %s failed", sfs->f_mntonname);
429 		if (errno != ENOENT) {
430 			free(orignfsdirname);
431 			return (1);
432 		}
433 		/* Compatibility for old kernels. */
434 		if (sfs->f_fsid.val[0] != 0 || sfs->f_fsid.val[1] != 0)
435 			warnx("retrying using path instead of file system ID");
436 		if (unmount(sfs->f_mntonname, fflag) != 0) {
437 			warn("unmount of %s failed", sfs->f_mntonname);
438 			free(orignfsdirname);
439 			return (1);
440 		}
441 	}
442 	/* Mark this file system as unmounted. */
443 	getmntentry(NULL, NULL, &sfs->f_fsid, REMOVE);
444 	if (vflag)
445 		(void)printf("%s: unmount from %s\n", sfs->f_mntfromname,
446 		    sfs->f_mntonname);
447 	/*
448 	 * Report to mountd-server which nfsname
449 	 * has been unmounted.
450 	 */
451 	if (ai != NULL && !(fflag & MNT_FORCE) && do_rpc) {
452 		clp = clnt_create(hostp, MOUNTPROG, MOUNTVERS3, proto_ptr);
453 		if (clp  == NULL) {
454 			warnx("%s: %s", hostp,
455 			    clnt_spcreateerror("MOUNTPROG"));
456 			free(orignfsdirname);
457 			return (1);
458 		}
459 		clp->cl_auth = authsys_create_default();
460 		try.tv_sec = 20;
461 		try.tv_usec = 0;
462 		clnt_stat = clnt_call(clp, MOUNTPROC_UMNT, (xdrproc_t)xdr_dir,
463 		    nfsdirname, (xdrproc_t)xdr_void, (caddr_t)0, try);
464 		if (clnt_stat != RPC_SUCCESS) {
465 			warnx("%s: %s", hostp,
466 			    clnt_sperror(clp, "RPCMNT_UMOUNT"));
467 			free(orignfsdirname);
468 			return (1);
469 		}
470 		/*
471 		 * Remove the unmounted entry from /var/db/mounttab.
472 		 */
473 		if (read_mtab()) {
474 			clean_mtab(hostp, nfsdirname, vflag);
475 			if(!write_mtab(vflag))
476 				warnx("cannot remove mounttab entry %s:%s",
477 				    hostp, nfsdirname);
478 			free_mtab();
479 		}
480 		auth_destroy(clp->cl_auth);
481 		clnt_destroy(clp);
482 	}
483 	free(orignfsdirname);
484 
485 	if (dflag) {
486 		if (md_detach(sfs->f_mntfromname) == 0) {
487 			if (vflag)
488 				(void)printf("%s: detached\n",
489 				    sfs->f_mntfromname);
490 		} else if (!all)
491 			return (-1);
492 	}
493 
494 	return (0);
495 }
496 
497 struct statfs *
getmntentry(const char * fromname,const char * onname,fsid_t * fsid,dowhat what)498 getmntentry(const char *fromname, const char *onname, fsid_t *fsid, dowhat what)
499 {
500 	static struct statfs *mntbuf;
501 	static size_t mntsize = 0;
502 	static int *mntcheck = NULL;
503 	struct statfs *sfs, *foundsfs;
504 	int i, count;
505 
506 	if (mntsize <= 0) {
507 		if ((mntsize = mntinfo(&mntbuf)) <= 0)
508 			return (NULL);
509 	}
510 	if (mntcheck == NULL) {
511 		if ((mntcheck = calloc(mntsize + 1, sizeof(int))) == NULL)
512 			err(1, "calloc");
513 	}
514 	/*
515 	 * We want to get the file systems in the reverse order
516 	 * that they were mounted. Unmounted file systems are marked
517 	 * in a table called 'mntcheck'.
518 	 */
519 	count = 0;
520 	foundsfs = NULL;
521 	for (i = mntsize - 1; i >= 0; i--) {
522 		if (mntcheck[i])
523 			continue;
524 		sfs = &mntbuf[i];
525 		if (fromname != NULL && strcmp(sfs->f_mntfromname,
526 		    fromname) != 0)
527 			continue;
528 		if (onname != NULL && strcmp(sfs->f_mntonname, onname) != 0)
529 			continue;
530 		if (fsid != NULL && fsidcmp(&sfs->f_fsid, fsid) != 0)
531 			continue;
532 
533 		switch (what) {
534 		case CHECKUNIQUE:
535 			foundsfs = sfs;
536 			count++;
537 			continue;
538 		case REMOVE:
539 			mntcheck[i] = 1;
540 			break;
541 		default:
542 			break;
543 		}
544 		return (sfs);
545 	}
546 
547 	if (what == CHECKUNIQUE && count == 1)
548 		return (foundsfs);
549 	return (NULL);
550 }
551 
552 int
sacmp(void * sa1,void * sa2)553 sacmp(void *sa1, void *sa2)
554 {
555 	void *p1, *p2;
556 	int len;
557 
558 	if (((struct sockaddr *)sa1)->sa_family !=
559 	    ((struct sockaddr *)sa2)->sa_family)
560 		return (1);
561 
562 	switch (((struct sockaddr *)sa1)->sa_family) {
563 	case AF_INET:
564 		p1 = &((struct sockaddr_in *)sa1)->sin_addr;
565 		p2 = &((struct sockaddr_in *)sa2)->sin_addr;
566 		len = 4;
567 		break;
568 	case AF_INET6:
569 		p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
570 		p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
571 		len = 16;
572 		if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
573 		    ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
574 			return (1);
575 		break;
576 	default:
577 		return (1);
578 	}
579 
580 	return memcmp(p1, p2, len);
581 }
582 
583 int
namematch(struct addrinfo * ai)584 namematch(struct addrinfo *ai)
585 {
586 	struct addrinfo *aip;
587 
588 	if (nfshost == NULL || nfshost_ai == NULL)
589 		return (1);
590 
591 	while (ai != NULL) {
592 		aip = nfshost_ai;
593 		while (aip != NULL) {
594 			if (sacmp(ai->ai_addr, aip->ai_addr) == 0)
595 				return (1);
596 			aip = aip->ai_next;
597 		}
598 		ai = ai->ai_next;
599 	}
600 
601 	return (0);
602 }
603 
604 struct statfs *
checkmntlist(char * mntname)605 checkmntlist(char *mntname)
606 {
607 	struct statfs *sfs;
608 	fsid_t fsid;
609 
610 	sfs = NULL;
611 	if (parsehexfsid(mntname, &fsid) == 0)
612 		sfs = getmntentry(NULL, NULL, &fsid, FIND);
613 	if (sfs == NULL)
614 		sfs = getmntentry(NULL, mntname, NULL, FIND);
615 	if (sfs == NULL)
616 		sfs = getmntentry(mntname, NULL, NULL, FIND);
617 	return (sfs);
618 }
619 
620 size_t
mntinfo(struct statfs ** mntbuf)621 mntinfo(struct statfs **mntbuf)
622 {
623 	static struct statfs *origbuf;
624 	size_t bufsize;
625 	int mntsize;
626 
627 	mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
628 	if (mntsize <= 0)
629 		return (0);
630 	bufsize = (mntsize + 1) * sizeof(struct statfs);
631 	if ((origbuf = malloc(bufsize)) == NULL)
632 		err(1, "malloc");
633 	mntsize = getfsstat(origbuf, (long)bufsize, MNT_NOWAIT);
634 	*mntbuf = origbuf;
635 	return (mntsize);
636 }
637 
638 /*
639  * Convert a hexadecimal filesystem ID to an fsid_t.
640  * Returns 0 on success.
641  */
642 int
parsehexfsid(const char * hex,fsid_t * fsid)643 parsehexfsid(const char *hex, fsid_t *fsid)
644 {
645 	char hexbuf[3];
646 	int i;
647 
648 	if (strlen(hex) != sizeof(*fsid) * 2)
649 		return (-1);
650 	hexbuf[2] = '\0';
651 	for (i = 0; i < (int)sizeof(*fsid); i++) {
652 		hexbuf[0] = hex[i * 2];
653 		hexbuf[1] = hex[i * 2 + 1];
654 		if (!isxdigit(hexbuf[0]) || !isxdigit(hexbuf[1]))
655 			return (-1);
656 		((u_char *)fsid)[i] = strtol(hexbuf, NULL, 16);
657 	}
658 	return (0);
659 }
660 
661 /*
662  * xdr routines for mount rpc's
663  */
664 int
xdr_dir(XDR * xdrsp,char * dirp)665 xdr_dir(XDR *xdrsp, char *dirp)
666 {
667 
668 	return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
669 }
670 
671 void
usage(void)672 usage(void)
673 {
674 
675 	(void)fprintf(stderr, "%s\n%s\n",
676 	    "usage: umount [-dfNnv] special ... | node ... | fsid ...",
677 	    "       umount -a | -A [-F fstab] [-dfnv] [-h host] [-t type]");
678 	exit(1);
679 }
680 
681 int
md_detach(const char * device)682 md_detach(const char *device)
683 {
684 	struct md_ioctl mdio;
685 	char *eptr;
686 	int fd;
687 
688 	if (strncmp(device, DEV_MD, sizeof(DEV_MD) - 1)) {
689 		if (!all)
690 			warnx("invalid md device: %s", device);
691 		return (-1);
692 	}
693 
694 	memset(&mdio, 0, sizeof(mdio));
695 	mdio.md_version = MDIOVERSION;
696 	mdio.md_options = fflag ? MD_FORCE : 0;
697 	mdio.md_unit = strtoul(device + sizeof(DEV_MD) - 1, &eptr, 0);
698 	if (mdio.md_unit == (unsigned)ULONG_MAX || eptr - device == sizeof(DEV_MD) - 1) {
699 		warnx("invalid md device: %s", device);
700 		return (-1);
701 	}
702 
703 	fd = open(DEV_MDCTL, O_RDWR, 0);
704 	if (fd < 0) {
705 		warn("%s", DEV_MDCTL);
706 		return (-1);
707 	}
708 
709 	if (ioctl(fd, MDIOCDETACH, &mdio) < 0) {
710 		warn("%s", DEV_MD);
711 		close(fd);
712 		return (-1);
713 	}
714 
715 	close(fd);
716 	return (0);
717 }
718