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