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