mount.c revision 163672
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 163672 2006-10-24 22:16:51Z ru $";
421558Srgrimes#endif /* not lint */
431558Srgrimes
441558Srgrimes#include <sys/param.h>
451558Srgrimes#include <sys/mount.h>
4623805Sbde#include <sys/stat.h>
471558Srgrimes#include <sys/wait.h>
481558Srgrimes
4995289Smux#include <ctype.h>
501558Srgrimes#include <err.h>
511558Srgrimes#include <errno.h>
521558Srgrimes#include <fstab.h>
53117031Sgordon#include <paths.h>
5423678Speter#include <pwd.h>
551558Srgrimes#include <signal.h>
56123242Siedowse#include <stdint.h>
571558Srgrimes#include <stdio.h>
581558Srgrimes#include <stdlib.h>
591558Srgrimes#include <string.h>
601558Srgrimes#include <unistd.h>
611558Srgrimes
6228671Ssteve#include "extern.h"
6352055Sphk#include "mntopts.h"
641558Srgrimes#include "pathnames.h"
651558Srgrimes
6646619Sjkoshy/* `meta' options */
67125933Sgrog#define MOUNT_META_OPTION_FSTAB		"fstab"
6846619Sjkoshy#define MOUNT_META_OPTION_CURRENT	"current"
6946619Sjkoshy
7023805Sbdeint debug, fstab_style, verbose;
711558Srgrimes
7292882Simpchar   *catopt(char *, const char *);
7392882Simpstruct statfs *getmntpt(const char *);
7492882Simpint	hasopt(const char *, const char *);
7592882Simpint	ismounted(struct fstab *, struct statfs *, int);
7692882Simpint	isremountable(const char *);
77159473Sjmallettvoid	mangle(char *, int *, char *[]);
7892882Simpchar   *update_options(char *, char *, int);
7992882Simpint	mountfs(const char *, const char *, const char *,
8092882Simp			int, const char *, const char *);
8192882Simpvoid	remopt(char *, const char *);
8292882Simpvoid	prmount(struct statfs *);
8392882Simpvoid	putfsent(const struct statfs *);
8492882Simpvoid	usage(void);
8592882Simpchar   *flags2opts(int);
861558Srgrimes
8737425Scharnier/* Map from mount options to printable formats. */
881558Srgrimesstatic struct opt {
891558Srgrimes	int o_opt;
901558Srgrimes	const char *o_name;
911558Srgrimes} optnames[] = {
921558Srgrimes	{ MNT_ASYNC,		"asynchronous" },
931558Srgrimes	{ MNT_EXPORTED,		"NFS exported" },
941558Srgrimes	{ MNT_LOCAL,		"local" },
9518007Sdg	{ MNT_NOATIME,		"noatime" },
961558Srgrimes	{ MNT_NOEXEC,		"noexec" },
971558Srgrimes	{ MNT_NOSUID,		"nosuid" },
9835105Swosch	{ MNT_NOSYMFOLLOW,	"nosymfollow" },
991558Srgrimes	{ MNT_QUOTA,		"with quotas" },
1001558Srgrimes	{ MNT_RDONLY,		"read-only" },
1011558Srgrimes	{ MNT_SYNCHRONOUS,	"synchronous" },
1021558Srgrimes	{ MNT_UNION,		"union" },
10329890Skato	{ MNT_NOCLUSTERR,	"noclusterr" },
10429890Skato	{ MNT_NOCLUSTERW,	"noclusterw" },
10531144Sjulian	{ MNT_SUIDDIR,		"suiddir" },
10634266Sjulian	{ MNT_SOFTDEP,		"soft-updates" },
107101205Srwatson	{ MNT_MULTILABEL,	"multilabel" },
108105118Srwatson	{ MNT_ACLS,		"acls" },
10946619Sjkoshy	{ 0, NULL }
1101558Srgrimes};
1111558Srgrimes
11228671Ssteve/*
11328671Ssteve * List of VFS types that can be remounted without becoming mounted on top
11428671Ssteve * of each other.
11528671Ssteve * XXX Is this list correct?
11628671Ssteve */
11728671Sstevestatic const char *
11828671Ssteveremountable_fs_names[] = {
11969056Sphantom	"ufs", "ffs", "ext2fs",
12028671Ssteve	0
12128671Ssteve};
12228671Ssteve
123158400Smaximstatic const char userquotaeq[] = "userquota=";
124158400Smaximstatic const char groupquotaeq[] = "groupquota=";
125158400Smaxim
126152344Srodrigcstatic int
127152344Srodrigcuse_mountprog(const char *vfstype)
128152344Srodrigc{
129152344Srodrigc	/* XXX: We need to get away from implementing external mount
130152344Srodrigc	 *      programs for every filesystem, and move towards having
131152344Srodrigc	 *	each filesystem properly implement the nmount() system call.
132152344Srodrigc	 */
133152344Srodrigc	unsigned int i;
134152344Srodrigc	const char *fs[] = {
135152778Savatar	"cd9660", "mfs", "msdosfs", "nfs", "nfs4", "ntfs",
136153360Srodrigc	"nwfs", "nullfs", "portalfs", "smbfs", "udf", "umapfs",
137152344Srodrigc	"unionfs",
138152344Srodrigc	NULL
139152344Srodrigc	};
140152344Srodrigc
141159473Sjmallett	for (i = 0; fs[i] != NULL; ++i) {
142152344Srodrigc		if (strcmp(vfstype, fs[i]) == 0)
143159473Sjmallett			return (1);
144152344Srodrigc	}
145152344Srodrigc
146159473Sjmallett	return (0);
147152344Srodrigc}
148152344Srodrigc
149152344Srodrigcstatic int
150159473Sjmallettexec_mountprog(const char *name, const char *execname, char *const argv[])
151152344Srodrigc{
152152344Srodrigc	pid_t pid;
153152344Srodrigc	int status;
154152344Srodrigc
155152344Srodrigc	switch (pid = fork()) {
156152344Srodrigc	case -1:				/* Error. */
157152344Srodrigc		warn("fork");
158152344Srodrigc		exit (1);
159152344Srodrigc	case 0:					/* Child. */
160152344Srodrigc		/* Go find an executable. */
161152344Srodrigc		execvP(execname, _PATH_SYSPATH, argv);
162152344Srodrigc		if (errno == ENOENT) {
163152344Srodrigc			warn("exec %s not found in %s", execname,
164152344Srodrigc			    _PATH_SYSPATH);
165152344Srodrigc		}
166152344Srodrigc		exit(1);
167152344Srodrigc	default:				/* Parent. */
168152344Srodrigc		if (waitpid(pid, &status, 0) < 0) {
169152344Srodrigc			warn("waitpid");
170152344Srodrigc			return (1);
171152344Srodrigc		}
172152344Srodrigc
173152344Srodrigc		if (WIFEXITED(status)) {
174152344Srodrigc			if (WEXITSTATUS(status) != 0)
175152344Srodrigc				return (WEXITSTATUS(status));
176152344Srodrigc		} else if (WIFSIGNALED(status)) {
177152344Srodrigc			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
178152344Srodrigc			return (1);
179152344Srodrigc		}
180152344Srodrigc		break;
181152344Srodrigc	}
182152344Srodrigc
183152344Srodrigc	return (0);
184152344Srodrigc}
185152344Srodrigc
186163672Srustatic int
187163672Sruspecified_ro(const char *arg)
188163671Sru{
189163671Sru	char *optbuf, *opt;
190163671Sru	int ret = 0;
191163671Sru
192163671Sru	optbuf = strdup(arg);
193163671Sru	if (optbuf == NULL)
194163671Sru		 err(1, NULL);
195163671Sru
196163671Sru	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
197163671Sru		if (strcmp(opt, "ro") == 0) {
198163671Sru			ret = 1;
199163671Sru			break;
200163671Sru		}
201163671Sru	}
202163671Sru	free(optbuf);
203163671Sru	return (ret);
204163671Sru}
205163671Sru
2061558Srgrimesint
207151042Srodrigcmain(int argc, char *argv[])
2081558Srgrimes{
20923678Speter	const char *mntfromname, **vfslist, *vfstype;
2101558Srgrimes	struct fstab *fs;
2111558Srgrimes	struct statfs *mntbuf;
2121558Srgrimes	FILE *mountdfp;
2131558Srgrimes	pid_t pid;
214163671Sru	int all, ch, i, init_flags, late, mntsize, rval, have_fstab, ro;
21595289Smux	char *cp, *ep, *options;
2161558Srgrimes
217160303Sdes	all = init_flags = late = 0;
218163671Sru	ro = 0;
219163671Sru	options = NULL;
2201558Srgrimes	vfslist = NULL;
2211558Srgrimes	vfstype = "ufs";
222163672Sru	while ((ch = getopt(argc, argv, "adF:flo:prt:uvw")) != -1)
2231558Srgrimes		switch (ch) {
2241558Srgrimes		case 'a':
2251558Srgrimes			all = 1;
2261558Srgrimes			break;
2271558Srgrimes		case 'd':
2281558Srgrimes			debug = 1;
2291558Srgrimes			break;
230113220Smdodd		case 'F':
231113220Smdodd			setfstab(optarg);
232113220Smdodd			break;
2331558Srgrimes		case 'f':
2341558Srgrimes			init_flags |= MNT_FORCE;
2351558Srgrimes			break;
236160303Sdes		case 'l':
237160303Sdes			late = 1;
238160303Sdes			break;
2391558Srgrimes		case 'o':
240163671Sru			if (*optarg) {
241163671Sru				options = catopt(options, optarg);
242163671Sru				if (specified_ro(optarg))
243163671Sru					ro = 1;
244163671Sru			}
2451558Srgrimes			break;
24623805Sbde		case 'p':
24723805Sbde			fstab_style = 1;
24823805Sbde			verbose = 1;
24923805Sbde			break;
2501558Srgrimes		case 'r':
25176198Sdd			options = catopt(options, "ro");
252163671Sru			ro = 1;
2531558Srgrimes			break;
2541558Srgrimes		case 't':
2551558Srgrimes			if (vfslist != NULL)
25637425Scharnier				errx(1, "only one -t option may be specified");
2571558Srgrimes			vfslist = makevfslist(optarg);
2581558Srgrimes			vfstype = optarg;
2591558Srgrimes			break;
2601558Srgrimes		case 'u':
2611558Srgrimes			init_flags |= MNT_UPDATE;
2621558Srgrimes			break;
2631558Srgrimes		case 'v':
2641558Srgrimes			verbose = 1;
2651558Srgrimes			break;
2661558Srgrimes		case 'w':
26776198Sdd			options = catopt(options, "noro");
2681558Srgrimes			break;
2691558Srgrimes		case '?':
2701558Srgrimes		default:
2711558Srgrimes			usage();
2721558Srgrimes			/* NOTREACHED */
2731558Srgrimes		}
2741558Srgrimes	argc -= optind;
2751558Srgrimes	argv += optind;
2761558Srgrimes
2771558Srgrimes#define	BADTYPE(type)							\
2781558Srgrimes	(strcmp(type, FSTAB_RO) &&					\
2791558Srgrimes	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
2801558Srgrimes
281163671Sru	if ((init_flags & MNT_UPDATE) && (ro == 0))
282163671Sru		options = catopt(options, "noro");
283163671Sru
2841558Srgrimes	rval = 0;
2851558Srgrimes	switch (argc) {
2861558Srgrimes	case 0:
28728671Ssteve		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
28828671Ssteve			err(1, "getmntinfo");
28928671Ssteve		if (all) {
2901558Srgrimes			while ((fs = getfsent()) != NULL) {
2911558Srgrimes				if (BADTYPE(fs->fs_type))
2921558Srgrimes					continue;
29323678Speter				if (checkvfsname(fs->fs_vfstype, vfslist))
2941558Srgrimes					continue;
29523678Speter				if (hasopt(fs->fs_mntops, "noauto"))
29610288Sdg					continue;
297160303Sdes				if (hasopt(fs->fs_mntops, "late") && !late)
298160303Sdes					continue;
29944811Sbde				if (!(init_flags & MNT_UPDATE) &&
30044811Sbde				    ismounted(fs, mntbuf, mntsize))
30128671Ssteve					continue;
302125194Sguido				options = update_options(options, fs->fs_mntops,
303125194Sguido				    mntbuf->f_flags);
3041558Srgrimes				if (mountfs(fs->fs_vfstype, fs->fs_spec,
30523805Sbde				    fs->fs_file, init_flags, options,
30623805Sbde				    fs->fs_mntops))
30723805Sbde					rval = 1;
3081558Srgrimes			}
30928671Ssteve		} else if (fstab_style) {
31017243Sjkh			for (i = 0; i < mntsize; i++) {
31123678Speter				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
31217243Sjkh					continue;
31323805Sbde				putfsent(&mntbuf[i]);
31417243Sjkh			}
31523805Sbde		} else {
3161558Srgrimes			for (i = 0; i < mntsize; i++) {
31723805Sbde				if (checkvfsname(mntbuf[i].f_fstypename,
31823805Sbde				    vfslist))
3191558Srgrimes					continue;
32023678Speter				prmount(&mntbuf[i]);
3211558Srgrimes			}
3221558Srgrimes		}
3231558Srgrimes		exit(rval);
3241558Srgrimes	case 1:
3251558Srgrimes		if (vfslist != NULL)
3261558Srgrimes			usage();
3271558Srgrimes
328124201Sanholt		rmslashes(*argv, *argv);
3291558Srgrimes		if (init_flags & MNT_UPDATE) {
33052678Sgreen			mntfromname = NULL;
33152678Sgreen			have_fstab = 0;
3321558Srgrimes			if ((mntbuf = getmntpt(*argv)) == NULL)
33352678Sgreen				errx(1, "not currently mounted %s", *argv);
33452678Sgreen			/*
33552678Sgreen			 * Only get the mntflags from fstab if both mntpoint
33652678Sgreen			 * and mntspec are identical. Also handle the special
33752678Sgreen			 * case where just '/' is mounted and 'spec' is not
33852678Sgreen			 * identical with the one from fstab ('/dev' is missing
33952678Sgreen			 * in the spec-string at boot-time).
34052678Sgreen			 */
34146619Sjkoshy			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
34252678Sgreen				if (strcmp(fs->fs_spec,
34352678Sgreen				    mntbuf->f_mntfromname) == 0 &&
34452678Sgreen				    strcmp(fs->fs_file,
34552678Sgreen				    mntbuf->f_mntonname) == 0) {
34652678Sgreen					have_fstab = 1;
34752678Sgreen					mntfromname = mntbuf->f_mntfromname;
34852678Sgreen				} else if (argv[0][0] == '/' &&
34952678Sgreen				    argv[0][1] == '\0') {
35052678Sgreen					fs = getfsfile("/");
35152678Sgreen					have_fstab = 1;
35252678Sgreen					mntfromname = fs->fs_spec;
35352678Sgreen				}
35452678Sgreen			}
35552678Sgreen			if (have_fstab) {
35646619Sjkoshy				options = update_options(options, fs->fs_mntops,
35746619Sjkoshy				    mntbuf->f_flags);
35846619Sjkoshy			} else {
35923678Speter				mntfromname = mntbuf->f_mntfromname;
36046619Sjkoshy				options = update_options(options, NULL,
36146619Sjkoshy				    mntbuf->f_flags);
36246619Sjkoshy			}
36323678Speter			rval = mountfs(mntbuf->f_fstypename, mntfromname,
36423678Speter			    mntbuf->f_mntonname, init_flags, options, 0);
36523678Speter			break;
3661558Srgrimes		}
36723678Speter		if ((fs = getfsfile(*argv)) == NULL &&
36823678Speter		    (fs = getfsspec(*argv)) == NULL)
369102231Strhodes			errx(1, "%s: unknown special file or file system",
37023678Speter			    *argv);
37123678Speter		if (BADTYPE(fs->fs_type))
372102231Strhodes			errx(1, "%s has unknown file system type",
37323678Speter			    *argv);
37423678Speter		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
37523678Speter		    init_flags, options, fs->fs_mntops);
3761558Srgrimes		break;
3771558Srgrimes	case 2:
3781558Srgrimes		/*
37952678Sgreen		 * If -t flag has not been specified, the path cannot be
38095289Smux		 * found, spec contains either a ':' or a '@', then assume
381102231Strhodes		 * that an NFS file system is being specified ala Sun.
38295289Smux		 * Check if the hostname contains only allowed characters
38395289Smux		 * to reduce false positives.  IPv6 addresses containing
38495289Smux		 * ':' will be correctly parsed only if the separator is '@'.
38595289Smux		 * The definition of a valid hostname is taken from RFC 1034.
3861558Srgrimes		 */
387123162Siedowse		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
388123162Siedowse		    (ep = strchr(argv[0], ':')) != NULL)) {
38995318Smux			if (*ep == '@') {
39095318Smux				cp = ep + 1;
39195318Smux				ep = cp + strlen(cp);
39295318Smux			} else
39395318Smux				cp = argv[0];
39495289Smux			while (cp != ep) {
39595289Smux				if (!isdigit(*cp) && !isalpha(*cp) &&
39695289Smux				    *cp != '.' && *cp != '-' && *cp != ':')
39795289Smux					break;
39895289Smux				cp++;
39995289Smux			}
40095289Smux			if (cp == ep)
40195289Smux				vfstype = "nfs";
40295289Smux		}
4031558Srgrimes		rval = mountfs(vfstype,
4041558Srgrimes		    argv[0], argv[1], init_flags, options, NULL);
4051558Srgrimes		break;
4061558Srgrimes	default:
4071558Srgrimes		usage();
4081558Srgrimes		/* NOTREACHED */
4091558Srgrimes	}
4101558Srgrimes
4111558Srgrimes	/*
4121558Srgrimes	 * If the mount was successfully, and done by root, tell mountd the
4131558Srgrimes	 * good news.  Pid checks are probably unnecessary, but don't hurt.
4141558Srgrimes	 */
4151558Srgrimes	if (rval == 0 && getuid() == 0 &&
4161558Srgrimes	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
41728671Ssteve		if (fscanf(mountdfp, "%d", &pid) == 1 &&
4181558Srgrimes		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
4191558Srgrimes			err(1, "signal mountd");
4201558Srgrimes		(void)fclose(mountdfp);
4211558Srgrimes	}
4221558Srgrimes
4231558Srgrimes	exit(rval);
4241558Srgrimes}
4251558Srgrimes
4261558Srgrimesint
427151042Srodrigcismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
42828671Ssteve{
429154512Spjd	char realfsfile[PATH_MAX];
43028671Ssteve	int i;
43128671Ssteve
43228671Ssteve	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
433102231Strhodes		/* the root file system can always be remounted */
43428671Ssteve		return (0);
43528671Ssteve
436154512Spjd	/* The user may have specified a symlink in fstab, resolve the path */
437154512Spjd	if (realpath(fs->fs_file, realfsfile) == NULL) {
438154512Spjd		/* Cannot resolve the path, use original one */
439154512Spjd		strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
440154512Spjd	}
441154512Spjd
44228671Ssteve	for (i = mntsize - 1; i >= 0; --i)
443154512Spjd		if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
44428671Ssteve		    (!isremountable(fs->fs_vfstype) ||
44528671Ssteve		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
44628671Ssteve			return (1);
44728671Ssteve	return (0);
44828671Ssteve}
44928671Ssteve
45028671Ssteveint
451151042Srodrigcisremountable(const char *vfsname)
45228671Ssteve{
45328671Ssteve	const char **cp;
45428671Ssteve
45528671Ssteve	for (cp = remountable_fs_names; *cp; cp++)
45628671Ssteve		if (strcmp(*cp, vfsname) == 0)
45728671Ssteve			return (1);
45828671Ssteve	return (0);
45928671Ssteve}
46028671Ssteve
46128671Ssteveint
462151042Srodrigchasopt(const char *mntopts, const char *option)
46323678Speter{
46423678Speter	int negative, found;
46523678Speter	char *opt, *optbuf;
46623678Speter
46723678Speter	if (option[0] == 'n' && option[1] == 'o') {
46823678Speter		negative = 1;
46923678Speter		option += 2;
47023678Speter	} else
47123678Speter		negative = 0;
47223678Speter	optbuf = strdup(mntopts);
47323678Speter	found = 0;
47423678Speter	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
47523678Speter		if (opt[0] == 'n' && opt[1] == 'o') {
47623678Speter			if (!strcasecmp(opt + 2, option))
47723678Speter				found = negative;
47823678Speter		} else if (!strcasecmp(opt, option))
47923678Speter			found = !negative;
48023678Speter	}
48123678Speter	free(optbuf);
48223678Speter	return (found);
48323678Speter}
48423678Speter
48523678Speterint
486151042Srodrigcmountfs(const char *vfstype, const char *spec, const char *name, int flags,
487151042Srodrigc	const char *options, const char *mntopts)
4881558Srgrimes{
489152344Srodrigc	char *argv[100];
4901558Srgrimes	struct statfs sf;
491152344Srodrigc	int argc, i, ret;
492118580Simp	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
4931558Srgrimes
49452055Sphk	/* resolve the mountpoint with realpath(3) */
49552055Sphk	(void)checkpath(name, mntpath);
4961558Srgrimes	name = mntpath;
4971558Srgrimes
49823678Speter	if (mntopts == NULL)
49923678Speter		mntopts = "";
5001558Srgrimes	optbuf = catopt(strdup(mntopts), options);
5011558Srgrimes
5021558Srgrimes	if (strcmp(name, "/") == 0)
5031558Srgrimes		flags |= MNT_UPDATE;
5041558Srgrimes	if (flags & MNT_FORCE)
5051558Srgrimes		optbuf = catopt(optbuf, "force");
5061558Srgrimes	if (flags & MNT_RDONLY)
5071558Srgrimes		optbuf = catopt(optbuf, "ro");
5081558Srgrimes	/*
5091558Srgrimes	 * XXX
5101558Srgrimes	 * The mount_mfs (newfs) command uses -o to select the
51137425Scharnier	 * optimization mode.  We don't pass the default "-o rw"
5121558Srgrimes	 * for that reason.
5131558Srgrimes	 */
5141558Srgrimes	if (flags & MNT_UPDATE)
5151558Srgrimes		optbuf = catopt(optbuf, "update");
5161558Srgrimes
51777577Sru	/* Compatibility glue. */
51877577Sru	if (strcmp(vfstype, "msdos") == 0)
51977577Sru		vfstype = "msdosfs";
52077577Sru
521144133Scperciva	/* Construct the name of the appropriate mount command */
522144133Scperciva	(void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
523144133Scperciva
5241558Srgrimes	argc = 0;
525144133Scperciva	argv[argc++] = execname;
5261558Srgrimes	mangle(optbuf, &argc, argv);
527152344Srodrigc	argv[argc++] = strdup(spec);
528152344Srodrigc	argv[argc++] = strdup(name);
5291558Srgrimes	argv[argc] = NULL;
5301558Srgrimes
5311558Srgrimes	if (debug) {
5321558Srgrimes		(void)printf("exec: mount_%s", vfstype);
5331558Srgrimes		for (i = 1; i < argc; i++)
5341558Srgrimes			(void)printf(" %s", argv[i]);
5351558Srgrimes		(void)printf("\n");
5361558Srgrimes		return (0);
5371558Srgrimes	}
5381558Srgrimes
539152668Srodrigc	if (use_mountprog(vfstype)) {
540152344Srodrigc		ret = exec_mountprog(name, execname, argv);
541152344Srodrigc	} else {
542152344Srodrigc		ret = mount_fs(vfstype, argc, argv);
543152344Srodrigc	}
5441558Srgrimes
545152344Srodrigc	free(optbuf);
5461558Srgrimes
547152344Srodrigc	if (verbose) {
548152344Srodrigc		if (statfs(name, &sf) < 0) {
549152344Srodrigc			warn("statfs %s", name);
5501558Srgrimes			return (1);
5511558Srgrimes		}
552152344Srodrigc		if (fstab_style)
553152344Srodrigc			putfsent(&sf);
554152344Srodrigc		else
555152344Srodrigc			prmount(&sf);
5561558Srgrimes	}
5571558Srgrimes
5581558Srgrimes	return (0);
5591558Srgrimes}
5601558Srgrimes
5611558Srgrimesvoid
562151042Srodrigcprmount(struct statfs *sfp)
56323678Speter{
564151043Srodrigc	int flags;
565151043Srodrigc	unsigned int i;
5661558Srgrimes	struct opt *o;
56723678Speter	struct passwd *pw;
5681558Srgrimes
56952036Sn_hibma	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
57052036Sn_hibma	    sfp->f_fstypename);
5711558Srgrimes
57223678Speter	flags = sfp->f_flags & MNT_VISFLAGMASK;
57352036Sn_hibma	for (o = optnames; flags && o->o_opt; o++)
5741558Srgrimes		if (flags & o->o_opt) {
57552036Sn_hibma			(void)printf(", %s", o->o_name);
5761558Srgrimes			flags &= ~o->o_opt;
5771558Srgrimes		}
578125339Spjd	/*
579125339Spjd	 * Inform when file system is mounted by an unprivileged user
580125339Spjd	 * or privileged non-root user.
581125339Spjd	 */
582125365Snectar	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
58352036Sn_hibma		(void)printf(", mounted by ");
58423678Speter		if ((pw = getpwuid(sfp->f_owner)) != NULL)
58523678Speter			(void)printf("%s", pw->pw_name);
58623678Speter		else
58723678Speter			(void)printf("%d", sfp->f_owner);
58823678Speter	}
58965023Ssheldonh	if (verbose) {
59065023Ssheldonh		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
591123242Siedowse			(void)printf(", writes: sync %ju async %ju",
592123242Siedowse			    (uintmax_t)sfp->f_syncwrites,
593123242Siedowse			    (uintmax_t)sfp->f_asyncwrites);
59465023Ssheldonh		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
595123242Siedowse			(void)printf(", reads: sync %ju async %ju",
596123242Siedowse			    (uintmax_t)sfp->f_syncreads,
597123242Siedowse			    (uintmax_t)sfp->f_asyncreads);
598123162Siedowse		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
599123162Siedowse			printf(", fsid ");
600123162Siedowse			for (i = 0; i < sizeof(sfp->f_fsid); i++)
601123162Siedowse				printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
602123162Siedowse		}
60365023Ssheldonh	}
60452036Sn_hibma	(void)printf(")\n");
6051558Srgrimes}
6061558Srgrimes
6071558Srgrimesstruct statfs *
608151042Srodrigcgetmntpt(const char *name)
6091558Srgrimes{
6101558Srgrimes	struct statfs *mntbuf;
6111558Srgrimes	int i, mntsize;
6121558Srgrimes
6131558Srgrimes	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
61452678Sgreen	for (i = mntsize - 1; i >= 0; i--) {
6151558Srgrimes		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
6161558Srgrimes		    strcmp(mntbuf[i].f_mntonname, name) == 0)
6171558Srgrimes			return (&mntbuf[i]);
61852678Sgreen	}
6191558Srgrimes	return (NULL);
6201558Srgrimes}
6211558Srgrimes
6221558Srgrimeschar *
623151042Srodrigccatopt(char *s0, const char *s1)
6241558Srgrimes{
6251558Srgrimes	size_t i;
6261558Srgrimes	char *cp;
6271558Srgrimes
62846619Sjkoshy	if (s1 == NULL || *s1 == '\0')
629159473Sjmallett		return (s0);
63046619Sjkoshy
6311558Srgrimes	if (s0 && *s0) {
6321558Srgrimes		i = strlen(s0) + strlen(s1) + 1 + 1;
6331558Srgrimes		if ((cp = malloc(i)) == NULL)
63437425Scharnier			errx(1, "malloc failed");
6351558Srgrimes		(void)snprintf(cp, i, "%s,%s", s0, s1);
6361558Srgrimes	} else
6371558Srgrimes		cp = strdup(s1);
6381558Srgrimes
6391558Srgrimes	if (s0)
6401558Srgrimes		free(s0);
6411558Srgrimes	return (cp);
6421558Srgrimes}
6431558Srgrimes
6441558Srgrimesvoid
645159473Sjmallettmangle(char *options, int *argcp, char *argv[])
6461558Srgrimes{
6471558Srgrimes	char *p, *s;
6481558Srgrimes	int argc;
6491558Srgrimes
6501558Srgrimes	argc = *argcp;
6511558Srgrimes	for (s = options; (p = strsep(&s, ",")) != NULL;)
65246619Sjkoshy		if (*p != '\0') {
653152465Srodrigc			if (strcmp(p, "noauto") == 0) {
654152465Srodrigc				/*
655152465Srodrigc				 * Do not pass noauto option to nmount().
656152465Srodrigc				 * or external mount program.  noauto is
657152465Srodrigc				 * only used to prevent mounting a filesystem
658152465Srodrigc				 * when 'mount -a' is specified, and is
659152465Srodrigc				 * not a real mount option.
660152465Srodrigc				 */
661152465Srodrigc				continue;
662160303Sdes			} else if (strcmp(p, "late") == 0) {
663160303Sdes				/*
664160303Sdes				 * "late" is used to prevent certain file
665160303Sdes				 * systems from being mounted before late
666160303Sdes				 * in the boot cycle; for instance,
667160303Sdes				 * loopback NFS mounts can't be mounted
668160303Sdes				 * before mountd starts.
669160303Sdes				 */
670160303Sdes				continue;
671152732Srodrigc			} else if (strcmp(p, "userquota") == 0) {
672152732Srodrigc				continue;
673158400Smaxim			} else if (strncmp(p, userquotaeq,
674158400Smaxim			    sizeof(userquotaeq) - 1) == 0) {
675158400Smaxim				continue;
676152732Srodrigc			} else if (strcmp(p, "groupquota") == 0) {
677152732Srodrigc				continue;
678158400Smaxim			} else if (strncmp(p, groupquotaeq,
679158400Smaxim			    sizeof(groupquotaeq) - 1) == 0) {
680158400Smaxim				continue;
681152465Srodrigc			} else if (*p == '-') {
6821558Srgrimes				argv[argc++] = p;
6831558Srgrimes				p = strchr(p, '=');
684123268Strhodes				if (p != NULL) {
6851558Srgrimes					*p = '\0';
6861558Srgrimes					argv[argc++] = p+1;
6871558Srgrimes				}
688153038Srodrigc			} else {
689152344Srodrigc				argv[argc++] = strdup("-o");
6901558Srgrimes				argv[argc++] = p;
6911558Srgrimes			}
69246619Sjkoshy		}
6931558Srgrimes
6941558Srgrimes	*argcp = argc;
6951558Srgrimes}
6961558Srgrimes
69746619Sjkoshy
69846619Sjkoshychar *
699159473Sjmallettupdate_options(char *opts, char *fstab, int curflags)
70046619Sjkoshy{
70146619Sjkoshy	char *o, *p;
70246619Sjkoshy	char *cur;
70346619Sjkoshy	char *expopt, *newopt, *tmpopt;
70446619Sjkoshy
70546619Sjkoshy	if (opts == NULL)
706159473Sjmallett		return (strdup(""));
70746619Sjkoshy
70846619Sjkoshy	/* remove meta options from list */
70946619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_FSTAB);
71046619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_CURRENT);
71146619Sjkoshy	cur = flags2opts(curflags);
71246619Sjkoshy
71346619Sjkoshy	/*
71446619Sjkoshy	 * Expand all meta-options passed to us first.
71546619Sjkoshy	 */
71646619Sjkoshy	expopt = NULL;
71746619Sjkoshy	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
71846619Sjkoshy		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
71946619Sjkoshy			expopt = catopt(expopt, fstab);
72046619Sjkoshy		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
72146619Sjkoshy			expopt = catopt(expopt, cur);
72246619Sjkoshy		else
72346619Sjkoshy			expopt = catopt(expopt, o);
72446619Sjkoshy	}
72546619Sjkoshy	free(cur);
72646619Sjkoshy	free(opts);
72746619Sjkoshy
72846619Sjkoshy	/*
72946619Sjkoshy	 * Remove previous contradictory arguments. Given option "foo" we
73046619Sjkoshy	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
73146619Sjkoshy	 * and "foo" - so we can deal with possible options like "notice".
73246619Sjkoshy	 */
73346619Sjkoshy	newopt = NULL;
73446619Sjkoshy	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
73546619Sjkoshy		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
73646619Sjkoshy			errx(1, "malloc failed");
737125933Sgrog
73846619Sjkoshy		strcpy(tmpopt, "no");
73946619Sjkoshy		strcat(tmpopt, o);
74046619Sjkoshy		remopt(newopt, tmpopt);
74146619Sjkoshy		free(tmpopt);
74246619Sjkoshy
74346619Sjkoshy		if (strncmp("no", o, 2) == 0)
74446619Sjkoshy			remopt(newopt, o+2);
74546619Sjkoshy
74646619Sjkoshy		newopt = catopt(newopt, o);
74746619Sjkoshy	}
74846619Sjkoshy	free(expopt);
74946619Sjkoshy
750159473Sjmallett	return (newopt);
75146619Sjkoshy}
75246619Sjkoshy
7531558Srgrimesvoid
754159473Sjmallettremopt(char *string, const char *opt)
75546619Sjkoshy{
75646619Sjkoshy	char *o, *p, *r;
75746619Sjkoshy
75846619Sjkoshy	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
75946619Sjkoshy		return;
76046619Sjkoshy
76146619Sjkoshy	r = string;
76246619Sjkoshy
76346619Sjkoshy	for (p = string; (o = strsep(&p, ",")) != NULL;) {
76446619Sjkoshy		if (strcmp(opt, o) != 0) {
76546619Sjkoshy			if (*r == ',' && *o != '\0')
76646619Sjkoshy				r++;
76746619Sjkoshy			while ((*r++ = *o++) != '\0')
76846619Sjkoshy			    ;
76946619Sjkoshy			*--r = ',';
77046619Sjkoshy		}
77146619Sjkoshy	}
77246619Sjkoshy	*r = '\0';
77346619Sjkoshy}
77446619Sjkoshy
77546619Sjkoshyvoid
776159473Sjmallettusage(void)
7771558Srgrimes{
7781558Srgrimes
77937425Scharnier	(void)fprintf(stderr, "%s\n%s\n%s\n",
780160303Sdes"usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
781141611Sru"       mount [-dfpruvw] special | node",
782141611Sru"       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
7831558Srgrimes	exit(1);
7841558Srgrimes}
78517243Sjkh
78617243Sjkhvoid
787159473Sjmallettputfsent(const struct statfs *ent)
78817243Sjkh{
78923805Sbde	struct fstab *fst;
79046619Sjkoshy	char *opts;
791125933Sgrog
79246619Sjkoshy	opts = flags2opts(ent->f_flags);
793156229Skeramida
794156229Skeramida	/*
795156229Skeramida	 * "rw" is not a real mount option; this is why we print NULL as "rw"
796156229Skeramida	 * if opts is still NULL here.
797156229Skeramida	 */
79823805Sbde	printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
799156229Skeramida	    ent->f_fstypename, opts ? opts : "rw");
80046619Sjkoshy	free(opts);
80117243Sjkh
80228671Ssteve	if ((fst = getfsspec(ent->f_mntfromname)))
80323805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
80428671Ssteve	else if ((fst = getfsfile(ent->f_mntonname)))
80523805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
80656038Sgreen	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
80756038Sgreen		if (strcmp(ent->f_mntonname, "/") == 0)
80856038Sgreen			printf("\t1 1\n");
80956038Sgreen		else
81056038Sgreen			printf("\t2 2\n");
81156038Sgreen	} else
81223805Sbde		printf("\t0 0\n");
81317243Sjkh}
81446619Sjkoshy
81546619Sjkoshy
81646619Sjkoshychar *
817159473Sjmallettflags2opts(int flags)
81846619Sjkoshy{
81946619Sjkoshy	char *res;
82046619Sjkoshy
82146619Sjkoshy	res = NULL;
82246619Sjkoshy
823155997Srodrigc	if (flags & MNT_RDONLY)		res = catopt(res, "ro");
82446619Sjkoshy	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
82546619Sjkoshy	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
82646619Sjkoshy	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
82746619Sjkoshy	if (flags & MNT_UNION)		res = catopt(res, "union");
82846619Sjkoshy	if (flags & MNT_ASYNC)		res = catopt(res, "async");
82946619Sjkoshy	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
83046619Sjkoshy	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
83146619Sjkoshy	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
83246619Sjkoshy	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
83346619Sjkoshy	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
834101205Srwatson	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
835105118Srwatson	if (flags & MNT_ACLS)		res = catopt(res, "acls");
83646619Sjkoshy
837159473Sjmallett	return (res);
83846619Sjkoshy}
839