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