mount.c revision 224294
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: head/sbin/mount/mount.c 224294 2011-07-24 18:27:09Z mckusick $");
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
6923805Sbdeint 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" },
11746619Sjkoshy	{ 0, NULL }
1181558Srgrimes};
1191558Srgrimes
12028671Ssteve/*
12128671Ssteve * List of VFS types that can be remounted without becoming mounted on top
12228671Ssteve * of each other.
12328671Ssteve * XXX Is this list correct?
12428671Ssteve */
12528671Sstevestatic const char *
12628671Ssteveremountable_fs_names[] = {
12769056Sphantom	"ufs", "ffs", "ext2fs",
12828671Ssteve	0
12928671Ssteve};
13028671Ssteve
131158400Smaximstatic const char userquotaeq[] = "userquota=";
132158400Smaximstatic const char groupquotaeq[] = "groupquota=";
133158400Smaxim
134189397Srodrigcstatic char *mountprog = NULL;
135189397Srodrigc
136152344Srodrigcstatic int
137152344Srodrigcuse_mountprog(const char *vfstype)
138152344Srodrigc{
139152344Srodrigc	/* XXX: We need to get away from implementing external mount
140152344Srodrigc	 *      programs for every filesystem, and move towards having
141152344Srodrigc	 *	each filesystem properly implement the nmount() system call.
142152344Srodrigc	 */
143152344Srodrigc	unsigned int i;
144152344Srodrigc	const char *fs[] = {
145221124Srmacklem	"cd9660", "mfs", "msdosfs", "nfs", "ntfs",
146221124Srmacklem	"nwfs", "nullfs", "oldnfs", "portalfs", "smbfs", "udf", "unionfs",
147152344Srodrigc	NULL
148152344Srodrigc	};
149152344Srodrigc
150189397Srodrigc	if (mountprog != NULL)
151189397Srodrigc		return (1);
152189397Srodrigc
153159473Sjmallett	for (i = 0; fs[i] != NULL; ++i) {
154152344Srodrigc		if (strcmp(vfstype, fs[i]) == 0)
155159473Sjmallett			return (1);
156152344Srodrigc	}
157186505Sobrien
158159473Sjmallett	return (0);
159152344Srodrigc}
160152344Srodrigc
161152344Srodrigcstatic int
162159473Sjmallettexec_mountprog(const char *name, const char *execname, char *const argv[])
163152344Srodrigc{
164152344Srodrigc	pid_t pid;
165152344Srodrigc	int status;
166152344Srodrigc
167152344Srodrigc	switch (pid = fork()) {
168152344Srodrigc	case -1:				/* Error. */
169152344Srodrigc		warn("fork");
170152344Srodrigc		exit (1);
171152344Srodrigc	case 0:					/* Child. */
172152344Srodrigc		/* Go find an executable. */
173152344Srodrigc		execvP(execname, _PATH_SYSPATH, argv);
174152344Srodrigc		if (errno == ENOENT) {
175189397Srodrigc			warn("exec %s not found", execname);
176189397Srodrigc			if (execname[0] != '/') {
177189397Srodrigc				warnx("in path: %s", _PATH_SYSPATH);
178189397Srodrigc			}
179152344Srodrigc		}
180152344Srodrigc		exit(1);
181152344Srodrigc	default:				/* Parent. */
182152344Srodrigc		if (waitpid(pid, &status, 0) < 0) {
183152344Srodrigc			warn("waitpid");
184152344Srodrigc			return (1);
185152344Srodrigc		}
186152344Srodrigc
187152344Srodrigc		if (WIFEXITED(status)) {
188152344Srodrigc			if (WEXITSTATUS(status) != 0)
189152344Srodrigc				return (WEXITSTATUS(status));
190152344Srodrigc		} else if (WIFSIGNALED(status)) {
191152344Srodrigc			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
192152344Srodrigc			return (1);
193152344Srodrigc		}
194152344Srodrigc		break;
195152344Srodrigc	}
196152344Srodrigc
197152344Srodrigc	return (0);
198152344Srodrigc}
199152344Srodrigc
200163672Srustatic int
201163672Sruspecified_ro(const char *arg)
202163671Sru{
203163671Sru	char *optbuf, *opt;
204163671Sru	int ret = 0;
205163671Sru
206163671Sru	optbuf = strdup(arg);
207163671Sru	if (optbuf == NULL)
208163671Sru		 err(1, NULL);
209163671Sru
210163671Sru	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
211163671Sru		if (strcmp(opt, "ro") == 0) {
212163671Sru			ret = 1;
213163671Sru			break;
214163671Sru		}
215163671Sru	}
216163671Sru	free(optbuf);
217163671Sru	return (ret);
218163671Sru}
219163671Sru
220166439Spjdstatic void
221166439Spjdrestart_mountd(void)
222166439Spjd{
223166439Spjd	struct pidfh *pfh;
224186505Sobrien	pid_t mountdpid;
225166439Spjd
226166439Spjd	pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
227166439Spjd	if (pfh != NULL) {
228166439Spjd		/* Mountd is not running. */
229166439Spjd		pidfile_remove(pfh);
230166439Spjd		return;
231166439Spjd	}
232166439Spjd	if (errno != EEXIST) {
233166439Spjd		/* Cannot open pidfile for some reason. */
234166439Spjd		return;
235166439Spjd	}
236166439Spjd	/* We have mountd(8) PID in mountdpid varible, let's signal it. */
237166439Spjd	if (kill(mountdpid, SIGHUP) == -1)
238166439Spjd		err(1, "signal mountd");
239166439Spjd}
240166439Spjd
2411558Srgrimesint
242151042Srodrigcmain(int argc, char *argv[])
2431558Srgrimes{
24423678Speter	const char *mntfromname, **vfslist, *vfstype;
2451558Srgrimes	struct fstab *fs;
2461558Srgrimes	struct statfs *mntbuf;
247222832Sdelphij	int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
24895289Smux	char *cp, *ep, *options;
2491558Srgrimes
250160303Sdes	all = init_flags = late = 0;
251163671Sru	ro = 0;
252163671Sru	options = NULL;
2531558Srgrimes	vfslist = NULL;
2541558Srgrimes	vfstype = "ufs";
255163672Sru	while ((ch = getopt(argc, argv, "adF:flo:prt:uvw")) != -1)
2561558Srgrimes		switch (ch) {
2571558Srgrimes		case 'a':
2581558Srgrimes			all = 1;
2591558Srgrimes			break;
2601558Srgrimes		case 'd':
2611558Srgrimes			debug = 1;
2621558Srgrimes			break;
263113220Smdodd		case 'F':
264113220Smdodd			setfstab(optarg);
265113220Smdodd			break;
2661558Srgrimes		case 'f':
2671558Srgrimes			init_flags |= MNT_FORCE;
2681558Srgrimes			break;
269160303Sdes		case 'l':
270160303Sdes			late = 1;
271160303Sdes			break;
2721558Srgrimes		case 'o':
273163671Sru			if (*optarg) {
274163671Sru				options = catopt(options, optarg);
275163671Sru				if (specified_ro(optarg))
276163671Sru					ro = 1;
277163671Sru			}
2781558Srgrimes			break;
27923805Sbde		case 'p':
28023805Sbde			fstab_style = 1;
28123805Sbde			verbose = 1;
28223805Sbde			break;
2831558Srgrimes		case 'r':
28476198Sdd			options = catopt(options, "ro");
285163671Sru			ro = 1;
2861558Srgrimes			break;
2871558Srgrimes		case 't':
2881558Srgrimes			if (vfslist != NULL)
28937425Scharnier				errx(1, "only one -t option may be specified");
2901558Srgrimes			vfslist = makevfslist(optarg);
2911558Srgrimes			vfstype = optarg;
2921558Srgrimes			break;
2931558Srgrimes		case 'u':
2941558Srgrimes			init_flags |= MNT_UPDATE;
2951558Srgrimes			break;
2961558Srgrimes		case 'v':
2971558Srgrimes			verbose = 1;
2981558Srgrimes			break;
2991558Srgrimes		case 'w':
30076198Sdd			options = catopt(options, "noro");
3011558Srgrimes			break;
3021558Srgrimes		case '?':
3031558Srgrimes		default:
3041558Srgrimes			usage();
3051558Srgrimes			/* NOTREACHED */
3061558Srgrimes		}
3071558Srgrimes	argc -= optind;
3081558Srgrimes	argv += optind;
3091558Srgrimes
3101558Srgrimes#define	BADTYPE(type)							\
3111558Srgrimes	(strcmp(type, FSTAB_RO) &&					\
3121558Srgrimes	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
3131558Srgrimes
314163671Sru	if ((init_flags & MNT_UPDATE) && (ro == 0))
315163671Sru		options = catopt(options, "noro");
316186505Sobrien
3171558Srgrimes	rval = 0;
3181558Srgrimes	switch (argc) {
3191558Srgrimes	case 0:
320224294Smckusick		if ((mntsize = getmntinfo(&mntbuf, MNT_WAIT)) == 0)
32128671Ssteve			err(1, "getmntinfo");
32228671Ssteve		if (all) {
3231558Srgrimes			while ((fs = getfsent()) != NULL) {
3241558Srgrimes				if (BADTYPE(fs->fs_type))
3251558Srgrimes					continue;
32623678Speter				if (checkvfsname(fs->fs_vfstype, vfslist))
3271558Srgrimes					continue;
32823678Speter				if (hasopt(fs->fs_mntops, "noauto"))
32910288Sdg					continue;
330160303Sdes				if (hasopt(fs->fs_mntops, "late") && !late)
331160303Sdes					continue;
332222832Sdelphij				if (hasopt(fs->fs_mntops, "failok"))
333222832Sdelphij					failok = 1;
334222832Sdelphij				else
335222832Sdelphij					failok = 0;
33644811Sbde				if (!(init_flags & MNT_UPDATE) &&
33744811Sbde				    ismounted(fs, mntbuf, mntsize))
33828671Ssteve					continue;
339125194Sguido				options = update_options(options, fs->fs_mntops,
340125194Sguido				    mntbuf->f_flags);
3411558Srgrimes				if (mountfs(fs->fs_vfstype, fs->fs_spec,
34223805Sbde				    fs->fs_file, init_flags, options,
343222832Sdelphij				    fs->fs_mntops) && !failok)
34423805Sbde					rval = 1;
3451558Srgrimes			}
34628671Ssteve		} else if (fstab_style) {
34717243Sjkh			for (i = 0; i < mntsize; i++) {
34823678Speter				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
34917243Sjkh					continue;
35023805Sbde				putfsent(&mntbuf[i]);
35117243Sjkh			}
35223805Sbde		} else {
3531558Srgrimes			for (i = 0; i < mntsize; i++) {
35423805Sbde				if (checkvfsname(mntbuf[i].f_fstypename,
35523805Sbde				    vfslist))
3561558Srgrimes					continue;
357197200Spjd				if (!verbose &&
358197200Spjd				    (mntbuf[i].f_flags & MNT_IGNORE) != 0)
359197200Spjd					continue;
36023678Speter				prmount(&mntbuf[i]);
3611558Srgrimes			}
3621558Srgrimes		}
3631558Srgrimes		exit(rval);
3641558Srgrimes	case 1:
3651558Srgrimes		if (vfslist != NULL)
3661558Srgrimes			usage();
3671558Srgrimes
368124201Sanholt		rmslashes(*argv, *argv);
3691558Srgrimes		if (init_flags & MNT_UPDATE) {
37052678Sgreen			mntfromname = NULL;
37152678Sgreen			have_fstab = 0;
3721558Srgrimes			if ((mntbuf = getmntpt(*argv)) == NULL)
37352678Sgreen				errx(1, "not currently mounted %s", *argv);
37452678Sgreen			/*
37552678Sgreen			 * Only get the mntflags from fstab if both mntpoint
37652678Sgreen			 * and mntspec are identical. Also handle the special
37752678Sgreen			 * case where just '/' is mounted and 'spec' is not
37852678Sgreen			 * identical with the one from fstab ('/dev' is missing
37952678Sgreen			 * in the spec-string at boot-time).
38052678Sgreen			 */
38146619Sjkoshy			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
38252678Sgreen				if (strcmp(fs->fs_spec,
38352678Sgreen				    mntbuf->f_mntfromname) == 0 &&
38452678Sgreen				    strcmp(fs->fs_file,
38552678Sgreen				    mntbuf->f_mntonname) == 0) {
38652678Sgreen					have_fstab = 1;
38752678Sgreen					mntfromname = mntbuf->f_mntfromname;
38852678Sgreen				} else if (argv[0][0] == '/' &&
38952678Sgreen				    argv[0][1] == '\0') {
39052678Sgreen					fs = getfsfile("/");
39152678Sgreen					have_fstab = 1;
39252678Sgreen					mntfromname = fs->fs_spec;
39352678Sgreen				}
39452678Sgreen			}
39552678Sgreen			if (have_fstab) {
39646619Sjkoshy				options = update_options(options, fs->fs_mntops,
39746619Sjkoshy				    mntbuf->f_flags);
39846619Sjkoshy			} else {
39923678Speter				mntfromname = mntbuf->f_mntfromname;
40046619Sjkoshy				options = update_options(options, NULL,
40146619Sjkoshy				    mntbuf->f_flags);
40246619Sjkoshy			}
40323678Speter			rval = mountfs(mntbuf->f_fstypename, mntfromname,
40423678Speter			    mntbuf->f_mntonname, init_flags, options, 0);
40523678Speter			break;
4061558Srgrimes		}
40723678Speter		if ((fs = getfsfile(*argv)) == NULL &&
40823678Speter		    (fs = getfsspec(*argv)) == NULL)
409102231Strhodes			errx(1, "%s: unknown special file or file system",
41023678Speter			    *argv);
41123678Speter		if (BADTYPE(fs->fs_type))
412102231Strhodes			errx(1, "%s has unknown file system type",
41323678Speter			    *argv);
41423678Speter		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
41523678Speter		    init_flags, options, fs->fs_mntops);
4161558Srgrimes		break;
4171558Srgrimes	case 2:
4181558Srgrimes		/*
41952678Sgreen		 * If -t flag has not been specified, the path cannot be
42095289Smux		 * found, spec contains either a ':' or a '@', then assume
421102231Strhodes		 * that an NFS file system is being specified ala Sun.
42295289Smux		 * Check if the hostname contains only allowed characters
42395289Smux		 * to reduce false positives.  IPv6 addresses containing
42495289Smux		 * ':' will be correctly parsed only if the separator is '@'.
42595289Smux		 * The definition of a valid hostname is taken from RFC 1034.
4261558Srgrimes		 */
427123162Siedowse		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
428123162Siedowse		    (ep = strchr(argv[0], ':')) != NULL)) {
42995318Smux			if (*ep == '@') {
43095318Smux				cp = ep + 1;
43195318Smux				ep = cp + strlen(cp);
43295318Smux			} else
43395318Smux				cp = argv[0];
43495289Smux			while (cp != ep) {
43595289Smux				if (!isdigit(*cp) && !isalpha(*cp) &&
43695289Smux				    *cp != '.' && *cp != '-' && *cp != ':')
43795289Smux					break;
43895289Smux				cp++;
43995289Smux			}
44095289Smux			if (cp == ep)
44195289Smux				vfstype = "nfs";
44295289Smux		}
4431558Srgrimes		rval = mountfs(vfstype,
4441558Srgrimes		    argv[0], argv[1], init_flags, options, NULL);
4451558Srgrimes		break;
4461558Srgrimes	default:
4471558Srgrimes		usage();
4481558Srgrimes		/* NOTREACHED */
4491558Srgrimes	}
4501558Srgrimes
4511558Srgrimes	/*
4521558Srgrimes	 * If the mount was successfully, and done by root, tell mountd the
453166439Spjd	 * good news.
4541558Srgrimes	 */
455166439Spjd	if (rval == 0 && getuid() == 0)
456166439Spjd		restart_mountd();
4571558Srgrimes
4581558Srgrimes	exit(rval);
4591558Srgrimes}
4601558Srgrimes
4611558Srgrimesint
462151042Srodrigcismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
46328671Ssteve{
464154512Spjd	char realfsfile[PATH_MAX];
46528671Ssteve	int i;
46628671Ssteve
46728671Ssteve	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
468102231Strhodes		/* the root file system can always be remounted */
46928671Ssteve		return (0);
47028671Ssteve
471154512Spjd	/* The user may have specified a symlink in fstab, resolve the path */
472154512Spjd	if (realpath(fs->fs_file, realfsfile) == NULL) {
473154512Spjd		/* Cannot resolve the path, use original one */
474154512Spjd		strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
475154512Spjd	}
476154512Spjd
47728671Ssteve	for (i = mntsize - 1; i >= 0; --i)
478154512Spjd		if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
47928671Ssteve		    (!isremountable(fs->fs_vfstype) ||
48028671Ssteve		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
48128671Ssteve			return (1);
48228671Ssteve	return (0);
48328671Ssteve}
48428671Ssteve
48528671Ssteveint
486151042Srodrigcisremountable(const char *vfsname)
48728671Ssteve{
48828671Ssteve	const char **cp;
48928671Ssteve
49028671Ssteve	for (cp = remountable_fs_names; *cp; cp++)
49128671Ssteve		if (strcmp(*cp, vfsname) == 0)
49228671Ssteve			return (1);
49328671Ssteve	return (0);
49428671Ssteve}
49528671Ssteve
49628671Ssteveint
497151042Srodrigchasopt(const char *mntopts, const char *option)
49823678Speter{
49923678Speter	int negative, found;
50023678Speter	char *opt, *optbuf;
50123678Speter
50223678Speter	if (option[0] == 'n' && option[1] == 'o') {
50323678Speter		negative = 1;
50423678Speter		option += 2;
50523678Speter	} else
50623678Speter		negative = 0;
50723678Speter	optbuf = strdup(mntopts);
50823678Speter	found = 0;
50923678Speter	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
51023678Speter		if (opt[0] == 'n' && opt[1] == 'o') {
51123678Speter			if (!strcasecmp(opt + 2, option))
51223678Speter				found = negative;
51323678Speter		} else if (!strcasecmp(opt, option))
51423678Speter			found = !negative;
51523678Speter	}
51623678Speter	free(optbuf);
51723678Speter	return (found);
51823678Speter}
51923678Speter
520187035Sobrienstatic void
521187035Sobrienappend_arg(struct cpa *sa, char *arg)
522187035Sobrien{
523187130Sobrien	if (sa->c + 1 == sa->sz) {
524187130Sobrien		sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
525187130Sobrien		sa->a = realloc(sa->a, sizeof(sa->a) * sa->sz);
526187093Sobrien		if (sa->a == NULL)
527187093Sobrien			errx(1, "realloc failed");
528187093Sobrien	}
529187035Sobrien	sa->a[++sa->c] = arg;
530187035Sobrien}
531187035Sobrien
53223678Speterint
533151042Srodrigcmountfs(const char *vfstype, const char *spec, const char *name, int flags,
534151042Srodrigc	const char *options, const char *mntopts)
5351558Srgrimes{
5361558Srgrimes	struct statfs sf;
537186504Sobrien	int i, ret;
538118580Simp	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
539187130Sobrien	static struct cpa mnt_argv;
5401558Srgrimes
54152055Sphk	/* resolve the mountpoint with realpath(3) */
54252055Sphk	(void)checkpath(name, mntpath);
5431558Srgrimes	name = mntpath;
5441558Srgrimes
54523678Speter	if (mntopts == NULL)
54623678Speter		mntopts = "";
5471558Srgrimes	optbuf = catopt(strdup(mntopts), options);
5481558Srgrimes
5491558Srgrimes	if (strcmp(name, "/") == 0)
5501558Srgrimes		flags |= MNT_UPDATE;
5511558Srgrimes	if (flags & MNT_FORCE)
5521558Srgrimes		optbuf = catopt(optbuf, "force");
5531558Srgrimes	if (flags & MNT_RDONLY)
5541558Srgrimes		optbuf = catopt(optbuf, "ro");
5551558Srgrimes	/*
5561558Srgrimes	 * XXX
5571558Srgrimes	 * The mount_mfs (newfs) command uses -o to select the
55837425Scharnier	 * optimization mode.  We don't pass the default "-o rw"
5591558Srgrimes	 * for that reason.
5601558Srgrimes	 */
5611558Srgrimes	if (flags & MNT_UPDATE)
5621558Srgrimes		optbuf = catopt(optbuf, "update");
5631558Srgrimes
56477577Sru	/* Compatibility glue. */
565166526Srodrigc	if (strcmp(vfstype, "msdos") == 0) {
566166526Srodrigc		warnx(
567166526Srodrigc		    "Using \"-t msdosfs\", since \"-t msdos\" is deprecated.");
56877577Sru		vfstype = "msdosfs";
569166526Srodrigc	}
57077577Sru
571144133Scperciva	/* Construct the name of the appropriate mount command */
572144133Scperciva	(void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
573144133Scperciva
574187035Sobrien	mnt_argv.c = -1;
575187035Sobrien	append_arg(&mnt_argv, execname);
576187035Sobrien	mangle(optbuf, &mnt_argv);
577189397Srodrigc	if (mountprog != NULL)
578189397Srodrigc		strcpy(execname, mountprog);
579189397Srodrigc
580187035Sobrien	append_arg(&mnt_argv, strdup(spec));
581187035Sobrien	append_arg(&mnt_argv, strdup(name));
582187035Sobrien	append_arg(&mnt_argv, NULL);
5831558Srgrimes
5841558Srgrimes	if (debug) {
585164266Srodrigc		if (use_mountprog(vfstype))
586189397Srodrigc			printf("exec: %s", execname);
587164266Srodrigc		else
588164266Srodrigc			printf("mount -t %s", vfstype);
589187035Sobrien		for (i = 1; i < mnt_argv.c; i++)
590187035Sobrien			(void)printf(" %s", mnt_argv.a[i]);
5911558Srgrimes		(void)printf("\n");
5921558Srgrimes		return (0);
5931558Srgrimes	}
5941558Srgrimes
595152668Srodrigc	if (use_mountprog(vfstype)) {
596187035Sobrien		ret = exec_mountprog(name, execname, mnt_argv.a);
597152344Srodrigc	} else {
598187035Sobrien		ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
599152344Srodrigc	}
6001558Srgrimes
601152344Srodrigc	free(optbuf);
6021558Srgrimes
603152344Srodrigc	if (verbose) {
604152344Srodrigc		if (statfs(name, &sf) < 0) {
605152344Srodrigc			warn("statfs %s", name);
6061558Srgrimes			return (1);
6071558Srgrimes		}
608152344Srodrigc		if (fstab_style)
609152344Srodrigc			putfsent(&sf);
610152344Srodrigc		else
611152344Srodrigc			prmount(&sf);
6121558Srgrimes	}
6131558Srgrimes
614182570Smatteo	return (ret);
6151558Srgrimes}
6161558Srgrimes
6171558Srgrimesvoid
618151042Srodrigcprmount(struct statfs *sfp)
61923678Speter{
620204840Sbz	uint64_t flags;
621151043Srodrigc	unsigned int i;
6221558Srgrimes	struct opt *o;
62323678Speter	struct passwd *pw;
6241558Srgrimes
62552036Sn_hibma	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
62652036Sn_hibma	    sfp->f_fstypename);
6271558Srgrimes
62823678Speter	flags = sfp->f_flags & MNT_VISFLAGMASK;
629204840Sbz	for (o = optnames; flags != 0 && o->o_opt != 0; o++)
6301558Srgrimes		if (flags & o->o_opt) {
63152036Sn_hibma			(void)printf(", %s", o->o_name);
6321558Srgrimes			flags &= ~o->o_opt;
6331558Srgrimes		}
634125339Spjd	/*
635125339Spjd	 * Inform when file system is mounted by an unprivileged user
636125339Spjd	 * or privileged non-root user.
637125339Spjd	 */
638125365Snectar	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
63952036Sn_hibma		(void)printf(", mounted by ");
64023678Speter		if ((pw = getpwuid(sfp->f_owner)) != NULL)
64123678Speter			(void)printf("%s", pw->pw_name);
64223678Speter		else
64323678Speter			(void)printf("%d", sfp->f_owner);
64423678Speter	}
64565023Ssheldonh	if (verbose) {
64665023Ssheldonh		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
647123242Siedowse			(void)printf(", writes: sync %ju async %ju",
648123242Siedowse			    (uintmax_t)sfp->f_syncwrites,
649123242Siedowse			    (uintmax_t)sfp->f_asyncwrites);
65065023Ssheldonh		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
651123242Siedowse			(void)printf(", reads: sync %ju async %ju",
652123242Siedowse			    (uintmax_t)sfp->f_syncreads,
653123242Siedowse			    (uintmax_t)sfp->f_asyncreads);
654123162Siedowse		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
655123162Siedowse			printf(", fsid ");
656123162Siedowse			for (i = 0; i < sizeof(sfp->f_fsid); i++)
657123162Siedowse				printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
658123162Siedowse		}
65965023Ssheldonh	}
66052036Sn_hibma	(void)printf(")\n");
6611558Srgrimes}
6621558Srgrimes
6631558Srgrimesstruct statfs *
664151042Srodrigcgetmntpt(const char *name)
6651558Srgrimes{
6661558Srgrimes	struct statfs *mntbuf;
6671558Srgrimes	int i, mntsize;
6681558Srgrimes
669224294Smckusick	mntsize = getmntinfo(&mntbuf, MNT_WAIT);
67052678Sgreen	for (i = mntsize - 1; i >= 0; i--) {
6711558Srgrimes		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
6721558Srgrimes		    strcmp(mntbuf[i].f_mntonname, name) == 0)
6731558Srgrimes			return (&mntbuf[i]);
67452678Sgreen	}
6751558Srgrimes	return (NULL);
6761558Srgrimes}
6771558Srgrimes
6781558Srgrimeschar *
679151042Srodrigccatopt(char *s0, const char *s1)
6801558Srgrimes{
6811558Srgrimes	size_t i;
6821558Srgrimes	char *cp;
6831558Srgrimes
68446619Sjkoshy	if (s1 == NULL || *s1 == '\0')
685159473Sjmallett		return (s0);
68646619Sjkoshy
6871558Srgrimes	if (s0 && *s0) {
6881558Srgrimes		i = strlen(s0) + strlen(s1) + 1 + 1;
6891558Srgrimes		if ((cp = malloc(i)) == NULL)
69037425Scharnier			errx(1, "malloc failed");
6911558Srgrimes		(void)snprintf(cp, i, "%s,%s", s0, s1);
6921558Srgrimes	} else
6931558Srgrimes		cp = strdup(s1);
6941558Srgrimes
6951558Srgrimes	if (s0)
6961558Srgrimes		free(s0);
6971558Srgrimes	return (cp);
6981558Srgrimes}
6991558Srgrimes
7001558Srgrimesvoid
701187035Sobrienmangle(char *options, struct cpa *a)
7021558Srgrimes{
703189397Srodrigc	char *p, *s, *val;
7041558Srgrimes
7051558Srgrimes	for (s = options; (p = strsep(&s, ",")) != NULL;)
70646619Sjkoshy		if (*p != '\0') {
707152465Srodrigc			if (strcmp(p, "noauto") == 0) {
708152465Srodrigc				/*
709152465Srodrigc				 * Do not pass noauto option to nmount().
710152465Srodrigc				 * or external mount program.  noauto is
711152465Srodrigc				 * only used to prevent mounting a filesystem
712152465Srodrigc				 * when 'mount -a' is specified, and is
713152465Srodrigc				 * not a real mount option.
714152465Srodrigc				 */
715152465Srodrigc				continue;
716160303Sdes			} else if (strcmp(p, "late") == 0) {
717160303Sdes				/*
718160303Sdes				 * "late" is used to prevent certain file
719160303Sdes				 * systems from being mounted before late
720160303Sdes				 * in the boot cycle; for instance,
721160303Sdes				 * loopback NFS mounts can't be mounted
722160303Sdes				 * before mountd starts.
723160303Sdes				 */
724160303Sdes				continue;
725222832Sdelphij			} else if (strcmp(p, "failok") == 0) {
726222832Sdelphij				/*
727222832Sdelphij				 * "failok" is used to prevent certain file
728222832Sdelphij				 * systems from being causing the system to
729222832Sdelphij				 * drop into single user mode in the boot
730222832Sdelphij				 * cycle, and is not a real mount option.
731222832Sdelphij				 */
732222832Sdelphij				continue;
733189397Srodrigc			} else if (strncmp(p, "mountprog", 9) == 0) {
734189397Srodrigc				/*
735189397Srodrigc				 * "mountprog" is used to force the use of
736189397Srodrigc				 * userland mount programs.
737189397Srodrigc				 */
738189397Srodrigc				val = strchr(p, '=');
739189397Srodrigc                        	if (val != NULL) {
740189397Srodrigc                                	++val;
741189397Srodrigc					if (*val != '\0')
742189397Srodrigc						mountprog = strdup(val);
743189397Srodrigc				}
744189397Srodrigc
745189397Srodrigc				if (mountprog == NULL) {
746189397Srodrigc					errx(1, "Need value for -o mountprog");
747189397Srodrigc				}
748189397Srodrigc				continue;
749152732Srodrigc			} else if (strcmp(p, "userquota") == 0) {
750152732Srodrigc				continue;
751158400Smaxim			} else if (strncmp(p, userquotaeq,
752158400Smaxim			    sizeof(userquotaeq) - 1) == 0) {
753158400Smaxim				continue;
754152732Srodrigc			} else if (strcmp(p, "groupquota") == 0) {
755152732Srodrigc				continue;
756158400Smaxim			} else if (strncmp(p, groupquotaeq,
757158400Smaxim			    sizeof(groupquotaeq) - 1) == 0) {
758158400Smaxim				continue;
759152465Srodrigc			} else if (*p == '-') {
760187035Sobrien				append_arg(a, p);
7611558Srgrimes				p = strchr(p, '=');
762123268Strhodes				if (p != NULL) {
7631558Srgrimes					*p = '\0';
764187035Sobrien					append_arg(a, p + 1);
7651558Srgrimes				}
766153038Srodrigc			} else {
767187035Sobrien				append_arg(a, strdup("-o"));
768187035Sobrien				append_arg(a, p);
7691558Srgrimes			}
77046619Sjkoshy		}
7711558Srgrimes}
7721558Srgrimes
77346619Sjkoshy
77446619Sjkoshychar *
775159473Sjmallettupdate_options(char *opts, char *fstab, int curflags)
77646619Sjkoshy{
77746619Sjkoshy	char *o, *p;
77846619Sjkoshy	char *cur;
77946619Sjkoshy	char *expopt, *newopt, *tmpopt;
78046619Sjkoshy
78146619Sjkoshy	if (opts == NULL)
782159473Sjmallett		return (strdup(""));
78346619Sjkoshy
78446619Sjkoshy	/* remove meta options from list */
78546619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_FSTAB);
78646619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_CURRENT);
78746619Sjkoshy	cur = flags2opts(curflags);
78846619Sjkoshy
78946619Sjkoshy	/*
79046619Sjkoshy	 * Expand all meta-options passed to us first.
79146619Sjkoshy	 */
79246619Sjkoshy	expopt = NULL;
79346619Sjkoshy	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
79446619Sjkoshy		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
79546619Sjkoshy			expopt = catopt(expopt, fstab);
79646619Sjkoshy		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
79746619Sjkoshy			expopt = catopt(expopt, cur);
79846619Sjkoshy		else
79946619Sjkoshy			expopt = catopt(expopt, o);
80046619Sjkoshy	}
80146619Sjkoshy	free(cur);
80246619Sjkoshy	free(opts);
80346619Sjkoshy
80446619Sjkoshy	/*
80546619Sjkoshy	 * Remove previous contradictory arguments. Given option "foo" we
80646619Sjkoshy	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
80746619Sjkoshy	 * and "foo" - so we can deal with possible options like "notice".
80846619Sjkoshy	 */
80946619Sjkoshy	newopt = NULL;
81046619Sjkoshy	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
81146619Sjkoshy		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
81246619Sjkoshy			errx(1, "malloc failed");
813125933Sgrog
81446619Sjkoshy		strcpy(tmpopt, "no");
81546619Sjkoshy		strcat(tmpopt, o);
81646619Sjkoshy		remopt(newopt, tmpopt);
81746619Sjkoshy		free(tmpopt);
81846619Sjkoshy
81946619Sjkoshy		if (strncmp("no", o, 2) == 0)
82046619Sjkoshy			remopt(newopt, o+2);
82146619Sjkoshy
82246619Sjkoshy		newopt = catopt(newopt, o);
82346619Sjkoshy	}
82446619Sjkoshy	free(expopt);
82546619Sjkoshy
826159473Sjmallett	return (newopt);
82746619Sjkoshy}
82846619Sjkoshy
8291558Srgrimesvoid
830159473Sjmallettremopt(char *string, const char *opt)
83146619Sjkoshy{
83246619Sjkoshy	char *o, *p, *r;
83346619Sjkoshy
83446619Sjkoshy	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
83546619Sjkoshy		return;
83646619Sjkoshy
83746619Sjkoshy	r = string;
83846619Sjkoshy
83946619Sjkoshy	for (p = string; (o = strsep(&p, ",")) != NULL;) {
84046619Sjkoshy		if (strcmp(opt, o) != 0) {
84146619Sjkoshy			if (*r == ',' && *o != '\0')
84246619Sjkoshy				r++;
84346619Sjkoshy			while ((*r++ = *o++) != '\0')
84446619Sjkoshy			    ;
84546619Sjkoshy			*--r = ',';
84646619Sjkoshy		}
84746619Sjkoshy	}
84846619Sjkoshy	*r = '\0';
84946619Sjkoshy}
85046619Sjkoshy
85146619Sjkoshyvoid
852159473Sjmallettusage(void)
8531558Srgrimes{
8541558Srgrimes
85537425Scharnier	(void)fprintf(stderr, "%s\n%s\n%s\n",
856160303Sdes"usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
857141611Sru"       mount [-dfpruvw] special | node",
858141611Sru"       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
8591558Srgrimes	exit(1);
8601558Srgrimes}
86117243Sjkh
86217243Sjkhvoid
863175949Smatteoputfsent(struct statfs *ent)
86417243Sjkh{
86523805Sbde	struct fstab *fst;
866213298Sjh	char *opts, *rw;
867168698Sphk	int l;
868125933Sgrog
869213298Sjh	opts = NULL;
870213298Sjh	/* flags2opts() doesn't return the "rw" option. */
871213298Sjh	if ((ent->f_flags & MNT_RDONLY) != 0)
872213298Sjh		rw = NULL;
873213298Sjh	else
874213298Sjh		rw = catopt(NULL, "rw");
875213298Sjh
87646619Sjkoshy	opts = flags2opts(ent->f_flags);
877213298Sjh	opts = catopt(rw, opts);
878156229Skeramida
879175949Smatteo	if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
880175949Smatteo	    strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
881175949Smatteo		strcpy(ent->f_mntfromname, (strnstr(ent->f_mntfromname, ":", 8)
882175949Smatteo		    +1));
883175949Smatteo	}
884175949Smatteo
885168698Sphk	l = strlen(ent->f_mntfromname);
886168698Sphk	printf("%s%s%s%s", ent->f_mntfromname,
887168698Sphk	    l < 8 ? "\t" : "",
888168698Sphk	    l < 16 ? "\t" : "",
889168698Sphk	    l < 24 ? "\t" : " ");
890168698Sphk	l = strlen(ent->f_mntonname);
891168698Sphk	printf("%s%s%s%s", ent->f_mntonname,
892168698Sphk	    l < 8 ? "\t" : "",
893168698Sphk	    l < 16 ? "\t" : "",
894168698Sphk	    l < 24 ? "\t" : " ");
895168698Sphk	printf("%s\t", ent->f_fstypename);
896213298Sjh	l = strlen(opts);
897213298Sjh	printf("%s%s", opts,
898213298Sjh	    l < 8 ? "\t" : " ");
89946619Sjkoshy	free(opts);
90017243Sjkh
90128671Ssteve	if ((fst = getfsspec(ent->f_mntfromname)))
90223805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
90328671Ssteve	else if ((fst = getfsfile(ent->f_mntonname)))
90423805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
90556038Sgreen	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
90656038Sgreen		if (strcmp(ent->f_mntonname, "/") == 0)
90756038Sgreen			printf("\t1 1\n");
90856038Sgreen		else
90956038Sgreen			printf("\t2 2\n");
91056038Sgreen	} else
91123805Sbde		printf("\t0 0\n");
91217243Sjkh}
91346619Sjkoshy
91446619Sjkoshy
91546619Sjkoshychar *
916159473Sjmallettflags2opts(int flags)
91746619Sjkoshy{
91846619Sjkoshy	char *res;
91946619Sjkoshy
92046619Sjkoshy	res = NULL;
92146619Sjkoshy
922155997Srodrigc	if (flags & MNT_RDONLY)		res = catopt(res, "ro");
92346619Sjkoshy	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
92446619Sjkoshy	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
92546619Sjkoshy	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
92646619Sjkoshy	if (flags & MNT_UNION)		res = catopt(res, "union");
92746619Sjkoshy	if (flags & MNT_ASYNC)		res = catopt(res, "async");
92846619Sjkoshy	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
92946619Sjkoshy	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
93046619Sjkoshy	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
93146619Sjkoshy	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
93246619Sjkoshy	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
933101205Srwatson	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
934105118Srwatson	if (flags & MNT_ACLS)		res = catopt(res, "acls");
935200796Strasz	if (flags & MNT_NFS4ACLS)	res = catopt(res, "nfsv4acls");
93646619Sjkoshy
937159473Sjmallett	return (res);
93846619Sjkoshy}
939