128671Ssteve/*-
21558Srgrimes * Copyright (c) 1980, 1989, 1993, 1994
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 4. Neither the name of the University nor the names of its contributors
141558Srgrimes *    may be used to endorse or promote products derived from this software
151558Srgrimes *    without specific prior written permission.
161558Srgrimes *
171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271558Srgrimes * SUCH DAMAGE.
281558Srgrimes */
291558Srgrimes
301558Srgrimes#ifndef lint
3128671Sstevestatic const char copyright[] =
321558Srgrimes"@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
331558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
3428671Ssteve#if 0
3523678Speterstatic char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
3637425Scharnier#endif
371558Srgrimes#endif /* not lint */
381558Srgrimes
39187093Sobrien#include <sys/cdefs.h>
40187093Sobrien__FBSDID("$FreeBSD$");
41187093Sobrien
421558Srgrimes#include <sys/param.h>
431558Srgrimes#include <sys/mount.h>
4423805Sbde#include <sys/stat.h>
451558Srgrimes#include <sys/wait.h>
461558Srgrimes
4795289Smux#include <ctype.h>
481558Srgrimes#include <err.h>
491558Srgrimes#include <errno.h>
501558Srgrimes#include <fstab.h>
51117031Sgordon#include <paths.h>
5223678Speter#include <pwd.h>
531558Srgrimes#include <signal.h>
54123242Siedowse#include <stdint.h>
551558Srgrimes#include <stdio.h>
561558Srgrimes#include <stdlib.h>
571558Srgrimes#include <string.h>
581558Srgrimes#include <unistd.h>
59166439Spjd#include <libutil.h>
601558Srgrimes
6128671Ssteve#include "extern.h"
6252055Sphk#include "mntopts.h"
631558Srgrimes#include "pathnames.h"
641558Srgrimes
6546619Sjkoshy/* `meta' options */
66125933Sgrog#define MOUNT_META_OPTION_FSTAB		"fstab"
6746619Sjkoshy#define MOUNT_META_OPTION_CURRENT	"current"
6846619Sjkoshy
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:
320224503Smckusick		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 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");
592225341Sjhb		free(optbuf);
593225341Sjhb		free(mountprog);
594225341Sjhb		mountprog = NULL;
5951558Srgrimes		return (0);
5961558Srgrimes	}
5971558Srgrimes
598152668Srodrigc	if (use_mountprog(vfstype)) {
599187035Sobrien		ret = exec_mountprog(name, execname, mnt_argv.a);
600152344Srodrigc	} else {
601187035Sobrien		ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
602152344Srodrigc	}
6031558Srgrimes
604152344Srodrigc	free(optbuf);
605225341Sjhb	free(mountprog);
606225341Sjhb	mountprog = NULL;
6071558Srgrimes
608152344Srodrigc	if (verbose) {
609152344Srodrigc		if (statfs(name, &sf) < 0) {
610152344Srodrigc			warn("statfs %s", name);
6111558Srgrimes			return (1);
6121558Srgrimes		}
613152344Srodrigc		if (fstab_style)
614152344Srodrigc			putfsent(&sf);
615152344Srodrigc		else
616152344Srodrigc			prmount(&sf);
6171558Srgrimes	}
6181558Srgrimes
619182570Smatteo	return (ret);
6201558Srgrimes}
6211558Srgrimes
6221558Srgrimesvoid
623151042Srodrigcprmount(struct statfs *sfp)
62423678Speter{
625204840Sbz	uint64_t flags;
626151043Srodrigc	unsigned int i;
6271558Srgrimes	struct opt *o;
62823678Speter	struct passwd *pw;
6291558Srgrimes
63052036Sn_hibma	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
63152036Sn_hibma	    sfp->f_fstypename);
6321558Srgrimes
63323678Speter	flags = sfp->f_flags & MNT_VISFLAGMASK;
634204840Sbz	for (o = optnames; flags != 0 && o->o_opt != 0; o++)
6351558Srgrimes		if (flags & o->o_opt) {
63652036Sn_hibma			(void)printf(", %s", o->o_name);
6371558Srgrimes			flags &= ~o->o_opt;
6381558Srgrimes		}
639125339Spjd	/*
640125339Spjd	 * Inform when file system is mounted by an unprivileged user
641125339Spjd	 * or privileged non-root user.
642125339Spjd	 */
643125365Snectar	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
64452036Sn_hibma		(void)printf(", mounted by ");
64523678Speter		if ((pw = getpwuid(sfp->f_owner)) != NULL)
64623678Speter			(void)printf("%s", pw->pw_name);
64723678Speter		else
64823678Speter			(void)printf("%d", sfp->f_owner);
64923678Speter	}
65065023Ssheldonh	if (verbose) {
65165023Ssheldonh		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
652123242Siedowse			(void)printf(", writes: sync %ju async %ju",
653123242Siedowse			    (uintmax_t)sfp->f_syncwrites,
654123242Siedowse			    (uintmax_t)sfp->f_asyncwrites);
65565023Ssheldonh		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
656123242Siedowse			(void)printf(", reads: sync %ju async %ju",
657123242Siedowse			    (uintmax_t)sfp->f_syncreads,
658123242Siedowse			    (uintmax_t)sfp->f_asyncreads);
659123162Siedowse		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
660123162Siedowse			printf(", fsid ");
661123162Siedowse			for (i = 0; i < sizeof(sfp->f_fsid); i++)
662123162Siedowse				printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
663123162Siedowse		}
66465023Ssheldonh	}
66552036Sn_hibma	(void)printf(")\n");
6661558Srgrimes}
6671558Srgrimes
6681558Srgrimesstruct statfs *
669151042Srodrigcgetmntpt(const char *name)
6701558Srgrimes{
6711558Srgrimes	struct statfs *mntbuf;
6721558Srgrimes	int i, mntsize;
6731558Srgrimes
674224503Smckusick	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
67552678Sgreen	for (i = mntsize - 1; i >= 0; i--) {
6761558Srgrimes		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
6771558Srgrimes		    strcmp(mntbuf[i].f_mntonname, name) == 0)
6781558Srgrimes			return (&mntbuf[i]);
67952678Sgreen	}
6801558Srgrimes	return (NULL);
6811558Srgrimes}
6821558Srgrimes
6831558Srgrimeschar *
684151042Srodrigccatopt(char *s0, const char *s1)
6851558Srgrimes{
6861558Srgrimes	size_t i;
6871558Srgrimes	char *cp;
6881558Srgrimes
68946619Sjkoshy	if (s1 == NULL || *s1 == '\0')
690159473Sjmallett		return (s0);
69146619Sjkoshy
6921558Srgrimes	if (s0 && *s0) {
6931558Srgrimes		i = strlen(s0) + strlen(s1) + 1 + 1;
6941558Srgrimes		if ((cp = malloc(i)) == NULL)
69537425Scharnier			errx(1, "malloc failed");
6961558Srgrimes		(void)snprintf(cp, i, "%s,%s", s0, s1);
6971558Srgrimes	} else
6981558Srgrimes		cp = strdup(s1);
6991558Srgrimes
7001558Srgrimes	if (s0)
7011558Srgrimes		free(s0);
7021558Srgrimes	return (cp);
7031558Srgrimes}
7041558Srgrimes
7051558Srgrimesvoid
706187035Sobrienmangle(char *options, struct cpa *a)
7071558Srgrimes{
708189397Srodrigc	char *p, *s, *val;
7091558Srgrimes
7101558Srgrimes	for (s = options; (p = strsep(&s, ",")) != NULL;)
71146619Sjkoshy		if (*p != '\0') {
712152465Srodrigc			if (strcmp(p, "noauto") == 0) {
713152465Srodrigc				/*
714152465Srodrigc				 * Do not pass noauto option to nmount().
715152465Srodrigc				 * or external mount program.  noauto is
716152465Srodrigc				 * only used to prevent mounting a filesystem
717152465Srodrigc				 * when 'mount -a' is specified, and is
718152465Srodrigc				 * not a real mount option.
719152465Srodrigc				 */
720152465Srodrigc				continue;
721160303Sdes			} else if (strcmp(p, "late") == 0) {
722160303Sdes				/*
723160303Sdes				 * "late" is used to prevent certain file
724160303Sdes				 * systems from being mounted before late
725160303Sdes				 * in the boot cycle; for instance,
726160303Sdes				 * loopback NFS mounts can't be mounted
727160303Sdes				 * before mountd starts.
728160303Sdes				 */
729160303Sdes				continue;
730222832Sdelphij			} else if (strcmp(p, "failok") == 0) {
731222832Sdelphij				/*
732222832Sdelphij				 * "failok" is used to prevent certain file
733222832Sdelphij				 * systems from being causing the system to
734222832Sdelphij				 * drop into single user mode in the boot
735222832Sdelphij				 * cycle, and is not a real mount option.
736222832Sdelphij				 */
737222832Sdelphij				continue;
738189397Srodrigc			} else if (strncmp(p, "mountprog", 9) == 0) {
739189397Srodrigc				/*
740189397Srodrigc				 * "mountprog" is used to force the use of
741189397Srodrigc				 * userland mount programs.
742189397Srodrigc				 */
743189397Srodrigc				val = strchr(p, '=');
744189397Srodrigc                        	if (val != NULL) {
745189397Srodrigc                                	++val;
746189397Srodrigc					if (*val != '\0')
747189397Srodrigc						mountprog = strdup(val);
748189397Srodrigc				}
749189397Srodrigc
750189397Srodrigc				if (mountprog == NULL) {
751189397Srodrigc					errx(1, "Need value for -o mountprog");
752189397Srodrigc				}
753189397Srodrigc				continue;
754152732Srodrigc			} else if (strcmp(p, "userquota") == 0) {
755152732Srodrigc				continue;
756158400Smaxim			} else if (strncmp(p, userquotaeq,
757158400Smaxim			    sizeof(userquotaeq) - 1) == 0) {
758158400Smaxim				continue;
759152732Srodrigc			} else if (strcmp(p, "groupquota") == 0) {
760152732Srodrigc				continue;
761158400Smaxim			} else if (strncmp(p, groupquotaeq,
762158400Smaxim			    sizeof(groupquotaeq) - 1) == 0) {
763158400Smaxim				continue;
764152465Srodrigc			} else if (*p == '-') {
765187035Sobrien				append_arg(a, p);
7661558Srgrimes				p = strchr(p, '=');
767123268Strhodes				if (p != NULL) {
7681558Srgrimes					*p = '\0';
769187035Sobrien					append_arg(a, p + 1);
7701558Srgrimes				}
771153038Srodrigc			} else {
772187035Sobrien				append_arg(a, strdup("-o"));
773187035Sobrien				append_arg(a, p);
7741558Srgrimes			}
77546619Sjkoshy		}
7761558Srgrimes}
7771558Srgrimes
77846619Sjkoshy
77946619Sjkoshychar *
780159473Sjmallettupdate_options(char *opts, char *fstab, int curflags)
78146619Sjkoshy{
78246619Sjkoshy	char *o, *p;
78346619Sjkoshy	char *cur;
78446619Sjkoshy	char *expopt, *newopt, *tmpopt;
78546619Sjkoshy
78646619Sjkoshy	if (opts == NULL)
787159473Sjmallett		return (strdup(""));
78846619Sjkoshy
78946619Sjkoshy	/* remove meta options from list */
79046619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_FSTAB);
79146619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_CURRENT);
79246619Sjkoshy	cur = flags2opts(curflags);
79346619Sjkoshy
79446619Sjkoshy	/*
79546619Sjkoshy	 * Expand all meta-options passed to us first.
79646619Sjkoshy	 */
79746619Sjkoshy	expopt = NULL;
79846619Sjkoshy	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
79946619Sjkoshy		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
80046619Sjkoshy			expopt = catopt(expopt, fstab);
80146619Sjkoshy		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
80246619Sjkoshy			expopt = catopt(expopt, cur);
80346619Sjkoshy		else
80446619Sjkoshy			expopt = catopt(expopt, o);
80546619Sjkoshy	}
80646619Sjkoshy	free(cur);
80746619Sjkoshy	free(opts);
80846619Sjkoshy
80946619Sjkoshy	/*
81046619Sjkoshy	 * Remove previous contradictory arguments. Given option "foo" we
81146619Sjkoshy	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
81246619Sjkoshy	 * and "foo" - so we can deal with possible options like "notice".
81346619Sjkoshy	 */
81446619Sjkoshy	newopt = NULL;
81546619Sjkoshy	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
81646619Sjkoshy		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
81746619Sjkoshy			errx(1, "malloc failed");
818125933Sgrog
81946619Sjkoshy		strcpy(tmpopt, "no");
82046619Sjkoshy		strcat(tmpopt, o);
82146619Sjkoshy		remopt(newopt, tmpopt);
82246619Sjkoshy		free(tmpopt);
82346619Sjkoshy
82446619Sjkoshy		if (strncmp("no", o, 2) == 0)
82546619Sjkoshy			remopt(newopt, o+2);
82646619Sjkoshy
82746619Sjkoshy		newopt = catopt(newopt, o);
82846619Sjkoshy	}
82946619Sjkoshy	free(expopt);
83046619Sjkoshy
831159473Sjmallett	return (newopt);
83246619Sjkoshy}
83346619Sjkoshy
8341558Srgrimesvoid
835159473Sjmallettremopt(char *string, const char *opt)
83646619Sjkoshy{
83746619Sjkoshy	char *o, *p, *r;
83846619Sjkoshy
83946619Sjkoshy	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
84046619Sjkoshy		return;
84146619Sjkoshy
84246619Sjkoshy	r = string;
84346619Sjkoshy
84446619Sjkoshy	for (p = string; (o = strsep(&p, ",")) != NULL;) {
84546619Sjkoshy		if (strcmp(opt, o) != 0) {
84646619Sjkoshy			if (*r == ',' && *o != '\0')
84746619Sjkoshy				r++;
84846619Sjkoshy			while ((*r++ = *o++) != '\0')
84946619Sjkoshy			    ;
85046619Sjkoshy			*--r = ',';
85146619Sjkoshy		}
85246619Sjkoshy	}
85346619Sjkoshy	*r = '\0';
85446619Sjkoshy}
85546619Sjkoshy
85646619Sjkoshyvoid
857159473Sjmallettusage(void)
8581558Srgrimes{
8591558Srgrimes
86037425Scharnier	(void)fprintf(stderr, "%s\n%s\n%s\n",
861160303Sdes"usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
862141611Sru"       mount [-dfpruvw] special | node",
863141611Sru"       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
8641558Srgrimes	exit(1);
8651558Srgrimes}
86617243Sjkh
86717243Sjkhvoid
868175949Smatteoputfsent(struct statfs *ent)
86917243Sjkh{
87023805Sbde	struct fstab *fst;
871213298Sjh	char *opts, *rw;
872168698Sphk	int l;
873125933Sgrog
874213298Sjh	opts = NULL;
875213298Sjh	/* flags2opts() doesn't return the "rw" option. */
876213298Sjh	if ((ent->f_flags & MNT_RDONLY) != 0)
877213298Sjh		rw = NULL;
878213298Sjh	else
879213298Sjh		rw = catopt(NULL, "rw");
880213298Sjh
88146619Sjkoshy	opts = flags2opts(ent->f_flags);
882213298Sjh	opts = catopt(rw, opts);
883156229Skeramida
884175949Smatteo	if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
885175949Smatteo	    strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
886175949Smatteo		strcpy(ent->f_mntfromname, (strnstr(ent->f_mntfromname, ":", 8)
887175949Smatteo		    +1));
888175949Smatteo	}
889175949Smatteo
890168698Sphk	l = strlen(ent->f_mntfromname);
891168698Sphk	printf("%s%s%s%s", ent->f_mntfromname,
892168698Sphk	    l < 8 ? "\t" : "",
893168698Sphk	    l < 16 ? "\t" : "",
894168698Sphk	    l < 24 ? "\t" : " ");
895168698Sphk	l = strlen(ent->f_mntonname);
896168698Sphk	printf("%s%s%s%s", ent->f_mntonname,
897168698Sphk	    l < 8 ? "\t" : "",
898168698Sphk	    l < 16 ? "\t" : "",
899168698Sphk	    l < 24 ? "\t" : " ");
900168698Sphk	printf("%s\t", ent->f_fstypename);
901213298Sjh	l = strlen(opts);
902213298Sjh	printf("%s%s", opts,
903213298Sjh	    l < 8 ? "\t" : " ");
90446619Sjkoshy	free(opts);
90517243Sjkh
90628671Ssteve	if ((fst = getfsspec(ent->f_mntfromname)))
90723805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
90828671Ssteve	else if ((fst = getfsfile(ent->f_mntonname)))
90923805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
91056038Sgreen	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
91156038Sgreen		if (strcmp(ent->f_mntonname, "/") == 0)
91256038Sgreen			printf("\t1 1\n");
91356038Sgreen		else
91456038Sgreen			printf("\t2 2\n");
91556038Sgreen	} else
91623805Sbde		printf("\t0 0\n");
91717243Sjkh}
91846619Sjkoshy
91946619Sjkoshy
92046619Sjkoshychar *
921159473Sjmallettflags2opts(int flags)
92246619Sjkoshy{
92346619Sjkoshy	char *res;
92446619Sjkoshy
92546619Sjkoshy	res = NULL;
92646619Sjkoshy
927155997Srodrigc	if (flags & MNT_RDONLY)		res = catopt(res, "ro");
92846619Sjkoshy	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
92946619Sjkoshy	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
93046619Sjkoshy	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
93146619Sjkoshy	if (flags & MNT_UNION)		res = catopt(res, "union");
93246619Sjkoshy	if (flags & MNT_ASYNC)		res = catopt(res, "async");
93346619Sjkoshy	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
93446619Sjkoshy	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
93546619Sjkoshy	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
93646619Sjkoshy	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
93746619Sjkoshy	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
938101205Srwatson	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
939105118Srwatson	if (flags & MNT_ACLS)		res = catopt(res, "acls");
940200796Strasz	if (flags & MNT_NFS4ACLS)	res = catopt(res, "nfsv4acls");
94146619Sjkoshy
942159473Sjmallett	return (res);
94346619Sjkoshy}
944