xref: /freebsd/sbin/mount/mount.c (revision 4e25c86f4cdc87f4dff7849ebfa28ea0aaf89ac2)
1fba1c154SSteve Price /*-
28fae3551SRodney W. Grimes  * Copyright (c) 1980, 1989, 1993, 1994
38fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
48fae3551SRodney W. Grimes  *
58fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
68fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
78fae3551SRodney W. Grimes  * are met:
88fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
98fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
108fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
118fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
128fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
138fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
148fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
158fae3551SRodney W. Grimes  *    without specific prior written permission.
168fae3551SRodney W. Grimes  *
178fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
188fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
208fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
218fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
228fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
238fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
248fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
258fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278fae3551SRodney W. Grimes  * SUCH DAMAGE.
288fae3551SRodney W. Grimes  */
298fae3551SRodney W. Grimes 
308fae3551SRodney W. Grimes #ifndef lint
31fba1c154SSteve Price static const char copyright[] =
328fae3551SRodney W. Grimes "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
338fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
34fba1c154SSteve Price #if 0
35c06fe0a0SPeter Wemm static char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
36fba1c154SSteve Price #endif
378fae3551SRodney W. Grimes #endif /* not lint */
388fae3551SRodney W. Grimes 
3900356118SDavid E. O'Brien #include <sys/cdefs.h>
4000356118SDavid E. O'Brien __FBSDID("$FreeBSD$");
4100356118SDavid E. O'Brien 
428fae3551SRodney W. Grimes #include <sys/param.h>
438fae3551SRodney W. Grimes #include <sys/mount.h>
448a978495SDavid Greenman #include <sys/stat.h>
4574cf460bSBruce Evans #include <sys/wait.h>
468fae3551SRodney W. Grimes 
47003dbca6SMaxime Henrion #include <ctype.h>
488fae3551SRodney W. Grimes #include <err.h>
498fae3551SRodney W. Grimes #include <errno.h>
508fae3551SRodney W. Grimes #include <fstab.h>
51a3ba4c65SGordon Tetlow #include <paths.h>
52c06fe0a0SPeter Wemm #include <pwd.h>
538fae3551SRodney W. Grimes #include <signal.h>
5496c65ccbSIan Dowse #include <stdint.h>
558fae3551SRodney W. Grimes #include <stdio.h>
568fae3551SRodney W. Grimes #include <stdlib.h>
578fae3551SRodney W. Grimes #include <string.h>
588fae3551SRodney W. Grimes #include <unistd.h>
59e9988cedSPawel Jakub Dawidek #include <libutil.h>
608fae3551SRodney W. Grimes 
61fba1c154SSteve Price #include "extern.h"
6273dd3167SPoul-Henning Kamp #include "mntopts.h"
638fae3551SRodney W. Grimes #include "pathnames.h"
648fae3551SRodney W. Grimes 
6518af6044SJoseph Koshy /* `meta' options */
6618af6044SJoseph Koshy #define MOUNT_META_OPTION_FSTAB		"fstab"
6718af6044SJoseph Koshy #define MOUNT_META_OPTION_CURRENT	"current"
6818af6044SJoseph Koshy 
691efe3c6bSEd Schouten static int debug, fstab_style, verbose;
70a257a45eSJordan K. Hubbard 
71a86de995SDavid E. O'Brien struct cpa {
7200356118SDavid E. O'Brien 	char	**a;
730e969ed7SDavid E. O'Brien 	ssize_t	sz;
74a86de995SDavid E. O'Brien 	int	c;
75a86de995SDavid E. O'Brien };
76a86de995SDavid E. O'Brien 
7785429990SWarner Losh char   *catopt(char *, const char *);
7885429990SWarner Losh struct statfs *getmntpt(const char *);
7985429990SWarner Losh int	hasopt(const char *, const char *);
8085429990SWarner Losh int	ismounted(struct fstab *, struct statfs *, int);
8185429990SWarner Losh int	isremountable(const char *);
82a86de995SDavid E. O'Brien void	mangle(char *, struct cpa *);
8385429990SWarner Losh char   *update_options(char *, char *, int);
8485429990SWarner Losh int	mountfs(const char *, const char *, const char *,
8585429990SWarner Losh 			int, const char *, const char *);
8685429990SWarner Losh void	remopt(char *, const char *);
8785429990SWarner Losh void	prmount(struct statfs *);
88031ea52fSMatteo Riondato void	putfsent(struct statfs *);
8985429990SWarner Losh void	usage(void);
9085429990SWarner Losh char   *flags2opts(int);
918fae3551SRodney W. Grimes 
92bcb1d846SPhilippe Charnier /* Map from mount options to printable formats. */
938fae3551SRodney W. Grimes static struct opt {
949df3a164SBjoern A. Zeeb 	uint64_t o_opt;
958fae3551SRodney W. Grimes 	const char *o_name;
968fae3551SRodney W. Grimes } optnames[] = {
978fae3551SRodney W. Grimes 	{ MNT_ASYNC,		"asynchronous" },
988fae3551SRodney W. Grimes 	{ MNT_EXPORTED,		"NFS exported" },
998fae3551SRodney W. Grimes 	{ MNT_LOCAL,		"local" },
10055e50aceSDavid Greenman 	{ MNT_NOATIME,		"noatime" },
1018fae3551SRodney W. Grimes 	{ MNT_NOEXEC,		"noexec" },
1028fae3551SRodney W. Grimes 	{ MNT_NOSUID,		"nosuid" },
1035ddc8dedSWolfram Schneider 	{ MNT_NOSYMFOLLOW,	"nosymfollow" },
1048fae3551SRodney W. Grimes 	{ MNT_QUOTA,		"with quotas" },
1058fae3551SRodney W. Grimes 	{ MNT_RDONLY,		"read-only" },
1068fae3551SRodney W. Grimes 	{ MNT_SYNCHRONOUS,	"synchronous" },
1078fae3551SRodney W. Grimes 	{ MNT_UNION,		"union" },
10875b714acSKATO Takenori 	{ MNT_NOCLUSTERR,	"noclusterr" },
10975b714acSKATO Takenori 	{ MNT_NOCLUSTERW,	"noclusterw" },
11052bf64c7SJulian Elischer 	{ MNT_SUIDDIR,		"suiddir" },
111b1897c19SJulian Elischer 	{ MNT_SOFTDEP,		"soft-updates" },
112d716efa9SKirk McKusick 	{ MNT_SUJ,		"journaled soft-updates" },
113ba0fbe96SRobert Watson 	{ MNT_MULTILABEL,	"multilabel" },
11403d94b50SRobert Watson 	{ MNT_ACLS,		"acls" },
1159340fc72SEdward Tomasz Napierala 	{ MNT_NFS4ACLS,		"nfsv4acls" },
116ac88569cSPawel Jakub Dawidek 	{ MNT_GJOURNAL,		"gjournal" },
1173914ddf8SEdward Tomasz Napierala 	{ MNT_AUTOMOUNTED,	"automounted" },
11818af6044SJoseph Koshy 	{ 0, NULL }
1198fae3551SRodney W. Grimes };
1208fae3551SRodney W. Grimes 
121fba1c154SSteve Price /*
122fba1c154SSteve Price  * List of VFS types that can be remounted without becoming mounted on top
123fba1c154SSteve Price  * of each other.
124fba1c154SSteve Price  * XXX Is this list correct?
125fba1c154SSteve Price  */
126fba1c154SSteve Price static const char *
127fba1c154SSteve Price remountable_fs_names[] = {
1285a4420e3SAlexey Zelkin 	"ufs", "ffs", "ext2fs",
129fba1c154SSteve Price 	0
130fba1c154SSteve Price };
131fba1c154SSteve Price 
1326e74fb9dSMaxim Konovalov static const char userquotaeq[] = "userquota=";
1336e74fb9dSMaxim Konovalov static const char groupquotaeq[] = "groupquota=";
1346e74fb9dSMaxim Konovalov 
135c7835769SCraig Rodrigues static char *mountprog = NULL;
136c7835769SCraig Rodrigues 
1376f5f1a6bSCraig Rodrigues static int
1386f5f1a6bSCraig Rodrigues use_mountprog(const char *vfstype)
1396f5f1a6bSCraig Rodrigues {
1406f5f1a6bSCraig Rodrigues 	/* XXX: We need to get away from implementing external mount
1416f5f1a6bSCraig Rodrigues 	 *      programs for every filesystem, and move towards having
1426f5f1a6bSCraig Rodrigues 	 *	each filesystem properly implement the nmount() system call.
1436f5f1a6bSCraig Rodrigues 	 */
1446f5f1a6bSCraig Rodrigues 	unsigned int i;
1456f5f1a6bSCraig Rodrigues 	const char *fs[] = {
146a42ac676SAttilio Rao 	"cd9660", "mfs", "msdosfs", "nfs",
147*4e25c86fSEdward Tomasz Napierala 	"nullfs", "smbfs", "udf", "unionfs",
1486f5f1a6bSCraig Rodrigues 	NULL
1496f5f1a6bSCraig Rodrigues 	};
1506f5f1a6bSCraig Rodrigues 
151c7835769SCraig Rodrigues 	if (mountprog != NULL)
152c7835769SCraig Rodrigues 		return (1);
153c7835769SCraig Rodrigues 
1546f5f1a6bSCraig Rodrigues 	for (i = 0; fs[i] != NULL; ++i) {
1556f5f1a6bSCraig Rodrigues 		if (strcmp(vfstype, fs[i]) == 0)
1564796c6ccSJuli Mallett 			return (1);
1576f5f1a6bSCraig Rodrigues 	}
1586f5f1a6bSCraig Rodrigues 
1594796c6ccSJuli Mallett 	return (0);
1606f5f1a6bSCraig Rodrigues }
1616f5f1a6bSCraig Rodrigues 
1626f5f1a6bSCraig Rodrigues static int
1634796c6ccSJuli Mallett exec_mountprog(const char *name, const char *execname, char *const argv[])
1646f5f1a6bSCraig Rodrigues {
1656f5f1a6bSCraig Rodrigues 	pid_t pid;
1666f5f1a6bSCraig Rodrigues 	int status;
1676f5f1a6bSCraig Rodrigues 
1686f5f1a6bSCraig Rodrigues 	switch (pid = fork()) {
1696f5f1a6bSCraig Rodrigues 	case -1:				/* Error. */
1706f5f1a6bSCraig Rodrigues 		warn("fork");
1716f5f1a6bSCraig Rodrigues 		exit (1);
1726f5f1a6bSCraig Rodrigues 	case 0:					/* Child. */
1736f5f1a6bSCraig Rodrigues 		/* Go find an executable. */
1746f5f1a6bSCraig Rodrigues 		execvP(execname, _PATH_SYSPATH, argv);
1756f5f1a6bSCraig Rodrigues 		if (errno == ENOENT) {
176c7835769SCraig Rodrigues 			warn("exec %s not found", execname);
177c7835769SCraig Rodrigues 			if (execname[0] != '/') {
178c7835769SCraig Rodrigues 				warnx("in path: %s", _PATH_SYSPATH);
179c7835769SCraig Rodrigues 			}
1806f5f1a6bSCraig Rodrigues 		}
1816f5f1a6bSCraig Rodrigues 		exit(1);
1826f5f1a6bSCraig Rodrigues 	default:				/* Parent. */
1836f5f1a6bSCraig Rodrigues 		if (waitpid(pid, &status, 0) < 0) {
1846f5f1a6bSCraig Rodrigues 			warn("waitpid");
1856f5f1a6bSCraig Rodrigues 			return (1);
1866f5f1a6bSCraig Rodrigues 		}
1876f5f1a6bSCraig Rodrigues 
1886f5f1a6bSCraig Rodrigues 		if (WIFEXITED(status)) {
1896f5f1a6bSCraig Rodrigues 			if (WEXITSTATUS(status) != 0)
1906f5f1a6bSCraig Rodrigues 				return (WEXITSTATUS(status));
1916f5f1a6bSCraig Rodrigues 		} else if (WIFSIGNALED(status)) {
1926f5f1a6bSCraig Rodrigues 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
1936f5f1a6bSCraig Rodrigues 			return (1);
1946f5f1a6bSCraig Rodrigues 		}
1956f5f1a6bSCraig Rodrigues 		break;
1966f5f1a6bSCraig Rodrigues 	}
1976f5f1a6bSCraig Rodrigues 
1986f5f1a6bSCraig Rodrigues 	return (0);
1996f5f1a6bSCraig Rodrigues }
2006f5f1a6bSCraig Rodrigues 
20149a41c4fSRuslan Ermilov static int
20249a41c4fSRuslan Ermilov specified_ro(const char *arg)
2033cbf527eSRuslan Ermilov {
2043cbf527eSRuslan Ermilov 	char *optbuf, *opt;
2053cbf527eSRuslan Ermilov 	int ret = 0;
2063cbf527eSRuslan Ermilov 
2073cbf527eSRuslan Ermilov 	optbuf = strdup(arg);
2083cbf527eSRuslan Ermilov 	if (optbuf == NULL)
2093cbf527eSRuslan Ermilov 		 err(1, NULL);
2103cbf527eSRuslan Ermilov 
2113cbf527eSRuslan Ermilov 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
2123cbf527eSRuslan Ermilov 		if (strcmp(opt, "ro") == 0) {
2133cbf527eSRuslan Ermilov 			ret = 1;
2143cbf527eSRuslan Ermilov 			break;
2153cbf527eSRuslan Ermilov 		}
2163cbf527eSRuslan Ermilov 	}
2173cbf527eSRuslan Ermilov 	free(optbuf);
2183cbf527eSRuslan Ermilov 	return (ret);
2193cbf527eSRuslan Ermilov }
2203cbf527eSRuslan Ermilov 
221e9988cedSPawel Jakub Dawidek static void
222e9988cedSPawel Jakub Dawidek restart_mountd(void)
223e9988cedSPawel Jakub Dawidek {
224e9988cedSPawel Jakub Dawidek 	struct pidfh *pfh;
225e9988cedSPawel Jakub Dawidek 	pid_t mountdpid;
226e9988cedSPawel Jakub Dawidek 
227e9988cedSPawel Jakub Dawidek 	pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
228e9988cedSPawel Jakub Dawidek 	if (pfh != NULL) {
229e9988cedSPawel Jakub Dawidek 		/* Mountd is not running. */
230e9988cedSPawel Jakub Dawidek 		pidfile_remove(pfh);
231e9988cedSPawel Jakub Dawidek 		return;
232e9988cedSPawel Jakub Dawidek 	}
233e9988cedSPawel Jakub Dawidek 	if (errno != EEXIST) {
234e9988cedSPawel Jakub Dawidek 		/* Cannot open pidfile for some reason. */
235e9988cedSPawel Jakub Dawidek 		return;
236e9988cedSPawel Jakub Dawidek 	}
237e9988cedSPawel Jakub Dawidek 	/* We have mountd(8) PID in mountdpid varible, let's signal it. */
238e9988cedSPawel Jakub Dawidek 	if (kill(mountdpid, SIGHUP) == -1)
239e9988cedSPawel Jakub Dawidek 		err(1, "signal mountd");
240e9988cedSPawel Jakub Dawidek }
241e9988cedSPawel Jakub Dawidek 
2428fae3551SRodney W. Grimes int
243e24dc56aSCraig Rodrigues main(int argc, char *argv[])
2448fae3551SRodney W. Grimes {
245c06fe0a0SPeter Wemm 	const char *mntfromname, **vfslist, *vfstype;
2468fae3551SRodney W. Grimes 	struct fstab *fs;
2478fae3551SRodney W. Grimes 	struct statfs *mntbuf;
248c7383075SXin LI 	int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
249b48b774fSChris Rees 	int onlylate;
250003dbca6SMaxime Henrion 	char *cp, *ep, *options;
2518fae3551SRodney W. Grimes 
252b48b774fSChris Rees 	all = init_flags = late = onlylate = 0;
2533cbf527eSRuslan Ermilov 	ro = 0;
2543cbf527eSRuslan Ermilov 	options = NULL;
2558fae3551SRodney W. Grimes 	vfslist = NULL;
2568fae3551SRodney W. Grimes 	vfstype = "ufs";
257fc98db27SRobert Millan 	while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
2588fae3551SRodney W. Grimes 		switch (ch) {
2598fae3551SRodney W. Grimes 		case 'a':
2608fae3551SRodney W. Grimes 			all = 1;
2618fae3551SRodney W. Grimes 			break;
2628fae3551SRodney W. Grimes 		case 'd':
2638fae3551SRodney W. Grimes 			debug = 1;
2648fae3551SRodney W. Grimes 			break;
265ef258dd9SMatthew N. Dodd 		case 'F':
266ef258dd9SMatthew N. Dodd 			setfstab(optarg);
267ef258dd9SMatthew N. Dodd 			break;
2688fae3551SRodney W. Grimes 		case 'f':
2698fae3551SRodney W. Grimes 			init_flags |= MNT_FORCE;
2708fae3551SRodney W. Grimes 			break;
271b48b774fSChris Rees 		case 'L':
272b48b774fSChris Rees 			onlylate = 1;
273b48b774fSChris Rees 			late = 1;
274b48b774fSChris Rees 			break;
2754b4f9170SDag-Erling Smørgrav 		case 'l':
2764b4f9170SDag-Erling Smørgrav 			late = 1;
2774b4f9170SDag-Erling Smørgrav 			break;
278fc98db27SRobert Millan 		case 'n':
279fc98db27SRobert Millan 			/* For compatibility with the Linux version of mount. */
280fc98db27SRobert Millan 			break;
2818fae3551SRodney W. Grimes 		case 'o':
2823cbf527eSRuslan Ermilov 			if (*optarg) {
2838fae3551SRodney W. Grimes 				options = catopt(options, optarg);
2843cbf527eSRuslan Ermilov 				if (specified_ro(optarg))
2853cbf527eSRuslan Ermilov 					ro = 1;
2863cbf527eSRuslan Ermilov 			}
2878fae3551SRodney W. Grimes 			break;
28874cf460bSBruce Evans 		case 'p':
28974cf460bSBruce Evans 			fstab_style = 1;
29074cf460bSBruce Evans 			verbose = 1;
29174cf460bSBruce Evans 			break;
2928fae3551SRodney W. Grimes 		case 'r':
293ad044771SDima Dorfman 			options = catopt(options, "ro");
2943cbf527eSRuslan Ermilov 			ro = 1;
2958fae3551SRodney W. Grimes 			break;
2968fae3551SRodney W. Grimes 		case 't':
2978fae3551SRodney W. Grimes 			if (vfslist != NULL)
298bcb1d846SPhilippe Charnier 				errx(1, "only one -t option may be specified");
2998fae3551SRodney W. Grimes 			vfslist = makevfslist(optarg);
3008fae3551SRodney W. Grimes 			vfstype = optarg;
3018fae3551SRodney W. Grimes 			break;
3028fae3551SRodney W. Grimes 		case 'u':
3038fae3551SRodney W. Grimes 			init_flags |= MNT_UPDATE;
3048fae3551SRodney W. Grimes 			break;
3058fae3551SRodney W. Grimes 		case 'v':
3068fae3551SRodney W. Grimes 			verbose = 1;
3078fae3551SRodney W. Grimes 			break;
3088fae3551SRodney W. Grimes 		case 'w':
309ad044771SDima Dorfman 			options = catopt(options, "noro");
3108fae3551SRodney W. Grimes 			break;
3118fae3551SRodney W. Grimes 		case '?':
3128fae3551SRodney W. Grimes 		default:
3138fae3551SRodney W. Grimes 			usage();
3148fae3551SRodney W. Grimes 			/* NOTREACHED */
3158fae3551SRodney W. Grimes 		}
3168fae3551SRodney W. Grimes 	argc -= optind;
3178fae3551SRodney W. Grimes 	argv += optind;
3188fae3551SRodney W. Grimes 
3198fae3551SRodney W. Grimes #define	BADTYPE(type)							\
3208fae3551SRodney W. Grimes 	(strcmp(type, FSTAB_RO) &&					\
3218fae3551SRodney W. Grimes 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
3228fae3551SRodney W. Grimes 
3233cbf527eSRuslan Ermilov 	if ((init_flags & MNT_UPDATE) && (ro == 0))
3243cbf527eSRuslan Ermilov 		options = catopt(options, "noro");
3253cbf527eSRuslan Ermilov 
3268fae3551SRodney W. Grimes 	rval = 0;
3278fae3551SRodney W. Grimes 	switch (argc) {
3288fae3551SRodney W. Grimes 	case 0:
329fddf7baeSKirk McKusick 		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
330fba1c154SSteve Price 			err(1, "getmntinfo");
331fba1c154SSteve Price 		if (all) {
3328fae3551SRodney W. Grimes 			while ((fs = getfsent()) != NULL) {
3338fae3551SRodney W. Grimes 				if (BADTYPE(fs->fs_type))
3348fae3551SRodney W. Grimes 					continue;
335c06fe0a0SPeter Wemm 				if (checkvfsname(fs->fs_vfstype, vfslist))
3368fae3551SRodney W. Grimes 					continue;
337c06fe0a0SPeter Wemm 				if (hasopt(fs->fs_mntops, "noauto"))
33889beb278SDavid Greenman 					continue;
339b48b774fSChris Rees 				if (!hasopt(fs->fs_mntops, "late") && onlylate)
340b48b774fSChris Rees 					continue;
3414b4f9170SDag-Erling Smørgrav 				if (hasopt(fs->fs_mntops, "late") && !late)
3424b4f9170SDag-Erling Smørgrav 					continue;
343c7383075SXin LI 				if (hasopt(fs->fs_mntops, "failok"))
344c7383075SXin LI 					failok = 1;
345c7383075SXin LI 				else
346c7383075SXin LI 					failok = 0;
34798201b0cSBruce Evans 				if (!(init_flags & MNT_UPDATE) &&
34898201b0cSBruce Evans 				    ismounted(fs, mntbuf, mntsize))
349fba1c154SSteve Price 					continue;
35013cbdf24SGuido van Rooij 				options = update_options(options, fs->fs_mntops,
35113cbdf24SGuido van Rooij 				    mntbuf->f_flags);
3528fae3551SRodney W. Grimes 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
3538fae3551SRodney W. Grimes 				    fs->fs_file, init_flags, options,
354c7383075SXin LI 				    fs->fs_mntops) && !failok)
3558fae3551SRodney W. Grimes 					rval = 1;
3568fae3551SRodney W. Grimes 			}
357fba1c154SSteve Price 		} else if (fstab_style) {
358a257a45eSJordan K. Hubbard 			for (i = 0; i < mntsize; i++) {
359c06fe0a0SPeter Wemm 				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
360a257a45eSJordan K. Hubbard 					continue;
361a257a45eSJordan K. Hubbard 				putfsent(&mntbuf[i]);
362a257a45eSJordan K. Hubbard 			}
36374cf460bSBruce Evans 		} else {
3648fae3551SRodney W. Grimes 			for (i = 0; i < mntsize; i++) {
36574cf460bSBruce Evans 				if (checkvfsname(mntbuf[i].f_fstypename,
36674cf460bSBruce Evans 				    vfslist))
3678fae3551SRodney W. Grimes 					continue;
3688abb2a6eSPawel Jakub Dawidek 				if (!verbose &&
3698abb2a6eSPawel Jakub Dawidek 				    (mntbuf[i].f_flags & MNT_IGNORE) != 0)
3708abb2a6eSPawel Jakub Dawidek 					continue;
371c06fe0a0SPeter Wemm 				prmount(&mntbuf[i]);
3728fae3551SRodney W. Grimes 			}
3738fae3551SRodney W. Grimes 		}
3748fae3551SRodney W. Grimes 		exit(rval);
3758fae3551SRodney W. Grimes 	case 1:
3768fae3551SRodney W. Grimes 		if (vfslist != NULL)
3778fae3551SRodney W. Grimes 			usage();
3788fae3551SRodney W. Grimes 
37901a9bce5SEric Anholt 		rmslashes(*argv, *argv);
3808fae3551SRodney W. Grimes 		if (init_flags & MNT_UPDATE) {
381cf96af72SBrian Feldman 			mntfromname = NULL;
382cf96af72SBrian Feldman 			have_fstab = 0;
3838fae3551SRodney W. Grimes 			if ((mntbuf = getmntpt(*argv)) == NULL)
384cf96af72SBrian Feldman 				errx(1, "not currently mounted %s", *argv);
385cf96af72SBrian Feldman 			/*
386cf96af72SBrian Feldman 			 * Only get the mntflags from fstab if both mntpoint
387cf96af72SBrian Feldman 			 * and mntspec are identical. Also handle the special
388cf96af72SBrian Feldman 			 * case where just '/' is mounted and 'spec' is not
389cf96af72SBrian Feldman 			 * identical with the one from fstab ('/dev' is missing
390cf96af72SBrian Feldman 			 * in the spec-string at boot-time).
391cf96af72SBrian Feldman 			 */
39218af6044SJoseph Koshy 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
393cf96af72SBrian Feldman 				if (strcmp(fs->fs_spec,
394cf96af72SBrian Feldman 				    mntbuf->f_mntfromname) == 0 &&
395cf96af72SBrian Feldman 				    strcmp(fs->fs_file,
396cf96af72SBrian Feldman 				    mntbuf->f_mntonname) == 0) {
397cf96af72SBrian Feldman 					have_fstab = 1;
398cf96af72SBrian Feldman 					mntfromname = mntbuf->f_mntfromname;
399cf96af72SBrian Feldman 				} else if (argv[0][0] == '/' &&
400cf96af72SBrian Feldman 				    argv[0][1] == '\0') {
401cf96af72SBrian Feldman 					fs = getfsfile("/");
402cf96af72SBrian Feldman 					have_fstab = 1;
403c06fe0a0SPeter Wemm 					mntfromname = fs->fs_spec;
404cf96af72SBrian Feldman 				}
405cf96af72SBrian Feldman 			}
406cf96af72SBrian Feldman 			if (have_fstab) {
40718af6044SJoseph Koshy 				options = update_options(options, fs->fs_mntops,
40818af6044SJoseph Koshy 				    mntbuf->f_flags);
40918af6044SJoseph Koshy 			} else {
410c06fe0a0SPeter Wemm 				mntfromname = mntbuf->f_mntfromname;
41118af6044SJoseph Koshy 				options = update_options(options, NULL,
41218af6044SJoseph Koshy 				    mntbuf->f_flags);
41318af6044SJoseph Koshy 			}
414c06fe0a0SPeter Wemm 			rval = mountfs(mntbuf->f_fstypename, mntfromname,
415c06fe0a0SPeter Wemm 			    mntbuf->f_mntonname, init_flags, options, 0);
416c06fe0a0SPeter Wemm 			break;
417c06fe0a0SPeter Wemm 		}
4188fae3551SRodney W. Grimes 		if ((fs = getfsfile(*argv)) == NULL &&
4198fae3551SRodney W. Grimes 		    (fs = getfsspec(*argv)) == NULL)
420bcb1d846SPhilippe Charnier 			errx(1, "%s: unknown special file or file system",
4218fae3551SRodney W. Grimes 			    *argv);
4228fae3551SRodney W. Grimes 		if (BADTYPE(fs->fs_type))
423bcb1d846SPhilippe Charnier 			errx(1, "%s has unknown file system type",
4248fae3551SRodney W. Grimes 			    *argv);
425c06fe0a0SPeter Wemm 		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
426c06fe0a0SPeter Wemm 		    init_flags, options, fs->fs_mntops);
4278fae3551SRodney W. Grimes 		break;
4288fae3551SRodney W. Grimes 	case 2:
4298fae3551SRodney W. Grimes 		/*
430cf96af72SBrian Feldman 		 * If -t flag has not been specified, the path cannot be
431003dbca6SMaxime Henrion 		 * found, spec contains either a ':' or a '@', then assume
432cf96af72SBrian Feldman 		 * that an NFS file system is being specified ala Sun.
433003dbca6SMaxime Henrion 		 * Check if the hostname contains only allowed characters
434003dbca6SMaxime Henrion 		 * to reduce false positives.  IPv6 addresses containing
435003dbca6SMaxime Henrion 		 * ':' will be correctly parsed only if the separator is '@'.
436003dbca6SMaxime Henrion 		 * The definition of a valid hostname is taken from RFC 1034.
4378fae3551SRodney W. Grimes 		 */
43805779418SIan Dowse 		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
43905779418SIan Dowse 		    (ep = strchr(argv[0], ':')) != NULL)) {
440da85c82bSMaxime Henrion 			if (*ep == '@') {
441da85c82bSMaxime Henrion 				cp = ep + 1;
442da85c82bSMaxime Henrion 				ep = cp + strlen(cp);
443da85c82bSMaxime Henrion 			} else
444003dbca6SMaxime Henrion 				cp = argv[0];
445003dbca6SMaxime Henrion 			while (cp != ep) {
446003dbca6SMaxime Henrion 				if (!isdigit(*cp) && !isalpha(*cp) &&
447003dbca6SMaxime Henrion 				    *cp != '.' && *cp != '-' && *cp != ':')
448003dbca6SMaxime Henrion 					break;
449003dbca6SMaxime Henrion 				cp++;
450003dbca6SMaxime Henrion 			}
451003dbca6SMaxime Henrion 			if (cp == ep)
4528fae3551SRodney W. Grimes 				vfstype = "nfs";
453003dbca6SMaxime Henrion 		}
4548fae3551SRodney W. Grimes 		rval = mountfs(vfstype,
4558fae3551SRodney W. Grimes 		    argv[0], argv[1], init_flags, options, NULL);
4568fae3551SRodney W. Grimes 		break;
4578fae3551SRodney W. Grimes 	default:
4588fae3551SRodney W. Grimes 		usage();
4598fae3551SRodney W. Grimes 		/* NOTREACHED */
4608fae3551SRodney W. Grimes 	}
4618fae3551SRodney W. Grimes 
4628fae3551SRodney W. Grimes 	/*
4638fae3551SRodney W. Grimes 	 * If the mount was successfully, and done by root, tell mountd the
464e9988cedSPawel Jakub Dawidek 	 * good news.
4658fae3551SRodney W. Grimes 	 */
466e9988cedSPawel Jakub Dawidek 	if (rval == 0 && getuid() == 0)
467e9988cedSPawel Jakub Dawidek 		restart_mountd();
4688fae3551SRodney W. Grimes 
4698fae3551SRodney W. Grimes 	exit(rval);
4708fae3551SRodney W. Grimes }
4718fae3551SRodney W. Grimes 
4728fae3551SRodney W. Grimes int
473e24dc56aSCraig Rodrigues ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
474fba1c154SSteve Price {
47590742659SPawel Jakub Dawidek 	char realfsfile[PATH_MAX];
476fba1c154SSteve Price 	int i;
477fba1c154SSteve Price 
478fba1c154SSteve Price 	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
479fba1c154SSteve Price 		/* the root file system can always be remounted */
480fba1c154SSteve Price 		return (0);
481fba1c154SSteve Price 
48290742659SPawel Jakub Dawidek 	/* The user may have specified a symlink in fstab, resolve the path */
48390742659SPawel Jakub Dawidek 	if (realpath(fs->fs_file, realfsfile) == NULL) {
48490742659SPawel Jakub Dawidek 		/* Cannot resolve the path, use original one */
48590742659SPawel Jakub Dawidek 		strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
48690742659SPawel Jakub Dawidek 	}
48790742659SPawel Jakub Dawidek 
488fba1c154SSteve Price 	for (i = mntsize - 1; i >= 0; --i)
48990742659SPawel Jakub Dawidek 		if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
490fba1c154SSteve Price 		    (!isremountable(fs->fs_vfstype) ||
491fba1c154SSteve Price 		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
492fba1c154SSteve Price 			return (1);
493fba1c154SSteve Price 	return (0);
494fba1c154SSteve Price }
495fba1c154SSteve Price 
496fba1c154SSteve Price int
497e24dc56aSCraig Rodrigues isremountable(const char *vfsname)
498fba1c154SSteve Price {
499fba1c154SSteve Price 	const char **cp;
500fba1c154SSteve Price 
501fba1c154SSteve Price 	for (cp = remountable_fs_names; *cp; cp++)
502fba1c154SSteve Price 		if (strcmp(*cp, vfsname) == 0)
503fba1c154SSteve Price 			return (1);
504fba1c154SSteve Price 	return (0);
505fba1c154SSteve Price }
506fba1c154SSteve Price 
507fba1c154SSteve Price int
508e24dc56aSCraig Rodrigues hasopt(const char *mntopts, const char *option)
509c06fe0a0SPeter Wemm {
510c06fe0a0SPeter Wemm 	int negative, found;
511c06fe0a0SPeter Wemm 	char *opt, *optbuf;
512c06fe0a0SPeter Wemm 
513c06fe0a0SPeter Wemm 	if (option[0] == 'n' && option[1] == 'o') {
514c06fe0a0SPeter Wemm 		negative = 1;
515c06fe0a0SPeter Wemm 		option += 2;
516c06fe0a0SPeter Wemm 	} else
517c06fe0a0SPeter Wemm 		negative = 0;
518c06fe0a0SPeter Wemm 	optbuf = strdup(mntopts);
519c06fe0a0SPeter Wemm 	found = 0;
520c06fe0a0SPeter Wemm 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
521c06fe0a0SPeter Wemm 		if (opt[0] == 'n' && opt[1] == 'o') {
522c06fe0a0SPeter Wemm 			if (!strcasecmp(opt + 2, option))
523c06fe0a0SPeter Wemm 				found = negative;
524c06fe0a0SPeter Wemm 		} else if (!strcasecmp(opt, option))
525c06fe0a0SPeter Wemm 			found = !negative;
526c06fe0a0SPeter Wemm 	}
527c06fe0a0SPeter Wemm 	free(optbuf);
528c06fe0a0SPeter Wemm 	return (found);
529c06fe0a0SPeter Wemm }
530c06fe0a0SPeter Wemm 
531a86de995SDavid E. O'Brien static void
532a86de995SDavid E. O'Brien append_arg(struct cpa *sa, char *arg)
533a86de995SDavid E. O'Brien {
5340e969ed7SDavid E. O'Brien 	if (sa->c + 1 == sa->sz) {
5350e969ed7SDavid E. O'Brien 		sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
5360e969ed7SDavid E. O'Brien 		sa->a = realloc(sa->a, sizeof(sa->a) * sa->sz);
53700356118SDavid E. O'Brien 		if (sa->a == NULL)
53800356118SDavid E. O'Brien 			errx(1, "realloc failed");
53900356118SDavid E. O'Brien 	}
540a86de995SDavid E. O'Brien 	sa->a[++sa->c] = arg;
541a86de995SDavid E. O'Brien }
542a86de995SDavid E. O'Brien 
543c06fe0a0SPeter Wemm int
544e24dc56aSCraig Rodrigues mountfs(const char *vfstype, const char *spec, const char *name, int flags,
545e24dc56aSCraig Rodrigues 	const char *options, const char *mntopts)
5468fae3551SRodney W. Grimes {
5478fae3551SRodney W. Grimes 	struct statfs sf;
54876c46216SDavid E. O'Brien 	int i, ret;
54968dd1ff4SWarner Losh 	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
5500e969ed7SDavid E. O'Brien 	static struct cpa mnt_argv;
5518fae3551SRodney W. Grimes 
55273dd3167SPoul-Henning Kamp 	/* resolve the mountpoint with realpath(3) */
553d3250014SJaakko Heinonen 	if (checkpath(name, mntpath) != 0) {
554d3250014SJaakko Heinonen 		warn("%s", mntpath);
555d3250014SJaakko Heinonen 		return (1);
556d3250014SJaakko Heinonen 	}
5578fae3551SRodney W. Grimes 	name = mntpath;
5588fae3551SRodney W. Grimes 
559c06fe0a0SPeter Wemm 	if (mntopts == NULL)
560c06fe0a0SPeter Wemm 		mntopts = "";
5618fae3551SRodney W. Grimes 	optbuf = catopt(strdup(mntopts), options);
5628fae3551SRodney W. Grimes 
5638fae3551SRodney W. Grimes 	if (strcmp(name, "/") == 0)
5648fae3551SRodney W. Grimes 		flags |= MNT_UPDATE;
5658fae3551SRodney W. Grimes 	if (flags & MNT_FORCE)
5668fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "force");
5678fae3551SRodney W. Grimes 	if (flags & MNT_RDONLY)
5688fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "ro");
5698fae3551SRodney W. Grimes 	/*
5708fae3551SRodney W. Grimes 	 * XXX
5718fae3551SRodney W. Grimes 	 * The mount_mfs (newfs) command uses -o to select the
572bcb1d846SPhilippe Charnier 	 * optimization mode.  We don't pass the default "-o rw"
5738fae3551SRodney W. Grimes 	 * for that reason.
5748fae3551SRodney W. Grimes 	 */
5758fae3551SRodney W. Grimes 	if (flags & MNT_UPDATE)
5768fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "update");
5778fae3551SRodney W. Grimes 
5784ccd7546SRuslan Ermilov 	/* Compatibility glue. */
5795b548564SCraig Rodrigues 	if (strcmp(vfstype, "msdos") == 0) {
5805b548564SCraig Rodrigues 		warnx(
5815b548564SCraig Rodrigues 		    "Using \"-t msdosfs\", since \"-t msdos\" is deprecated.");
5824ccd7546SRuslan Ermilov 		vfstype = "msdosfs";
5835b548564SCraig Rodrigues 	}
5844ccd7546SRuslan Ermilov 
5856e34b479SColin Percival 	/* Construct the name of the appropriate mount command */
5866e34b479SColin Percival 	(void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
5876e34b479SColin Percival 
588a86de995SDavid E. O'Brien 	mnt_argv.c = -1;
589a86de995SDavid E. O'Brien 	append_arg(&mnt_argv, execname);
590a86de995SDavid E. O'Brien 	mangle(optbuf, &mnt_argv);
591c7835769SCraig Rodrigues 	if (mountprog != NULL)
592c7835769SCraig Rodrigues 		strcpy(execname, mountprog);
593c7835769SCraig Rodrigues 
594a86de995SDavid E. O'Brien 	append_arg(&mnt_argv, strdup(spec));
595a86de995SDavid E. O'Brien 	append_arg(&mnt_argv, strdup(name));
596a86de995SDavid E. O'Brien 	append_arg(&mnt_argv, NULL);
597fce5f960SDavid E. O'Brien 
5988fae3551SRodney W. Grimes 	if (debug) {
599c195c7f6SCraig Rodrigues 		if (use_mountprog(vfstype))
600c7835769SCraig Rodrigues 			printf("exec: %s", execname);
601c195c7f6SCraig Rodrigues 		else
602c195c7f6SCraig Rodrigues 			printf("mount -t %s", vfstype);
603a86de995SDavid E. O'Brien 		for (i = 1; i < mnt_argv.c; i++)
604a86de995SDavid E. O'Brien 			(void)printf(" %s", mnt_argv.a[i]);
6058fae3551SRodney W. Grimes 		(void)printf("\n");
606d2e17ce9SJohn Baldwin 		free(optbuf);
607d2e17ce9SJohn Baldwin 		free(mountprog);
608d2e17ce9SJohn Baldwin 		mountprog = NULL;
6098fae3551SRodney W. Grimes 		return (0);
6108fae3551SRodney W. Grimes 	}
6118fae3551SRodney W. Grimes 
612b1e6b712SCraig Rodrigues 	if (use_mountprog(vfstype)) {
613a86de995SDavid E. O'Brien 		ret = exec_mountprog(name, execname, mnt_argv.a);
6146f5f1a6bSCraig Rodrigues 	} else {
615a86de995SDavid E. O'Brien 		ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
6166f5f1a6bSCraig Rodrigues 	}
6176f5f1a6bSCraig Rodrigues 
6188fae3551SRodney W. Grimes 	free(optbuf);
619d2e17ce9SJohn Baldwin 	free(mountprog);
620d2e17ce9SJohn Baldwin 	mountprog = NULL;
6218fae3551SRodney W. Grimes 
6228fae3551SRodney W. Grimes 	if (verbose) {
6238fae3551SRodney W. Grimes 		if (statfs(name, &sf) < 0) {
624c06fe0a0SPeter Wemm 			warn("statfs %s", name);
6258fae3551SRodney W. Grimes 			return (1);
6268fae3551SRodney W. Grimes 		}
627a257a45eSJordan K. Hubbard 		if (fstab_style)
628a257a45eSJordan K. Hubbard 			putfsent(&sf);
629a257a45eSJordan K. Hubbard 		else
630c06fe0a0SPeter Wemm 			prmount(&sf);
6318fae3551SRodney W. Grimes 	}
6328fae3551SRodney W. Grimes 
633bbe9d7daSMatteo Riondato 	return (ret);
6348fae3551SRodney W. Grimes }
6358fae3551SRodney W. Grimes 
6368fae3551SRodney W. Grimes void
637e24dc56aSCraig Rodrigues prmount(struct statfs *sfp)
6388fae3551SRodney W. Grimes {
6399df3a164SBjoern A. Zeeb 	uint64_t flags;
640aedf10acSCraig Rodrigues 	unsigned int i;
6418fae3551SRodney W. Grimes 	struct opt *o;
642c06fe0a0SPeter Wemm 	struct passwd *pw;
6438fae3551SRodney W. Grimes 
644af2ea5aaSNick Hibma 	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
645af2ea5aaSNick Hibma 	    sfp->f_fstypename);
6468fae3551SRodney W. Grimes 
647c06fe0a0SPeter Wemm 	flags = sfp->f_flags & MNT_VISFLAGMASK;
6489df3a164SBjoern A. Zeeb 	for (o = optnames; flags != 0 && o->o_opt != 0; o++)
6498fae3551SRodney W. Grimes 		if (flags & o->o_opt) {
650af2ea5aaSNick Hibma 			(void)printf(", %s", o->o_name);
6518fae3551SRodney W. Grimes 			flags &= ~o->o_opt;
6528fae3551SRodney W. Grimes 		}
653dc9c6194SPawel Jakub Dawidek 	/*
654dc9c6194SPawel Jakub Dawidek 	 * Inform when file system is mounted by an unprivileged user
655dc9c6194SPawel Jakub Dawidek 	 * or privileged non-root user.
656dc9c6194SPawel Jakub Dawidek 	 */
657ddb842ccSJacques Vidrine 	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
658af2ea5aaSNick Hibma 		(void)printf(", mounted by ");
659c06fe0a0SPeter Wemm 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
660c06fe0a0SPeter Wemm 			(void)printf("%s", pw->pw_name);
661c06fe0a0SPeter Wemm 		else
662c06fe0a0SPeter Wemm 			(void)printf("%d", sfp->f_owner);
663c06fe0a0SPeter Wemm 	}
664c62ffab6SSheldon Hearn 	if (verbose) {
665677b9b3fSBruce Evans 		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
66696c65ccbSIan Dowse 			(void)printf(", writes: sync %ju async %ju",
66796c65ccbSIan Dowse 			    (uintmax_t)sfp->f_syncwrites,
66896c65ccbSIan Dowse 			    (uintmax_t)sfp->f_asyncwrites);
669461bb71eSKirk McKusick 		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
67096c65ccbSIan Dowse 			(void)printf(", reads: sync %ju async %ju",
67196c65ccbSIan Dowse 			    (uintmax_t)sfp->f_syncreads,
67296c65ccbSIan Dowse 			    (uintmax_t)sfp->f_asyncreads);
67305779418SIan Dowse 		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
67438f102c2SIan Dowse 			printf(", fsid ");
67538f102c2SIan Dowse 			for (i = 0; i < sizeof(sfp->f_fsid); i++)
67638f102c2SIan Dowse 				printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
677c62ffab6SSheldon Hearn 		}
67805779418SIan Dowse 	}
679af2ea5aaSNick Hibma 	(void)printf(")\n");
6808fae3551SRodney W. Grimes }
6818fae3551SRodney W. Grimes 
6828fae3551SRodney W. Grimes struct statfs *
683e24dc56aSCraig Rodrigues getmntpt(const char *name)
6848fae3551SRodney W. Grimes {
6858fae3551SRodney W. Grimes 	struct statfs *mntbuf;
6868fae3551SRodney W. Grimes 	int i, mntsize;
6878fae3551SRodney W. Grimes 
688fddf7baeSKirk McKusick 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
689cf96af72SBrian Feldman 	for (i = mntsize - 1; i >= 0; i--) {
6908fae3551SRodney W. Grimes 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
6918fae3551SRodney W. Grimes 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
6928fae3551SRodney W. Grimes 			return (&mntbuf[i]);
693cf96af72SBrian Feldman 	}
6948fae3551SRodney W. Grimes 	return (NULL);
6958fae3551SRodney W. Grimes }
6968fae3551SRodney W. Grimes 
6978fae3551SRodney W. Grimes char *
698e24dc56aSCraig Rodrigues catopt(char *s0, const char *s1)
6998fae3551SRodney W. Grimes {
7008fae3551SRodney W. Grimes 	size_t i;
7018fae3551SRodney W. Grimes 	char *cp;
7028fae3551SRodney W. Grimes 
70318af6044SJoseph Koshy 	if (s1 == NULL || *s1 == '\0')
7044796c6ccSJuli Mallett 		return (s0);
70518af6044SJoseph Koshy 
7068fae3551SRodney W. Grimes 	if (s0 && *s0) {
7078fae3551SRodney W. Grimes 		i = strlen(s0) + strlen(s1) + 1 + 1;
7088fae3551SRodney W. Grimes 		if ((cp = malloc(i)) == NULL)
709bcb1d846SPhilippe Charnier 			errx(1, "malloc failed");
7108fae3551SRodney W. Grimes 		(void)snprintf(cp, i, "%s,%s", s0, s1);
7118fae3551SRodney W. Grimes 	} else
7128fae3551SRodney W. Grimes 		cp = strdup(s1);
7138fae3551SRodney W. Grimes 
7148fae3551SRodney W. Grimes 	if (s0)
7158fae3551SRodney W. Grimes 		free(s0);
7168fae3551SRodney W. Grimes 	return (cp);
7178fae3551SRodney W. Grimes }
7188fae3551SRodney W. Grimes 
7198fae3551SRodney W. Grimes void
720a86de995SDavid E. O'Brien mangle(char *options, struct cpa *a)
7218fae3551SRodney W. Grimes {
722c7835769SCraig Rodrigues 	char *p, *s, *val;
7238fae3551SRodney W. Grimes 
7248fae3551SRodney W. Grimes 	for (s = options; (p = strsep(&s, ",")) != NULL;)
72518af6044SJoseph Koshy 		if (*p != '\0') {
726d9fa6ce7SCraig Rodrigues 			if (strcmp(p, "noauto") == 0) {
727d9fa6ce7SCraig Rodrigues 				/*
728d9fa6ce7SCraig Rodrigues 				 * Do not pass noauto option to nmount().
729d9fa6ce7SCraig Rodrigues 				 * or external mount program.  noauto is
730d9fa6ce7SCraig Rodrigues 				 * only used to prevent mounting a filesystem
731d9fa6ce7SCraig Rodrigues 				 * when 'mount -a' is specified, and is
732d9fa6ce7SCraig Rodrigues 				 * not a real mount option.
733d9fa6ce7SCraig Rodrigues 				 */
734d9fa6ce7SCraig Rodrigues 				continue;
7354b4f9170SDag-Erling Smørgrav 			} else if (strcmp(p, "late") == 0) {
7364b4f9170SDag-Erling Smørgrav 				/*
7374b4f9170SDag-Erling Smørgrav 				 * "late" is used to prevent certain file
7384b4f9170SDag-Erling Smørgrav 				 * systems from being mounted before late
7394b4f9170SDag-Erling Smørgrav 				 * in the boot cycle; for instance,
7404b4f9170SDag-Erling Smørgrav 				 * loopback NFS mounts can't be mounted
7414b4f9170SDag-Erling Smørgrav 				 * before mountd starts.
7424b4f9170SDag-Erling Smørgrav 				 */
7434b4f9170SDag-Erling Smørgrav 				continue;
744c7383075SXin LI 			} else if (strcmp(p, "failok") == 0) {
745c7383075SXin LI 				/*
746c7383075SXin LI 				 * "failok" is used to prevent certain file
747c7383075SXin LI 				 * systems from being causing the system to
748c7383075SXin LI 				 * drop into single user mode in the boot
749c7383075SXin LI 				 * cycle, and is not a real mount option.
750c7383075SXin LI 				 */
751c7383075SXin LI 				continue;
752c7835769SCraig Rodrigues 			} else if (strncmp(p, "mountprog", 9) == 0) {
753c7835769SCraig Rodrigues 				/*
754c7835769SCraig Rodrigues 				 * "mountprog" is used to force the use of
755c7835769SCraig Rodrigues 				 * userland mount programs.
756c7835769SCraig Rodrigues 				 */
757c7835769SCraig Rodrigues 				val = strchr(p, '=');
758c7835769SCraig Rodrigues                         	if (val != NULL) {
759c7835769SCraig Rodrigues                                 	++val;
760c7835769SCraig Rodrigues 					if (*val != '\0')
761c7835769SCraig Rodrigues 						mountprog = strdup(val);
762c7835769SCraig Rodrigues 				}
763c7835769SCraig Rodrigues 
764c7835769SCraig Rodrigues 				if (mountprog == NULL) {
765c7835769SCraig Rodrigues 					errx(1, "Need value for -o mountprog");
766c7835769SCraig Rodrigues 				}
767c7835769SCraig Rodrigues 				continue;
76835d6c7f5SCraig Rodrigues 			} else if (strcmp(p, "userquota") == 0) {
76935d6c7f5SCraig Rodrigues 				continue;
7706e74fb9dSMaxim Konovalov 			} else if (strncmp(p, userquotaeq,
7716e74fb9dSMaxim Konovalov 			    sizeof(userquotaeq) - 1) == 0) {
7726e74fb9dSMaxim Konovalov 				continue;
77335d6c7f5SCraig Rodrigues 			} else if (strcmp(p, "groupquota") == 0) {
77435d6c7f5SCraig Rodrigues 				continue;
7756e74fb9dSMaxim Konovalov 			} else if (strncmp(p, groupquotaeq,
7766e74fb9dSMaxim Konovalov 			    sizeof(groupquotaeq) - 1) == 0) {
7776e74fb9dSMaxim Konovalov 				continue;
778d9fa6ce7SCraig Rodrigues 			} else if (*p == '-') {
779a86de995SDavid E. O'Brien 				append_arg(a, p);
7808fae3551SRodney W. Grimes 				p = strchr(p, '=');
781adfdbe22STom Rhodes 				if (p != NULL) {
7828fae3551SRodney W. Grimes 					*p = '\0';
783a86de995SDavid E. O'Brien 					append_arg(a, p + 1);
7848fae3551SRodney W. Grimes 				}
785748e259bSCraig Rodrigues 			} else {
786a86de995SDavid E. O'Brien 				append_arg(a, strdup("-o"));
787a86de995SDavid E. O'Brien 				append_arg(a, p);
7888fae3551SRodney W. Grimes 			}
78918af6044SJoseph Koshy 		}
7908fae3551SRodney W. Grimes }
7918fae3551SRodney W. Grimes 
79218af6044SJoseph Koshy 
79318af6044SJoseph Koshy char *
7944796c6ccSJuli Mallett update_options(char *opts, char *fstab, int curflags)
79518af6044SJoseph Koshy {
79618af6044SJoseph Koshy 	char *o, *p;
79718af6044SJoseph Koshy 	char *cur;
79818af6044SJoseph Koshy 	char *expopt, *newopt, *tmpopt;
79918af6044SJoseph Koshy 
80018af6044SJoseph Koshy 	if (opts == NULL)
8014796c6ccSJuli Mallett 		return (strdup(""));
80218af6044SJoseph Koshy 
80318af6044SJoseph Koshy 	/* remove meta options from list */
80418af6044SJoseph Koshy 	remopt(fstab, MOUNT_META_OPTION_FSTAB);
80518af6044SJoseph Koshy 	remopt(fstab, MOUNT_META_OPTION_CURRENT);
80618af6044SJoseph Koshy 	cur = flags2opts(curflags);
80718af6044SJoseph Koshy 
80818af6044SJoseph Koshy 	/*
80918af6044SJoseph Koshy 	 * Expand all meta-options passed to us first.
81018af6044SJoseph Koshy 	 */
81118af6044SJoseph Koshy 	expopt = NULL;
81218af6044SJoseph Koshy 	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
81318af6044SJoseph Koshy 		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
81418af6044SJoseph Koshy 			expopt = catopt(expopt, fstab);
81518af6044SJoseph Koshy 		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
81618af6044SJoseph Koshy 			expopt = catopt(expopt, cur);
81718af6044SJoseph Koshy 		else
81818af6044SJoseph Koshy 			expopt = catopt(expopt, o);
81918af6044SJoseph Koshy 	}
82018af6044SJoseph Koshy 	free(cur);
82118af6044SJoseph Koshy 	free(opts);
82218af6044SJoseph Koshy 
82318af6044SJoseph Koshy 	/*
82418af6044SJoseph Koshy 	 * Remove previous contradictory arguments. Given option "foo" we
82518af6044SJoseph Koshy 	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
82618af6044SJoseph Koshy 	 * and "foo" - so we can deal with possible options like "notice".
82718af6044SJoseph Koshy 	 */
82818af6044SJoseph Koshy 	newopt = NULL;
82918af6044SJoseph Koshy 	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
83018af6044SJoseph Koshy 		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
83118af6044SJoseph Koshy 			errx(1, "malloc failed");
83218af6044SJoseph Koshy 
83318af6044SJoseph Koshy 		strcpy(tmpopt, "no");
83418af6044SJoseph Koshy 		strcat(tmpopt, o);
83518af6044SJoseph Koshy 		remopt(newopt, tmpopt);
83618af6044SJoseph Koshy 		free(tmpopt);
83718af6044SJoseph Koshy 
83818af6044SJoseph Koshy 		if (strncmp("no", o, 2) == 0)
83918af6044SJoseph Koshy 			remopt(newopt, o+2);
84018af6044SJoseph Koshy 
84118af6044SJoseph Koshy 		newopt = catopt(newopt, o);
84218af6044SJoseph Koshy 	}
84318af6044SJoseph Koshy 	free(expopt);
84418af6044SJoseph Koshy 
8454796c6ccSJuli Mallett 	return (newopt);
84618af6044SJoseph Koshy }
84718af6044SJoseph Koshy 
84818af6044SJoseph Koshy void
8494796c6ccSJuli Mallett remopt(char *string, const char *opt)
85018af6044SJoseph Koshy {
85118af6044SJoseph Koshy 	char *o, *p, *r;
85218af6044SJoseph Koshy 
85318af6044SJoseph Koshy 	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
85418af6044SJoseph Koshy 		return;
85518af6044SJoseph Koshy 
85618af6044SJoseph Koshy 	r = string;
85718af6044SJoseph Koshy 
85818af6044SJoseph Koshy 	for (p = string; (o = strsep(&p, ",")) != NULL;) {
85918af6044SJoseph Koshy 		if (strcmp(opt, o) != 0) {
86018af6044SJoseph Koshy 			if (*r == ',' && *o != '\0')
86118af6044SJoseph Koshy 				r++;
86218af6044SJoseph Koshy 			while ((*r++ = *o++) != '\0')
86318af6044SJoseph Koshy 			    ;
86418af6044SJoseph Koshy 			*--r = ',';
86518af6044SJoseph Koshy 		}
86618af6044SJoseph Koshy 	}
86718af6044SJoseph Koshy 	*r = '\0';
86818af6044SJoseph Koshy }
86918af6044SJoseph Koshy 
8708fae3551SRodney W. Grimes void
8714796c6ccSJuli Mallett usage(void)
8728fae3551SRodney W. Grimes {
8738fae3551SRodney W. Grimes 
874bcb1d846SPhilippe Charnier 	(void)fprintf(stderr, "%s\n%s\n%s\n",
8754b4f9170SDag-Erling Smørgrav "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
8768d646af5SRuslan Ermilov "       mount [-dfpruvw] special | node",
8778d646af5SRuslan Ermilov "       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
8788fae3551SRodney W. Grimes 	exit(1);
8798fae3551SRodney W. Grimes }
880a257a45eSJordan K. Hubbard 
881a257a45eSJordan K. Hubbard void
882031ea52fSMatteo Riondato putfsent(struct statfs *ent)
883a257a45eSJordan K. Hubbard {
884a257a45eSJordan K. Hubbard 	struct fstab *fst;
88572da5470SJaakko Heinonen 	char *opts, *rw;
886306d73d6SPoul-Henning Kamp 	int l;
887a257a45eSJordan K. Hubbard 
88872da5470SJaakko Heinonen 	opts = NULL;
88972da5470SJaakko Heinonen 	/* flags2opts() doesn't return the "rw" option. */
89072da5470SJaakko Heinonen 	if ((ent->f_flags & MNT_RDONLY) != 0)
89172da5470SJaakko Heinonen 		rw = NULL;
89272da5470SJaakko Heinonen 	else
89372da5470SJaakko Heinonen 		rw = catopt(NULL, "rw");
89472da5470SJaakko Heinonen 
89518af6044SJoseph Koshy 	opts = flags2opts(ent->f_flags);
89672da5470SJaakko Heinonen 	opts = catopt(rw, opts);
8973ae7ea68SGiorgos Keramidas 
898031ea52fSMatteo Riondato 	if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
899031ea52fSMatteo Riondato 	    strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
900031ea52fSMatteo Riondato 		strcpy(ent->f_mntfromname, (strnstr(ent->f_mntfromname, ":", 8)
901031ea52fSMatteo Riondato 		    +1));
902031ea52fSMatteo Riondato 	}
903031ea52fSMatteo Riondato 
904306d73d6SPoul-Henning Kamp 	l = strlen(ent->f_mntfromname);
905306d73d6SPoul-Henning Kamp 	printf("%s%s%s%s", ent->f_mntfromname,
906306d73d6SPoul-Henning Kamp 	    l < 8 ? "\t" : "",
907306d73d6SPoul-Henning Kamp 	    l < 16 ? "\t" : "",
908306d73d6SPoul-Henning Kamp 	    l < 24 ? "\t" : " ");
909306d73d6SPoul-Henning Kamp 	l = strlen(ent->f_mntonname);
910306d73d6SPoul-Henning Kamp 	printf("%s%s%s%s", ent->f_mntonname,
911306d73d6SPoul-Henning Kamp 	    l < 8 ? "\t" : "",
912306d73d6SPoul-Henning Kamp 	    l < 16 ? "\t" : "",
913306d73d6SPoul-Henning Kamp 	    l < 24 ? "\t" : " ");
914306d73d6SPoul-Henning Kamp 	printf("%s\t", ent->f_fstypename);
915306d73d6SPoul-Henning Kamp 	l = strlen(opts);
916306d73d6SPoul-Henning Kamp 	printf("%s%s", opts,
917306d73d6SPoul-Henning Kamp 	    l < 8 ? "\t" : " ");
91818af6044SJoseph Koshy 	free(opts);
919c06fe0a0SPeter Wemm 
920fba1c154SSteve Price 	if ((fst = getfsspec(ent->f_mntfromname)))
921a257a45eSJordan K. Hubbard 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
922fba1c154SSteve Price 	else if ((fst = getfsfile(ent->f_mntonname)))
923a257a45eSJordan K. Hubbard 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
924ab80d6faSBrian Feldman 	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
925ab80d6faSBrian Feldman 		if (strcmp(ent->f_mntonname, "/") == 0)
926a257a45eSJordan K. Hubbard 			printf("\t1 1\n");
927a257a45eSJordan K. Hubbard 		else
928ab80d6faSBrian Feldman 			printf("\t2 2\n");
929ab80d6faSBrian Feldman 	} else
930a257a45eSJordan K. Hubbard 		printf("\t0 0\n");
931a257a45eSJordan K. Hubbard }
93218af6044SJoseph Koshy 
93318af6044SJoseph Koshy 
93418af6044SJoseph Koshy char *
9354796c6ccSJuli Mallett flags2opts(int flags)
93618af6044SJoseph Koshy {
93718af6044SJoseph Koshy 	char *res;
93818af6044SJoseph Koshy 
93918af6044SJoseph Koshy 	res = NULL;
94018af6044SJoseph Koshy 
94188e2c335SCraig Rodrigues 	if (flags & MNT_RDONLY)		res = catopt(res, "ro");
94218af6044SJoseph Koshy 	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
94318af6044SJoseph Koshy 	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
94418af6044SJoseph Koshy 	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
94518af6044SJoseph Koshy 	if (flags & MNT_UNION)		res = catopt(res, "union");
94618af6044SJoseph Koshy 	if (flags & MNT_ASYNC)		res = catopt(res, "async");
94718af6044SJoseph Koshy 	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
94818af6044SJoseph Koshy 	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
94918af6044SJoseph Koshy 	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
95018af6044SJoseph Koshy 	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
95118af6044SJoseph Koshy 	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
952ba0fbe96SRobert Watson 	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
95303d94b50SRobert Watson 	if (flags & MNT_ACLS)		res = catopt(res, "acls");
9549340fc72SEdward Tomasz Napierala 	if (flags & MNT_NFS4ACLS)	res = catopt(res, "nfsv4acls");
95518af6044SJoseph Koshy 
9564796c6ccSJuli Mallett 	return (res);
95718af6044SJoseph Koshy }
958