xref: /freebsd/sbin/mount/mount.c (revision 88e2c3356462f15fec2f4812364d1fd70a7cdebc)
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 *);
776f5f1a6bSCraig Rodrigues 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 
1236f5f1a6bSCraig Rodrigues static int
1246f5f1a6bSCraig Rodrigues use_mountprog(const char *vfstype)
1256f5f1a6bSCraig Rodrigues {
1266f5f1a6bSCraig Rodrigues 	/* XXX: We need to get away from implementing external mount
1276f5f1a6bSCraig Rodrigues 	 *      programs for every filesystem, and move towards having
1286f5f1a6bSCraig Rodrigues 	 *	each filesystem properly implement the nmount() system call.
1296f5f1a6bSCraig Rodrigues 	 */
1306f5f1a6bSCraig Rodrigues 	unsigned int i;
1316f5f1a6bSCraig Rodrigues 	const char *fs[] = {
1326973ce04STai-hwa Liang 	"cd9660", "mfs", "msdosfs", "nfs", "nfs4", "ntfs",
133cf9e56b0SCraig Rodrigues 	"nwfs", "nullfs", "portalfs", "smbfs", "udf", "umapfs",
1346f5f1a6bSCraig Rodrigues 	"unionfs",
1356f5f1a6bSCraig Rodrigues 	NULL
1366f5f1a6bSCraig Rodrigues 	};
1376f5f1a6bSCraig Rodrigues 
1386f5f1a6bSCraig Rodrigues 	for (i=0; fs[i] != NULL; ++i) {
1396f5f1a6bSCraig Rodrigues 		if (strcmp(vfstype, fs[i]) == 0)
1406f5f1a6bSCraig Rodrigues 			return 1;
1416f5f1a6bSCraig Rodrigues 	}
1426f5f1a6bSCraig Rodrigues 
1436f5f1a6bSCraig Rodrigues 	return 0;
1446f5f1a6bSCraig Rodrigues }
1456f5f1a6bSCraig Rodrigues 
1466f5f1a6bSCraig Rodrigues static int
1476f5f1a6bSCraig Rodrigues exec_mountprog(const char *name, const char *execname,
1486f5f1a6bSCraig Rodrigues 	char *const argv[])
1496f5f1a6bSCraig Rodrigues {
1506f5f1a6bSCraig Rodrigues 	pid_t pid;
1516f5f1a6bSCraig Rodrigues 	int status;
1526f5f1a6bSCraig Rodrigues 
1536f5f1a6bSCraig Rodrigues 	switch (pid = fork()) {
1546f5f1a6bSCraig Rodrigues 	case -1:				/* Error. */
1556f5f1a6bSCraig Rodrigues 		warn("fork");
1566f5f1a6bSCraig Rodrigues 		exit (1);
1576f5f1a6bSCraig Rodrigues 	case 0:					/* Child. */
1586f5f1a6bSCraig Rodrigues 		/* Go find an executable. */
1596f5f1a6bSCraig Rodrigues 		execvP(execname, _PATH_SYSPATH, argv);
1606f5f1a6bSCraig Rodrigues 		if (errno == ENOENT) {
1616f5f1a6bSCraig Rodrigues 			warn("exec %s not found in %s", execname,
1626f5f1a6bSCraig Rodrigues 			    _PATH_SYSPATH);
1636f5f1a6bSCraig Rodrigues 		}
1646f5f1a6bSCraig Rodrigues 		exit(1);
1656f5f1a6bSCraig Rodrigues 	default:				/* Parent. */
1666f5f1a6bSCraig Rodrigues 		if (waitpid(pid, &status, 0) < 0) {
1676f5f1a6bSCraig Rodrigues 			warn("waitpid");
1686f5f1a6bSCraig Rodrigues 			return (1);
1696f5f1a6bSCraig Rodrigues 		}
1706f5f1a6bSCraig Rodrigues 
1716f5f1a6bSCraig Rodrigues 		if (WIFEXITED(status)) {
1726f5f1a6bSCraig Rodrigues 			if (WEXITSTATUS(status) != 0)
1736f5f1a6bSCraig Rodrigues 				return (WEXITSTATUS(status));
1746f5f1a6bSCraig Rodrigues 		} else if (WIFSIGNALED(status)) {
1756f5f1a6bSCraig Rodrigues 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
1766f5f1a6bSCraig Rodrigues 			return (1);
1776f5f1a6bSCraig Rodrigues 		}
1786f5f1a6bSCraig Rodrigues 		break;
1796f5f1a6bSCraig Rodrigues 	}
1806f5f1a6bSCraig Rodrigues 
1816f5f1a6bSCraig Rodrigues 	return (0);
1826f5f1a6bSCraig Rodrigues }
1836f5f1a6bSCraig Rodrigues 
1848fae3551SRodney W. Grimes int
185e24dc56aSCraig Rodrigues main(int argc, char *argv[])
1868fae3551SRodney W. Grimes {
187c06fe0a0SPeter Wemm 	const char *mntfromname, **vfslist, *vfstype;
1888fae3551SRodney W. Grimes 	struct fstab *fs;
1898fae3551SRodney W. Grimes 	struct statfs *mntbuf;
1908fae3551SRodney W. Grimes 	FILE *mountdfp;
1918fae3551SRodney W. Grimes 	pid_t pid;
19288e2c335SCraig Rodrigues 	int all, ch, i, init_flags, mntsize, rval, have_fstab, ro;
193003dbca6SMaxime Henrion 	char *cp, *ep, *options;
1948fae3551SRodney W. Grimes 
1958fae3551SRodney W. Grimes 	all = init_flags = 0;
19688e2c335SCraig Rodrigues 	ro = 0;
1978fae3551SRodney W. Grimes 	options = NULL;
1988fae3551SRodney W. Grimes 	vfslist = NULL;
1998fae3551SRodney W. Grimes 	vfstype = "ufs";
200ef258dd9SMatthew N. Dodd 	while ((ch = getopt(argc, argv, "adF:fo:prwt:uv")) != -1)
2018fae3551SRodney W. Grimes 		switch (ch) {
2028fae3551SRodney W. Grimes 		case 'a':
2038fae3551SRodney W. Grimes 			all = 1;
2048fae3551SRodney W. Grimes 			break;
2058fae3551SRodney W. Grimes 		case 'd':
2068fae3551SRodney W. Grimes 			debug = 1;
2078fae3551SRodney W. Grimes 			break;
208ef258dd9SMatthew N. Dodd 		case 'F':
209ef258dd9SMatthew N. Dodd 			setfstab(optarg);
210ef258dd9SMatthew N. Dodd 			break;
2118fae3551SRodney W. Grimes 		case 'f':
2128fae3551SRodney W. Grimes 			init_flags |= MNT_FORCE;
2138fae3551SRodney W. Grimes 			break;
2148fae3551SRodney W. Grimes 		case 'o':
2158fae3551SRodney W. Grimes 			if (*optarg)
2168fae3551SRodney W. Grimes 				options = catopt(options, optarg);
2178fae3551SRodney W. Grimes 			break;
21874cf460bSBruce Evans 		case 'p':
21974cf460bSBruce Evans 			fstab_style = 1;
22074cf460bSBruce Evans 			verbose = 1;
22174cf460bSBruce Evans 			break;
2228fae3551SRodney W. Grimes 		case 'r':
223ad044771SDima Dorfman 			options = catopt(options, "ro");
22488e2c335SCraig Rodrigues 			ro = 1;
2258fae3551SRodney W. Grimes 			break;
2268fae3551SRodney W. Grimes 		case 't':
2278fae3551SRodney W. Grimes 			if (vfslist != NULL)
228bcb1d846SPhilippe Charnier 				errx(1, "only one -t option may be specified");
2298fae3551SRodney W. Grimes 			vfslist = makevfslist(optarg);
2308fae3551SRodney W. Grimes 			vfstype = optarg;
2318fae3551SRodney W. Grimes 			break;
2328fae3551SRodney W. Grimes 		case 'u':
2338fae3551SRodney W. Grimes 			init_flags |= MNT_UPDATE;
2348fae3551SRodney W. Grimes 			break;
2358fae3551SRodney W. Grimes 		case 'v':
2368fae3551SRodney W. Grimes 			verbose = 1;
2378fae3551SRodney W. Grimes 			break;
2388fae3551SRodney W. Grimes 		case 'w':
239ad044771SDima Dorfman 			options = catopt(options, "noro");
2408fae3551SRodney W. Grimes 			break;
2418fae3551SRodney W. Grimes 		case '?':
2428fae3551SRodney W. Grimes 		default:
2438fae3551SRodney W. Grimes 			usage();
2448fae3551SRodney W. Grimes 			/* NOTREACHED */
2458fae3551SRodney W. Grimes 		}
2468fae3551SRodney W. Grimes 	argc -= optind;
2478fae3551SRodney W. Grimes 	argv += optind;
2488fae3551SRodney W. Grimes 
2498fae3551SRodney W. Grimes #define	BADTYPE(type)							\
2508fae3551SRodney W. Grimes 	(strcmp(type, FSTAB_RO) &&					\
2518fae3551SRodney W. Grimes 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
2528fae3551SRodney W. Grimes 
25388e2c335SCraig Rodrigues 	if ((init_flags & MNT_UPDATE) && (ro == 0))
25488e2c335SCraig Rodrigues 		options = catopt(options, "noro");
25588e2c335SCraig Rodrigues 
2568fae3551SRodney W. Grimes 	rval = 0;
2578fae3551SRodney W. Grimes 	switch (argc) {
2588fae3551SRodney W. Grimes 	case 0:
259fba1c154SSteve Price 		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
260fba1c154SSteve Price 			err(1, "getmntinfo");
261fba1c154SSteve Price 		if (all) {
2628fae3551SRodney W. Grimes 			while ((fs = getfsent()) != NULL) {
2638fae3551SRodney W. Grimes 				if (BADTYPE(fs->fs_type))
2648fae3551SRodney W. Grimes 					continue;
265c06fe0a0SPeter Wemm 				if (checkvfsname(fs->fs_vfstype, vfslist))
2668fae3551SRodney W. Grimes 					continue;
267c06fe0a0SPeter Wemm 				if (hasopt(fs->fs_mntops, "noauto"))
26889beb278SDavid Greenman 					continue;
26998201b0cSBruce Evans 				if (!(init_flags & MNT_UPDATE) &&
27098201b0cSBruce Evans 				    ismounted(fs, mntbuf, mntsize))
271fba1c154SSteve Price 					continue;
27213cbdf24SGuido van Rooij 				options = update_options(options, fs->fs_mntops,
27313cbdf24SGuido van Rooij 				    mntbuf->f_flags);
2748fae3551SRodney W. Grimes 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
2758fae3551SRodney W. Grimes 				    fs->fs_file, init_flags, options,
2768fae3551SRodney W. Grimes 				    fs->fs_mntops))
2778fae3551SRodney W. Grimes 					rval = 1;
2788fae3551SRodney W. Grimes 			}
279fba1c154SSteve Price 		} else if (fstab_style) {
280a257a45eSJordan K. Hubbard 			for (i = 0; i < mntsize; i++) {
281c06fe0a0SPeter Wemm 				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
282a257a45eSJordan K. Hubbard 					continue;
283a257a45eSJordan K. Hubbard 				putfsent(&mntbuf[i]);
284a257a45eSJordan K. Hubbard 			}
28574cf460bSBruce Evans 		} else {
2868fae3551SRodney W. Grimes 			for (i = 0; i < mntsize; i++) {
28774cf460bSBruce Evans 				if (checkvfsname(mntbuf[i].f_fstypename,
28874cf460bSBruce Evans 				    vfslist))
2898fae3551SRodney W. Grimes 					continue;
290c06fe0a0SPeter Wemm 				prmount(&mntbuf[i]);
2918fae3551SRodney W. Grimes 			}
2928fae3551SRodney W. Grimes 		}
2938fae3551SRodney W. Grimes 		exit(rval);
2948fae3551SRodney W. Grimes 	case 1:
2958fae3551SRodney W. Grimes 		if (vfslist != NULL)
2968fae3551SRodney W. Grimes 			usage();
2978fae3551SRodney W. Grimes 
29801a9bce5SEric Anholt 		rmslashes(*argv, *argv);
2998fae3551SRodney W. Grimes 		if (init_flags & MNT_UPDATE) {
300cf96af72SBrian Feldman 			mntfromname = NULL;
301cf96af72SBrian Feldman 			have_fstab = 0;
3028fae3551SRodney W. Grimes 			if ((mntbuf = getmntpt(*argv)) == NULL)
303cf96af72SBrian Feldman 				errx(1, "not currently mounted %s", *argv);
304cf96af72SBrian Feldman 			/*
305cf96af72SBrian Feldman 			 * Only get the mntflags from fstab if both mntpoint
306cf96af72SBrian Feldman 			 * and mntspec are identical. Also handle the special
307cf96af72SBrian Feldman 			 * case where just '/' is mounted and 'spec' is not
308cf96af72SBrian Feldman 			 * identical with the one from fstab ('/dev' is missing
309cf96af72SBrian Feldman 			 * in the spec-string at boot-time).
310cf96af72SBrian Feldman 			 */
31118af6044SJoseph Koshy 			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
312cf96af72SBrian Feldman 				if (strcmp(fs->fs_spec,
313cf96af72SBrian Feldman 				    mntbuf->f_mntfromname) == 0 &&
314cf96af72SBrian Feldman 				    strcmp(fs->fs_file,
315cf96af72SBrian Feldman 				    mntbuf->f_mntonname) == 0) {
316cf96af72SBrian Feldman 					have_fstab = 1;
317cf96af72SBrian Feldman 					mntfromname = mntbuf->f_mntfromname;
318cf96af72SBrian Feldman 				} else if (argv[0][0] == '/' &&
319cf96af72SBrian Feldman 				    argv[0][1] == '\0') {
320cf96af72SBrian Feldman 					fs = getfsfile("/");
321cf96af72SBrian Feldman 					have_fstab = 1;
322c06fe0a0SPeter Wemm 					mntfromname = fs->fs_spec;
323cf96af72SBrian Feldman 				}
324cf96af72SBrian Feldman 			}
325cf96af72SBrian Feldman 			if (have_fstab) {
32618af6044SJoseph Koshy 				options = update_options(options, fs->fs_mntops,
32718af6044SJoseph Koshy 				    mntbuf->f_flags);
32818af6044SJoseph Koshy 			} else {
329c06fe0a0SPeter Wemm 				mntfromname = mntbuf->f_mntfromname;
33018af6044SJoseph Koshy 				options = update_options(options, NULL,
33118af6044SJoseph Koshy 				    mntbuf->f_flags);
33218af6044SJoseph Koshy 			}
333c06fe0a0SPeter Wemm 			rval = mountfs(mntbuf->f_fstypename, mntfromname,
334c06fe0a0SPeter Wemm 			    mntbuf->f_mntonname, init_flags, options, 0);
335c06fe0a0SPeter Wemm 			break;
336c06fe0a0SPeter Wemm 		}
3378fae3551SRodney W. Grimes 		if ((fs = getfsfile(*argv)) == NULL &&
3388fae3551SRodney W. Grimes 		    (fs = getfsspec(*argv)) == NULL)
339bcb1d846SPhilippe Charnier 			errx(1, "%s: unknown special file or file system",
3408fae3551SRodney W. Grimes 			    *argv);
3418fae3551SRodney W. Grimes 		if (BADTYPE(fs->fs_type))
342bcb1d846SPhilippe Charnier 			errx(1, "%s has unknown file system type",
3438fae3551SRodney W. Grimes 			    *argv);
344c06fe0a0SPeter Wemm 		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
345c06fe0a0SPeter Wemm 		    init_flags, options, fs->fs_mntops);
3468fae3551SRodney W. Grimes 		break;
3478fae3551SRodney W. Grimes 	case 2:
3488fae3551SRodney W. Grimes 		/*
349cf96af72SBrian Feldman 		 * If -t flag has not been specified, the path cannot be
350003dbca6SMaxime Henrion 		 * found, spec contains either a ':' or a '@', then assume
351cf96af72SBrian Feldman 		 * that an NFS file system is being specified ala Sun.
352003dbca6SMaxime Henrion 		 * Check if the hostname contains only allowed characters
353003dbca6SMaxime Henrion 		 * to reduce false positives.  IPv6 addresses containing
354003dbca6SMaxime Henrion 		 * ':' will be correctly parsed only if the separator is '@'.
355003dbca6SMaxime Henrion 		 * The definition of a valid hostname is taken from RFC 1034.
3568fae3551SRodney W. Grimes 		 */
35705779418SIan Dowse 		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
35805779418SIan Dowse 		    (ep = strchr(argv[0], ':')) != NULL)) {
359da85c82bSMaxime Henrion 			if (*ep == '@') {
360da85c82bSMaxime Henrion 				cp = ep + 1;
361da85c82bSMaxime Henrion 				ep = cp + strlen(cp);
362da85c82bSMaxime Henrion 			} else
363003dbca6SMaxime Henrion 				cp = argv[0];
364003dbca6SMaxime Henrion 			while (cp != ep) {
365003dbca6SMaxime Henrion 				if (!isdigit(*cp) && !isalpha(*cp) &&
366003dbca6SMaxime Henrion 				    *cp != '.' && *cp != '-' && *cp != ':')
367003dbca6SMaxime Henrion 					break;
368003dbca6SMaxime Henrion 				cp++;
369003dbca6SMaxime Henrion 			}
370003dbca6SMaxime Henrion 			if (cp == ep)
3718fae3551SRodney W. Grimes 				vfstype = "nfs";
372003dbca6SMaxime Henrion 		}
3738fae3551SRodney W. Grimes 		rval = mountfs(vfstype,
3748fae3551SRodney W. Grimes 		    argv[0], argv[1], init_flags, options, NULL);
3758fae3551SRodney W. Grimes 		break;
3768fae3551SRodney W. Grimes 	default:
3778fae3551SRodney W. Grimes 		usage();
3788fae3551SRodney W. Grimes 		/* NOTREACHED */
3798fae3551SRodney W. Grimes 	}
3808fae3551SRodney W. Grimes 
3818fae3551SRodney W. Grimes 	/*
3828fae3551SRodney W. Grimes 	 * If the mount was successfully, and done by root, tell mountd the
3838fae3551SRodney W. Grimes 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
3848fae3551SRodney W. Grimes 	 */
3858fae3551SRodney W. Grimes 	if (rval == 0 && getuid() == 0 &&
3868fae3551SRodney W. Grimes 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
387fba1c154SSteve Price 		if (fscanf(mountdfp, "%d", &pid) == 1 &&
3888fae3551SRodney W. Grimes 		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
3898fae3551SRodney W. Grimes 			err(1, "signal mountd");
3908fae3551SRodney W. Grimes 		(void)fclose(mountdfp);
3918fae3551SRodney W. Grimes 	}
3928fae3551SRodney W. Grimes 
3938fae3551SRodney W. Grimes 	exit(rval);
3948fae3551SRodney W. Grimes }
3958fae3551SRodney W. Grimes 
3968fae3551SRodney W. Grimes int
397e24dc56aSCraig Rodrigues ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
398fba1c154SSteve Price {
39990742659SPawel Jakub Dawidek 	char realfsfile[PATH_MAX];
400fba1c154SSteve Price 	int i;
401fba1c154SSteve Price 
402fba1c154SSteve Price 	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
403fba1c154SSteve Price 		/* the root file system can always be remounted */
404fba1c154SSteve Price 		return (0);
405fba1c154SSteve Price 
40690742659SPawel Jakub Dawidek 	/* The user may have specified a symlink in fstab, resolve the path */
40790742659SPawel Jakub Dawidek 	if (realpath(fs->fs_file, realfsfile) == NULL) {
40890742659SPawel Jakub Dawidek 		/* Cannot resolve the path, use original one */
40990742659SPawel Jakub Dawidek 		strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
41090742659SPawel Jakub Dawidek 	}
41190742659SPawel Jakub Dawidek 
412fba1c154SSteve Price 	for (i = mntsize - 1; i >= 0; --i)
41390742659SPawel Jakub Dawidek 		if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
414fba1c154SSteve Price 		    (!isremountable(fs->fs_vfstype) ||
415fba1c154SSteve Price 		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
416fba1c154SSteve Price 			return (1);
417fba1c154SSteve Price 	return (0);
418fba1c154SSteve Price }
419fba1c154SSteve Price 
420fba1c154SSteve Price int
421e24dc56aSCraig Rodrigues isremountable(const char *vfsname)
422fba1c154SSteve Price {
423fba1c154SSteve Price 	const char **cp;
424fba1c154SSteve Price 
425fba1c154SSteve Price 	for (cp = remountable_fs_names; *cp; cp++)
426fba1c154SSteve Price 		if (strcmp(*cp, vfsname) == 0)
427fba1c154SSteve Price 			return (1);
428fba1c154SSteve Price 	return (0);
429fba1c154SSteve Price }
430fba1c154SSteve Price 
431fba1c154SSteve Price int
432e24dc56aSCraig Rodrigues hasopt(const char *mntopts, const char *option)
433c06fe0a0SPeter Wemm {
434c06fe0a0SPeter Wemm 	int negative, found;
435c06fe0a0SPeter Wemm 	char *opt, *optbuf;
436c06fe0a0SPeter Wemm 
437c06fe0a0SPeter Wemm 	if (option[0] == 'n' && option[1] == 'o') {
438c06fe0a0SPeter Wemm 		negative = 1;
439c06fe0a0SPeter Wemm 		option += 2;
440c06fe0a0SPeter Wemm 	} else
441c06fe0a0SPeter Wemm 		negative = 0;
442c06fe0a0SPeter Wemm 	optbuf = strdup(mntopts);
443c06fe0a0SPeter Wemm 	found = 0;
444c06fe0a0SPeter Wemm 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
445c06fe0a0SPeter Wemm 		if (opt[0] == 'n' && opt[1] == 'o') {
446c06fe0a0SPeter Wemm 			if (!strcasecmp(opt + 2, option))
447c06fe0a0SPeter Wemm 				found = negative;
448c06fe0a0SPeter Wemm 		} else if (!strcasecmp(opt, option))
449c06fe0a0SPeter Wemm 			found = !negative;
450c06fe0a0SPeter Wemm 	}
451c06fe0a0SPeter Wemm 	free(optbuf);
452c06fe0a0SPeter Wemm 	return (found);
453c06fe0a0SPeter Wemm }
454c06fe0a0SPeter Wemm 
455c06fe0a0SPeter Wemm int
456e24dc56aSCraig Rodrigues mountfs(const char *vfstype, const char *spec, const char *name, int flags,
457e24dc56aSCraig Rodrigues 	const char *options, const char *mntopts)
4588fae3551SRodney W. Grimes {
4596f5f1a6bSCraig Rodrigues 	char *argv[100];
4608fae3551SRodney W. Grimes 	struct statfs sf;
4616f5f1a6bSCraig Rodrigues 	int argc, i, ret;
46268dd1ff4SWarner Losh 	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
4638fae3551SRodney W. Grimes 
46473dd3167SPoul-Henning Kamp 	/* resolve the mountpoint with realpath(3) */
46573dd3167SPoul-Henning Kamp 	(void)checkpath(name, mntpath);
4668fae3551SRodney W. Grimes 	name = mntpath;
4678fae3551SRodney W. Grimes 
468c06fe0a0SPeter Wemm 	if (mntopts == NULL)
469c06fe0a0SPeter Wemm 		mntopts = "";
4708fae3551SRodney W. Grimes 	optbuf = catopt(strdup(mntopts), options);
4718fae3551SRodney W. Grimes 
4728fae3551SRodney W. Grimes 	if (strcmp(name, "/") == 0)
4738fae3551SRodney W. Grimes 		flags |= MNT_UPDATE;
4748fae3551SRodney W. Grimes 	if (flags & MNT_FORCE)
4758fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "force");
4768fae3551SRodney W. Grimes 	if (flags & MNT_RDONLY)
4778fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "ro");
4788fae3551SRodney W. Grimes 	/*
4798fae3551SRodney W. Grimes 	 * XXX
4808fae3551SRodney W. Grimes 	 * The mount_mfs (newfs) command uses -o to select the
481bcb1d846SPhilippe Charnier 	 * optimization mode.  We don't pass the default "-o rw"
4828fae3551SRodney W. Grimes 	 * for that reason.
4838fae3551SRodney W. Grimes 	 */
4848fae3551SRodney W. Grimes 	if (flags & MNT_UPDATE)
4858fae3551SRodney W. Grimes 		optbuf = catopt(optbuf, "update");
4868fae3551SRodney W. Grimes 
4874ccd7546SRuslan Ermilov 	/* Compatibility glue. */
4884ccd7546SRuslan Ermilov 	if (strcmp(vfstype, "msdos") == 0)
4894ccd7546SRuslan Ermilov 		vfstype = "msdosfs";
4904ccd7546SRuslan Ermilov 
4916e34b479SColin Percival 	/* Construct the name of the appropriate mount command */
4926e34b479SColin Percival 	(void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
4936e34b479SColin Percival 
4948fae3551SRodney W. Grimes 	argc = 0;
4956e34b479SColin Percival 	argv[argc++] = execname;
4968fae3551SRodney W. Grimes 	mangle(optbuf, &argc, argv);
4976f5f1a6bSCraig Rodrigues 	argv[argc++] = strdup(spec);
4986f5f1a6bSCraig Rodrigues 	argv[argc++] = strdup(name);
4998fae3551SRodney W. Grimes 	argv[argc] = NULL;
5008fae3551SRodney W. Grimes 
5018fae3551SRodney W. Grimes 	if (debug) {
5028fae3551SRodney W. Grimes 		(void)printf("exec: mount_%s", vfstype);
5038fae3551SRodney W. Grimes 		for (i = 1; i < argc; i++)
5048fae3551SRodney W. Grimes 			(void)printf(" %s", argv[i]);
5058fae3551SRodney W. Grimes 		(void)printf("\n");
5068fae3551SRodney W. Grimes 		return (0);
5078fae3551SRodney W. Grimes 	}
5088fae3551SRodney W. Grimes 
509b1e6b712SCraig Rodrigues 	if (use_mountprog(vfstype)) {
5106f5f1a6bSCraig Rodrigues 		ret = exec_mountprog(name, execname, argv);
5116f5f1a6bSCraig Rodrigues 	} else {
5126f5f1a6bSCraig Rodrigues 		ret = mount_fs(vfstype, argc, argv);
5136f5f1a6bSCraig Rodrigues 	}
5146f5f1a6bSCraig Rodrigues 
5158fae3551SRodney W. Grimes 	free(optbuf);
5168fae3551SRodney W. Grimes 
5178fae3551SRodney W. Grimes 	if (verbose) {
5188fae3551SRodney W. Grimes 		if (statfs(name, &sf) < 0) {
519c06fe0a0SPeter Wemm 			warn("statfs %s", name);
5208fae3551SRodney W. Grimes 			return (1);
5218fae3551SRodney W. Grimes 		}
522a257a45eSJordan K. Hubbard 		if (fstab_style)
523a257a45eSJordan K. Hubbard 			putfsent(&sf);
524a257a45eSJordan K. Hubbard 		else
525c06fe0a0SPeter Wemm 			prmount(&sf);
5268fae3551SRodney W. Grimes 	}
5278fae3551SRodney W. Grimes 
5288fae3551SRodney W. Grimes 	return (0);
5298fae3551SRodney W. Grimes }
5308fae3551SRodney W. Grimes 
5318fae3551SRodney W. Grimes void
532e24dc56aSCraig Rodrigues prmount(struct statfs *sfp)
5338fae3551SRodney W. Grimes {
534aedf10acSCraig Rodrigues 	int flags;
535aedf10acSCraig Rodrigues 	unsigned int i;
5368fae3551SRodney W. Grimes 	struct opt *o;
537c06fe0a0SPeter Wemm 	struct passwd *pw;
5388fae3551SRodney W. Grimes 
539af2ea5aaSNick Hibma 	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
540af2ea5aaSNick Hibma 	    sfp->f_fstypename);
5418fae3551SRodney W. Grimes 
542c06fe0a0SPeter Wemm 	flags = sfp->f_flags & MNT_VISFLAGMASK;
543af2ea5aaSNick Hibma 	for (o = optnames; flags && o->o_opt; o++)
5448fae3551SRodney W. Grimes 		if (flags & o->o_opt) {
545af2ea5aaSNick Hibma 			(void)printf(", %s", o->o_name);
5468fae3551SRodney W. Grimes 			flags &= ~o->o_opt;
5478fae3551SRodney W. Grimes 		}
548dc9c6194SPawel Jakub Dawidek 	/*
549dc9c6194SPawel Jakub Dawidek 	 * Inform when file system is mounted by an unprivileged user
550dc9c6194SPawel Jakub Dawidek 	 * or privileged non-root user.
551dc9c6194SPawel Jakub Dawidek 	 */
552ddb842ccSJacques Vidrine 	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
553af2ea5aaSNick Hibma 		(void)printf(", mounted by ");
554c06fe0a0SPeter Wemm 		if ((pw = getpwuid(sfp->f_owner)) != NULL)
555c06fe0a0SPeter Wemm 			(void)printf("%s", pw->pw_name);
556c06fe0a0SPeter Wemm 		else
557c06fe0a0SPeter Wemm 			(void)printf("%d", sfp->f_owner);
558c06fe0a0SPeter Wemm 	}
559c62ffab6SSheldon Hearn 	if (verbose) {
560677b9b3fSBruce Evans 		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
56196c65ccbSIan Dowse 			(void)printf(", writes: sync %ju async %ju",
56296c65ccbSIan Dowse 			    (uintmax_t)sfp->f_syncwrites,
56396c65ccbSIan Dowse 			    (uintmax_t)sfp->f_asyncwrites);
564461bb71eSKirk McKusick 		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
56596c65ccbSIan Dowse 			(void)printf(", reads: sync %ju async %ju",
56696c65ccbSIan Dowse 			    (uintmax_t)sfp->f_syncreads,
56796c65ccbSIan Dowse 			    (uintmax_t)sfp->f_asyncreads);
56805779418SIan Dowse 		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
56938f102c2SIan Dowse 			printf(", fsid ");
57038f102c2SIan Dowse 			for (i = 0; i < sizeof(sfp->f_fsid); i++)
57138f102c2SIan Dowse 				printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
572c62ffab6SSheldon Hearn 		}
57305779418SIan Dowse 	}
574af2ea5aaSNick Hibma 	(void)printf(")\n");
5758fae3551SRodney W. Grimes }
5768fae3551SRodney W. Grimes 
5778fae3551SRodney W. Grimes struct statfs *
578e24dc56aSCraig Rodrigues getmntpt(const char *name)
5798fae3551SRodney W. Grimes {
5808fae3551SRodney W. Grimes 	struct statfs *mntbuf;
5818fae3551SRodney W. Grimes 	int i, mntsize;
5828fae3551SRodney W. Grimes 
5838fae3551SRodney W. Grimes 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
584cf96af72SBrian Feldman 	for (i = mntsize - 1; i >= 0; i--) {
5858fae3551SRodney W. Grimes 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
5868fae3551SRodney W. Grimes 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
5878fae3551SRodney W. Grimes 			return (&mntbuf[i]);
588cf96af72SBrian Feldman 	}
5898fae3551SRodney W. Grimes 	return (NULL);
5908fae3551SRodney W. Grimes }
5918fae3551SRodney W. Grimes 
5928fae3551SRodney W. Grimes char *
593e24dc56aSCraig Rodrigues catopt(char *s0, const char *s1)
5948fae3551SRodney W. Grimes {
5958fae3551SRodney W. Grimes 	size_t i;
5968fae3551SRodney W. Grimes 	char *cp;
5978fae3551SRodney W. Grimes 
59818af6044SJoseph Koshy 	if (s1 == NULL || *s1 == '\0')
59918af6044SJoseph Koshy 		return s0;
60018af6044SJoseph Koshy 
6018fae3551SRodney W. Grimes 	if (s0 && *s0) {
6028fae3551SRodney W. Grimes 		i = strlen(s0) + strlen(s1) + 1 + 1;
6038fae3551SRodney W. Grimes 		if ((cp = malloc(i)) == NULL)
604bcb1d846SPhilippe Charnier 			errx(1, "malloc failed");
6058fae3551SRodney W. Grimes 		(void)snprintf(cp, i, "%s,%s", s0, s1);
6068fae3551SRodney W. Grimes 	} else
6078fae3551SRodney W. Grimes 		cp = strdup(s1);
6088fae3551SRodney W. Grimes 
6098fae3551SRodney W. Grimes 	if (s0)
6108fae3551SRodney W. Grimes 		free(s0);
6118fae3551SRodney W. Grimes 	return (cp);
6128fae3551SRodney W. Grimes }
6138fae3551SRodney W. Grimes 
6148fae3551SRodney W. Grimes void
6158fae3551SRodney W. Grimes mangle(options, argcp, argv)
6168fae3551SRodney W. Grimes 	char *options;
6178fae3551SRodney W. Grimes 	int *argcp;
6186f5f1a6bSCraig Rodrigues 	char **argv;
6198fae3551SRodney W. Grimes {
6208fae3551SRodney W. Grimes 	char *p, *s;
6218fae3551SRodney W. Grimes 	int argc;
6228fae3551SRodney W. Grimes 
6238fae3551SRodney W. Grimes 	argc = *argcp;
6248fae3551SRodney W. Grimes 	for (s = options; (p = strsep(&s, ",")) != NULL;)
62518af6044SJoseph Koshy 		if (*p != '\0') {
626d9fa6ce7SCraig Rodrigues 			if (strcmp(p, "noauto") == 0) {
627d9fa6ce7SCraig Rodrigues 				/*
628d9fa6ce7SCraig Rodrigues 				 * Do not pass noauto option to nmount().
629d9fa6ce7SCraig Rodrigues 				 * or external mount program.  noauto is
630d9fa6ce7SCraig Rodrigues 				 * only used to prevent mounting a filesystem
631d9fa6ce7SCraig Rodrigues 				 * when 'mount -a' is specified, and is
632d9fa6ce7SCraig Rodrigues 				 * not a real mount option.
633d9fa6ce7SCraig Rodrigues 				 */
634d9fa6ce7SCraig Rodrigues 				continue;
63535d6c7f5SCraig Rodrigues 			} else if (strcmp(p, "userquota") == 0) {
63635d6c7f5SCraig Rodrigues 				continue;
63735d6c7f5SCraig Rodrigues 			} else if (strcmp(p, "groupquota") == 0) {
63835d6c7f5SCraig Rodrigues 				continue;
639d9fa6ce7SCraig Rodrigues 			} else if (*p == '-') {
6408fae3551SRodney W. Grimes 				argv[argc++] = p;
6418fae3551SRodney W. Grimes 				p = strchr(p, '=');
642adfdbe22STom Rhodes 				if (p != NULL) {
6438fae3551SRodney W. Grimes 					*p = '\0';
6448fae3551SRodney W. Grimes 					argv[argc++] = p+1;
6458fae3551SRodney W. Grimes 				}
646748e259bSCraig Rodrigues 			} else {
6476f5f1a6bSCraig Rodrigues 				argv[argc++] = strdup("-o");
6488fae3551SRodney W. Grimes 				argv[argc++] = p;
6498fae3551SRodney W. Grimes 			}
65018af6044SJoseph Koshy 		}
6518fae3551SRodney W. Grimes 
6528fae3551SRodney W. Grimes 	*argcp = argc;
6538fae3551SRodney W. Grimes }
6548fae3551SRodney W. Grimes 
65518af6044SJoseph Koshy 
65618af6044SJoseph Koshy char *
65718af6044SJoseph Koshy update_options(opts, fstab, curflags)
65818af6044SJoseph Koshy 	char *opts;
65918af6044SJoseph Koshy 	char *fstab;
66018af6044SJoseph Koshy 	int curflags;
66118af6044SJoseph Koshy {
66218af6044SJoseph Koshy 	char *o, *p;
66318af6044SJoseph Koshy 	char *cur;
66418af6044SJoseph Koshy 	char *expopt, *newopt, *tmpopt;
66518af6044SJoseph Koshy 
66618af6044SJoseph Koshy 	if (opts == NULL)
66718af6044SJoseph Koshy 		return strdup("");
66818af6044SJoseph Koshy 
66918af6044SJoseph Koshy 	/* remove meta options from list */
67018af6044SJoseph Koshy 	remopt(fstab, MOUNT_META_OPTION_FSTAB);
67118af6044SJoseph Koshy 	remopt(fstab, MOUNT_META_OPTION_CURRENT);
67218af6044SJoseph Koshy 	cur = flags2opts(curflags);
67318af6044SJoseph Koshy 
67418af6044SJoseph Koshy 	/*
67518af6044SJoseph Koshy 	 * Expand all meta-options passed to us first.
67618af6044SJoseph Koshy 	 */
67718af6044SJoseph Koshy 	expopt = NULL;
67818af6044SJoseph Koshy 	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
67918af6044SJoseph Koshy 		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
68018af6044SJoseph Koshy 			expopt = catopt(expopt, fstab);
68118af6044SJoseph Koshy 		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
68218af6044SJoseph Koshy 			expopt = catopt(expopt, cur);
68318af6044SJoseph Koshy 		else
68418af6044SJoseph Koshy 			expopt = catopt(expopt, o);
68518af6044SJoseph Koshy 	}
68618af6044SJoseph Koshy 	free(cur);
68718af6044SJoseph Koshy 	free(opts);
68818af6044SJoseph Koshy 
68918af6044SJoseph Koshy 	/*
69018af6044SJoseph Koshy 	 * Remove previous contradictory arguments. Given option "foo" we
69118af6044SJoseph Koshy 	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
69218af6044SJoseph Koshy 	 * and "foo" - so we can deal with possible options like "notice".
69318af6044SJoseph Koshy 	 */
69418af6044SJoseph Koshy 	newopt = NULL;
69518af6044SJoseph Koshy 	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
69618af6044SJoseph Koshy 		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
69718af6044SJoseph Koshy 			errx(1, "malloc failed");
69818af6044SJoseph Koshy 
69918af6044SJoseph Koshy 		strcpy(tmpopt, "no");
70018af6044SJoseph Koshy 		strcat(tmpopt, o);
70118af6044SJoseph Koshy 		remopt(newopt, tmpopt);
70218af6044SJoseph Koshy 		free(tmpopt);
70318af6044SJoseph Koshy 
70418af6044SJoseph Koshy 		if (strncmp("no", o, 2) == 0)
70518af6044SJoseph Koshy 			remopt(newopt, o+2);
70618af6044SJoseph Koshy 
70718af6044SJoseph Koshy 		newopt = catopt(newopt, o);
70818af6044SJoseph Koshy 	}
70918af6044SJoseph Koshy 	free(expopt);
71018af6044SJoseph Koshy 
71118af6044SJoseph Koshy 	return newopt;
71218af6044SJoseph Koshy }
71318af6044SJoseph Koshy 
71418af6044SJoseph Koshy void
71518af6044SJoseph Koshy remopt(string, opt)
71618af6044SJoseph Koshy 	char *string;
71718af6044SJoseph Koshy 	const char *opt;
71818af6044SJoseph Koshy {
71918af6044SJoseph Koshy 	char *o, *p, *r;
72018af6044SJoseph Koshy 
72118af6044SJoseph Koshy 	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
72218af6044SJoseph Koshy 		return;
72318af6044SJoseph Koshy 
72418af6044SJoseph Koshy 	r = string;
72518af6044SJoseph Koshy 
72618af6044SJoseph Koshy 	for (p = string; (o = strsep(&p, ",")) != NULL;) {
72718af6044SJoseph Koshy 		if (strcmp(opt, o) != 0) {
72818af6044SJoseph Koshy 			if (*r == ',' && *o != '\0')
72918af6044SJoseph Koshy 				r++;
73018af6044SJoseph Koshy 			while ((*r++ = *o++) != '\0')
73118af6044SJoseph Koshy 			    ;
73218af6044SJoseph Koshy 			*--r = ',';
73318af6044SJoseph Koshy 		}
73418af6044SJoseph Koshy 	}
73518af6044SJoseph Koshy 	*r = '\0';
73618af6044SJoseph Koshy }
73718af6044SJoseph Koshy 
7388fae3551SRodney W. Grimes void
7398fae3551SRodney W. Grimes usage()
7408fae3551SRodney W. Grimes {
7418fae3551SRodney W. Grimes 
742bcb1d846SPhilippe Charnier 	(void)fprintf(stderr, "%s\n%s\n%s\n",
7438d646af5SRuslan Ermilov "usage: mount [-adfpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
7448d646af5SRuslan Ermilov "       mount [-dfpruvw] special | node",
7458d646af5SRuslan Ermilov "       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
7468fae3551SRodney W. Grimes 	exit(1);
7478fae3551SRodney W. Grimes }
748a257a45eSJordan K. Hubbard 
749a257a45eSJordan K. Hubbard void
750a257a45eSJordan K. Hubbard putfsent(ent)
751a257a45eSJordan K. Hubbard 	const struct statfs *ent;
752a257a45eSJordan K. Hubbard {
753a257a45eSJordan K. Hubbard 	struct fstab *fst;
75418af6044SJoseph Koshy 	char *opts;
755a257a45eSJordan K. Hubbard 
75618af6044SJoseph Koshy 	opts = flags2opts(ent->f_flags);
75774cf460bSBruce Evans 	printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
75818af6044SJoseph Koshy 	    ent->f_fstypename, opts);
75918af6044SJoseph Koshy 	free(opts);
760c06fe0a0SPeter Wemm 
761fba1c154SSteve Price 	if ((fst = getfsspec(ent->f_mntfromname)))
762a257a45eSJordan K. Hubbard 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
763fba1c154SSteve Price 	else if ((fst = getfsfile(ent->f_mntonname)))
764a257a45eSJordan K. Hubbard 		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
765ab80d6faSBrian Feldman 	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
766ab80d6faSBrian Feldman 		if (strcmp(ent->f_mntonname, "/") == 0)
767a257a45eSJordan K. Hubbard 			printf("\t1 1\n");
768a257a45eSJordan K. Hubbard 		else
769ab80d6faSBrian Feldman 			printf("\t2 2\n");
770ab80d6faSBrian Feldman 	} else
771a257a45eSJordan K. Hubbard 		printf("\t0 0\n");
772a257a45eSJordan K. Hubbard }
77318af6044SJoseph Koshy 
77418af6044SJoseph Koshy 
77518af6044SJoseph Koshy char *
77618af6044SJoseph Koshy flags2opts(flags)
77718af6044SJoseph Koshy 	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 
79718af6044SJoseph Koshy 	return res;
79818af6044SJoseph Koshy }
799