xref: /freebsd/sbin/mount/mount.c (revision c783576970fd40b22705bda102c03976d78badd2)
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 
6974cf460bSBruce Evans 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 {
948fae3551SRodney W. Grimes 	int 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" },
112ba0fbe96SRobert Watson 	{ MNT_MULTILABEL,	"multilabel" },
11303d94b50SRobert Watson 	{ MNT_ACLS,		"acls" },
114ac88569cSPawel Jakub Dawidek 	{ MNT_GJOURNAL,		"gjournal" },
11518af6044SJoseph Koshy 	{ 0, NULL }
1168fae3551SRodney W. Grimes };
1178fae3551SRodney W. Grimes 
118fba1c154SSteve Price /*
119fba1c154SSteve Price  * List of VFS types that can be remounted without becoming mounted on top
120fba1c154SSteve Price  * of each other.
121fba1c154SSteve Price  * XXX Is this list correct?
122fba1c154SSteve Price  */
123fba1c154SSteve Price static const char *
124fba1c154SSteve Price remountable_fs_names[] = {
1255a4420e3SAlexey Zelkin 	"ufs", "ffs", "ext2fs",
126fba1c154SSteve Price 	0
127fba1c154SSteve Price };
128fba1c154SSteve Price 
1296e74fb9dSMaxim Konovalov static const char userquotaeq[] = "userquota=";
1306e74fb9dSMaxim Konovalov static const char groupquotaeq[] = "groupquota=";
1316e74fb9dSMaxim Konovalov 
132c7835769SCraig Rodrigues static char *mountprog = NULL;
133c7835769SCraig Rodrigues 
1346f5f1a6bSCraig Rodrigues static int
1356f5f1a6bSCraig Rodrigues use_mountprog(const char *vfstype)
1366f5f1a6bSCraig Rodrigues {
1376f5f1a6bSCraig Rodrigues 	/* XXX: We need to get away from implementing external mount
1386f5f1a6bSCraig Rodrigues 	 *      programs for every filesystem, and move towards having
1396f5f1a6bSCraig Rodrigues 	 *	each filesystem properly implement the nmount() system call.
1406f5f1a6bSCraig Rodrigues 	 */
1416f5f1a6bSCraig Rodrigues 	unsigned int i;
1426f5f1a6bSCraig Rodrigues 	const char *fs[] = {
1436973ce04STai-hwa Liang 	"cd9660", "mfs", "msdosfs", "nfs", "nfs4", "ntfs",
144534046e3SRong-En Fan 	"nwfs", "nullfs", "portalfs", "smbfs", "udf", "unionfs",
1456f5f1a6bSCraig Rodrigues 	NULL
1466f5f1a6bSCraig Rodrigues 	};
1476f5f1a6bSCraig Rodrigues 
148c7835769SCraig Rodrigues 	if (mountprog != NULL)
149c7835769SCraig Rodrigues 		return (1);
150c7835769SCraig Rodrigues 
1516f5f1a6bSCraig Rodrigues 	for (i = 0; fs[i] != NULL; ++i) {
1526f5f1a6bSCraig Rodrigues 		if (strcmp(vfstype, fs[i]) == 0)
1534796c6ccSJuli Mallett 			return (1);
1546f5f1a6bSCraig Rodrigues 	}
1556f5f1a6bSCraig Rodrigues 
1564796c6ccSJuli Mallett 	return (0);
1576f5f1a6bSCraig Rodrigues }
1586f5f1a6bSCraig Rodrigues 
1596f5f1a6bSCraig Rodrigues static int
1604796c6ccSJuli Mallett exec_mountprog(const char *name, const char *execname, char *const argv[])
1616f5f1a6bSCraig Rodrigues {
1626f5f1a6bSCraig Rodrigues 	pid_t pid;
1636f5f1a6bSCraig Rodrigues 	int status;
1646f5f1a6bSCraig Rodrigues 
1656f5f1a6bSCraig Rodrigues 	switch (pid = fork()) {
1666f5f1a6bSCraig Rodrigues 	case -1:				/* Error. */
1676f5f1a6bSCraig Rodrigues 		warn("fork");
1686f5f1a6bSCraig Rodrigues 		exit (1);
1696f5f1a6bSCraig Rodrigues 	case 0:					/* Child. */
1706f5f1a6bSCraig Rodrigues 		/* Go find an executable. */
1716f5f1a6bSCraig Rodrigues 		execvP(execname, _PATH_SYSPATH, argv);
1726f5f1a6bSCraig Rodrigues 		if (errno == ENOENT) {
173c7835769SCraig Rodrigues 			warn("exec %s not found", execname);
174c7835769SCraig Rodrigues 			if (execname[0] != '/') {
175c7835769SCraig Rodrigues 				warnx("in path: %s", _PATH_SYSPATH);
176c7835769SCraig Rodrigues 			}
1776f5f1a6bSCraig Rodrigues 		}
1786f5f1a6bSCraig Rodrigues 		exit(1);
1796f5f1a6bSCraig Rodrigues 	default:				/* Parent. */
1806f5f1a6bSCraig Rodrigues 		if (waitpid(pid, &status, 0) < 0) {
1816f5f1a6bSCraig Rodrigues 			warn("waitpid");
1826f5f1a6bSCraig Rodrigues 			return (1);
1836f5f1a6bSCraig Rodrigues 		}
1846f5f1a6bSCraig Rodrigues 
1856f5f1a6bSCraig Rodrigues 		if (WIFEXITED(status)) {
1866f5f1a6bSCraig Rodrigues 			if (WEXITSTATUS(status) != 0)
1876f5f1a6bSCraig Rodrigues 				return (WEXITSTATUS(status));
1886f5f1a6bSCraig Rodrigues 		} else if (WIFSIGNALED(status)) {
1896f5f1a6bSCraig Rodrigues 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
1906f5f1a6bSCraig Rodrigues 			return (1);
1916f5f1a6bSCraig Rodrigues 		}
1926f5f1a6bSCraig Rodrigues 		break;
1936f5f1a6bSCraig Rodrigues 	}
1946f5f1a6bSCraig Rodrigues 
1956f5f1a6bSCraig Rodrigues 	return (0);
1966f5f1a6bSCraig Rodrigues }
1976f5f1a6bSCraig Rodrigues 
19849a41c4fSRuslan Ermilov static int
19949a41c4fSRuslan Ermilov specified_ro(const char *arg)
2003cbf527eSRuslan Ermilov {
2013cbf527eSRuslan Ermilov 	char *optbuf, *opt;
2023cbf527eSRuslan Ermilov 	int ret = 0;
2033cbf527eSRuslan Ermilov 
2043cbf527eSRuslan Ermilov 	optbuf = strdup(arg);
2053cbf527eSRuslan Ermilov 	if (optbuf == NULL)
2063cbf527eSRuslan Ermilov 		 err(1, NULL);
2073cbf527eSRuslan Ermilov 
2083cbf527eSRuslan Ermilov 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
2093cbf527eSRuslan Ermilov 		if (strcmp(opt, "ro") == 0) {
2103cbf527eSRuslan Ermilov 			ret = 1;
2113cbf527eSRuslan Ermilov 			break;
2123cbf527eSRuslan Ermilov 		}
2133cbf527eSRuslan Ermilov 	}
2143cbf527eSRuslan Ermilov 	free(optbuf);
2153cbf527eSRuslan Ermilov 	return (ret);
2163cbf527eSRuslan Ermilov }
2173cbf527eSRuslan Ermilov 
218e9988cedSPawel Jakub Dawidek static void
219e9988cedSPawel Jakub Dawidek restart_mountd(void)
220e9988cedSPawel Jakub Dawidek {
221e9988cedSPawel Jakub Dawidek 	struct pidfh *pfh;
222e9988cedSPawel Jakub Dawidek 	pid_t mountdpid;
223e9988cedSPawel Jakub Dawidek 
224e9988cedSPawel Jakub Dawidek 	pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
225e9988cedSPawel Jakub Dawidek 	if (pfh != NULL) {
226e9988cedSPawel Jakub Dawidek 		/* Mountd is not running. */
227e9988cedSPawel Jakub Dawidek 		pidfile_remove(pfh);
228e9988cedSPawel Jakub Dawidek 		return;
229e9988cedSPawel Jakub Dawidek 	}
230e9988cedSPawel Jakub Dawidek 	if (errno != EEXIST) {
231e9988cedSPawel Jakub Dawidek 		/* Cannot open pidfile for some reason. */
232e9988cedSPawel Jakub Dawidek 		return;
233e9988cedSPawel Jakub Dawidek 	}
234e9988cedSPawel Jakub Dawidek 	/* We have mountd(8) PID in mountdpid varible, let's signal it. */
235e9988cedSPawel Jakub Dawidek 	if (kill(mountdpid, SIGHUP) == -1)
236e9988cedSPawel Jakub Dawidek 		err(1, "signal mountd");
237e9988cedSPawel Jakub Dawidek }
238e9988cedSPawel Jakub Dawidek 
2398fae3551SRodney W. Grimes int
240e24dc56aSCraig Rodrigues main(int argc, char *argv[])
2418fae3551SRodney W. Grimes {
242c06fe0a0SPeter Wemm 	const char *mntfromname, **vfslist, *vfstype;
2438fae3551SRodney W. Grimes 	struct fstab *fs;
2448fae3551SRodney W. Grimes 	struct statfs *mntbuf;
2453cbf527eSRuslan Ermilov 	int all, ch, i, init_flags, late, mntsize, rval, have_fstab, ro;
246003dbca6SMaxime Henrion 	char *cp, *ep, *options;
2478fae3551SRodney W. Grimes 
2484b4f9170SDag-Erling Smørgrav 	all = init_flags = late = 0;
2493cbf527eSRuslan Ermilov 	ro = 0;
2503cbf527eSRuslan Ermilov 	options = NULL;
2518fae3551SRodney W. Grimes 	vfslist = NULL;
2528fae3551SRodney W. Grimes 	vfstype = "ufs";
25349a41c4fSRuslan Ermilov 	while ((ch = getopt(argc, argv, "adF:flo:prt:uvw")) != -1)
2548fae3551SRodney W. Grimes 		switch (ch) {
2558fae3551SRodney W. Grimes 		case 'a':
2568fae3551SRodney W. Grimes 			all = 1;
2578fae3551SRodney W. Grimes 			break;
2588fae3551SRodney W. Grimes 		case 'd':
2598fae3551SRodney W. Grimes 			debug = 1;
2608fae3551SRodney W. Grimes 			break;
261ef258dd9SMatthew N. Dodd 		case 'F':
262ef258dd9SMatthew N. Dodd 			setfstab(optarg);
263ef258dd9SMatthew N. Dodd 			break;
2648fae3551SRodney W. Grimes 		case 'f':
2658fae3551SRodney W. Grimes 			init_flags |= MNT_FORCE;
2668fae3551SRodney W. Grimes 			break;
2674b4f9170SDag-Erling Smørgrav 		case 'l':
2684b4f9170SDag-Erling Smørgrav 			late = 1;
2694b4f9170SDag-Erling Smørgrav 			break;
2708fae3551SRodney W. Grimes 		case 'o':
2713cbf527eSRuslan Ermilov 			if (*optarg) {
2728fae3551SRodney W. Grimes 				options = catopt(options, optarg);
2733cbf527eSRuslan Ermilov 				if (specified_ro(optarg))
2743cbf527eSRuslan Ermilov 					ro = 1;
2753cbf527eSRuslan Ermilov 			}
2768fae3551SRodney W. Grimes 			break;
27774cf460bSBruce Evans 		case 'p':
27874cf460bSBruce Evans 			fstab_style = 1;
27974cf460bSBruce Evans 			verbose = 1;
28074cf460bSBruce Evans 			break;
2818fae3551SRodney W. Grimes 		case 'r':
282ad044771SDima Dorfman 			options = catopt(options, "ro");
2833cbf527eSRuslan Ermilov 			ro = 1;
2848fae3551SRodney W. Grimes 			break;
2858fae3551SRodney W. Grimes 		case 't':
2868fae3551SRodney W. Grimes 			if (vfslist != NULL)
287bcb1d846SPhilippe Charnier 				errx(1, "only one -t option may be specified");
2888fae3551SRodney W. Grimes 			vfslist = makevfslist(optarg);
2898fae3551SRodney W. Grimes 			vfstype = optarg;
2908fae3551SRodney W. Grimes 			break;
2918fae3551SRodney W. Grimes 		case 'u':
2928fae3551SRodney W. Grimes 			init_flags |= MNT_UPDATE;
2938fae3551SRodney W. Grimes 			break;
2948fae3551SRodney W. Grimes 		case 'v':
2958fae3551SRodney W. Grimes 			verbose = 1;
2968fae3551SRodney W. Grimes 			break;
2978fae3551SRodney W. Grimes 		case 'w':
298ad044771SDima Dorfman 			options = catopt(options, "noro");
2998fae3551SRodney W. Grimes 			break;
3008fae3551SRodney W. Grimes 		case '?':
3018fae3551SRodney W. Grimes 		default:
3028fae3551SRodney W. Grimes 			usage();
3038fae3551SRodney W. Grimes 			/* NOTREACHED */
3048fae3551SRodney W. Grimes 		}
3058fae3551SRodney W. Grimes 	argc -= optind;
3068fae3551SRodney W. Grimes 	argv += optind;
3078fae3551SRodney W. Grimes 
3088fae3551SRodney W. Grimes #define	BADTYPE(type)							\
3098fae3551SRodney W. Grimes 	(strcmp(type, FSTAB_RO) &&					\
3108fae3551SRodney W. Grimes 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
3118fae3551SRodney W. Grimes 
3123cbf527eSRuslan Ermilov 	if ((init_flags & MNT_UPDATE) && (ro == 0))
3133cbf527eSRuslan Ermilov 		options = catopt(options, "noro");
3143cbf527eSRuslan Ermilov 
3158fae3551SRodney W. Grimes 	rval = 0;
3168fae3551SRodney W. Grimes 	switch (argc) {
3178fae3551SRodney W. Grimes 	case 0:
318fba1c154SSteve Price 		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
319fba1c154SSteve Price 			err(1, "getmntinfo");
320fba1c154SSteve Price 		if (all) {
3218fae3551SRodney W. Grimes 			while ((fs = getfsent()) != NULL) {
3228fae3551SRodney W. Grimes 				if (BADTYPE(fs->fs_type))
3238fae3551SRodney W. Grimes 					continue;
324c06fe0a0SPeter Wemm 				if (checkvfsname(fs->fs_vfstype, vfslist))
3258fae3551SRodney W. Grimes 					continue;
326c06fe0a0SPeter Wemm 				if (hasopt(fs->fs_mntops, "noauto"))
32789beb278SDavid Greenman 					continue;
3284b4f9170SDag-Erling Smørgrav 				if (hasopt(fs->fs_mntops, "late") && !late)
3294b4f9170SDag-Erling Smørgrav 					continue;
33098201b0cSBruce Evans 				if (!(init_flags & MNT_UPDATE) &&
33198201b0cSBruce Evans 				    ismounted(fs, mntbuf, mntsize))
332fba1c154SSteve Price 					continue;
33313cbdf24SGuido van Rooij 				options = update_options(options, fs->fs_mntops,
33413cbdf24SGuido van Rooij 				    mntbuf->f_flags);
3358fae3551SRodney W. Grimes 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
3368fae3551SRodney W. Grimes 				    fs->fs_file, init_flags, options,
3378fae3551SRodney W. Grimes 				    fs->fs_mntops))
3388fae3551SRodney W. Grimes 					rval = 1;
3398fae3551SRodney W. Grimes 			}
340fba1c154SSteve Price 		} else if (fstab_style) {
341a257a45eSJordan K. Hubbard 			for (i = 0; i < mntsize; i++) {
342c06fe0a0SPeter Wemm 				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
343a257a45eSJordan K. Hubbard 					continue;
344a257a45eSJordan K. Hubbard 				putfsent(&mntbuf[i]);
345a257a45eSJordan K. Hubbard 			}
34674cf460bSBruce Evans 		} else {
3478fae3551SRodney W. Grimes 			for (i = 0; i < mntsize; i++) {
34874cf460bSBruce Evans 				if (checkvfsname(mntbuf[i].f_fstypename,
34974cf460bSBruce Evans 				    vfslist))
3508fae3551SRodney W. Grimes 					continue;
351c06fe0a0SPeter Wemm 				prmount(&mntbuf[i]);
3528fae3551SRodney W. Grimes 			}
3538fae3551SRodney W. Grimes 		}
3548fae3551SRodney W. Grimes 		exit(rval);
3558fae3551SRodney W. Grimes 	case 1:
3568fae3551SRodney W. Grimes 		if (vfslist != NULL)
3578fae3551SRodney W. Grimes 			usage();
3588fae3551SRodney W. Grimes 
35901a9bce5SEric Anholt 		rmslashes(*argv, *argv);
3608fae3551SRodney W. Grimes 		if (init_flags & MNT_UPDATE) {
361cf96af72SBrian Feldman 			mntfromname = NULL;
362cf96af72SBrian Feldman 			have_fstab = 0;
3638fae3551SRodney W. Grimes 			if ((mntbuf = getmntpt(*argv)) == NULL)
364cf96af72SBrian Feldman 				errx(1, "not currently mounted %s", *argv);
365cf96af72SBrian Feldman 			/*
366cf96af72SBrian Feldman 			 * Only get the mntflags from fstab if both mntpoint
367cf96af72SBrian Feldman 			 * and mntspec are identical. Also handle the special
368cf96af72SBrian Feldman 			 * case where just '/' is mounted and 'spec' is not
369cf96af72SBrian Feldman 			 * identical with the one from fstab ('/dev' is missing
370cf96af72SBrian Feldman 			 * in the spec-string at boot-time).
371cf96af72SBrian Feldman 			 */
37218af6044SJoseph Koshy 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
373cf96af72SBrian Feldman 				if (strcmp(fs->fs_spec,
374cf96af72SBrian Feldman 				    mntbuf->f_mntfromname) == 0 &&
375cf96af72SBrian Feldman 				    strcmp(fs->fs_file,
376cf96af72SBrian Feldman 				    mntbuf->f_mntonname) == 0) {
377cf96af72SBrian Feldman 					have_fstab = 1;
378cf96af72SBrian Feldman 					mntfromname = mntbuf->f_mntfromname;
379cf96af72SBrian Feldman 				} else if (argv[0][0] == '/' &&
380cf96af72SBrian Feldman 				    argv[0][1] == '\0') {
381cf96af72SBrian Feldman 					fs = getfsfile("/");
382cf96af72SBrian Feldman 					have_fstab = 1;
383c06fe0a0SPeter Wemm 					mntfromname = fs->fs_spec;
384cf96af72SBrian Feldman 				}
385cf96af72SBrian Feldman 			}
386cf96af72SBrian Feldman 			if (have_fstab) {
38718af6044SJoseph Koshy 				options = update_options(options, fs->fs_mntops,
38818af6044SJoseph Koshy 				    mntbuf->f_flags);
38918af6044SJoseph Koshy 			} else {
390c06fe0a0SPeter Wemm 				mntfromname = mntbuf->f_mntfromname;
39118af6044SJoseph Koshy 				options = update_options(options, NULL,
39218af6044SJoseph Koshy 				    mntbuf->f_flags);
39318af6044SJoseph Koshy 			}
394c06fe0a0SPeter Wemm 			rval = mountfs(mntbuf->f_fstypename, mntfromname,
395c06fe0a0SPeter Wemm 			    mntbuf->f_mntonname, init_flags, options, 0);
396c06fe0a0SPeter Wemm 			break;
397c06fe0a0SPeter Wemm 		}
3988fae3551SRodney W. Grimes 		if ((fs = getfsfile(*argv)) == NULL &&
3998fae3551SRodney W. Grimes 		    (fs = getfsspec(*argv)) == NULL)
400bcb1d846SPhilippe Charnier 			errx(1, "%s: unknown special file or file system",
4018fae3551SRodney W. Grimes 			    *argv);
4028fae3551SRodney W. Grimes 		if (BADTYPE(fs->fs_type))
403bcb1d846SPhilippe Charnier 			errx(1, "%s has unknown file system type",
4048fae3551SRodney W. Grimes 			    *argv);
405c06fe0a0SPeter Wemm 		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
406c06fe0a0SPeter Wemm 		    init_flags, options, fs->fs_mntops);
4078fae3551SRodney W. Grimes 		break;
4088fae3551SRodney W. Grimes 	case 2:
4098fae3551SRodney W. Grimes 		/*
410cf96af72SBrian Feldman 		 * If -t flag has not been specified, the path cannot be
411003dbca6SMaxime Henrion 		 * found, spec contains either a ':' or a '@', then assume
412cf96af72SBrian Feldman 		 * that an NFS file system is being specified ala Sun.
413003dbca6SMaxime Henrion 		 * Check if the hostname contains only allowed characters
414003dbca6SMaxime Henrion 		 * to reduce false positives.  IPv6 addresses containing
415003dbca6SMaxime Henrion 		 * ':' will be correctly parsed only if the separator is '@'.
416003dbca6SMaxime Henrion 		 * The definition of a valid hostname is taken from RFC 1034.
4178fae3551SRodney W. Grimes 		 */
41805779418SIan Dowse 		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
41905779418SIan Dowse 		    (ep = strchr(argv[0], ':')) != NULL)) {
420da85c82bSMaxime Henrion 			if (*ep == '@') {
421da85c82bSMaxime Henrion 				cp = ep + 1;
422da85c82bSMaxime Henrion 				ep = cp + strlen(cp);
423da85c82bSMaxime Henrion 			} else
424003dbca6SMaxime Henrion 				cp = argv[0];
425003dbca6SMaxime Henrion 			while (cp != ep) {
426003dbca6SMaxime Henrion 				if (!isdigit(*cp) && !isalpha(*cp) &&
427003dbca6SMaxime Henrion 				    *cp != '.' && *cp != '-' && *cp != ':')
428003dbca6SMaxime Henrion 					break;
429003dbca6SMaxime Henrion 				cp++;
430003dbca6SMaxime Henrion 			}
431003dbca6SMaxime Henrion 			if (cp == ep)
4328fae3551SRodney W. Grimes 				vfstype = "nfs";
433003dbca6SMaxime Henrion 		}
4348fae3551SRodney W. Grimes 		rval = mountfs(vfstype,
4358fae3551SRodney W. Grimes 		    argv[0], argv[1], init_flags, options, NULL);
4368fae3551SRodney W. Grimes 		break;
4378fae3551SRodney W. Grimes 	default:
4388fae3551SRodney W. Grimes 		usage();
4398fae3551SRodney W. Grimes 		/* NOTREACHED */
4408fae3551SRodney W. Grimes 	}
4418fae3551SRodney W. Grimes 
4428fae3551SRodney W. Grimes 	/*
4438fae3551SRodney W. Grimes 	 * If the mount was successfully, and done by root, tell mountd the
444e9988cedSPawel Jakub Dawidek 	 * good news.
4458fae3551SRodney W. Grimes 	 */
446e9988cedSPawel Jakub Dawidek 	if (rval == 0 && getuid() == 0)
447e9988cedSPawel Jakub Dawidek 		restart_mountd();
4488fae3551SRodney W. Grimes 
4498fae3551SRodney W. Grimes 	exit(rval);
4508fae3551SRodney W. Grimes }
4518fae3551SRodney W. Grimes 
4528fae3551SRodney W. Grimes int
453e24dc56aSCraig Rodrigues ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
454fba1c154SSteve Price {
45590742659SPawel Jakub Dawidek 	char realfsfile[PATH_MAX];
456fba1c154SSteve Price 	int i;
457fba1c154SSteve Price 
458fba1c154SSteve Price 	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
459fba1c154SSteve Price 		/* the root file system can always be remounted */
460fba1c154SSteve Price 		return (0);
461fba1c154SSteve Price 
46290742659SPawel Jakub Dawidek 	/* The user may have specified a symlink in fstab, resolve the path */
46390742659SPawel Jakub Dawidek 	if (realpath(fs->fs_file, realfsfile) == NULL) {
46490742659SPawel Jakub Dawidek 		/* Cannot resolve the path, use original one */
46590742659SPawel Jakub Dawidek 		strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
46690742659SPawel Jakub Dawidek 	}
46790742659SPawel Jakub Dawidek 
468fba1c154SSteve Price 	for (i = mntsize - 1; i >= 0; --i)
46990742659SPawel Jakub Dawidek 		if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
470fba1c154SSteve Price 		    (!isremountable(fs->fs_vfstype) ||
471fba1c154SSteve Price 		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
472fba1c154SSteve Price 			return (1);
473fba1c154SSteve Price 	return (0);
474fba1c154SSteve Price }
475fba1c154SSteve Price 
476fba1c154SSteve Price int
477e24dc56aSCraig Rodrigues isremountable(const char *vfsname)
478fba1c154SSteve Price {
479fba1c154SSteve Price 	const char **cp;
480fba1c154SSteve Price 
481fba1c154SSteve Price 	for (cp = remountable_fs_names; *cp; cp++)
482fba1c154SSteve Price 		if (strcmp(*cp, vfsname) == 0)
483fba1c154SSteve Price 			return (1);
484fba1c154SSteve Price 	return (0);
485fba1c154SSteve Price }
486fba1c154SSteve Price 
487fba1c154SSteve Price int
488e24dc56aSCraig Rodrigues hasopt(const char *mntopts, const char *option)
489c06fe0a0SPeter Wemm {
490c06fe0a0SPeter Wemm 	int negative, found;
491c06fe0a0SPeter Wemm 	char *opt, *optbuf;
492c06fe0a0SPeter Wemm 
493c06fe0a0SPeter Wemm 	if (option[0] == 'n' && option[1] == 'o') {
494c06fe0a0SPeter Wemm 		negative = 1;
495c06fe0a0SPeter Wemm 		option += 2;
496c06fe0a0SPeter Wemm 	} else
497c06fe0a0SPeter Wemm 		negative = 0;
498c06fe0a0SPeter Wemm 	optbuf = strdup(mntopts);
499c06fe0a0SPeter Wemm 	found = 0;
500c06fe0a0SPeter Wemm 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
501c06fe0a0SPeter Wemm 		if (opt[0] == 'n' && opt[1] == 'o') {
502c06fe0a0SPeter Wemm 			if (!strcasecmp(opt + 2, option))
503c06fe0a0SPeter Wemm 				found = negative;
504c06fe0a0SPeter Wemm 		} else if (!strcasecmp(opt, option))
505c06fe0a0SPeter Wemm 			found = !negative;
506c06fe0a0SPeter Wemm 	}
507c06fe0a0SPeter Wemm 	free(optbuf);
508c06fe0a0SPeter Wemm 	return (found);
509c06fe0a0SPeter Wemm }
510c06fe0a0SPeter Wemm 
511a86de995SDavid E. O'Brien static void
512a86de995SDavid E. O'Brien append_arg(struct cpa *sa, char *arg)
513a86de995SDavid E. O'Brien {
5140e969ed7SDavid E. O'Brien 	if (sa->c + 1 == sa->sz) {
5150e969ed7SDavid E. O'Brien 		sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
5160e969ed7SDavid E. O'Brien 		sa->a = realloc(sa->a, sizeof(sa->a) * sa->sz);
51700356118SDavid E. O'Brien 		if (sa->a == NULL)
51800356118SDavid E. O'Brien 			errx(1, "realloc failed");
51900356118SDavid E. O'Brien 	}
520a86de995SDavid E. O'Brien 	sa->a[++sa->c] = arg;
521a86de995SDavid E. O'Brien }
522a86de995SDavid E. O'Brien 
523c06fe0a0SPeter Wemm int
524e24dc56aSCraig Rodrigues mountfs(const char *vfstype, const char *spec, const char *name, int flags,
525e24dc56aSCraig Rodrigues 	const char *options, const char *mntopts)
5268fae3551SRodney W. Grimes {
5278fae3551SRodney W. Grimes 	struct statfs sf;
52876c46216SDavid E. O'Brien 	int i, ret;
52968dd1ff4SWarner Losh 	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
5300e969ed7SDavid E. O'Brien 	static struct cpa mnt_argv;
5318fae3551SRodney W. Grimes 
53273dd3167SPoul-Henning Kamp 	/* resolve the mountpoint with realpath(3) */
53373dd3167SPoul-Henning Kamp 	(void)checkpath(name, mntpath);
5348fae3551SRodney W. Grimes 	name = mntpath;
5358fae3551SRodney W. Grimes 
536c06fe0a0SPeter Wemm 	if (mntopts == NULL)
537c06fe0a0SPeter Wemm 		mntopts = "";
5388fae3551SRodney W. Grimes 	optbuf = catopt(strdup(mntopts), options);
5398fae3551SRodney W. Grimes 
5408fae3551SRodney W. Grimes 	if (strcmp(name, "/") == 0)
5418fae3551SRodney W. Grimes 		flags |= MNT_UPDATE;
5428fae3551SRodney W. Grimes 	if (flags & MNT_FORCE)
5438fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "force");
5448fae3551SRodney W. Grimes 	if (flags & MNT_RDONLY)
5458fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "ro");
5468fae3551SRodney W. Grimes 	/*
5478fae3551SRodney W. Grimes 	 * XXX
5488fae3551SRodney W. Grimes 	 * The mount_mfs (newfs) command uses -o to select the
549bcb1d846SPhilippe Charnier 	 * optimization mode.  We don't pass the default "-o rw"
5508fae3551SRodney W. Grimes 	 * for that reason.
5518fae3551SRodney W. Grimes 	 */
5528fae3551SRodney W. Grimes 	if (flags & MNT_UPDATE)
5538fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "update");
5548fae3551SRodney W. Grimes 
5554ccd7546SRuslan Ermilov 	/* Compatibility glue. */
5565b548564SCraig Rodrigues 	if (strcmp(vfstype, "msdos") == 0) {
5575b548564SCraig Rodrigues 		warnx(
5585b548564SCraig Rodrigues 		    "Using \"-t msdosfs\", since \"-t msdos\" is deprecated.");
5594ccd7546SRuslan Ermilov 		vfstype = "msdosfs";
5605b548564SCraig Rodrigues 	}
5614ccd7546SRuslan Ermilov 
5626e34b479SColin Percival 	/* Construct the name of the appropriate mount command */
5636e34b479SColin Percival 	(void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
5646e34b479SColin Percival 
565a86de995SDavid E. O'Brien 	mnt_argv.c = -1;
566a86de995SDavid E. O'Brien 	append_arg(&mnt_argv, execname);
567a86de995SDavid E. O'Brien 	mangle(optbuf, &mnt_argv);
568c7835769SCraig Rodrigues 	if (mountprog != NULL)
569c7835769SCraig Rodrigues 		strcpy(execname, mountprog);
570c7835769SCraig Rodrigues 
571a86de995SDavid E. O'Brien 	append_arg(&mnt_argv, strdup(spec));
572a86de995SDavid E. O'Brien 	append_arg(&mnt_argv, strdup(name));
573a86de995SDavid E. O'Brien 	append_arg(&mnt_argv, NULL);
574fce5f960SDavid E. O'Brien 
5758fae3551SRodney W. Grimes 	if (debug) {
576c195c7f6SCraig Rodrigues 		if (use_mountprog(vfstype))
577c7835769SCraig Rodrigues 			printf("exec: %s", execname);
578c195c7f6SCraig Rodrigues 		else
579c195c7f6SCraig Rodrigues 			printf("mount -t %s", vfstype);
580a86de995SDavid E. O'Brien 		for (i = 1; i < mnt_argv.c; i++)
581a86de995SDavid E. O'Brien 			(void)printf(" %s", mnt_argv.a[i]);
5828fae3551SRodney W. Grimes 		(void)printf("\n");
5838fae3551SRodney W. Grimes 		return (0);
5848fae3551SRodney W. Grimes 	}
5858fae3551SRodney W. Grimes 
586b1e6b712SCraig Rodrigues 	if (use_mountprog(vfstype)) {
587a86de995SDavid E. O'Brien 		ret = exec_mountprog(name, execname, mnt_argv.a);
5886f5f1a6bSCraig Rodrigues 	} else {
589a86de995SDavid E. O'Brien 		ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
5906f5f1a6bSCraig Rodrigues 	}
5916f5f1a6bSCraig Rodrigues 
5928fae3551SRodney W. Grimes 	free(optbuf);
5938fae3551SRodney W. Grimes 
5948fae3551SRodney W. Grimes 	if (verbose) {
5958fae3551SRodney W. Grimes 		if (statfs(name, &sf) < 0) {
596c06fe0a0SPeter Wemm 			warn("statfs %s", name);
5978fae3551SRodney W. Grimes 			return (1);
5988fae3551SRodney W. Grimes 		}
599a257a45eSJordan K. Hubbard 		if (fstab_style)
600a257a45eSJordan K. Hubbard 			putfsent(&sf);
601a257a45eSJordan K. Hubbard 		else
602c06fe0a0SPeter Wemm 			prmount(&sf);
6038fae3551SRodney W. Grimes 	}
6048fae3551SRodney W. Grimes 
605bbe9d7daSMatteo Riondato 	return (ret);
6068fae3551SRodney W. Grimes }
6078fae3551SRodney W. Grimes 
6088fae3551SRodney W. Grimes void
609e24dc56aSCraig Rodrigues prmount(struct statfs *sfp)
6108fae3551SRodney W. Grimes {
611aedf10acSCraig Rodrigues 	int flags;
612aedf10acSCraig Rodrigues 	unsigned int i;
6138fae3551SRodney W. Grimes 	struct opt *o;
614c06fe0a0SPeter Wemm 	struct passwd *pw;
6158fae3551SRodney W. Grimes 
616af2ea5aaSNick Hibma 	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
617af2ea5aaSNick Hibma 	    sfp->f_fstypename);
6188fae3551SRodney W. Grimes 
619c06fe0a0SPeter Wemm 	flags = sfp->f_flags & MNT_VISFLAGMASK;
620af2ea5aaSNick Hibma 	for (o = optnames; flags && o->o_opt; o++)
6218fae3551SRodney W. Grimes 		if (flags & o->o_opt) {
622af2ea5aaSNick Hibma 			(void)printf(", %s", o->o_name);
6238fae3551SRodney W. Grimes 			flags &= ~o->o_opt;
6248fae3551SRodney W. Grimes 		}
625dc9c6194SPawel Jakub Dawidek 	/*
626dc9c6194SPawel Jakub Dawidek 	 * Inform when file system is mounted by an unprivileged user
627dc9c6194SPawel Jakub Dawidek 	 * or privileged non-root user.
628dc9c6194SPawel Jakub Dawidek 	 */
629ddb842ccSJacques Vidrine 	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
630af2ea5aaSNick Hibma 		(void)printf(", mounted by ");
631c06fe0a0SPeter Wemm 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
632c06fe0a0SPeter Wemm 			(void)printf("%s", pw->pw_name);
633c06fe0a0SPeter Wemm 		else
634c06fe0a0SPeter Wemm 			(void)printf("%d", sfp->f_owner);
635c06fe0a0SPeter Wemm 	}
636c62ffab6SSheldon Hearn 	if (verbose) {
637677b9b3fSBruce Evans 		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
63896c65ccbSIan Dowse 			(void)printf(", writes: sync %ju async %ju",
63996c65ccbSIan Dowse 			    (uintmax_t)sfp->f_syncwrites,
64096c65ccbSIan Dowse 			    (uintmax_t)sfp->f_asyncwrites);
641461bb71eSKirk McKusick 		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
64296c65ccbSIan Dowse 			(void)printf(", reads: sync %ju async %ju",
64396c65ccbSIan Dowse 			    (uintmax_t)sfp->f_syncreads,
64496c65ccbSIan Dowse 			    (uintmax_t)sfp->f_asyncreads);
64505779418SIan Dowse 		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
64638f102c2SIan Dowse 			printf(", fsid ");
64738f102c2SIan Dowse 			for (i = 0; i < sizeof(sfp->f_fsid); i++)
64838f102c2SIan Dowse 				printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
649c62ffab6SSheldon Hearn 		}
65005779418SIan Dowse 	}
651af2ea5aaSNick Hibma 	(void)printf(")\n");
6528fae3551SRodney W. Grimes }
6538fae3551SRodney W. Grimes 
6548fae3551SRodney W. Grimes struct statfs *
655e24dc56aSCraig Rodrigues getmntpt(const char *name)
6568fae3551SRodney W. Grimes {
6578fae3551SRodney W. Grimes 	struct statfs *mntbuf;
6588fae3551SRodney W. Grimes 	int i, mntsize;
6598fae3551SRodney W. Grimes 
6608fae3551SRodney W. Grimes 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
661cf96af72SBrian Feldman 	for (i = mntsize - 1; i >= 0; i--) {
6628fae3551SRodney W. Grimes 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
6638fae3551SRodney W. Grimes 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
6648fae3551SRodney W. Grimes 			return (&mntbuf[i]);
665cf96af72SBrian Feldman 	}
6668fae3551SRodney W. Grimes 	return (NULL);
6678fae3551SRodney W. Grimes }
6688fae3551SRodney W. Grimes 
6698fae3551SRodney W. Grimes char *
670e24dc56aSCraig Rodrigues catopt(char *s0, const char *s1)
6718fae3551SRodney W. Grimes {
6728fae3551SRodney W. Grimes 	size_t i;
6738fae3551SRodney W. Grimes 	char *cp;
6748fae3551SRodney W. Grimes 
67518af6044SJoseph Koshy 	if (s1 == NULL || *s1 == '\0')
6764796c6ccSJuli Mallett 		return (s0);
67718af6044SJoseph Koshy 
6788fae3551SRodney W. Grimes 	if (s0 && *s0) {
6798fae3551SRodney W. Grimes 		i = strlen(s0) + strlen(s1) + 1 + 1;
6808fae3551SRodney W. Grimes 		if ((cp = malloc(i)) == NULL)
681bcb1d846SPhilippe Charnier 			errx(1, "malloc failed");
6828fae3551SRodney W. Grimes 		(void)snprintf(cp, i, "%s,%s", s0, s1);
6838fae3551SRodney W. Grimes 	} else
6848fae3551SRodney W. Grimes 		cp = strdup(s1);
6858fae3551SRodney W. Grimes 
6868fae3551SRodney W. Grimes 	if (s0)
6878fae3551SRodney W. Grimes 		free(s0);
6888fae3551SRodney W. Grimes 	return (cp);
6898fae3551SRodney W. Grimes }
6908fae3551SRodney W. Grimes 
6918fae3551SRodney W. Grimes void
692a86de995SDavid E. O'Brien mangle(char *options, struct cpa *a)
6938fae3551SRodney W. Grimes {
694c7835769SCraig Rodrigues 	char *p, *s, *val;
6958fae3551SRodney W. Grimes 
6968fae3551SRodney W. Grimes 	for (s = options; (p = strsep(&s, ",")) != NULL;)
69718af6044SJoseph Koshy 		if (*p != '\0') {
698d9fa6ce7SCraig Rodrigues 			if (strcmp(p, "noauto") == 0) {
699d9fa6ce7SCraig Rodrigues 				/*
700d9fa6ce7SCraig Rodrigues 				 * Do not pass noauto option to nmount().
701d9fa6ce7SCraig Rodrigues 				 * or external mount program.  noauto is
702d9fa6ce7SCraig Rodrigues 				 * only used to prevent mounting a filesystem
703d9fa6ce7SCraig Rodrigues 				 * when 'mount -a' is specified, and is
704d9fa6ce7SCraig Rodrigues 				 * not a real mount option.
705d9fa6ce7SCraig Rodrigues 				 */
706d9fa6ce7SCraig Rodrigues 				continue;
7074b4f9170SDag-Erling Smørgrav 			} else if (strcmp(p, "late") == 0) {
7084b4f9170SDag-Erling Smørgrav 				/*
7094b4f9170SDag-Erling Smørgrav 				 * "late" is used to prevent certain file
7104b4f9170SDag-Erling Smørgrav 				 * systems from being mounted before late
7114b4f9170SDag-Erling Smørgrav 				 * in the boot cycle; for instance,
7124b4f9170SDag-Erling Smørgrav 				 * loopback NFS mounts can't be mounted
7134b4f9170SDag-Erling Smørgrav 				 * before mountd starts.
7144b4f9170SDag-Erling Smørgrav 				 */
7154b4f9170SDag-Erling Smørgrav 				continue;
716c7835769SCraig Rodrigues 			} else if (strncmp(p, "mountprog", 9) == 0) {
717c7835769SCraig Rodrigues 				/*
718c7835769SCraig Rodrigues 				 * "mountprog" is used to force the use of
719c7835769SCraig Rodrigues 				 * userland mount programs.
720c7835769SCraig Rodrigues 				 */
721c7835769SCraig Rodrigues 				val = strchr(p, '=');
722c7835769SCraig Rodrigues                         	if (val != NULL) {
723c7835769SCraig Rodrigues                                 	++val;
724c7835769SCraig Rodrigues 					if (*val != '\0')
725c7835769SCraig Rodrigues 						mountprog = strdup(val);
726c7835769SCraig Rodrigues 				}
727c7835769SCraig Rodrigues 
728c7835769SCraig Rodrigues 				if (mountprog == NULL) {
729c7835769SCraig Rodrigues 					errx(1, "Need value for -o mountprog");
730c7835769SCraig Rodrigues 				}
731c7835769SCraig Rodrigues 				continue;
73235d6c7f5SCraig Rodrigues 			} else if (strcmp(p, "userquota") == 0) {
73335d6c7f5SCraig Rodrigues 				continue;
7346e74fb9dSMaxim Konovalov 			} else if (strncmp(p, userquotaeq,
7356e74fb9dSMaxim Konovalov 			    sizeof(userquotaeq) - 1) == 0) {
7366e74fb9dSMaxim Konovalov 				continue;
73735d6c7f5SCraig Rodrigues 			} else if (strcmp(p, "groupquota") == 0) {
73835d6c7f5SCraig Rodrigues 				continue;
7396e74fb9dSMaxim Konovalov 			} else if (strncmp(p, groupquotaeq,
7406e74fb9dSMaxim Konovalov 			    sizeof(groupquotaeq) - 1) == 0) {
7416e74fb9dSMaxim Konovalov 				continue;
742d9fa6ce7SCraig Rodrigues 			} else if (*p == '-') {
743a86de995SDavid E. O'Brien 				append_arg(a, p);
7448fae3551SRodney W. Grimes 				p = strchr(p, '=');
745adfdbe22STom Rhodes 				if (p != NULL) {
7468fae3551SRodney W. Grimes 					*p = '\0';
747a86de995SDavid E. O'Brien 					append_arg(a, p + 1);
7488fae3551SRodney W. Grimes 				}
749748e259bSCraig Rodrigues 			} else {
750a86de995SDavid E. O'Brien 				append_arg(a, strdup("-o"));
751a86de995SDavid E. O'Brien 				append_arg(a, p);
7528fae3551SRodney W. Grimes 			}
75318af6044SJoseph Koshy 		}
7548fae3551SRodney W. Grimes }
7558fae3551SRodney W. Grimes 
75618af6044SJoseph Koshy 
75718af6044SJoseph Koshy char *
7584796c6ccSJuli Mallett update_options(char *opts, char *fstab, int curflags)
75918af6044SJoseph Koshy {
76018af6044SJoseph Koshy 	char *o, *p;
76118af6044SJoseph Koshy 	char *cur;
76218af6044SJoseph Koshy 	char *expopt, *newopt, *tmpopt;
76318af6044SJoseph Koshy 
76418af6044SJoseph Koshy 	if (opts == NULL)
7654796c6ccSJuli Mallett 		return (strdup(""));
76618af6044SJoseph Koshy 
76718af6044SJoseph Koshy 	/* remove meta options from list */
76818af6044SJoseph Koshy 	remopt(fstab, MOUNT_META_OPTION_FSTAB);
76918af6044SJoseph Koshy 	remopt(fstab, MOUNT_META_OPTION_CURRENT);
77018af6044SJoseph Koshy 	cur = flags2opts(curflags);
77118af6044SJoseph Koshy 
77218af6044SJoseph Koshy 	/*
77318af6044SJoseph Koshy 	 * Expand all meta-options passed to us first.
77418af6044SJoseph Koshy 	 */
77518af6044SJoseph Koshy 	expopt = NULL;
77618af6044SJoseph Koshy 	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
77718af6044SJoseph Koshy 		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
77818af6044SJoseph Koshy 			expopt = catopt(expopt, fstab);
77918af6044SJoseph Koshy 		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
78018af6044SJoseph Koshy 			expopt = catopt(expopt, cur);
78118af6044SJoseph Koshy 		else
78218af6044SJoseph Koshy 			expopt = catopt(expopt, o);
78318af6044SJoseph Koshy 	}
78418af6044SJoseph Koshy 	free(cur);
78518af6044SJoseph Koshy 	free(opts);
78618af6044SJoseph Koshy 
78718af6044SJoseph Koshy 	/*
78818af6044SJoseph Koshy 	 * Remove previous contradictory arguments. Given option "foo" we
78918af6044SJoseph Koshy 	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
79018af6044SJoseph Koshy 	 * and "foo" - so we can deal with possible options like "notice".
79118af6044SJoseph Koshy 	 */
79218af6044SJoseph Koshy 	newopt = NULL;
79318af6044SJoseph Koshy 	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
79418af6044SJoseph Koshy 		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
79518af6044SJoseph Koshy 			errx(1, "malloc failed");
79618af6044SJoseph Koshy 
79718af6044SJoseph Koshy 		strcpy(tmpopt, "no");
79818af6044SJoseph Koshy 		strcat(tmpopt, o);
79918af6044SJoseph Koshy 		remopt(newopt, tmpopt);
80018af6044SJoseph Koshy 		free(tmpopt);
80118af6044SJoseph Koshy 
80218af6044SJoseph Koshy 		if (strncmp("no", o, 2) == 0)
80318af6044SJoseph Koshy 			remopt(newopt, o+2);
80418af6044SJoseph Koshy 
80518af6044SJoseph Koshy 		newopt = catopt(newopt, o);
80618af6044SJoseph Koshy 	}
80718af6044SJoseph Koshy 	free(expopt);
80818af6044SJoseph Koshy 
8094796c6ccSJuli Mallett 	return (newopt);
81018af6044SJoseph Koshy }
81118af6044SJoseph Koshy 
81218af6044SJoseph Koshy void
8134796c6ccSJuli Mallett remopt(char *string, const char *opt)
81418af6044SJoseph Koshy {
81518af6044SJoseph Koshy 	char *o, *p, *r;
81618af6044SJoseph Koshy 
81718af6044SJoseph Koshy 	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
81818af6044SJoseph Koshy 		return;
81918af6044SJoseph Koshy 
82018af6044SJoseph Koshy 	r = string;
82118af6044SJoseph Koshy 
82218af6044SJoseph Koshy 	for (p = string; (o = strsep(&p, ",")) != NULL;) {
82318af6044SJoseph Koshy 		if (strcmp(opt, o) != 0) {
82418af6044SJoseph Koshy 			if (*r == ',' && *o != '\0')
82518af6044SJoseph Koshy 				r++;
82618af6044SJoseph Koshy 			while ((*r++ = *o++) != '\0')
82718af6044SJoseph Koshy 			    ;
82818af6044SJoseph Koshy 			*--r = ',';
82918af6044SJoseph Koshy 		}
83018af6044SJoseph Koshy 	}
83118af6044SJoseph Koshy 	*r = '\0';
83218af6044SJoseph Koshy }
83318af6044SJoseph Koshy 
8348fae3551SRodney W. Grimes void
8354796c6ccSJuli Mallett usage(void)
8368fae3551SRodney W. Grimes {
8378fae3551SRodney W. Grimes 
838bcb1d846SPhilippe Charnier 	(void)fprintf(stderr, "%s\n%s\n%s\n",
8394b4f9170SDag-Erling Smørgrav "usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
8408d646af5SRuslan Ermilov "       mount [-dfpruvw] special | node",
8418d646af5SRuslan Ermilov "       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
8428fae3551SRodney W. Grimes 	exit(1);
8438fae3551SRodney W. Grimes }
844a257a45eSJordan K. Hubbard 
845a257a45eSJordan K. Hubbard void
846031ea52fSMatteo Riondato putfsent(struct statfs *ent)
847a257a45eSJordan K. Hubbard {
848a257a45eSJordan K. Hubbard 	struct fstab *fst;
84918af6044SJoseph Koshy 	char *opts;
850306d73d6SPoul-Henning Kamp 	int l;
851a257a45eSJordan K. Hubbard 
85218af6044SJoseph Koshy 	opts = flags2opts(ent->f_flags);
8533ae7ea68SGiorgos Keramidas 
854031ea52fSMatteo Riondato 	if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
855031ea52fSMatteo Riondato 	    strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
856031ea52fSMatteo Riondato 		strcpy(ent->f_mntfromname, (strnstr(ent->f_mntfromname, ":", 8)
857031ea52fSMatteo Riondato 		    +1));
858031ea52fSMatteo Riondato 	}
859031ea52fSMatteo Riondato 
8603ae7ea68SGiorgos Keramidas 	/*
8613ae7ea68SGiorgos Keramidas 	 * "rw" is not a real mount option; this is why we print NULL as "rw"
8623ae7ea68SGiorgos Keramidas 	 * if opts is still NULL here.
8633ae7ea68SGiorgos Keramidas 	 */
864306d73d6SPoul-Henning Kamp 	l = strlen(ent->f_mntfromname);
865306d73d6SPoul-Henning Kamp 	printf("%s%s%s%s", ent->f_mntfromname,
866306d73d6SPoul-Henning Kamp 	    l < 8 ? "\t" : "",
867306d73d6SPoul-Henning Kamp 	    l < 16 ? "\t" : "",
868306d73d6SPoul-Henning Kamp 	    l < 24 ? "\t" : " ");
869306d73d6SPoul-Henning Kamp 	l = strlen(ent->f_mntonname);
870306d73d6SPoul-Henning Kamp 	printf("%s%s%s%s", ent->f_mntonname,
871306d73d6SPoul-Henning Kamp 	    l < 8 ? "\t" : "",
872306d73d6SPoul-Henning Kamp 	    l < 16 ? "\t" : "",
873306d73d6SPoul-Henning Kamp 	    l < 24 ? "\t" : " ");
874306d73d6SPoul-Henning Kamp 	printf("%s\t", ent->f_fstypename);
875306d73d6SPoul-Henning Kamp 	if (opts == NULL) {
876306d73d6SPoul-Henning Kamp 		printf("%s\t", "rw");
877306d73d6SPoul-Henning Kamp 	} else {
878306d73d6SPoul-Henning Kamp 		l = strlen(opts);
879306d73d6SPoul-Henning Kamp 		printf("%s%s", opts,
880306d73d6SPoul-Henning Kamp 		    l < 8 ? "\t" : " ");
881306d73d6SPoul-Henning Kamp 	}
88218af6044SJoseph Koshy 	free(opts);
883c06fe0a0SPeter Wemm 
884fba1c154SSteve Price 	if ((fst = getfsspec(ent->f_mntfromname)))
885a257a45eSJordan K. Hubbard 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
886fba1c154SSteve Price 	else if ((fst = getfsfile(ent->f_mntonname)))
887a257a45eSJordan K. Hubbard 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
888ab80d6faSBrian Feldman 	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
889ab80d6faSBrian Feldman 		if (strcmp(ent->f_mntonname, "/") == 0)
890a257a45eSJordan K. Hubbard 			printf("\t1 1\n");
891a257a45eSJordan K. Hubbard 		else
892ab80d6faSBrian Feldman 			printf("\t2 2\n");
893ab80d6faSBrian Feldman 	} else
894a257a45eSJordan K. Hubbard 		printf("\t0 0\n");
895a257a45eSJordan K. Hubbard }
89618af6044SJoseph Koshy 
89718af6044SJoseph Koshy 
89818af6044SJoseph Koshy char *
8994796c6ccSJuli Mallett flags2opts(int flags)
90018af6044SJoseph Koshy {
90118af6044SJoseph Koshy 	char *res;
90218af6044SJoseph Koshy 
90318af6044SJoseph Koshy 	res = NULL;
90418af6044SJoseph Koshy 
90588e2c335SCraig Rodrigues 	if (flags & MNT_RDONLY)		res = catopt(res, "ro");
90618af6044SJoseph Koshy 	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
90718af6044SJoseph Koshy 	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
90818af6044SJoseph Koshy 	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
90918af6044SJoseph Koshy 	if (flags & MNT_UNION)		res = catopt(res, "union");
91018af6044SJoseph Koshy 	if (flags & MNT_ASYNC)		res = catopt(res, "async");
91118af6044SJoseph Koshy 	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
91218af6044SJoseph Koshy 	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
91318af6044SJoseph Koshy 	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
91418af6044SJoseph Koshy 	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
91518af6044SJoseph Koshy 	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
916ba0fbe96SRobert Watson 	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
91703d94b50SRobert Watson 	if (flags & MNT_ACLS)		res = catopt(res, "acls");
91818af6044SJoseph Koshy 
9194796c6ccSJuli Mallett 	return (res);
92018af6044SJoseph Koshy }
921