xref: /freebsd/sbin/mount/mount.c (revision 4796c6cc6ec51c166b9df368cc60d78e861abb7b)
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";
348fae3551SRodney W. Grimes #endif /* not lint */
358fae3551SRodney W. Grimes 
368fae3551SRodney W. Grimes #ifndef lint
37fba1c154SSteve Price #if 0
38c06fe0a0SPeter Wemm static char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
39fba1c154SSteve Price #endif
40bcb1d846SPhilippe Charnier static const char rcsid[] =
417f3dea24SPeter Wemm   "$FreeBSD$";
428fae3551SRodney W. Grimes #endif /* not lint */
438fae3551SRodney W. Grimes 
448fae3551SRodney W. Grimes #include <sys/param.h>
458fae3551SRodney W. Grimes #include <sys/mount.h>
468a978495SDavid Greenman #include <sys/stat.h>
4774cf460bSBruce Evans #include <sys/wait.h>
488fae3551SRodney W. Grimes 
49003dbca6SMaxime Henrion #include <ctype.h>
508fae3551SRodney W. Grimes #include <err.h>
518fae3551SRodney W. Grimes #include <errno.h>
528fae3551SRodney W. Grimes #include <fstab.h>
53a3ba4c65SGordon Tetlow #include <paths.h>
54c06fe0a0SPeter Wemm #include <pwd.h>
558fae3551SRodney W. Grimes #include <signal.h>
5696c65ccbSIan Dowse #include <stdint.h>
578fae3551SRodney W. Grimes #include <stdio.h>
588fae3551SRodney W. Grimes #include <stdlib.h>
598fae3551SRodney W. Grimes #include <string.h>
608fae3551SRodney W. Grimes #include <unistd.h>
618fae3551SRodney W. Grimes 
62fba1c154SSteve Price #include "extern.h"
6373dd3167SPoul-Henning Kamp #include "mntopts.h"
648fae3551SRodney W. Grimes #include "pathnames.h"
658fae3551SRodney W. Grimes 
6618af6044SJoseph Koshy /* `meta' options */
6718af6044SJoseph Koshy #define MOUNT_META_OPTION_FSTAB		"fstab"
6818af6044SJoseph Koshy #define MOUNT_META_OPTION_CURRENT	"current"
6918af6044SJoseph Koshy 
7074cf460bSBruce Evans int debug, fstab_style, verbose;
71a257a45eSJordan K. Hubbard 
7285429990SWarner Losh char   *catopt(char *, const char *);
7385429990SWarner Losh struct statfs *getmntpt(const char *);
7485429990SWarner Losh int	hasopt(const char *, const char *);
7585429990SWarner Losh int	ismounted(struct fstab *, struct statfs *, int);
7685429990SWarner Losh int	isremountable(const char *);
774796c6ccSJuli Mallett void	mangle(char *, int *, char *[]);
7885429990SWarner Losh char   *update_options(char *, char *, int);
7985429990SWarner Losh int	mountfs(const char *, const char *, const char *,
8085429990SWarner Losh 			int, const char *, const char *);
8185429990SWarner Losh void	remopt(char *, const char *);
8285429990SWarner Losh void	prmount(struct statfs *);
8385429990SWarner Losh void	putfsent(const struct statfs *);
8485429990SWarner Losh void	usage(void);
8585429990SWarner Losh char   *flags2opts(int);
868fae3551SRodney W. Grimes 
87bcb1d846SPhilippe Charnier /* Map from mount options to printable formats. */
888fae3551SRodney W. Grimes static struct opt {
898fae3551SRodney W. Grimes 	int o_opt;
908fae3551SRodney W. Grimes 	const char *o_name;
918fae3551SRodney W. Grimes } optnames[] = {
928fae3551SRodney W. Grimes 	{ MNT_ASYNC,		"asynchronous" },
938fae3551SRodney W. Grimes 	{ MNT_EXPORTED,		"NFS exported" },
948fae3551SRodney W. Grimes 	{ MNT_LOCAL,		"local" },
9555e50aceSDavid Greenman 	{ MNT_NOATIME,		"noatime" },
968fae3551SRodney W. Grimes 	{ MNT_NOEXEC,		"noexec" },
978fae3551SRodney W. Grimes 	{ MNT_NOSUID,		"nosuid" },
985ddc8dedSWolfram Schneider 	{ MNT_NOSYMFOLLOW,	"nosymfollow" },
998fae3551SRodney W. Grimes 	{ MNT_QUOTA,		"with quotas" },
1008fae3551SRodney W. Grimes 	{ MNT_RDONLY,		"read-only" },
1018fae3551SRodney W. Grimes 	{ MNT_SYNCHRONOUS,	"synchronous" },
1028fae3551SRodney W. Grimes 	{ MNT_UNION,		"union" },
10375b714acSKATO Takenori 	{ MNT_NOCLUSTERR,	"noclusterr" },
10475b714acSKATO Takenori 	{ MNT_NOCLUSTERW,	"noclusterw" },
10552bf64c7SJulian Elischer 	{ MNT_SUIDDIR,		"suiddir" },
106b1897c19SJulian Elischer 	{ MNT_SOFTDEP,		"soft-updates" },
107ba0fbe96SRobert Watson 	{ MNT_MULTILABEL,	"multilabel" },
10803d94b50SRobert Watson 	{ MNT_ACLS,		"acls" },
10918af6044SJoseph Koshy 	{ 0, NULL }
1108fae3551SRodney W. Grimes };
1118fae3551SRodney W. Grimes 
112fba1c154SSteve Price /*
113fba1c154SSteve Price  * List of VFS types that can be remounted without becoming mounted on top
114fba1c154SSteve Price  * of each other.
115fba1c154SSteve Price  * XXX Is this list correct?
116fba1c154SSteve Price  */
117fba1c154SSteve Price static const char *
118fba1c154SSteve Price remountable_fs_names[] = {
1195a4420e3SAlexey Zelkin 	"ufs", "ffs", "ext2fs",
120fba1c154SSteve Price 	0
121fba1c154SSteve Price };
122fba1c154SSteve Price 
1236e74fb9dSMaxim Konovalov static const char userquotaeq[] = "userquota=";
1246e74fb9dSMaxim Konovalov static const char groupquotaeq[] = "groupquota=";
1256e74fb9dSMaxim Konovalov 
1266f5f1a6bSCraig Rodrigues static int
1276f5f1a6bSCraig Rodrigues use_mountprog(const char *vfstype)
1286f5f1a6bSCraig Rodrigues {
1296f5f1a6bSCraig Rodrigues 	/* XXX: We need to get away from implementing external mount
1306f5f1a6bSCraig Rodrigues 	 *      programs for every filesystem, and move towards having
1316f5f1a6bSCraig Rodrigues 	 *	each filesystem properly implement the nmount() system call.
1326f5f1a6bSCraig Rodrigues 	 */
1336f5f1a6bSCraig Rodrigues 	unsigned int i;
1346f5f1a6bSCraig Rodrigues 	const char *fs[] = {
1356973ce04STai-hwa Liang 	"cd9660", "mfs", "msdosfs", "nfs", "nfs4", "ntfs",
136cf9e56b0SCraig Rodrigues 	"nwfs", "nullfs", "portalfs", "smbfs", "udf", "umapfs",
1376f5f1a6bSCraig Rodrigues 	"unionfs",
1386f5f1a6bSCraig Rodrigues 	NULL
1396f5f1a6bSCraig Rodrigues 	};
1406f5f1a6bSCraig Rodrigues 
1416f5f1a6bSCraig Rodrigues 	for (i = 0; fs[i] != NULL; ++i) {
1426f5f1a6bSCraig Rodrigues 		if (strcmp(vfstype, fs[i]) == 0)
1434796c6ccSJuli Mallett 			return (1);
1446f5f1a6bSCraig Rodrigues 	}
1456f5f1a6bSCraig Rodrigues 
1464796c6ccSJuli Mallett 	return (0);
1476f5f1a6bSCraig Rodrigues }
1486f5f1a6bSCraig Rodrigues 
1496f5f1a6bSCraig Rodrigues static int
1504796c6ccSJuli Mallett exec_mountprog(const char *name, const char *execname, char *const argv[])
1516f5f1a6bSCraig Rodrigues {
1526f5f1a6bSCraig Rodrigues 	pid_t pid;
1536f5f1a6bSCraig Rodrigues 	int status;
1546f5f1a6bSCraig Rodrigues 
1556f5f1a6bSCraig Rodrigues 	switch (pid = fork()) {
1566f5f1a6bSCraig Rodrigues 	case -1:				/* Error. */
1576f5f1a6bSCraig Rodrigues 		warn("fork");
1586f5f1a6bSCraig Rodrigues 		exit (1);
1596f5f1a6bSCraig Rodrigues 	case 0:					/* Child. */
1606f5f1a6bSCraig Rodrigues 		/* Go find an executable. */
1616f5f1a6bSCraig Rodrigues 		execvP(execname, _PATH_SYSPATH, argv);
1626f5f1a6bSCraig Rodrigues 		if (errno == ENOENT) {
1636f5f1a6bSCraig Rodrigues 			warn("exec %s not found in %s", execname,
1646f5f1a6bSCraig Rodrigues 			    _PATH_SYSPATH);
1656f5f1a6bSCraig Rodrigues 		}
1666f5f1a6bSCraig Rodrigues 		exit(1);
1676f5f1a6bSCraig Rodrigues 	default:				/* Parent. */
1686f5f1a6bSCraig Rodrigues 		if (waitpid(pid, &status, 0) < 0) {
1696f5f1a6bSCraig Rodrigues 			warn("waitpid");
1706f5f1a6bSCraig Rodrigues 			return (1);
1716f5f1a6bSCraig Rodrigues 		}
1726f5f1a6bSCraig Rodrigues 
1736f5f1a6bSCraig Rodrigues 		if (WIFEXITED(status)) {
1746f5f1a6bSCraig Rodrigues 			if (WEXITSTATUS(status) != 0)
1756f5f1a6bSCraig Rodrigues 				return (WEXITSTATUS(status));
1766f5f1a6bSCraig Rodrigues 		} else if (WIFSIGNALED(status)) {
1776f5f1a6bSCraig Rodrigues 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
1786f5f1a6bSCraig Rodrigues 			return (1);
1796f5f1a6bSCraig Rodrigues 		}
1806f5f1a6bSCraig Rodrigues 		break;
1816f5f1a6bSCraig Rodrigues 	}
1826f5f1a6bSCraig Rodrigues 
1836f5f1a6bSCraig Rodrigues 	return (0);
1846f5f1a6bSCraig Rodrigues }
1856f5f1a6bSCraig Rodrigues 
1868fae3551SRodney W. Grimes int
187e24dc56aSCraig Rodrigues main(int argc, char *argv[])
1888fae3551SRodney W. Grimes {
189c06fe0a0SPeter Wemm 	const char *mntfromname, **vfslist, *vfstype;
1908fae3551SRodney W. Grimes 	struct fstab *fs;
1918fae3551SRodney W. Grimes 	struct statfs *mntbuf;
1928fae3551SRodney W. Grimes 	FILE *mountdfp;
1938fae3551SRodney W. Grimes 	pid_t pid;
1945256cc21SJuli Mallett 	int all, ch, i, init_flags, mntsize, rval, have_fstab;
195003dbca6SMaxime Henrion 	char *cp, *ep, *options;
1968fae3551SRodney W. Grimes 
1975256cc21SJuli Mallett 	options = strdup("noro");
1985256cc21SJuli Mallett 	if (options == NULL)
1995256cc21SJuli Mallett 		errx(1, "malloc failed");
2005256cc21SJuli Mallett 
2018fae3551SRodney W. Grimes 	all = init_flags = 0;
2028fae3551SRodney W. Grimes 	vfslist = NULL;
2038fae3551SRodney W. Grimes 	vfstype = "ufs";
204ef258dd9SMatthew N. Dodd 	while ((ch = getopt(argc, argv, "adF:fo:prwt:uv")) != -1)
2058fae3551SRodney W. Grimes 		switch (ch) {
2068fae3551SRodney W. Grimes 		case 'a':
2078fae3551SRodney W. Grimes 			all = 1;
2088fae3551SRodney W. Grimes 			break;
2098fae3551SRodney W. Grimes 		case 'd':
2108fae3551SRodney W. Grimes 			debug = 1;
2118fae3551SRodney W. Grimes 			break;
212ef258dd9SMatthew N. Dodd 		case 'F':
213ef258dd9SMatthew N. Dodd 			setfstab(optarg);
214ef258dd9SMatthew N. Dodd 			break;
2158fae3551SRodney W. Grimes 		case 'f':
2168fae3551SRodney W. Grimes 			init_flags |= MNT_FORCE;
2178fae3551SRodney W. Grimes 			break;
2188fae3551SRodney W. Grimes 		case 'o':
2198fae3551SRodney W. Grimes 			options = catopt(options, optarg);
2208fae3551SRodney W. Grimes 			break;
22174cf460bSBruce Evans 		case 'p':
22274cf460bSBruce Evans 			fstab_style = 1;
22374cf460bSBruce Evans 			verbose = 1;
22474cf460bSBruce Evans 			break;
2258fae3551SRodney W. Grimes 		case 'r':
226ad044771SDima Dorfman 			options = catopt(options, "ro");
2278fae3551SRodney W. Grimes 			break;
2288fae3551SRodney W. Grimes 		case 't':
2298fae3551SRodney W. Grimes 			if (vfslist != NULL)
230bcb1d846SPhilippe Charnier 				errx(1, "only one -t option may be specified");
2318fae3551SRodney W. Grimes 			vfslist = makevfslist(optarg);
2328fae3551SRodney W. Grimes 			vfstype = optarg;
2338fae3551SRodney W. Grimes 			break;
2348fae3551SRodney W. Grimes 		case 'u':
2358fae3551SRodney W. Grimes 			init_flags |= MNT_UPDATE;
2368fae3551SRodney W. Grimes 			break;
2378fae3551SRodney W. Grimes 		case 'v':
2388fae3551SRodney W. Grimes 			verbose = 1;
2398fae3551SRodney W. Grimes 			break;
2408fae3551SRodney W. Grimes 		case 'w':
241ad044771SDima Dorfman 			options = catopt(options, "noro");
2428fae3551SRodney W. Grimes 			break;
2438fae3551SRodney W. Grimes 		case '?':
2448fae3551SRodney W. Grimes 		default:
2458fae3551SRodney W. Grimes 			usage();
2468fae3551SRodney W. Grimes 			/* NOTREACHED */
2478fae3551SRodney W. Grimes 		}
2488fae3551SRodney W. Grimes 	argc -= optind;
2498fae3551SRodney W. Grimes 	argv += optind;
2508fae3551SRodney W. Grimes 
2518fae3551SRodney W. Grimes #define	BADTYPE(type)							\
2528fae3551SRodney W. Grimes 	(strcmp(type, FSTAB_RO) &&					\
2538fae3551SRodney W. Grimes 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
2548fae3551SRodney W. Grimes 
2558fae3551SRodney W. Grimes 	rval = 0;
2568fae3551SRodney W. Grimes 	switch (argc) {
2578fae3551SRodney W. Grimes 	case 0:
258fba1c154SSteve Price 		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
259fba1c154SSteve Price 			err(1, "getmntinfo");
260fba1c154SSteve Price 		if (all) {
2618fae3551SRodney W. Grimes 			while ((fs = getfsent()) != NULL) {
2628fae3551SRodney W. Grimes 				if (BADTYPE(fs->fs_type))
2638fae3551SRodney W. Grimes 					continue;
264c06fe0a0SPeter Wemm 				if (checkvfsname(fs->fs_vfstype, vfslist))
2658fae3551SRodney W. Grimes 					continue;
266c06fe0a0SPeter Wemm 				if (hasopt(fs->fs_mntops, "noauto"))
26789beb278SDavid Greenman 					continue;
26898201b0cSBruce Evans 				if (!(init_flags & MNT_UPDATE) &&
26998201b0cSBruce Evans 				    ismounted(fs, mntbuf, mntsize))
270fba1c154SSteve Price 					continue;
27113cbdf24SGuido van Rooij 				options = update_options(options, fs->fs_mntops,
27213cbdf24SGuido van Rooij 				    mntbuf->f_flags);
2738fae3551SRodney W. Grimes 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
2748fae3551SRodney W. Grimes 				    fs->fs_file, init_flags, options,
2758fae3551SRodney W. Grimes 				    fs->fs_mntops))
2768fae3551SRodney W. Grimes 					rval = 1;
2778fae3551SRodney W. Grimes 			}
278fba1c154SSteve Price 		} else if (fstab_style) {
279a257a45eSJordan K. Hubbard 			for (i = 0; i < mntsize; i++) {
280c06fe0a0SPeter Wemm 				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
281a257a45eSJordan K. Hubbard 					continue;
282a257a45eSJordan K. Hubbard 				putfsent(&mntbuf[i]);
283a257a45eSJordan K. Hubbard 			}
28474cf460bSBruce Evans 		} else {
2858fae3551SRodney W. Grimes 			for (i = 0; i < mntsize; i++) {
28674cf460bSBruce Evans 				if (checkvfsname(mntbuf[i].f_fstypename,
28774cf460bSBruce Evans 				    vfslist))
2888fae3551SRodney W. Grimes 					continue;
289c06fe0a0SPeter Wemm 				prmount(&mntbuf[i]);
2908fae3551SRodney W. Grimes 			}
2918fae3551SRodney W. Grimes 		}
2928fae3551SRodney W. Grimes 		exit(rval);
2938fae3551SRodney W. Grimes 	case 1:
2948fae3551SRodney W. Grimes 		if (vfslist != NULL)
2958fae3551SRodney W. Grimes 			usage();
2968fae3551SRodney W. Grimes 
29701a9bce5SEric Anholt 		rmslashes(*argv, *argv);
2988fae3551SRodney W. Grimes 		if (init_flags & MNT_UPDATE) {
299cf96af72SBrian Feldman 			mntfromname = NULL;
300cf96af72SBrian Feldman 			have_fstab = 0;
3018fae3551SRodney W. Grimes 			if ((mntbuf = getmntpt(*argv)) == NULL)
302cf96af72SBrian Feldman 				errx(1, "not currently mounted %s", *argv);
303cf96af72SBrian Feldman 			/*
304cf96af72SBrian Feldman 			 * Only get the mntflags from fstab if both mntpoint
305cf96af72SBrian Feldman 			 * and mntspec are identical. Also handle the special
306cf96af72SBrian Feldman 			 * case where just '/' is mounted and 'spec' is not
307cf96af72SBrian Feldman 			 * identical with the one from fstab ('/dev' is missing
308cf96af72SBrian Feldman 			 * in the spec-string at boot-time).
309cf96af72SBrian Feldman 			 */
31018af6044SJoseph Koshy 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
311cf96af72SBrian Feldman 				if (strcmp(fs->fs_spec,
312cf96af72SBrian Feldman 				    mntbuf->f_mntfromname) == 0 &&
313cf96af72SBrian Feldman 				    strcmp(fs->fs_file,
314cf96af72SBrian Feldman 				    mntbuf->f_mntonname) == 0) {
315cf96af72SBrian Feldman 					have_fstab = 1;
316cf96af72SBrian Feldman 					mntfromname = mntbuf->f_mntfromname;
317cf96af72SBrian Feldman 				} else if (argv[0][0] == '/' &&
318cf96af72SBrian Feldman 				    argv[0][1] == '\0') {
319cf96af72SBrian Feldman 					fs = getfsfile("/");
320cf96af72SBrian Feldman 					have_fstab = 1;
321c06fe0a0SPeter Wemm 					mntfromname = fs->fs_spec;
322cf96af72SBrian Feldman 				}
323cf96af72SBrian Feldman 			}
324cf96af72SBrian Feldman 			if (have_fstab) {
32518af6044SJoseph Koshy 				options = update_options(options, fs->fs_mntops,
32618af6044SJoseph Koshy 				    mntbuf->f_flags);
32718af6044SJoseph Koshy 			} else {
328c06fe0a0SPeter Wemm 				mntfromname = mntbuf->f_mntfromname;
32918af6044SJoseph Koshy 				options = update_options(options, NULL,
33018af6044SJoseph Koshy 				    mntbuf->f_flags);
33118af6044SJoseph Koshy 			}
332c06fe0a0SPeter Wemm 			rval = mountfs(mntbuf->f_fstypename, mntfromname,
333c06fe0a0SPeter Wemm 			    mntbuf->f_mntonname, init_flags, options, 0);
334c06fe0a0SPeter Wemm 			break;
335c06fe0a0SPeter Wemm 		}
3368fae3551SRodney W. Grimes 		if ((fs = getfsfile(*argv)) == NULL &&
3378fae3551SRodney W. Grimes 		    (fs = getfsspec(*argv)) == NULL)
338bcb1d846SPhilippe Charnier 			errx(1, "%s: unknown special file or file system",
3398fae3551SRodney W. Grimes 			    *argv);
3408fae3551SRodney W. Grimes 		if (BADTYPE(fs->fs_type))
341bcb1d846SPhilippe Charnier 			errx(1, "%s has unknown file system type",
3428fae3551SRodney W. Grimes 			    *argv);
343c06fe0a0SPeter Wemm 		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
344c06fe0a0SPeter Wemm 		    init_flags, options, fs->fs_mntops);
3458fae3551SRodney W. Grimes 		break;
3468fae3551SRodney W. Grimes 	case 2:
3478fae3551SRodney W. Grimes 		/*
348cf96af72SBrian Feldman 		 * If -t flag has not been specified, the path cannot be
349003dbca6SMaxime Henrion 		 * found, spec contains either a ':' or a '@', then assume
350cf96af72SBrian Feldman 		 * that an NFS file system is being specified ala Sun.
351003dbca6SMaxime Henrion 		 * Check if the hostname contains only allowed characters
352003dbca6SMaxime Henrion 		 * to reduce false positives.  IPv6 addresses containing
353003dbca6SMaxime Henrion 		 * ':' will be correctly parsed only if the separator is '@'.
354003dbca6SMaxime Henrion 		 * The definition of a valid hostname is taken from RFC 1034.
3558fae3551SRodney W. Grimes 		 */
35605779418SIan Dowse 		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
35705779418SIan Dowse 		    (ep = strchr(argv[0], ':')) != NULL)) {
358da85c82bSMaxime Henrion 			if (*ep == '@') {
359da85c82bSMaxime Henrion 				cp = ep + 1;
360da85c82bSMaxime Henrion 				ep = cp + strlen(cp);
361da85c82bSMaxime Henrion 			} else
362003dbca6SMaxime Henrion 				cp = argv[0];
363003dbca6SMaxime Henrion 			while (cp != ep) {
364003dbca6SMaxime Henrion 				if (!isdigit(*cp) && !isalpha(*cp) &&
365003dbca6SMaxime Henrion 				    *cp != '.' && *cp != '-' && *cp != ':')
366003dbca6SMaxime Henrion 					break;
367003dbca6SMaxime Henrion 				cp++;
368003dbca6SMaxime Henrion 			}
369003dbca6SMaxime Henrion 			if (cp == ep)
3708fae3551SRodney W. Grimes 				vfstype = "nfs";
371003dbca6SMaxime Henrion 		}
3728fae3551SRodney W. Grimes 		rval = mountfs(vfstype,
3738fae3551SRodney W. Grimes 		    argv[0], argv[1], init_flags, options, NULL);
3748fae3551SRodney W. Grimes 		break;
3758fae3551SRodney W. Grimes 	default:
3768fae3551SRodney W. Grimes 		usage();
3778fae3551SRodney W. Grimes 		/* NOTREACHED */
3788fae3551SRodney W. Grimes 	}
3798fae3551SRodney W. Grimes 
3808fae3551SRodney W. Grimes 	/*
3818fae3551SRodney W. Grimes 	 * If the mount was successfully, and done by root, tell mountd the
3828fae3551SRodney W. Grimes 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
3838fae3551SRodney W. Grimes 	 */
3848fae3551SRodney W. Grimes 	if (rval == 0 && getuid() == 0 &&
3858fae3551SRodney W. Grimes 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
386fba1c154SSteve Price 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
3878fae3551SRodney W. Grimes 		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
3888fae3551SRodney W. Grimes 			err(1, "signal mountd");
3898fae3551SRodney W. Grimes 		(void)fclose(mountdfp);
3908fae3551SRodney W. Grimes 	}
3918fae3551SRodney W. Grimes 
3928fae3551SRodney W. Grimes 	exit(rval);
3938fae3551SRodney W. Grimes }
3948fae3551SRodney W. Grimes 
3958fae3551SRodney W. Grimes int
396e24dc56aSCraig Rodrigues ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
397fba1c154SSteve Price {
39890742659SPawel Jakub Dawidek 	char realfsfile[PATH_MAX];
399fba1c154SSteve Price 	int i;
400fba1c154SSteve Price 
401fba1c154SSteve Price 	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
402fba1c154SSteve Price 		/* the root file system can always be remounted */
403fba1c154SSteve Price 		return (0);
404fba1c154SSteve Price 
40590742659SPawel Jakub Dawidek 	/* The user may have specified a symlink in fstab, resolve the path */
40690742659SPawel Jakub Dawidek 	if (realpath(fs->fs_file, realfsfile) == NULL) {
40790742659SPawel Jakub Dawidek 		/* Cannot resolve the path, use original one */
40890742659SPawel Jakub Dawidek 		strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
40990742659SPawel Jakub Dawidek 	}
41090742659SPawel Jakub Dawidek 
411fba1c154SSteve Price 	for (i = mntsize - 1; i >= 0; --i)
41290742659SPawel Jakub Dawidek 		if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
413fba1c154SSteve Price 		    (!isremountable(fs->fs_vfstype) ||
414fba1c154SSteve Price 		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
415fba1c154SSteve Price 			return (1);
416fba1c154SSteve Price 	return (0);
417fba1c154SSteve Price }
418fba1c154SSteve Price 
419fba1c154SSteve Price int
420e24dc56aSCraig Rodrigues isremountable(const char *vfsname)
421fba1c154SSteve Price {
422fba1c154SSteve Price 	const char **cp;
423fba1c154SSteve Price 
424fba1c154SSteve Price 	for (cp = remountable_fs_names; *cp; cp++)
425fba1c154SSteve Price 		if (strcmp(*cp, vfsname) == 0)
426fba1c154SSteve Price 			return (1);
427fba1c154SSteve Price 	return (0);
428fba1c154SSteve Price }
429fba1c154SSteve Price 
430fba1c154SSteve Price int
431e24dc56aSCraig Rodrigues hasopt(const char *mntopts, const char *option)
432c06fe0a0SPeter Wemm {
433c06fe0a0SPeter Wemm 	int negative, found;
434c06fe0a0SPeter Wemm 	char *opt, *optbuf;
435c06fe0a0SPeter Wemm 
436c06fe0a0SPeter Wemm 	if (option[0] == 'n' && option[1] == 'o') {
437c06fe0a0SPeter Wemm 		negative = 1;
438c06fe0a0SPeter Wemm 		option += 2;
439c06fe0a0SPeter Wemm 	} else
440c06fe0a0SPeter Wemm 		negative = 0;
441c06fe0a0SPeter Wemm 	optbuf = strdup(mntopts);
442c06fe0a0SPeter Wemm 	found = 0;
443c06fe0a0SPeter Wemm 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
444c06fe0a0SPeter Wemm 		if (opt[0] == 'n' && opt[1] == 'o') {
445c06fe0a0SPeter Wemm 			if (!strcasecmp(opt + 2, option))
446c06fe0a0SPeter Wemm 				found = negative;
447c06fe0a0SPeter Wemm 		} else if (!strcasecmp(opt, option))
448c06fe0a0SPeter Wemm 			found = !negative;
449c06fe0a0SPeter Wemm 	}
450c06fe0a0SPeter Wemm 	free(optbuf);
451c06fe0a0SPeter Wemm 	return (found);
452c06fe0a0SPeter Wemm }
453c06fe0a0SPeter Wemm 
454c06fe0a0SPeter Wemm int
455e24dc56aSCraig Rodrigues mountfs(const char *vfstype, const char *spec, const char *name, int flags,
456e24dc56aSCraig Rodrigues 	const char *options, const char *mntopts)
4578fae3551SRodney W. Grimes {
4586f5f1a6bSCraig Rodrigues 	char *argv[100];
4598fae3551SRodney W. Grimes 	struct statfs sf;
4606f5f1a6bSCraig Rodrigues 	int argc, i, ret;
46168dd1ff4SWarner Losh 	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
4628fae3551SRodney W. Grimes 
46373dd3167SPoul-Henning Kamp 	/* resolve the mountpoint with realpath(3) */
46473dd3167SPoul-Henning Kamp 	(void)checkpath(name, mntpath);
4658fae3551SRodney W. Grimes 	name = mntpath;
4668fae3551SRodney W. Grimes 
467c06fe0a0SPeter Wemm 	if (mntopts == NULL)
468c06fe0a0SPeter Wemm 		mntopts = "";
4698fae3551SRodney W. Grimes 	optbuf = catopt(strdup(mntopts), options);
4708fae3551SRodney W. Grimes 
4718fae3551SRodney W. Grimes 	if (strcmp(name, "/") == 0)
4728fae3551SRodney W. Grimes 		flags |= MNT_UPDATE;
4738fae3551SRodney W. Grimes 	if (flags & MNT_FORCE)
4748fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "force");
4758fae3551SRodney W. Grimes 	if (flags & MNT_RDONLY)
4768fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "ro");
4778fae3551SRodney W. Grimes 	/*
4788fae3551SRodney W. Grimes 	 * XXX
4798fae3551SRodney W. Grimes 	 * The mount_mfs (newfs) command uses -o to select the
480bcb1d846SPhilippe Charnier 	 * optimization mode.  We don't pass the default "-o rw"
4818fae3551SRodney W. Grimes 	 * for that reason.
4828fae3551SRodney W. Grimes 	 */
4838fae3551SRodney W. Grimes 	if (flags & MNT_UPDATE)
4848fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "update");
4858fae3551SRodney W. Grimes 
4864ccd7546SRuslan Ermilov 	/* Compatibility glue. */
4874ccd7546SRuslan Ermilov 	if (strcmp(vfstype, "msdos") == 0)
4884ccd7546SRuslan Ermilov 		vfstype = "msdosfs";
4894ccd7546SRuslan Ermilov 
4906e34b479SColin Percival 	/* Construct the name of the appropriate mount command */
4916e34b479SColin Percival 	(void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
4926e34b479SColin Percival 
4938fae3551SRodney W. Grimes 	argc = 0;
4946e34b479SColin Percival 	argv[argc++] = execname;
4958fae3551SRodney W. Grimes 	mangle(optbuf, &argc, argv);
4966f5f1a6bSCraig Rodrigues 	argv[argc++] = strdup(spec);
4976f5f1a6bSCraig Rodrigues 	argv[argc++] = strdup(name);
4988fae3551SRodney W. Grimes 	argv[argc] = NULL;
4998fae3551SRodney W. Grimes 
5008fae3551SRodney W. Grimes 	if (debug) {
5018fae3551SRodney W. Grimes 		(void)printf("exec: mount_%s", vfstype);
5028fae3551SRodney W. Grimes 		for (i = 1; i < argc; i++)
5038fae3551SRodney W. Grimes 			(void)printf(" %s", argv[i]);
5048fae3551SRodney W. Grimes 		(void)printf("\n");
5058fae3551SRodney W. Grimes 		return (0);
5068fae3551SRodney W. Grimes 	}
5078fae3551SRodney W. Grimes 
508b1e6b712SCraig Rodrigues 	if (use_mountprog(vfstype)) {
5096f5f1a6bSCraig Rodrigues 		ret = exec_mountprog(name, execname, argv);
5106f5f1a6bSCraig Rodrigues 	} else {
5116f5f1a6bSCraig Rodrigues 		ret = mount_fs(vfstype, argc, argv);
5126f5f1a6bSCraig Rodrigues 	}
5136f5f1a6bSCraig Rodrigues 
5148fae3551SRodney W. Grimes 	free(optbuf);
5158fae3551SRodney W. Grimes 
5168fae3551SRodney W. Grimes 	if (verbose) {
5178fae3551SRodney W. Grimes 		if (statfs(name, &sf) < 0) {
518c06fe0a0SPeter Wemm 			warn("statfs %s", name);
5198fae3551SRodney W. Grimes 			return (1);
5208fae3551SRodney W. Grimes 		}
521a257a45eSJordan K. Hubbard 		if (fstab_style)
522a257a45eSJordan K. Hubbard 			putfsent(&sf);
523a257a45eSJordan K. Hubbard 		else
524c06fe0a0SPeter Wemm 			prmount(&sf);
5258fae3551SRodney W. Grimes 	}
5268fae3551SRodney W. Grimes 
5278fae3551SRodney W. Grimes 	return (0);
5288fae3551SRodney W. Grimes }
5298fae3551SRodney W. Grimes 
5308fae3551SRodney W. Grimes void
531e24dc56aSCraig Rodrigues prmount(struct statfs *sfp)
5328fae3551SRodney W. Grimes {
533aedf10acSCraig Rodrigues 	int flags;
534aedf10acSCraig Rodrigues 	unsigned int i;
5358fae3551SRodney W. Grimes 	struct opt *o;
536c06fe0a0SPeter Wemm 	struct passwd *pw;
5378fae3551SRodney W. Grimes 
538af2ea5aaSNick Hibma 	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
539af2ea5aaSNick Hibma 	    sfp->f_fstypename);
5408fae3551SRodney W. Grimes 
541c06fe0a0SPeter Wemm 	flags = sfp->f_flags & MNT_VISFLAGMASK;
542af2ea5aaSNick Hibma 	for (o = optnames; flags && o->o_opt; o++)
5438fae3551SRodney W. Grimes 		if (flags & o->o_opt) {
544af2ea5aaSNick Hibma 			(void)printf(", %s", o->o_name);
5458fae3551SRodney W. Grimes 			flags &= ~o->o_opt;
5468fae3551SRodney W. Grimes 		}
547dc9c6194SPawel Jakub Dawidek 	/*
548dc9c6194SPawel Jakub Dawidek 	 * Inform when file system is mounted by an unprivileged user
549dc9c6194SPawel Jakub Dawidek 	 * or privileged non-root user.
550dc9c6194SPawel Jakub Dawidek 	 */
551ddb842ccSJacques Vidrine 	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
552af2ea5aaSNick Hibma 		(void)printf(", mounted by ");
553c06fe0a0SPeter Wemm 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
554c06fe0a0SPeter Wemm 			(void)printf("%s", pw->pw_name);
555c06fe0a0SPeter Wemm 		else
556c06fe0a0SPeter Wemm 			(void)printf("%d", sfp->f_owner);
557c06fe0a0SPeter Wemm 	}
558c62ffab6SSheldon Hearn 	if (verbose) {
559677b9b3fSBruce Evans 		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
56096c65ccbSIan Dowse 			(void)printf(", writes: sync %ju async %ju",
56196c65ccbSIan Dowse 			    (uintmax_t)sfp->f_syncwrites,
56296c65ccbSIan Dowse 			    (uintmax_t)sfp->f_asyncwrites);
563461bb71eSKirk McKusick 		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
56496c65ccbSIan Dowse 			(void)printf(", reads: sync %ju async %ju",
56596c65ccbSIan Dowse 			    (uintmax_t)sfp->f_syncreads,
56696c65ccbSIan Dowse 			    (uintmax_t)sfp->f_asyncreads);
56705779418SIan Dowse 		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
56838f102c2SIan Dowse 			printf(", fsid ");
56938f102c2SIan Dowse 			for (i = 0; i < sizeof(sfp->f_fsid); i++)
57038f102c2SIan Dowse 				printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
571c62ffab6SSheldon Hearn 		}
57205779418SIan Dowse 	}
573af2ea5aaSNick Hibma 	(void)printf(")\n");
5748fae3551SRodney W. Grimes }
5758fae3551SRodney W. Grimes 
5768fae3551SRodney W. Grimes struct statfs *
577e24dc56aSCraig Rodrigues getmntpt(const char *name)
5788fae3551SRodney W. Grimes {
5798fae3551SRodney W. Grimes 	struct statfs *mntbuf;
5808fae3551SRodney W. Grimes 	int i, mntsize;
5818fae3551SRodney W. Grimes 
5828fae3551SRodney W. Grimes 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
583cf96af72SBrian Feldman 	for (i = mntsize - 1; i >= 0; i--) {
5848fae3551SRodney W. Grimes 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
5858fae3551SRodney W. Grimes 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
5868fae3551SRodney W. Grimes 			return (&mntbuf[i]);
587cf96af72SBrian Feldman 	}
5888fae3551SRodney W. Grimes 	return (NULL);
5898fae3551SRodney W. Grimes }
5908fae3551SRodney W. Grimes 
5918fae3551SRodney W. Grimes char *
592e24dc56aSCraig Rodrigues catopt(char *s0, const char *s1)
5938fae3551SRodney W. Grimes {
5948fae3551SRodney W. Grimes 	size_t i;
5958fae3551SRodney W. Grimes 	char *cp;
5968fae3551SRodney W. Grimes 
59718af6044SJoseph Koshy 	if (s1 == NULL || *s1 == '\0')
5984796c6ccSJuli Mallett 		return (s0);
59918af6044SJoseph Koshy 
6008fae3551SRodney W. Grimes 	if (s0 && *s0) {
6018fae3551SRodney W. Grimes 		i = strlen(s0) + strlen(s1) + 1 + 1;
6028fae3551SRodney W. Grimes 		if ((cp = malloc(i)) == NULL)
603bcb1d846SPhilippe Charnier 			errx(1, "malloc failed");
6048fae3551SRodney W. Grimes 		(void)snprintf(cp, i, "%s,%s", s0, s1);
6058fae3551SRodney W. Grimes 	} else
6068fae3551SRodney W. Grimes 		cp = strdup(s1);
6078fae3551SRodney W. Grimes 
6088fae3551SRodney W. Grimes 	if (s0)
6098fae3551SRodney W. Grimes 		free(s0);
6108fae3551SRodney W. Grimes 	return (cp);
6118fae3551SRodney W. Grimes }
6128fae3551SRodney W. Grimes 
6138fae3551SRodney W. Grimes void
6144796c6ccSJuli Mallett mangle(char *options, int *argcp, char *argv[])
6158fae3551SRodney W. Grimes {
6168fae3551SRodney W. Grimes 	char *p, *s;
6178fae3551SRodney W. Grimes 	int argc;
6188fae3551SRodney W. Grimes 
6198fae3551SRodney W. Grimes 	argc = *argcp;
6208fae3551SRodney W. Grimes 	for (s = options; (p = strsep(&s, ",")) != NULL;)
62118af6044SJoseph Koshy 		if (*p != '\0') {
622d9fa6ce7SCraig Rodrigues 			if (strcmp(p, "noauto") == 0) {
623d9fa6ce7SCraig Rodrigues 				/*
624d9fa6ce7SCraig Rodrigues 				 * Do not pass noauto option to nmount().
625d9fa6ce7SCraig Rodrigues 				 * or external mount program.  noauto is
626d9fa6ce7SCraig Rodrigues 				 * only used to prevent mounting a filesystem
627d9fa6ce7SCraig Rodrigues 				 * when 'mount -a' is specified, and is
628d9fa6ce7SCraig Rodrigues 				 * not a real mount option.
629d9fa6ce7SCraig Rodrigues 				 */
630d9fa6ce7SCraig Rodrigues 				continue;
63135d6c7f5SCraig Rodrigues 			} else if (strcmp(p, "userquota") == 0) {
63235d6c7f5SCraig Rodrigues 				continue;
6336e74fb9dSMaxim Konovalov 			} else if (strncmp(p, userquotaeq,
6346e74fb9dSMaxim Konovalov 			    sizeof(userquotaeq) - 1) == 0) {
6356e74fb9dSMaxim Konovalov 				continue;
63635d6c7f5SCraig Rodrigues 			} else if (strcmp(p, "groupquota") == 0) {
63735d6c7f5SCraig Rodrigues 				continue;
6386e74fb9dSMaxim Konovalov 			} else if (strncmp(p, groupquotaeq,
6396e74fb9dSMaxim Konovalov 			    sizeof(groupquotaeq) - 1) == 0) {
6406e74fb9dSMaxim Konovalov 				continue;
641d9fa6ce7SCraig Rodrigues 			} else if (*p == '-') {
6428fae3551SRodney W. Grimes 				argv[argc++] = p;
6438fae3551SRodney W. Grimes 				p = strchr(p, '=');
644adfdbe22STom Rhodes 				if (p != NULL) {
6458fae3551SRodney W. Grimes 					*p = '\0';
6468fae3551SRodney W. Grimes 					argv[argc++] = p+1;
6478fae3551SRodney W. Grimes 				}
648748e259bSCraig Rodrigues 			} else {
6496f5f1a6bSCraig Rodrigues 				argv[argc++] = strdup("-o");
6508fae3551SRodney W. Grimes 				argv[argc++] = p;
6518fae3551SRodney W. Grimes 			}
65218af6044SJoseph Koshy 		}
6538fae3551SRodney W. Grimes 
6548fae3551SRodney W. Grimes 	*argcp = argc;
6558fae3551SRodney W. Grimes }
6568fae3551SRodney W. Grimes 
65718af6044SJoseph Koshy 
65818af6044SJoseph Koshy char *
6594796c6ccSJuli Mallett update_options(char *opts, char *fstab, int curflags)
66018af6044SJoseph Koshy {
66118af6044SJoseph Koshy 	char *o, *p;
66218af6044SJoseph Koshy 	char *cur;
66318af6044SJoseph Koshy 	char *expopt, *newopt, *tmpopt;
66418af6044SJoseph Koshy 
66518af6044SJoseph Koshy 	if (opts == NULL)
6664796c6ccSJuli Mallett 		return (strdup(""));
66718af6044SJoseph Koshy 
66818af6044SJoseph Koshy 	/* remove meta options from list */
66918af6044SJoseph Koshy 	remopt(fstab, MOUNT_META_OPTION_FSTAB);
67018af6044SJoseph Koshy 	remopt(fstab, MOUNT_META_OPTION_CURRENT);
67118af6044SJoseph Koshy 	cur = flags2opts(curflags);
67218af6044SJoseph Koshy 
67318af6044SJoseph Koshy 	/*
67418af6044SJoseph Koshy 	 * Expand all meta-options passed to us first.
67518af6044SJoseph Koshy 	 */
67618af6044SJoseph Koshy 	expopt = NULL;
67718af6044SJoseph Koshy 	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
67818af6044SJoseph Koshy 		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
67918af6044SJoseph Koshy 			expopt = catopt(expopt, fstab);
68018af6044SJoseph Koshy 		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
68118af6044SJoseph Koshy 			expopt = catopt(expopt, cur);
68218af6044SJoseph Koshy 		else
68318af6044SJoseph Koshy 			expopt = catopt(expopt, o);
68418af6044SJoseph Koshy 	}
68518af6044SJoseph Koshy 	free(cur);
68618af6044SJoseph Koshy 	free(opts);
68718af6044SJoseph Koshy 
68818af6044SJoseph Koshy 	/*
68918af6044SJoseph Koshy 	 * Remove previous contradictory arguments. Given option "foo" we
69018af6044SJoseph Koshy 	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
69118af6044SJoseph Koshy 	 * and "foo" - so we can deal with possible options like "notice".
69218af6044SJoseph Koshy 	 */
69318af6044SJoseph Koshy 	newopt = NULL;
69418af6044SJoseph Koshy 	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
69518af6044SJoseph Koshy 		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
69618af6044SJoseph Koshy 			errx(1, "malloc failed");
69718af6044SJoseph Koshy 
69818af6044SJoseph Koshy 		strcpy(tmpopt, "no");
69918af6044SJoseph Koshy 		strcat(tmpopt, o);
70018af6044SJoseph Koshy 		remopt(newopt, tmpopt);
70118af6044SJoseph Koshy 		free(tmpopt);
70218af6044SJoseph Koshy 
70318af6044SJoseph Koshy 		if (strncmp("no", o, 2) == 0)
70418af6044SJoseph Koshy 			remopt(newopt, o+2);
70518af6044SJoseph Koshy 
70618af6044SJoseph Koshy 		newopt = catopt(newopt, o);
70718af6044SJoseph Koshy 	}
70818af6044SJoseph Koshy 	free(expopt);
70918af6044SJoseph Koshy 
7104796c6ccSJuli Mallett 	return (newopt);
71118af6044SJoseph Koshy }
71218af6044SJoseph Koshy 
71318af6044SJoseph Koshy void
7144796c6ccSJuli Mallett remopt(char *string, const char *opt)
71518af6044SJoseph Koshy {
71618af6044SJoseph Koshy 	char *o, *p, *r;
71718af6044SJoseph Koshy 
71818af6044SJoseph Koshy 	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
71918af6044SJoseph Koshy 		return;
72018af6044SJoseph Koshy 
72118af6044SJoseph Koshy 	r = string;
72218af6044SJoseph Koshy 
72318af6044SJoseph Koshy 	for (p = string; (o = strsep(&p, ",")) != NULL;) {
72418af6044SJoseph Koshy 		if (strcmp(opt, o) != 0) {
72518af6044SJoseph Koshy 			if (*r == ',' && *o != '\0')
72618af6044SJoseph Koshy 				r++;
72718af6044SJoseph Koshy 			while ((*r++ = *o++) != '\0')
72818af6044SJoseph Koshy 			    ;
72918af6044SJoseph Koshy 			*--r = ',';
73018af6044SJoseph Koshy 		}
73118af6044SJoseph Koshy 	}
73218af6044SJoseph Koshy 	*r = '\0';
73318af6044SJoseph Koshy }
73418af6044SJoseph Koshy 
7358fae3551SRodney W. Grimes void
7364796c6ccSJuli Mallett usage(void)
7378fae3551SRodney W. Grimes {
7388fae3551SRodney W. Grimes 
739bcb1d846SPhilippe Charnier 	(void)fprintf(stderr, "%s\n%s\n%s\n",
7408d646af5SRuslan Ermilov "usage: mount [-adfpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
7418d646af5SRuslan Ermilov "       mount [-dfpruvw] special | node",
7428d646af5SRuslan Ermilov "       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
7438fae3551SRodney W. Grimes 	exit(1);
7448fae3551SRodney W. Grimes }
745a257a45eSJordan K. Hubbard 
746a257a45eSJordan K. Hubbard void
7474796c6ccSJuli Mallett putfsent(const struct statfs *ent)
748a257a45eSJordan K. Hubbard {
749a257a45eSJordan K. Hubbard 	struct fstab *fst;
75018af6044SJoseph Koshy 	char *opts;
751a257a45eSJordan K. Hubbard 
75218af6044SJoseph Koshy 	opts = flags2opts(ent->f_flags);
7533ae7ea68SGiorgos Keramidas 
7543ae7ea68SGiorgos Keramidas 	/*
7553ae7ea68SGiorgos Keramidas 	 * "rw" is not a real mount option; this is why we print NULL as "rw"
7563ae7ea68SGiorgos Keramidas 	 * if opts is still NULL here.
7573ae7ea68SGiorgos Keramidas 	 */
75874cf460bSBruce Evans 	printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
7593ae7ea68SGiorgos Keramidas 	    ent->f_fstypename, opts ? opts : "rw");
76018af6044SJoseph Koshy 	free(opts);
761c06fe0a0SPeter Wemm 
762fba1c154SSteve Price 	if ((fst = getfsspec(ent->f_mntfromname)))
763a257a45eSJordan K. Hubbard 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
764fba1c154SSteve Price 	else if ((fst = getfsfile(ent->f_mntonname)))
765a257a45eSJordan K. Hubbard 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
766ab80d6faSBrian Feldman 	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
767ab80d6faSBrian Feldman 		if (strcmp(ent->f_mntonname, "/") == 0)
768a257a45eSJordan K. Hubbard 			printf("\t1 1\n");
769a257a45eSJordan K. Hubbard 		else
770ab80d6faSBrian Feldman 			printf("\t2 2\n");
771ab80d6faSBrian Feldman 	} else
772a257a45eSJordan K. Hubbard 		printf("\t0 0\n");
773a257a45eSJordan K. Hubbard }
77418af6044SJoseph Koshy 
77518af6044SJoseph Koshy 
77618af6044SJoseph Koshy char *
7774796c6ccSJuli Mallett flags2opts(int flags)
77818af6044SJoseph Koshy {
77918af6044SJoseph Koshy 	char *res;
78018af6044SJoseph Koshy 
78118af6044SJoseph Koshy 	res = NULL;
78218af6044SJoseph Koshy 
78388e2c335SCraig Rodrigues 	if (flags & MNT_RDONLY)		res = catopt(res, "ro");
78418af6044SJoseph Koshy 	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
78518af6044SJoseph Koshy 	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
78618af6044SJoseph Koshy 	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
78718af6044SJoseph Koshy 	if (flags & MNT_UNION)		res = catopt(res, "union");
78818af6044SJoseph Koshy 	if (flags & MNT_ASYNC)		res = catopt(res, "async");
78918af6044SJoseph Koshy 	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
79018af6044SJoseph Koshy 	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
79118af6044SJoseph Koshy 	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
79218af6044SJoseph Koshy 	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
79318af6044SJoseph Koshy 	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
794ba0fbe96SRobert Watson 	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
79503d94b50SRobert Watson 	if (flags & MNT_ACLS)		res = catopt(res, "acls");
79618af6044SJoseph Koshy 
7974796c6ccSJuli Mallett 	return (res);
79818af6044SJoseph Koshy }
799