Home | History | Annotate | Line # | Download | only in mount
mount.c revision 1.87
      1 /*	$NetBSD: mount.c,v 1.87 2009/01/11 12:33:50 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.87 2009/01/11 12:33:50 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 	(void) snprintf(execbase, sizeof(execbase), "mount_%s", vfstype);
    434 	argc = 0;
    435 	argv[argc++] = execbase;
    436 	if (optbuf)
    437 		mangle(optbuf, &argc, &argv, &maxargc);
    438 	argv[argc++] = spec;
    439 	argv[argc++] = name;
    440 	argv[argc] = NULL;
    441 
    442 	if ((verbose && buf == NULL) || debug) {
    443 		(void)printf("exec:");
    444 		for (i = 0; i < argc; i++)
    445 			(void)printf(" %s", argv[i]);
    446 		(void)printf("\n");
    447 	}
    448 
    449 	if (buf) {
    450 		if (pipe(pfd) == -1)
    451 			warn("Cannot create pipe");
    452 	}
    453 
    454 	switch (pid = vfork()) {
    455 	case -1:				/* Error. */
    456 		warn("vfork");
    457 		if (optbuf)
    458 			free(optbuf);
    459 		free(argv);
    460 		return (1);
    461 
    462 	case 0:					/* Child. */
    463 		if (debug)
    464 			_exit(0);
    465 
    466 		if (buf) {
    467 			(void)close(pfd[0]);
    468 			(void)close(STDOUT_FILENO);
    469 			if (dup2(pfd[1], STDOUT_FILENO) == -1)
    470 				warn("Cannot open fd to mount program");
    471 		}
    472 
    473 		/* Go find an executable. */
    474 		edir = edirs;
    475 		do {
    476 			(void)snprintf(execname,
    477 			    sizeof(execname), "%s/%s", *edir, execbase);
    478 			(void)execv(execname, __UNCONST(argv));
    479 			if (errno != ENOENT)
    480 				warn("exec %s for %s", execname, name);
    481 		} while (*++edir != NULL);
    482 
    483 		if (errno == ENOENT)
    484 			warnx("%s not found for %s", execbase, name);
    485 		_exit(1);
    486 		/* NOTREACHED */
    487 
    488 	default:				/* Parent. */
    489 		if (optbuf)
    490 			free(optbuf);
    491 		free(argv);
    492 
    493 		if (buf || getargs) {
    494 			char tbuf[1024], *ptr;
    495 			int nread;
    496 
    497 			if (buf == NULL) {
    498 				ptr = tbuf;
    499 				buflen = sizeof(tbuf) - 1;
    500 			} else {
    501 				ptr = buf;
    502 				buflen--;
    503 			}
    504 			(void)close(pfd[1]);
    505 			(void)signal(SIGPIPE, SIG_IGN);
    506 			while ((nread = read(pfd[0], ptr, buflen)) > 0) {
    507 				buflen -= nread;
    508 				ptr += nread;
    509 			}
    510 			*ptr = '\0';
    511 			if (buflen == 0) {
    512 				while (read(pfd[0], &nread, sizeof(nread)) > 0)
    513 					continue;
    514 			}
    515 			if (buf == NULL)
    516 				(void)fprintf(stdout, "%s", tbuf);
    517 		}
    518 
    519 		if (waitpid(pid, &status, 0) < 0) {
    520 			warn("waitpid");
    521 			return (1);
    522 		}
    523 
    524 		if (WIFEXITED(status)) {
    525 			if (WEXITSTATUS(status) != 0)
    526 				return (WEXITSTATUS(status));
    527 		} else if (WIFSIGNALED(status)) {
    528 			warnx("%s: %s", name, strsignal(WTERMSIG(status)));
    529 			return (1);
    530 		}
    531 
    532 		if (buf == NULL) {
    533 			if (verbose) {
    534 				if (statvfs(name, &sf) < 0) {
    535 					warn("statvfs %s", name);
    536 					return (1);
    537 				}
    538 				prmount(&sf);
    539 			}
    540 		}
    541 		break;
    542 	}
    543 
    544 	return (0);
    545 }
    546 
    547 static void
    548 prmount(struct statvfs *sfp)
    549 {
    550 	int flags;
    551 	const struct opt *o;
    552 	struct passwd *pw;
    553 	int f;
    554 
    555 	(void)printf("%s on %s type %.*s", sfp->f_mntfromname,
    556 	    sfp->f_mntonname, (int)sizeof(sfp->f_fstypename),
    557 	    sfp->f_fstypename);
    558 
    559 	flags = sfp->f_flag & MNT_VISFLAGMASK;
    560 	for (f = 0, o = optnames; flags && o <
    561 	    &optnames[sizeof(optnames)/sizeof(optnames[0])]; o++)
    562 		if (flags & o->o_opt) {
    563 			if (!o->o_silent || verbose)
    564 				(void)printf("%s%s", !f++ ? " (" : ", ",
    565 				    o->o_name);
    566 			flags &= ~o->o_opt;
    567 		}
    568 	if (flags)
    569 		(void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
    570 		    flags & (flags - 1) ? "s" : "", flags);
    571 	if (sfp->f_owner) {
    572 		(void)printf("%smounted by ", !f++ ? " (" : ", ");
    573 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
    574 			(void)printf("%s", pw->pw_name);
    575 		else
    576 			(void)printf("%d", sfp->f_owner);
    577 	}
    578 	if (verbose)
    579 		(void)printf("%sfsid: 0x%x/0x%x",
    580 		    !f++ ? " (" /* ) */: ", ",
    581 		    sfp->f_fsidx.__fsid_val[0], sfp->f_fsidx.__fsid_val[1]);
    582 
    583 	if (verbose) {
    584 		(void)printf("%s", !f++ ? " (" : ", ");
    585 		(void)printf("reads: sync %" PRIu64 " async %" PRIu64 "",
    586 		    sfp->f_syncreads, sfp->f_asyncreads);
    587 		(void)printf(", writes: sync %" PRIu64 " async %" PRIu64 "",
    588 		    sfp->f_syncwrites, sfp->f_asyncwrites);
    589 		if (verbose > 1) {
    590 			char buf[2048];
    591 
    592 			if (getmntargs(sfp, buf, sizeof(buf)))
    593 				printf(", [%s: %s]", sfp->f_fstypename, buf);
    594 		}
    595 		printf(")\n");
    596 	} else
    597 		(void)printf("%s", f ? ")\n" : "\n");
    598 }
    599 
    600 static int
    601 getmntargs(struct statvfs *sfs, char *buf, 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(const char *name)
    618 {
    619 	struct statvfs *mntbuf;
    620 	int i, mntsize;
    621 
    622 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    623 	for (i = 0; i < mntsize; i++)
    624 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
    625 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
    626 			return (&mntbuf[i]);
    627 	return (NULL);
    628 }
    629 
    630 static void
    631 catopt(char **sp, const char *o)
    632 {
    633 	char *s, *n;
    634 
    635 	s = *sp;
    636 	if (s) {
    637 		if (asprintf(&n, "%s,%s", s, o) < 0)
    638 			err(1, "asprintf");
    639 		free(s);
    640 		s = n;
    641 	} else
    642 		s = strdup(o);
    643 	*sp = s;
    644 }
    645 
    646 static void
    647 mangle(char *options, int *argcp, const char ** volatile *argvp, int *maxargcp)
    648 {
    649 	char *p, *s;
    650 	int argc, maxargc;
    651 	const char **argv, **nargv;
    652 
    653 	argc = *argcp;
    654 	argv = *argvp;
    655 	maxargc = *maxargcp;
    656 
    657 	for (s = options; (p = strsep(&s, ",")) != NULL;) {
    658 		/* Always leave space for one more argument and the NULL. */
    659 		if (argc >= maxargc - 4) {
    660 			nargv = realloc(argv, (maxargc << 1) * sizeof(char *));
    661 			if (!nargv)
    662 				err(1, "realloc");
    663 			argv = nargv;
    664 			maxargc <<= 1;
    665 		}
    666 		if (*p != '\0') {
    667 			if (*p == '-') {
    668 				argv[argc++] = p;
    669 				p = strchr(p, '=');
    670 				if (p) {
    671 					*p = '\0';
    672 					argv[argc++] = p+1;
    673 				}
    674 			} else if (strcmp(p, "rw") != 0) {
    675 				argv[argc++] = "-o";
    676 				argv[argc++] = p;
    677 			}
    678 		}
    679 	}
    680 
    681 	*argcp = argc;
    682 	*argvp = argv;
    683 	*maxargcp = maxargc;
    684 }
    685 
    686 /* Deduce the filesystem type from the disk label. */
    687 static const char *
    688 getfslab(const char *str)
    689 {
    690 	static struct dkwedge_info dkw;
    691 	struct disklabel dl;
    692 	int fd;
    693 	int part;
    694 	const char *vfstype;
    695 	u_char fstype;
    696 	char buf[MAXPATHLEN + 1];
    697 	char *sp, *ep;
    698 
    699 	if ((fd = open(str, O_RDONLY)) == -1) {
    700 		/*
    701 		 * Iff we get EBUSY try the raw device. Since mount always uses
    702 		 * the block device we know we are never passed a raw device.
    703 		 */
    704 		if (errno != EBUSY)
    705 			err(1, "cannot open `%s'", str);
    706 		strlcpy(buf, str, MAXPATHLEN);
    707 		if ((sp = strrchr(buf, '/')) != NULL)
    708 			++sp;
    709 		else
    710 			sp = buf;
    711 		for (ep = sp + strlen(sp) + 1;  ep > sp; ep--)
    712 			*ep = *(ep - 1);
    713 		*sp = 'r';
    714 
    715 		/* Silently fail here - mount call can display error */
    716 		if ((fd = open(buf, O_RDONLY)) == -1)
    717 			return (NULL);
    718 	}
    719 
    720 	/* Check to see if this is a wedge. */
    721 	if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
    722 		/* Yup, this is easy. */
    723 		(void) close(fd);
    724 		return (dkw.dkw_ptype);
    725 	}
    726 
    727 	if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
    728 		(void) close(fd);
    729 		return (NULL);
    730 	}
    731 
    732 	(void) close(fd);
    733 
    734 	part = str[strlen(str) - 1] - 'a';
    735 
    736 	if (part < 0 || part >= dl.d_npartitions)
    737 		return (NULL);
    738 
    739 	/* Return NULL for unknown types - caller can fall back to ffs */
    740 	if ((fstype = dl.d_partitions[part].p_fstype) >= FSMAXMOUNTNAMES)
    741 		vfstype = NULL;
    742 	else
    743 		vfstype = mountnames[fstype];
    744 
    745 	return (vfstype);
    746 }
    747 
    748 static void
    749 usage(void)
    750 {
    751 
    752 	(void)fprintf(stderr,
    753 	    "usage: mount %s\n       mount %s\n       mount %s\n",
    754 	    "[-Aadfruvw] [-t type]",
    755 	    "[-dfruvw] special | node",
    756 	    "[-dfruvw] [-o options] [-t type] special node");
    757 	exit(1);
    758 	/* NOTREACHED */
    759 }
    760