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