mount.c revision 175949
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 175949 2008-02-03 17:23:58Z matteo $";
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>
61166439Spjd#include <libutil.h>
621558Srgrimes
6328671Ssteve#include "extern.h"
6452055Sphk#include "mntopts.h"
651558Srgrimes#include "pathnames.h"
661558Srgrimes
6746619Sjkoshy/* `meta' options */
68125933Sgrog#define MOUNT_META_OPTION_FSTAB		"fstab"
6946619Sjkoshy#define MOUNT_META_OPTION_CURRENT	"current"
7046619Sjkoshy
7123805Sbdeint debug, fstab_style, verbose;
721558Srgrimes
7392882Simpchar   *catopt(char *, const char *);
7492882Simpstruct statfs *getmntpt(const char *);
7592882Simpint	hasopt(const char *, const char *);
7692882Simpint	ismounted(struct fstab *, struct statfs *, int);
7792882Simpint	isremountable(const char *);
78159473Sjmallettvoid	mangle(char *, int *, char *[]);
7992882Simpchar   *update_options(char *, char *, int);
8092882Simpint	mountfs(const char *, const char *, const char *,
8192882Simp			int, const char *, const char *);
8292882Simpvoid	remopt(char *, const char *);
8392882Simpvoid	prmount(struct statfs *);
84175949Smatteovoid	putfsent(struct statfs *);
8592882Simpvoid	usage(void);
8692882Simpchar   *flags2opts(int);
871558Srgrimes
8837425Scharnier/* Map from mount options to printable formats. */
891558Srgrimesstatic struct opt {
901558Srgrimes	int o_opt;
911558Srgrimes	const char *o_name;
921558Srgrimes} optnames[] = {
931558Srgrimes	{ MNT_ASYNC,		"asynchronous" },
941558Srgrimes	{ MNT_EXPORTED,		"NFS exported" },
951558Srgrimes	{ MNT_LOCAL,		"local" },
9618007Sdg	{ MNT_NOATIME,		"noatime" },
971558Srgrimes	{ MNT_NOEXEC,		"noexec" },
981558Srgrimes	{ MNT_NOSUID,		"nosuid" },
9935105Swosch	{ MNT_NOSYMFOLLOW,	"nosymfollow" },
1001558Srgrimes	{ MNT_QUOTA,		"with quotas" },
1011558Srgrimes	{ MNT_RDONLY,		"read-only" },
1021558Srgrimes	{ MNT_SYNCHRONOUS,	"synchronous" },
1031558Srgrimes	{ MNT_UNION,		"union" },
10429890Skato	{ MNT_NOCLUSTERR,	"noclusterr" },
10529890Skato	{ MNT_NOCLUSTERW,	"noclusterw" },
10631144Sjulian	{ MNT_SUIDDIR,		"suiddir" },
10734266Sjulian	{ MNT_SOFTDEP,		"soft-updates" },
108101205Srwatson	{ MNT_MULTILABEL,	"multilabel" },
109105118Srwatson	{ MNT_ACLS,		"acls" },
110163843Spjd	{ MNT_GJOURNAL,		"gjournal" },
11146619Sjkoshy	{ 0, NULL }
1121558Srgrimes};
1131558Srgrimes
11428671Ssteve/*
11528671Ssteve * List of VFS types that can be remounted without becoming mounted on top
11628671Ssteve * of each other.
11728671Ssteve * XXX Is this list correct?
11828671Ssteve */
11928671Sstevestatic const char *
12028671Ssteveremountable_fs_names[] = {
12169056Sphantom	"ufs", "ffs", "ext2fs",
12228671Ssteve	0
12328671Ssteve};
12428671Ssteve
125158400Smaximstatic const char userquotaeq[] = "userquota=";
126158400Smaximstatic const char groupquotaeq[] = "groupquota=";
127158400Smaxim
128152344Srodrigcstatic int
129152344Srodrigcuse_mountprog(const char *vfstype)
130152344Srodrigc{
131152344Srodrigc	/* XXX: We need to get away from implementing external mount
132152344Srodrigc	 *      programs for every filesystem, and move towards having
133152344Srodrigc	 *	each filesystem properly implement the nmount() system call.
134152344Srodrigc	 */
135152344Srodrigc	unsigned int i;
136152344Srodrigc	const char *fs[] = {
137152778Savatar	"cd9660", "mfs", "msdosfs", "nfs", "nfs4", "ntfs",
138171023Srafan	"nwfs", "nullfs", "portalfs", "smbfs", "udf", "unionfs",
139152344Srodrigc	NULL
140152344Srodrigc	};
141152344Srodrigc
142159473Sjmallett	for (i = 0; fs[i] != NULL; ++i) {
143152344Srodrigc		if (strcmp(vfstype, fs[i]) == 0)
144159473Sjmallett			return (1);
145152344Srodrigc	}
146152344Srodrigc
147159473Sjmallett	return (0);
148152344Srodrigc}
149152344Srodrigc
150152344Srodrigcstatic int
151159473Sjmallettexec_mountprog(const char *name, const char *execname, char *const argv[])
152152344Srodrigc{
153152344Srodrigc	pid_t pid;
154152344Srodrigc	int status;
155152344Srodrigc
156152344Srodrigc	switch (pid = fork()) {
157152344Srodrigc	case -1:				/* Error. */
158152344Srodrigc		warn("fork");
159152344Srodrigc		exit (1);
160152344Srodrigc	case 0:					/* Child. */
161152344Srodrigc		/* Go find an executable. */
162152344Srodrigc		execvP(execname, _PATH_SYSPATH, argv);
163152344Srodrigc		if (errno == ENOENT) {
164152344Srodrigc			warn("exec %s not found in %s", execname,
165152344Srodrigc			    _PATH_SYSPATH);
166152344Srodrigc		}
167152344Srodrigc		exit(1);
168152344Srodrigc	default:				/* Parent. */
169152344Srodrigc		if (waitpid(pid, &status, 0) < 0) {
170152344Srodrigc			warn("waitpid");
171152344Srodrigc			return (1);
172152344Srodrigc		}
173152344Srodrigc
174152344Srodrigc		if (WIFEXITED(status)) {
175152344Srodrigc			if (WEXITSTATUS(status) != 0)
176152344Srodrigc				return (WEXITSTATUS(status));
177152344Srodrigc		} else if (WIFSIGNALED(status)) {
178152344Srodrigc			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
179152344Srodrigc			return (1);
180152344Srodrigc		}
181152344Srodrigc		break;
182152344Srodrigc	}
183152344Srodrigc
184152344Srodrigc	return (0);
185152344Srodrigc}
186152344Srodrigc
187163672Srustatic int
188163672Sruspecified_ro(const char *arg)
189163671Sru{
190163671Sru	char *optbuf, *opt;
191163671Sru	int ret = 0;
192163671Sru
193163671Sru	optbuf = strdup(arg);
194163671Sru	if (optbuf == NULL)
195163671Sru		 err(1, NULL);
196163671Sru
197163671Sru	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
198163671Sru		if (strcmp(opt, "ro") == 0) {
199163671Sru			ret = 1;
200163671Sru			break;
201163671Sru		}
202163671Sru	}
203163671Sru	free(optbuf);
204163671Sru	return (ret);
205163671Sru}
206163671Sru
207166439Spjdstatic void
208166439Spjdrestart_mountd(void)
209166439Spjd{
210166439Spjd	struct pidfh *pfh;
211166439Spjd	pid_t mountdpid;
212166439Spjd
213166439Spjd	pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
214166439Spjd	if (pfh != NULL) {
215166439Spjd		/* Mountd is not running. */
216166439Spjd		pidfile_remove(pfh);
217166439Spjd		return;
218166439Spjd	}
219166439Spjd	if (errno != EEXIST) {
220166439Spjd		/* Cannot open pidfile for some reason. */
221166439Spjd		return;
222166439Spjd	}
223166439Spjd	/* We have mountd(8) PID in mountdpid varible, let's signal it. */
224166439Spjd	if (kill(mountdpid, SIGHUP) == -1)
225166439Spjd		err(1, "signal mountd");
226166439Spjd}
227166439Spjd
2281558Srgrimesint
229151042Srodrigcmain(int argc, char *argv[])
2301558Srgrimes{
23123678Speter	const char *mntfromname, **vfslist, *vfstype;
2321558Srgrimes	struct fstab *fs;
2331558Srgrimes	struct statfs *mntbuf;
234163671Sru	int all, ch, i, init_flags, late, mntsize, rval, have_fstab, ro;
23595289Smux	char *cp, *ep, *options;
2361558Srgrimes
237160303Sdes	all = init_flags = late = 0;
238163671Sru	ro = 0;
239163671Sru	options = NULL;
2401558Srgrimes	vfslist = NULL;
2411558Srgrimes	vfstype = "ufs";
242163672Sru	while ((ch = getopt(argc, argv, "adF:flo:prt:uvw")) != -1)
2431558Srgrimes		switch (ch) {
2441558Srgrimes		case 'a':
2451558Srgrimes			all = 1;
2461558Srgrimes			break;
2471558Srgrimes		case 'd':
2481558Srgrimes			debug = 1;
2491558Srgrimes			break;
250113220Smdodd		case 'F':
251113220Smdodd			setfstab(optarg);
252113220Smdodd			break;
2531558Srgrimes		case 'f':
2541558Srgrimes			init_flags |= MNT_FORCE;
2551558Srgrimes			break;
256160303Sdes		case 'l':
257160303Sdes			late = 1;
258160303Sdes			break;
2591558Srgrimes		case 'o':
260163671Sru			if (*optarg) {
261163671Sru				options = catopt(options, optarg);
262163671Sru				if (specified_ro(optarg))
263163671Sru					ro = 1;
264163671Sru			}
2651558Srgrimes			break;
26623805Sbde		case 'p':
26723805Sbde			fstab_style = 1;
26823805Sbde			verbose = 1;
26923805Sbde			break;
2701558Srgrimes		case 'r':
27176198Sdd			options = catopt(options, "ro");
272163671Sru			ro = 1;
2731558Srgrimes			break;
2741558Srgrimes		case 't':
2751558Srgrimes			if (vfslist != NULL)
27637425Scharnier				errx(1, "only one -t option may be specified");
2771558Srgrimes			vfslist = makevfslist(optarg);
2781558Srgrimes			vfstype = optarg;
2791558Srgrimes			break;
2801558Srgrimes		case 'u':
2811558Srgrimes			init_flags |= MNT_UPDATE;
2821558Srgrimes			break;
2831558Srgrimes		case 'v':
2841558Srgrimes			verbose = 1;
2851558Srgrimes			break;
2861558Srgrimes		case 'w':
28776198Sdd			options = catopt(options, "noro");
2881558Srgrimes			break;
2891558Srgrimes		case '?':
2901558Srgrimes		default:
2911558Srgrimes			usage();
2921558Srgrimes			/* NOTREACHED */
2931558Srgrimes		}
2941558Srgrimes	argc -= optind;
2951558Srgrimes	argv += optind;
2961558Srgrimes
2971558Srgrimes#define	BADTYPE(type)							\
2981558Srgrimes	(strcmp(type, FSTAB_RO) &&					\
2991558Srgrimes	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
3001558Srgrimes
301163671Sru	if ((init_flags & MNT_UPDATE) && (ro == 0))
302163671Sru		options = catopt(options, "noro");
303163671Sru
3041558Srgrimes	rval = 0;
3051558Srgrimes	switch (argc) {
3061558Srgrimes	case 0:
30728671Ssteve		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
30828671Ssteve			err(1, "getmntinfo");
30928671Ssteve		if (all) {
3101558Srgrimes			while ((fs = getfsent()) != NULL) {
3111558Srgrimes				if (BADTYPE(fs->fs_type))
3121558Srgrimes					continue;
31323678Speter				if (checkvfsname(fs->fs_vfstype, vfslist))
3141558Srgrimes					continue;
31523678Speter				if (hasopt(fs->fs_mntops, "noauto"))
31610288Sdg					continue;
317160303Sdes				if (hasopt(fs->fs_mntops, "late") && !late)
318160303Sdes					continue;
31944811Sbde				if (!(init_flags & MNT_UPDATE) &&
32044811Sbde				    ismounted(fs, mntbuf, mntsize))
32128671Ssteve					continue;
322125194Sguido				options = update_options(options, fs->fs_mntops,
323125194Sguido				    mntbuf->f_flags);
3241558Srgrimes				if (mountfs(fs->fs_vfstype, fs->fs_spec,
32523805Sbde				    fs->fs_file, init_flags, options,
32623805Sbde				    fs->fs_mntops))
32723805Sbde					rval = 1;
3281558Srgrimes			}
32928671Ssteve		} else if (fstab_style) {
33017243Sjkh			for (i = 0; i < mntsize; i++) {
33123678Speter				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
33217243Sjkh					continue;
33323805Sbde				putfsent(&mntbuf[i]);
33417243Sjkh			}
33523805Sbde		} else {
3361558Srgrimes			for (i = 0; i < mntsize; i++) {
33723805Sbde				if (checkvfsname(mntbuf[i].f_fstypename,
33823805Sbde				    vfslist))
3391558Srgrimes					continue;
34023678Speter				prmount(&mntbuf[i]);
3411558Srgrimes			}
3421558Srgrimes		}
3431558Srgrimes		exit(rval);
3441558Srgrimes	case 1:
3451558Srgrimes		if (vfslist != NULL)
3461558Srgrimes			usage();
3471558Srgrimes
348124201Sanholt		rmslashes(*argv, *argv);
3491558Srgrimes		if (init_flags & MNT_UPDATE) {
35052678Sgreen			mntfromname = NULL;
35152678Sgreen			have_fstab = 0;
3521558Srgrimes			if ((mntbuf = getmntpt(*argv)) == NULL)
35352678Sgreen				errx(1, "not currently mounted %s", *argv);
35452678Sgreen			/*
35552678Sgreen			 * Only get the mntflags from fstab if both mntpoint
35652678Sgreen			 * and mntspec are identical. Also handle the special
35752678Sgreen			 * case where just '/' is mounted and 'spec' is not
35852678Sgreen			 * identical with the one from fstab ('/dev' is missing
35952678Sgreen			 * in the spec-string at boot-time).
36052678Sgreen			 */
36146619Sjkoshy			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
36252678Sgreen				if (strcmp(fs->fs_spec,
36352678Sgreen				    mntbuf->f_mntfromname) == 0 &&
36452678Sgreen				    strcmp(fs->fs_file,
36552678Sgreen				    mntbuf->f_mntonname) == 0) {
36652678Sgreen					have_fstab = 1;
36752678Sgreen					mntfromname = mntbuf->f_mntfromname;
36852678Sgreen				} else if (argv[0][0] == '/' &&
36952678Sgreen				    argv[0][1] == '\0') {
37052678Sgreen					fs = getfsfile("/");
37152678Sgreen					have_fstab = 1;
37252678Sgreen					mntfromname = fs->fs_spec;
37352678Sgreen				}
37452678Sgreen			}
37552678Sgreen			if (have_fstab) {
37646619Sjkoshy				options = update_options(options, fs->fs_mntops,
37746619Sjkoshy				    mntbuf->f_flags);
37846619Sjkoshy			} else {
37923678Speter				mntfromname = mntbuf->f_mntfromname;
38046619Sjkoshy				options = update_options(options, NULL,
38146619Sjkoshy				    mntbuf->f_flags);
38246619Sjkoshy			}
38323678Speter			rval = mountfs(mntbuf->f_fstypename, mntfromname,
38423678Speter			    mntbuf->f_mntonname, init_flags, options, 0);
38523678Speter			break;
3861558Srgrimes		}
38723678Speter		if ((fs = getfsfile(*argv)) == NULL &&
38823678Speter		    (fs = getfsspec(*argv)) == NULL)
389102231Strhodes			errx(1, "%s: unknown special file or file system",
39023678Speter			    *argv);
39123678Speter		if (BADTYPE(fs->fs_type))
392102231Strhodes			errx(1, "%s has unknown file system type",
39323678Speter			    *argv);
39423678Speter		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
39523678Speter		    init_flags, options, fs->fs_mntops);
3961558Srgrimes		break;
3971558Srgrimes	case 2:
3981558Srgrimes		/*
39952678Sgreen		 * If -t flag has not been specified, the path cannot be
40095289Smux		 * found, spec contains either a ':' or a '@', then assume
401102231Strhodes		 * that an NFS file system is being specified ala Sun.
40295289Smux		 * Check if the hostname contains only allowed characters
40395289Smux		 * to reduce false positives.  IPv6 addresses containing
40495289Smux		 * ':' will be correctly parsed only if the separator is '@'.
40595289Smux		 * The definition of a valid hostname is taken from RFC 1034.
4061558Srgrimes		 */
407123162Siedowse		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
408123162Siedowse		    (ep = strchr(argv[0], ':')) != NULL)) {
40995318Smux			if (*ep == '@') {
41095318Smux				cp = ep + 1;
41195318Smux				ep = cp + strlen(cp);
41295318Smux			} else
41395318Smux				cp = argv[0];
41495289Smux			while (cp != ep) {
41595289Smux				if (!isdigit(*cp) && !isalpha(*cp) &&
41695289Smux				    *cp != '.' && *cp != '-' && *cp != ':')
41795289Smux					break;
41895289Smux				cp++;
41995289Smux			}
42095289Smux			if (cp == ep)
42195289Smux				vfstype = "nfs";
42295289Smux		}
4231558Srgrimes		rval = mountfs(vfstype,
4241558Srgrimes		    argv[0], argv[1], init_flags, options, NULL);
4251558Srgrimes		break;
4261558Srgrimes	default:
4271558Srgrimes		usage();
4281558Srgrimes		/* NOTREACHED */
4291558Srgrimes	}
4301558Srgrimes
4311558Srgrimes	/*
4321558Srgrimes	 * If the mount was successfully, and done by root, tell mountd the
433166439Spjd	 * good news.
4341558Srgrimes	 */
435166439Spjd	if (rval == 0 && getuid() == 0)
436166439Spjd		restart_mountd();
4371558Srgrimes
4381558Srgrimes	exit(rval);
4391558Srgrimes}
4401558Srgrimes
4411558Srgrimesint
442151042Srodrigcismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
44328671Ssteve{
444154512Spjd	char realfsfile[PATH_MAX];
44528671Ssteve	int i;
44628671Ssteve
44728671Ssteve	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
448102231Strhodes		/* the root file system can always be remounted */
44928671Ssteve		return (0);
45028671Ssteve
451154512Spjd	/* The user may have specified a symlink in fstab, resolve the path */
452154512Spjd	if (realpath(fs->fs_file, realfsfile) == NULL) {
453154512Spjd		/* Cannot resolve the path, use original one */
454154512Spjd		strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
455154512Spjd	}
456154512Spjd
45728671Ssteve	for (i = mntsize - 1; i >= 0; --i)
458154512Spjd		if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
45928671Ssteve		    (!isremountable(fs->fs_vfstype) ||
46028671Ssteve		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
46128671Ssteve			return (1);
46228671Ssteve	return (0);
46328671Ssteve}
46428671Ssteve
46528671Ssteveint
466151042Srodrigcisremountable(const char *vfsname)
46728671Ssteve{
46828671Ssteve	const char **cp;
46928671Ssteve
47028671Ssteve	for (cp = remountable_fs_names; *cp; cp++)
47128671Ssteve		if (strcmp(*cp, vfsname) == 0)
47228671Ssteve			return (1);
47328671Ssteve	return (0);
47428671Ssteve}
47528671Ssteve
47628671Ssteveint
477151042Srodrigchasopt(const char *mntopts, const char *option)
47823678Speter{
47923678Speter	int negative, found;
48023678Speter	char *opt, *optbuf;
48123678Speter
48223678Speter	if (option[0] == 'n' && option[1] == 'o') {
48323678Speter		negative = 1;
48423678Speter		option += 2;
48523678Speter	} else
48623678Speter		negative = 0;
48723678Speter	optbuf = strdup(mntopts);
48823678Speter	found = 0;
48923678Speter	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
49023678Speter		if (opt[0] == 'n' && opt[1] == 'o') {
49123678Speter			if (!strcasecmp(opt + 2, option))
49223678Speter				found = negative;
49323678Speter		} else if (!strcasecmp(opt, option))
49423678Speter			found = !negative;
49523678Speter	}
49623678Speter	free(optbuf);
49723678Speter	return (found);
49823678Speter}
49923678Speter
50023678Speterint
501151042Srodrigcmountfs(const char *vfstype, const char *spec, const char *name, int flags,
502151042Srodrigc	const char *options, const char *mntopts)
5031558Srgrimes{
504152344Srodrigc	char *argv[100];
5051558Srgrimes	struct statfs sf;
506152344Srodrigc	int argc, i, ret;
507118580Simp	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
5081558Srgrimes
50952055Sphk	/* resolve the mountpoint with realpath(3) */
51052055Sphk	(void)checkpath(name, mntpath);
5111558Srgrimes	name = mntpath;
5121558Srgrimes
51323678Speter	if (mntopts == NULL)
51423678Speter		mntopts = "";
5151558Srgrimes	optbuf = catopt(strdup(mntopts), options);
5161558Srgrimes
5171558Srgrimes	if (strcmp(name, "/") == 0)
5181558Srgrimes		flags |= MNT_UPDATE;
5191558Srgrimes	if (flags & MNT_FORCE)
5201558Srgrimes		optbuf = catopt(optbuf, "force");
5211558Srgrimes	if (flags & MNT_RDONLY)
5221558Srgrimes		optbuf = catopt(optbuf, "ro");
5231558Srgrimes	/*
5241558Srgrimes	 * XXX
5251558Srgrimes	 * The mount_mfs (newfs) command uses -o to select the
52637425Scharnier	 * optimization mode.  We don't pass the default "-o rw"
5271558Srgrimes	 * for that reason.
5281558Srgrimes	 */
5291558Srgrimes	if (flags & MNT_UPDATE)
5301558Srgrimes		optbuf = catopt(optbuf, "update");
5311558Srgrimes
53277577Sru	/* Compatibility glue. */
533166526Srodrigc	if (strcmp(vfstype, "msdos") == 0) {
534166526Srodrigc		warnx(
535166526Srodrigc		    "Using \"-t msdosfs\", since \"-t msdos\" is deprecated.");
53677577Sru		vfstype = "msdosfs";
537166526Srodrigc	}
53877577Sru
539144133Scperciva	/* Construct the name of the appropriate mount command */
540144133Scperciva	(void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
541144133Scperciva
5421558Srgrimes	argc = 0;
543144133Scperciva	argv[argc++] = execname;
5441558Srgrimes	mangle(optbuf, &argc, argv);
545152344Srodrigc	argv[argc++] = strdup(spec);
546152344Srodrigc	argv[argc++] = strdup(name);
5471558Srgrimes	argv[argc] = NULL;
5481558Srgrimes
5491558Srgrimes	if (debug) {
550164266Srodrigc		if (use_mountprog(vfstype))
551164266Srodrigc			printf("exec: mount_%s", vfstype);
552164266Srodrigc		else
553164266Srodrigc			printf("mount -t %s", vfstype);
5541558Srgrimes		for (i = 1; i < argc; i++)
5551558Srgrimes			(void)printf(" %s", argv[i]);
5561558Srgrimes		(void)printf("\n");
5571558Srgrimes		return (0);
5581558Srgrimes	}
5591558Srgrimes
560152668Srodrigc	if (use_mountprog(vfstype)) {
561152344Srodrigc		ret = exec_mountprog(name, execname, argv);
562152344Srodrigc	} else {
563152344Srodrigc		ret = mount_fs(vfstype, argc, argv);
564152344Srodrigc	}
5651558Srgrimes
566152344Srodrigc	free(optbuf);
5671558Srgrimes
568152344Srodrigc	if (verbose) {
569152344Srodrigc		if (statfs(name, &sf) < 0) {
570152344Srodrigc			warn("statfs %s", name);
5711558Srgrimes			return (1);
5721558Srgrimes		}
573152344Srodrigc		if (fstab_style)
574152344Srodrigc			putfsent(&sf);
575152344Srodrigc		else
576152344Srodrigc			prmount(&sf);
5771558Srgrimes	}
5781558Srgrimes
5791558Srgrimes	return (0);
5801558Srgrimes}
5811558Srgrimes
5821558Srgrimesvoid
583151042Srodrigcprmount(struct statfs *sfp)
58423678Speter{
585151043Srodrigc	int flags;
586151043Srodrigc	unsigned int i;
5871558Srgrimes	struct opt *o;
58823678Speter	struct passwd *pw;
5891558Srgrimes
59052036Sn_hibma	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
59152036Sn_hibma	    sfp->f_fstypename);
5921558Srgrimes
59323678Speter	flags = sfp->f_flags & MNT_VISFLAGMASK;
59452036Sn_hibma	for (o = optnames; flags && o->o_opt; o++)
5951558Srgrimes		if (flags & o->o_opt) {
59652036Sn_hibma			(void)printf(", %s", o->o_name);
5971558Srgrimes			flags &= ~o->o_opt;
5981558Srgrimes		}
599125339Spjd	/*
600125339Spjd	 * Inform when file system is mounted by an unprivileged user
601125339Spjd	 * or privileged non-root user.
602125339Spjd	 */
603125365Snectar	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
60452036Sn_hibma		(void)printf(", mounted by ");
60523678Speter		if ((pw = getpwuid(sfp->f_owner)) != NULL)
60623678Speter			(void)printf("%s", pw->pw_name);
60723678Speter		else
60823678Speter			(void)printf("%d", sfp->f_owner);
60923678Speter	}
61065023Ssheldonh	if (verbose) {
61165023Ssheldonh		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
612123242Siedowse			(void)printf(", writes: sync %ju async %ju",
613123242Siedowse			    (uintmax_t)sfp->f_syncwrites,
614123242Siedowse			    (uintmax_t)sfp->f_asyncwrites);
61565023Ssheldonh		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
616123242Siedowse			(void)printf(", reads: sync %ju async %ju",
617123242Siedowse			    (uintmax_t)sfp->f_syncreads,
618123242Siedowse			    (uintmax_t)sfp->f_asyncreads);
619123162Siedowse		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
620123162Siedowse			printf(", fsid ");
621123162Siedowse			for (i = 0; i < sizeof(sfp->f_fsid); i++)
622123162Siedowse				printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
623123162Siedowse		}
62465023Ssheldonh	}
62552036Sn_hibma	(void)printf(")\n");
6261558Srgrimes}
6271558Srgrimes
6281558Srgrimesstruct statfs *
629151042Srodrigcgetmntpt(const char *name)
6301558Srgrimes{
6311558Srgrimes	struct statfs *mntbuf;
6321558Srgrimes	int i, mntsize;
6331558Srgrimes
6341558Srgrimes	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
63552678Sgreen	for (i = mntsize - 1; i >= 0; i--) {
6361558Srgrimes		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
6371558Srgrimes		    strcmp(mntbuf[i].f_mntonname, name) == 0)
6381558Srgrimes			return (&mntbuf[i]);
63952678Sgreen	}
6401558Srgrimes	return (NULL);
6411558Srgrimes}
6421558Srgrimes
6431558Srgrimeschar *
644151042Srodrigccatopt(char *s0, const char *s1)
6451558Srgrimes{
6461558Srgrimes	size_t i;
6471558Srgrimes	char *cp;
6481558Srgrimes
64946619Sjkoshy	if (s1 == NULL || *s1 == '\0')
650159473Sjmallett		return (s0);
65146619Sjkoshy
6521558Srgrimes	if (s0 && *s0) {
6531558Srgrimes		i = strlen(s0) + strlen(s1) + 1 + 1;
6541558Srgrimes		if ((cp = malloc(i)) == NULL)
65537425Scharnier			errx(1, "malloc failed");
6561558Srgrimes		(void)snprintf(cp, i, "%s,%s", s0, s1);
6571558Srgrimes	} else
6581558Srgrimes		cp = strdup(s1);
6591558Srgrimes
6601558Srgrimes	if (s0)
6611558Srgrimes		free(s0);
6621558Srgrimes	return (cp);
6631558Srgrimes}
6641558Srgrimes
6651558Srgrimesvoid
666159473Sjmallettmangle(char *options, int *argcp, char *argv[])
6671558Srgrimes{
6681558Srgrimes	char *p, *s;
6691558Srgrimes	int argc;
6701558Srgrimes
6711558Srgrimes	argc = *argcp;
6721558Srgrimes	for (s = options; (p = strsep(&s, ",")) != NULL;)
67346619Sjkoshy		if (*p != '\0') {
674152465Srodrigc			if (strcmp(p, "noauto") == 0) {
675152465Srodrigc				/*
676152465Srodrigc				 * Do not pass noauto option to nmount().
677152465Srodrigc				 * or external mount program.  noauto is
678152465Srodrigc				 * only used to prevent mounting a filesystem
679152465Srodrigc				 * when 'mount -a' is specified, and is
680152465Srodrigc				 * not a real mount option.
681152465Srodrigc				 */
682152465Srodrigc				continue;
683160303Sdes			} else if (strcmp(p, "late") == 0) {
684160303Sdes				/*
685160303Sdes				 * "late" is used to prevent certain file
686160303Sdes				 * systems from being mounted before late
687160303Sdes				 * in the boot cycle; for instance,
688160303Sdes				 * loopback NFS mounts can't be mounted
689160303Sdes				 * before mountd starts.
690160303Sdes				 */
691160303Sdes				continue;
692152732Srodrigc			} else if (strcmp(p, "userquota") == 0) {
693152732Srodrigc				continue;
694158400Smaxim			} else if (strncmp(p, userquotaeq,
695158400Smaxim			    sizeof(userquotaeq) - 1) == 0) {
696158400Smaxim				continue;
697152732Srodrigc			} else if (strcmp(p, "groupquota") == 0) {
698152732Srodrigc				continue;
699158400Smaxim			} else if (strncmp(p, groupquotaeq,
700158400Smaxim			    sizeof(groupquotaeq) - 1) == 0) {
701158400Smaxim				continue;
702152465Srodrigc			} else if (*p == '-') {
7031558Srgrimes				argv[argc++] = p;
7041558Srgrimes				p = strchr(p, '=');
705123268Strhodes				if (p != NULL) {
7061558Srgrimes					*p = '\0';
7071558Srgrimes					argv[argc++] = p+1;
7081558Srgrimes				}
709153038Srodrigc			} else {
710152344Srodrigc				argv[argc++] = strdup("-o");
7111558Srgrimes				argv[argc++] = p;
7121558Srgrimes			}
71346619Sjkoshy		}
7141558Srgrimes
7151558Srgrimes	*argcp = argc;
7161558Srgrimes}
7171558Srgrimes
71846619Sjkoshy
71946619Sjkoshychar *
720159473Sjmallettupdate_options(char *opts, char *fstab, int curflags)
72146619Sjkoshy{
72246619Sjkoshy	char *o, *p;
72346619Sjkoshy	char *cur;
72446619Sjkoshy	char *expopt, *newopt, *tmpopt;
72546619Sjkoshy
72646619Sjkoshy	if (opts == NULL)
727159473Sjmallett		return (strdup(""));
72846619Sjkoshy
72946619Sjkoshy	/* remove meta options from list */
73046619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_FSTAB);
73146619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_CURRENT);
73246619Sjkoshy	cur = flags2opts(curflags);
73346619Sjkoshy
73446619Sjkoshy	/*
73546619Sjkoshy	 * Expand all meta-options passed to us first.
73646619Sjkoshy	 */
73746619Sjkoshy	expopt = NULL;
73846619Sjkoshy	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
73946619Sjkoshy		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
74046619Sjkoshy			expopt = catopt(expopt, fstab);
74146619Sjkoshy		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
74246619Sjkoshy			expopt = catopt(expopt, cur);
74346619Sjkoshy		else
74446619Sjkoshy			expopt = catopt(expopt, o);
74546619Sjkoshy	}
74646619Sjkoshy	free(cur);
74746619Sjkoshy	free(opts);
74846619Sjkoshy
74946619Sjkoshy	/*
75046619Sjkoshy	 * Remove previous contradictory arguments. Given option "foo" we
75146619Sjkoshy	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
75246619Sjkoshy	 * and "foo" - so we can deal with possible options like "notice".
75346619Sjkoshy	 */
75446619Sjkoshy	newopt = NULL;
75546619Sjkoshy	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
75646619Sjkoshy		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
75746619Sjkoshy			errx(1, "malloc failed");
758125933Sgrog
75946619Sjkoshy		strcpy(tmpopt, "no");
76046619Sjkoshy		strcat(tmpopt, o);
76146619Sjkoshy		remopt(newopt, tmpopt);
76246619Sjkoshy		free(tmpopt);
76346619Sjkoshy
76446619Sjkoshy		if (strncmp("no", o, 2) == 0)
76546619Sjkoshy			remopt(newopt, o+2);
76646619Sjkoshy
76746619Sjkoshy		newopt = catopt(newopt, o);
76846619Sjkoshy	}
76946619Sjkoshy	free(expopt);
77046619Sjkoshy
771159473Sjmallett	return (newopt);
77246619Sjkoshy}
77346619Sjkoshy
7741558Srgrimesvoid
775159473Sjmallettremopt(char *string, const char *opt)
77646619Sjkoshy{
77746619Sjkoshy	char *o, *p, *r;
77846619Sjkoshy
77946619Sjkoshy	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
78046619Sjkoshy		return;
78146619Sjkoshy
78246619Sjkoshy	r = string;
78346619Sjkoshy
78446619Sjkoshy	for (p = string; (o = strsep(&p, ",")) != NULL;) {
78546619Sjkoshy		if (strcmp(opt, o) != 0) {
78646619Sjkoshy			if (*r == ',' && *o != '\0')
78746619Sjkoshy				r++;
78846619Sjkoshy			while ((*r++ = *o++) != '\0')
78946619Sjkoshy			    ;
79046619Sjkoshy			*--r = ',';
79146619Sjkoshy		}
79246619Sjkoshy	}
79346619Sjkoshy	*r = '\0';
79446619Sjkoshy}
79546619Sjkoshy
79646619Sjkoshyvoid
797159473Sjmallettusage(void)
7981558Srgrimes{
7991558Srgrimes
80037425Scharnier	(void)fprintf(stderr, "%s\n%s\n%s\n",
801160303Sdes"usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
802141611Sru"       mount [-dfpruvw] special | node",
803141611Sru"       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
8041558Srgrimes	exit(1);
8051558Srgrimes}
80617243Sjkh
80717243Sjkhvoid
808175949Smatteoputfsent(struct statfs *ent)
80917243Sjkh{
81023805Sbde	struct fstab *fst;
81146619Sjkoshy	char *opts;
812168698Sphk	int l;
813125933Sgrog
81446619Sjkoshy	opts = flags2opts(ent->f_flags);
815156229Skeramida
816175949Smatteo	if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
817175949Smatteo	    strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
818175949Smatteo		strcpy(ent->f_mntfromname, (strnstr(ent->f_mntfromname, ":", 8)
819175949Smatteo		    +1));
820175949Smatteo	}
821175949Smatteo
822156229Skeramida	/*
823156229Skeramida	 * "rw" is not a real mount option; this is why we print NULL as "rw"
824156229Skeramida	 * if opts is still NULL here.
825156229Skeramida	 */
826168698Sphk	l = strlen(ent->f_mntfromname);
827168698Sphk	printf("%s%s%s%s", ent->f_mntfromname,
828168698Sphk	    l < 8 ? "\t" : "",
829168698Sphk	    l < 16 ? "\t" : "",
830168698Sphk	    l < 24 ? "\t" : " ");
831168698Sphk	l = strlen(ent->f_mntonname);
832168698Sphk	printf("%s%s%s%s", ent->f_mntonname,
833168698Sphk	    l < 8 ? "\t" : "",
834168698Sphk	    l < 16 ? "\t" : "",
835168698Sphk	    l < 24 ? "\t" : " ");
836168698Sphk	printf("%s\t", ent->f_fstypename);
837168698Sphk	if (opts == NULL) {
838168698Sphk		printf("%s\t", "rw");
839168698Sphk	} else {
840168698Sphk		l = strlen(opts);
841168698Sphk		printf("%s%s", opts,
842168698Sphk		    l < 8 ? "\t" : " ");
843168698Sphk	}
84446619Sjkoshy	free(opts);
84517243Sjkh
84628671Ssteve	if ((fst = getfsspec(ent->f_mntfromname)))
84723805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
84828671Ssteve	else if ((fst = getfsfile(ent->f_mntonname)))
84923805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
85056038Sgreen	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
85156038Sgreen		if (strcmp(ent->f_mntonname, "/") == 0)
85256038Sgreen			printf("\t1 1\n");
85356038Sgreen		else
85456038Sgreen			printf("\t2 2\n");
85556038Sgreen	} else
85623805Sbde		printf("\t0 0\n");
85717243Sjkh}
85846619Sjkoshy
85946619Sjkoshy
86046619Sjkoshychar *
861159473Sjmallettflags2opts(int flags)
86246619Sjkoshy{
86346619Sjkoshy	char *res;
86446619Sjkoshy
86546619Sjkoshy	res = NULL;
86646619Sjkoshy
867155997Srodrigc	if (flags & MNT_RDONLY)		res = catopt(res, "ro");
86846619Sjkoshy	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
86946619Sjkoshy	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
87046619Sjkoshy	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
87146619Sjkoshy	if (flags & MNT_UNION)		res = catopt(res, "union");
87246619Sjkoshy	if (flags & MNT_ASYNC)		res = catopt(res, "async");
87346619Sjkoshy	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
87446619Sjkoshy	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
87546619Sjkoshy	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
87646619Sjkoshy	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
87746619Sjkoshy	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
878101205Srwatson	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
879105118Srwatson	if (flags & MNT_ACLS)		res = catopt(res, "acls");
880163843Spjd	if (flags & MNT_GJOURNAL)	res = catopt(res, "gjournal");
88146619Sjkoshy
882159473Sjmallett	return (res);
88346619Sjkoshy}
884