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";
3428671Ssteve#if 0
3523678Speterstatic char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
3637425Scharnier#endif
371558Srgrimes#endif /* not lint */
381558Srgrimes
39187093Sobrien#include <sys/cdefs.h>
40187093Sobrien__FBSDID("$FreeBSD$");
41187093Sobrien
421558Srgrimes#include <sys/param.h>
431558Srgrimes#include <sys/mount.h>
4423805Sbde#include <sys/stat.h>
451558Srgrimes#include <sys/wait.h>
461558Srgrimes
4795289Smux#include <ctype.h>
481558Srgrimes#include <err.h>
491558Srgrimes#include <errno.h>
501558Srgrimes#include <fstab.h>
51117031Sgordon#include <paths.h>
5223678Speter#include <pwd.h>
531558Srgrimes#include <signal.h>
54123242Siedowse#include <stdint.h>
551558Srgrimes#include <stdio.h>
561558Srgrimes#include <stdlib.h>
571558Srgrimes#include <string.h>
581558Srgrimes#include <unistd.h>
59166439Spjd#include <libutil.h>
601558Srgrimes
6128671Ssteve#include "extern.h"
6252055Sphk#include "mntopts.h"
631558Srgrimes#include "pathnames.h"
641558Srgrimes
6546619Sjkoshy/* `meta' options */
66125933Sgrog#define MOUNT_META_OPTION_FSTAB		"fstab"
6746619Sjkoshy#define MOUNT_META_OPTION_CURRENT	"current"
6846619Sjkoshy
69227081Sedstatic int debug, fstab_style, verbose;
701558Srgrimes
71187035Sobrienstruct cpa {
72187093Sobrien	char	**a;
73187130Sobrien	ssize_t	sz;
74187035Sobrien	int	c;
75187035Sobrien};
76187035Sobrien
7792882Simpchar   *catopt(char *, const char *);
7892882Simpstruct statfs *getmntpt(const char *);
7992882Simpint	hasopt(const char *, const char *);
8092882Simpint	ismounted(struct fstab *, struct statfs *, int);
8192882Simpint	isremountable(const char *);
82187035Sobrienvoid	mangle(char *, struct cpa *);
8392882Simpchar   *update_options(char *, char *, int);
8492882Simpint	mountfs(const char *, const char *, const char *,
8592882Simp			int, const char *, const char *);
8692882Simpvoid	remopt(char *, const char *);
8792882Simpvoid	prmount(struct statfs *);
88175949Smatteovoid	putfsent(struct statfs *);
8992882Simpvoid	usage(void);
9092882Simpchar   *flags2opts(int);
911558Srgrimes
9237425Scharnier/* Map from mount options to printable formats. */
931558Srgrimesstatic struct opt {
94204840Sbz	uint64_t o_opt;
951558Srgrimes	const char *o_name;
961558Srgrimes} optnames[] = {
971558Srgrimes	{ MNT_ASYNC,		"asynchronous" },
981558Srgrimes	{ MNT_EXPORTED,		"NFS exported" },
991558Srgrimes	{ MNT_LOCAL,		"local" },
10018007Sdg	{ MNT_NOATIME,		"noatime" },
1011558Srgrimes	{ MNT_NOEXEC,		"noexec" },
1021558Srgrimes	{ MNT_NOSUID,		"nosuid" },
10335105Swosch	{ MNT_NOSYMFOLLOW,	"nosymfollow" },
1041558Srgrimes	{ MNT_QUOTA,		"with quotas" },
1051558Srgrimes	{ MNT_RDONLY,		"read-only" },
1061558Srgrimes	{ MNT_SYNCHRONOUS,	"synchronous" },
1071558Srgrimes	{ MNT_UNION,		"union" },
10829890Skato	{ MNT_NOCLUSTERR,	"noclusterr" },
10929890Skato	{ MNT_NOCLUSTERW,	"noclusterw" },
11031144Sjulian	{ MNT_SUIDDIR,		"suiddir" },
11134266Sjulian	{ MNT_SOFTDEP,		"soft-updates" },
112224294Smckusick	{ MNT_SUJ,		"journaled soft-updates" },
113101205Srwatson	{ MNT_MULTILABEL,	"multilabel" },
114105118Srwatson	{ MNT_ACLS,		"acls" },
115200796Strasz	{ MNT_NFS4ACLS,		"nfsv4acls" },
116163843Spjd	{ MNT_GJOURNAL,		"gjournal" },
117270892Strasz	{ MNT_AUTOMOUNTED,	"automounted" },
11846619Sjkoshy	{ 0, NULL }
1191558Srgrimes};
1201558Srgrimes
12128671Ssteve/*
12228671Ssteve * List of VFS types that can be remounted without becoming mounted on top
12328671Ssteve * of each other.
12428671Ssteve * XXX Is this list correct?
12528671Ssteve */
12628671Sstevestatic const char *
12728671Ssteveremountable_fs_names[] = {
12869056Sphantom	"ufs", "ffs", "ext2fs",
12928671Ssteve	0
13028671Ssteve};
13128671Ssteve
132158400Smaximstatic const char userquotaeq[] = "userquota=";
133158400Smaximstatic const char groupquotaeq[] = "groupquota=";
134158400Smaxim
135189397Srodrigcstatic char *mountprog = NULL;
136189397Srodrigc
137152344Srodrigcstatic int
138152344Srodrigcuse_mountprog(const char *vfstype)
139152344Srodrigc{
140152344Srodrigc	/* XXX: We need to get away from implementing external mount
141152344Srodrigc	 *      programs for every filesystem, and move towards having
142152344Srodrigc	 *	each filesystem properly implement the nmount() system call.
143152344Srodrigc	 */
144152344Srodrigc	unsigned int i;
145152344Srodrigc	const char *fs[] = {
146241636Sattilio	"cd9660", "mfs", "msdosfs", "nfs",
147252356Sdavide	"nullfs", "oldnfs", "smbfs", "udf", "unionfs",
148152344Srodrigc	NULL
149152344Srodrigc	};
150152344Srodrigc
151189397Srodrigc	if (mountprog != NULL)
152189397Srodrigc		return (1);
153189397Srodrigc
154159473Sjmallett	for (i = 0; fs[i] != NULL; ++i) {
155152344Srodrigc		if (strcmp(vfstype, fs[i]) == 0)
156159473Sjmallett			return (1);
157152344Srodrigc	}
158186505Sobrien
159159473Sjmallett	return (0);
160152344Srodrigc}
161152344Srodrigc
162152344Srodrigcstatic int
163159473Sjmallettexec_mountprog(const char *name, const char *execname, char *const argv[])
164152344Srodrigc{
165152344Srodrigc	pid_t pid;
166152344Srodrigc	int status;
167152344Srodrigc
168152344Srodrigc	switch (pid = fork()) {
169152344Srodrigc	case -1:				/* Error. */
170152344Srodrigc		warn("fork");
171152344Srodrigc		exit (1);
172152344Srodrigc	case 0:					/* Child. */
173152344Srodrigc		/* Go find an executable. */
174152344Srodrigc		execvP(execname, _PATH_SYSPATH, argv);
175152344Srodrigc		if (errno == ENOENT) {
176189397Srodrigc			warn("exec %s not found", execname);
177189397Srodrigc			if (execname[0] != '/') {
178189397Srodrigc				warnx("in path: %s", _PATH_SYSPATH);
179189397Srodrigc			}
180152344Srodrigc		}
181152344Srodrigc		exit(1);
182152344Srodrigc	default:				/* Parent. */
183152344Srodrigc		if (waitpid(pid, &status, 0) < 0) {
184152344Srodrigc			warn("waitpid");
185152344Srodrigc			return (1);
186152344Srodrigc		}
187152344Srodrigc
188152344Srodrigc		if (WIFEXITED(status)) {
189152344Srodrigc			if (WEXITSTATUS(status) != 0)
190152344Srodrigc				return (WEXITSTATUS(status));
191152344Srodrigc		} else if (WIFSIGNALED(status)) {
192152344Srodrigc			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
193152344Srodrigc			return (1);
194152344Srodrigc		}
195152344Srodrigc		break;
196152344Srodrigc	}
197152344Srodrigc
198152344Srodrigc	return (0);
199152344Srodrigc}
200152344Srodrigc
201163672Srustatic int
202163672Sruspecified_ro(const char *arg)
203163671Sru{
204163671Sru	char *optbuf, *opt;
205163671Sru	int ret = 0;
206163671Sru
207163671Sru	optbuf = strdup(arg);
208163671Sru	if (optbuf == NULL)
209163671Sru		 err(1, NULL);
210163671Sru
211163671Sru	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
212163671Sru		if (strcmp(opt, "ro") == 0) {
213163671Sru			ret = 1;
214163671Sru			break;
215163671Sru		}
216163671Sru	}
217163671Sru	free(optbuf);
218163671Sru	return (ret);
219163671Sru}
220163671Sru
221166439Spjdstatic void
222166439Spjdrestart_mountd(void)
223166439Spjd{
224166439Spjd	struct pidfh *pfh;
225186505Sobrien	pid_t mountdpid;
226166439Spjd
227166439Spjd	pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
228166439Spjd	if (pfh != NULL) {
229166439Spjd		/* Mountd is not running. */
230166439Spjd		pidfile_remove(pfh);
231166439Spjd		return;
232166439Spjd	}
233166439Spjd	if (errno != EEXIST) {
234166439Spjd		/* Cannot open pidfile for some reason. */
235166439Spjd		return;
236166439Spjd	}
237166439Spjd	/* We have mountd(8) PID in mountdpid varible, let's signal it. */
238166439Spjd	if (kill(mountdpid, SIGHUP) == -1)
239166439Spjd		err(1, "signal mountd");
240166439Spjd}
241166439Spjd
2421558Srgrimesint
243151042Srodrigcmain(int argc, char *argv[])
2441558Srgrimes{
24523678Speter	const char *mntfromname, **vfslist, *vfstype;
2461558Srgrimes	struct fstab *fs;
2471558Srgrimes	struct statfs *mntbuf;
248222832Sdelphij	int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
249250235Screes	int onlylate;
25095289Smux	char *cp, *ep, *options;
2511558Srgrimes
252250235Screes	all = init_flags = late = onlylate = 0;
253163671Sru	ro = 0;
254163671Sru	options = NULL;
2551558Srgrimes	vfslist = NULL;
2561558Srgrimes	vfstype = "ufs";
257253372Srmh	while ((ch = getopt(argc, argv, "adF:fLlno:prt:uvw")) != -1)
2581558Srgrimes		switch (ch) {
2591558Srgrimes		case 'a':
2601558Srgrimes			all = 1;
2611558Srgrimes			break;
2621558Srgrimes		case 'd':
2631558Srgrimes			debug = 1;
2641558Srgrimes			break;
265113220Smdodd		case 'F':
266113220Smdodd			setfstab(optarg);
267113220Smdodd			break;
2681558Srgrimes		case 'f':
2691558Srgrimes			init_flags |= MNT_FORCE;
2701558Srgrimes			break;
271250235Screes		case 'L':
272250235Screes			onlylate = 1;
273250235Screes			late = 1;
274250235Screes			break;
275160303Sdes		case 'l':
276160303Sdes			late = 1;
277160303Sdes			break;
278253372Srmh		case 'n':
279253372Srmh			/* For compatibility with the Linux version of mount. */
280253372Srmh			break;
2811558Srgrimes		case 'o':
282163671Sru			if (*optarg) {
283163671Sru				options = catopt(options, optarg);
284163671Sru				if (specified_ro(optarg))
285163671Sru					ro = 1;
286163671Sru			}
2871558Srgrimes			break;
28823805Sbde		case 'p':
28923805Sbde			fstab_style = 1;
29023805Sbde			verbose = 1;
29123805Sbde			break;
2921558Srgrimes		case 'r':
29376198Sdd			options = catopt(options, "ro");
294163671Sru			ro = 1;
2951558Srgrimes			break;
2961558Srgrimes		case 't':
2971558Srgrimes			if (vfslist != NULL)
29837425Scharnier				errx(1, "only one -t option may be specified");
2991558Srgrimes			vfslist = makevfslist(optarg);
3001558Srgrimes			vfstype = optarg;
3011558Srgrimes			break;
3021558Srgrimes		case 'u':
3031558Srgrimes			init_flags |= MNT_UPDATE;
3041558Srgrimes			break;
3051558Srgrimes		case 'v':
3061558Srgrimes			verbose = 1;
3071558Srgrimes			break;
3081558Srgrimes		case 'w':
30976198Sdd			options = catopt(options, "noro");
3101558Srgrimes			break;
3111558Srgrimes		case '?':
3121558Srgrimes		default:
3131558Srgrimes			usage();
3141558Srgrimes			/* NOTREACHED */
3151558Srgrimes		}
3161558Srgrimes	argc -= optind;
3171558Srgrimes	argv += optind;
3181558Srgrimes
3191558Srgrimes#define	BADTYPE(type)							\
3201558Srgrimes	(strcmp(type, FSTAB_RO) &&					\
3211558Srgrimes	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
3221558Srgrimes
323163671Sru	if ((init_flags & MNT_UPDATE) && (ro == 0))
324163671Sru		options = catopt(options, "noro");
325186505Sobrien
3261558Srgrimes	rval = 0;
3271558Srgrimes	switch (argc) {
3281558Srgrimes	case 0:
329224503Smckusick		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
33028671Ssteve			err(1, "getmntinfo");
33128671Ssteve		if (all) {
3321558Srgrimes			while ((fs = getfsent()) != NULL) {
3331558Srgrimes				if (BADTYPE(fs->fs_type))
3341558Srgrimes					continue;
33523678Speter				if (checkvfsname(fs->fs_vfstype, vfslist))
3361558Srgrimes					continue;
33723678Speter				if (hasopt(fs->fs_mntops, "noauto"))
33810288Sdg					continue;
339250235Screes				if (!hasopt(fs->fs_mntops, "late") && onlylate)
340250235Screes					continue;
341160303Sdes				if (hasopt(fs->fs_mntops, "late") && !late)
342160303Sdes					continue;
343222832Sdelphij				if (hasopt(fs->fs_mntops, "failok"))
344222832Sdelphij					failok = 1;
345222832Sdelphij				else
346222832Sdelphij					failok = 0;
34744811Sbde				if (!(init_flags & MNT_UPDATE) &&
34844811Sbde				    ismounted(fs, mntbuf, mntsize))
34928671Ssteve					continue;
350125194Sguido				options = update_options(options, fs->fs_mntops,
351125194Sguido				    mntbuf->f_flags);
3521558Srgrimes				if (mountfs(fs->fs_vfstype, fs->fs_spec,
35323805Sbde				    fs->fs_file, init_flags, options,
354222832Sdelphij				    fs->fs_mntops) && !failok)
35523805Sbde					rval = 1;
3561558Srgrimes			}
35728671Ssteve		} else if (fstab_style) {
35817243Sjkh			for (i = 0; i < mntsize; i++) {
35923678Speter				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
36017243Sjkh					continue;
36123805Sbde				putfsent(&mntbuf[i]);
36217243Sjkh			}
36323805Sbde		} else {
3641558Srgrimes			for (i = 0; i < mntsize; i++) {
36523805Sbde				if (checkvfsname(mntbuf[i].f_fstypename,
36623805Sbde				    vfslist))
3671558Srgrimes					continue;
368197200Spjd				if (!verbose &&
369197200Spjd				    (mntbuf[i].f_flags & MNT_IGNORE) != 0)
370197200Spjd					continue;
37123678Speter				prmount(&mntbuf[i]);
3721558Srgrimes			}
3731558Srgrimes		}
3741558Srgrimes		exit(rval);
3751558Srgrimes	case 1:
3761558Srgrimes		if (vfslist != NULL)
3771558Srgrimes			usage();
3781558Srgrimes
379124201Sanholt		rmslashes(*argv, *argv);
3801558Srgrimes		if (init_flags & MNT_UPDATE) {
38152678Sgreen			mntfromname = NULL;
38252678Sgreen			have_fstab = 0;
3831558Srgrimes			if ((mntbuf = getmntpt(*argv)) == NULL)
38452678Sgreen				errx(1, "not currently mounted %s", *argv);
38552678Sgreen			/*
38652678Sgreen			 * Only get the mntflags from fstab if both mntpoint
38752678Sgreen			 * and mntspec are identical. Also handle the special
38852678Sgreen			 * case where just '/' is mounted and 'spec' is not
38952678Sgreen			 * identical with the one from fstab ('/dev' is missing
39052678Sgreen			 * in the spec-string at boot-time).
39152678Sgreen			 */
39246619Sjkoshy			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
39352678Sgreen				if (strcmp(fs->fs_spec,
39452678Sgreen				    mntbuf->f_mntfromname) == 0 &&
39552678Sgreen				    strcmp(fs->fs_file,
39652678Sgreen				    mntbuf->f_mntonname) == 0) {
39752678Sgreen					have_fstab = 1;
39852678Sgreen					mntfromname = mntbuf->f_mntfromname;
39952678Sgreen				} else if (argv[0][0] == '/' &&
40052678Sgreen				    argv[0][1] == '\0') {
40152678Sgreen					fs = getfsfile("/");
40252678Sgreen					have_fstab = 1;
40352678Sgreen					mntfromname = fs->fs_spec;
40452678Sgreen				}
40552678Sgreen			}
40652678Sgreen			if (have_fstab) {
40746619Sjkoshy				options = update_options(options, fs->fs_mntops,
40846619Sjkoshy				    mntbuf->f_flags);
40946619Sjkoshy			} else {
41023678Speter				mntfromname = mntbuf->f_mntfromname;
41146619Sjkoshy				options = update_options(options, NULL,
41246619Sjkoshy				    mntbuf->f_flags);
41346619Sjkoshy			}
41423678Speter			rval = mountfs(mntbuf->f_fstypename, mntfromname,
41523678Speter			    mntbuf->f_mntonname, init_flags, options, 0);
41623678Speter			break;
4171558Srgrimes		}
41823678Speter		if ((fs = getfsfile(*argv)) == NULL &&
41923678Speter		    (fs = getfsspec(*argv)) == NULL)
420102231Strhodes			errx(1, "%s: unknown special file or file system",
42123678Speter			    *argv);
42223678Speter		if (BADTYPE(fs->fs_type))
423102231Strhodes			errx(1, "%s has unknown file system type",
42423678Speter			    *argv);
42523678Speter		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
42623678Speter		    init_flags, options, fs->fs_mntops);
4271558Srgrimes		break;
4281558Srgrimes	case 2:
4291558Srgrimes		/*
43052678Sgreen		 * If -t flag has not been specified, the path cannot be
43195289Smux		 * found, spec contains either a ':' or a '@', then assume
432102231Strhodes		 * that an NFS file system is being specified ala Sun.
43395289Smux		 * Check if the hostname contains only allowed characters
43495289Smux		 * to reduce false positives.  IPv6 addresses containing
43595289Smux		 * ':' will be correctly parsed only if the separator is '@'.
43695289Smux		 * The definition of a valid hostname is taken from RFC 1034.
4371558Srgrimes		 */
438123162Siedowse		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
439123162Siedowse		    (ep = strchr(argv[0], ':')) != NULL)) {
44095318Smux			if (*ep == '@') {
44195318Smux				cp = ep + 1;
44295318Smux				ep = cp + strlen(cp);
44395318Smux			} else
44495318Smux				cp = argv[0];
44595289Smux			while (cp != ep) {
44695289Smux				if (!isdigit(*cp) && !isalpha(*cp) &&
44795289Smux				    *cp != '.' && *cp != '-' && *cp != ':')
44895289Smux					break;
44995289Smux				cp++;
45095289Smux			}
45195289Smux			if (cp == ep)
45295289Smux				vfstype = "nfs";
45395289Smux		}
4541558Srgrimes		rval = mountfs(vfstype,
4551558Srgrimes		    argv[0], argv[1], init_flags, options, NULL);
4561558Srgrimes		break;
4571558Srgrimes	default:
4581558Srgrimes		usage();
4591558Srgrimes		/* NOTREACHED */
4601558Srgrimes	}
4611558Srgrimes
4621558Srgrimes	/*
4631558Srgrimes	 * If the mount was successfully, and done by root, tell mountd the
464166439Spjd	 * good news.
4651558Srgrimes	 */
466166439Spjd	if (rval == 0 && getuid() == 0)
467166439Spjd		restart_mountd();
4681558Srgrimes
4691558Srgrimes	exit(rval);
4701558Srgrimes}
4711558Srgrimes
4721558Srgrimesint
473151042Srodrigcismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
47428671Ssteve{
475154512Spjd	char realfsfile[PATH_MAX];
47628671Ssteve	int i;
47728671Ssteve
47828671Ssteve	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
479102231Strhodes		/* the root file system can always be remounted */
48028671Ssteve		return (0);
48128671Ssteve
482154512Spjd	/* The user may have specified a symlink in fstab, resolve the path */
483154512Spjd	if (realpath(fs->fs_file, realfsfile) == NULL) {
484154512Spjd		/* Cannot resolve the path, use original one */
485154512Spjd		strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
486154512Spjd	}
487154512Spjd
48828671Ssteve	for (i = mntsize - 1; i >= 0; --i)
489154512Spjd		if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
49028671Ssteve		    (!isremountable(fs->fs_vfstype) ||
49128671Ssteve		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
49228671Ssteve			return (1);
49328671Ssteve	return (0);
49428671Ssteve}
49528671Ssteve
49628671Ssteveint
497151042Srodrigcisremountable(const char *vfsname)
49828671Ssteve{
49928671Ssteve	const char **cp;
50028671Ssteve
50128671Ssteve	for (cp = remountable_fs_names; *cp; cp++)
50228671Ssteve		if (strcmp(*cp, vfsname) == 0)
50328671Ssteve			return (1);
50428671Ssteve	return (0);
50528671Ssteve}
50628671Ssteve
50728671Ssteveint
508151042Srodrigchasopt(const char *mntopts, const char *option)
50923678Speter{
51023678Speter	int negative, found;
51123678Speter	char *opt, *optbuf;
51223678Speter
51323678Speter	if (option[0] == 'n' && option[1] == 'o') {
51423678Speter		negative = 1;
51523678Speter		option += 2;
51623678Speter	} else
51723678Speter		negative = 0;
51823678Speter	optbuf = strdup(mntopts);
51923678Speter	found = 0;
52023678Speter	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
52123678Speter		if (opt[0] == 'n' && opt[1] == 'o') {
52223678Speter			if (!strcasecmp(opt + 2, option))
52323678Speter				found = negative;
52423678Speter		} else if (!strcasecmp(opt, option))
52523678Speter			found = !negative;
52623678Speter	}
52723678Speter	free(optbuf);
52823678Speter	return (found);
52923678Speter}
53023678Speter
531187035Sobrienstatic void
532187035Sobrienappend_arg(struct cpa *sa, char *arg)
533187035Sobrien{
534187130Sobrien	if (sa->c + 1 == sa->sz) {
535187130Sobrien		sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
536187130Sobrien		sa->a = realloc(sa->a, sizeof(sa->a) * sa->sz);
537187093Sobrien		if (sa->a == NULL)
538187093Sobrien			errx(1, "realloc failed");
539187093Sobrien	}
540187035Sobrien	sa->a[++sa->c] = arg;
541187035Sobrien}
542187035Sobrien
54323678Speterint
544151042Srodrigcmountfs(const char *vfstype, const char *spec, const char *name, int flags,
545151042Srodrigc	const char *options, const char *mntopts)
5461558Srgrimes{
5471558Srgrimes	struct statfs sf;
548186504Sobrien	int i, ret;
549118580Simp	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
550187130Sobrien	static struct cpa mnt_argv;
5511558Srgrimes
55252055Sphk	/* resolve the mountpoint with realpath(3) */
553230226Sjh	if (checkpath(name, mntpath) != 0) {
554230226Sjh		warn("%s", mntpath);
555230226Sjh		return (1);
556230226Sjh	}
5571558Srgrimes	name = mntpath;
5581558Srgrimes
55923678Speter	if (mntopts == NULL)
56023678Speter		mntopts = "";
5611558Srgrimes	optbuf = catopt(strdup(mntopts), options);
5621558Srgrimes
5631558Srgrimes	if (strcmp(name, "/") == 0)
5641558Srgrimes		flags |= MNT_UPDATE;
5651558Srgrimes	if (flags & MNT_FORCE)
5661558Srgrimes		optbuf = catopt(optbuf, "force");
5671558Srgrimes	if (flags & MNT_RDONLY)
5681558Srgrimes		optbuf = catopt(optbuf, "ro");
5691558Srgrimes	/*
5701558Srgrimes	 * XXX
5711558Srgrimes	 * The mount_mfs (newfs) command uses -o to select the
57237425Scharnier	 * optimization mode.  We don't pass the default "-o rw"
5731558Srgrimes	 * for that reason.
5741558Srgrimes	 */
5751558Srgrimes	if (flags & MNT_UPDATE)
5761558Srgrimes		optbuf = catopt(optbuf, "update");
5771558Srgrimes
57877577Sru	/* Compatibility glue. */
579166526Srodrigc	if (strcmp(vfstype, "msdos") == 0) {
580166526Srodrigc		warnx(
581166526Srodrigc		    "Using \"-t msdosfs\", since \"-t msdos\" is deprecated.");
58277577Sru		vfstype = "msdosfs";
583166526Srodrigc	}
58477577Sru
585144133Scperciva	/* Construct the name of the appropriate mount command */
586144133Scperciva	(void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
587144133Scperciva
588187035Sobrien	mnt_argv.c = -1;
589187035Sobrien	append_arg(&mnt_argv, execname);
590187035Sobrien	mangle(optbuf, &mnt_argv);
591189397Srodrigc	if (mountprog != NULL)
592189397Srodrigc		strcpy(execname, mountprog);
593189397Srodrigc
594187035Sobrien	append_arg(&mnt_argv, strdup(spec));
595187035Sobrien	append_arg(&mnt_argv, strdup(name));
596187035Sobrien	append_arg(&mnt_argv, NULL);
5971558Srgrimes
5981558Srgrimes	if (debug) {
599164266Srodrigc		if (use_mountprog(vfstype))
600189397Srodrigc			printf("exec: %s", execname);
601164266Srodrigc		else
602164266Srodrigc			printf("mount -t %s", vfstype);
603187035Sobrien		for (i = 1; i < mnt_argv.c; i++)
604187035Sobrien			(void)printf(" %s", mnt_argv.a[i]);
6051558Srgrimes		(void)printf("\n");
606225341Sjhb		free(optbuf);
607225341Sjhb		free(mountprog);
608225341Sjhb		mountprog = NULL;
6091558Srgrimes		return (0);
6101558Srgrimes	}
6111558Srgrimes
612152668Srodrigc	if (use_mountprog(vfstype)) {
613187035Sobrien		ret = exec_mountprog(name, execname, mnt_argv.a);
614152344Srodrigc	} else {
615187035Sobrien		ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
616152344Srodrigc	}
6171558Srgrimes
618152344Srodrigc	free(optbuf);
619225341Sjhb	free(mountprog);
620225341Sjhb	mountprog = NULL;
6211558Srgrimes
622152344Srodrigc	if (verbose) {
623152344Srodrigc		if (statfs(name, &sf) < 0) {
624152344Srodrigc			warn("statfs %s", name);
6251558Srgrimes			return (1);
6261558Srgrimes		}
627152344Srodrigc		if (fstab_style)
628152344Srodrigc			putfsent(&sf);
629152344Srodrigc		else
630152344Srodrigc			prmount(&sf);
6311558Srgrimes	}
6321558Srgrimes
633182570Smatteo	return (ret);
6341558Srgrimes}
6351558Srgrimes
6361558Srgrimesvoid
637151042Srodrigcprmount(struct statfs *sfp)
63823678Speter{
639204840Sbz	uint64_t flags;
640151043Srodrigc	unsigned int i;
6411558Srgrimes	struct opt *o;
64223678Speter	struct passwd *pw;
6431558Srgrimes
64452036Sn_hibma	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
64552036Sn_hibma	    sfp->f_fstypename);
6461558Srgrimes
64723678Speter	flags = sfp->f_flags & MNT_VISFLAGMASK;
648204840Sbz	for (o = optnames; flags != 0 && o->o_opt != 0; o++)
6491558Srgrimes		if (flags & o->o_opt) {
65052036Sn_hibma			(void)printf(", %s", o->o_name);
6511558Srgrimes			flags &= ~o->o_opt;
6521558Srgrimes		}
653125339Spjd	/*
654125339Spjd	 * Inform when file system is mounted by an unprivileged user
655125339Spjd	 * or privileged non-root user.
656125339Spjd	 */
657125365Snectar	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
65852036Sn_hibma		(void)printf(", mounted by ");
65923678Speter		if ((pw = getpwuid(sfp->f_owner)) != NULL)
66023678Speter			(void)printf("%s", pw->pw_name);
66123678Speter		else
66223678Speter			(void)printf("%d", sfp->f_owner);
66323678Speter	}
66465023Ssheldonh	if (verbose) {
66565023Ssheldonh		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
666123242Siedowse			(void)printf(", writes: sync %ju async %ju",
667123242Siedowse			    (uintmax_t)sfp->f_syncwrites,
668123242Siedowse			    (uintmax_t)sfp->f_asyncwrites);
66965023Ssheldonh		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
670123242Siedowse			(void)printf(", reads: sync %ju async %ju",
671123242Siedowse			    (uintmax_t)sfp->f_syncreads,
672123242Siedowse			    (uintmax_t)sfp->f_asyncreads);
673123162Siedowse		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
674123162Siedowse			printf(", fsid ");
675123162Siedowse			for (i = 0; i < sizeof(sfp->f_fsid); i++)
676123162Siedowse				printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
677123162Siedowse		}
67865023Ssheldonh	}
67952036Sn_hibma	(void)printf(")\n");
6801558Srgrimes}
6811558Srgrimes
6821558Srgrimesstruct statfs *
683151042Srodrigcgetmntpt(const char *name)
6841558Srgrimes{
6851558Srgrimes	struct statfs *mntbuf;
6861558Srgrimes	int i, mntsize;
6871558Srgrimes
688224503Smckusick	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
68952678Sgreen	for (i = mntsize - 1; i >= 0; i--) {
6901558Srgrimes		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
6911558Srgrimes		    strcmp(mntbuf[i].f_mntonname, name) == 0)
6921558Srgrimes			return (&mntbuf[i]);
69352678Sgreen	}
6941558Srgrimes	return (NULL);
6951558Srgrimes}
6961558Srgrimes
6971558Srgrimeschar *
698151042Srodrigccatopt(char *s0, const char *s1)
6991558Srgrimes{
7001558Srgrimes	size_t i;
7011558Srgrimes	char *cp;
7021558Srgrimes
70346619Sjkoshy	if (s1 == NULL || *s1 == '\0')
704159473Sjmallett		return (s0);
70546619Sjkoshy
7061558Srgrimes	if (s0 && *s0) {
7071558Srgrimes		i = strlen(s0) + strlen(s1) + 1 + 1;
7081558Srgrimes		if ((cp = malloc(i)) == NULL)
70937425Scharnier			errx(1, "malloc failed");
7101558Srgrimes		(void)snprintf(cp, i, "%s,%s", s0, s1);
7111558Srgrimes	} else
7121558Srgrimes		cp = strdup(s1);
7131558Srgrimes
7141558Srgrimes	if (s0)
7151558Srgrimes		free(s0);
7161558Srgrimes	return (cp);
7171558Srgrimes}
7181558Srgrimes
7191558Srgrimesvoid
720187035Sobrienmangle(char *options, struct cpa *a)
7211558Srgrimes{
722189397Srodrigc	char *p, *s, *val;
7231558Srgrimes
7241558Srgrimes	for (s = options; (p = strsep(&s, ",")) != NULL;)
72546619Sjkoshy		if (*p != '\0') {
726152465Srodrigc			if (strcmp(p, "noauto") == 0) {
727152465Srodrigc				/*
728152465Srodrigc				 * Do not pass noauto option to nmount().
729152465Srodrigc				 * or external mount program.  noauto is
730152465Srodrigc				 * only used to prevent mounting a filesystem
731152465Srodrigc				 * when 'mount -a' is specified, and is
732152465Srodrigc				 * not a real mount option.
733152465Srodrigc				 */
734152465Srodrigc				continue;
735160303Sdes			} else if (strcmp(p, "late") == 0) {
736160303Sdes				/*
737160303Sdes				 * "late" is used to prevent certain file
738160303Sdes				 * systems from being mounted before late
739160303Sdes				 * in the boot cycle; for instance,
740160303Sdes				 * loopback NFS mounts can't be mounted
741160303Sdes				 * before mountd starts.
742160303Sdes				 */
743160303Sdes				continue;
744222832Sdelphij			} else if (strcmp(p, "failok") == 0) {
745222832Sdelphij				/*
746222832Sdelphij				 * "failok" is used to prevent certain file
747222832Sdelphij				 * systems from being causing the system to
748222832Sdelphij				 * drop into single user mode in the boot
749222832Sdelphij				 * cycle, and is not a real mount option.
750222832Sdelphij				 */
751222832Sdelphij				continue;
752189397Srodrigc			} else if (strncmp(p, "mountprog", 9) == 0) {
753189397Srodrigc				/*
754189397Srodrigc				 * "mountprog" is used to force the use of
755189397Srodrigc				 * userland mount programs.
756189397Srodrigc				 */
757189397Srodrigc				val = strchr(p, '=');
758189397Srodrigc                        	if (val != NULL) {
759189397Srodrigc                                	++val;
760189397Srodrigc					if (*val != '\0')
761189397Srodrigc						mountprog = strdup(val);
762189397Srodrigc				}
763189397Srodrigc
764189397Srodrigc				if (mountprog == NULL) {
765189397Srodrigc					errx(1, "Need value for -o mountprog");
766189397Srodrigc				}
767189397Srodrigc				continue;
768152732Srodrigc			} else if (strcmp(p, "userquota") == 0) {
769152732Srodrigc				continue;
770158400Smaxim			} else if (strncmp(p, userquotaeq,
771158400Smaxim			    sizeof(userquotaeq) - 1) == 0) {
772158400Smaxim				continue;
773152732Srodrigc			} else if (strcmp(p, "groupquota") == 0) {
774152732Srodrigc				continue;
775158400Smaxim			} else if (strncmp(p, groupquotaeq,
776158400Smaxim			    sizeof(groupquotaeq) - 1) == 0) {
777158400Smaxim				continue;
778152465Srodrigc			} else if (*p == '-') {
779187035Sobrien				append_arg(a, p);
7801558Srgrimes				p = strchr(p, '=');
781123268Strhodes				if (p != NULL) {
7821558Srgrimes					*p = '\0';
783187035Sobrien					append_arg(a, p + 1);
7841558Srgrimes				}
785153038Srodrigc			} else {
786187035Sobrien				append_arg(a, strdup("-o"));
787187035Sobrien				append_arg(a, p);
7881558Srgrimes			}
78946619Sjkoshy		}
7901558Srgrimes}
7911558Srgrimes
79246619Sjkoshy
79346619Sjkoshychar *
794159473Sjmallettupdate_options(char *opts, char *fstab, int curflags)
79546619Sjkoshy{
79646619Sjkoshy	char *o, *p;
79746619Sjkoshy	char *cur;
79846619Sjkoshy	char *expopt, *newopt, *tmpopt;
79946619Sjkoshy
80046619Sjkoshy	if (opts == NULL)
801159473Sjmallett		return (strdup(""));
80246619Sjkoshy
80346619Sjkoshy	/* remove meta options from list */
80446619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_FSTAB);
80546619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_CURRENT);
80646619Sjkoshy	cur = flags2opts(curflags);
80746619Sjkoshy
80846619Sjkoshy	/*
80946619Sjkoshy	 * Expand all meta-options passed to us first.
81046619Sjkoshy	 */
81146619Sjkoshy	expopt = NULL;
81246619Sjkoshy	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
81346619Sjkoshy		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
81446619Sjkoshy			expopt = catopt(expopt, fstab);
81546619Sjkoshy		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
81646619Sjkoshy			expopt = catopt(expopt, cur);
81746619Sjkoshy		else
81846619Sjkoshy			expopt = catopt(expopt, o);
81946619Sjkoshy	}
82046619Sjkoshy	free(cur);
82146619Sjkoshy	free(opts);
82246619Sjkoshy
82346619Sjkoshy	/*
82446619Sjkoshy	 * Remove previous contradictory arguments. Given option "foo" we
82546619Sjkoshy	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
82646619Sjkoshy	 * and "foo" - so we can deal with possible options like "notice".
82746619Sjkoshy	 */
82846619Sjkoshy	newopt = NULL;
82946619Sjkoshy	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
83046619Sjkoshy		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
83146619Sjkoshy			errx(1, "malloc failed");
832125933Sgrog
83346619Sjkoshy		strcpy(tmpopt, "no");
83446619Sjkoshy		strcat(tmpopt, o);
83546619Sjkoshy		remopt(newopt, tmpopt);
83646619Sjkoshy		free(tmpopt);
83746619Sjkoshy
83846619Sjkoshy		if (strncmp("no", o, 2) == 0)
83946619Sjkoshy			remopt(newopt, o+2);
84046619Sjkoshy
84146619Sjkoshy		newopt = catopt(newopt, o);
84246619Sjkoshy	}
84346619Sjkoshy	free(expopt);
84446619Sjkoshy
845159473Sjmallett	return (newopt);
84646619Sjkoshy}
84746619Sjkoshy
8481558Srgrimesvoid
849159473Sjmallettremopt(char *string, const char *opt)
85046619Sjkoshy{
85146619Sjkoshy	char *o, *p, *r;
85246619Sjkoshy
85346619Sjkoshy	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
85446619Sjkoshy		return;
85546619Sjkoshy
85646619Sjkoshy	r = string;
85746619Sjkoshy
85846619Sjkoshy	for (p = string; (o = strsep(&p, ",")) != NULL;) {
85946619Sjkoshy		if (strcmp(opt, o) != 0) {
86046619Sjkoshy			if (*r == ',' && *o != '\0')
86146619Sjkoshy				r++;
86246619Sjkoshy			while ((*r++ = *o++) != '\0')
86346619Sjkoshy			    ;
86446619Sjkoshy			*--r = ',';
86546619Sjkoshy		}
86646619Sjkoshy	}
86746619Sjkoshy	*r = '\0';
86846619Sjkoshy}
86946619Sjkoshy
87046619Sjkoshyvoid
871159473Sjmallettusage(void)
8721558Srgrimes{
8731558Srgrimes
87437425Scharnier	(void)fprintf(stderr, "%s\n%s\n%s\n",
875160303Sdes"usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
876141611Sru"       mount [-dfpruvw] special | node",
877141611Sru"       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
8781558Srgrimes	exit(1);
8791558Srgrimes}
88017243Sjkh
88117243Sjkhvoid
882175949Smatteoputfsent(struct statfs *ent)
88317243Sjkh{
88423805Sbde	struct fstab *fst;
885213298Sjh	char *opts, *rw;
886168698Sphk	int l;
887125933Sgrog
888213298Sjh	opts = NULL;
889213298Sjh	/* flags2opts() doesn't return the "rw" option. */
890213298Sjh	if ((ent->f_flags & MNT_RDONLY) != 0)
891213298Sjh		rw = NULL;
892213298Sjh	else
893213298Sjh		rw = catopt(NULL, "rw");
894213298Sjh
89546619Sjkoshy	opts = flags2opts(ent->f_flags);
896213298Sjh	opts = catopt(rw, opts);
897156229Skeramida
898175949Smatteo	if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
899175949Smatteo	    strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
900175949Smatteo		strcpy(ent->f_mntfromname, (strnstr(ent->f_mntfromname, ":", 8)
901175949Smatteo		    +1));
902175949Smatteo	}
903175949Smatteo
904168698Sphk	l = strlen(ent->f_mntfromname);
905168698Sphk	printf("%s%s%s%s", ent->f_mntfromname,
906168698Sphk	    l < 8 ? "\t" : "",
907168698Sphk	    l < 16 ? "\t" : "",
908168698Sphk	    l < 24 ? "\t" : " ");
909168698Sphk	l = strlen(ent->f_mntonname);
910168698Sphk	printf("%s%s%s%s", ent->f_mntonname,
911168698Sphk	    l < 8 ? "\t" : "",
912168698Sphk	    l < 16 ? "\t" : "",
913168698Sphk	    l < 24 ? "\t" : " ");
914168698Sphk	printf("%s\t", ent->f_fstypename);
915213298Sjh	l = strlen(opts);
916213298Sjh	printf("%s%s", opts,
917213298Sjh	    l < 8 ? "\t" : " ");
91846619Sjkoshy	free(opts);
91917243Sjkh
92028671Ssteve	if ((fst = getfsspec(ent->f_mntfromname)))
92123805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
92228671Ssteve	else if ((fst = getfsfile(ent->f_mntonname)))
92323805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
92456038Sgreen	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
92556038Sgreen		if (strcmp(ent->f_mntonname, "/") == 0)
92656038Sgreen			printf("\t1 1\n");
92756038Sgreen		else
92856038Sgreen			printf("\t2 2\n");
92956038Sgreen	} else
93023805Sbde		printf("\t0 0\n");
93117243Sjkh}
93246619Sjkoshy
93346619Sjkoshy
93446619Sjkoshychar *
935159473Sjmallettflags2opts(int flags)
93646619Sjkoshy{
93746619Sjkoshy	char *res;
93846619Sjkoshy
93946619Sjkoshy	res = NULL;
94046619Sjkoshy
941155997Srodrigc	if (flags & MNT_RDONLY)		res = catopt(res, "ro");
94246619Sjkoshy	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
94346619Sjkoshy	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
94446619Sjkoshy	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
94546619Sjkoshy	if (flags & MNT_UNION)		res = catopt(res, "union");
94646619Sjkoshy	if (flags & MNT_ASYNC)		res = catopt(res, "async");
94746619Sjkoshy	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
94846619Sjkoshy	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
94946619Sjkoshy	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
95046619Sjkoshy	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
95146619Sjkoshy	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
952101205Srwatson	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
953105118Srwatson	if (flags & MNT_ACLS)		res = catopt(res, "acls");
954200796Strasz	if (flags & MNT_NFS4ACLS)	res = catopt(res, "nfsv4acls");
95546619Sjkoshy
956159473Sjmallett	return (res);
95746619Sjkoshy}
958