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