xref: /netbsd-src/sbin/mount/mount.c (revision 73d56d5b0be8704e4f0a7e8221a2c7309572c9a1)
1 /*	$NetBSD: mount.c,v 1.103 2020/10/18 10:57:30 mlelstv Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1989, 1993, 1994
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 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1989, 1993, 1994\
35  The Regents of the University of California.  All rights reserved.");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
41 #else
42 __RCSID("$NetBSD: mount.c,v 1.103 2020/10/18 10:57:30 mlelstv Exp $");
43 #endif
44 #endif /* not lint */
45 
46 #include <sys/param.h>
47 #include <sys/mount.h>
48 #include <sys/wait.h>
49 
50 #include <fs/puffs/puffs_msgif.h>
51 
52 #include <err.h>
53 #include <errno.h>
54 #include <fstab.h>
55 #include <pwd.h>
56 #include <signal.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include <util.h>
62 
63 #define MOUNTNAMES
64 #include <fcntl.h>
65 #include <sys/disk.h>
66 #include <sys/disklabel.h>
67 #include <sys/ioctl.h>
68 
69 #include "pathnames.h"
70 #include "mountprog.h"
71 
72 static int	debug, verbose;
73 
74 static void	catopt(char **, const char *);
75 static const char *
76 		getfslab(const char *str);
77 static struct statvfs *
78 		getmntpt(const char *);
79 static int 	getmntargs(struct statvfs *, char *, size_t);
80 static int	hasopt(const char *, const char *);
81 static void	mangle(char *, int *, const char ** volatile *, int *);
82 static int	mountfs(const char *, const char *, const char *,
83 		    int, const char *, const char *, int, char *, size_t);
84 static void	prmount(struct statvfs *);
85 __dead static void	usage(void);
86 
87 
88 /* Map from mount options to printable formats. */
89 static const struct opt {
90 	int o_opt;
91 	int o_silent;
92 	const char *o_name;
93 } optnames[] = {
94 	__MNT_FLAGS
95 };
96 
97 #define FLG_UPDATE	1
98 #define FLG_RDONLY	2
99 #define FLG_RDWRITE	4
100 #define FLG_FORCE	8
101 
102 static const char ffs_fstype[] = "ffs";
103 
104 int
105 main(int argc, char *argv[])
106 {
107 	const char *mntfromname, *mntonname, **vfslist, *vfstype;
108 	struct fstab *fs;
109 	struct statvfs *mntbuf;
110 #if 0
111 	FILE *mountdfp;
112 #endif
113 	int all, ch, forceall, i, init_flags, mntsize, rval;
114 	char *options;
115 	const char *mountopts, *fstypename;
116 	char canonical_path_buf[MAXPATHLEN], buf[MAXPATHLEN];
117 	char *canonical_path;
118 
119 	/* started as "mount" */
120 	all = forceall = init_flags = 0;
121 	options = NULL;
122 	vfslist = NULL;
123 	vfstype = ffs_fstype;
124 	while ((ch = getopt(argc, argv, "Aadfo:rwt:uv")) != -1)
125 		switch (ch) {
126 		case 'A':
127 			all = forceall = 1;
128 			break;
129 		case 'a':
130 			all = 1;
131 			break;
132 		case 'd':
133 			debug = 1;
134 			break;
135 		case 'f':
136 			init_flags |= FLG_FORCE;
137 			break;
138 		case 'o':
139 			if (*optarg)
140 				catopt(&options, optarg);
141 			break;
142 		case 'r':
143 			init_flags |= FLG_RDONLY;
144 			break;
145 		case 't':
146 			if (vfslist != NULL)
147 				errx(EXIT_FAILURE,
148 				    "Only one -t option may be specified.");
149 			vfslist = makevfslist(optarg);
150 			vfstype = optarg;
151 			break;
152 		case 'u':
153 			init_flags |= FLG_UPDATE;
154 			break;
155 		case 'v':
156 			verbose++;
157 			break;
158 		case 'w':
159 			init_flags |= FLG_RDWRITE;
160 			break;
161 		case '?':
162 		default:
163 			usage();
164 			/* NOTREACHED */
165 		}
166 	argc -= optind;
167 	argv += optind;
168 
169 #define	BADTYPE(type)							\
170 	(strcmp(type, FSTAB_RO) &&					\
171 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
172 
173 	rval = 0;
174 	switch (argc) {
175 	case 0:
176 		if (all)
177 			while ((fs = getfsent()) != NULL) {
178 				if (BADTYPE(fs->fs_type))
179 					continue;
180 				if (checkvfsname(fs->fs_vfstype, vfslist))
181 					continue;
182 				if (hasopt(fs->fs_mntops, "noauto"))
183 					continue;
184 				if (strcmp(fs->fs_spec, "from_mount") == 0) {
185 					if ((mntbuf = getmntpt(fs->fs_file))
186 					    == NULL)
187 						errx(EXIT_FAILURE,
188 						    "Unknown file system %s",
189 						    fs->fs_file);
190 					mntfromname = mntbuf->f_mntfromname;
191 				} else
192 					mntfromname = fs->fs_spec;
193 				mntfromname = getfsspecname(buf, sizeof(buf),
194 				    mntfromname);
195 				if (mntfromname == NULL)
196 					err(EXIT_FAILURE, "%s", buf);
197 				if (mountfs(fs->fs_vfstype, mntfromname,
198 				    fs->fs_file, init_flags, options,
199 				    fs->fs_mntops, !forceall, NULL, 0))
200 					rval = 1;
201 			}
202 		else {
203 			if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
204 				err(EXIT_FAILURE, "getmntinfo");
205 			for (i = 0; i < mntsize; i++) {
206 				if (checkvfsname(mntbuf[i].f_fstypename,
207 				    vfslist))
208 					continue;
209 				prmount(&mntbuf[i]);
210 			}
211 		}
212 		return rval;
213 	case 1:
214 		if (vfslist != NULL) {
215 			usage();
216 			/* NOTREACHED */
217 		}
218 
219 		/*
220 		 * Create a canonical version of the device or mount path
221 		 * passed to us.  It's ok for this to fail.  It's also ok
222 		 * for the result to be exactly the same as the original.
223 		 */
224 		canonical_path = realpath(*argv, canonical_path_buf);
225 
226 		if (init_flags & FLG_UPDATE) {
227 			/*
228 			 * Try looking up the canonical path first,
229 			 * then try exactly what the user entered.
230 			 */
231 			if ((canonical_path == NULL ||
232 			    (mntbuf = getmntpt(canonical_path)) == NULL) &&
233 			    (mntbuf = getmntpt(*argv)) == NULL) {
234 out:
235 				errx(EXIT_FAILURE,
236 				    "Unknown special file or file system `%s'",
237 				    *argv);
238 			}
239 			mntfromname = mntbuf->f_mntfromname;
240 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
241 				if (strcmp(fs->fs_spec, "from_mount") != 0)
242 					mntfromname = fs->fs_spec;
243 				/* ignore the fstab file options.  */
244 				fs->fs_mntops = NULL;
245 			}
246 			mntonname  = mntbuf->f_mntonname;
247 			fstypename = mntbuf->f_fstypename;
248 			mountopts  = NULL;
249 		} else {
250 			/*
251 			 * Try looking up the canonical path first,
252 			 * then try exactly what the user entered.
253 			 */
254 			if (canonical_path == NULL ||
255 			    ((fs = getfsfile(canonical_path)) == NULL &&
256 			     (fs = getfsspec(canonical_path)) == NULL))
257 			{
258 				if ((fs = getfsfile(*argv)) == NULL &&
259 				    (fs = getfsspec(*argv)) == NULL) {
260 					goto out;
261 				}
262 			}
263 			if (BADTYPE(fs->fs_type))
264 				errx(EXIT_FAILURE,
265 				    "Unknown file system type for `%s'",
266 				    *argv);
267 			if (strcmp(fs->fs_spec, "from_mount") == 0) {
268 				if ((canonical_path == NULL ||
269 				    (mntbuf = getmntpt(canonical_path))
270 				     == NULL) &&
271 				    (mntbuf = getmntpt(*argv)) == NULL)
272 					goto out;
273 				mntfromname = mntbuf->f_mntfromname;
274 			} else
275 				mntfromname = fs->fs_spec;
276 			mntonname   = fs->fs_file;
277 			fstypename  = fs->fs_vfstype;
278 			mountopts   = fs->fs_mntops;
279 		}
280 		mntfromname = getfsspecname(buf, sizeof(buf), mntfromname);
281 		if (mntfromname == NULL)
282 			err(EXIT_FAILURE, "%s", buf);
283 		rval = mountfs(fstypename, mntfromname,
284 		    mntonname, init_flags, options, mountopts, 0, NULL, 0);
285 		break;
286 	case 2:
287 		/*
288 		 * If -t flag has not been specified, and spec contains either
289 		 * a ':' or a '@' then assume that an NFS filesystem is being
290 		 * specified ala Sun.
291 		 */
292 		mntfromname = getfsspecname(buf, sizeof(buf), argv[0]);
293 		if (mntfromname == NULL)
294 			err(EXIT_FAILURE, "%s", buf);
295 		if (vfslist == NULL) {
296 			if (strpbrk(argv[0], ":@") != NULL) {
297 				fprintf(stderr, "WARNING: autoselecting nfs "
298 				    "based on : or @ in the device name is "
299 				    "deprecated!\n"
300 				    "WARNING: This behaviour will be removed "
301 				    "in a future release\n");
302 				vfstype = "nfs";
303 			} else {
304 				vfstype = getfslab(mntfromname);
305 				if (vfstype == NULL)
306 					vfstype = ffs_fstype;
307 			}
308 		}
309 		rval = mountfs(vfstype, mntfromname, argv[1], init_flags,
310 		    options, NULL, 0, NULL, 0);
311 		break;
312 	default:
313 		usage();
314 	}
315 
316 #if 0	/* disabled because it interferes the service. */
317 	/*
318 	 * If the mount was successfully, and done by root, tell mountd the
319 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
320 	 */
321 	if (rval == 0 && getuid() == 0 &&
322 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
323 		int pid;
324 
325 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
326 		    pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
327 			err(EXIT_FAILURE, "signal mountd");
328 		(void)fclose(mountdfp);
329 	}
330 #endif
331 
332 	return rval;
333 }
334 
335 int
336 hasopt(const char *mntopts, const char *option)
337 {
338 	int negative, found;
339 	char *opt, *optbuf;
340 
341 	if (option[0] == 'n' && option[1] == 'o') {
342 		negative = 1;
343 		option += 2;
344 	} else
345 		negative = 0;
346 	optbuf = estrdup(mntopts);
347 	found = 0;
348 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
349 		if (opt[0] == 'n' && opt[1] == 'o') {
350 			if (!strcasecmp(opt + 2, option))
351 				found = negative;
352 		} else if (!strcasecmp(opt, option))
353 			found = !negative;
354 	}
355 	free(optbuf);
356 	return (found);
357 }
358 
359 static int
360 mountfs(const char *vfstype, const char *spec, const char *name,
361     int flags, const char *options, const char *mntopts,
362     int skipmounted, char *buf, size_t buflen)
363 {
364 	/* List of directories containing mount_xxx subcommands. */
365 	static const char *edirs[] = {
366 #ifdef RESCUEDIR
367 		RESCUEDIR,
368 #endif
369 		_PATH_SBIN,
370 		_PATH_USRSBIN,
371 		NULL
372 	};
373 	const char ** volatile argv, **edir;
374 	struct statvfs *sfp, sf;
375 	pid_t pid;
376 	int pfd[2];
377 	int argc, numfs, i, status, maxargc;
378 	char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN],
379 	    mntpath[MAXPATHLEN];
380 	volatile int getargs;
381 
382 	if (realpath(name, mntpath) == NULL) {
383 		warn("realpath `%s'", name);
384 		return (1);
385 	}
386 
387 	name = mntpath;
388 
389 	optbuf = NULL;
390 	if (mntopts)
391 		catopt(&optbuf, mntopts);
392 
393 	if (options) {
394 		catopt(&optbuf, options);
395 		getargs = strstr(options, "getargs") != NULL;
396 	} else
397 		getargs = 0;
398 
399 	if (!mntopts && !options)
400 		catopt(&optbuf, "rw");
401 
402 	if (getargs == 0 && strcmp(name, "/") == 0 && !hasopt(optbuf, "union"))
403 		flags |= FLG_UPDATE;
404 	else if (skipmounted) {
405 		if ((numfs = getmntinfo(&sfp, MNT_WAIT)) == 0) {
406 			warn("getmntinfo");
407 			return (1);
408 		}
409 		for(i = 0; i < numfs; i++) {
410 			const char *mountedtype = sfp[i].f_fstypename;
411 			size_t cmplen = sizeof(sfp[i].f_fstypename);
412 
413 			/* remove "puffs|" from comparisons, if present */
414 #define TYPESIZE (sizeof(PUFFS_TYPEPREFIX)-1)
415 			if (strncmp(mountedtype,
416 			    PUFFS_TYPEPREFIX, TYPESIZE) == 0) {
417 				mountedtype += TYPESIZE;
418 				cmplen -= TYPESIZE;
419 			}
420 
421 			/*
422 			 * XXX can't check f_mntfromname,
423 			 * thanks to mfs, union, etc.
424 			 */
425 			if (strncmp(name, sfp[i].f_mntonname, MNAMELEN) == 0 &&
426 			    strncmp(vfstype, mountedtype, cmplen) == 0) {
427 				if (verbose)
428 					(void)printf("%s on %s type %.*s: "
429 					    "%s\n",
430 					    sfp[i].f_mntfromname,
431 					    sfp[i].f_mntonname,
432 					    (int)sizeof(sfp[i].f_fstypename),
433 					    sfp[i].f_fstypename,
434 					    "already mounted");
435 				return (0);
436 			}
437 		}
438 	}
439 	if (flags & FLG_FORCE)
440 		catopt(&optbuf, "force");
441 	if (flags & FLG_RDONLY)
442 		catopt(&optbuf, "ro");
443 	/* make -w override -r */
444 	if (flags & FLG_RDWRITE)
445 		catopt(&optbuf, "rw");
446 
447 	if (flags & FLG_UPDATE) {
448 		catopt(&optbuf, "update");
449 		/* Figure out the fstype only if we defaulted to ffs */
450 		if (vfstype == ffs_fstype && statvfs(name, &sf) != -1)
451 			vfstype = sf.f_fstypename;
452 	}
453 
454 	maxargc = 64;
455 	argv = ecalloc(maxargc, sizeof(*argv));
456 
457 	if (getargs &&
458 	    strncmp(vfstype, PUFFS_TYPEPREFIX, sizeof(PUFFS_TYPEPREFIX)-1) == 0)
459 		(void)snprintf(execbase, sizeof(execbase), "mount_puffs");
460 	else if (hasopt(optbuf, "rump"))
461 		(void)snprintf(execbase, sizeof(execbase), "rump_%s", vfstype);
462 	else
463 		(void)snprintf(execbase, sizeof(execbase), "mount_%s", vfstype);
464 	argc = 0;
465 	argv[argc++] = execbase;
466 	if (optbuf)
467 		mangle(optbuf, &argc, &argv, &maxargc);
468 	argv[argc++] = spec;
469 	argv[argc++] = name;
470 	argv[argc] = NULL;
471 
472 	if ((verbose && buf == NULL) || debug) {
473 		(void)printf("exec:");
474 		for (i = 0; i < argc; i++)
475 			(void)printf(" %s", argv[i]);
476 		(void)printf("\n");
477 	}
478 
479 	if (buf) {
480 		if (pipe(pfd) == -1)
481 			warn("Cannot create pipe");
482 	}
483 
484 	switch (pid = fork()) {
485 	case -1:				/* Error. */
486 		warn("fork");
487 		if (optbuf)
488 			free(optbuf);
489 		free(argv);
490 		return (1);
491 
492 	case 0:					/* Child. */
493 		if (debug)
494 			_exit(0);
495 
496 		if (buf) {
497 			(void)close(pfd[0]);
498 			(void)close(STDOUT_FILENO);
499 			if (dup2(pfd[1], STDOUT_FILENO) == -1)
500 				warn("Cannot open fd to mount program");
501 		}
502 
503 		/* Go find an executable. */
504 		edir = edirs;
505 		do {
506 			(void)snprintf(execname,
507 			    sizeof(execname), "%s/%s", *edir, execbase);
508 			(void)execv(execname, __UNCONST(argv));
509 			if (errno != ENOENT)
510 				warn("exec %s for %s", execname, name);
511 		} while (*++edir != NULL);
512 
513 		if (errno == ENOENT)
514 			warn("exec %s for %s: %s", execbase, name, execbase);
515 		_exit(1);
516 		/* NOTREACHED */
517 
518 	default:				/* Parent. */
519 		if (optbuf)
520 			free(optbuf);
521 		free(argv);
522 
523 		if (buf || getargs) {
524 			char tbuf[1024], *ptr;
525 			int nread;
526 
527 			if (buf == NULL) {
528 				ptr = tbuf;
529 				buflen = sizeof(tbuf) - 1;
530 			} else {
531 				ptr = buf;
532 				buflen--;
533 			}
534 			(void)close(pfd[1]);
535 			(void)signal(SIGPIPE, SIG_IGN);
536 			while ((nread = read(pfd[0], ptr, buflen)) > 0) {
537 				buflen -= nread;
538 				ptr += nread;
539 			}
540 			*ptr = '\0';
541 			if (buflen == 0) {
542 				while (read(pfd[0], &nread, sizeof(nread)) > 0)
543 					continue;
544 			}
545 			if (buf == NULL)
546 				(void)fprintf(stdout, "%s", tbuf);
547 		}
548 
549 		if (waitpid(pid, &status, 0) == -1) {
550 			warn("waitpid");
551 			return (1);
552 		}
553 
554 		if (WIFEXITED(status)) {
555 			if (WEXITSTATUS(status) != 0)
556 				return (WEXITSTATUS(status));
557 		} else if (WIFSIGNALED(status)) {
558 			warnx("%s: %s", name, strsignal(WTERMSIG(status)));
559 			return (1);
560 		}
561 
562 		if (buf == NULL) {
563 			if (verbose) {
564 				if (statvfs(name, &sf) == -1) {
565 					warn("statvfs %s", name);
566 					return (1);
567 				}
568 				prmount(&sf);
569 			}
570 		}
571 		break;
572 	}
573 
574 	return (0);
575 }
576 
577 static void
578 prmount(struct statvfs *sfp)
579 {
580 	int flags;
581 	const struct opt *o;
582 	struct passwd *pw;
583 	int f;
584 
585 	(void)printf("%s on %s type %.*s", sfp->f_mntfromname,
586 	    sfp->f_mntonname, (int)sizeof(sfp->f_fstypename),
587 	    sfp->f_fstypename);
588 
589 	flags = sfp->f_flag & MNT_VISFLAGMASK;
590 	for (f = 0, o = optnames; flags && o <
591 	    &optnames[sizeof(optnames)/sizeof(optnames[0])]; o++)
592 		if (flags & o->o_opt) {
593 			if (!o->o_silent || verbose)
594 				(void)printf("%s%s", !f++ ? " (" : ", ",
595 				    o->o_name);
596 			flags &= ~o->o_opt;
597 		}
598 	if (flags)
599 		(void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
600 		    flags & (flags - 1) ? "s" : "", flags);
601 	if (sfp->f_owner) {
602 		(void)printf("%smounted by ", !f++ ? " (" : ", ");
603 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
604 			(void)printf("%s", pw->pw_name);
605 		else
606 			(void)printf("%d", sfp->f_owner);
607 	}
608 	if (verbose)
609 		(void)printf("%sfsid: 0x%x/0x%x",
610 		    !f++ ? " (" /* ) */: ", ",
611 		    sfp->f_fsidx.__fsid_val[0], sfp->f_fsidx.__fsid_val[1]);
612 
613 	if (verbose) {
614 		(void)printf("%s", !f++ ? " (" : ", ");
615 		(void)printf("reads: sync %" PRIu64 " async %" PRIu64 "",
616 		    sfp->f_syncreads, sfp->f_asyncreads);
617 		(void)printf(", writes: sync %" PRIu64 " async %" PRIu64 "",
618 		    sfp->f_syncwrites, sfp->f_asyncwrites);
619 		if (verbose > 1) {
620 			char buf[2048];
621 
622 			if (getmntargs(sfp, buf, sizeof(buf)))
623 				printf(", [%s: %s]", sfp->f_fstypename, buf);
624 		}
625 		printf(")\n");
626 	} else
627 		(void)printf("%s", f ? ")\n" : "\n");
628 }
629 
630 static int
631 getmntargs(struct statvfs *sfs, char *buf, size_t buflen)
632 {
633 
634 	if (mountfs(sfs->f_fstypename, sfs->f_mntfromname, sfs->f_mntonname, 0,
635 	    "getargs", NULL, 0, buf, buflen))
636 		return (0);
637 	else {
638 		if (*buf == '\0')
639 			return (0);
640 		if ((buf = strchr(buf, '\n')) != NULL)
641 			*buf = '\0';
642 		return (1);
643 	}
644 }
645 
646 static struct statvfs *
647 getmntpt(const char *name)
648 {
649 	struct statvfs *mntbuf;
650 	int i, mntsize;
651 
652 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
653 	for (i = 0; i < mntsize; i++)
654 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
655 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
656 			return (&mntbuf[i]);
657 	return (NULL);
658 }
659 
660 static void
661 catopt(char **sp, const char *o)
662 {
663 	char *s, *n;
664 
665 	s = *sp;
666 	if (s) {
667 		easprintf(&n, "%s,%s", s, o);
668 		free(s);
669 		s = n;
670 	} else
671 		s = estrdup(o);
672 	*sp = s;
673 }
674 
675 static void
676 mangle(char *options, int *argcp, const char ** volatile *argvp, int *maxargcp)
677 {
678 	char *p, *s;
679 	int argc, maxargc;
680 	const char **argv, **nargv;
681 
682 	argc = *argcp;
683 	argv = *argvp;
684 	maxargc = *maxargcp;
685 
686 	for (s = options; (p = strsep(&s, ",")) != NULL;) {
687 		/* Always leave space for one more argument and the NULL. */
688 		if (argc >= maxargc - 4) {
689 			nargv = erealloc(argv, (maxargc << 1) * sizeof(nargv));
690 			argv = nargv;
691 			maxargc <<= 1;
692 		}
693 		if (*p != '\0') {
694 			if (*p == '-') {
695 				argv[argc++] = p;
696 				p = strchr(p, '=');
697 				if (p) {
698 					*p = '\0';
699 					argv[argc++] = p+1;
700 				}
701 			} else {
702 				argv[argc++] = "-o";
703 				argv[argc++] = p;
704 			}
705 		}
706 	}
707 
708 	*argcp = argc;
709 	*argvp = argv;
710 	*maxargcp = maxargc;
711 }
712 
713 /* Deduce the filesystem type from the disk label. */
714 static const char *
715 getfslab(const char *str)
716 {
717 	static struct dkwedge_info dkw;
718 	struct disklabel dl;
719 	int fd;
720 	int part;
721 	const char *vfstype;
722 	u_char fstype;
723 	char buf[MAXPATHLEN + 1];
724 	char *sp, *ep;
725 
726 	if ((fd = open(str, O_RDONLY)) == -1) {
727 		/*
728 		 * Iff we get EBUSY try the raw device. Since mount always uses
729 		 * the block device we know we are never passed a raw device.
730 		 */
731 		if (errno != EBUSY)
732 			err(EXIT_FAILURE, "cannot open `%s'", str);
733 		strlcpy(buf, str, MAXPATHLEN);
734 		if ((sp = strrchr(buf, '/')) != NULL)
735 			++sp;
736 		else
737 			sp = buf;
738 		for (ep = sp + strlen(sp) + 1;  ep > sp; ep--)
739 			*ep = *(ep - 1);
740 		*sp = 'r';
741 
742 		/* Silently fail here - mount call can display error */
743 		if ((fd = open(buf, O_RDONLY)) == -1)
744 			return (NULL);
745 	}
746 
747 	/* Check to see if this is a wedge. */
748 	if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
749 		/* Yup, this is easy. */
750 		(void) close(fd);
751 		return (dkw.dkw_ptype);
752 	}
753 
754 	if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
755 		(void) close(fd);
756 		return (NULL);
757 	}
758 
759 	(void) close(fd);
760 
761 	part = str[strlen(str) - 1] - 'a';
762 
763 	if (part < 0 || part >= dl.d_npartitions)
764 		return (NULL);
765 
766 	/* Return NULL for unknown types - caller can fall back to ffs */
767 	if ((fstype = dl.d_partitions[part].p_fstype) >= FSMAXMOUNTNAMES)
768 		vfstype = NULL;
769 	else
770 		vfstype = mountnames[fstype];
771 
772 	return (vfstype);
773 }
774 
775 static void
776 usage(void)
777 {
778 
779 	(void)fprintf(stderr, "Usage: %s [-Aadfruvw] [-t type]\n"
780 	    "\t%s [-dfruvw] special | node\n"
781 	    "\t%s [-dfruvw] [-o options] [-t type] special node\n",
782 	    getprogname(), getprogname(), getprogname());
783 	exit(1);
784 }
785