Home | History | Annotate | Line # | Download | only in mount
mount.c revision 1.61
      1 /*	$NetBSD: mount.c,v 1.61 2002/09/21 18:43:32 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
     45 #else
     46 __RCSID("$NetBSD: mount.c,v 1.61 2002/09/21 18:43:32 christos Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/mount.h>
     52 #include <sys/wait.h>
     53 
     54 #include <err.h>
     55 #include <errno.h>
     56 #include <fstab.h>
     57 #include <pwd.h>
     58 #include <signal.h>
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 #include <unistd.h>
     63 
     64 #define MOUNTNAMES
     65 #include <fcntl.h>
     66 #include <sys/disklabel.h>
     67 #include <sys/ioctl.h>
     68 
     69 #include "pathnames.h"
     70 #include "vfslist.h"
     71 
     72 static int	debug, verbose;
     73 
     74 static void	catopt __P((char **, const char *));
     75 static const char *
     76 		getfslab __P((const char *str));
     77 static struct statfs *
     78 		getmntpt __P((const char *));
     79 static int 	getmntargs __P((struct statfs *, char *, size_t));
     80 static int	hasopt __P((const char *, const char *));
     81 static void	mangle __P((char *, int *, const char ***, int *));
     82 static int	mountfs __P((const char *, const char *, const char *,
     83 		    int, const char *, const char *, int, char *, size_t));
     84 static void	prmount __P((struct statfs *));
     85 static void	usage __P((void));
     86 
     87 #ifndef NO_MOUNT_PROGS
     88 void	checkname __P((int, char *[]));
     89 #endif
     90 int	main __P((int, char *[]));
     91 
     92 /* Map from mount otions to printable formats. */
     93 static const struct opt {
     94 	int o_opt;
     95 	int o_silent;
     96 	const char *o_name;
     97 } optnames[] = {
     98 	__MNT_FLAGS
     99 };
    100 
    101 static char ffs_fstype[] = "ffs";
    102 
    103 int
    104 main(argc, argv)
    105 	int argc;
    106 	char *argv[];
    107 {
    108 	const char *mntfromname, *mntonname, **vfslist, *vfstype;
    109 	struct fstab *fs;
    110 	struct statfs *mntbuf;
    111 	FILE *mountdfp;
    112 	int all, ch, forceall, i, init_flags, mntsize, rval;
    113 	char *options;
    114 	const char *mountopts, *fstypename;
    115 
    116 #ifndef NO_MOUNT_PROGS
    117 	/* if called as specific mount, call it's main mount routine */
    118 	checkname(argc, argv);
    119 #endif
    120 
    121 	/* started as "mount" */
    122 	all = forceall = init_flags = 0;
    123 	options = NULL;
    124 	vfslist = NULL;
    125 	vfstype = ffs_fstype;
    126 	while ((ch = getopt(argc, argv, "Aadfo:rwt:uv")) != -1)
    127 		switch (ch) {
    128 		case 'A':
    129 			all = forceall = 1;
    130 			break;
    131 		case 'a':
    132 			all = 1;
    133 			break;
    134 		case 'd':
    135 			debug = 1;
    136 			break;
    137 		case 'f':
    138 			init_flags |= MNT_FORCE;
    139 			break;
    140 		case 'o':
    141 			if (*optarg)
    142 				catopt(&options, optarg);
    143 			break;
    144 		case 'r':
    145 			init_flags |= MNT_RDONLY;
    146 			break;
    147 		case 't':
    148 			if (vfslist != NULL)
    149 				errx(1,
    150 				    "only one -t option may be specified.");
    151 			vfslist = makevfslist(optarg);
    152 			vfstype = optarg;
    153 			break;
    154 		case 'u':
    155 			init_flags |= MNT_UPDATE;
    156 			break;
    157 		case 'v':
    158 			verbose++;
    159 			break;
    160 		case 'w':
    161 			init_flags &= ~MNT_RDONLY;
    162 			break;
    163 		case '?':
    164 		default:
    165 			usage();
    166 			/* NOTREACHED */
    167 		}
    168 	argc -= optind;
    169 	argv += optind;
    170 
    171 #define	BADTYPE(type)							\
    172 	(strcmp(type, FSTAB_RO) &&					\
    173 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
    174 
    175 	rval = 0;
    176 	switch (argc) {
    177 	case 0:
    178 		if (all)
    179 			while ((fs = getfsent()) != NULL) {
    180 				if (BADTYPE(fs->fs_type))
    181 					continue;
    182 				if (checkvfsname(fs->fs_vfstype, vfslist))
    183 					continue;
    184 				if (hasopt(fs->fs_mntops, "noauto"))
    185 					continue;
    186 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
    187 				    fs->fs_file, init_flags, options,
    188 				    fs->fs_mntops, !forceall, NULL, 0))
    189 					rval = 1;
    190 			}
    191 		else {
    192 			if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
    193 				err(1, "getmntinfo");
    194 			for (i = 0; i < mntsize; i++) {
    195 				if (checkvfsname(mntbuf[i].f_fstypename,
    196 				    vfslist))
    197 					continue;
    198 				prmount(&mntbuf[i]);
    199 			}
    200 		}
    201 		exit(rval);
    202 		/* NOTREACHED */
    203 	case 1:
    204 		if (vfslist != NULL) {
    205 			usage();
    206 			/* NOTREACHED */
    207 		}
    208 
    209 		if (init_flags & MNT_UPDATE) {
    210 			if ((mntbuf = getmntpt(*argv)) == NULL)
    211 				errx(1,
    212 				    "unknown special file or file system %s.",
    213 				    *argv);
    214 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
    215 				mntfromname = fs->fs_spec;
    216 				/* ignore the fstab file options.  */
    217 				fs->fs_mntops = NULL;
    218 			} else
    219 				mntfromname = mntbuf->f_mntfromname;
    220 			mntonname  = mntbuf->f_mntonname;
    221 			fstypename = mntbuf->f_fstypename;
    222 			mountopts  = NULL;
    223 		} else {
    224 			if ((fs = getfsfile(*argv)) == NULL &&
    225 			    (fs = getfsspec(*argv)) == NULL)
    226 				errx(1,
    227 				    "%s: unknown special file or file system.",
    228 				    *argv);
    229 			if (BADTYPE(fs->fs_type))
    230 				errx(1, "%s has unknown file system type.",
    231 				    *argv);
    232 			mntfromname = fs->fs_spec;
    233 			mntonname   = fs->fs_file;
    234 			fstypename  = fs->fs_vfstype;
    235 			mountopts   = fs->fs_mntops;
    236 		}
    237 		rval = mountfs(fstypename, mntfromname,
    238 		    mntonname, init_flags, options, mountopts, 0, NULL, 0);
    239 		break;
    240 	case 2:
    241 		/*
    242 		 * If -t flag has not been specified, and spec contains either
    243 		 * a ':' or a '@' then assume that an NFS filesystem is being
    244 		 * specified ala Sun.
    245 		 */
    246 		if (vfslist == NULL) {
    247 			if (strpbrk(argv[0], ":@") != NULL)
    248 				vfstype = "nfs";
    249 			else {
    250 				vfstype = getfslab(argv[0]);
    251 				if (vfstype == NULL)
    252 					vfstype = ffs_fstype;
    253 			}
    254 		}
    255 		rval = mountfs(vfstype,
    256 		    argv[0], argv[1], init_flags, options, NULL, 0, NULL, 0);
    257 		break;
    258 	default:
    259 		usage();
    260 		/* NOTREACHED */
    261 	}
    262 
    263 	/*
    264 	 * If the mount was successfully, and done by root, tell mountd the
    265 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
    266 	 */
    267 	if (rval == 0 && getuid() == 0 &&
    268 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
    269 		int pid;
    270 
    271 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
    272 		    pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
    273 			err(1, "signal mountd");
    274 		(void)fclose(mountdfp);
    275 	}
    276 
    277 	exit(rval);
    278 	/* NOTREACHED */
    279 }
    280 
    281 int
    282 hasopt(mntopts, option)
    283 	const char *mntopts, *option;
    284 {
    285 	int negative, found;
    286 	char *opt, *optbuf;
    287 
    288 	if (option[0] == 'n' && option[1] == 'o') {
    289 		negative = 1;
    290 		option += 2;
    291 	} else
    292 		negative = 0;
    293 	optbuf = strdup(mntopts);
    294 	found = 0;
    295 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
    296 		if (opt[0] == 'n' && opt[1] == 'o') {
    297 			if (!strcasecmp(opt + 2, option))
    298 				found = negative;
    299 		} else if (!strcasecmp(opt, option))
    300 			found = !negative;
    301 	}
    302 	free(optbuf);
    303 	return (found);
    304 }
    305 
    306 static int
    307 mountfs(vfstype, spec, name, flags, options, mntopts, skipmounted, buf, buflen)
    308 	const char *vfstype, *spec, *name, *options, *mntopts;
    309 	int flags, skipmounted;
    310 	char *buf;
    311 	size_t buflen;
    312 {
    313 	/* List of directories containing mount_xxx subcommands. */
    314 	static const char *edirs[] = {
    315 #ifdef _PATH_RESCUE
    316 		_PATH_RESCUE,
    317 #endif
    318 		_PATH_SBIN,
    319 		_PATH_USRSBIN,
    320 		NULL
    321 	};
    322 	const char **argv, **edir;
    323 	struct statfs *sfp, sf;
    324 	pid_t pid;
    325 	int pfd[2];
    326 	int argc, numfs, i, status, maxargc;
    327 	char *optbuf, execname[MAXPATHLEN + 1], execbase[MAXPATHLEN],
    328 	    mntpath[MAXPATHLEN];
    329 
    330 #ifdef __GNUC__
    331 	(void) &name;
    332 	(void) &optbuf;
    333 	(void) &vfstype;
    334 #endif
    335 
    336 	if (realpath(name, mntpath) == NULL) {
    337 		warn("realpath %s", name);
    338 		return (1);
    339 	}
    340 
    341 	name = mntpath;
    342 
    343 	optbuf = NULL;
    344 	if (mntopts)
    345 		catopt(&optbuf, mntopts);
    346 	if (options)
    347 		catopt(&optbuf, options);
    348 	if (!mntopts && !options)
    349 		catopt(&optbuf, "rw");
    350 
    351 	if (!strcmp(name, "/"))
    352 		flags |= MNT_UPDATE;
    353 	else if (skipmounted) {
    354 		if ((numfs = getmntinfo(&sfp, MNT_WAIT)) == 0) {
    355 			warn("getmntinfo");
    356 			return (1);
    357 		}
    358 		for(i = 0; i < numfs; i++) {
    359 			/*
    360 			 * XXX can't check f_mntfromname,
    361 			 * thanks to mfs, union, etc.
    362 			 */
    363 			if (strncmp(name, sfp[i].f_mntonname, MNAMELEN) == 0 &&
    364 			    strncmp(vfstype, sfp[i].f_fstypename,
    365 				MFSNAMELEN) == 0) {
    366 				if (verbose)
    367 					(void)printf("%s on %s type %.*s: "
    368 					    "%s\n",
    369 					    sfp[i].f_mntfromname,
    370 					    sfp[i].f_mntonname,
    371 					    MFSNAMELEN,
    372 					    sfp[i].f_fstypename,
    373 					    "already mounted");
    374 				return (0);
    375 			}
    376 		}
    377 	}
    378 	if (flags & MNT_FORCE)
    379 		catopt(&optbuf, "force");
    380 	if (flags & MNT_RDONLY)
    381 		catopt(&optbuf, "ro");
    382 
    383 	if (flags & MNT_UPDATE) {
    384 		catopt(&optbuf, "update");
    385 		/* Figure out the fstype only if we defaulted to ffs */
    386 		if (vfstype == ffs_fstype && statfs(name, &sf) != -1)
    387 			vfstype = sf.f_fstypename;
    388 	}
    389 
    390 	maxargc = 64;
    391 	argv = malloc(sizeof(char *) * maxargc);
    392 
    393 	(void) snprintf(execbase, sizeof(execbase), "mount_%s", vfstype);
    394 	argc = 0;
    395 	argv[argc++] = execbase;
    396 	if (optbuf)
    397 		mangle(optbuf, &argc, &argv, &maxargc);
    398 	argv[argc++] = spec;
    399 	argv[argc++] = name;
    400 	argv[argc] = NULL;
    401 
    402 	if (verbose && buf == NULL) {
    403 		(void)printf("exec:");
    404 		for (i = 0; i < argc; i++)
    405 			(void)printf(" %s", argv[i]);
    406 		(void)printf("\n");
    407 	}
    408 
    409 	if (buf) {
    410 		if (pipe(pfd) == -1)
    411 			warn("Cannot create pipe");
    412 	}
    413 
    414 	switch (pid = vfork()) {
    415 	case -1:				/* Error. */
    416 		warn("vfork");
    417 		if (optbuf)
    418 			free(optbuf);
    419 		return (1);
    420 
    421 	case 0:					/* Child. */
    422 		if (debug)
    423 			_exit(0);
    424 
    425 		if (buf) {
    426 			(void)close(pfd[0]);
    427 			(void)close(STDOUT_FILENO);
    428 			if (dup2(pfd[1], STDOUT_FILENO) == -1)
    429 				warn("Cannot open fd to mount program");
    430 		}
    431 
    432 		/* Go find an executable. */
    433 		edir = edirs;
    434 		do {
    435 			(void)snprintf(execname,
    436 			    sizeof(execname), "%s/%s", *edir, execbase);
    437 			(void)execv(execname, (char * const *)argv);
    438 			if (errno != ENOENT)
    439 				warn("exec %s for %s", execname, name);
    440 		} while (*++edir != NULL);
    441 
    442 		if (errno == ENOENT)
    443 			warnx("%s not found for %s", execbase, name);
    444 		_exit(1);
    445 		/* NOTREACHED */
    446 
    447 	default:				/* Parent. */
    448 		if (optbuf)
    449 			free(optbuf);
    450 
    451 		if (buf || strstr(options, "getargs") != NULL) {
    452 			char tbuf[1024], *ptr;
    453 			int nread;
    454 			if (buf == NULL) {
    455 				ptr = tbuf;
    456 				buflen = sizeof(tbuf) - 1;
    457 			} else {
    458 				ptr = buf;
    459 				buflen--;
    460 			}
    461 			(void)close(pfd[1]);
    462 			(void)signal(SIGPIPE, SIG_IGN);
    463 			while ((nread = read(pfd[0], ptr, buflen)) > 0) {
    464 				buflen -= nread;
    465 				ptr += nread;
    466 			}
    467 			*ptr = '\0';
    468 			if (buflen == 0) {
    469 				while (read(pfd[0], &nread, sizeof(nread)) > 0)
    470 					continue;
    471 			}
    472 			if (buf == NULL)
    473 			    (void)fprintf(stdout, "%s", tbuf);
    474 		}
    475 
    476 		if (waitpid(pid, &status, 0) < 0) {
    477 			warn("waitpid");
    478 			return (1);
    479 		}
    480 
    481 		if (WIFEXITED(status)) {
    482 			if (WEXITSTATUS(status) != 0)
    483 				return (WEXITSTATUS(status));
    484 		} else if (WIFSIGNALED(status)) {
    485 			warnx("%s: %s", name, strsignal(WTERMSIG(status)));
    486 			return (1);
    487 		}
    488 
    489 		if (buf == NULL) {
    490 			if (verbose) {
    491 				if (statfs(name, &sf) < 0) {
    492 					warn("statfs %s", name);
    493 					return (1);
    494 				}
    495 				prmount(&sf);
    496 			}
    497 		}
    498 		break;
    499 	}
    500 
    501 	return (0);
    502 }
    503 
    504 static void
    505 prmount(sfp)
    506 	struct statfs *sfp;
    507 {
    508 	int flags;
    509 	const struct opt *o;
    510 	struct passwd *pw;
    511 	int f;
    512 
    513 	(void)printf("%s on %s type %.*s", sfp->f_mntfromname,
    514 	    sfp->f_mntonname, MFSNAMELEN, sfp->f_fstypename);
    515 
    516 	flags = sfp->f_flags & MNT_VISFLAGMASK;
    517 	for (f = 0, o = optnames; flags && o <
    518 	    &optnames[sizeof(optnames)/sizeof(optnames[0])]; o++)
    519 		if (flags & o->o_opt) {
    520 			if (!o->o_silent || verbose)
    521 				(void)printf("%s%s", !f++ ? " (" : ", ",
    522 				    o->o_name);
    523 			flags &= ~o->o_opt;
    524 		}
    525 	if (flags)
    526 		(void)printf("%sunknown flag%s %#x", !f++ ? " (" : ", ",
    527 		    flags & (flags - 1) ? "s" : "", flags);
    528 	if (sfp->f_owner) {
    529 		(void)printf("%smounted by ", !f++ ? " (" : ", ");
    530 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
    531 			(void)printf("%s", pw->pw_name);
    532 		else
    533 			(void)printf("%d", sfp->f_owner);
    534 	}
    535 	if (verbose) {
    536 		(void)printf("%swrites: sync %ld async %ld",
    537 		    !f++ ? " (" : ", ", sfp->f_syncwrites, sfp->f_asyncwrites);
    538 		if (verbose > 1) {
    539 			char buf[2048];
    540 			if (getmntargs(sfp, buf, sizeof(buf)))
    541 				printf(", [%s: %s])\n", sfp->f_fstypename, buf);
    542 			else
    543 				printf(")\n");
    544 		}
    545 	} else
    546 		(void)printf("%s", f ? ")\n" : "\n");
    547 }
    548 
    549 static int
    550 getmntargs(sfs, buf, buflen)
    551 	struct statfs *sfs;
    552 	char *buf;
    553 	size_t buflen;
    554 {
    555 	if (mountfs(sfs->f_fstypename, sfs->f_mntfromname, sfs->f_mntonname, 0,
    556 	    "getargs", NULL, 0, buf, buflen))
    557 		return 0;
    558 	else {
    559 		if (*buf == '\0')
    560 			return 0;
    561 		if ((buf = strchr(buf, '\n')) != NULL)
    562 			*buf = '\0';
    563 		return 1;
    564 	}
    565 }
    566 
    567 static struct statfs *
    568 getmntpt(name)
    569 	const char *name;
    570 {
    571 	struct statfs *mntbuf;
    572 	int i, mntsize;
    573 
    574 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    575 	for (i = 0; i < mntsize; i++)
    576 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
    577 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
    578 			return (&mntbuf[i]);
    579 	return (NULL);
    580 }
    581 
    582 static void
    583 catopt(sp, o)
    584 	char **sp;
    585 	const char *o;
    586 {
    587 	char *s;
    588 	size_t i, j;
    589 
    590 	s = *sp;
    591 	if (s) {
    592 		i = strlen(s);
    593 		j = i + 1 + strlen(o) + 1;
    594 		s = realloc(s, j);
    595 		(void)snprintf(s + i, j, ",%s", o);
    596 	} else
    597 		s = strdup(o);
    598 	*sp = s;
    599 }
    600 
    601 static void
    602 mangle(options, argcp, argvp, maxargcp)
    603 	char *options;
    604 	int *argcp, *maxargcp;
    605 	const char ***argvp;
    606 {
    607 	char *p, *s;
    608 	int argc, maxargc;
    609 	const char **argv;
    610 
    611 	argc = *argcp;
    612 	argv = *argvp;
    613 	maxargc = *maxargcp;
    614 
    615 	for (s = options; (p = strsep(&s, ",")) != NULL;) {
    616 		/* Always leave space for one more argument and the NULL. */
    617 		if (argc >= maxargc - 4) {
    618 			maxargc <<= 1;
    619 			argv = realloc(argv, maxargc * sizeof(char *));
    620 		}
    621 		if (*p != '\0') {
    622 			if (*p == '-') {
    623 				argv[argc++] = p;
    624 				p = strchr(p, '=');
    625 				if (p) {
    626 					*p = '\0';
    627 					argv[argc++] = p+1;
    628 				}
    629 			} else if (strcmp(p, "rw") != 0) {
    630 				argv[argc++] = "-o";
    631 				argv[argc++] = p;
    632 			}
    633 		}
    634 	}
    635 
    636 	*argcp = argc;
    637 	*argvp = argv;
    638 	*maxargcp = maxargc;
    639 }
    640 
    641 /* Deduce the filesystem type from the disk label. */
    642 static const char *
    643 getfslab(str)
    644 	const char *str;
    645 {
    646 	struct disklabel dl;
    647 	int fd;
    648 	int part;
    649 	const char *vfstype;
    650 	u_char fstype;
    651 	char buf[MAXPATHLEN + 1];
    652 	char *sp, *ep;
    653 
    654 	if ((fd = open(str, O_RDONLY)) == -1) {
    655 		/*
    656 		 * Iff we get EBUSY try the raw device. Since mount always uses
    657 		 * the block device we know we are never passed a raw device.
    658 		 */
    659 		if (errno != EBUSY)
    660 			err(1, "cannot open `%s'", str);
    661 		strlcpy(buf, str, MAXPATHLEN);
    662 		if ((sp = strrchr(buf, '/')) != NULL)
    663 			++sp;
    664 		else
    665 			sp = buf;
    666 		for (ep = sp + strlen(sp) + 1;  ep > sp; ep--)
    667 			*ep = *(ep - 1);
    668 		*sp = 'r';
    669 
    670 		/* Silently fail here - mount call can display error */
    671 		if ((fd = open(buf, O_RDONLY)) == -1)
    672 			return (NULL);
    673 	}
    674 
    675 	if (ioctl(fd, DIOCGDINFO, &dl) == -1) {
    676 		(void) close(fd);
    677 		return (NULL);
    678 	}
    679 
    680 	(void) close(fd);
    681 
    682 	part = str[strlen(str) - 1] - 'a';
    683 
    684 	if (part < 0 || part >= dl.d_npartitions)
    685 		return (NULL);
    686 
    687 	/* Return NULL for unknown types - caller can fall back to ffs */
    688 	if ((fstype = dl.d_partitions[part].p_fstype) >= FSMAXMOUNTNAMES)
    689 		vfstype = NULL;
    690 	else
    691 		vfstype = mountnames[fstype];
    692 
    693 	return (vfstype);
    694 }
    695 
    696 static void
    697 usage()
    698 {
    699 
    700 	(void)fprintf(stderr,
    701 	    "usage: mount %s\n       mount %s\n       mount %s\n",
    702 	    "[-Aadfruvw] [-t type]",
    703 	    "[-dfruvw] special | node",
    704 	    "[-dfruvw] [-o options] [-t type] special node");
    705 	exit(1);
    706 	/* NOTREACHED */
    707 }
    708