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