xref: /freebsd/sbin/mount/mount.c (revision d2ba5111c125104b09aa1acd1bfe8af2a24c79cc)
1 /*-
2  * Copyright (c) 1980, 1989, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
33 	The Regents of the University of California.  All rights reserved.\n";
34 #if 0
35 static char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
36 #endif
37 #endif /* not lint */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 #include <sys/stat.h>
45 #include <sys/wait.h>
46 
47 #include <ctype.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <fstab.h>
51 #include <paths.h>
52 #include <pwd.h>
53 #include <signal.h>
54 #include <stdint.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 #include <libutil.h>
60 
61 #include "extern.h"
62 #include "mntopts.h"
63 #include "pathnames.h"
64 
65 /* `meta' options */
66 #define MOUNT_META_OPTION_FSTAB		"fstab"
67 #define MOUNT_META_OPTION_CURRENT	"current"
68 
69 static int debug, fstab_style, verbose;
70 
71 struct cpa {
72 	char	**a;
73 	ssize_t	sz;
74 	int	c;
75 };
76 
77 char   *catopt(char *, const char *);
78 struct statfs *getmntpt(const char *);
79 int	hasopt(const char *, const char *);
80 int	ismounted(struct fstab *, struct statfs *, int);
81 int	isremountable(const char *);
82 void	mangle(char *, struct cpa *);
83 char   *update_options(char *, char *, int);
84 int	mountfs(const char *, const char *, const char *,
85 			int, const char *, const char *);
86 void	remopt(char *, const char *);
87 void	prmount(struct statfs *);
88 void	putfsent(struct statfs *);
89 void	usage(void);
90 char   *flags2opts(int);
91 
92 /* Map from mount options to printable formats. */
93 static struct opt {
94 	uint64_t o_opt;
95 	const char *o_name;
96 } optnames[] = {
97 	{ MNT_ASYNC,		"asynchronous" },
98 	{ MNT_EXPORTED,		"NFS exported" },
99 	{ MNT_LOCAL,		"local" },
100 	{ MNT_NOATIME,		"noatime" },
101 	{ MNT_NOEXEC,		"noexec" },
102 	{ MNT_NOSUID,		"nosuid" },
103 	{ MNT_NOSYMFOLLOW,	"nosymfollow" },
104 	{ MNT_QUOTA,		"with quotas" },
105 	{ MNT_RDONLY,		"read-only" },
106 	{ MNT_SYNCHRONOUS,	"synchronous" },
107 	{ MNT_UNION,		"union" },
108 	{ MNT_NOCLUSTERR,	"noclusterr" },
109 	{ MNT_NOCLUSTERW,	"noclusterw" },
110 	{ MNT_SUIDDIR,		"suiddir" },
111 	{ MNT_SOFTDEP,		"soft-updates" },
112 	{ MNT_SUJ,		"journaled soft-updates" },
113 	{ MNT_MULTILABEL,	"multilabel" },
114 	{ MNT_ACLS,		"acls" },
115 	{ MNT_NFS4ACLS,		"nfsv4acls" },
116 	{ MNT_GJOURNAL,		"gjournal" },
117 	{ MNT_AUTOMOUNTED,	"automounted" },
118 	{ MNT_VERIFIED,		"verified" },
119 	{ 0, NULL }
120 };
121 
122 /*
123  * List of VFS types that can be remounted without becoming mounted on top
124  * of each other.
125  * XXX Is this list correct?
126  */
127 static const char *
128 remountable_fs_names[] = {
129 	"ufs", "ffs", "ext2fs",
130 	0
131 };
132 
133 static const char userquotaeq[] = "userquota=";
134 static const char groupquotaeq[] = "groupquota=";
135 
136 static char *mountprog = NULL;
137 
138 static int
139 use_mountprog(const char *vfstype)
140 {
141 	/* XXX: We need to get away from implementing external mount
142 	 *      programs for every filesystem, and move towards having
143 	 *	each filesystem properly implement the nmount() system call.
144 	 */
145 	unsigned int i;
146 	const char *fs[] = {
147 	"cd9660", "mfs", "msdosfs", "nfs",
148 	"nullfs", "smbfs", "udf", "unionfs",
149 	NULL
150 	};
151 
152 	if (mountprog != NULL)
153 		return (1);
154 
155 	for (i = 0; fs[i] != NULL; ++i) {
156 		if (strcmp(vfstype, fs[i]) == 0)
157 			return (1);
158 	}
159 
160 	return (0);
161 }
162 
163 static int
164 exec_mountprog(const char *name, const char *execname, char *const argv[])
165 {
166 	pid_t pid;
167 	int status;
168 
169 	switch (pid = fork()) {
170 	case -1:				/* Error. */
171 		warn("fork");
172 		exit (1);
173 	case 0:					/* Child. */
174 		/* Go find an executable. */
175 		execvP(execname, _PATH_SYSPATH, argv);
176 		if (errno == ENOENT) {
177 			warn("exec %s not found", execname);
178 			if (execname[0] != '/') {
179 				warnx("in path: %s", _PATH_SYSPATH);
180 			}
181 		}
182 		exit(1);
183 	default:				/* Parent. */
184 		if (waitpid(pid, &status, 0) < 0) {
185 			warn("waitpid");
186 			return (1);
187 		}
188 
189 		if (WIFEXITED(status)) {
190 			if (WEXITSTATUS(status) != 0)
191 				return (WEXITSTATUS(status));
192 		} else if (WIFSIGNALED(status)) {
193 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
194 			return (1);
195 		}
196 		break;
197 	}
198 
199 	return (0);
200 }
201 
202 static int
203 specified_ro(const char *arg)
204 {
205 	char *optbuf, *opt;
206 	int ret = 0;
207 
208 	optbuf = strdup(arg);
209 	if (optbuf == NULL)
210 		 err(1, NULL);
211 
212 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
213 		if (strcmp(opt, "ro") == 0) {
214 			ret = 1;
215 			break;
216 		}
217 	}
218 	free(optbuf);
219 	return (ret);
220 }
221 
222 static void
223 restart_mountd(void)
224 {
225 	struct pidfh *pfh;
226 	pid_t mountdpid;
227 
228 	pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
229 	if (pfh != NULL) {
230 		/* Mountd is not running. */
231 		pidfile_remove(pfh);
232 		return;
233 	}
234 	if (errno != EEXIST) {
235 		/* Cannot open pidfile for some reason. */
236 		return;
237 	}
238 	/* We have mountd(8) PID in mountdpid varible, let's signal it. */
239 	if (kill(mountdpid, SIGHUP) == -1)
240 		err(1, "signal mountd");
241 }
242 
243 int
244 main(int argc, char *argv[])
245 {
246 	const char *mntfromname, **vfslist, *vfstype;
247 	struct fstab *fs;
248 	struct statfs *mntbuf;
249 	int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
250 	int onlylate;
251 	char *cp, *ep, *options;
252 
253 	all = init_flags = late = onlylate = 0;
254 	ro = 0;
255 	options = NULL;
256 	vfslist = NULL;
257 	vfstype = "ufs";
258 	while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
259 		switch (ch) {
260 		case 'a':
261 			all = 1;
262 			break;
263 		case 'd':
264 			debug = 1;
265 			break;
266 		case 'F':
267 			setfstab(optarg);
268 			break;
269 		case 'f':
270 			init_flags |= MNT_FORCE;
271 			break;
272 		case 'L':
273 			onlylate = 1;
274 			late = 1;
275 			break;
276 		case 'l':
277 			late = 1;
278 			break;
279 		case 'n':
280 			/* For compatibility with the Linux version of mount. */
281 			break;
282 		case 'o':
283 			if (*optarg) {
284 				options = catopt(options, optarg);
285 				if (specified_ro(optarg))
286 					ro = 1;
287 			}
288 			break;
289 		case 'p':
290 			fstab_style = 1;
291 			verbose = 1;
292 			break;
293 		case 'r':
294 			options = catopt(options, "ro");
295 			ro = 1;
296 			break;
297 		case 't':
298 			if (vfslist != NULL)
299 				errx(1, "only one -t option may be specified");
300 			vfslist = makevfslist(optarg);
301 			vfstype = optarg;
302 			break;
303 		case 'u':
304 			init_flags |= MNT_UPDATE;
305 			break;
306 		case 'v':
307 			verbose = 1;
308 			break;
309 		case 'w':
310 			options = catopt(options, "noro");
311 			break;
312 		case '?':
313 		default:
314 			usage();
315 			/* NOTREACHED */
316 		}
317 	argc -= optind;
318 	argv += optind;
319 
320 #define	BADTYPE(type)							\
321 	(strcmp(type, FSTAB_RO) &&					\
322 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
323 
324 	if ((init_flags & MNT_UPDATE) && (ro == 0))
325 		options = catopt(options, "noro");
326 
327 	rval = 0;
328 	switch (argc) {
329 	case 0:
330 		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
331 			err(1, "getmntinfo");
332 		if (all) {
333 			while ((fs = getfsent()) != NULL) {
334 				if (BADTYPE(fs->fs_type))
335 					continue;
336 				if (checkvfsname(fs->fs_vfstype, vfslist))
337 					continue;
338 				if (hasopt(fs->fs_mntops, "noauto"))
339 					continue;
340 				if (!hasopt(fs->fs_mntops, "late") && onlylate)
341 					continue;
342 				if (hasopt(fs->fs_mntops, "late") && !late)
343 					continue;
344 				if (hasopt(fs->fs_mntops, "failok"))
345 					failok = 1;
346 				else
347 					failok = 0;
348 				if (!(init_flags & MNT_UPDATE) &&
349 				    ismounted(fs, mntbuf, mntsize))
350 					continue;
351 				options = update_options(options, fs->fs_mntops,
352 				    mntbuf->f_flags);
353 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
354 				    fs->fs_file, init_flags, options,
355 				    fs->fs_mntops) && !failok)
356 					rval = 1;
357 			}
358 		} else if (fstab_style) {
359 			for (i = 0; i < mntsize; i++) {
360 				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
361 					continue;
362 				putfsent(&mntbuf[i]);
363 			}
364 		} else {
365 			for (i = 0; i < mntsize; i++) {
366 				if (checkvfsname(mntbuf[i].f_fstypename,
367 				    vfslist))
368 					continue;
369 				if (!verbose &&
370 				    (mntbuf[i].f_flags & MNT_IGNORE) != 0)
371 					continue;
372 				prmount(&mntbuf[i]);
373 			}
374 		}
375 		exit(rval);
376 	case 1:
377 		if (vfslist != NULL)
378 			usage();
379 
380 		rmslashes(*argv, *argv);
381 		if (init_flags & MNT_UPDATE) {
382 			mntfromname = NULL;
383 			have_fstab = 0;
384 			if ((mntbuf = getmntpt(*argv)) == NULL)
385 				errx(1, "not currently mounted %s", *argv);
386 			/*
387 			 * Only get the mntflags from fstab if both mntpoint
388 			 * and mntspec are identical. Also handle the special
389 			 * case where just '/' is mounted and 'spec' is not
390 			 * identical with the one from fstab ('/dev' is missing
391 			 * in the spec-string at boot-time).
392 			 */
393 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
394 				if (strcmp(fs->fs_spec,
395 				    mntbuf->f_mntfromname) == 0 &&
396 				    strcmp(fs->fs_file,
397 				    mntbuf->f_mntonname) == 0) {
398 					have_fstab = 1;
399 					mntfromname = mntbuf->f_mntfromname;
400 				} else if (argv[0][0] == '/' &&
401 				    argv[0][1] == '\0' &&
402 				    strcmp(fs->fs_vfstype,
403 				    mntbuf->f_fstypename) == 0) {
404 					fs = getfsfile("/");
405 					have_fstab = 1;
406 					mntfromname = fs->fs_spec;
407 				}
408 			}
409 			if (have_fstab) {
410 				options = update_options(options, fs->fs_mntops,
411 				    mntbuf->f_flags);
412 			} else {
413 				mntfromname = mntbuf->f_mntfromname;
414 				options = update_options(options, NULL,
415 				    mntbuf->f_flags);
416 			}
417 			rval = mountfs(mntbuf->f_fstypename, mntfromname,
418 			    mntbuf->f_mntonname, init_flags, options, 0);
419 			break;
420 		}
421 		if ((fs = getfsfile(*argv)) == NULL &&
422 		    (fs = getfsspec(*argv)) == NULL)
423 			errx(1, "%s: unknown special file or file system",
424 			    *argv);
425 		if (BADTYPE(fs->fs_type))
426 			errx(1, "%s has unknown file system type",
427 			    *argv);
428 		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
429 		    init_flags, options, fs->fs_mntops);
430 		break;
431 	case 2:
432 		/*
433 		 * If -t flag has not been specified, the path cannot be
434 		 * found, spec contains either a ':' or a '@', then assume
435 		 * that an NFS file system is being specified ala Sun.
436 		 * Check if the hostname contains only allowed characters
437 		 * to reduce false positives.  IPv6 addresses containing
438 		 * ':' will be correctly parsed only if the separator is '@'.
439 		 * The definition of a valid hostname is taken from RFC 1034.
440 		 */
441 		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
442 		    (ep = strchr(argv[0], ':')) != NULL)) {
443 			if (*ep == '@') {
444 				cp = ep + 1;
445 				ep = cp + strlen(cp);
446 			} else
447 				cp = argv[0];
448 			while (cp != ep) {
449 				if (!isdigit(*cp) && !isalpha(*cp) &&
450 				    *cp != '.' && *cp != '-' && *cp != ':')
451 					break;
452 				cp++;
453 			}
454 			if (cp == ep)
455 				vfstype = "nfs";
456 		}
457 		rval = mountfs(vfstype,
458 		    argv[0], argv[1], init_flags, options, NULL);
459 		break;
460 	default:
461 		usage();
462 		/* NOTREACHED */
463 	}
464 
465 	/*
466 	 * If the mount was successfully, and done by root, tell mountd the
467 	 * good news.
468 	 */
469 	if (rval == 0 && getuid() == 0)
470 		restart_mountd();
471 
472 	exit(rval);
473 }
474 
475 int
476 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
477 {
478 	char realfsfile[PATH_MAX];
479 	int i;
480 
481 	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
482 		/* the root file system can always be remounted */
483 		return (0);
484 
485 	/* The user may have specified a symlink in fstab, resolve the path */
486 	if (realpath(fs->fs_file, realfsfile) == NULL) {
487 		/* Cannot resolve the path, use original one */
488 		strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
489 	}
490 
491 	/*
492 	 * Consider the filesystem to be mounted if:
493 	 * It has the same mountpoint as a mounted filesytem, and
494 	 * It has the same type as that same mounted filesystem, and
495 	 * It has the same device name as that same mounted filesystem, OR
496 	 *     It is a nonremountable filesystem
497 	 */
498 	for (i = mntsize - 1; i >= 0; --i)
499 		if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
500 		    strcmp(fs->fs_vfstype, mntbuf[i].f_fstypename) == 0 &&
501 		    (!isremountable(fs->fs_vfstype) ||
502 		     (strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)))
503 			return (1);
504 	return (0);
505 }
506 
507 int
508 isremountable(const char *vfsname)
509 {
510 	const char **cp;
511 
512 	for (cp = remountable_fs_names; *cp; cp++)
513 		if (strcmp(*cp, vfsname) == 0)
514 			return (1);
515 	return (0);
516 }
517 
518 int
519 hasopt(const char *mntopts, const char *option)
520 {
521 	int negative, found;
522 	char *opt, *optbuf;
523 
524 	if (option[0] == 'n' && option[1] == 'o') {
525 		negative = 1;
526 		option += 2;
527 	} else
528 		negative = 0;
529 	optbuf = strdup(mntopts);
530 	found = 0;
531 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
532 		if (opt[0] == 'n' && opt[1] == 'o') {
533 			if (!strcasecmp(opt + 2, option))
534 				found = negative;
535 		} else if (!strcasecmp(opt, option))
536 			found = !negative;
537 	}
538 	free(optbuf);
539 	return (found);
540 }
541 
542 static void
543 append_arg(struct cpa *sa, char *arg)
544 {
545 	if (sa->c + 1 == sa->sz) {
546 		sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
547 		sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz);
548 		if (sa->a == NULL)
549 			errx(1, "realloc failed");
550 	}
551 	sa->a[++sa->c] = arg;
552 }
553 
554 int
555 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
556 	const char *options, const char *mntopts)
557 {
558 	struct statfs sf;
559 	int i, ret;
560 	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
561 	static struct cpa mnt_argv;
562 
563 	/* resolve the mountpoint with realpath(3) */
564 	if (checkpath(name, mntpath) != 0) {
565 		warn("%s", mntpath);
566 		return (1);
567 	}
568 	name = mntpath;
569 
570 	if (mntopts == NULL)
571 		mntopts = "";
572 	optbuf = catopt(strdup(mntopts), options);
573 
574 	if (strcmp(name, "/") == 0)
575 		flags |= MNT_UPDATE;
576 	if (flags & MNT_FORCE)
577 		optbuf = catopt(optbuf, "force");
578 	if (flags & MNT_RDONLY)
579 		optbuf = catopt(optbuf, "ro");
580 	/*
581 	 * XXX
582 	 * The mount_mfs (newfs) command uses -o to select the
583 	 * optimization mode.  We don't pass the default "-o rw"
584 	 * for that reason.
585 	 */
586 	if (flags & MNT_UPDATE)
587 		optbuf = catopt(optbuf, "update");
588 
589 	/* Compatibility glue. */
590 	if (strcmp(vfstype, "msdos") == 0) {
591 		warnx(
592 		    "Using \"-t msdosfs\", since \"-t msdos\" is deprecated.");
593 		vfstype = "msdosfs";
594 	}
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