xref: /freebsd/sbin/mount/mount.c (revision 7bead17ddc28dbbd27424a098f3a8834500351bf)
1fba1c154SSteve Price /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
48fae3551SRodney W. Grimes  * Copyright (c) 1980, 1989, 1993, 1994
58fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
68fae3551SRodney W. Grimes  *
78fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
88fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
98fae3551SRodney W. Grimes  * are met:
108fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
118fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
128fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
138fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
148fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
168fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
178fae3551SRodney W. Grimes  *    without specific prior written permission.
188fae3551SRodney W. Grimes  *
198fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
208fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
238fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298fae3551SRodney W. Grimes  * SUCH DAMAGE.
308fae3551SRodney W. Grimes  */
318fae3551SRodney W. Grimes 
328fae3551SRodney W. Grimes #ifndef lint
33fba1c154SSteve Price static const char copyright[] =
348fae3551SRodney W. Grimes "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
358fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
36fba1c154SSteve Price #if 0
37c06fe0a0SPeter Wemm static char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
38fba1c154SSteve Price #endif
398fae3551SRodney W. Grimes #endif /* not lint */
408fae3551SRodney W. Grimes 
4100356118SDavid E. O'Brien #include <sys/cdefs.h>
4200356118SDavid E. O'Brien __FBSDID("$FreeBSD$");
4300356118SDavid E. O'Brien 
448fae3551SRodney W. Grimes #include <sys/param.h>
458fae3551SRodney W. Grimes #include <sys/mount.h>
468a978495SDavid Greenman #include <sys/stat.h>
4774cf460bSBruce Evans #include <sys/wait.h>
488fae3551SRodney W. Grimes 
49003dbca6SMaxime Henrion #include <ctype.h>
508fae3551SRodney W. Grimes #include <err.h>
518fae3551SRodney W. Grimes #include <errno.h>
528fae3551SRodney W. Grimes #include <fstab.h>
53a3ba4c65SGordon Tetlow #include <paths.h>
54c06fe0a0SPeter Wemm #include <pwd.h>
558fae3551SRodney W. Grimes #include <signal.h>
5696c65ccbSIan Dowse #include <stdint.h>
578fae3551SRodney W. Grimes #include <stdio.h>
588fae3551SRodney W. Grimes #include <stdlib.h>
598fae3551SRodney W. Grimes #include <string.h>
608fae3551SRodney W. Grimes #include <unistd.h>
61e9988cedSPawel Jakub Dawidek #include <libutil.h>
628fae3551SRodney W. Grimes 
63fba1c154SSteve Price #include "extern.h"
6473dd3167SPoul-Henning Kamp #include "mntopts.h"
658fae3551SRodney W. Grimes #include "pathnames.h"
668fae3551SRodney W. Grimes 
6718af6044SJoseph Koshy /* `meta' options */
6818af6044SJoseph Koshy #define MOUNT_META_OPTION_FSTAB		"fstab"
6918af6044SJoseph Koshy #define MOUNT_META_OPTION_CURRENT	"current"
7018af6044SJoseph Koshy 
711efe3c6bSEd Schouten static int debug, fstab_style, verbose;
72a257a45eSJordan K. Hubbard 
73a86de995SDavid E. O'Brien struct cpa {
7400356118SDavid E. O'Brien 	char	**a;
750e969ed7SDavid E. O'Brien 	ssize_t	sz;
76a86de995SDavid E. O'Brien 	int	c;
77a86de995SDavid E. O'Brien };
78a86de995SDavid E. O'Brien 
7985429990SWarner Losh char   *catopt(char *, const char *);
8085429990SWarner Losh struct statfs *getmntpt(const char *);
8185429990SWarner Losh int	hasopt(const char *, const char *);
8285429990SWarner Losh int	ismounted(struct fstab *, struct statfs *, int);
8385429990SWarner Losh int	isremountable(const char *);
84a86de995SDavid E. O'Brien void	mangle(char *, struct cpa *);
8585429990SWarner Losh char   *update_options(char *, char *, int);
8685429990SWarner Losh int	mountfs(const char *, const char *, const char *,
8785429990SWarner Losh 			int, const char *, const char *);
8885429990SWarner Losh void	remopt(char *, const char *);
8985429990SWarner Losh void	prmount(struct statfs *);
90031ea52fSMatteo Riondato void	putfsent(struct statfs *);
9185429990SWarner Losh void	usage(void);
9285429990SWarner Losh char   *flags2opts(int);
938fae3551SRodney W. Grimes 
94bcb1d846SPhilippe Charnier /* Map from mount options to printable formats. */
958fae3551SRodney W. Grimes static struct opt {
969df3a164SBjoern A. Zeeb 	uint64_t o_opt;
978fae3551SRodney W. Grimes 	const char *o_name;
988fae3551SRodney W. Grimes } optnames[] = {
998fae3551SRodney W. Grimes 	{ MNT_ASYNC,		"asynchronous" },
1008fae3551SRodney W. Grimes 	{ MNT_EXPORTED,		"NFS exported" },
1018fae3551SRodney W. Grimes 	{ MNT_LOCAL,		"local" },
10255e50aceSDavid Greenman 	{ MNT_NOATIME,		"noatime" },
1038fae3551SRodney W. Grimes 	{ MNT_NOEXEC,		"noexec" },
1048fae3551SRodney W. Grimes 	{ MNT_NOSUID,		"nosuid" },
1055ddc8dedSWolfram Schneider 	{ MNT_NOSYMFOLLOW,	"nosymfollow" },
1068fae3551SRodney W. Grimes 	{ MNT_QUOTA,		"with quotas" },
1078fae3551SRodney W. Grimes 	{ MNT_RDONLY,		"read-only" },
1088fae3551SRodney W. Grimes 	{ MNT_SYNCHRONOUS,	"synchronous" },
1098fae3551SRodney W. Grimes 	{ MNT_UNION,		"union" },
11075b714acSKATO Takenori 	{ MNT_NOCLUSTERR,	"noclusterr" },
11175b714acSKATO Takenori 	{ MNT_NOCLUSTERW,	"noclusterw" },
11252bf64c7SJulian Elischer 	{ MNT_SUIDDIR,		"suiddir" },
113b1897c19SJulian Elischer 	{ MNT_SOFTDEP,		"soft-updates" },
114d716efa9SKirk McKusick 	{ MNT_SUJ,		"journaled soft-updates" },
115ba0fbe96SRobert Watson 	{ MNT_MULTILABEL,	"multilabel" },
11603d94b50SRobert Watson 	{ MNT_ACLS,		"acls" },
1179340fc72SEdward Tomasz Napierala 	{ MNT_NFS4ACLS,		"nfsv4acls" },
118ac88569cSPawel Jakub Dawidek 	{ MNT_GJOURNAL,		"gjournal" },
1193914ddf8SEdward Tomasz Napierala 	{ MNT_AUTOMOUNTED,	"automounted" },
1209a81ba0fSStephen J. Kiernan 	{ MNT_VERIFIED,		"verified" },
12118af6044SJoseph Koshy 	{ 0, NULL }
1228fae3551SRodney W. Grimes };
1238fae3551SRodney W. Grimes 
124fba1c154SSteve Price /*
125fba1c154SSteve Price  * List of VFS types that can be remounted without becoming mounted on top
126fba1c154SSteve Price  * of each other.
127fba1c154SSteve Price  * XXX Is this list correct?
128fba1c154SSteve Price  */
129fba1c154SSteve Price static const char *
130fba1c154SSteve Price remountable_fs_names[] = {
1315a4420e3SAlexey Zelkin 	"ufs", "ffs", "ext2fs",
132fba1c154SSteve Price 	0
133fba1c154SSteve Price };
134fba1c154SSteve Price 
1356e74fb9dSMaxim Konovalov static const char userquotaeq[] = "userquota=";
1366e74fb9dSMaxim Konovalov static const char groupquotaeq[] = "groupquota=";
1376e74fb9dSMaxim Konovalov 
138c7835769SCraig Rodrigues static char *mountprog = NULL;
139c7835769SCraig Rodrigues 
1406f5f1a6bSCraig Rodrigues static int
1416f5f1a6bSCraig Rodrigues use_mountprog(const char *vfstype)
1426f5f1a6bSCraig Rodrigues {
1436f5f1a6bSCraig Rodrigues 	/* XXX: We need to get away from implementing external mount
1446f5f1a6bSCraig Rodrigues 	 *      programs for every filesystem, and move towards having
1456f5f1a6bSCraig Rodrigues 	 *	each filesystem properly implement the nmount() system call.
1466f5f1a6bSCraig Rodrigues 	 */
1476f5f1a6bSCraig Rodrigues 	unsigned int i;
1486f5f1a6bSCraig Rodrigues 	const char *fs[] = {
149a42ac676SAttilio Rao 	"cd9660", "mfs", "msdosfs", "nfs",
1504e25c86fSEdward Tomasz Napierala 	"nullfs", "smbfs", "udf", "unionfs",
1516f5f1a6bSCraig Rodrigues 	NULL
1526f5f1a6bSCraig Rodrigues 	};
1536f5f1a6bSCraig Rodrigues 
154c7835769SCraig Rodrigues 	if (mountprog != NULL)
155c7835769SCraig Rodrigues 		return (1);
156c7835769SCraig Rodrigues 
1576f5f1a6bSCraig Rodrigues 	for (i = 0; fs[i] != NULL; ++i) {
1586f5f1a6bSCraig Rodrigues 		if (strcmp(vfstype, fs[i]) == 0)
1594796c6ccSJuli Mallett 			return (1);
1606f5f1a6bSCraig Rodrigues 	}
1616f5f1a6bSCraig Rodrigues 
1624796c6ccSJuli Mallett 	return (0);
1636f5f1a6bSCraig Rodrigues }
1646f5f1a6bSCraig Rodrigues 
1656f5f1a6bSCraig Rodrigues static int
1664796c6ccSJuli Mallett exec_mountprog(const char *name, const char *execname, char *const argv[])
1676f5f1a6bSCraig Rodrigues {
1686f5f1a6bSCraig Rodrigues 	pid_t pid;
1696f5f1a6bSCraig Rodrigues 	int status;
1706f5f1a6bSCraig Rodrigues 
1716f5f1a6bSCraig Rodrigues 	switch (pid = fork()) {
1726f5f1a6bSCraig Rodrigues 	case -1:				/* Error. */
1736f5f1a6bSCraig Rodrigues 		warn("fork");
1746f5f1a6bSCraig Rodrigues 		exit (1);
1756f5f1a6bSCraig Rodrigues 	case 0:					/* Child. */
1766f5f1a6bSCraig Rodrigues 		/* Go find an executable. */
1776f5f1a6bSCraig Rodrigues 		execvP(execname, _PATH_SYSPATH, argv);
1786f5f1a6bSCraig Rodrigues 		if (errno == ENOENT) {
179c7835769SCraig Rodrigues 			warn("exec %s not found", execname);
180c7835769SCraig Rodrigues 			if (execname[0] != '/') {
181c7835769SCraig Rodrigues 				warnx("in path: %s", _PATH_SYSPATH);
182c7835769SCraig Rodrigues 			}
1836f5f1a6bSCraig Rodrigues 		}
1846f5f1a6bSCraig Rodrigues 		exit(1);
1856f5f1a6bSCraig Rodrigues 	default:				/* Parent. */
1866f5f1a6bSCraig Rodrigues 		if (waitpid(pid, &status, 0) < 0) {
1876f5f1a6bSCraig Rodrigues 			warn("waitpid");
1886f5f1a6bSCraig Rodrigues 			return (1);
1896f5f1a6bSCraig Rodrigues 		}
1906f5f1a6bSCraig Rodrigues 
1916f5f1a6bSCraig Rodrigues 		if (WIFEXITED(status)) {
1926f5f1a6bSCraig Rodrigues 			if (WEXITSTATUS(status) != 0)
1936f5f1a6bSCraig Rodrigues 				return (WEXITSTATUS(status));
1946f5f1a6bSCraig Rodrigues 		} else if (WIFSIGNALED(status)) {
1956f5f1a6bSCraig Rodrigues 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
1966f5f1a6bSCraig Rodrigues 			return (1);
1976f5f1a6bSCraig Rodrigues 		}
1986f5f1a6bSCraig Rodrigues 		break;
1996f5f1a6bSCraig Rodrigues 	}
2006f5f1a6bSCraig Rodrigues 
2016f5f1a6bSCraig Rodrigues 	return (0);
2026f5f1a6bSCraig Rodrigues }
2036f5f1a6bSCraig Rodrigues 
20449a41c4fSRuslan Ermilov static int
20549a41c4fSRuslan Ermilov specified_ro(const char *arg)
2063cbf527eSRuslan Ermilov {
2073cbf527eSRuslan Ermilov 	char *optbuf, *opt;
2083cbf527eSRuslan Ermilov 	int ret = 0;
2093cbf527eSRuslan Ermilov 
2103cbf527eSRuslan Ermilov 	optbuf = strdup(arg);
2113cbf527eSRuslan Ermilov 	if (optbuf == NULL)
2123cbf527eSRuslan Ermilov 		 err(1, NULL);
2133cbf527eSRuslan Ermilov 
2143cbf527eSRuslan Ermilov 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
2153cbf527eSRuslan Ermilov 		if (strcmp(opt, "ro") == 0) {
2163cbf527eSRuslan Ermilov 			ret = 1;
2173cbf527eSRuslan Ermilov 			break;
2183cbf527eSRuslan Ermilov 		}
2193cbf527eSRuslan Ermilov 	}
2203cbf527eSRuslan Ermilov 	free(optbuf);
2213cbf527eSRuslan Ermilov 	return (ret);
2223cbf527eSRuslan Ermilov }
2233cbf527eSRuslan Ermilov 
224e9988cedSPawel Jakub Dawidek static void
225e9988cedSPawel Jakub Dawidek restart_mountd(void)
226e9988cedSPawel Jakub Dawidek {
227e9988cedSPawel Jakub Dawidek 	struct pidfh *pfh;
228e9988cedSPawel Jakub Dawidek 	pid_t mountdpid;
229e9988cedSPawel Jakub Dawidek 
230*7bead17dSKonstantin Belousov 	mountdpid = 0;
231e9988cedSPawel Jakub Dawidek 	pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
232e9988cedSPawel Jakub Dawidek 	if (pfh != NULL) {
233e9988cedSPawel Jakub Dawidek 		/* Mountd is not running. */
234e9988cedSPawel Jakub Dawidek 		pidfile_remove(pfh);
235e9988cedSPawel Jakub Dawidek 		return;
236e9988cedSPawel Jakub Dawidek 	}
237e9988cedSPawel Jakub Dawidek 	if (errno != EEXIST) {
238e9988cedSPawel Jakub Dawidek 		/* Cannot open pidfile for some reason. */
239e9988cedSPawel Jakub Dawidek 		return;
240e9988cedSPawel Jakub Dawidek 	}
241*7bead17dSKonstantin Belousov 
242*7bead17dSKonstantin Belousov 	/*
243*7bead17dSKonstantin Belousov 	 * Refuse to send broadcast or group signals, this has
244*7bead17dSKonstantin Belousov 	 * happened due to the bugs in pidfile(3).
245*7bead17dSKonstantin Belousov 	 */
246*7bead17dSKonstantin Belousov 	if (mountdpid <= 0) {
247*7bead17dSKonstantin Belousov 		warnx("mountd pid %d, refusing to send SIGHUP", mountdpid);
248*7bead17dSKonstantin Belousov 		return;
249*7bead17dSKonstantin Belousov 	}
250*7bead17dSKonstantin Belousov 
251e9988cedSPawel Jakub Dawidek 	/* We have mountd(8) PID in mountdpid varible, let's signal it. */
252e9988cedSPawel Jakub Dawidek 	if (kill(mountdpid, SIGHUP) == -1)
253e9988cedSPawel Jakub Dawidek 		err(1, "signal mountd");
254e9988cedSPawel Jakub Dawidek }
255e9988cedSPawel Jakub Dawidek 
2568fae3551SRodney W. Grimes int
257e24dc56aSCraig Rodrigues main(int argc, char *argv[])
2588fae3551SRodney W. Grimes {
259c06fe0a0SPeter Wemm 	const char *mntfromname, **vfslist, *vfstype;
2608fae3551SRodney W. Grimes 	struct fstab *fs;
2618fae3551SRodney W. Grimes 	struct statfs *mntbuf;
262c7383075SXin LI 	int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
263b48b774fSChris Rees 	int onlylate;
264003dbca6SMaxime Henrion 	char *cp, *ep, *options;
2658fae3551SRodney W. Grimes 
266b48b774fSChris Rees 	all = init_flags = late = onlylate = 0;
2673cbf527eSRuslan Ermilov 	ro = 0;
2683cbf527eSRuslan Ermilov 	options = NULL;
2698fae3551SRodney W. Grimes 	vfslist = NULL;
2708fae3551SRodney W. Grimes 	vfstype = "ufs";
271fc98db27SRobert Millan 	while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
2728fae3551SRodney W. Grimes 		switch (ch) {
2738fae3551SRodney W. Grimes 		case 'a':
2748fae3551SRodney W. Grimes 			all = 1;
2758fae3551SRodney W. Grimes 			break;
2768fae3551SRodney W. Grimes 		case 'd':
2778fae3551SRodney W. Grimes 			debug = 1;
2788fae3551SRodney W. Grimes 			break;
279ef258dd9SMatthew N. Dodd 		case 'F':
280ef258dd9SMatthew N. Dodd 			setfstab(optarg);
281ef258dd9SMatthew N. Dodd 			break;
2828fae3551SRodney W. Grimes 		case 'f':
2838fae3551SRodney W. Grimes 			init_flags |= MNT_FORCE;
2848fae3551SRodney W. Grimes 			break;
285b48b774fSChris Rees 		case 'L':
286b48b774fSChris Rees 			onlylate = 1;
287b48b774fSChris Rees 			late = 1;
288b48b774fSChris Rees 			break;
2894b4f9170SDag-Erling Smørgrav 		case 'l':
2904b4f9170SDag-Erling Smørgrav 			late = 1;
2914b4f9170SDag-Erling Smørgrav 			break;
292fc98db27SRobert Millan 		case 'n':
293fc98db27SRobert Millan 			/* For compatibility with the Linux version of mount. */
294fc98db27SRobert Millan 			break;
2958fae3551SRodney W. Grimes 		case 'o':
2963cbf527eSRuslan Ermilov 			if (*optarg) {
2978fae3551SRodney W. Grimes 				options = catopt(options, optarg);
2983cbf527eSRuslan Ermilov 				if (specified_ro(optarg))
2993cbf527eSRuslan Ermilov 					ro = 1;
3003cbf527eSRuslan Ermilov 			}
3018fae3551SRodney W. Grimes 			break;
30274cf460bSBruce Evans 		case 'p':
30374cf460bSBruce Evans 			fstab_style = 1;
30474cf460bSBruce Evans 			verbose = 1;
30574cf460bSBruce Evans 			break;
3068fae3551SRodney W. Grimes 		case 'r':
307ad044771SDima Dorfman 			options = catopt(options, "ro");
3083cbf527eSRuslan Ermilov 			ro = 1;
3098fae3551SRodney W. Grimes 			break;
3108fae3551SRodney W. Grimes 		case 't':
3118fae3551SRodney W. Grimes 			if (vfslist != NULL)
312bcb1d846SPhilippe Charnier 				errx(1, "only one -t option may be specified");
3138fae3551SRodney W. Grimes 			vfslist = makevfslist(optarg);
3148fae3551SRodney W. Grimes 			vfstype = optarg;
3158fae3551SRodney W. Grimes 			break;
3168fae3551SRodney W. Grimes 		case 'u':
3178fae3551SRodney W. Grimes 			init_flags |= MNT_UPDATE;
3188fae3551SRodney W. Grimes 			break;
3198fae3551SRodney W. Grimes 		case 'v':
3208fae3551SRodney W. Grimes 			verbose = 1;
3218fae3551SRodney W. Grimes 			break;
3228fae3551SRodney W. Grimes 		case 'w':
323ad044771SDima Dorfman 			options = catopt(options, "noro");
3248fae3551SRodney W. Grimes 			break;
3258fae3551SRodney W. Grimes 		case '?':
3268fae3551SRodney W. Grimes 		default:
3278fae3551SRodney W. Grimes 			usage();
3288fae3551SRodney W. Grimes 			/* NOTREACHED */
3298fae3551SRodney W. Grimes 		}
3308fae3551SRodney W. Grimes 	argc -= optind;
3318fae3551SRodney W. Grimes 	argv += optind;
3328fae3551SRodney W. Grimes 
3338fae3551SRodney W. Grimes #define	BADTYPE(type)							\
3348fae3551SRodney W. Grimes 	(strcmp(type, FSTAB_RO) &&					\
3358fae3551SRodney W. Grimes 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
3368fae3551SRodney W. Grimes 
3373cbf527eSRuslan Ermilov 	if ((init_flags & MNT_UPDATE) && (ro == 0))
3383cbf527eSRuslan Ermilov 		options = catopt(options, "noro");
3393cbf527eSRuslan Ermilov 
3408fae3551SRodney W. Grimes 	rval = 0;
3418fae3551SRodney W. Grimes 	switch (argc) {
3428fae3551SRodney W. Grimes 	case 0:
3434818bd98SKirk McKusick 		if ((mntsize = getmntinfo(&mntbuf,
3444818bd98SKirk McKusick 		     verbose ? MNT_WAIT : MNT_NOWAIT)) == 0)
345fba1c154SSteve Price 			err(1, "getmntinfo");
346fba1c154SSteve Price 		if (all) {
3478fae3551SRodney W. Grimes 			while ((fs = getfsent()) != NULL) {
3488fae3551SRodney W. Grimes 				if (BADTYPE(fs->fs_type))
3498fae3551SRodney W. Grimes 					continue;
350c06fe0a0SPeter Wemm 				if (checkvfsname(fs->fs_vfstype, vfslist))
3518fae3551SRodney W. Grimes 					continue;
352c06fe0a0SPeter Wemm 				if (hasopt(fs->fs_mntops, "noauto"))
35389beb278SDavid Greenman 					continue;
354b48b774fSChris Rees 				if (!hasopt(fs->fs_mntops, "late") && onlylate)
355b48b774fSChris Rees 					continue;
3564b4f9170SDag-Erling Smørgrav 				if (hasopt(fs->fs_mntops, "late") && !late)
3574b4f9170SDag-Erling Smørgrav 					continue;
358c7383075SXin LI 				if (hasopt(fs->fs_mntops, "failok"))
359c7383075SXin LI 					failok = 1;
360c7383075SXin LI 				else
361c7383075SXin LI 					failok = 0;
36298201b0cSBruce Evans 				if (!(init_flags & MNT_UPDATE) &&
36398201b0cSBruce Evans 				    ismounted(fs, mntbuf, mntsize))
364fba1c154SSteve Price 					continue;
36513cbdf24SGuido van Rooij 				options = update_options(options, fs->fs_mntops,
36613cbdf24SGuido van Rooij 				    mntbuf->f_flags);
3678fae3551SRodney W. Grimes 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
3688fae3551SRodney W. Grimes 				    fs->fs_file, init_flags, options,
369c7383075SXin LI 				    fs->fs_mntops) && !failok)
3708fae3551SRodney W. Grimes 					rval = 1;
3718fae3551SRodney W. Grimes 			}
372fba1c154SSteve Price 		} else if (fstab_style) {
373a257a45eSJordan K. Hubbard 			for (i = 0; i < mntsize; i++) {
374c06fe0a0SPeter Wemm 				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
375a257a45eSJordan K. Hubbard 					continue;
376a257a45eSJordan K. Hubbard 				putfsent(&mntbuf[i]);
377a257a45eSJordan K. Hubbard 			}
37874cf460bSBruce Evans 		} else {
3798fae3551SRodney W. Grimes 			for (i = 0; i < mntsize; i++) {
38074cf460bSBruce Evans 				if (checkvfsname(mntbuf[i].f_fstypename,
38174cf460bSBruce Evans 				    vfslist))
3828fae3551SRodney W. Grimes 					continue;
3838abb2a6eSPawel Jakub Dawidek 				if (!verbose &&
3848abb2a6eSPawel Jakub Dawidek 				    (mntbuf[i].f_flags & MNT_IGNORE) != 0)
3858abb2a6eSPawel Jakub Dawidek 					continue;
386c06fe0a0SPeter Wemm 				prmount(&mntbuf[i]);
3878fae3551SRodney W. Grimes 			}
3888fae3551SRodney W. Grimes 		}
3898fae3551SRodney W. Grimes 		exit(rval);
3908fae3551SRodney W. Grimes 	case 1:
3918fae3551SRodney W. Grimes 		if (vfslist != NULL)
3928fae3551SRodney W. Grimes 			usage();
3938fae3551SRodney W. Grimes 
39401a9bce5SEric Anholt 		rmslashes(*argv, *argv);
3958fae3551SRodney W. Grimes 		if (init_flags & MNT_UPDATE) {
396cf96af72SBrian Feldman 			mntfromname = NULL;
397cf96af72SBrian Feldman 			have_fstab = 0;
3988fae3551SRodney W. Grimes 			if ((mntbuf = getmntpt(*argv)) == NULL)
399cf96af72SBrian Feldman 				errx(1, "not currently mounted %s", *argv);
400cf96af72SBrian Feldman 			/*
401cf96af72SBrian Feldman 			 * Only get the mntflags from fstab if both mntpoint
402cf96af72SBrian Feldman 			 * and mntspec are identical. Also handle the special
403cf96af72SBrian Feldman 			 * case where just '/' is mounted and 'spec' is not
404cf96af72SBrian Feldman 			 * identical with the one from fstab ('/dev' is missing
405cf96af72SBrian Feldman 			 * in the spec-string at boot-time).
406cf96af72SBrian Feldman 			 */
40718af6044SJoseph Koshy 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
408cf96af72SBrian Feldman 				if (strcmp(fs->fs_spec,
409cf96af72SBrian Feldman 				    mntbuf->f_mntfromname) == 0 &&
410cf96af72SBrian Feldman 				    strcmp(fs->fs_file,
411cf96af72SBrian Feldman 				    mntbuf->f_mntonname) == 0) {
412cf96af72SBrian Feldman 					have_fstab = 1;
413cf96af72SBrian Feldman 					mntfromname = mntbuf->f_mntfromname;
414cf96af72SBrian Feldman 				} else if (argv[0][0] == '/' &&
4151f3aded0SEdward Tomasz Napierala 				    argv[0][1] == '\0' &&
4161f3aded0SEdward Tomasz Napierala 				    strcmp(fs->fs_vfstype,
4171f3aded0SEdward Tomasz Napierala 				    mntbuf->f_fstypename) == 0) {
418cf96af72SBrian Feldman 					fs = getfsfile("/");
419cf96af72SBrian Feldman 					have_fstab = 1;
420c06fe0a0SPeter Wemm 					mntfromname = fs->fs_spec;
421cf96af72SBrian Feldman 				}
422cf96af72SBrian Feldman 			}
423cf96af72SBrian Feldman 			if (have_fstab) {
42418af6044SJoseph Koshy 				options = update_options(options, fs->fs_mntops,
42518af6044SJoseph Koshy 				    mntbuf->f_flags);
42618af6044SJoseph Koshy 			} else {
427c06fe0a0SPeter Wemm 				mntfromname = mntbuf->f_mntfromname;
42818af6044SJoseph Koshy 				options = update_options(options, NULL,
42918af6044SJoseph Koshy 				    mntbuf->f_flags);
43018af6044SJoseph Koshy 			}
431c06fe0a0SPeter Wemm 			rval = mountfs(mntbuf->f_fstypename, mntfromname,
432c06fe0a0SPeter Wemm 			    mntbuf->f_mntonname, init_flags, options, 0);
433c06fe0a0SPeter Wemm 			break;
434c06fe0a0SPeter Wemm 		}
4358fae3551SRodney W. Grimes 		if ((fs = getfsfile(*argv)) == NULL &&
4368fae3551SRodney W. Grimes 		    (fs = getfsspec(*argv)) == NULL)
437bcb1d846SPhilippe Charnier 			errx(1, "%s: unknown special file or file system",
4388fae3551SRodney W. Grimes 			    *argv);
4398fae3551SRodney W. Grimes 		if (BADTYPE(fs->fs_type))
440bcb1d846SPhilippe Charnier 			errx(1, "%s has unknown file system type",
4418fae3551SRodney W. Grimes 			    *argv);
442c06fe0a0SPeter Wemm 		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
443c06fe0a0SPeter Wemm 		    init_flags, options, fs->fs_mntops);
4448fae3551SRodney W. Grimes 		break;
4458fae3551SRodney W. Grimes 	case 2:
4468fae3551SRodney W. Grimes 		/*
447cf96af72SBrian Feldman 		 * If -t flag has not been specified, the path cannot be
448003dbca6SMaxime Henrion 		 * found, spec contains either a ':' or a '@', then assume
449cf96af72SBrian Feldman 		 * that an NFS file system is being specified ala Sun.
450003dbca6SMaxime Henrion 		 * Check if the hostname contains only allowed characters
451003dbca6SMaxime Henrion 		 * to reduce false positives.  IPv6 addresses containing
452003dbca6SMaxime Henrion 		 * ':' will be correctly parsed only if the separator is '@'.
453003dbca6SMaxime Henrion 		 * The definition of a valid hostname is taken from RFC 1034.
4548fae3551SRodney W. Grimes 		 */
45505779418SIan Dowse 		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
45605779418SIan Dowse 		    (ep = strchr(argv[0], ':')) != NULL)) {
457da85c82bSMaxime Henrion 			if (*ep == '@') {
458da85c82bSMaxime Henrion 				cp = ep + 1;
459da85c82bSMaxime Henrion 				ep = cp + strlen(cp);
460da85c82bSMaxime Henrion 			} else
461003dbca6SMaxime Henrion 				cp = argv[0];
462003dbca6SMaxime Henrion 			while (cp != ep) {
463003dbca6SMaxime Henrion 				if (!isdigit(*cp) && !isalpha(*cp) &&
464003dbca6SMaxime Henrion 				    *cp != '.' && *cp != '-' && *cp != ':')
465003dbca6SMaxime Henrion 					break;
466003dbca6SMaxime Henrion 				cp++;
467003dbca6SMaxime Henrion 			}
468003dbca6SMaxime Henrion 			if (cp == ep)
4698fae3551SRodney W. Grimes 				vfstype = "nfs";
470003dbca6SMaxime Henrion 		}
4718fae3551SRodney W. Grimes 		rval = mountfs(vfstype,
4728fae3551SRodney W. Grimes 		    argv[0], argv[1], init_flags, options, NULL);
4738fae3551SRodney W. Grimes 		break;
4748fae3551SRodney W. Grimes 	default:
4758fae3551SRodney W. Grimes 		usage();
4768fae3551SRodney W. Grimes 		/* NOTREACHED */
4778fae3551SRodney W. Grimes 	}
4788fae3551SRodney W. Grimes 
4798fae3551SRodney W. Grimes 	/*
4808fae3551SRodney W. Grimes 	 * If the mount was successfully, and done by root, tell mountd the
481e9988cedSPawel Jakub Dawidek 	 * good news.
4828fae3551SRodney W. Grimes 	 */
483e9988cedSPawel Jakub Dawidek 	if (rval == 0 && getuid() == 0)
484e9988cedSPawel Jakub Dawidek 		restart_mountd();
4858fae3551SRodney W. Grimes 
4868fae3551SRodney W. Grimes 	exit(rval);
4878fae3551SRodney W. Grimes }
4888fae3551SRodney W. Grimes 
4898fae3551SRodney W. Grimes int
490e24dc56aSCraig Rodrigues ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
491fba1c154SSteve Price {
49290742659SPawel Jakub Dawidek 	char realfsfile[PATH_MAX];
493fba1c154SSteve Price 	int i;
494fba1c154SSteve Price 
495fba1c154SSteve Price 	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
496fba1c154SSteve Price 		/* the root file system can always be remounted */
497fba1c154SSteve Price 		return (0);
498fba1c154SSteve Price 
49990742659SPawel Jakub Dawidek 	/* The user may have specified a symlink in fstab, resolve the path */
50090742659SPawel Jakub Dawidek 	if (realpath(fs->fs_file, realfsfile) == NULL) {
50190742659SPawel Jakub Dawidek 		/* Cannot resolve the path, use original one */
50290742659SPawel Jakub Dawidek 		strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
50390742659SPawel Jakub Dawidek 	}
50490742659SPawel Jakub Dawidek 
505b5bbeb21SAlan Somers 	/*
506b5bbeb21SAlan Somers 	 * Consider the filesystem to be mounted if:
507b5bbeb21SAlan Somers 	 * It has the same mountpoint as a mounted filesytem, and
508b5bbeb21SAlan Somers 	 * It has the same type as that same mounted filesystem, and
509b5bbeb21SAlan Somers 	 * It has the same device name as that same mounted filesystem, OR
510b5bbeb21SAlan Somers 	 *     It is a nonremountable filesystem
511b5bbeb21SAlan Somers 	 */
512fba1c154SSteve Price 	for (i = mntsize - 1; i >= 0; --i)
51390742659SPawel Jakub Dawidek 		if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
514b5bbeb21SAlan Somers 		    strcmp(fs->fs_vfstype, mntbuf[i].f_fstypename) == 0 &&
515fba1c154SSteve Price 		    (!isremountable(fs->fs_vfstype) ||
516b5bbeb21SAlan Somers 		     (strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0)))
517fba1c154SSteve Price 			return (1);
518fba1c154SSteve Price 	return (0);
519fba1c154SSteve Price }
520fba1c154SSteve Price 
521fba1c154SSteve Price int
522e24dc56aSCraig Rodrigues isremountable(const char *vfsname)
523fba1c154SSteve Price {
524fba1c154SSteve Price 	const char **cp;
525fba1c154SSteve Price 
526fba1c154SSteve Price 	for (cp = remountable_fs_names; *cp; cp++)
527fba1c154SSteve Price 		if (strcmp(*cp, vfsname) == 0)
528fba1c154SSteve Price 			return (1);
529fba1c154SSteve Price 	return (0);
530fba1c154SSteve Price }
531fba1c154SSteve Price 
532fba1c154SSteve Price int
533e24dc56aSCraig Rodrigues hasopt(const char *mntopts, const char *option)
534c06fe0a0SPeter Wemm {
535c06fe0a0SPeter Wemm 	int negative, found;
536c06fe0a0SPeter Wemm 	char *opt, *optbuf;
537c06fe0a0SPeter Wemm 
538c06fe0a0SPeter Wemm 	if (option[0] == 'n' && option[1] == 'o') {
539c06fe0a0SPeter Wemm 		negative = 1;
540c06fe0a0SPeter Wemm 		option += 2;
541c06fe0a0SPeter Wemm 	} else
542c06fe0a0SPeter Wemm 		negative = 0;
543c06fe0a0SPeter Wemm 	optbuf = strdup(mntopts);
544c06fe0a0SPeter Wemm 	found = 0;
545c06fe0a0SPeter Wemm 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
546c06fe0a0SPeter Wemm 		if (opt[0] == 'n' && opt[1] == 'o') {
547c06fe0a0SPeter Wemm 			if (!strcasecmp(opt + 2, option))
548c06fe0a0SPeter Wemm 				found = negative;
549c06fe0a0SPeter Wemm 		} else if (!strcasecmp(opt, option))
550c06fe0a0SPeter Wemm 			found = !negative;
551c06fe0a0SPeter Wemm 	}
552c06fe0a0SPeter Wemm 	free(optbuf);
553c06fe0a0SPeter Wemm 	return (found);
554c06fe0a0SPeter Wemm }
555c06fe0a0SPeter Wemm 
556a86de995SDavid E. O'Brien static void
557a86de995SDavid E. O'Brien append_arg(struct cpa *sa, char *arg)
558a86de995SDavid E. O'Brien {
5590e969ed7SDavid E. O'Brien 	if (sa->c + 1 == sa->sz) {
5600e969ed7SDavid E. O'Brien 		sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
561e96092e8SUlrich Spörlein 		sa->a = realloc(sa->a, sizeof(*sa->a) * sa->sz);
56200356118SDavid E. O'Brien 		if (sa->a == NULL)
56300356118SDavid E. O'Brien 			errx(1, "realloc failed");
56400356118SDavid E. O'Brien 	}
565a86de995SDavid E. O'Brien 	sa->a[++sa->c] = arg;
566a86de995SDavid E. O'Brien }
567a86de995SDavid E. O'Brien 
568c06fe0a0SPeter Wemm int
569e24dc56aSCraig Rodrigues mountfs(const char *vfstype, const char *spec, const char *name, int flags,
570e24dc56aSCraig Rodrigues 	const char *options, const char *mntopts)
5718fae3551SRodney W. Grimes {
5728fae3551SRodney W. Grimes 	struct statfs sf;
57376c46216SDavid E. O'Brien 	int i, ret;
57468dd1ff4SWarner Losh 	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
5750e969ed7SDavid E. O'Brien 	static struct cpa mnt_argv;
5768fae3551SRodney W. Grimes 
57773dd3167SPoul-Henning Kamp 	/* resolve the mountpoint with realpath(3) */
578d3250014SJaakko Heinonen 	if (checkpath(name, mntpath) != 0) {
579d3250014SJaakko Heinonen 		warn("%s", mntpath);
580d3250014SJaakko Heinonen 		return (1);
581d3250014SJaakko Heinonen 	}
5828fae3551SRodney W. Grimes 	name = mntpath;
5838fae3551SRodney W. Grimes 
584c06fe0a0SPeter Wemm 	if (mntopts == NULL)
585c06fe0a0SPeter Wemm 		mntopts = "";
5868fae3551SRodney W. Grimes 	optbuf = catopt(strdup(mntopts), options);
5878fae3551SRodney W. Grimes 
5888fae3551SRodney W. Grimes 	if (strcmp(name, "/") == 0)
5898fae3551SRodney W. Grimes 		flags |= MNT_UPDATE;
5908fae3551SRodney W. Grimes 	if (flags & MNT_FORCE)
5918fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "force");
5928fae3551SRodney W. Grimes 	if (flags & MNT_RDONLY)
5938fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "ro");
5948fae3551SRodney W. Grimes 	/*
5958fae3551SRodney W. Grimes 	 * XXX
5968fae3551SRodney W. Grimes 	 * The mount_mfs (newfs) command uses -o to select the
597bcb1d846SPhilippe Charnier 	 * optimization mode.  We don't pass the default "-o rw"
5988fae3551SRodney W. Grimes 	 * for that reason.
5998fae3551SRodney W. Grimes 	 */
6008fae3551SRodney W. Grimes 	if (flags & MNT_UPDATE)
6018fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "update");
6028fae3551SRodney W. Grimes 
6034ccd7546SRuslan Ermilov 	/* Compatibility glue. */
6040b8c3359SWarner Losh 	if (strcmp(vfstype, "msdos") == 0)
6054ccd7546SRuslan Ermilov 		vfstype = "msdosfs";
6064ccd7546SRuslan Ermilov 
6076e34b479SColin Percival 	/* Construct the name of the appropriate mount command */
6086e34b479SColin Percival 	(void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
6096e34b479SColin Percival 
610a86de995SDavid E. O'Brien 	mnt_argv.c = -1;
611a86de995SDavid E. O'Brien 	append_arg(&mnt_argv, execname);
612a86de995SDavid E. O'Brien 	mangle(optbuf, &mnt_argv);
613c7835769SCraig Rodrigues 	if (mountprog != NULL)
6146e24000aSAlan Somers 		strlcpy(execname, mountprog, sizeof(execname));
615c7835769SCraig Rodrigues 
616a86de995SDavid E. O'Brien 	append_arg(&mnt_argv, strdup(spec));
617a86de995SDavid E. O'Brien 	append_arg(&mnt_argv, strdup(name));
618a86de995SDavid E. O'Brien 	append_arg(&mnt_argv, NULL);
619fce5f960SDavid E. O'Brien 
6208fae3551SRodney W. Grimes 	if (debug) {
621c195c7f6SCraig Rodrigues 		if (use_mountprog(vfstype))
622c7835769SCraig Rodrigues 			printf("exec: %s", execname);
623c195c7f6SCraig Rodrigues 		else
624c195c7f6SCraig Rodrigues 			printf("mount -t %s", vfstype);
625a86de995SDavid E. O'Brien 		for (i = 1; i < mnt_argv.c; i++)
626a86de995SDavid E. O'Brien 			(void)printf(" %s", mnt_argv.a[i]);
6278fae3551SRodney W. Grimes 		(void)printf("\n");
628d2e17ce9SJohn Baldwin 		free(optbuf);
629d2e17ce9SJohn Baldwin 		free(mountprog);
630d2e17ce9SJohn Baldwin 		mountprog = NULL;
6318fae3551SRodney W. Grimes 		return (0);
6328fae3551SRodney W. Grimes 	}
6338fae3551SRodney W. Grimes 
634b1e6b712SCraig Rodrigues 	if (use_mountprog(vfstype)) {
635a86de995SDavid E. O'Brien 		ret = exec_mountprog(name, execname, mnt_argv.a);
6366f5f1a6bSCraig Rodrigues 	} else {
637a86de995SDavid E. O'Brien 		ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
6386f5f1a6bSCraig Rodrigues 	}
6396f5f1a6bSCraig Rodrigues 
6408fae3551SRodney W. Grimes 	free(optbuf);
641d2e17ce9SJohn Baldwin 	free(mountprog);
642d2e17ce9SJohn Baldwin 	mountprog = NULL;
6438fae3551SRodney W. Grimes 
6448fae3551SRodney W. Grimes 	if (verbose) {
6458fae3551SRodney W. Grimes 		if (statfs(name, &sf) < 0) {
646c06fe0a0SPeter Wemm 			warn("statfs %s", name);
6478fae3551SRodney W. Grimes 			return (1);
6488fae3551SRodney W. Grimes 		}
649a257a45eSJordan K. Hubbard 		if (fstab_style)
650a257a45eSJordan K. Hubbard 			putfsent(&sf);
651a257a45eSJordan K. Hubbard 		else
652c06fe0a0SPeter Wemm 			prmount(&sf);
6538fae3551SRodney W. Grimes 	}
6548fae3551SRodney W. Grimes 
655bbe9d7daSMatteo Riondato 	return (ret);
6568fae3551SRodney W. Grimes }
6578fae3551SRodney W. Grimes 
6588fae3551SRodney W. Grimes void
659e24dc56aSCraig Rodrigues prmount(struct statfs *sfp)
6608fae3551SRodney W. Grimes {
6619df3a164SBjoern A. Zeeb 	uint64_t flags;
662aedf10acSCraig Rodrigues 	unsigned int i;
6638fae3551SRodney W. Grimes 	struct opt *o;
664c06fe0a0SPeter Wemm 	struct passwd *pw;
6658fae3551SRodney W. Grimes 
666af2ea5aaSNick Hibma 	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
667af2ea5aaSNick Hibma 	    sfp->f_fstypename);
6688fae3551SRodney W. Grimes 
669c06fe0a0SPeter Wemm 	flags = sfp->f_flags & MNT_VISFLAGMASK;
6709df3a164SBjoern A. Zeeb 	for (o = optnames; flags != 0 && o->o_opt != 0; o++)
6718fae3551SRodney W. Grimes 		if (flags & o->o_opt) {
672af2ea5aaSNick Hibma 			(void)printf(", %s", o->o_name);
6738fae3551SRodney W. Grimes 			flags &= ~o->o_opt;
6748fae3551SRodney W. Grimes 		}
675dc9c6194SPawel Jakub Dawidek 	/*
676dc9c6194SPawel Jakub Dawidek 	 * Inform when file system is mounted by an unprivileged user
677dc9c6194SPawel Jakub Dawidek 	 * or privileged non-root user.
678dc9c6194SPawel Jakub Dawidek 	 */
679ddb842ccSJacques Vidrine 	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
680af2ea5aaSNick Hibma 		(void)printf(", mounted by ");
681c06fe0a0SPeter Wemm 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
682c06fe0a0SPeter Wemm 			(void)printf("%s", pw->pw_name);
683c06fe0a0SPeter Wemm 		else
684c06fe0a0SPeter Wemm 			(void)printf("%d", sfp->f_owner);
685c06fe0a0SPeter Wemm 	}
686c62ffab6SSheldon Hearn 	if (verbose) {
687677b9b3fSBruce Evans 		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
68896c65ccbSIan Dowse 			(void)printf(", writes: sync %ju async %ju",
68996c65ccbSIan Dowse 			    (uintmax_t)sfp->f_syncwrites,
69096c65ccbSIan Dowse 			    (uintmax_t)sfp->f_asyncwrites);
691461bb71eSKirk McKusick 		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
69296c65ccbSIan Dowse 			(void)printf(", reads: sync %ju async %ju",
69396c65ccbSIan Dowse 			    (uintmax_t)sfp->f_syncreads,
69496c65ccbSIan Dowse 			    (uintmax_t)sfp->f_asyncreads);
69505779418SIan Dowse 		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
69638f102c2SIan Dowse 			printf(", fsid ");
69738f102c2SIan Dowse 			for (i = 0; i < sizeof(sfp->f_fsid); i++)
69838f102c2SIan Dowse 				printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
699c62ffab6SSheldon Hearn 		}
70005779418SIan Dowse 	}
701af2ea5aaSNick Hibma 	(void)printf(")\n");
7028fae3551SRodney W. Grimes }
7038fae3551SRodney W. Grimes 
7048fae3551SRodney W. Grimes struct statfs *
705e24dc56aSCraig Rodrigues getmntpt(const char *name)
7068fae3551SRodney W. Grimes {
7078fae3551SRodney W. Grimes 	struct statfs *mntbuf;
7088fae3551SRodney W. Grimes 	int i, mntsize;
7098fae3551SRodney W. Grimes 
710fddf7baeSKirk McKusick 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
711cf96af72SBrian Feldman 	for (i = mntsize - 1; i >= 0; i--) {
7128fae3551SRodney W. Grimes 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
7138fae3551SRodney W. Grimes 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
7148fae3551SRodney W. Grimes 			return (&mntbuf[i]);
715cf96af72SBrian Feldman 	}
7168fae3551SRodney W. Grimes 	return (NULL);
7178fae3551SRodney W. Grimes }
7188fae3551SRodney W. Grimes 
7198fae3551SRodney W. Grimes char *
720e24dc56aSCraig Rodrigues catopt(char *s0, const char *s1)
7218fae3551SRodney W. Grimes {
7228fae3551SRodney W. Grimes 	char *cp;
7238fae3551SRodney W. Grimes 
72418af6044SJoseph Koshy 	if (s1 == NULL || *s1 == '\0')
7254796c6ccSJuli Mallett 		return (s0);
72618af6044SJoseph Koshy 
7278fae3551SRodney W. Grimes 	if (s0 && *s0) {
7288967299bSPedro F. Giffuni 		if (asprintf(&cp, "%s,%s", s0, s1) == -1)
7298967299bSPedro F. Giffuni 			errx(1, "asprintf failed");
7308fae3551SRodney W. Grimes 	} else
7318fae3551SRodney W. Grimes 		cp = strdup(s1);
7328fae3551SRodney W. Grimes 
7338fae3551SRodney W. Grimes 	if (s0)
7348fae3551SRodney W. Grimes 		free(s0);
7358fae3551SRodney W. Grimes 	return (cp);
7368fae3551SRodney W. Grimes }
7378fae3551SRodney W. Grimes 
7388fae3551SRodney W. Grimes void
739a86de995SDavid E. O'Brien mangle(char *options, struct cpa *a)
7408fae3551SRodney W. Grimes {
741c7835769SCraig Rodrigues 	char *p, *s, *val;
7428fae3551SRodney W. Grimes 
7438fae3551SRodney W. Grimes 	for (s = options; (p = strsep(&s, ",")) != NULL;)
74418af6044SJoseph Koshy 		if (*p != '\0') {
745d9fa6ce7SCraig Rodrigues 			if (strcmp(p, "noauto") == 0) {
746d9fa6ce7SCraig Rodrigues 				/*
747d9fa6ce7SCraig Rodrigues 				 * Do not pass noauto option to nmount().
748d9fa6ce7SCraig Rodrigues 				 * or external mount program.  noauto is
749d9fa6ce7SCraig Rodrigues 				 * only used to prevent mounting a filesystem
750d9fa6ce7SCraig Rodrigues 				 * when 'mount -a' is specified, and is
751d9fa6ce7SCraig Rodrigues 				 * not a real mount option.
752d9fa6ce7SCraig Rodrigues 				 */
753d9fa6ce7SCraig Rodrigues 				continue;
7544b4f9170SDag-Erling Smørgrav 			} else if (strcmp(p, "late") == 0) {
7554b4f9170SDag-Erling Smørgrav 				/*
7564b4f9170SDag-Erling Smørgrav 				 * "late" is used to prevent certain file
7574b4f9170SDag-Erling Smørgrav 				 * systems from being mounted before late
7584b4f9170SDag-Erling Smørgrav 				 * in the boot cycle; for instance,
7594b4f9170SDag-Erling Smørgrav 				 * loopback NFS mounts can't be mounted
7604b4f9170SDag-Erling Smørgrav 				 * before mountd starts.
7614b4f9170SDag-Erling Smørgrav 				 */
7624b4f9170SDag-Erling Smørgrav 				continue;
763c7383075SXin LI 			} else if (strcmp(p, "failok") == 0) {
764c7383075SXin LI 				/*
765c7383075SXin LI 				 * "failok" is used to prevent certain file
766c7383075SXin LI 				 * systems from being causing the system to
767c7383075SXin LI 				 * drop into single user mode in the boot
768c7383075SXin LI 				 * cycle, and is not a real mount option.
769c7383075SXin LI 				 */
770c7383075SXin LI 				continue;
771c7835769SCraig Rodrigues 			} else if (strncmp(p, "mountprog", 9) == 0) {
772c7835769SCraig Rodrigues 				/*
773c7835769SCraig Rodrigues 				 * "mountprog" is used to force the use of
774c7835769SCraig Rodrigues 				 * userland mount programs.
775c7835769SCraig Rodrigues 				 */
776c7835769SCraig Rodrigues 				val = strchr(p, '=');
777c7835769SCraig Rodrigues                         	if (val != NULL) {
778c7835769SCraig Rodrigues                                 	++val;
779c7835769SCraig Rodrigues 					if (*val != '\0')
780c7835769SCraig Rodrigues 						mountprog = strdup(val);
781c7835769SCraig Rodrigues 				}
782c7835769SCraig Rodrigues 
783c7835769SCraig Rodrigues 				if (mountprog == NULL) {
784c7835769SCraig Rodrigues 					errx(1, "Need value for -o mountprog");
785c7835769SCraig Rodrigues 				}
786c7835769SCraig Rodrigues 				continue;
78735d6c7f5SCraig Rodrigues 			} else if (strcmp(p, "userquota") == 0) {
78835d6c7f5SCraig Rodrigues 				continue;
7896e74fb9dSMaxim Konovalov 			} else if (strncmp(p, userquotaeq,
7906e74fb9dSMaxim Konovalov 			    sizeof(userquotaeq) - 1) == 0) {
7916e74fb9dSMaxim Konovalov 				continue;
79235d6c7f5SCraig Rodrigues 			} else if (strcmp(p, "groupquota") == 0) {
79335d6c7f5SCraig Rodrigues 				continue;
7946e74fb9dSMaxim Konovalov 			} else if (strncmp(p, groupquotaeq,
7956e74fb9dSMaxim Konovalov 			    sizeof(groupquotaeq) - 1) == 0) {
7966e74fb9dSMaxim Konovalov 				continue;
797d9fa6ce7SCraig Rodrigues 			} else if (*p == '-') {
798a86de995SDavid E. O'Brien 				append_arg(a, p);
7998fae3551SRodney W. Grimes 				p = strchr(p, '=');
800adfdbe22STom Rhodes 				if (p != NULL) {
8018fae3551SRodney W. Grimes 					*p = '\0';
802a86de995SDavid E. O'Brien 					append_arg(a, p + 1);
8038fae3551SRodney W. Grimes 				}
804748e259bSCraig Rodrigues 			} else {
805a86de995SDavid E. O'Brien 				append_arg(a, strdup("-o"));
806a86de995SDavid E. O'Brien 				append_arg(a, p);
8078fae3551SRodney W. Grimes 			}
80818af6044SJoseph Koshy 		}
8098fae3551SRodney W. Grimes }
8108fae3551SRodney W. Grimes 
81118af6044SJoseph Koshy 
81218af6044SJoseph Koshy char *
8134796c6ccSJuli Mallett update_options(char *opts, char *fstab, int curflags)
81418af6044SJoseph Koshy {
81518af6044SJoseph Koshy 	char *o, *p;
81618af6044SJoseph Koshy 	char *cur;
81718af6044SJoseph Koshy 	char *expopt, *newopt, *tmpopt;
81818af6044SJoseph Koshy 
81918af6044SJoseph Koshy 	if (opts == NULL)
8204796c6ccSJuli Mallett 		return (strdup(""));
82118af6044SJoseph Koshy 
82218af6044SJoseph Koshy 	/* remove meta options from list */
82318af6044SJoseph Koshy 	remopt(fstab, MOUNT_META_OPTION_FSTAB);
82418af6044SJoseph Koshy 	remopt(fstab, MOUNT_META_OPTION_CURRENT);
82518af6044SJoseph Koshy 	cur = flags2opts(curflags);
82618af6044SJoseph Koshy 
82718af6044SJoseph Koshy 	/*
82818af6044SJoseph Koshy 	 * Expand all meta-options passed to us first.
82918af6044SJoseph Koshy 	 */
83018af6044SJoseph Koshy 	expopt = NULL;
83118af6044SJoseph Koshy 	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
83218af6044SJoseph Koshy 		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
83318af6044SJoseph Koshy 			expopt = catopt(expopt, fstab);
83418af6044SJoseph Koshy 		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
83518af6044SJoseph Koshy 			expopt = catopt(expopt, cur);
83618af6044SJoseph Koshy 		else
83718af6044SJoseph Koshy 			expopt = catopt(expopt, o);
83818af6044SJoseph Koshy 	}
83918af6044SJoseph Koshy 	free(cur);
84018af6044SJoseph Koshy 	free(opts);
84118af6044SJoseph Koshy 
84218af6044SJoseph Koshy 	/*
84318af6044SJoseph Koshy 	 * Remove previous contradictory arguments. Given option "foo" we
84418af6044SJoseph Koshy 	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
84518af6044SJoseph Koshy 	 * and "foo" - so we can deal with possible options like "notice".
84618af6044SJoseph Koshy 	 */
84718af6044SJoseph Koshy 	newopt = NULL;
84818af6044SJoseph Koshy 	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
84918af6044SJoseph Koshy 		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
85018af6044SJoseph Koshy 			errx(1, "malloc failed");
85118af6044SJoseph Koshy 
85218af6044SJoseph Koshy 		strcpy(tmpopt, "no");
85318af6044SJoseph Koshy 		strcat(tmpopt, o);
85418af6044SJoseph Koshy 		remopt(newopt, tmpopt);
85518af6044SJoseph Koshy 		free(tmpopt);
85618af6044SJoseph Koshy 
85718af6044SJoseph Koshy 		if (strncmp("no", o, 2) == 0)
85818af6044SJoseph Koshy 			remopt(newopt, o+2);
85918af6044SJoseph Koshy 
86018af6044SJoseph Koshy 		newopt = catopt(newopt, o);
86118af6044SJoseph Koshy 	}
86218af6044SJoseph Koshy 	free(expopt);
86318af6044SJoseph Koshy 
8644796c6ccSJuli Mallett 	return (newopt);
86518af6044SJoseph Koshy }
86618af6044SJoseph Koshy 
86718af6044SJoseph Koshy void
8684796c6ccSJuli Mallett remopt(char *string, const char *opt)
86918af6044SJoseph Koshy {
87018af6044SJoseph Koshy 	char *o, *p, *r;
87118af6044SJoseph Koshy 
87218af6044SJoseph Koshy 	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
87318af6044SJoseph Koshy 		return;
87418af6044SJoseph Koshy 
87518af6044SJoseph Koshy 	r = string;
87618af6044SJoseph Koshy 
87718af6044SJoseph Koshy 	for (p = string; (o = strsep(&p, ",")) != NULL;) {
87818af6044SJoseph Koshy 		if (strcmp(opt, o) != 0) {
87918af6044SJoseph Koshy 			if (*r == ',' && *o != '\0')
88018af6044SJoseph Koshy 				r++;
88118af6044SJoseph Koshy 			while ((*r++ = *o++) != '\0')
88218af6044SJoseph Koshy 			    ;
88318af6044SJoseph Koshy 			*--r = ',';
88418af6044SJoseph Koshy 		}
88518af6044SJoseph Koshy 	}
88618af6044SJoseph Koshy 	*r = '\0';
88718af6044SJoseph Koshy }
88818af6044SJoseph Koshy 
8898fae3551SRodney W. Grimes void
8904796c6ccSJuli Mallett usage(void)
8918fae3551SRodney W. Grimes {
8928fae3551SRodney W. Grimes 
893bcb1d846SPhilippe Charnier 	(void)fprintf(stderr, "%s\n%s\n%s\n",
8944b4f9170SDag-Erling Smørgrav "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
8958d646af5SRuslan Ermilov "       mount [-dfpruvw] special | node",
8968d646af5SRuslan Ermilov "       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
8978fae3551SRodney W. Grimes 	exit(1);
8988fae3551SRodney W. Grimes }
899a257a45eSJordan K. Hubbard 
900a257a45eSJordan K. Hubbard void
901031ea52fSMatteo Riondato putfsent(struct statfs *ent)
902a257a45eSJordan K. Hubbard {
903a257a45eSJordan K. Hubbard 	struct fstab *fst;
90472da5470SJaakko Heinonen 	char *opts, *rw;
905306d73d6SPoul-Henning Kamp 	int l;
906a257a45eSJordan K. Hubbard 
90772da5470SJaakko Heinonen 	opts = NULL;
90872da5470SJaakko Heinonen 	/* flags2opts() doesn't return the "rw" option. */
90972da5470SJaakko Heinonen 	if ((ent->f_flags & MNT_RDONLY) != 0)
91072da5470SJaakko Heinonen 		rw = NULL;
91172da5470SJaakko Heinonen 	else
91272da5470SJaakko Heinonen 		rw = catopt(NULL, "rw");
91372da5470SJaakko Heinonen 
91418af6044SJoseph Koshy 	opts = flags2opts(ent->f_flags);
91572da5470SJaakko Heinonen 	opts = catopt(rw, opts);
9163ae7ea68SGiorgos Keramidas 
917031ea52fSMatteo Riondato 	if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
918031ea52fSMatteo Riondato 	    strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
9196e24000aSAlan Somers 		strlcpy(ent->f_mntfromname,
9206e24000aSAlan Somers 		    (strnstr(ent->f_mntfromname, ":", 8) +1),
9216e24000aSAlan Somers 		    sizeof(ent->f_mntfromname));
922031ea52fSMatteo Riondato 	}
923031ea52fSMatteo Riondato 
924306d73d6SPoul-Henning Kamp 	l = strlen(ent->f_mntfromname);
925306d73d6SPoul-Henning Kamp 	printf("%s%s%s%s", ent->f_mntfromname,
926306d73d6SPoul-Henning Kamp 	    l < 8 ? "\t" : "",
927306d73d6SPoul-Henning Kamp 	    l < 16 ? "\t" : "",
928306d73d6SPoul-Henning Kamp 	    l < 24 ? "\t" : " ");
929306d73d6SPoul-Henning Kamp 	l = strlen(ent->f_mntonname);
930306d73d6SPoul-Henning Kamp 	printf("%s%s%s%s", ent->f_mntonname,
931306d73d6SPoul-Henning Kamp 	    l < 8 ? "\t" : "",
932306d73d6SPoul-Henning Kamp 	    l < 16 ? "\t" : "",
933306d73d6SPoul-Henning Kamp 	    l < 24 ? "\t" : " ");
934306d73d6SPoul-Henning Kamp 	printf("%s\t", ent->f_fstypename);
935306d73d6SPoul-Henning Kamp 	l = strlen(opts);
936306d73d6SPoul-Henning Kamp 	printf("%s%s", opts,
937306d73d6SPoul-Henning Kamp 	    l < 8 ? "\t" : " ");
93818af6044SJoseph Koshy 	free(opts);
939c06fe0a0SPeter Wemm 
940fba1c154SSteve Price 	if ((fst = getfsspec(ent->f_mntfromname)))
941a257a45eSJordan K. Hubbard 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
942fba1c154SSteve Price 	else if ((fst = getfsfile(ent->f_mntonname)))
943a257a45eSJordan K. Hubbard 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
944ab80d6faSBrian Feldman 	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
945ab80d6faSBrian Feldman 		if (strcmp(ent->f_mntonname, "/") == 0)
946a257a45eSJordan K. Hubbard 			printf("\t1 1\n");
947a257a45eSJordan K. Hubbard 		else
948ab80d6faSBrian Feldman 			printf("\t2 2\n");
949ab80d6faSBrian Feldman 	} else
950a257a45eSJordan K. Hubbard 		printf("\t0 0\n");
951a257a45eSJordan K. Hubbard }
95218af6044SJoseph Koshy 
95318af6044SJoseph Koshy 
95418af6044SJoseph Koshy char *
9554796c6ccSJuli Mallett flags2opts(int flags)
95618af6044SJoseph Koshy {
95718af6044SJoseph Koshy 	char *res;
95818af6044SJoseph Koshy 
95918af6044SJoseph Koshy 	res = NULL;
96018af6044SJoseph Koshy 
96188e2c335SCraig Rodrigues 	if (flags & MNT_RDONLY)		res = catopt(res, "ro");
96218af6044SJoseph Koshy 	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
96318af6044SJoseph Koshy 	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
96418af6044SJoseph Koshy 	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
96518af6044SJoseph Koshy 	if (flags & MNT_UNION)		res = catopt(res, "union");
96618af6044SJoseph Koshy 	if (flags & MNT_ASYNC)		res = catopt(res, "async");
96718af6044SJoseph Koshy 	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
96818af6044SJoseph Koshy 	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
96918af6044SJoseph Koshy 	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
97018af6044SJoseph Koshy 	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
97118af6044SJoseph Koshy 	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
972ba0fbe96SRobert Watson 	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
97303d94b50SRobert Watson 	if (flags & MNT_ACLS)		res = catopt(res, "acls");
9749340fc72SEdward Tomasz Napierala 	if (flags & MNT_NFS4ACLS)	res = catopt(res, "nfsv4acls");
97518af6044SJoseph Koshy 
9764796c6ccSJuli Mallett 	return (res);
97718af6044SJoseph Koshy }
978