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