xref: /freebsd/sbin/mount/mount.c (revision 9086e0e06819bdf7290dc15cc04985fe8e66a711)
1 /*-
2  * Copyright (c) 1980, 1989, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
33 	The Regents of the University of California.  All rights reserved.\n";
34 #if 0
35 static char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
36 #endif
37 #endif /* not lint */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 #include <sys/stat.h>
45 #include <sys/wait.h>
46 
47 #include <ctype.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <fstab.h>
51 #include <paths.h>
52 #include <pwd.h>
53 #include <signal.h>
54 #include <stdint.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 #include <libutil.h>
60 
61 #include "extern.h"
62 #include "mntopts.h"
63 #include "pathnames.h"
64 
65 /* `meta' options */
66 #define MOUNT_META_OPTION_FSTAB		"fstab"
67 #define MOUNT_META_OPTION_CURRENT	"current"
68 
69 static int debug, fstab_style, verbose;
70 
71 struct cpa {
72 	char	**a;
73 	ssize_t	sz;
74 	int	c;
75 };
76 
77 char   *catopt(char *, const char *);
78 struct statfs *getmntpt(const char *);
79 int	hasopt(const char *, const char *);
80 int	ismounted(struct fstab *, struct statfs *, int);
81 int	isremountable(const char *);
82 void	mangle(char *, struct cpa *);
83 char   *update_options(char *, char *, int);
84 int	mountfs(const char *, const char *, const char *,
85 			int, const char *, const char *);
86 void	remopt(char *, const char *);
87 void	prmount(struct statfs *);
88 void	putfsent(struct statfs *);
89 void	usage(void);
90 char   *flags2opts(int);
91 
92 /* Map from mount options to printable formats. */
93 static struct opt {
94 	uint64_t o_opt;
95 	const char *o_name;
96 } optnames[] = {
97 	{ MNT_ASYNC,		"asynchronous" },
98 	{ MNT_EXPORTED,		"NFS exported" },
99 	{ MNT_LOCAL,		"local" },
100 	{ MNT_NOATIME,		"noatime" },
101 	{ MNT_NOEXEC,		"noexec" },
102 	{ MNT_NOSUID,		"nosuid" },
103 	{ MNT_NOSYMFOLLOW,	"nosymfollow" },
104 	{ MNT_QUOTA,		"with quotas" },
105 	{ MNT_RDONLY,		"read-only" },
106 	{ MNT_SYNCHRONOUS,	"synchronous" },
107 	{ MNT_UNION,		"union" },
108 	{ MNT_NOCLUSTERR,	"noclusterr" },
109 	{ MNT_NOCLUSTERW,	"noclusterw" },
110 	{ MNT_SUIDDIR,		"suiddir" },
111 	{ MNT_SOFTDEP,		"soft-updates" },
112 	{ MNT_SUJ,		"journaled soft-updates" },
113 	{ MNT_MULTILABEL,	"multilabel" },
114 	{ MNT_ACLS,		"acls" },
115 	{ MNT_NFS4ACLS,		"nfsv4acls" },
116 	{ MNT_GJOURNAL,		"gjournal" },
117 	{ MNT_AUTOMOUNTED,	"automounted" },
118 	{ MNT_VERIFIED,		"verified" },
119 	{ 0, NULL }
120 };
121 
122 /*
123  * List of VFS types that can be remounted without becoming mounted on top
124  * of each other.
125  * XXX Is this list correct?
126  */
127 static const char *
128 remountable_fs_names[] = {
129 	"ufs", "ffs", "ext2fs",
130 	0
131 };
132 
133 static const char userquotaeq[] = "userquota=";
134 static const char groupquotaeq[] = "groupquota=";
135 
136 static char *mountprog = NULL;
137 
138 static int
139 use_mountprog(const char *vfstype)
140 {
141 	/* XXX: We need to get away from implementing external mount
142 	 *      programs for every filesystem, and move towards having
143 	 *	each filesystem properly implement the nmount() system call.
144 	 */
145 	unsigned int i;
146 	const char *fs[] = {
147 	"cd9660", "mfs", "msdosfs", "nfs",
148 	"nullfs", "smbfs", "udf", "unionfs",
149 	NULL
150 	};
151 
152 	if (mountprog != NULL)
153 		return (1);
154 
155 	for (i = 0; fs[i] != NULL; ++i) {
156 		if (strcmp(vfstype, fs[i]) == 0)
157 			return (1);
158 	}
159 
160 	return (0);
161 }
162 
163 static int
164 exec_mountprog(const char *name, const char *execname, char *const argv[])
165 {
166 	pid_t pid;
167 	int status;
168 
169 	switch (pid = fork()) {
170 	case -1:				/* Error. */
171 		warn("fork");
172 		exit (1);
173 	case 0:					/* Child. */
174 		/* Go find an executable. */
175 		execvP(execname, _PATH_SYSPATH, argv);
176 		if (errno == ENOENT) {
177 			warn("exec %s not found", execname);
178 			if (execname[0] != '/') {
179 				warnx("in path: %s", _PATH_SYSPATH);
180 			}
181 		}
182 		exit(1);
183 	default:				/* Parent. */
184 		if (waitpid(pid, &status, 0) < 0) {
185 			warn("waitpid");
186 			return (1);
187 		}
188 
189 		if (WIFEXITED(status)) {
190 			if (WEXITSTATUS(status) != 0)
191 				return (WEXITSTATUS(status));
192 		} else if (WIFSIGNALED(status)) {
193 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
194 			return (1);
195 		}
196 		break;
197 	}
198 
199 	return (0);
200 }
201 
202 static int
203 specified_ro(const char *arg)
204 {
205 	char *optbuf, *opt;
206 	int ret = 0;
207 
208 	optbuf = strdup(arg);
209 	if (optbuf == NULL)
210 		 err(1, NULL);
211 
212 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
213 		if (strcmp(opt, "ro") == 0) {
214 			ret = 1;
215 			break;
216 		}
217 	}
218 	free(optbuf);
219 	return (ret);
220 }
221 
222 static void
223 restart_mountd(void)
224 {
225 	struct pidfh *pfh;
226 	pid_t mountdpid;
227 
228 	pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
229 	if (pfh != NULL) {
230 		/* Mountd is not running. */
231 		pidfile_remove(pfh);
232 		return;
233 	}
234 	if (errno != EEXIST) {
235 		/* Cannot open pidfile for some reason. */
236 		return;
237 	}
238 	/* We have mountd(8) PID in mountdpid varible, let's signal it. */
239 	if (kill(mountdpid, SIGHUP) == -1)
240 		err(1, "signal mountd");
241 }
242 
243 int
244 main(int argc, char *argv[])
245 {
246 	const char *mntfromname, **vfslist, *vfstype;
247 	struct fstab *fs;
248 	struct statfs *mntbuf;
249 	int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
250 	int onlylate;
251 	char *cp, *ep, *options;
252 
253 	all = init_flags = late = onlylate = 0;
254 	ro = 0;
255 	options = NULL;
256 	vfslist = NULL;
257 	vfstype = "ufs";
258 	while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
259 		switch (ch) {
260 		case 'a':
261 			all = 1;
262 			break;
263 		case 'd':
264 			debug = 1;
265 			break;
266 		case 'F':
267 			setfstab(optarg);
268 			break;
269 		case 'f':
270 			init_flags |= MNT_FORCE;
271 			break;
272 		case 'L':
273 			onlylate = 1;
274 			late = 1;
275 			break;
276 		case 'l':
277 			late = 1;
278 			break;
279 		case 'n':
280 			/* For compatibility with the Linux version of mount. */
281 			break;
282 		case 'o':
283 			if (*optarg) {
284 				options = catopt(options, optarg);
285 				if (specified_ro(optarg))
286 					ro = 1;
287 			}
288 			break;
289 		case 'p':
290 			fstab_style = 1;
291 			verbose = 1;
292 			break;
293 		case 'r':
294 			options = catopt(options, "ro");
295 			ro = 1;
296 			break;
297 		case 't':
298 			if (vfslist != NULL)
299 				errx(1, "only one -t option may be specified");
300 			vfslist = makevfslist(optarg);
301 			vfstype = optarg;
302 			break;
303 		case 'u':
304 			init_flags |= MNT_UPDATE;
305 			break;
306 		case 'v':
307 			verbose = 1;
308 			break;
309 		case 'w':
310 			options = catopt(options, "noro");
311 			break;
312 		case '?':
313 		default:
314 			usage();
315 			/* NOTREACHED */
316 		}
317 	argc -= optind;
318 	argv += optind;
319 
320 #define	BADTYPE(type)							\
321 	(strcmp(type, FSTAB_RO) &&					\
322 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
323 
324 	if ((init_flags & MNT_UPDATE) && (ro == 0))
325 		options = catopt(options, "noro");
326 
327 	rval = 0;
328 	switch (argc) {
329 	case 0:
330 		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
331 			err(1, "getmntinfo");
332 		if (all) {
333 			while ((fs = getfsent()) != NULL) {
334 				if (BADTYPE(fs->fs_type))
335 					continue;
336 				if (checkvfsname(fs->fs_vfstype, vfslist))
337 					continue;
338 				if (hasopt(fs->fs_mntops, "noauto"))
339 					continue;
340 				if (!hasopt(fs->fs_mntops, "late") && onlylate)
341 					continue;
342 				if (hasopt(fs->fs_mntops, "late") && !late)
343 					continue;
344 				if (hasopt(fs->fs_mntops, "failok"))
345 					failok = 1;
346 				else
347 					failok = 0;
348 				if (!(init_flags & MNT_UPDATE) &&
349 				    ismounted(fs, mntbuf, mntsize))
350 					continue;
351 				options = update_options(options, fs->fs_mntops,
352 				    mntbuf->f_flags);
353 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
354 				    fs->fs_file, init_flags, options,
355 				    fs->fs_mntops) && !failok)
356 					rval = 1;
357 			}
358 		} else if (fstab_style) {
359 			for (i = 0; i < mntsize; i++) {
360 				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
361 					continue;
362 				putfsent(&mntbuf[i]);
363 			}
364 		} else {
365 			for (i = 0; i < mntsize; i++) {
366 				if (checkvfsname(mntbuf[i].f_fstypename,
367 				    vfslist))
368 					continue;
369 				if (!verbose &&
370 				    (mntbuf[i].f_flags & MNT_IGNORE) != 0)
371 					continue;
372 				prmount(&mntbuf[i]);
373 			}
374 		}
375 		exit(rval);
376 	case 1:
377 		if (vfslist != NULL)
378 			usage();
379 
380 		rmslashes(*argv, *argv);
381 		if (init_flags & MNT_UPDATE) {
382 			mntfromname = NULL;
383 			have_fstab = 0;
384 			if ((mntbuf = getmntpt(*argv)) == NULL)
385 				errx(1, "not currently mounted %s", *argv);
386 			/*
387 			 * Only get the mntflags from fstab if both mntpoint
388 			 * and mntspec are identical. Also handle the special
389 			 * case where just '/' is mounted and 'spec' is not
390 			 * identical with the one from fstab ('/dev' is missing
391 			 * in the spec-string at boot-time).
392 			 */
393 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
394 				if (strcmp(fs->fs_spec,
395 				    mntbuf->f_mntfromname) == 0 &&
396 				    strcmp(fs->fs_file,
397 				    mntbuf->f_mntonname) == 0) {
398 					have_fstab = 1;
399 					mntfromname = mntbuf->f_mntfromname;
400 				} else if (argv[0][0] == '/' &&
401 				    argv[0][1] == '\0') {
402 					fs = getfsfile("/");
403 					have_fstab = 1;
404 					mntfromname = fs->fs_spec;
405 				}
406 			}
407 			if (have_fstab) {
408 				options = update_options(options, fs->fs_mntops,
409 				    mntbuf->f_flags);
410 			} else {
411 				mntfromname = mntbuf->f_mntfromname;
412 				options = update_options(options, NULL,
413 				    mntbuf->f_flags);
414 			}
415 			rval = mountfs(mntbuf->f_fstypename, mntfromname,
416 			    mntbuf->f_mntonname, init_flags, options, 0);
417 			break;
418 		}
419 		if ((fs = getfsfile(*argv)) == NULL &&
420 		    (fs = getfsspec(*argv)) == NULL)
421 			errx(1, "%s: unknown special file or file system",
422 			    *argv);
423 		if (BADTYPE(fs->fs_type))
424 			errx(1, "%s has unknown file system type",
425 			    *argv);
426 		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
427 		    init_flags, options, fs->fs_mntops);
428 		break;
429 	case 2:
430 		/*
431 		 * If -t flag has not been specified, the path cannot be
432 		 * found, spec contains either a ':' or a '@', then assume
433 		 * that an NFS file system is being specified ala Sun.
434 		 * Check if the hostname contains only allowed characters
435 		 * to reduce false positives.  IPv6 addresses containing
436 		 * ':' will be correctly parsed only if the separator is '@'.
437 		 * The definition of a valid hostname is taken from RFC 1034.
438 		 */
439 		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
440 		    (ep = strchr(argv[0], ':')) != NULL)) {
441 			if (*ep == '@') {
442 				cp = ep + 1;
443 				ep = cp + strlen(cp);
444 			} else
445 				cp = argv[0];
446 			while (cp != ep) {
447 				if (!isdigit(*cp) && !isalpha(*cp) &&
448 				    *cp != '.' && *cp != '-' && *cp != ':')
449 					break;
450 				cp++;
451 			}
452 			if (cp == ep)
453 				vfstype = "nfs";
454 		}
455 		rval = mountfs(vfstype,
456 		    argv[0], argv[1], init_flags, options, NULL);
457 		break;
458 	default:
459 		usage();
460 		/* NOTREACHED */
461 	}
462 
463 	/*
464 	 * If the mount was successfully, and done by root, tell mountd the
465 	 * good news.
466 	 */
467 	if (rval == 0 && getuid() == 0)
468 		restart_mountd();
469 
470 	exit(rval);
471 }
472 
473 int
474 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
475 {
476 	char realfsfile[PATH_MAX];
477 	int i;
478 
479 	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
480 		/* the root file system can always be remounted */
481 		return (0);
482 
483 	/* The user may have specified a symlink in fstab, resolve the path */
484 	if (realpath(fs->fs_file, realfsfile) == NULL) {
485 		/* Cannot resolve the path, use original one */
486 		strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
487 	}
488 
489 	/*
490 	 * Consider the filesystem to be mounted if:
491 	 * It has the same mountpoint as a mounted filesytem, and
492 	 * It has the same type as that same mounted filesystem, and
493 	 * It has the same device name as that same mounted filesystem, OR
494 	 *     It is a nonremountable filesystem
495 	 */
496 	for (i = mntsize - 1; i >= 0; --i)
497 		if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
498 		    strcmp(fs->fs_vfstype, mntbuf[i].f_fstypename) == 0 &&
499 		    (!isremountable(fs->fs_vfstype) ||
500 		     (strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)))
501 			return (1);
502 	return (0);
503 }
504 
505 int
506 isremountable(const char *vfsname)
507 {
508 	const char **cp;
509 
510 	for (cp = remountable_fs_names; *cp; cp++)
511 		if (strcmp(*cp, vfsname) == 0)
512 			return (1);
513 	return (0);
514 }
515 
516 int
517 hasopt(const char *mntopts, const char *option)
518 {
519 	int negative, found;
520 	char *opt, *optbuf;
521 
522 	if (option[0] == 'n' && option[1] == 'o') {
523 		negative = 1;
524 		option += 2;
525 	} else
526 		negative = 0;
527 	optbuf = strdup(mntopts);
528 	found = 0;
529 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
530 		if (opt[0] == 'n' && opt[1] == 'o') {
531 			if (!strcasecmp(opt + 2, option))
532 				found = negative;
533 		} else if (!strcasecmp(opt, option))
534 			found = !negative;
535 	}
536 	free(optbuf);
537 	return (found);
538 }
539 
540 static void
541 append_arg(struct cpa *sa, char *arg)
542 {
543 	if (sa->c + 1 == sa->sz) {
544 		sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
545 		sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz);
546 		if (sa->a == NULL)
547 			errx(1, "realloc failed");
548 	}
549 	sa->a[++sa->c] = arg;
550 }
551 
552 int
553 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
554 	const char *options, const char *mntopts)
555 {
556 	struct statfs sf;
557 	int i, ret;
558 	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
559 	static struct cpa mnt_argv;
560 
561 	/* resolve the mountpoint with realpath(3) */
562 	if (checkpath(name, mntpath) != 0) {
563 		warn("%s", mntpath);
564 		return (1);
565 	}
566 	name = mntpath;
567 
568 	if (mntopts == NULL)
569 		mntopts = "";
570 	optbuf = catopt(strdup(mntopts), options);
571 
572 	if (strcmp(name, "/") == 0)
573 		flags |= MNT_UPDATE;
574 	if (flags & MNT_FORCE)
575 		optbuf = catopt(optbuf, "force");
576 	if (flags & MNT_RDONLY)
577 		optbuf = catopt(optbuf, "ro");
578 	/*
579 	 * XXX
580 	 * The mount_mfs (newfs) command uses -o to select the
581 	 * optimization mode.  We don't pass the default "-o rw"
582 	 * for that reason.
583 	 */
584 	if (flags & MNT_UPDATE)
585 		optbuf = catopt(optbuf, "update");
586 
587 	/* Compatibility glue. */
588 	if (strcmp(vfstype, "msdos") == 0) {
589 		warnx(
590 		    "Using \"-t msdosfs\", since \"-t msdos\" is deprecated.");
591 		vfstype = "msdosfs";
592 	}
593 
594 	/* Construct the name of the appropriate mount command */
595 	(void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
596 
597 	mnt_argv.c = -1;
598 	append_arg(&mnt_argv, execname);
599 	mangle(optbuf, &mnt_argv);
600 	if (mountprog != NULL)
601 		strlcpy(execname, mountprog, sizeof(execname));
602 
603 	append_arg(&mnt_argv, strdup(spec));
604 	append_arg(&mnt_argv, strdup(name));
605 	append_arg(&mnt_argv, NULL);
606 
607 	if (debug) {
608 		if (use_mountprog(vfstype))
609 			printf("exec: %s", execname);
610 		else
611 			printf("mount -t %s", vfstype);
612 		for (i = 1; i < mnt_argv.c; i++)
613 			(void)printf(" %s", mnt_argv.a[i]);
614 		(void)printf("\n");
615 		free(optbuf);
616 		free(mountprog);
617 		mountprog = NULL;
618 		return (0);
619 	}
620 
621 	if (use_mountprog(vfstype)) {
622 		ret = exec_mountprog(name, execname, mnt_argv.a);
623 	} else {
624 		ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
625 	}
626 
627 	free(optbuf);
628 	free(mountprog);
629 	mountprog = NULL;
630 
631 	if (verbose) {
632 		if (statfs(name, &sf) < 0) {
633 			warn("statfs %s", name);
634 			return (1);
635 		}
636 		if (fstab_style)
637 			putfsent(&sf);
638 		else
639 			prmount(&sf);
640 	}
641 
642 	return (ret);
643 }
644 
645 void
646 prmount(struct statfs *sfp)
647 {
648 	uint64_t flags;
649 	unsigned int i;
650 	struct opt *o;
651 	struct passwd *pw;
652 
653 	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
654 	    sfp->f_fstypename);
655 
656 	flags = sfp->f_flags & MNT_VISFLAGMASK;
657 	for (o = optnames; flags != 0 && o->o_opt != 0; o++)
658 		if (flags & o->o_opt) {
659 			(void)printf(", %s", o->o_name);
660 			flags &= ~o->o_opt;
661 		}
662 	/*
663 	 * Inform when file system is mounted by an unprivileged user
664 	 * or privileged non-root user.
665 	 */
666 	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
667 		(void)printf(", mounted by ");
668 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
669 			(void)printf("%s", pw->pw_name);
670 		else
671 			(void)printf("%d", sfp->f_owner);
672 	}
673 	if (verbose) {
674 		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
675 			(void)printf(", writes: sync %ju async %ju",
676 			    (uintmax_t)sfp->f_syncwrites,
677 			    (uintmax_t)sfp->f_asyncwrites);
678 		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
679 			(void)printf(", reads: sync %ju async %ju",
680 			    (uintmax_t)sfp->f_syncreads,
681 			    (uintmax_t)sfp->f_asyncreads);
682 		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
683 			printf(", fsid ");
684 			for (i = 0; i < sizeof(sfp->f_fsid); i++)
685 				printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
686 		}
687 	}
688 	(void)printf(")\n");
689 }
690 
691 struct statfs *
692 getmntpt(const char *name)
693 {
694 	struct statfs *mntbuf;
695 	int i, mntsize;
696 
697 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
698 	for (i = mntsize - 1; i >= 0; i--) {
699 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
700 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
701 			return (&mntbuf[i]);
702 	}
703 	return (NULL);
704 }
705 
706 char *
707 catopt(char *s0, const char *s1)
708 {
709 	char *cp;
710 
711 	if (s1 == NULL || *s1 == '\0')
712 		return (s0);
713 
714 	if (s0 && *s0) {
715 		if (asprintf(&cp, "%s,%s", s0, s1) == -1)
716 			errx(1, "asprintf failed");
717 	} else
718 		cp = strdup(s1);
719 
720 	if (s0)
721 		free(s0);
722 	return (cp);
723 }
724 
725 void
726 mangle(char *options, struct cpa *a)
727 {
728 	char *p, *s, *val;
729 
730 	for (s = options; (p = strsep(&s, ",")) != NULL;)
731 		if (*p != '\0') {
732 			if (strcmp(p, "noauto") == 0) {
733 				/*
734 				 * Do not pass noauto option to nmount().
735 				 * or external mount program.  noauto is
736 				 * only used to prevent mounting a filesystem
737 				 * when 'mount -a' is specified, and is
738 				 * not a real mount option.
739 				 */
740 				continue;
741 			} else if (strcmp(p, "late") == 0) {
742 				/*
743 				 * "late" is used to prevent certain file
744 				 * systems from being mounted before late
745 				 * in the boot cycle; for instance,
746 				 * loopback NFS mounts can't be mounted
747 				 * before mountd starts.
748 				 */
749 				continue;
750 			} else if (strcmp(p, "failok") == 0) {
751 				/*
752 				 * "failok" is used to prevent certain file
753 				 * systems from being causing the system to
754 				 * drop into single user mode in the boot
755 				 * cycle, and is not a real mount option.
756 				 */
757 				continue;
758 			} else if (strncmp(p, "mountprog", 9) == 0) {
759 				/*
760 				 * "mountprog" is used to force the use of
761 				 * userland mount programs.
762 				 */
763 				val = strchr(p, '=');
764                         	if (val != NULL) {
765                                 	++val;
766 					if (*val != '\0')
767 						mountprog = strdup(val);
768 				}
769 
770 				if (mountprog == NULL) {
771 					errx(1, "Need value for -o mountprog");
772 				}
773 				continue;
774 			} else if (strcmp(p, "userquota") == 0) {
775 				continue;
776 			} else if (strncmp(p, userquotaeq,
777 			    sizeof(userquotaeq) - 1) == 0) {
778 				continue;
779 			} else if (strcmp(p, "groupquota") == 0) {
780 				continue;
781 			} else if (strncmp(p, groupquotaeq,
782 			    sizeof(groupquotaeq) - 1) == 0) {
783 				continue;
784 			} else if (*p == '-') {
785 				append_arg(a, p);
786 				p = strchr(p, '=');
787 				if (p != NULL) {
788 					*p = '\0';
789 					append_arg(a, p + 1);
790 				}
791 			} else {
792 				append_arg(a, strdup("-o"));
793 				append_arg(a, p);
794 			}
795 		}
796 }
797 
798 
799 char *
800 update_options(char *opts, char *fstab, int curflags)
801 {
802 	char *o, *p;
803 	char *cur;
804 	char *expopt, *newopt, *tmpopt;
805 
806 	if (opts == NULL)
807 		return (strdup(""));
808 
809 	/* remove meta options from list */
810 	remopt(fstab, MOUNT_META_OPTION_FSTAB);
811 	remopt(fstab, MOUNT_META_OPTION_CURRENT);
812 	cur = flags2opts(curflags);
813 
814 	/*
815 	 * Expand all meta-options passed to us first.
816 	 */
817 	expopt = NULL;
818 	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
819 		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
820 			expopt = catopt(expopt, fstab);
821 		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
822 			expopt = catopt(expopt, cur);
823 		else
824 			expopt = catopt(expopt, o);
825 	}
826 	free(cur);
827 	free(opts);
828 
829 	/*
830 	 * Remove previous contradictory arguments. Given option "foo" we
831 	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
832 	 * and "foo" - so we can deal with possible options like "notice".
833 	 */
834 	newopt = NULL;
835 	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
836 		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
837 			errx(1, "malloc failed");
838 
839 		strcpy(tmpopt, "no");
840 		strcat(tmpopt, o);
841 		remopt(newopt, tmpopt);
842 		free(tmpopt);
843 
844 		if (strncmp("no", o, 2) == 0)
845 			remopt(newopt, o+2);
846 
847 		newopt = catopt(newopt, o);
848 	}
849 	free(expopt);
850 
851 	return (newopt);
852 }
853 
854 void
855 remopt(char *string, const char *opt)
856 {
857 	char *o, *p, *r;
858 
859 	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
860 		return;
861 
862 	r = string;
863 
864 	for (p = string; (o = strsep(&p, ",")) != NULL;) {
865 		if (strcmp(opt, o) != 0) {
866 			if (*r == ',' && *o != '\0')
867 				r++;
868 			while ((*r++ = *o++) != '\0')
869 			    ;
870 			*--r = ',';
871 		}
872 	}
873 	*r = '\0';
874 }
875 
876 void
877 usage(void)
878 {
879 
880 	(void)fprintf(stderr, "%s\n%s\n%s\n",
881 "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
882 "       mount [-dfpruvw] special | node",
883 "       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
884 	exit(1);
885 }
886 
887 void
888 putfsent(struct statfs *ent)
889 {
890 	struct fstab *fst;
891 	char *opts, *rw;
892 	int l;
893 
894 	opts = NULL;
895 	/* flags2opts() doesn't return the "rw" option. */
896 	if ((ent->f_flags & MNT_RDONLY) != 0)
897 		rw = NULL;
898 	else
899 		rw = catopt(NULL, "rw");
900 
901 	opts = flags2opts(ent->f_flags);
902 	opts = catopt(rw, opts);
903 
904 	if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
905 	    strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
906 		strlcpy(ent->f_mntfromname,
907 		    (strnstr(ent->f_mntfromname, ":", 8) +1),
908 		    sizeof(ent->f_mntfromname));
909 	}
910 
911 	l = strlen(ent->f_mntfromname);
912 	printf("%s%s%s%s", ent->f_mntfromname,
913 	    l < 8 ? "\t" : "",
914 	    l < 16 ? "\t" : "",
915 	    l < 24 ? "\t" : " ");
916 	l = strlen(ent->f_mntonname);
917 	printf("%s%s%s%s", ent->f_mntonname,
918 	    l < 8 ? "\t" : "",
919 	    l < 16 ? "\t" : "",
920 	    l < 24 ? "\t" : " ");
921 	printf("%s\t", ent->f_fstypename);
922 	l = strlen(opts);
923 	printf("%s%s", opts,
924 	    l < 8 ? "\t" : " ");
925 	free(opts);
926 
927 	if ((fst = getfsspec(ent->f_mntfromname)))
928 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
929 	else if ((fst = getfsfile(ent->f_mntonname)))
930 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
931 	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
932 		if (strcmp(ent->f_mntonname, "/") == 0)
933 			printf("\t1 1\n");
934 		else
935 			printf("\t2 2\n");
936 	} else
937 		printf("\t0 0\n");
938 }
939 
940 
941 char *
942 flags2opts(int flags)
943 {
944 	char *res;
945 
946 	res = NULL;
947 
948 	if (flags & MNT_RDONLY)		res = catopt(res, "ro");
949 	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
950 	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
951 	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
952 	if (flags & MNT_UNION)		res = catopt(res, "union");
953 	if (flags & MNT_ASYNC)		res = catopt(res, "async");
954 	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
955 	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
956 	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
957 	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
958 	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
959 	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
960 	if (flags & MNT_ACLS)		res = catopt(res, "acls");
961 	if (flags & MNT_NFS4ACLS)	res = catopt(res, "nfsv4acls");
962 
963 	return (res);
964 }
965