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