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