mount.c revision 101205
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 * 3. All advertising materials mentioning features or use of this software
141558Srgrimes *    must display the following acknowledgement:
151558Srgrimes *	This product includes software developed by the University of
161558Srgrimes *	California, Berkeley and its contributors.
171558Srgrimes * 4. Neither the name of the University nor the names of its contributors
181558Srgrimes *    may be used to endorse or promote products derived from this software
191558Srgrimes *    without specific prior written permission.
201558Srgrimes *
211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311558Srgrimes * SUCH DAMAGE.
321558Srgrimes */
331558Srgrimes
341558Srgrimes#ifndef lint
3528671Sstevestatic const char copyright[] =
361558Srgrimes"@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
371558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381558Srgrimes#endif /* not lint */
391558Srgrimes
401558Srgrimes#ifndef lint
4128671Ssteve#if 0
4223678Speterstatic char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
4337425Scharnier#endif
4428671Sstevestatic const char rcsid[] =
4550476Speter  "$FreeBSD: head/sbin/mount/mount.c 101205 2002-08-02 07:02:51Z rwatson $";
461558Srgrimes#endif /* not lint */
471558Srgrimes
481558Srgrimes#include <sys/param.h>
491558Srgrimes#include <sys/mount.h>
5023805Sbde#include <sys/stat.h>
511558Srgrimes#include <sys/wait.h>
521558Srgrimes
5395289Smux#include <ctype.h>
541558Srgrimes#include <err.h>
551558Srgrimes#include <errno.h>
561558Srgrimes#include <fstab.h>
5723678Speter#include <pwd.h>
581558Srgrimes#include <signal.h>
591558Srgrimes#include <stdio.h>
601558Srgrimes#include <stdlib.h>
611558Srgrimes#include <string.h>
621558Srgrimes#include <unistd.h>
631558Srgrimes
6428671Ssteve#include "extern.h"
6552055Sphk#include "mntopts.h"
661558Srgrimes#include "pathnames.h"
671558Srgrimes
6846619Sjkoshy/* `meta' options */
6946619Sjkoshy#define MOUNT_META_OPTION_FSTAB		"fstab"
7046619Sjkoshy#define MOUNT_META_OPTION_CURRENT	"current"
7146619Sjkoshy
7223805Sbdeint debug, fstab_style, verbose;
731558Srgrimes
7492882Simpchar   *catopt(char *, const char *);
7592882Simpstruct statfs *getmntpt(const char *);
7692882Simpint	hasopt(const char *, const char *);
7792882Simpint	ismounted(struct fstab *, struct statfs *, int);
7892882Simpint	isremountable(const char *);
7992882Simpvoid	mangle(char *, int *, const char **);
8092882Simpchar   *update_options(char *, char *, int);
8192882Simpint	mountfs(const char *, const char *, const char *,
8292882Simp			int, const char *, const char *);
8392882Simpvoid	remopt(char *, const char *);
8492882Simpvoid	prmount(struct statfs *);
8592882Simpvoid	putfsent(const struct statfs *);
8692882Simpvoid	usage(void);
8792882Simpchar   *flags2opts(int);
881558Srgrimes
8937425Scharnier/* Map from mount options to printable formats. */
901558Srgrimesstatic struct opt {
911558Srgrimes	int o_opt;
921558Srgrimes	const char *o_name;
931558Srgrimes} optnames[] = {
941558Srgrimes	{ MNT_ASYNC,		"asynchronous" },
951558Srgrimes	{ MNT_EXPORTED,		"NFS exported" },
961558Srgrimes	{ MNT_LOCAL,		"local" },
9718007Sdg	{ MNT_NOATIME,		"noatime" },
981558Srgrimes	{ MNT_NODEV,		"nodev" },
991558Srgrimes	{ MNT_NOEXEC,		"noexec" },
1001558Srgrimes	{ MNT_NOSUID,		"nosuid" },
10135105Swosch	{ MNT_NOSYMFOLLOW,	"nosymfollow" },
1021558Srgrimes	{ MNT_QUOTA,		"with quotas" },
1031558Srgrimes	{ MNT_RDONLY,		"read-only" },
1041558Srgrimes	{ MNT_SYNCHRONOUS,	"synchronous" },
1051558Srgrimes	{ MNT_UNION,		"union" },
10629890Skato	{ MNT_NOCLUSTERR,	"noclusterr" },
10729890Skato	{ MNT_NOCLUSTERW,	"noclusterw" },
10831144Sjulian	{ MNT_SUIDDIR,		"suiddir" },
10934266Sjulian	{ MNT_SOFTDEP,		"soft-updates" },
110101205Srwatson	{ MNT_MULTILABEL,	"multilabel" },
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
1251558Srgrimesint
1261558Srgrimesmain(argc, argv)
1271558Srgrimes	int argc;
1281558Srgrimes	char * const argv[];
1291558Srgrimes{
13023678Speter	const char *mntfromname, **vfslist, *vfstype;
1311558Srgrimes	struct fstab *fs;
1321558Srgrimes	struct statfs *mntbuf;
1331558Srgrimes	FILE *mountdfp;
1341558Srgrimes	pid_t pid;
13552678Sgreen	int all, ch, i, init_flags, mntsize, rval, have_fstab;
13695289Smux	char *cp, *ep, *options;
1371558Srgrimes
1381558Srgrimes	all = init_flags = 0;
1391558Srgrimes	options = NULL;
1401558Srgrimes	vfslist = NULL;
1411558Srgrimes	vfstype = "ufs";
14223805Sbde	while ((ch = getopt(argc, argv, "adfo:prwt:uv")) != -1)
1431558Srgrimes		switch (ch) {
1441558Srgrimes		case 'a':
1451558Srgrimes			all = 1;
1461558Srgrimes			break;
1471558Srgrimes		case 'd':
1481558Srgrimes			debug = 1;
1491558Srgrimes			break;
1501558Srgrimes		case 'f':
1511558Srgrimes			init_flags |= MNT_FORCE;
1521558Srgrimes			break;
1531558Srgrimes		case 'o':
1541558Srgrimes			if (*optarg)
1551558Srgrimes				options = catopt(options, optarg);
1561558Srgrimes			break;
15723805Sbde		case 'p':
15823805Sbde			fstab_style = 1;
15923805Sbde			verbose = 1;
16023805Sbde			break;
1611558Srgrimes		case 'r':
16276198Sdd			options = catopt(options, "ro");
1631558Srgrimes			break;
1641558Srgrimes		case 't':
1651558Srgrimes			if (vfslist != NULL)
16637425Scharnier				errx(1, "only one -t option may be specified");
1671558Srgrimes			vfslist = makevfslist(optarg);
1681558Srgrimes			vfstype = optarg;
1691558Srgrimes			break;
1701558Srgrimes		case 'u':
1711558Srgrimes			init_flags |= MNT_UPDATE;
1721558Srgrimes			break;
1731558Srgrimes		case 'v':
1741558Srgrimes			verbose = 1;
1751558Srgrimes			break;
1761558Srgrimes		case 'w':
17776198Sdd			options = catopt(options, "noro");
1781558Srgrimes			break;
1791558Srgrimes		case '?':
1801558Srgrimes		default:
1811558Srgrimes			usage();
1821558Srgrimes			/* NOTREACHED */
1831558Srgrimes		}
1841558Srgrimes	argc -= optind;
1851558Srgrimes	argv += optind;
1861558Srgrimes
1871558Srgrimes#define	BADTYPE(type)							\
1881558Srgrimes	(strcmp(type, FSTAB_RO) &&					\
1891558Srgrimes	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
1901558Srgrimes
1911558Srgrimes	rval = 0;
1921558Srgrimes	switch (argc) {
1931558Srgrimes	case 0:
19428671Ssteve		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
19528671Ssteve			err(1, "getmntinfo");
19628671Ssteve		if (all) {
1971558Srgrimes			while ((fs = getfsent()) != NULL) {
1981558Srgrimes				if (BADTYPE(fs->fs_type))
1991558Srgrimes					continue;
20023678Speter				if (checkvfsname(fs->fs_vfstype, vfslist))
2011558Srgrimes					continue;
20223678Speter				if (hasopt(fs->fs_mntops, "noauto"))
20310288Sdg					continue;
20444811Sbde				if (!(init_flags & MNT_UPDATE) &&
20544811Sbde				    ismounted(fs, mntbuf, mntsize))
20628671Ssteve					continue;
2071558Srgrimes				if (mountfs(fs->fs_vfstype, fs->fs_spec,
20823805Sbde				    fs->fs_file, init_flags, options,
20923805Sbde				    fs->fs_mntops))
21023805Sbde					rval = 1;
2111558Srgrimes			}
21228671Ssteve		} else if (fstab_style) {
21317243Sjkh			for (i = 0; i < mntsize; i++) {
21423678Speter				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
21517243Sjkh					continue;
21623805Sbde				putfsent(&mntbuf[i]);
21717243Sjkh			}
21823805Sbde		} else {
2191558Srgrimes			for (i = 0; i < mntsize; i++) {
22023805Sbde				if (checkvfsname(mntbuf[i].f_fstypename,
22123805Sbde				    vfslist))
2221558Srgrimes					continue;
22323678Speter				prmount(&mntbuf[i]);
2241558Srgrimes			}
2251558Srgrimes		}
2261558Srgrimes		exit(rval);
2271558Srgrimes	case 1:
2281558Srgrimes		if (vfslist != NULL)
2291558Srgrimes			usage();
2301558Srgrimes
2311558Srgrimes		if (init_flags & MNT_UPDATE) {
23252678Sgreen			mntfromname = NULL;
23352678Sgreen			have_fstab = 0;
2341558Srgrimes			if ((mntbuf = getmntpt(*argv)) == NULL)
23552678Sgreen				errx(1, "not currently mounted %s", *argv);
23652678Sgreen			/*
23752678Sgreen			 * Only get the mntflags from fstab if both mntpoint
23852678Sgreen			 * and mntspec are identical. Also handle the special
23952678Sgreen			 * case where just '/' is mounted and 'spec' is not
24052678Sgreen			 * identical with the one from fstab ('/dev' is missing
24152678Sgreen			 * in the spec-string at boot-time).
24252678Sgreen			 */
24346619Sjkoshy			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
24452678Sgreen				if (strcmp(fs->fs_spec,
24552678Sgreen				    mntbuf->f_mntfromname) == 0 &&
24652678Sgreen				    strcmp(fs->fs_file,
24752678Sgreen				    mntbuf->f_mntonname) == 0) {
24852678Sgreen					have_fstab = 1;
24952678Sgreen					mntfromname = mntbuf->f_mntfromname;
25052678Sgreen				} else if (argv[0][0] == '/' &&
25152678Sgreen				    argv[0][1] == '\0') {
25252678Sgreen					fs = getfsfile("/");
25352678Sgreen					have_fstab = 1;
25452678Sgreen					mntfromname = fs->fs_spec;
25552678Sgreen				}
25652678Sgreen			}
25752678Sgreen			if (have_fstab) {
25846619Sjkoshy				options = update_options(options, fs->fs_mntops,
25946619Sjkoshy				    mntbuf->f_flags);
26046619Sjkoshy			} else {
26123678Speter				mntfromname = mntbuf->f_mntfromname;
26246619Sjkoshy				options = update_options(options, NULL,
26346619Sjkoshy				    mntbuf->f_flags);
26446619Sjkoshy			}
26523678Speter			rval = mountfs(mntbuf->f_fstypename, mntfromname,
26623678Speter			    mntbuf->f_mntonname, init_flags, options, 0);
26723678Speter			break;
2681558Srgrimes		}
26955118Seivind		rmslashes(*argv, *argv);
27023678Speter		if ((fs = getfsfile(*argv)) == NULL &&
27123678Speter		    (fs = getfsspec(*argv)) == NULL)
27296707Strhodes			errx(1, "%s: unknown special file or filesystem",
27323678Speter			    *argv);
27423678Speter		if (BADTYPE(fs->fs_type))
27596707Strhodes			errx(1, "%s has unknown filesystem type",
27623678Speter			    *argv);
27723678Speter		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
27823678Speter		    init_flags, options, fs->fs_mntops);
2791558Srgrimes		break;
2801558Srgrimes	case 2:
2811558Srgrimes		/*
28252678Sgreen		 * If -t flag has not been specified, the path cannot be
28395289Smux		 * found, spec contains either a ':' or a '@', then assume
28452678Sgreen		 * that an NFS filesystem is being specified ala Sun.
28595289Smux		 * Check if the hostname contains only allowed characters
28695289Smux		 * to reduce false positives.  IPv6 addresses containing
28795289Smux		 * ':' will be correctly parsed only if the separator is '@'.
28895289Smux		 * The definition of a valid hostname is taken from RFC 1034.
2891558Srgrimes		 */
29095289Smux		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL) ||
29195289Smux		    ((ep = strchr(argv[0], ':')) != NULL)) {
29295318Smux			if (*ep == '@') {
29395318Smux				cp = ep + 1;
29495318Smux				ep = cp + strlen(cp);
29595318Smux			} else
29695318Smux				cp = argv[0];
29795289Smux			while (cp != ep) {
29895289Smux				if (!isdigit(*cp) && !isalpha(*cp) &&
29995289Smux				    *cp != '.' && *cp != '-' && *cp != ':')
30095289Smux					break;
30195289Smux				cp++;
30295289Smux			}
30395289Smux			if (cp == ep)
30495289Smux				vfstype = "nfs";
30595289Smux		}
3061558Srgrimes		rval = mountfs(vfstype,
3071558Srgrimes		    argv[0], argv[1], init_flags, options, NULL);
3081558Srgrimes		break;
3091558Srgrimes	default:
3101558Srgrimes		usage();
3111558Srgrimes		/* NOTREACHED */
3121558Srgrimes	}
3131558Srgrimes
3141558Srgrimes	/*
3151558Srgrimes	 * If the mount was successfully, and done by root, tell mountd the
3161558Srgrimes	 * good news.  Pid checks are probably unnecessary, but don't hurt.
3171558Srgrimes	 */
3181558Srgrimes	if (rval == 0 && getuid() == 0 &&
3191558Srgrimes	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
32028671Ssteve		if (fscanf(mountdfp, "%d", &pid) == 1 &&
3211558Srgrimes		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
3221558Srgrimes			err(1, "signal mountd");
3231558Srgrimes		(void)fclose(mountdfp);
3241558Srgrimes	}
3251558Srgrimes
3261558Srgrimes	exit(rval);
3271558Srgrimes}
3281558Srgrimes
3291558Srgrimesint
33028671Ssteveismounted(fs, mntbuf, mntsize)
33128671Ssteve	struct fstab *fs;
33228671Ssteve	struct statfs *mntbuf;
33328671Ssteve	int mntsize;
33428671Ssteve{
33528671Ssteve	int i;
33628671Ssteve
33728671Ssteve	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
33896707Strhodes		/* the root filesystem can always be remounted */
33928671Ssteve		return (0);
34028671Ssteve
34128671Ssteve	for (i = mntsize - 1; i >= 0; --i)
34228671Ssteve		if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
34328671Ssteve		    (!isremountable(fs->fs_vfstype) ||
34428671Ssteve		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
34528671Ssteve			return (1);
34628671Ssteve	return (0);
34728671Ssteve}
34828671Ssteve
34928671Ssteveint
35028671Ssteveisremountable(vfsname)
35128671Ssteve	const char *vfsname;
35228671Ssteve{
35328671Ssteve	const char **cp;
35428671Ssteve
35528671Ssteve	for (cp = remountable_fs_names; *cp; cp++)
35628671Ssteve		if (strcmp(*cp, vfsname) == 0)
35728671Ssteve			return (1);
35828671Ssteve	return (0);
35928671Ssteve}
36028671Ssteve
36128671Ssteveint
36223678Speterhasopt(mntopts, option)
36323678Speter	const char *mntopts, *option;
36423678Speter{
36523678Speter	int negative, found;
36623678Speter	char *opt, *optbuf;
36723678Speter
36823678Speter	if (option[0] == 'n' && option[1] == 'o') {
36923678Speter		negative = 1;
37023678Speter		option += 2;
37123678Speter	} else
37223678Speter		negative = 0;
37323678Speter	optbuf = strdup(mntopts);
37423678Speter	found = 0;
37523678Speter	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
37623678Speter		if (opt[0] == 'n' && opt[1] == 'o') {
37723678Speter			if (!strcasecmp(opt + 2, option))
37823678Speter				found = negative;
37923678Speter		} else if (!strcasecmp(opt, option))
38023678Speter			found = !negative;
38123678Speter	}
38223678Speter	free(optbuf);
38323678Speter	return (found);
38423678Speter}
38523678Speter
38623678Speterint
3871558Srgrimesmountfs(vfstype, spec, name, flags, options, mntopts)
3881558Srgrimes	const char *vfstype, *spec, *name, *options, *mntopts;
3891558Srgrimes	int flags;
3901558Srgrimes{
3911558Srgrimes	/* List of directories containing mount_xxx subcommands. */
3921558Srgrimes	static const char *edirs[] = {
3931558Srgrimes		_PATH_SBIN,
3941558Srgrimes		_PATH_USRSBIN,
3951558Srgrimes		NULL
3961558Srgrimes	};
3971558Srgrimes	const char *argv[100], **edir;
3981558Srgrimes	struct statfs sf;
3991558Srgrimes	pid_t pid;
4001558Srgrimes	int argc, i, status;
4011558Srgrimes	char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
4021558Srgrimes
40328671Ssteve#if __GNUC__
40428671Ssteve	(void)&optbuf;
40528671Ssteve	(void)&name;
40628671Ssteve#endif
40728671Ssteve
40852055Sphk	/* resolve the mountpoint with realpath(3) */
40952055Sphk	(void)checkpath(name, mntpath);
4101558Srgrimes	name = mntpath;
4111558Srgrimes
41223678Speter	if (mntopts == NULL)
41323678Speter		mntopts = "";
4141558Srgrimes	if (options == NULL) {
41523678Speter		if (*mntopts == '\0') {
4161558Srgrimes			options = "rw";
41723678Speter		} else {
4181558Srgrimes			options = mntopts;
41923678Speter			mntopts = "";
42023678Speter		}
4211558Srgrimes	}
4221558Srgrimes	optbuf = catopt(strdup(mntopts), options);
4231558Srgrimes
4241558Srgrimes	if (strcmp(name, "/") == 0)
4251558Srgrimes		flags |= MNT_UPDATE;
4261558Srgrimes	if (flags & MNT_FORCE)
4271558Srgrimes		optbuf = catopt(optbuf, "force");
4281558Srgrimes	if (flags & MNT_RDONLY)
4291558Srgrimes		optbuf = catopt(optbuf, "ro");
4301558Srgrimes	/*
4311558Srgrimes	 * XXX
4321558Srgrimes	 * The mount_mfs (newfs) command uses -o to select the
43337425Scharnier	 * optimization mode.  We don't pass the default "-o rw"
4341558Srgrimes	 * for that reason.
4351558Srgrimes	 */
4361558Srgrimes	if (flags & MNT_UPDATE)
4371558Srgrimes		optbuf = catopt(optbuf, "update");
4381558Srgrimes
43977577Sru	/* Compatibility glue. */
44077577Sru	if (strcmp(vfstype, "msdos") == 0)
44177577Sru		vfstype = "msdosfs";
44277577Sru
4431558Srgrimes	argc = 0;
4441558Srgrimes	argv[argc++] = vfstype;
4451558Srgrimes	mangle(optbuf, &argc, argv);
4461558Srgrimes	argv[argc++] = spec;
4471558Srgrimes	argv[argc++] = name;
4481558Srgrimes	argv[argc] = NULL;
4491558Srgrimes
4501558Srgrimes	if (debug) {
4511558Srgrimes		(void)printf("exec: mount_%s", vfstype);
4521558Srgrimes		for (i = 1; i < argc; i++)
4531558Srgrimes			(void)printf(" %s", argv[i]);
4541558Srgrimes		(void)printf("\n");
4551558Srgrimes		return (0);
4561558Srgrimes	}
4571558Srgrimes
45825120Sache	switch (pid = fork()) {
4591558Srgrimes	case -1:				/* Error. */
46025120Sache		warn("fork");
4611558Srgrimes		free(optbuf);
4621558Srgrimes		return (1);
4631558Srgrimes	case 0:					/* Child. */
4641558Srgrimes		if (strcmp(vfstype, "ufs") == 0)
4651558Srgrimes			exit(mount_ufs(argc, (char * const *) argv));
4661558Srgrimes
4671558Srgrimes		/* Go find an executable. */
46814626Sasami		for (edir = edirs; *edir; edir++) {
4691558Srgrimes			(void)snprintf(execname,
4701558Srgrimes			    sizeof(execname), "%s/mount_%s", *edir, vfstype);
4711558Srgrimes			execv(execname, (char * const *)argv);
47214626Sasami		}
47314626Sasami		if (errno == ENOENT) {
47414626Sasami			int len = 0;
47514626Sasami			char *cp;
47614626Sasami			for (edir = edirs; *edir; edir++)
47714626Sasami				len += strlen(*edir) + 2;	/* ", " */
47837425Scharnier			if ((cp = malloc(len)) == NULL)
47937425Scharnier				errx(1, "malloc failed");
48014626Sasami			cp[0] = '\0';
48114626Sasami			for (edir = edirs; *edir; edir++) {
48214626Sasami				strcat(cp, *edir);
48314626Sasami				if (edir[1] != NULL)
48414626Sasami					strcat(cp, ", ");
48514626Sasami			}
48614626Sasami			warn("exec mount_%s not found in %s", vfstype, cp);
48714626Sasami		}
4881558Srgrimes		exit(1);
4891558Srgrimes		/* NOTREACHED */
4901558Srgrimes	default:				/* Parent. */
4911558Srgrimes		free(optbuf);
4921558Srgrimes
4931558Srgrimes		if (waitpid(pid, &status, 0) < 0) {
4941558Srgrimes			warn("waitpid");
4951558Srgrimes			return (1);
4961558Srgrimes		}
4971558Srgrimes
4981558Srgrimes		if (WIFEXITED(status)) {
4991558Srgrimes			if (WEXITSTATUS(status) != 0)
5001558Srgrimes				return (WEXITSTATUS(status));
5011558Srgrimes		} else if (WIFSIGNALED(status)) {
5021558Srgrimes			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
5031558Srgrimes			return (1);
5041558Srgrimes		}
5051558Srgrimes
5061558Srgrimes		if (verbose) {
5071558Srgrimes			if (statfs(name, &sf) < 0) {
50823678Speter				warn("statfs %s", name);
5091558Srgrimes				return (1);
5101558Srgrimes			}
51117243Sjkh			if (fstab_style)
51223805Sbde				putfsent(&sf);
51317243Sjkh			else
51423678Speter				prmount(&sf);
5151558Srgrimes		}
5161558Srgrimes		break;
5171558Srgrimes	}
5181558Srgrimes
5191558Srgrimes	return (0);
5201558Srgrimes}
5211558Srgrimes
5221558Srgrimesvoid
52323678Speterprmount(sfp)
52423678Speter	struct statfs *sfp;
52523678Speter{
5261558Srgrimes	int flags;
5271558Srgrimes	struct opt *o;
52823678Speter	struct passwd *pw;
5291558Srgrimes
53052036Sn_hibma	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
53152036Sn_hibma	    sfp->f_fstypename);
5321558Srgrimes
53323678Speter	flags = sfp->f_flags & MNT_VISFLAGMASK;
53452036Sn_hibma	for (o = optnames; flags && o->o_opt; o++)
5351558Srgrimes		if (flags & o->o_opt) {
53652036Sn_hibma			(void)printf(", %s", o->o_name);
5371558Srgrimes			flags &= ~o->o_opt;
5381558Srgrimes		}
53923678Speter	if (sfp->f_owner) {
54052036Sn_hibma		(void)printf(", mounted by ");
54123678Speter		if ((pw = getpwuid(sfp->f_owner)) != NULL)
54223678Speter			(void)printf("%s", pw->pw_name);
54323678Speter		else
54423678Speter			(void)printf("%d", sfp->f_owner);
54523678Speter	}
54665023Ssheldonh	if (verbose) {
54765023Ssheldonh		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
54865023Ssheldonh			(void)printf(", writes: sync %ld async %ld",
54965023Ssheldonh			    sfp->f_syncwrites, sfp->f_asyncwrites);
55065023Ssheldonh		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
55165023Ssheldonh			(void)printf(", reads: sync %ld async %ld",
55265023Ssheldonh			    sfp->f_syncreads, sfp->f_asyncreads);
55365023Ssheldonh	}
55452036Sn_hibma	(void)printf(")\n");
5551558Srgrimes}
5561558Srgrimes
5571558Srgrimesstruct statfs *
5581558Srgrimesgetmntpt(name)
5591558Srgrimes	const char *name;
5601558Srgrimes{
5611558Srgrimes	struct statfs *mntbuf;
5621558Srgrimes	int i, mntsize;
5631558Srgrimes
5641558Srgrimes	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
56552678Sgreen	for (i = mntsize - 1; i >= 0; i--) {
5661558Srgrimes		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
5671558Srgrimes		    strcmp(mntbuf[i].f_mntonname, name) == 0)
5681558Srgrimes			return (&mntbuf[i]);
56952678Sgreen	}
5701558Srgrimes	return (NULL);
5711558Srgrimes}
5721558Srgrimes
5731558Srgrimeschar *
5741558Srgrimescatopt(s0, s1)
5751558Srgrimes	char *s0;
5761558Srgrimes	const char *s1;
5771558Srgrimes{
5781558Srgrimes	size_t i;
5791558Srgrimes	char *cp;
5801558Srgrimes
58146619Sjkoshy	if (s1 == NULL || *s1 == '\0')
58246619Sjkoshy		return s0;
58346619Sjkoshy
5841558Srgrimes	if (s0 && *s0) {
5851558Srgrimes		i = strlen(s0) + strlen(s1) + 1 + 1;
5861558Srgrimes		if ((cp = malloc(i)) == NULL)
58737425Scharnier			errx(1, "malloc failed");
5881558Srgrimes		(void)snprintf(cp, i, "%s,%s", s0, s1);
5891558Srgrimes	} else
5901558Srgrimes		cp = strdup(s1);
5911558Srgrimes
5921558Srgrimes	if (s0)
5931558Srgrimes		free(s0);
5941558Srgrimes	return (cp);
5951558Srgrimes}
5961558Srgrimes
5971558Srgrimesvoid
5981558Srgrimesmangle(options, argcp, argv)
5991558Srgrimes	char *options;
6001558Srgrimes	int *argcp;
6011558Srgrimes	const char **argv;
6021558Srgrimes{
6031558Srgrimes	char *p, *s;
6041558Srgrimes	int argc;
6051558Srgrimes
6061558Srgrimes	argc = *argcp;
6071558Srgrimes	for (s = options; (p = strsep(&s, ",")) != NULL;)
60846619Sjkoshy		if (*p != '\0') {
6091558Srgrimes			if (*p == '-') {
6101558Srgrimes				argv[argc++] = p;
6111558Srgrimes				p = strchr(p, '=');
6121558Srgrimes				if (p) {
6131558Srgrimes					*p = '\0';
6141558Srgrimes					argv[argc++] = p+1;
6151558Srgrimes				}
6161558Srgrimes			} else if (strcmp(p, "rw") != 0) {
6171558Srgrimes				argv[argc++] = "-o";
6181558Srgrimes				argv[argc++] = p;
6191558Srgrimes			}
62046619Sjkoshy		}
6211558Srgrimes
6221558Srgrimes	*argcp = argc;
6231558Srgrimes}
6241558Srgrimes
62546619Sjkoshy
62646619Sjkoshychar *
62746619Sjkoshyupdate_options(opts, fstab, curflags)
62846619Sjkoshy	char *opts;
62946619Sjkoshy	char *fstab;
63046619Sjkoshy	int curflags;
63146619Sjkoshy{
63246619Sjkoshy	char *o, *p;
63346619Sjkoshy	char *cur;
63446619Sjkoshy	char *expopt, *newopt, *tmpopt;
63546619Sjkoshy
63646619Sjkoshy	if (opts == NULL)
63746619Sjkoshy		return strdup("");
63846619Sjkoshy
63946619Sjkoshy	/* remove meta options from list */
64046619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_FSTAB);
64146619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_CURRENT);
64246619Sjkoshy	cur = flags2opts(curflags);
64346619Sjkoshy
64446619Sjkoshy	/*
64546619Sjkoshy	 * Expand all meta-options passed to us first.
64646619Sjkoshy	 */
64746619Sjkoshy	expopt = NULL;
64846619Sjkoshy	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
64946619Sjkoshy		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
65046619Sjkoshy			expopt = catopt(expopt, fstab);
65146619Sjkoshy		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
65246619Sjkoshy			expopt = catopt(expopt, cur);
65346619Sjkoshy		else
65446619Sjkoshy			expopt = catopt(expopt, o);
65546619Sjkoshy	}
65646619Sjkoshy	free(cur);
65746619Sjkoshy	free(opts);
65846619Sjkoshy
65946619Sjkoshy	/*
66046619Sjkoshy	 * Remove previous contradictory arguments. Given option "foo" we
66146619Sjkoshy	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
66246619Sjkoshy	 * and "foo" - so we can deal with possible options like "notice".
66346619Sjkoshy	 */
66446619Sjkoshy	newopt = NULL;
66546619Sjkoshy	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
66646619Sjkoshy		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
66746619Sjkoshy			errx(1, "malloc failed");
66846619Sjkoshy
66946619Sjkoshy		strcpy(tmpopt, "no");
67046619Sjkoshy		strcat(tmpopt, o);
67146619Sjkoshy		remopt(newopt, tmpopt);
67246619Sjkoshy		free(tmpopt);
67346619Sjkoshy
67446619Sjkoshy		if (strncmp("no", o, 2) == 0)
67546619Sjkoshy			remopt(newopt, o+2);
67646619Sjkoshy
67746619Sjkoshy		newopt = catopt(newopt, o);
67846619Sjkoshy	}
67946619Sjkoshy	free(expopt);
68046619Sjkoshy
68146619Sjkoshy	return newopt;
68246619Sjkoshy}
68346619Sjkoshy
6841558Srgrimesvoid
68546619Sjkoshyremopt(string, opt)
68646619Sjkoshy	char *string;
68746619Sjkoshy 	const char *opt;
68846619Sjkoshy{
68946619Sjkoshy	char *o, *p, *r;
69046619Sjkoshy
69146619Sjkoshy	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
69246619Sjkoshy		return;
69346619Sjkoshy
69446619Sjkoshy	r = string;
69546619Sjkoshy
69646619Sjkoshy	for (p = string; (o = strsep(&p, ",")) != NULL;) {
69746619Sjkoshy		if (strcmp(opt, o) != 0) {
69846619Sjkoshy			if (*r == ',' && *o != '\0')
69946619Sjkoshy				r++;
70046619Sjkoshy			while ((*r++ = *o++) != '\0')
70146619Sjkoshy			    ;
70246619Sjkoshy			*--r = ',';
70346619Sjkoshy		}
70446619Sjkoshy	}
70546619Sjkoshy	*r = '\0';
70646619Sjkoshy}
70746619Sjkoshy
70846619Sjkoshyvoid
7091558Srgrimesusage()
7101558Srgrimes{
7111558Srgrimes
71237425Scharnier	(void)fprintf(stderr, "%s\n%s\n%s\n",
71337425Scharnier"usage: mount [-dfpruvw] [-o options] [-t ufs | external_type] special node",
71437425Scharnier"       mount [-adfpruvw] [-t ufs | external_type]",
71537425Scharnier"       mount [-dfpruvw] special | node");
7161558Srgrimes	exit(1);
7171558Srgrimes}
71817243Sjkh
71917243Sjkhvoid
72023805Sbdeputfsent(ent)
72123805Sbde	const struct statfs *ent;
72217243Sjkh{
72323805Sbde	struct fstab *fst;
72446619Sjkoshy	char *opts;
72546619Sjkoshy
72646619Sjkoshy	opts = flags2opts(ent->f_flags);
72723805Sbde	printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
72846619Sjkoshy	    ent->f_fstypename, opts);
72946619Sjkoshy	free(opts);
73017243Sjkh
73128671Ssteve	if ((fst = getfsspec(ent->f_mntfromname)))
73223805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
73328671Ssteve	else if ((fst = getfsfile(ent->f_mntonname)))
73423805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
73556038Sgreen	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
73656038Sgreen		if (strcmp(ent->f_mntonname, "/") == 0)
73756038Sgreen			printf("\t1 1\n");
73856038Sgreen		else
73956038Sgreen			printf("\t2 2\n");
74056038Sgreen	} else
74123805Sbde		printf("\t0 0\n");
74217243Sjkh}
74346619Sjkoshy
74446619Sjkoshy
74546619Sjkoshychar *
74646619Sjkoshyflags2opts(flags)
74746619Sjkoshy	int flags;
74846619Sjkoshy{
74946619Sjkoshy	char *res;
75046619Sjkoshy
75146619Sjkoshy	res = NULL;
75246619Sjkoshy
75346619Sjkoshy	res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
75446619Sjkoshy
75546619Sjkoshy	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
75646619Sjkoshy	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
75746619Sjkoshy	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
75846619Sjkoshy	if (flags & MNT_NODEV)		res = catopt(res, "nodev");
75946619Sjkoshy	if (flags & MNT_UNION)		res = catopt(res, "union");
76046619Sjkoshy	if (flags & MNT_ASYNC)		res = catopt(res, "async");
76146619Sjkoshy	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
76246619Sjkoshy	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
76346619Sjkoshy	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
76446619Sjkoshy	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
76546619Sjkoshy	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
766101205Srwatson	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
76746619Sjkoshy
76846619Sjkoshy	return res;
76946619Sjkoshy}
770