Home | History | Annotate | Line # | Download | only in mount
mount.c revision 1.88
      1 /*	$NetBSD: mount.c,v 1.88 2009/01/11 20:39:34 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.88 2009/01/11 20:39:34 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 <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 "mountprog.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 ** volatile *, 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 				fprintf(stderr, "WARNING: autoselecting nfs "
    285 				    "based on : or @ in the device name is "
    286 				    "deprecated!\n"
    287 				    "WARNING: This behaviour will be removed "
    288 				    "in a future release\n");
    289 				vfstype = "nfs";
    290 			} else {
    291 				vfstype = getfslab(argv[0]);
    292 				if (vfstype == NULL)
    293 					vfstype = ffs_fstype;
    294 			}
    295 		}
    296 		rval = mountfs(vfstype,
    297 		    argv[0], argv[1], init_flags, options, NULL, 0, NULL, 0);
    298 		break;
    299 	default:
    300 		usage();
    301 		/* NOTREACHED */
    302 	}
    303 
    304 	/*
    305 	 * If the mount was successfully, and done by root, tell mountd the
    306 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
    307 	 */
    308 	if (rval == 0 && getuid() == 0 &&
    309 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
    310 		int pid;
    311 
    312 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
    313 		    pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
    314 			err(1, "signal mountd");
    315 		(void)fclose(mountdfp);
    316 	}
    317 
    318 	exit(rval);
    319 	/* NOTREACHED */
    320 }
    321 
    322 int
    323 hasopt(const char *mntopts, const char *option)
    324 {
    325 	int negative, found;
    326 	char *opt, *optbuf;
    327 
    328 	if (option[0] == 'n' && option[1] == 'o') {
    329 		negative = 1;
    330 		option += 2;
    331 	} else
    332 		negative = 0;
    333 	optbuf = strdup(mntopts);
    334 	found = 0;
    335 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
    336 		if (opt[0] == 'n' && opt[1] == 'o') {
    337 			if (!strcasecmp(opt + 2, option))
    338 				found = negative;
    339 		} else if (!strcasecmp(opt, option))
    340 			found = !negative;
    341 	}
    342 	free(optbuf);
    343 	return (found);
    344 }
    345 
    346 static int
    347 mountfs(const char *vfstype, const char *spec, const char *name,
    348 	int flags, const char *options, const char *mntopts,
    349 	int skipmounted, char *buf, 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 ** volatile 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 	volatile int getargs;
    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 
    380 	if (options) {
    381 		catopt(&optbuf, options);
    382 		getargs = strstr(options, "getargs") != NULL;
    383 	} else
    384 		getargs = 0;
    385 
    386 	if (!mntopts && !options)
    387 		catopt(&optbuf, "rw");
    388 
    389 	if (getargs == 0 && strcmp(name, "/") == 0)
    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 				sizeof(sfp[i].f_fstypename)) == 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 					    (int)sizeof(sfp[i].f_fstypename),
    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 	if (argv == NULL)
    431 		err(1, "malloc");
    432 
    433 	if (hasopt(optbuf, "rump"))
    434 		(void)snprintf(execbase, sizeof(execbase), "rump_%s", vfstype);
    435 	else
    436 		(void)snprintf(execbase, sizeof(execbase), "mount_%s", vfstype);
    437 	argc = 0;
    438 	argv[argc++] = execbase;
    439 	if (optbuf)
    440 		mangle(optbuf, &argc, &argv, &maxargc);
    441 	argv[argc++] = spec;
    442 	argv[argc++] = name;
    443 	argv[argc] = NULL;
    444 
    445 	if ((verbose && buf == NULL) || debug) {
    446 		(void)printf("exec:");
    447 		for (i = 0; i < argc; i++)
    448 			(void)printf(" %s", argv[i]);
    449 		(void)printf("\n");
    450 	}
    451 
    452 	if (buf) {
    453 		if (pipe(pfd) == -1)
    454 			warn("Cannot create pipe");
    455 	}
    456 
    457 	switch (pid = vfork()) {
    458 	case -1:				/* Error. */
    459 		warn("vfork");
    460 		if (optbuf)
    461 			free(optbuf);
    462 		free(argv);
    463 		return (1);
    464 
    465 	case 0:					/* Child. */
    466 		if (debug)
    467 			_exit(0);
    468 
    469 		if (buf) {
    470 			(void)close(pfd[0]);
    471 			(void)close(STDOUT_FILENO);
    472 			if (dup2(pfd[1], STDOUT_FILENO) == -1)
    473 				warn("Cannot open fd to mount program");
    474 		}
    475 
    476 		/* Go find an executable. */
    477 		edir = edirs;
    478 		do {
    479 			(void)snprintf(execname,
    480 			    sizeof(execname), "%s/%s", *edir, execbase);
    481 			(void)execv(execname, __UNCONST(argv));
    482 			if (errno != ENOENT)
    483 				warn("exec %s for %s", execname, name);
    484 		} while (*++edir != NULL);
    485 
    486 		if (errno == ENOENT)
    487 			warnx("%s not found for %s", execbase, name);
    488 		_exit(1);
    489 		/* NOTREACHED */
    490 
    491 	default:				/* Parent. */
    492 		if (optbuf)
    493 			free(optbuf);
    494 		free(argv);
    495 
    496 		if (buf || getargs) {
    497 			char tbuf[1024], *ptr;
    498 			int nread;
    499 
    500 			if (buf == NULL) {
    501 				ptr = tbuf;
    502 				buflen = sizeof(tbuf) - 1;
    503 			} else {
    504 				ptr = buf;
    505 				buflen--;
    506 			}
    507 			(void)close(pfd[1]);
    508 			(void)signal(SIGPIPE, SIG_IGN);
    509 			while ((nread = read(pfd[0], ptr, buflen)) > 0) {
    510 				buflen -= nread;
    511 				ptr += nread;
    512 			}
    513 			*ptr = '\0';
    514 			if (buflen == 0) {
    515 				while (read(pfd[0], &nread, sizeof(nread)) > 0)
    516 					continue;
    517 			}
    518 			if (buf == NULL)
    519 				(void)fprintf(stdout, "%s", tbuf);
    520 		}
    521 
    522 		if (waitpid(pid, &status, 0) < 0) {
    523 			warn("waitpid");
    524 			return (1);
    525 		}
    526 
    527 		if (WIFEXITED(status)) {
    528 			if (WEXITSTATUS(status) != 0)
    529 				return (WEXITSTATUS(status));
    530 		} else if (WIFSIGNALED(status)) {
    531 			warnx("%s: %s", name, strsignal(WTERMSIG(status)));
    532 			return (1);
    533 		}
    534 
    535 		if (buf == NULL) {
    536 			if (verbose) {
    537 				if (statvfs(name, &sf) < 0) {
    538 					warn("statvfs %s", name);
    539 					return (1);
    540 				}
    541 				prmount(&sf);
    542 			}
    543 		}
    544 		break;
    545 	}
    546 
    547 	return (0);
    548 }
    549 
    550 static void
    551 prmount(struct statvfs *sfp)
    552 {
    553 	int flags;
    554 	const struct opt *o;
    555 	struct passwd *pw;
    556 	int f;
    557 
    558 	(void)printf("%s on %s type %.*s", sfp->f_mntfromname,
    559 	    sfp->f_mntonname, (int)sizeof(sfp->f_fstypename),
    560 	    sfp->f_fstypename);
    561 
    562 	flags = sfp->f_flag & MNT_VISFLAGMASK;
    563 	for (f = 0, o = optnames; flags && o <
    564 	    &optnames[sizeof(optnames)/sizeof(optnames[0])]; o++)
    565 		if (flags & o->o_opt) {
    566 			if (!o->o_silent || verbose)
    567 				(void)printf("%s%s", !f++ ? " (" : ", ",
    568 				    o->o_name);
    569 			flags &= ~o->o_opt;
    570 		}
    571 	if (flags)
    572 		(void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
    573 		    flags & (flags - 1) ? "s" : "", flags);
    574 	if (sfp->f_owner) {
    575 		(void)printf("%smounted by ", !f++ ? " (" : ", ");
    576 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
    577 			(void)printf("%s", pw->pw_name);
    578 		else
    579 			(void)printf("%d", sfp->f_owner);
    580 	}
    581 	if (verbose)
    582 		(void)printf("%sfsid: 0x%x/0x%x",
    583 		    !f++ ? " (" /* ) */: ", ",
    584 		    sfp->f_fsidx.__fsid_val[0], sfp->f_fsidx.__fsid_val[1]);
    585 
    586 	if (verbose) {
    587 		(void)printf("%s", !f++ ? " (" : ", ");
    588 		(void)printf("reads: sync %" PRIu64 " async %" PRIu64 "",
    589 		    sfp->f_syncreads, sfp->f_asyncreads);
    590 		(void)printf(", writes: sync %" PRIu64 " async %" PRIu64 "",
    591 		    sfp->f_syncwrites, sfp->f_asyncwrites);
    592 		if (verbose > 1) {
    593 			char buf[2048];
    594 
    595 			if (getmntargs(sfp, buf, sizeof(buf)))
    596 				printf(", [%s: %s]", sfp->f_fstypename, buf);
    597 		}
    598 		printf(")\n");
    599 	} else
    600 		(void)printf("%s", f ? ")\n" : "\n");
    601 }
    602 
    603 static int
    604 getmntargs(struct statvfs *sfs, char *buf, size_t buflen)
    605 {
    606 
    607 	if (mountfs(sfs->f_fstypename, sfs->f_mntfromname, sfs->f_mntonname, 0,
    608 	    "getargs", NULL, 0, buf, buflen))
    609 		return (0);
    610 	else {
    611 		if (*buf == '\0')
    612 			return (0);
    613 		if ((buf = strchr(buf, '\n')) != NULL)
    614 			*buf = '\0';
    615 		return (1);
    616 	}
    617 }
    618 
    619 static struct statvfs *
    620 getmntpt(const char *name)
    621 {
    622 	struct statvfs *mntbuf;
    623 	int i, mntsize;
    624 
    625 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    626 	for (i = 0; i < mntsize; i++)
    627 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
    628 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
    629 			return (&mntbuf[i]);
    630 	return (NULL);
    631 }
    632 
    633 static void
    634 catopt(char **sp, 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(char *options, int *argcp, const char ** volatile *argvp, int *maxargcp)
    651 {
    652 	char *p, *s;
    653 	int argc, maxargc;
    654 	const char **argv, **nargv;
    655 
    656 	argc = *argcp;
    657 	argv = *argvp;
    658 	maxargc = *maxargcp;
    659 
    660 	for (s = options; (p = strsep(&s, ",")) != NULL;) {
    661 		/* Always leave space for one more argument and the NULL. */
    662 		if (argc >= maxargc - 4) {
    663 			nargv = realloc(argv, (maxargc << 1) * sizeof(char *));
    664 			if (!nargv)
    665 				err(1, "realloc");
    666 			argv = nargv;
    667 			maxargc <<= 1;
    668 		}
    669 		if (*p != '\0') {
    670 			if (*p == '-') {
    671 				argv[argc++] = p;
    672 				p = strchr(p, '=');
    673 				if (p) {
    674 					*p = '\0';
    675 					argv[argc++] = p+1;
    676 				}
    677 			} else if (strcmp(p, "rw") != 0) {
    678 				argv[argc++] = "-o";
    679 				argv[argc++] = p;
    680 			}
    681 		}
    682 	}
    683 
    684 	*argcp = argc;
    685 	*argvp = argv;
    686 	*maxargcp = maxargc;
    687 }
    688 
    689 /* Deduce the filesystem type from the disk label. */
    690 static const char *
    691 getfslab(const char *str)
    692 {
    693 	static struct dkwedge_info dkw;
    694 	struct disklabel dl;
    695 	int fd;
    696 	int part;
    697 	const char *vfstype;
    698 	u_char fstype;
    699 	char buf[MAXPATHLEN + 1];
    700 	char *sp, *ep;
    701 
    702 	if ((fd = open(str, O_RDONLY)) == -1) {
    703 		/*
    704 		 * Iff we get EBUSY try the raw device. Since mount always uses
    705 		 * the block device we know we are never passed a raw device.
    706 		 */
    707 		if (errno != EBUSY)
    708 			err(1, "cannot open `%s'", str);
    709 		strlcpy(buf, str, MAXPATHLEN);
    710 		if ((sp = strrchr(buf, '/')) != NULL)
    711 			++sp;
    712 		else
    713 			sp = buf;
    714 		for (ep = sp + strlen(sp) + 1;  ep > sp; ep--)
    715 			*ep = *(ep - 1);
    716 		*sp = 'r';
    717 
    718 		/* Silently fail here - mount call can display error */
    719 		if ((fd = open(buf, O_RDONLY)) == -1)
    720 			return (NULL);
    721 	}
    722 
    723 	/* Check to see if this is a wedge. */
    724 	if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
    725 		/* Yup, this is easy. */
    726 		(void) close(fd);
    727 		return (dkw.dkw_ptype);
    728 	}
    729 
    730 	if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
    731 		(void) close(fd);
    732 		return (NULL);
    733 	}
    734 
    735 	(void) close(fd);
    736 
    737 	part = str[strlen(str) - 1] - 'a';
    738 
    739 	if (part < 0 || part >= dl.d_npartitions)
    740 		return (NULL);
    741 
    742 	/* Return NULL for unknown types - caller can fall back to ffs */
    743 	if ((fstype = dl.d_partitions[part].p_fstype) >= FSMAXMOUNTNAMES)
    744 		vfstype = NULL;
    745 	else
    746 		vfstype = mountnames[fstype];
    747 
    748 	return (vfstype);
    749 }
    750 
    751 static void
    752 usage(void)
    753 {
    754 
    755 	(void)fprintf(stderr,
    756 	    "usage: mount %s\n       mount %s\n       mount %s\n",
    757 	    "[-Aadfruvw] [-t type]",
    758 	    "[-dfruvw] special | node",
    759 	    "[-dfruvw] [-o options] [-t type] special node");
    760 	exit(1);
    761 	/* NOTREACHED */
    762 }
    763