mount.c revision 159473
128671Ssteve/*-
21558Srgrimes * Copyright (c) 1980, 1989, 1993, 1994
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 4. Neither the name of the University nor the names of its contributors
141558Srgrimes *    may be used to endorse or promote products derived from this software
151558Srgrimes *    without specific prior written permission.
161558Srgrimes *
171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271558Srgrimes * SUCH DAMAGE.
281558Srgrimes */
291558Srgrimes
301558Srgrimes#ifndef lint
3128671Sstevestatic const char copyright[] =
321558Srgrimes"@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
331558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
341558Srgrimes#endif /* not lint */
351558Srgrimes
361558Srgrimes#ifndef lint
3728671Ssteve#if 0
3823678Speterstatic char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
3937425Scharnier#endif
4028671Sstevestatic const char rcsid[] =
4150476Speter  "$FreeBSD: head/sbin/mount/mount.c 159473 2006-06-10 01:44:57Z jmallett $";
421558Srgrimes#endif /* not lint */
431558Srgrimes
441558Srgrimes#include <sys/param.h>
451558Srgrimes#include <sys/mount.h>
4623805Sbde#include <sys/stat.h>
471558Srgrimes#include <sys/wait.h>
481558Srgrimes
4995289Smux#include <ctype.h>
501558Srgrimes#include <err.h>
511558Srgrimes#include <errno.h>
521558Srgrimes#include <fstab.h>
53117031Sgordon#include <paths.h>
5423678Speter#include <pwd.h>
551558Srgrimes#include <signal.h>
56123242Siedowse#include <stdint.h>
571558Srgrimes#include <stdio.h>
581558Srgrimes#include <stdlib.h>
591558Srgrimes#include <string.h>
601558Srgrimes#include <unistd.h>
611558Srgrimes
6228671Ssteve#include "extern.h"
6352055Sphk#include "mntopts.h"
641558Srgrimes#include "pathnames.h"
651558Srgrimes
6646619Sjkoshy/* `meta' options */
67125933Sgrog#define MOUNT_META_OPTION_FSTAB		"fstab"
6846619Sjkoshy#define MOUNT_META_OPTION_CURRENT	"current"
6946619Sjkoshy
7023805Sbdeint debug, fstab_style, verbose;
711558Srgrimes
7292882Simpchar   *catopt(char *, const char *);
7392882Simpstruct statfs *getmntpt(const char *);
7492882Simpint	hasopt(const char *, const char *);
7592882Simpint	ismounted(struct fstab *, struct statfs *, int);
7692882Simpint	isremountable(const char *);
77159473Sjmallettvoid	mangle(char *, int *, char *[]);
7892882Simpchar   *update_options(char *, char *, int);
7992882Simpint	mountfs(const char *, const char *, const char *,
8092882Simp			int, const char *, const char *);
8192882Simpvoid	remopt(char *, const char *);
8292882Simpvoid	prmount(struct statfs *);
8392882Simpvoid	putfsent(const struct statfs *);
8492882Simpvoid	usage(void);
8592882Simpchar   *flags2opts(int);
861558Srgrimes
8737425Scharnier/* Map from mount options to printable formats. */
881558Srgrimesstatic struct opt {
891558Srgrimes	int o_opt;
901558Srgrimes	const char *o_name;
911558Srgrimes} optnames[] = {
921558Srgrimes	{ MNT_ASYNC,		"asynchronous" },
931558Srgrimes	{ MNT_EXPORTED,		"NFS exported" },
941558Srgrimes	{ MNT_LOCAL,		"local" },
9518007Sdg	{ MNT_NOATIME,		"noatime" },
961558Srgrimes	{ MNT_NOEXEC,		"noexec" },
971558Srgrimes	{ MNT_NOSUID,		"nosuid" },
9835105Swosch	{ MNT_NOSYMFOLLOW,	"nosymfollow" },
991558Srgrimes	{ MNT_QUOTA,		"with quotas" },
1001558Srgrimes	{ MNT_RDONLY,		"read-only" },
1011558Srgrimes	{ MNT_SYNCHRONOUS,	"synchronous" },
1021558Srgrimes	{ MNT_UNION,		"union" },
10329890Skato	{ MNT_NOCLUSTERR,	"noclusterr" },
10429890Skato	{ MNT_NOCLUSTERW,	"noclusterw" },
10531144Sjulian	{ MNT_SUIDDIR,		"suiddir" },
10634266Sjulian	{ MNT_SOFTDEP,		"soft-updates" },
107101205Srwatson	{ MNT_MULTILABEL,	"multilabel" },
108105118Srwatson	{ MNT_ACLS,		"acls" },
10946619Sjkoshy	{ 0, NULL }
1101558Srgrimes};
1111558Srgrimes
11228671Ssteve/*
11328671Ssteve * List of VFS types that can be remounted without becoming mounted on top
11428671Ssteve * of each other.
11528671Ssteve * XXX Is this list correct?
11628671Ssteve */
11728671Sstevestatic const char *
11828671Ssteveremountable_fs_names[] = {
11969056Sphantom	"ufs", "ffs", "ext2fs",
12028671Ssteve	0
12128671Ssteve};
12228671Ssteve
123158400Smaximstatic const char userquotaeq[] = "userquota=";
124158400Smaximstatic const char groupquotaeq[] = "groupquota=";
125158400Smaxim
126152344Srodrigcstatic int
127152344Srodrigcuse_mountprog(const char *vfstype)
128152344Srodrigc{
129152344Srodrigc	/* XXX: We need to get away from implementing external mount
130152344Srodrigc	 *      programs for every filesystem, and move towards having
131152344Srodrigc	 *	each filesystem properly implement the nmount() system call.
132152344Srodrigc	 */
133152344Srodrigc	unsigned int i;
134152344Srodrigc	const char *fs[] = {
135152778Savatar	"cd9660", "mfs", "msdosfs", "nfs", "nfs4", "ntfs",
136153360Srodrigc	"nwfs", "nullfs", "portalfs", "smbfs", "udf", "umapfs",
137152344Srodrigc	"unionfs",
138152344Srodrigc	NULL
139152344Srodrigc	};
140152344Srodrigc
141159473Sjmallett	for (i = 0; fs[i] != NULL; ++i) {
142152344Srodrigc		if (strcmp(vfstype, fs[i]) == 0)
143159473Sjmallett			return (1);
144152344Srodrigc	}
145152344Srodrigc
146159473Sjmallett	return (0);
147152344Srodrigc}
148152344Srodrigc
149152344Srodrigcstatic int
150159473Sjmallettexec_mountprog(const char *name, const char *execname, char *const argv[])
151152344Srodrigc{
152152344Srodrigc	pid_t pid;
153152344Srodrigc	int status;
154152344Srodrigc
155152344Srodrigc	switch (pid = fork()) {
156152344Srodrigc	case -1:				/* Error. */
157152344Srodrigc		warn("fork");
158152344Srodrigc		exit (1);
159152344Srodrigc	case 0:					/* Child. */
160152344Srodrigc		/* Go find an executable. */
161152344Srodrigc		execvP(execname, _PATH_SYSPATH, argv);
162152344Srodrigc		if (errno == ENOENT) {
163152344Srodrigc			warn("exec %s not found in %s", execname,
164152344Srodrigc			    _PATH_SYSPATH);
165152344Srodrigc		}
166152344Srodrigc		exit(1);
167152344Srodrigc	default:				/* Parent. */
168152344Srodrigc		if (waitpid(pid, &status, 0) < 0) {
169152344Srodrigc			warn("waitpid");
170152344Srodrigc			return (1);
171152344Srodrigc		}
172152344Srodrigc
173152344Srodrigc		if (WIFEXITED(status)) {
174152344Srodrigc			if (WEXITSTATUS(status) != 0)
175152344Srodrigc				return (WEXITSTATUS(status));
176152344Srodrigc		} else if (WIFSIGNALED(status)) {
177152344Srodrigc			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
178152344Srodrigc			return (1);
179152344Srodrigc		}
180152344Srodrigc		break;
181152344Srodrigc	}
182152344Srodrigc
183152344Srodrigc	return (0);
184152344Srodrigc}
185152344Srodrigc
1861558Srgrimesint
187151042Srodrigcmain(int argc, char *argv[])
1881558Srgrimes{
18923678Speter	const char *mntfromname, **vfslist, *vfstype;
1901558Srgrimes	struct fstab *fs;
1911558Srgrimes	struct statfs *mntbuf;
1921558Srgrimes	FILE *mountdfp;
1931558Srgrimes	pid_t pid;
194159472Sjmallett	int all, ch, i, init_flags, mntsize, rval, have_fstab;
19595289Smux	char *cp, *ep, *options;
1961558Srgrimes
197159472Sjmallett	options = strdup("noro");
198159472Sjmallett	if (options == NULL)
199159472Sjmallett		errx(1, "malloc failed");
200159472Sjmallett
2011558Srgrimes	all = init_flags = 0;
2021558Srgrimes	vfslist = NULL;
2031558Srgrimes	vfstype = "ufs";
204113220Smdodd	while ((ch = getopt(argc, argv, "adF:fo:prwt:uv")) != -1)
2051558Srgrimes		switch (ch) {
2061558Srgrimes		case 'a':
2071558Srgrimes			all = 1;
2081558Srgrimes			break;
2091558Srgrimes		case 'd':
2101558Srgrimes			debug = 1;
2111558Srgrimes			break;
212113220Smdodd		case 'F':
213113220Smdodd			setfstab(optarg);
214113220Smdodd			break;
2151558Srgrimes		case 'f':
2161558Srgrimes			init_flags |= MNT_FORCE;
2171558Srgrimes			break;
2181558Srgrimes		case 'o':
219159472Sjmallett			options = catopt(options, optarg);
2201558Srgrimes			break;
22123805Sbde		case 'p':
22223805Sbde			fstab_style = 1;
22323805Sbde			verbose = 1;
22423805Sbde			break;
2251558Srgrimes		case 'r':
22676198Sdd			options = catopt(options, "ro");
2271558Srgrimes			break;
2281558Srgrimes		case 't':
2291558Srgrimes			if (vfslist != NULL)
23037425Scharnier				errx(1, "only one -t option may be specified");
2311558Srgrimes			vfslist = makevfslist(optarg);
2321558Srgrimes			vfstype = optarg;
2331558Srgrimes			break;
2341558Srgrimes		case 'u':
2351558Srgrimes			init_flags |= MNT_UPDATE;
2361558Srgrimes			break;
2371558Srgrimes		case 'v':
2381558Srgrimes			verbose = 1;
2391558Srgrimes			break;
2401558Srgrimes		case 'w':
24176198Sdd			options = catopt(options, "noro");
2421558Srgrimes			break;
2431558Srgrimes		case '?':
2441558Srgrimes		default:
2451558Srgrimes			usage();
2461558Srgrimes			/* NOTREACHED */
2471558Srgrimes		}
2481558Srgrimes	argc -= optind;
2491558Srgrimes	argv += optind;
2501558Srgrimes
2511558Srgrimes#define	BADTYPE(type)							\
2521558Srgrimes	(strcmp(type, FSTAB_RO) &&					\
2531558Srgrimes	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
2541558Srgrimes
2551558Srgrimes	rval = 0;
2561558Srgrimes	switch (argc) {
2571558Srgrimes	case 0:
25828671Ssteve		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
25928671Ssteve			err(1, "getmntinfo");
26028671Ssteve		if (all) {
2611558Srgrimes			while ((fs = getfsent()) != NULL) {
2621558Srgrimes				if (BADTYPE(fs->fs_type))
2631558Srgrimes					continue;
26423678Speter				if (checkvfsname(fs->fs_vfstype, vfslist))
2651558Srgrimes					continue;
26623678Speter				if (hasopt(fs->fs_mntops, "noauto"))
26710288Sdg					continue;
26844811Sbde				if (!(init_flags & MNT_UPDATE) &&
26944811Sbde				    ismounted(fs, mntbuf, mntsize))
27028671Ssteve					continue;
271125194Sguido				options = update_options(options, fs->fs_mntops,
272125194Sguido				    mntbuf->f_flags);
2731558Srgrimes				if (mountfs(fs->fs_vfstype, fs->fs_spec,
27423805Sbde				    fs->fs_file, init_flags, options,
27523805Sbde				    fs->fs_mntops))
27623805Sbde					rval = 1;
2771558Srgrimes			}
27828671Ssteve		} else if (fstab_style) {
27917243Sjkh			for (i = 0; i < mntsize; i++) {
28023678Speter				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
28117243Sjkh					continue;
28223805Sbde				putfsent(&mntbuf[i]);
28317243Sjkh			}
28423805Sbde		} else {
2851558Srgrimes			for (i = 0; i < mntsize; i++) {
28623805Sbde				if (checkvfsname(mntbuf[i].f_fstypename,
28723805Sbde				    vfslist))
2881558Srgrimes					continue;
28923678Speter				prmount(&mntbuf[i]);
2901558Srgrimes			}
2911558Srgrimes		}
2921558Srgrimes		exit(rval);
2931558Srgrimes	case 1:
2941558Srgrimes		if (vfslist != NULL)
2951558Srgrimes			usage();
2961558Srgrimes
297124201Sanholt		rmslashes(*argv, *argv);
2981558Srgrimes		if (init_flags & MNT_UPDATE) {
29952678Sgreen			mntfromname = NULL;
30052678Sgreen			have_fstab = 0;
3011558Srgrimes			if ((mntbuf = getmntpt(*argv)) == NULL)
30252678Sgreen				errx(1, "not currently mounted %s", *argv);
30352678Sgreen			/*
30452678Sgreen			 * Only get the mntflags from fstab if both mntpoint
30552678Sgreen			 * and mntspec are identical. Also handle the special
30652678Sgreen			 * case where just '/' is mounted and 'spec' is not
30752678Sgreen			 * identical with the one from fstab ('/dev' is missing
30852678Sgreen			 * in the spec-string at boot-time).
30952678Sgreen			 */
31046619Sjkoshy			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
31152678Sgreen				if (strcmp(fs->fs_spec,
31252678Sgreen				    mntbuf->f_mntfromname) == 0 &&
31352678Sgreen				    strcmp(fs->fs_file,
31452678Sgreen				    mntbuf->f_mntonname) == 0) {
31552678Sgreen					have_fstab = 1;
31652678Sgreen					mntfromname = mntbuf->f_mntfromname;
31752678Sgreen				} else if (argv[0][0] == '/' &&
31852678Sgreen				    argv[0][1] == '\0') {
31952678Sgreen					fs = getfsfile("/");
32052678Sgreen					have_fstab = 1;
32152678Sgreen					mntfromname = fs->fs_spec;
32252678Sgreen				}
32352678Sgreen			}
32452678Sgreen			if (have_fstab) {
32546619Sjkoshy				options = update_options(options, fs->fs_mntops,
32646619Sjkoshy				    mntbuf->f_flags);
32746619Sjkoshy			} else {
32823678Speter				mntfromname = mntbuf->f_mntfromname;
32946619Sjkoshy				options = update_options(options, NULL,
33046619Sjkoshy				    mntbuf->f_flags);
33146619Sjkoshy			}
33223678Speter			rval = mountfs(mntbuf->f_fstypename, mntfromname,
33323678Speter			    mntbuf->f_mntonname, init_flags, options, 0);
33423678Speter			break;
3351558Srgrimes		}
33623678Speter		if ((fs = getfsfile(*argv)) == NULL &&
33723678Speter		    (fs = getfsspec(*argv)) == NULL)
338102231Strhodes			errx(1, "%s: unknown special file or file system",
33923678Speter			    *argv);
34023678Speter		if (BADTYPE(fs->fs_type))
341102231Strhodes			errx(1, "%s has unknown file system type",
34223678Speter			    *argv);
34323678Speter		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
34423678Speter		    init_flags, options, fs->fs_mntops);
3451558Srgrimes		break;
3461558Srgrimes	case 2:
3471558Srgrimes		/*
34852678Sgreen		 * If -t flag has not been specified, the path cannot be
34995289Smux		 * found, spec contains either a ':' or a '@', then assume
350102231Strhodes		 * that an NFS file system is being specified ala Sun.
35195289Smux		 * Check if the hostname contains only allowed characters
35295289Smux		 * to reduce false positives.  IPv6 addresses containing
35395289Smux		 * ':' will be correctly parsed only if the separator is '@'.
35495289Smux		 * The definition of a valid hostname is taken from RFC 1034.
3551558Srgrimes		 */
356123162Siedowse		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
357123162Siedowse		    (ep = strchr(argv[0], ':')) != NULL)) {
35895318Smux			if (*ep == '@') {
35995318Smux				cp = ep + 1;
36095318Smux				ep = cp + strlen(cp);
36195318Smux			} else
36295318Smux				cp = argv[0];
36395289Smux			while (cp != ep) {
36495289Smux				if (!isdigit(*cp) && !isalpha(*cp) &&
36595289Smux				    *cp != '.' && *cp != '-' && *cp != ':')
36695289Smux					break;
36795289Smux				cp++;
36895289Smux			}
36995289Smux			if (cp == ep)
37095289Smux				vfstype = "nfs";
37195289Smux		}
3721558Srgrimes		rval = mountfs(vfstype,
3731558Srgrimes		    argv[0], argv[1], init_flags, options, NULL);
3741558Srgrimes		break;
3751558Srgrimes	default:
3761558Srgrimes		usage();
3771558Srgrimes		/* NOTREACHED */
3781558Srgrimes	}
3791558Srgrimes
3801558Srgrimes	/*
3811558Srgrimes	 * If the mount was successfully, and done by root, tell mountd the
3821558Srgrimes	 * good news.  Pid checks are probably unnecessary, but don't hurt.
3831558Srgrimes	 */
3841558Srgrimes	if (rval == 0 && getuid() == 0 &&
3851558Srgrimes	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
38628671Ssteve		if (fscanf(mountdfp, "%d", &pid) == 1 &&
3871558Srgrimes		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
3881558Srgrimes			err(1, "signal mountd");
3891558Srgrimes		(void)fclose(mountdfp);
3901558Srgrimes	}
3911558Srgrimes
3921558Srgrimes	exit(rval);
3931558Srgrimes}
3941558Srgrimes
3951558Srgrimesint
396151042Srodrigcismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
39728671Ssteve{
398154512Spjd	char realfsfile[PATH_MAX];
39928671Ssteve	int i;
40028671Ssteve
40128671Ssteve	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
402102231Strhodes		/* the root file system can always be remounted */
40328671Ssteve		return (0);
40428671Ssteve
405154512Spjd	/* The user may have specified a symlink in fstab, resolve the path */
406154512Spjd	if (realpath(fs->fs_file, realfsfile) == NULL) {
407154512Spjd		/* Cannot resolve the path, use original one */
408154512Spjd		strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
409154512Spjd	}
410154512Spjd
41128671Ssteve	for (i = mntsize - 1; i >= 0; --i)
412154512Spjd		if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
41328671Ssteve		    (!isremountable(fs->fs_vfstype) ||
41428671Ssteve		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
41528671Ssteve			return (1);
41628671Ssteve	return (0);
41728671Ssteve}
41828671Ssteve
41928671Ssteveint
420151042Srodrigcisremountable(const char *vfsname)
42128671Ssteve{
42228671Ssteve	const char **cp;
42328671Ssteve
42428671Ssteve	for (cp = remountable_fs_names; *cp; cp++)
42528671Ssteve		if (strcmp(*cp, vfsname) == 0)
42628671Ssteve			return (1);
42728671Ssteve	return (0);
42828671Ssteve}
42928671Ssteve
43028671Ssteveint
431151042Srodrigchasopt(const char *mntopts, const char *option)
43223678Speter{
43323678Speter	int negative, found;
43423678Speter	char *opt, *optbuf;
43523678Speter
43623678Speter	if (option[0] == 'n' && option[1] == 'o') {
43723678Speter		negative = 1;
43823678Speter		option += 2;
43923678Speter	} else
44023678Speter		negative = 0;
44123678Speter	optbuf = strdup(mntopts);
44223678Speter	found = 0;
44323678Speter	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
44423678Speter		if (opt[0] == 'n' && opt[1] == 'o') {
44523678Speter			if (!strcasecmp(opt + 2, option))
44623678Speter				found = negative;
44723678Speter		} else if (!strcasecmp(opt, option))
44823678Speter			found = !negative;
44923678Speter	}
45023678Speter	free(optbuf);
45123678Speter	return (found);
45223678Speter}
45323678Speter
45423678Speterint
455151042Srodrigcmountfs(const char *vfstype, const char *spec, const char *name, int flags,
456151042Srodrigc	const char *options, const char *mntopts)
4571558Srgrimes{
458152344Srodrigc	char *argv[100];
4591558Srgrimes	struct statfs sf;
460152344Srodrigc	int argc, i, ret;
461118580Simp	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
4621558Srgrimes
46352055Sphk	/* resolve the mountpoint with realpath(3) */
46452055Sphk	(void)checkpath(name, mntpath);
4651558Srgrimes	name = mntpath;
4661558Srgrimes
46723678Speter	if (mntopts == NULL)
46823678Speter		mntopts = "";
4691558Srgrimes	optbuf = catopt(strdup(mntopts), options);
4701558Srgrimes
4711558Srgrimes	if (strcmp(name, "/") == 0)
4721558Srgrimes		flags |= MNT_UPDATE;
4731558Srgrimes	if (flags & MNT_FORCE)
4741558Srgrimes		optbuf = catopt(optbuf, "force");
4751558Srgrimes	if (flags & MNT_RDONLY)
4761558Srgrimes		optbuf = catopt(optbuf, "ro");
4771558Srgrimes	/*
4781558Srgrimes	 * XXX
4791558Srgrimes	 * The mount_mfs (newfs) command uses -o to select the
48037425Scharnier	 * optimization mode.  We don't pass the default "-o rw"
4811558Srgrimes	 * for that reason.
4821558Srgrimes	 */
4831558Srgrimes	if (flags & MNT_UPDATE)
4841558Srgrimes		optbuf = catopt(optbuf, "update");
4851558Srgrimes
48677577Sru	/* Compatibility glue. */
48777577Sru	if (strcmp(vfstype, "msdos") == 0)
48877577Sru		vfstype = "msdosfs";
48977577Sru
490144133Scperciva	/* Construct the name of the appropriate mount command */
491144133Scperciva	(void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
492144133Scperciva
4931558Srgrimes	argc = 0;
494144133Scperciva	argv[argc++] = execname;
4951558Srgrimes	mangle(optbuf, &argc, argv);
496152344Srodrigc	argv[argc++] = strdup(spec);
497152344Srodrigc	argv[argc++] = strdup(name);
4981558Srgrimes	argv[argc] = NULL;
4991558Srgrimes
5001558Srgrimes	if (debug) {
5011558Srgrimes		(void)printf("exec: mount_%s", vfstype);
5021558Srgrimes		for (i = 1; i < argc; i++)
5031558Srgrimes			(void)printf(" %s", argv[i]);
5041558Srgrimes		(void)printf("\n");
5051558Srgrimes		return (0);
5061558Srgrimes	}
5071558Srgrimes
508152668Srodrigc	if (use_mountprog(vfstype)) {
509152344Srodrigc		ret = exec_mountprog(name, execname, argv);
510152344Srodrigc	} else {
511152344Srodrigc		ret = mount_fs(vfstype, argc, argv);
512152344Srodrigc	}
5131558Srgrimes
514152344Srodrigc	free(optbuf);
5151558Srgrimes
516152344Srodrigc	if (verbose) {
517152344Srodrigc		if (statfs(name, &sf) < 0) {
518152344Srodrigc			warn("statfs %s", name);
5191558Srgrimes			return (1);
5201558Srgrimes		}
521152344Srodrigc		if (fstab_style)
522152344Srodrigc			putfsent(&sf);
523152344Srodrigc		else
524152344Srodrigc			prmount(&sf);
5251558Srgrimes	}
5261558Srgrimes
5271558Srgrimes	return (0);
5281558Srgrimes}
5291558Srgrimes
5301558Srgrimesvoid
531151042Srodrigcprmount(struct statfs *sfp)
53223678Speter{
533151043Srodrigc	int flags;
534151043Srodrigc	unsigned int i;
5351558Srgrimes	struct opt *o;
53623678Speter	struct passwd *pw;
5371558Srgrimes
53852036Sn_hibma	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
53952036Sn_hibma	    sfp->f_fstypename);
5401558Srgrimes
54123678Speter	flags = sfp->f_flags & MNT_VISFLAGMASK;
54252036Sn_hibma	for (o = optnames; flags && o->o_opt; o++)
5431558Srgrimes		if (flags & o->o_opt) {
54452036Sn_hibma			(void)printf(", %s", o->o_name);
5451558Srgrimes			flags &= ~o->o_opt;
5461558Srgrimes		}
547125339Spjd	/*
548125339Spjd	 * Inform when file system is mounted by an unprivileged user
549125339Spjd	 * or privileged non-root user.
550125339Spjd	 */
551125365Snectar	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
55252036Sn_hibma		(void)printf(", mounted by ");
55323678Speter		if ((pw = getpwuid(sfp->f_owner)) != NULL)
55423678Speter			(void)printf("%s", pw->pw_name);
55523678Speter		else
55623678Speter			(void)printf("%d", sfp->f_owner);
55723678Speter	}
55865023Ssheldonh	if (verbose) {
55965023Ssheldonh		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
560123242Siedowse			(void)printf(", writes: sync %ju async %ju",
561123242Siedowse			    (uintmax_t)sfp->f_syncwrites,
562123242Siedowse			    (uintmax_t)sfp->f_asyncwrites);
56365023Ssheldonh		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
564123242Siedowse			(void)printf(", reads: sync %ju async %ju",
565123242Siedowse			    (uintmax_t)sfp->f_syncreads,
566123242Siedowse			    (uintmax_t)sfp->f_asyncreads);
567123162Siedowse		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
568123162Siedowse			printf(", fsid ");
569123162Siedowse			for (i = 0; i < sizeof(sfp->f_fsid); i++)
570123162Siedowse				printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
571123162Siedowse		}
57265023Ssheldonh	}
57352036Sn_hibma	(void)printf(")\n");
5741558Srgrimes}
5751558Srgrimes
5761558Srgrimesstruct statfs *
577151042Srodrigcgetmntpt(const char *name)
5781558Srgrimes{
5791558Srgrimes	struct statfs *mntbuf;
5801558Srgrimes	int i, mntsize;
5811558Srgrimes
5821558Srgrimes	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
58352678Sgreen	for (i = mntsize - 1; i >= 0; i--) {
5841558Srgrimes		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
5851558Srgrimes		    strcmp(mntbuf[i].f_mntonname, name) == 0)
5861558Srgrimes			return (&mntbuf[i]);
58752678Sgreen	}
5881558Srgrimes	return (NULL);
5891558Srgrimes}
5901558Srgrimes
5911558Srgrimeschar *
592151042Srodrigccatopt(char *s0, const char *s1)
5931558Srgrimes{
5941558Srgrimes	size_t i;
5951558Srgrimes	char *cp;
5961558Srgrimes
59746619Sjkoshy	if (s1 == NULL || *s1 == '\0')
598159473Sjmallett		return (s0);
59946619Sjkoshy
6001558Srgrimes	if (s0 && *s0) {
6011558Srgrimes		i = strlen(s0) + strlen(s1) + 1 + 1;
6021558Srgrimes		if ((cp = malloc(i)) == NULL)
60337425Scharnier			errx(1, "malloc failed");
6041558Srgrimes		(void)snprintf(cp, i, "%s,%s", s0, s1);
6051558Srgrimes	} else
6061558Srgrimes		cp = strdup(s1);
6071558Srgrimes
6081558Srgrimes	if (s0)
6091558Srgrimes		free(s0);
6101558Srgrimes	return (cp);
6111558Srgrimes}
6121558Srgrimes
6131558Srgrimesvoid
614159473Sjmallettmangle(char *options, int *argcp, char *argv[])
6151558Srgrimes{
6161558Srgrimes	char *p, *s;
6171558Srgrimes	int argc;
6181558Srgrimes
6191558Srgrimes	argc = *argcp;
6201558Srgrimes	for (s = options; (p = strsep(&s, ",")) != NULL;)
62146619Sjkoshy		if (*p != '\0') {
622152465Srodrigc			if (strcmp(p, "noauto") == 0) {
623152465Srodrigc				/*
624152465Srodrigc				 * Do not pass noauto option to nmount().
625152465Srodrigc				 * or external mount program.  noauto is
626152465Srodrigc				 * only used to prevent mounting a filesystem
627152465Srodrigc				 * when 'mount -a' is specified, and is
628152465Srodrigc				 * not a real mount option.
629152465Srodrigc				 */
630152465Srodrigc				continue;
631152732Srodrigc			} else if (strcmp(p, "userquota") == 0) {
632152732Srodrigc				continue;
633158400Smaxim			} else if (strncmp(p, userquotaeq,
634158400Smaxim			    sizeof(userquotaeq) - 1) == 0) {
635158400Smaxim				continue;
636152732Srodrigc			} else if (strcmp(p, "groupquota") == 0) {
637152732Srodrigc				continue;
638158400Smaxim			} else if (strncmp(p, groupquotaeq,
639158400Smaxim			    sizeof(groupquotaeq) - 1) == 0) {
640158400Smaxim				continue;
641152465Srodrigc			} else if (*p == '-') {
6421558Srgrimes				argv[argc++] = p;
6431558Srgrimes				p = strchr(p, '=');
644123268Strhodes				if (p != NULL) {
6451558Srgrimes					*p = '\0';
6461558Srgrimes					argv[argc++] = p+1;
6471558Srgrimes				}
648153038Srodrigc			} else {
649152344Srodrigc				argv[argc++] = strdup("-o");
6501558Srgrimes				argv[argc++] = p;
6511558Srgrimes			}
65246619Sjkoshy		}
6531558Srgrimes
6541558Srgrimes	*argcp = argc;
6551558Srgrimes}
6561558Srgrimes
65746619Sjkoshy
65846619Sjkoshychar *
659159473Sjmallettupdate_options(char *opts, char *fstab, int curflags)
66046619Sjkoshy{
66146619Sjkoshy	char *o, *p;
66246619Sjkoshy	char *cur;
66346619Sjkoshy	char *expopt, *newopt, *tmpopt;
66446619Sjkoshy
66546619Sjkoshy	if (opts == NULL)
666159473Sjmallett		return (strdup(""));
66746619Sjkoshy
66846619Sjkoshy	/* remove meta options from list */
66946619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_FSTAB);
67046619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_CURRENT);
67146619Sjkoshy	cur = flags2opts(curflags);
67246619Sjkoshy
67346619Sjkoshy	/*
67446619Sjkoshy	 * Expand all meta-options passed to us first.
67546619Sjkoshy	 */
67646619Sjkoshy	expopt = NULL;
67746619Sjkoshy	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
67846619Sjkoshy		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
67946619Sjkoshy			expopt = catopt(expopt, fstab);
68046619Sjkoshy		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
68146619Sjkoshy			expopt = catopt(expopt, cur);
68246619Sjkoshy		else
68346619Sjkoshy			expopt = catopt(expopt, o);
68446619Sjkoshy	}
68546619Sjkoshy	free(cur);
68646619Sjkoshy	free(opts);
68746619Sjkoshy
68846619Sjkoshy	/*
68946619Sjkoshy	 * Remove previous contradictory arguments. Given option "foo" we
69046619Sjkoshy	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
69146619Sjkoshy	 * and "foo" - so we can deal with possible options like "notice".
69246619Sjkoshy	 */
69346619Sjkoshy	newopt = NULL;
69446619Sjkoshy	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
69546619Sjkoshy		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
69646619Sjkoshy			errx(1, "malloc failed");
697125933Sgrog
69846619Sjkoshy		strcpy(tmpopt, "no");
69946619Sjkoshy		strcat(tmpopt, o);
70046619Sjkoshy		remopt(newopt, tmpopt);
70146619Sjkoshy		free(tmpopt);
70246619Sjkoshy
70346619Sjkoshy		if (strncmp("no", o, 2) == 0)
70446619Sjkoshy			remopt(newopt, o+2);
70546619Sjkoshy
70646619Sjkoshy		newopt = catopt(newopt, o);
70746619Sjkoshy	}
70846619Sjkoshy	free(expopt);
70946619Sjkoshy
710159473Sjmallett	return (newopt);
71146619Sjkoshy}
71246619Sjkoshy
7131558Srgrimesvoid
714159473Sjmallettremopt(char *string, const char *opt)
71546619Sjkoshy{
71646619Sjkoshy	char *o, *p, *r;
71746619Sjkoshy
71846619Sjkoshy	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
71946619Sjkoshy		return;
72046619Sjkoshy
72146619Sjkoshy	r = string;
72246619Sjkoshy
72346619Sjkoshy	for (p = string; (o = strsep(&p, ",")) != NULL;) {
72446619Sjkoshy		if (strcmp(opt, o) != 0) {
72546619Sjkoshy			if (*r == ',' && *o != '\0')
72646619Sjkoshy				r++;
72746619Sjkoshy			while ((*r++ = *o++) != '\0')
72846619Sjkoshy			    ;
72946619Sjkoshy			*--r = ',';
73046619Sjkoshy		}
73146619Sjkoshy	}
73246619Sjkoshy	*r = '\0';
73346619Sjkoshy}
73446619Sjkoshy
73546619Sjkoshyvoid
736159473Sjmallettusage(void)
7371558Srgrimes{
7381558Srgrimes
73937425Scharnier	(void)fprintf(stderr, "%s\n%s\n%s\n",
740141611Sru"usage: mount [-adfpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
741141611Sru"       mount [-dfpruvw] special | node",
742141611Sru"       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
7431558Srgrimes	exit(1);
7441558Srgrimes}
74517243Sjkh
74617243Sjkhvoid
747159473Sjmallettputfsent(const struct statfs *ent)
74817243Sjkh{
74923805Sbde	struct fstab *fst;
75046619Sjkoshy	char *opts;
751125933Sgrog
75246619Sjkoshy	opts = flags2opts(ent->f_flags);
753156229Skeramida
754156229Skeramida	/*
755156229Skeramida	 * "rw" is not a real mount option; this is why we print NULL as "rw"
756156229Skeramida	 * if opts is still NULL here.
757156229Skeramida	 */
75823805Sbde	printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
759156229Skeramida	    ent->f_fstypename, opts ? opts : "rw");
76046619Sjkoshy	free(opts);
76117243Sjkh
76228671Ssteve	if ((fst = getfsspec(ent->f_mntfromname)))
76323805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
76428671Ssteve	else if ((fst = getfsfile(ent->f_mntonname)))
76523805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
76656038Sgreen	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
76756038Sgreen		if (strcmp(ent->f_mntonname, "/") == 0)
76856038Sgreen			printf("\t1 1\n");
76956038Sgreen		else
77056038Sgreen			printf("\t2 2\n");
77156038Sgreen	} else
77223805Sbde		printf("\t0 0\n");
77317243Sjkh}
77446619Sjkoshy
77546619Sjkoshy
77646619Sjkoshychar *
777159473Sjmallettflags2opts(int flags)
77846619Sjkoshy{
77946619Sjkoshy	char *res;
78046619Sjkoshy
78146619Sjkoshy	res = NULL;
78246619Sjkoshy
783155997Srodrigc	if (flags & MNT_RDONLY)		res = catopt(res, "ro");
78446619Sjkoshy	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
78546619Sjkoshy	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
78646619Sjkoshy	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
78746619Sjkoshy	if (flags & MNT_UNION)		res = catopt(res, "union");
78846619Sjkoshy	if (flags & MNT_ASYNC)		res = catopt(res, "async");
78946619Sjkoshy	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
79046619Sjkoshy	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
79146619Sjkoshy	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
79246619Sjkoshy	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
79346619Sjkoshy	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
794101205Srwatson	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
795105118Srwatson	if (flags & MNT_ACLS)		res = catopt(res, "acls");
79646619Sjkoshy
797159473Sjmallett	return (res);
79846619Sjkoshy}
799