Home | History | Annotate | Line # | Download | only in mount
mount.c revision 1.94
      1 /*	$NetBSD: mount.c,v 1.94 2012/04/07 03:13:32 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.94 2012/04/07 03:13:32 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 #include <sys/disk.h>
     50 #include <sys/sysctl.h>
     51 
     52 #include <fs/puffs/puffs_msgif.h>
     53 
     54 #include <err.h>
     55 #include <errno.h>
     56 #include <fstab.h>
     57 #include <pwd.h>
     58 #include <signal.h>
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 #include <unistd.h>
     63 #include <util.h>
     64 #include <vis.h>
     65 
     66 #define MOUNTNAMES
     67 #include <fcntl.h>
     68 #include <sys/disk.h>
     69 #include <sys/disklabel.h>
     70 #include <sys/ioctl.h>
     71 
     72 #include "pathnames.h"
     73 #include "mountprog.h"
     74 
     75 static int	debug, verbose;
     76 
     77 static void	catopt(char **, const char *);
     78 static const char *
     79 		getfslab(const char *str);
     80 static struct statvfs *
     81 		getmntpt(const char *);
     82 static int 	getmntargs(struct statvfs *, char *, size_t);
     83 static const char *
     84 		getspec(char *, size_t, const char *);
     85 static int	hasopt(const char *, const char *);
     86 static void	mangle(char *, int *, const char ** volatile *, int *);
     87 static int	mountfs(const char *, const char *, const char *,
     88 		    int, const char *, const char *, int, char *, size_t);
     89 static void	prmount(struct statvfs *);
     90 __dead static void	usage(void);
     91 
     92 
     93 /* Map from mount otions to printable formats. */
     94 static const struct opt {
     95 	int o_opt;
     96 	int o_silent;
     97 	const char *o_name;
     98 } optnames[] = {
     99 	__MNT_FLAGS
    100 };
    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 |= MNT_FORCE;
    137 			break;
    138 		case 'o':
    139 			if (*optarg)
    140 				catopt(&options, optarg);
    141 			break;
    142 		case 'r':
    143 			init_flags |= MNT_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 |= MNT_UPDATE;
    154 			break;
    155 		case 'v':
    156 			verbose++;
    157 			break;
    158 		case 'w':
    159 			init_flags &= ~MNT_RDONLY;
    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 =
    194 				    getspec(buf, sizeof(buf), mntfromname);
    195 				if (mountfs(fs->fs_vfstype, mntfromname,
    196 				    fs->fs_file, init_flags, options,
    197 				    fs->fs_mntops, !forceall, NULL, 0))
    198 					rval = 1;
    199 			}
    200 		else {
    201 			if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
    202 				err(EXIT_FAILURE, "getmntinfo");
    203 			for (i = 0; i < mntsize; i++) {
    204 				if (checkvfsname(mntbuf[i].f_fstypename,
    205 				    vfslist))
    206 					continue;
    207 				prmount(&mntbuf[i]);
    208 			}
    209 		}
    210 		exit(rval);
    211 		/* NOTREACHED */
    212 	case 1:
    213 		if (vfslist != NULL) {
    214 			usage();
    215 			/* NOTREACHED */
    216 		}
    217 
    218 		/*
    219 		 * Create a canonical version of the device or mount path
    220 		 * passed to us.  It's ok for this to fail.  It's also ok
    221 		 * for the result to be exactly the same as the original.
    222 		 */
    223 		canonical_path = realpath(*argv, canonical_path_buf);
    224 
    225 		if (init_flags & MNT_UPDATE) {
    226 			/*
    227 			 * Try looking up the canonical path first,
    228 			 * then try exactly what the user entered.
    229 			 */
    230 			if ((canonical_path == NULL ||
    231 			    (mntbuf = getmntpt(canonical_path)) == NULL) &&
    232 			    (mntbuf = getmntpt(*argv)) == NULL) {
    233 out:
    234 				errx(EXIT_FAILURE,
    235 				    "Unknown special file or file system `%s'",
    236 				    *argv);
    237 			}
    238 			mntfromname = mntbuf->f_mntfromname;
    239 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
    240 				if (strcmp(fs->fs_spec, "from_mount") != 0)
    241 					mntfromname = fs->fs_spec;
    242 				/* ignore the fstab file options.  */
    243 				fs->fs_mntops = NULL;
    244 			}
    245 			mntonname  = mntbuf->f_mntonname;
    246 			fstypename = mntbuf->f_fstypename;
    247 			mountopts  = NULL;
    248 		} else {
    249 			/*
    250 			 * Try looking up the canonical path first,
    251 			 * then try exactly what the user entered.
    252 			 */
    253 			if (canonical_path == NULL ||
    254 			    ((fs = getfsfile(canonical_path)) == NULL &&
    255 			     (fs = getfsspec(canonical_path)) == NULL))
    256 			{
    257 				if ((fs = getfsfile(*argv)) == NULL &&
    258 				    (fs = getfsspec(*argv)) == NULL) {
    259 					goto out;
    260 				}
    261 			}
    262 			if (BADTYPE(fs->fs_type))
    263 				errx(EXIT_FAILURE,
    264 				    "Unknown file system type for `%s'",
    265 				    *argv);
    266 			if (strcmp(fs->fs_spec, "from_mount") == 0) {
    267 				if ((canonical_path == NULL ||
    268 				    (mntbuf = getmntpt(canonical_path))
    269 				     == NULL) &&
    270 				    (mntbuf = getmntpt(*argv)) == NULL)
    271 					goto out;
    272 				mntfromname = mntbuf->f_mntfromname;
    273 			} else
    274 				mntfromname = fs->fs_spec;
    275 			mntonname   = fs->fs_file;
    276 			fstypename  = fs->fs_vfstype;
    277 			mountopts   = fs->fs_mntops;
    278 		}
    279 		mntfromname = getspec(buf, sizeof(buf), mntfromname);
    280 		rval = mountfs(fstypename, mntfromname,
    281 		    mntonname, init_flags, options, mountopts, 0, NULL, 0);
    282 		break;
    283 	case 2:
    284 		/*
    285 		 * If -t flag has not been specified, and spec contains either
    286 		 * a ':' or a '@' then assume that an NFS filesystem is being
    287 		 * specified ala Sun.
    288 		 */
    289 		mntfromname = getspec(buf, sizeof(buf), argv[0]);
    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 0;
    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 = vfork()) {
    477 	case -1:				/* Error. */
    478 		warn("vfork");
    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 			warnx("%s not found for %s", execbase, name);
    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 /* Query device path from disk name */
    768 static const char *
    769 getspec(char *buf, size_t bufsiz, const char *name)
    770 {
    771 	static const int mib[] = { CTL_HW, HW_DISKNAMES };
    772 	char *drives, *dk;
    773 	size_t len;
    774 	int fd;
    775 	char *vname;
    776 
    777 	if (strncasecmp(name, "NAME=", 5) != 0)
    778 		return name;
    779 	name += 5;
    780 	vname = emalloc(strlen(name) * 4 + 1);
    781 	strunvis(vname, name);
    782 
    783 	if (sysctl(mib, __arraycount(mib), NULL, &len, NULL, 0) == -1)
    784 		err(EXIT_FAILURE, "sysctl hw.disknames failed");
    785 
    786 	drives = emalloc(len);
    787 	if (sysctl(mib, __arraycount(mib), drives, &len, NULL, 0) == -1)
    788 		err(EXIT_FAILURE, "sysctl hw.disknames failed");
    789 
    790 	for (dk = strtok(drives, " "); dk != NULL; dk = strtok(NULL, " ")) {
    791 		struct dkwedge_info dkw;
    792 		if (strncmp(dk, "dk", 2) != 0)
    793 			continue;
    794 		fd = opendisk(dk, O_RDONLY, buf, bufsiz, 0);
    795 		if (fd == -1)
    796 			continue;
    797 		if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == -1)
    798 			err(EXIT_FAILURE, "%s: getwedgeinfo", dk);
    799 		(void)close(fd);
    800 		if (strcmp(vname, (char *)dkw.dkw_wname) == 0) {
    801 			free(drives);
    802 			free(vname);
    803 			vname = strstr(buf, "/rdk");
    804 			if (vname++ == NULL)
    805 				return buf;
    806 			strcpy(vname, vname + 1);
    807 			return buf;
    808 		}
    809 	}
    810 	err(EXIT_FAILURE, "Cannot find a match for `%s'", vname);
    811 }
    812 
    813 static void
    814 usage(void)
    815 {
    816 
    817 	(void)fprintf(stderr, "Usage: %s [-Aadfruvw] [-t type]\n"
    818 	    "\t%s [-dfruvw] special | node\n"
    819 	    "\t%s [-dfruvw] [-o options] [-t type] special node\n",
    820 	    getprogname(), getprogname(), getprogname());
    821 	exit(1);
    822 }
    823