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