Home | History | Annotate | Line # | Download | only in mount
mount.c revision 1.70
      1 /*	$NetBSD: mount.c,v 1.70 2004/04/22 10:17:00 hannken 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.70 2004/04/22 10:17:00 hannken 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/disklabel.h>
     63 #include <sys/ioctl.h>
     64 
     65 #include "pathnames.h"
     66 #include "vfslist.h"
     67 
     68 static int	debug, verbose;
     69 
     70 static void	catopt __P((char **, const char *));
     71 static const char *
     72 		getfslab __P((const char *str));
     73 static struct statvfs *
     74 		getmntpt __P((const char *));
     75 static int 	getmntargs __P((struct statvfs *, char *, size_t));
     76 static int	hasopt __P((const char *, const char *));
     77 static void	mangle __P((char *, int *, const char ***, int *));
     78 static int	mountfs __P((const char *, const char *, const char *,
     79 		    int, const char *, const char *, int, char *, size_t));
     80 static void	prmount __P((struct statvfs *));
     81 static void	usage __P((void));
     82 
     83 int	main __P((int, char *[]));
     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(argc, argv)
     98 	int argc;
     99 	char *argv[];
    100 {
    101 	const char *mntfromname, *mntonname, **vfslist, *vfstype;
    102 	struct fstab *fs;
    103 	struct statvfs *mntbuf;
    104 	FILE *mountdfp;
    105 	int all, ch, forceall, i, init_flags, mntsize, rval;
    106 	char *options;
    107 	const char *mountopts, *fstypename;
    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 		if (init_flags & MNT_UPDATE) {
    206 			if ((mntbuf = getmntpt(*argv)) == NULL)
    207 				errx(1,
    208 				    "unknown special file or file system %s.",
    209 				    *argv);
    210 			mntfromname = mntbuf->f_mntfromname;
    211 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
    212 				if (strcmp(fs->fs_spec, "from_mount") != 0)
    213 					mntfromname = fs->fs_spec;
    214 				/* ignore the fstab file options.  */
    215 				fs->fs_mntops = NULL;
    216 			}
    217 			mntonname  = mntbuf->f_mntonname;
    218 			fstypename = mntbuf->f_fstypename;
    219 			mountopts  = NULL;
    220 		} else {
    221 			if ((fs = getfsfile(*argv)) == NULL &&
    222 			    (fs = getfsspec(*argv)) == NULL)
    223 				errx(1,
    224 				    "%s: unknown special file or file system.",
    225 				    *argv);
    226 			if (BADTYPE(fs->fs_type))
    227 				errx(1, "%s has unknown file system type.",
    228 				    *argv);
    229 			if (strcmp(fs->fs_spec, "from_mount") == 0) {
    230 				if ((mntbuf = getmntpt(*argv)) == NULL)
    231 					errx(1,
    232 					    "unknown special file or file system %s.",
    233 					    *argv);
    234 				mntfromname = mntbuf->f_mntfromname;
    235 			} else
    236 				mntfromname = fs->fs_spec;
    237 			mntonname   = fs->fs_file;
    238 			fstypename  = fs->fs_vfstype;
    239 			mountopts   = fs->fs_mntops;
    240 		}
    241 		rval = mountfs(fstypename, mntfromname,
    242 		    mntonname, init_flags, options, mountopts, 0, NULL, 0);
    243 		break;
    244 	case 2:
    245 		/*
    246 		 * If -t flag has not been specified, and spec contains either
    247 		 * a ':' or a '@' then assume that an NFS filesystem is being
    248 		 * specified ala Sun.
    249 		 */
    250 		if (vfslist == NULL) {
    251 			if (strpbrk(argv[0], ":@") != NULL)
    252 				vfstype = "nfs";
    253 			else {
    254 				vfstype = getfslab(argv[0]);
    255 				if (vfstype == NULL)
    256 					vfstype = ffs_fstype;
    257 			}
    258 		}
    259 		rval = mountfs(vfstype,
    260 		    argv[0], argv[1], init_flags, options, NULL, 0, NULL, 0);
    261 		break;
    262 	default:
    263 		usage();
    264 		/* NOTREACHED */
    265 	}
    266 
    267 	/*
    268 	 * If the mount was successfully, and done by root, tell mountd the
    269 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
    270 	 */
    271 	if (rval == 0 && getuid() == 0 &&
    272 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
    273 		int pid;
    274 
    275 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
    276 		    pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
    277 			err(1, "signal mountd");
    278 		(void)fclose(mountdfp);
    279 	}
    280 
    281 	exit(rval);
    282 	/* NOTREACHED */
    283 }
    284 
    285 int
    286 hasopt(mntopts, option)
    287 	const char *mntopts, *option;
    288 {
    289 	int negative, found;
    290 	char *opt, *optbuf;
    291 
    292 	if (option[0] == 'n' && option[1] == 'o') {
    293 		negative = 1;
    294 		option += 2;
    295 	} else
    296 		negative = 0;
    297 	optbuf = strdup(mntopts);
    298 	found = 0;
    299 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
    300 		if (opt[0] == 'n' && opt[1] == 'o') {
    301 			if (!strcasecmp(opt + 2, option))
    302 				found = negative;
    303 		} else if (!strcasecmp(opt, option))
    304 			found = !negative;
    305 	}
    306 	free(optbuf);
    307 	return (found);
    308 }
    309 
    310 static int
    311 mountfs(vfstype, spec, name, flags, options, mntopts, skipmounted, buf, buflen)
    312 	const char *vfstype, *spec, *name, *options, *mntopts;
    313 	int flags, skipmounted;
    314 	char *buf;
    315 	size_t buflen;
    316 {
    317 	/* List of directories containing mount_xxx subcommands. */
    318 	static const char *edirs[] = {
    319 #ifdef _PATH_RESCUE
    320 		_PATH_RESCUE,
    321 #endif
    322 		_PATH_SBIN,
    323 		_PATH_USRSBIN,
    324 		NULL
    325 	};
    326 	const char **argv, **edir;
    327 	struct statvfs *sfp, sf;
    328 	pid_t pid;
    329 	int pfd[2];
    330 	int argc, numfs, i, status, maxargc;
    331 	char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN],
    332 	    mntpath[MAXPATHLEN];
    333 
    334 #ifdef __GNUC__
    335 	(void) &name;
    336 	(void) &optbuf;
    337 	(void) &vfstype;
    338 #endif
    339 
    340 	if (realpath(name, mntpath) == NULL) {
    341 		warn("realpath %s", name);
    342 		return (1);
    343 	}
    344 
    345 	name = mntpath;
    346 
    347 	optbuf = NULL;
    348 	if (mntopts)
    349 		catopt(&optbuf, mntopts);
    350 	if (options)
    351 		catopt(&optbuf, options);
    352 	if (!mntopts && !options)
    353 		catopt(&optbuf, "rw");
    354 
    355 	if (!strcmp(name, "/"))
    356 		flags |= MNT_UPDATE;
    357 	else if (skipmounted) {
    358 		if ((numfs = getmntinfo(&sfp, MNT_WAIT)) == 0) {
    359 			warn("getmntinfo");
    360 			return (1);
    361 		}
    362 		for(i = 0; i < numfs; i++) {
    363 			/*
    364 			 * XXX can't check f_mntfromname,
    365 			 * thanks to mfs, union, etc.
    366 			 */
    367 			if (strncmp(name, sfp[i].f_mntonname, MNAMELEN) == 0 &&
    368 			    strncmp(vfstype, sfp[i].f_fstypename,
    369 				MFSNAMELEN) == 0) {
    370 				if (verbose)
    371 					(void)printf("%s on %s type %.*s: "
    372 					    "%s\n",
    373 					    sfp[i].f_mntfromname,
    374 					    sfp[i].f_mntonname,
    375 					    MFSNAMELEN,
    376 					    sfp[i].f_fstypename,
    377 					    "already mounted");
    378 				return (0);
    379 			}
    380 		}
    381 	}
    382 	if (flags & MNT_FORCE)
    383 		catopt(&optbuf, "force");
    384 	if (flags & MNT_RDONLY)
    385 		catopt(&optbuf, "ro");
    386 
    387 	if (flags & MNT_UPDATE) {
    388 		catopt(&optbuf, "update");
    389 		/* Figure out the fstype only if we defaulted to ffs */
    390 		if (vfstype == ffs_fstype && statvfs(name, &sf) != -1)
    391 			vfstype = sf.f_fstypename;
    392 	}
    393 
    394 	maxargc = 64;
    395 	argv = malloc(sizeof(char *) * maxargc);
    396 
    397 	(void) snprintf(execbase, sizeof(execbase), "mount_%s", vfstype);
    398 	argc = 0;
    399 	argv[argc++] = execbase;
    400 	if (optbuf)
    401 		mangle(optbuf, &argc, &argv, &maxargc);
    402 	argv[argc++] = spec;
    403 	argv[argc++] = name;
    404 	argv[argc] = NULL;
    405 
    406 	if (verbose && buf == NULL) {
    407 		(void)printf("exec:");
    408 		for (i = 0; i < argc; i++)
    409 			(void)printf(" %s", argv[i]);
    410 		(void)printf("\n");
    411 	}
    412 
    413 	if (buf) {
    414 		if (pipe(pfd) == -1)
    415 			warn("Cannot create pipe");
    416 	}
    417 
    418 	switch (pid = vfork()) {
    419 	case -1:				/* Error. */
    420 		warn("vfork");
    421 		if (optbuf)
    422 			free(optbuf);
    423 		return (1);
    424 
    425 	case 0:					/* Child. */
    426 		if (debug)
    427 			_exit(0);
    428 
    429 		if (buf) {
    430 			(void)close(pfd[0]);
    431 			(void)close(STDOUT_FILENO);
    432 			if (dup2(pfd[1], STDOUT_FILENO) == -1)
    433 				warn("Cannot open fd to mount program");
    434 		}
    435 
    436 		/* Go find an executable. */
    437 		edir = edirs;
    438 		do {
    439 			(void)snprintf(execname,
    440 			    sizeof(execname), "%s/%s", *edir, execbase);
    441 			(void)execv(execname, (char * const *)argv);
    442 			if (errno != ENOENT)
    443 				warn("exec %s for %s", execname, name);
    444 		} while (*++edir != NULL);
    445 
    446 		if (errno == ENOENT)
    447 			warnx("%s not found for %s", execbase, name);
    448 		_exit(1);
    449 		/* NOTREACHED */
    450 
    451 	default:				/* Parent. */
    452 		if (optbuf)
    453 			free(optbuf);
    454 
    455 		if (buf || (options != NULL &&
    456 		    strstr(options, "getargs") != NULL)) {
    457 			char tbuf[1024], *ptr;
    458 			int nread;
    459 
    460 			if (buf == NULL) {
    461 				ptr = tbuf;
    462 				buflen = sizeof(tbuf) - 1;
    463 			} else {
    464 				ptr = buf;
    465 				buflen--;
    466 			}
    467 			(void)close(pfd[1]);
    468 			(void)signal(SIGPIPE, SIG_IGN);
    469 			while ((nread = read(pfd[0], ptr, buflen)) > 0) {
    470 				buflen -= nread;
    471 				ptr += nread;
    472 			}
    473 			*ptr = '\0';
    474 			if (buflen == 0) {
    475 				while (read(pfd[0], &nread, sizeof(nread)) > 0)
    476 					continue;
    477 			}
    478 			if (buf == NULL)
    479 				(void)fprintf(stdout, "%s", tbuf);
    480 		}
    481 
    482 		if (waitpid(pid, &status, 0) < 0) {
    483 			warn("waitpid");
    484 			return (1);
    485 		}
    486 
    487 		if (WIFEXITED(status)) {
    488 			if (WEXITSTATUS(status) != 0)
    489 				return (WEXITSTATUS(status));
    490 		} else if (WIFSIGNALED(status)) {
    491 			warnx("%s: %s", name, strsignal(WTERMSIG(status)));
    492 			return (1);
    493 		}
    494 
    495 		if (buf == NULL) {
    496 			if (verbose) {
    497 				if (statvfs(name, &sf) < 0) {
    498 					warn("statvfs %s", name);
    499 					return (1);
    500 				}
    501 				prmount(&sf);
    502 			}
    503 		}
    504 		break;
    505 	}
    506 
    507 	return (0);
    508 }
    509 
    510 static void
    511 prmount(sfp)
    512 	struct statvfs *sfp;
    513 {
    514 	int flags;
    515 	const struct opt *o;
    516 	struct passwd *pw;
    517 	int f;
    518 
    519 	(void)printf("%s on %s type %.*s", sfp->f_mntfromname,
    520 	    sfp->f_mntonname, MFSNAMELEN, sfp->f_fstypename);
    521 
    522 	flags = sfp->f_flag & MNT_VISFLAGMASK;
    523 	for (f = 0, o = optnames; flags && o <
    524 	    &optnames[sizeof(optnames)/sizeof(optnames[0])]; o++)
    525 		if (flags & o->o_opt) {
    526 			if (!o->o_silent || verbose)
    527 				(void)printf("%s%s", !f++ ? " (" : ", ",
    528 				    o->o_name);
    529 			flags &= ~o->o_opt;
    530 		}
    531 	if (flags)
    532 		(void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
    533 		    flags & (flags - 1) ? "s" : "", flags);
    534 	if (sfp->f_owner) {
    535 		(void)printf("%smounted by ", !f++ ? " (" : ", ");
    536 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
    537 			(void)printf("%s", pw->pw_name);
    538 		else
    539 			(void)printf("%d", sfp->f_owner);
    540 	}
    541 	if (verbose) {
    542 		(void)printf("%s", !f++ ? " (" : ", ");
    543 		(void)printf("reads: sync %" PRIu64 " async %" PRIu64 "",
    544 		    sfp->f_syncreads, sfp->f_asyncreads);
    545 		(void)printf(", writes: sync %" PRIu64 " async %" PRIu64 "",
    546 		    sfp->f_syncwrites, sfp->f_asyncwrites);
    547 		if (verbose > 1) {
    548 			char buf[2048];
    549 
    550 			if (getmntargs(sfp, buf, sizeof(buf)))
    551 				printf(", [%s: %s]", sfp->f_fstypename, buf);
    552 		}
    553 		printf(")\n");
    554 	} else
    555 		(void)printf("%s", f ? ")\n" : "\n");
    556 }
    557 
    558 static int
    559 getmntargs(sfs, buf, buflen)
    560 	struct statvfs *sfs;
    561 	char *buf;
    562 	size_t buflen;
    563 {
    564 
    565 	if (mountfs(sfs->f_fstypename, sfs->f_mntfromname, sfs->f_mntonname, 0,
    566 	    "getargs", NULL, 0, buf, buflen))
    567 		return (0);
    568 	else {
    569 		if (*buf == '\0')
    570 			return (0);
    571 		if ((buf = strchr(buf, '\n')) != NULL)
    572 			*buf = '\0';
    573 		return (1);
    574 	}
    575 }
    576 
    577 static struct statvfs *
    578 getmntpt(name)
    579 	const char *name;
    580 {
    581 	struct statvfs *mntbuf;
    582 	int i, mntsize;
    583 
    584 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    585 	for (i = 0; i < mntsize; i++)
    586 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
    587 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
    588 			return (&mntbuf[i]);
    589 	return (NULL);
    590 }
    591 
    592 static void
    593 catopt(sp, o)
    594 	char **sp;
    595 	const char *o;
    596 {
    597 	char *s, *n;
    598 
    599 	s = *sp;
    600 	if (s) {
    601 		if (asprintf(&n, "%s,%s", s, o) < 0)
    602 			err(1, "asprintf");
    603 		free(s);
    604 		s = n;
    605 	} else
    606 		s = strdup(o);
    607 	*sp = s;
    608 }
    609 
    610 static void
    611 mangle(options, argcp, argvp, maxargcp)
    612 	char *options;
    613 	int *argcp, *maxargcp;
    614 	const char ***argvp;
    615 {
    616 	char *p, *s;
    617 	int argc, maxargc;
    618 	const char **argv, **nargv;
    619 
    620 	argc = *argcp;
    621 	argv = *argvp;
    622 	maxargc = *maxargcp;
    623 
    624 	for (s = options; (p = strsep(&s, ",")) != NULL;) {
    625 		/* Always leave space for one more argument and the NULL. */
    626 		if (argc >= maxargc - 4) {
    627 			nargv = realloc(argv, (maxargc << 1) * sizeof(char *));
    628 			if (!nargv)
    629 				err(1, "realloc");
    630 			argv = nargv;
    631 			maxargc <<= 1;
    632 		}
    633 		if (*p != '\0') {
    634 			if (*p == '-') {
    635 				argv[argc++] = p;
    636 				p = strchr(p, '=');
    637 				if (p) {
    638 					*p = '\0';
    639 					argv[argc++] = p+1;
    640 				}
    641 			} else if (strcmp(p, "rw") != 0) {
    642 				argv[argc++] = "-o";
    643 				argv[argc++] = p;
    644 			}
    645 		}
    646 	}
    647 
    648 	*argcp = argc;
    649 	*argvp = argv;
    650 	*maxargcp = maxargc;
    651 }
    652 
    653 /* Deduce the filesystem type from the disk label. */
    654 static const char *
    655 getfslab(str)
    656 	const char *str;
    657 {
    658 	struct disklabel dl;
    659 	int fd;
    660 	int part;
    661 	const char *vfstype;
    662 	u_char fstype;
    663 	char buf[MAXPATHLEN + 1];
    664 	char *sp, *ep;
    665 
    666 	if ((fd = open(str, O_RDONLY)) == -1) {
    667 		/*
    668 		 * Iff we get EBUSY try the raw device. Since mount always uses
    669 		 * the block device we know we are never passed a raw device.
    670 		 */
    671 		if (errno != EBUSY)
    672 			err(1, "cannot open `%s'", str);
    673 		strlcpy(buf, str, MAXPATHLEN);
    674 		if ((sp = strrchr(buf, '/')) != NULL)
    675 			++sp;
    676 		else
    677 			sp = buf;
    678 		for (ep = sp + strlen(sp) + 1;  ep > sp; ep--)
    679 			*ep = *(ep - 1);
    680 		*sp = 'r';
    681 
    682 		/* Silently fail here - mount call can display error */
    683 		if ((fd = open(buf, O_RDONLY)) == -1)
    684 			return (NULL);
    685 	}
    686 
    687 	if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
    688 		(void) close(fd);
    689 		return (NULL);
    690 	}
    691 
    692 	(void) close(fd);
    693 
    694 	part = str[strlen(str) - 1] - 'a';
    695 
    696 	if (part < 0 || part >= dl.d_npartitions)
    697 		return (NULL);
    698 
    699 	/* Return NULL for unknown types - caller can fall back to ffs */
    700 	if ((fstype = dl.d_partitions[part].p_fstype) >= FSMAXMOUNTNAMES)
    701 		vfstype = NULL;
    702 	else
    703 		vfstype = mountnames[fstype];
    704 
    705 	return (vfstype);
    706 }
    707 
    708 static void
    709 usage()
    710 {
    711 
    712 	(void)fprintf(stderr,
    713 	    "usage: mount %s\n       mount %s\n       mount %s\n",
    714 	    "[-Aadfruvw] [-t type]",
    715 	    "[-dfruvw] special | node",
    716 	    "[-dfruvw] [-o options] [-t type] special node");
    717 	exit(1);
    718 	/* NOTREACHED */
    719 }
    720