xref: /freebsd/sbin/mount/mount.c (revision f6a3b357e9be4c6423c85eff9a847163a0d307c8)
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 	{ MNT_UNTRUSTED,	"untrusted" },
122 	{ 0, NULL }
123 };
124 
125 /*
126  * List of VFS types that can be remounted without becoming mounted on top
127  * of each other.
128  * XXX Is this list correct?
129  */
130 static const char *
131 remountable_fs_names[] = {
132 	"ufs", "ffs", "ext2fs",
133 	0
134 };
135 
136 static const char userquotaeq[] = "userquota=";
137 static const char groupquotaeq[] = "groupquota=";
138 
139 static char *mountprog = NULL;
140 
141 static int
142 use_mountprog(const char *vfstype)
143 {
144 	/* XXX: We need to get away from implementing external mount
145 	 *      programs for every filesystem, and move towards having
146 	 *	each filesystem properly implement the nmount() system call.
147 	 */
148 	unsigned int i;
149 	const char *fs[] = {
150 	"cd9660", "mfs", "msdosfs", "nfs",
151 	"nullfs", "smbfs", "udf", "unionfs",
152 	NULL
153 	};
154 
155 	if (mountprog != NULL)
156 		return (1);
157 
158 	for (i = 0; fs[i] != NULL; ++i) {
159 		if (strcmp(vfstype, fs[i]) == 0)
160 			return (1);
161 	}
162 
163 	return (0);
164 }
165 
166 static int
167 exec_mountprog(const char *name, const char *execname, char *const argv[])
168 {
169 	pid_t pid;
170 	int status;
171 
172 	switch (pid = fork()) {
173 	case -1:				/* Error. */
174 		warn("fork");
175 		exit (1);
176 	case 0:					/* Child. */
177 		/* Go find an executable. */
178 		execvP(execname, _PATH_SYSPATH, argv);
179 		if (errno == ENOENT) {
180 			warn("exec %s not found", execname);
181 			if (execname[0] != '/') {
182 				warnx("in path: %s", _PATH_SYSPATH);
183 			}
184 		}
185 		exit(1);
186 	default:				/* Parent. */
187 		if (waitpid(pid, &status, 0) < 0) {
188 			warn("waitpid");
189 			return (1);
190 		}
191 
192 		if (WIFEXITED(status)) {
193 			if (WEXITSTATUS(status) != 0)
194 				return (WEXITSTATUS(status));
195 		} else if (WIFSIGNALED(status)) {
196 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
197 			return (1);
198 		}
199 		break;
200 	}
201 
202 	return (0);
203 }
204 
205 static int
206 specified_ro(const char *arg)
207 {
208 	char *optbuf, *opt;
209 	int ret = 0;
210 
211 	optbuf = strdup(arg);
212 	if (optbuf == NULL)
213 		 err(1, NULL);
214 
215 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
216 		if (strcmp(opt, "ro") == 0) {
217 			ret = 1;
218 			break;
219 		}
220 	}
221 	free(optbuf);
222 	return (ret);
223 }
224 
225 static void
226 restart_mountd(void)
227 {
228 	struct pidfh *pfh;
229 	pid_t mountdpid;
230 
231 	mountdpid = 0;
232 	pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
233 	if (pfh != NULL) {
234 		/* Mountd is not running. */
235 		pidfile_remove(pfh);
236 		return;
237 	}
238 	if (errno != EEXIST) {
239 		/* Cannot open pidfile for some reason. */
240 		return;
241 	}
242 
243 	/*
244 	 * Refuse to send broadcast or group signals, this has
245 	 * happened due to the bugs in pidfile(3).
246 	 */
247 	if (mountdpid <= 0) {
248 		warnx("mountd pid %d, refusing to send SIGHUP", mountdpid);
249 		return;
250 	}
251 
252 	/* We have mountd(8) PID in mountdpid varible, let's signal it. */
253 	if (kill(mountdpid, SIGHUP) == -1)
254 		err(1, "signal mountd");
255 }
256 
257 int
258 main(int argc, char *argv[])
259 {
260 	const char *mntfromname, **vfslist, *vfstype;
261 	struct fstab *fs;
262 	struct statfs *mntbuf;
263 	int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
264 	int onlylate;
265 	char *cp, *ep, *options;
266 
267 	all = init_flags = late = onlylate = 0;
268 	ro = 0;
269 	options = NULL;
270 	vfslist = NULL;
271 	vfstype = "ufs";
272 	while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
273 		switch (ch) {
274 		case 'a':
275 			all = 1;
276 			break;
277 		case 'd':
278 			debug = 1;
279 			break;
280 		case 'F':
281 			setfstab(optarg);
282 			break;
283 		case 'f':
284 			init_flags |= MNT_FORCE;
285 			break;
286 		case 'L':
287 			onlylate = 1;
288 			late = 1;
289 			break;
290 		case 'l':
291 			late = 1;
292 			break;
293 		case 'n':
294 			/* For compatibility with the Linux version of mount. */
295 			break;
296 		case 'o':
297 			if (*optarg) {
298 				options = catopt(options, optarg);
299 				if (specified_ro(optarg))
300 					ro = 1;
301 			}
302 			break;
303 		case 'p':
304 			fstab_style = 1;
305 			verbose = 1;
306 			break;
307 		case 'r':
308 			options = catopt(options, "ro");
309 			ro = 1;
310 			break;
311 		case 't':
312 			if (vfslist != NULL)
313 				errx(1, "only one -t option may be specified");
314 			vfslist = makevfslist(optarg);
315 			vfstype = optarg;
316 			break;
317 		case 'u':
318 			init_flags |= MNT_UPDATE;
319 			break;
320 		case 'v':
321 			verbose = 1;
322 			break;
323 		case 'w':
324 			options = catopt(options, "noro");
325 			break;
326 		case '?':
327 		default:
328 			usage();
329 			/* NOTREACHED */
330 		}
331 	argc -= optind;
332 	argv += optind;
333 
334 #define	BADTYPE(type)							\
335 	(strcmp(type, FSTAB_RO) &&					\
336 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
337 
338 	if ((init_flags & MNT_UPDATE) && (ro == 0))
339 		options = catopt(options, "noro");
340 
341 	rval = 0;
342 	switch (argc) {
343 	case 0:
344 		if ((mntsize = getmntinfo(&mntbuf,
345 		     verbose ? MNT_WAIT : MNT_NOWAIT)) == 0)
346 			err(1, "getmntinfo");
347 		if (all) {
348 			while ((fs = getfsent()) != NULL) {
349 				if (BADTYPE(fs->fs_type))
350 					continue;
351 				if (checkvfsname(fs->fs_vfstype, vfslist))
352 					continue;
353 				if (hasopt(fs->fs_mntops, "noauto"))
354 					continue;
355 				if (!hasopt(fs->fs_mntops, "late") && onlylate)
356 					continue;
357 				if (hasopt(fs->fs_mntops, "late") && !late)
358 					continue;
359 				if (hasopt(fs->fs_mntops, "failok"))
360 					failok = 1;
361 				else
362 					failok = 0;
363 				if (!(init_flags & MNT_UPDATE) &&
364 				    !hasopt(fs->fs_mntops, "update") &&
365 				    ismounted(fs, mntbuf, mntsize))
366 					continue;
367 				options = update_options(options, fs->fs_mntops,
368 				    mntbuf->f_flags);
369 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
370 				    fs->fs_file, init_flags, options,
371 				    fs->fs_mntops) && !failok)
372 					rval = 1;
373 			}
374 		} else if (fstab_style) {
375 			for (i = 0; i < mntsize; i++) {
376 				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
377 					continue;
378 				putfsent(&mntbuf[i]);
379 			}
380 		} else {
381 			for (i = 0; i < mntsize; i++) {
382 				if (checkvfsname(mntbuf[i].f_fstypename,
383 				    vfslist))
384 					continue;
385 				if (!verbose &&
386 				    (mntbuf[i].f_flags & MNT_IGNORE) != 0)
387 					continue;
388 				prmount(&mntbuf[i]);
389 			}
390 		}
391 		exit(rval);
392 	case 1:
393 		if (vfslist != NULL)
394 			usage();
395 
396 		rmslashes(*argv, *argv);
397 		if (init_flags & MNT_UPDATE) {
398 			mntfromname = NULL;
399 			have_fstab = 0;
400 			if ((mntbuf = getmntpt(*argv)) == NULL)
401 				errx(1, "not currently mounted %s", *argv);
402 			/*
403 			 * Only get the mntflags from fstab if both mntpoint
404 			 * and mntspec are identical. Also handle the special
405 			 * case where just '/' is mounted and 'spec' is not
406 			 * identical with the one from fstab ('/dev' is missing
407 			 * in the spec-string at boot-time).
408 			 */
409 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
410 				if (strcmp(fs->fs_spec,
411 				    mntbuf->f_mntfromname) == 0 &&
412 				    strcmp(fs->fs_file,
413 				    mntbuf->f_mntonname) == 0) {
414 					have_fstab = 1;
415 					mntfromname = mntbuf->f_mntfromname;
416 				} else if (argv[0][0] == '/' &&
417 				    argv[0][1] == '\0' &&
418 				    strcmp(fs->fs_vfstype,
419 				    mntbuf->f_fstypename) == 0) {
420 					fs = getfsfile("/");
421 					have_fstab = 1;
422 					mntfromname = fs->fs_spec;
423 				}
424 			}
425 			if (have_fstab) {
426 				options = update_options(options, fs->fs_mntops,
427 				    mntbuf->f_flags);
428 			} else {
429 				mntfromname = mntbuf->f_mntfromname;
430 				options = update_options(options, NULL,
431 				    mntbuf->f_flags);
432 			}
433 			rval = mountfs(mntbuf->f_fstypename, mntfromname,
434 			    mntbuf->f_mntonname, init_flags, options, 0);
435 			break;
436 		}
437 		if ((fs = getfsfile(*argv)) == NULL &&
438 		    (fs = getfsspec(*argv)) == NULL)
439 			errx(1, "%s: unknown special file or file system",
440 			    *argv);
441 		if (BADTYPE(fs->fs_type))
442 			errx(1, "%s has unknown file system type",
443 			    *argv);
444 		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
445 		    init_flags, options, fs->fs_mntops);
446 		break;
447 	case 2:
448 		/*
449 		 * If -t flag has not been specified, the path cannot be
450 		 * found, spec contains either a ':' or a '@', then assume
451 		 * that an NFS file system is being specified ala Sun.
452 		 * Check if the hostname contains only allowed characters
453 		 * to reduce false positives.  IPv6 addresses containing
454 		 * ':' will be correctly parsed only if the separator is '@'.
455 		 * The definition of a valid hostname is taken from RFC 1034.
456 		 */
457 		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
458 		    (ep = strchr(argv[0], ':')) != NULL)) {
459 			if (*ep == '@') {
460 				cp = ep + 1;
461 				ep = cp + strlen(cp);
462 			} else
463 				cp = argv[0];
464 			while (cp != ep) {
465 				if (!isdigit(*cp) && !isalpha(*cp) &&
466 				    *cp != '.' && *cp != '-' && *cp != ':')
467 					break;
468 				cp++;
469 			}
470 			if (cp == ep)
471 				vfstype = "nfs";
472 		}
473 		rval = mountfs(vfstype,
474 		    argv[0], argv[1], init_flags, options, NULL);
475 		break;
476 	default:
477 		usage();
478 		/* NOTREACHED */
479 	}
480 
481 	/*
482 	 * If the mount was successfully, and done by root, tell mountd the
483 	 * good news.
484 	 */
485 	if (rval == 0 && getuid() == 0)
486 		restart_mountd();
487 
488 	exit(rval);
489 }
490 
491 int
492 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
493 {
494 	char realfsfile[PATH_MAX];
495 	int i;
496 
497 	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
498 		/* the root file system can always be remounted */
499 		return (0);
500 
501 	/* The user may have specified a symlink in fstab, resolve the path */
502 	if (realpath(fs->fs_file, realfsfile) == NULL) {
503 		/* Cannot resolve the path, use original one */
504 		strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
505 	}
506 
507 	/*
508 	 * Consider the filesystem to be mounted if:
509 	 * It has the same mountpoint as a mounted filesytem, and
510 	 * It has the same type as that same mounted filesystem, and
511 	 * It has the same device name as that same mounted filesystem, OR
512 	 *     It is a nonremountable filesystem
513 	 */
514 	for (i = mntsize - 1; i >= 0; --i)
515 		if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
516 		    strcmp(fs->fs_vfstype, mntbuf[i].f_fstypename) == 0 &&
517 		    (!isremountable(fs->fs_vfstype) ||
518 		     (strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)))
519 			return (1);
520 	return (0);
521 }
522 
523 int
524 isremountable(const char *vfsname)
525 {
526 	const char **cp;
527 
528 	for (cp = remountable_fs_names; *cp; cp++)
529 		if (strcmp(*cp, vfsname) == 0)
530 			return (1);
531 	return (0);
532 }
533 
534 int
535 hasopt(const char *mntopts, const char *option)
536 {
537 	int negative, found;
538 	char *opt, *optbuf;
539 
540 	if (option[0] == 'n' && option[1] == 'o') {
541 		negative = 1;
542 		option += 2;
543 	} else
544 		negative = 0;
545 	optbuf = strdup(mntopts);
546 	found = 0;
547 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
548 		if (opt[0] == 'n' && opt[1] == 'o') {
549 			if (!strcasecmp(opt + 2, option))
550 				found = negative;
551 		} else if (!strcasecmp(opt, option))
552 			found = !negative;
553 	}
554 	free(optbuf);
555 	return (found);
556 }
557 
558 static void
559 append_arg(struct cpa *sa, char *arg)
560 {
561 	if (sa->c + 1 == sa->sz) {
562 		sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
563 		sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz);
564 		if (sa->a == NULL)
565 			errx(1, "realloc failed");
566 	}
567 	sa->a[++sa->c] = arg;
568 }
569 
570 int
571 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
572 	const char *options, const char *mntopts)
573 {
574 	struct statfs sf;
575 	int i, ret;
576 	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
577 	static struct cpa mnt_argv;
578 
579 	/* resolve the mountpoint with realpath(3) */
580 	if (checkpath(name, mntpath) != 0) {
581 		warn("%s", mntpath);
582 		return (1);
583 	}
584 	name = mntpath;
585 
586 	if (mntopts == NULL)
587 		mntopts = "";
588 	optbuf = catopt(strdup(mntopts), options);
589 
590 	if (strcmp(name, "/") == 0)
591 		flags |= MNT_UPDATE;
592 	if (flags & MNT_FORCE)
593 		optbuf = catopt(optbuf, "force");
594 	if (flags & MNT_RDONLY)
595 		optbuf = catopt(optbuf, "ro");
596 	/*
597 	 * XXX
598 	 * The mount_mfs (newfs) command uses -o to select the
599 	 * optimization mode.  We don't pass the default "-o rw"
600 	 * for that reason.
601 	 */
602 	if (flags & MNT_UPDATE)
603 		optbuf = catopt(optbuf, "update");
604 
605 	/* Compatibility glue. */
606 	if (strcmp(vfstype, "msdos") == 0)
607 		vfstype = "msdosfs";
608 
609 	/* Construct the name of the appropriate mount command */
610 	(void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
611 
612 	mnt_argv.c = -1;
613 	append_arg(&mnt_argv, execname);
614 	mangle(optbuf, &mnt_argv);
615 	if (mountprog != NULL)
616 		strlcpy(execname, mountprog, sizeof(execname));
617 
618 	append_arg(&mnt_argv, strdup(spec));
619 	append_arg(&mnt_argv, strdup(name));
620 	append_arg(&mnt_argv, NULL);
621 
622 	if (debug) {
623 		if (use_mountprog(vfstype))
624 			printf("exec: %s", execname);
625 		else
626 			printf("mount -t %s", vfstype);
627 		for (i = 1; i < mnt_argv.c; i++)
628 			(void)printf(" %s", mnt_argv.a[i]);
629 		(void)printf("\n");
630 		free(optbuf);
631 		free(mountprog);
632 		mountprog = NULL;
633 		return (0);
634 	}
635 
636 	if (use_mountprog(vfstype)) {
637 		ret = exec_mountprog(name, execname, mnt_argv.a);
638 	} else {
639 		ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
640 	}
641 
642 	free(optbuf);
643 	free(mountprog);
644 	mountprog = NULL;
645 
646 	if (verbose) {
647 		if (statfs(name, &sf) < 0) {
648 			warn("statfs %s", name);
649 			return (1);
650 		}
651 		if (fstab_style)
652 			putfsent(&sf);
653 		else
654 			prmount(&sf);
655 	}
656 
657 	return (ret);
658 }
659 
660 void
661 prmount(struct statfs *sfp)
662 {
663 	uint64_t flags;
664 	unsigned int i;
665 	struct opt *o;
666 	struct passwd *pw;
667 
668 	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
669 	    sfp->f_fstypename);
670 
671 	flags = sfp->f_flags & MNT_VISFLAGMASK;
672 	for (o = optnames; flags != 0 && o->o_opt != 0; o++)
673 		if (flags & o->o_opt) {
674 			(void)printf(", %s", o->o_name);
675 			flags &= ~o->o_opt;
676 		}
677 	/*
678 	 * Inform when file system is mounted by an unprivileged user
679 	 * or privileged non-root user.
680 	 */
681 	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
682 		(void)printf(", mounted by ");
683 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
684 			(void)printf("%s", pw->pw_name);
685 		else
686 			(void)printf("%d", sfp->f_owner);
687 	}
688 	if (verbose) {
689 		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
690 			(void)printf(", writes: sync %ju async %ju",
691 			    (uintmax_t)sfp->f_syncwrites,
692 			    (uintmax_t)sfp->f_asyncwrites);
693 		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
694 			(void)printf(", reads: sync %ju async %ju",
695 			    (uintmax_t)sfp->f_syncreads,
696 			    (uintmax_t)sfp->f_asyncreads);
697 		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
698 			printf(", fsid ");
699 			for (i = 0; i < sizeof(sfp->f_fsid); i++)
700 				printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
701 		}
702 	}
703 	(void)printf(")\n");
704 }
705 
706 struct statfs *
707 getmntpt(const char *name)
708 {
709 	struct statfs *mntbuf;
710 	int i, mntsize;
711 
712 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
713 	for (i = mntsize - 1; i >= 0; i--) {
714 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
715 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
716 			return (&mntbuf[i]);
717 	}
718 	return (NULL);
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 			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 					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 			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 	(void)fprintf(stderr, "%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 	printf("%s%s%s%s", ent->f_mntfromname,
928 	    l < 8 ? "\t" : "",
929 	    l < 16 ? "\t" : "",
930 	    l < 24 ? "\t" : " ");
931 	l = strlen(ent->f_mntonname);
932 	printf("%s%s%s%s", ent->f_mntonname,
933 	    l < 8 ? "\t" : "",
934 	    l < 16 ? "\t" : "",
935 	    l < 24 ? "\t" : " ");
936 	printf("%s\t", ent->f_fstypename);
937 	l = strlen(opts);
938 	printf("%s%s", opts,
939 	    l < 8 ? "\t" : " ");
940 	free(opts);
941 
942 	if ((fst = getfsspec(ent->f_mntfromname)))
943 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
944 	else if ((fst = getfsfile(ent->f_mntonname)))
945 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
946 	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
947 		if (strcmp(ent->f_mntonname, "/") == 0)
948 			printf("\t1 1\n");
949 		else
950 			printf("\t2 2\n");
951 	} else
952 		printf("\t0 0\n");
953 }
954 
955 
956 char *
957 flags2opts(int flags)
958 {
959 	char *res;
960 
961 	res = NULL;
962 
963 	if (flags & MNT_RDONLY)		res = catopt(res, "ro");
964 	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
965 	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
966 	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
967 	if (flags & MNT_UNION)		res = catopt(res, "union");
968 	if (flags & MNT_ASYNC)		res = catopt(res, "async");
969 	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
970 	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
971 	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
972 	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
973 	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
974 	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
975 	if (flags & MNT_ACLS)		res = catopt(res, "acls");
976 	if (flags & MNT_NFS4ACLS)	res = catopt(res, "nfsv4acls");
977 	if (flags & MNT_UNTRUSTED)	res = catopt(res, "untrusted");
978 
979 	return (res);
980 }
981