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