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