mount.c revision 95318
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 95318 2002-04-23 17:24:02Z mux $";
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" },
11046619Sjkoshy	{ 0, NULL }
1111558Srgrimes};
1121558Srgrimes
11328671Ssteve/*
11428671Ssteve * List of VFS types that can be remounted without becoming mounted on top
11528671Ssteve * of each other.
11628671Ssteve * XXX Is this list correct?
11728671Ssteve */
11828671Sstevestatic const char *
11928671Ssteveremountable_fs_names[] = {
12069056Sphantom	"ufs", "ffs", "ext2fs",
12128671Ssteve	0
12228671Ssteve};
12328671Ssteve
1241558Srgrimesint
1251558Srgrimesmain(argc, argv)
1261558Srgrimes	int argc;
1271558Srgrimes	char * const argv[];
1281558Srgrimes{
12923678Speter	const char *mntfromname, **vfslist, *vfstype;
1301558Srgrimes	struct fstab *fs;
1311558Srgrimes	struct statfs *mntbuf;
1321558Srgrimes	FILE *mountdfp;
1331558Srgrimes	pid_t pid;
13452678Sgreen	int all, ch, i, init_flags, mntsize, rval, have_fstab;
13595289Smux	char *cp, *ep, *options;
1361558Srgrimes
1371558Srgrimes	all = init_flags = 0;
1381558Srgrimes	options = NULL;
1391558Srgrimes	vfslist = NULL;
1401558Srgrimes	vfstype = "ufs";
14123805Sbde	while ((ch = getopt(argc, argv, "adfo:prwt:uv")) != -1)
1421558Srgrimes		switch (ch) {
1431558Srgrimes		case 'a':
1441558Srgrimes			all = 1;
1451558Srgrimes			break;
1461558Srgrimes		case 'd':
1471558Srgrimes			debug = 1;
1481558Srgrimes			break;
1491558Srgrimes		case 'f':
1501558Srgrimes			init_flags |= MNT_FORCE;
1511558Srgrimes			break;
1521558Srgrimes		case 'o':
1531558Srgrimes			if (*optarg)
1541558Srgrimes				options = catopt(options, optarg);
1551558Srgrimes			break;
15623805Sbde		case 'p':
15723805Sbde			fstab_style = 1;
15823805Sbde			verbose = 1;
15923805Sbde			break;
1601558Srgrimes		case 'r':
16176198Sdd			options = catopt(options, "ro");
1621558Srgrimes			break;
1631558Srgrimes		case 't':
1641558Srgrimes			if (vfslist != NULL)
16537425Scharnier				errx(1, "only one -t option may be specified");
1661558Srgrimes			vfslist = makevfslist(optarg);
1671558Srgrimes			vfstype = optarg;
1681558Srgrimes			break;
1691558Srgrimes		case 'u':
1701558Srgrimes			init_flags |= MNT_UPDATE;
1711558Srgrimes			break;
1721558Srgrimes		case 'v':
1731558Srgrimes			verbose = 1;
1741558Srgrimes			break;
1751558Srgrimes		case 'w':
17676198Sdd			options = catopt(options, "noro");
1771558Srgrimes			break;
1781558Srgrimes		case '?':
1791558Srgrimes		default:
1801558Srgrimes			usage();
1811558Srgrimes			/* NOTREACHED */
1821558Srgrimes		}
1831558Srgrimes	argc -= optind;
1841558Srgrimes	argv += optind;
1851558Srgrimes
1861558Srgrimes#define	BADTYPE(type)							\
1871558Srgrimes	(strcmp(type, FSTAB_RO) &&					\
1881558Srgrimes	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
1891558Srgrimes
1901558Srgrimes	rval = 0;
1911558Srgrimes	switch (argc) {
1921558Srgrimes	case 0:
19328671Ssteve		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
19428671Ssteve			err(1, "getmntinfo");
19528671Ssteve		if (all) {
1961558Srgrimes			while ((fs = getfsent()) != NULL) {
1971558Srgrimes				if (BADTYPE(fs->fs_type))
1981558Srgrimes					continue;
19923678Speter				if (checkvfsname(fs->fs_vfstype, vfslist))
2001558Srgrimes					continue;
20123678Speter				if (hasopt(fs->fs_mntops, "noauto"))
20210288Sdg					continue;
20344811Sbde				if (!(init_flags & MNT_UPDATE) &&
20444811Sbde				    ismounted(fs, mntbuf, mntsize))
20528671Ssteve					continue;
2061558Srgrimes				if (mountfs(fs->fs_vfstype, fs->fs_spec,
20723805Sbde				    fs->fs_file, init_flags, options,
20823805Sbde				    fs->fs_mntops))
20923805Sbde					rval = 1;
2101558Srgrimes			}
21128671Ssteve		} else if (fstab_style) {
21217243Sjkh			for (i = 0; i < mntsize; i++) {
21323678Speter				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
21417243Sjkh					continue;
21523805Sbde				putfsent(&mntbuf[i]);
21617243Sjkh			}
21723805Sbde		} else {
2181558Srgrimes			for (i = 0; i < mntsize; i++) {
21923805Sbde				if (checkvfsname(mntbuf[i].f_fstypename,
22023805Sbde				    vfslist))
2211558Srgrimes					continue;
22223678Speter				prmount(&mntbuf[i]);
2231558Srgrimes			}
2241558Srgrimes		}
2251558Srgrimes		exit(rval);
2261558Srgrimes	case 1:
2271558Srgrimes		if (vfslist != NULL)
2281558Srgrimes			usage();
2291558Srgrimes
2301558Srgrimes		if (init_flags & MNT_UPDATE) {
23152678Sgreen			mntfromname = NULL;
23252678Sgreen			have_fstab = 0;
2331558Srgrimes			if ((mntbuf = getmntpt(*argv)) == NULL)
23452678Sgreen				errx(1, "not currently mounted %s", *argv);
23552678Sgreen			/*
23652678Sgreen			 * Only get the mntflags from fstab if both mntpoint
23752678Sgreen			 * and mntspec are identical. Also handle the special
23852678Sgreen			 * case where just '/' is mounted and 'spec' is not
23952678Sgreen			 * identical with the one from fstab ('/dev' is missing
24052678Sgreen			 * in the spec-string at boot-time).
24152678Sgreen			 */
24246619Sjkoshy			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
24352678Sgreen				if (strcmp(fs->fs_spec,
24452678Sgreen				    mntbuf->f_mntfromname) == 0 &&
24552678Sgreen				    strcmp(fs->fs_file,
24652678Sgreen				    mntbuf->f_mntonname) == 0) {
24752678Sgreen					have_fstab = 1;
24852678Sgreen					mntfromname = mntbuf->f_mntfromname;
24952678Sgreen				} else if (argv[0][0] == '/' &&
25052678Sgreen				    argv[0][1] == '\0') {
25152678Sgreen					fs = getfsfile("/");
25252678Sgreen					have_fstab = 1;
25352678Sgreen					mntfromname = fs->fs_spec;
25452678Sgreen				}
25552678Sgreen			}
25652678Sgreen			if (have_fstab) {
25746619Sjkoshy				options = update_options(options, fs->fs_mntops,
25846619Sjkoshy				    mntbuf->f_flags);
25946619Sjkoshy			} else {
26023678Speter				mntfromname = mntbuf->f_mntfromname;
26146619Sjkoshy				options = update_options(options, NULL,
26246619Sjkoshy				    mntbuf->f_flags);
26346619Sjkoshy			}
26423678Speter			rval = mountfs(mntbuf->f_fstypename, mntfromname,
26523678Speter			    mntbuf->f_mntonname, init_flags, options, 0);
26623678Speter			break;
2671558Srgrimes		}
26855118Seivind		rmslashes(*argv, *argv);
26923678Speter		if ((fs = getfsfile(*argv)) == NULL &&
27023678Speter		    (fs = getfsspec(*argv)) == NULL)
27137425Scharnier			errx(1, "%s: unknown special file or file system",
27223678Speter			    *argv);
27323678Speter		if (BADTYPE(fs->fs_type))
27437425Scharnier			errx(1, "%s has unknown file system type",
27523678Speter			    *argv);
27623678Speter		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
27723678Speter		    init_flags, options, fs->fs_mntops);
2781558Srgrimes		break;
2791558Srgrimes	case 2:
2801558Srgrimes		/*
28152678Sgreen		 * If -t flag has not been specified, the path cannot be
28295289Smux		 * found, spec contains either a ':' or a '@', then assume
28352678Sgreen		 * that an NFS filesystem is being specified ala Sun.
28495289Smux		 * Check if the hostname contains only allowed characters
28595289Smux		 * to reduce false positives.  IPv6 addresses containing
28695289Smux		 * ':' will be correctly parsed only if the separator is '@'.
28795289Smux		 * The definition of a valid hostname is taken from RFC 1034.
2881558Srgrimes		 */
28995289Smux		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL) ||
29095289Smux		    ((ep = strchr(argv[0], ':')) != NULL)) {
29195318Smux			if (*ep == '@') {
29295318Smux				cp = ep + 1;
29395318Smux				ep = cp + strlen(cp);
29495318Smux			} else
29595318Smux				cp = argv[0];
29695289Smux			while (cp != ep) {
29795289Smux				if (!isdigit(*cp) && !isalpha(*cp) &&
29895289Smux				    *cp != '.' && *cp != '-' && *cp != ':')
29995289Smux					break;
30095289Smux				cp++;
30195289Smux			}
30295289Smux			if (cp == ep)
30395289Smux				vfstype = "nfs";
30495289Smux		}
3051558Srgrimes		rval = mountfs(vfstype,
3061558Srgrimes		    argv[0], argv[1], init_flags, options, NULL);
3071558Srgrimes		break;
3081558Srgrimes	default:
3091558Srgrimes		usage();
3101558Srgrimes		/* NOTREACHED */
3111558Srgrimes	}
3121558Srgrimes
3131558Srgrimes	/*
3141558Srgrimes	 * If the mount was successfully, and done by root, tell mountd the
3151558Srgrimes	 * good news.  Pid checks are probably unnecessary, but don't hurt.
3161558Srgrimes	 */
3171558Srgrimes	if (rval == 0 && getuid() == 0 &&
3181558Srgrimes	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
31928671Ssteve		if (fscanf(mountdfp, "%d", &pid) == 1 &&
3201558Srgrimes		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
3211558Srgrimes			err(1, "signal mountd");
3221558Srgrimes		(void)fclose(mountdfp);
3231558Srgrimes	}
3241558Srgrimes
3251558Srgrimes	exit(rval);
3261558Srgrimes}
3271558Srgrimes
3281558Srgrimesint
32928671Ssteveismounted(fs, mntbuf, mntsize)
33028671Ssteve	struct fstab *fs;
33128671Ssteve	struct statfs *mntbuf;
33228671Ssteve	int mntsize;
33328671Ssteve{
33428671Ssteve	int i;
33528671Ssteve
33628671Ssteve	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
33728671Ssteve		/* the root file system can always be remounted */
33828671Ssteve		return (0);
33928671Ssteve
34028671Ssteve	for (i = mntsize - 1; i >= 0; --i)
34128671Ssteve		if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
34228671Ssteve		    (!isremountable(fs->fs_vfstype) ||
34328671Ssteve		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
34428671Ssteve			return (1);
34528671Ssteve	return (0);
34628671Ssteve}
34728671Ssteve
34828671Ssteveint
34928671Ssteveisremountable(vfsname)
35028671Ssteve	const char *vfsname;
35128671Ssteve{
35228671Ssteve	const char **cp;
35328671Ssteve
35428671Ssteve	for (cp = remountable_fs_names; *cp; cp++)
35528671Ssteve		if (strcmp(*cp, vfsname) == 0)
35628671Ssteve			return (1);
35728671Ssteve	return (0);
35828671Ssteve}
35928671Ssteve
36028671Ssteveint
36123678Speterhasopt(mntopts, option)
36223678Speter	const char *mntopts, *option;
36323678Speter{
36423678Speter	int negative, found;
36523678Speter	char *opt, *optbuf;
36623678Speter
36723678Speter	if (option[0] == 'n' && option[1] == 'o') {
36823678Speter		negative = 1;
36923678Speter		option += 2;
37023678Speter	} else
37123678Speter		negative = 0;
37223678Speter	optbuf = strdup(mntopts);
37323678Speter	found = 0;
37423678Speter	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
37523678Speter		if (opt[0] == 'n' && opt[1] == 'o') {
37623678Speter			if (!strcasecmp(opt + 2, option))
37723678Speter				found = negative;
37823678Speter		} else if (!strcasecmp(opt, option))
37923678Speter			found = !negative;
38023678Speter	}
38123678Speter	free(optbuf);
38223678Speter	return (found);
38323678Speter}
38423678Speter
38523678Speterint
3861558Srgrimesmountfs(vfstype, spec, name, flags, options, mntopts)
3871558Srgrimes	const char *vfstype, *spec, *name, *options, *mntopts;
3881558Srgrimes	int flags;
3891558Srgrimes{
3901558Srgrimes	/* List of directories containing mount_xxx subcommands. */
3911558Srgrimes	static const char *edirs[] = {
3921558Srgrimes		_PATH_SBIN,
3931558Srgrimes		_PATH_USRSBIN,
3941558Srgrimes		NULL
3951558Srgrimes	};
3961558Srgrimes	const char *argv[100], **edir;
3971558Srgrimes	struct statfs sf;
3981558Srgrimes	pid_t pid;
3991558Srgrimes	int argc, i, status;
4001558Srgrimes	char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
4011558Srgrimes
40228671Ssteve#if __GNUC__
40328671Ssteve	(void)&optbuf;
40428671Ssteve	(void)&name;
40528671Ssteve#endif
40628671Ssteve
40752055Sphk	/* resolve the mountpoint with realpath(3) */
40852055Sphk	(void)checkpath(name, mntpath);
4091558Srgrimes	name = mntpath;
4101558Srgrimes
41123678Speter	if (mntopts == NULL)
41223678Speter		mntopts = "";
4131558Srgrimes	if (options == NULL) {
41423678Speter		if (*mntopts == '\0') {
4151558Srgrimes			options = "rw";
41623678Speter		} else {
4171558Srgrimes			options = mntopts;
41823678Speter			mntopts = "";
41923678Speter		}
4201558Srgrimes	}
4211558Srgrimes	optbuf = catopt(strdup(mntopts), options);
4221558Srgrimes
4231558Srgrimes	if (strcmp(name, "/") == 0)
4241558Srgrimes		flags |= MNT_UPDATE;
4251558Srgrimes	if (flags & MNT_FORCE)
4261558Srgrimes		optbuf = catopt(optbuf, "force");
4271558Srgrimes	if (flags & MNT_RDONLY)
4281558Srgrimes		optbuf = catopt(optbuf, "ro");
4291558Srgrimes	/*
4301558Srgrimes	 * XXX
4311558Srgrimes	 * The mount_mfs (newfs) command uses -o to select the
43237425Scharnier	 * optimization mode.  We don't pass the default "-o rw"
4331558Srgrimes	 * for that reason.
4341558Srgrimes	 */
4351558Srgrimes	if (flags & MNT_UPDATE)
4361558Srgrimes		optbuf = catopt(optbuf, "update");
4371558Srgrimes
43877577Sru	/* Compatibility glue. */
43977577Sru	if (strcmp(vfstype, "msdos") == 0)
44077577Sru		vfstype = "msdosfs";
44177577Sru
4421558Srgrimes	argc = 0;
4431558Srgrimes	argv[argc++] = vfstype;
4441558Srgrimes	mangle(optbuf, &argc, argv);
4451558Srgrimes	argv[argc++] = spec;
4461558Srgrimes	argv[argc++] = name;
4471558Srgrimes	argv[argc] = NULL;
4481558Srgrimes
4491558Srgrimes	if (debug) {
4501558Srgrimes		(void)printf("exec: mount_%s", vfstype);
4511558Srgrimes		for (i = 1; i < argc; i++)
4521558Srgrimes			(void)printf(" %s", argv[i]);
4531558Srgrimes		(void)printf("\n");
4541558Srgrimes		return (0);
4551558Srgrimes	}
4561558Srgrimes
45725120Sache	switch (pid = fork()) {
4581558Srgrimes	case -1:				/* Error. */
45925120Sache		warn("fork");
4601558Srgrimes		free(optbuf);
4611558Srgrimes		return (1);
4621558Srgrimes	case 0:					/* Child. */
4631558Srgrimes		if (strcmp(vfstype, "ufs") == 0)
4641558Srgrimes			exit(mount_ufs(argc, (char * const *) argv));
4651558Srgrimes
4661558Srgrimes		/* Go find an executable. */
46714626Sasami		for (edir = edirs; *edir; edir++) {
4681558Srgrimes			(void)snprintf(execname,
4691558Srgrimes			    sizeof(execname), "%s/mount_%s", *edir, vfstype);
4701558Srgrimes			execv(execname, (char * const *)argv);
47114626Sasami		}
47214626Sasami		if (errno == ENOENT) {
47314626Sasami			int len = 0;
47414626Sasami			char *cp;
47514626Sasami			for (edir = edirs; *edir; edir++)
47614626Sasami				len += strlen(*edir) + 2;	/* ", " */
47737425Scharnier			if ((cp = malloc(len)) == NULL)
47837425Scharnier				errx(1, "malloc failed");
47914626Sasami			cp[0] = '\0';
48014626Sasami			for (edir = edirs; *edir; edir++) {
48114626Sasami				strcat(cp, *edir);
48214626Sasami				if (edir[1] != NULL)
48314626Sasami					strcat(cp, ", ");
48414626Sasami			}
48514626Sasami			warn("exec mount_%s not found in %s", vfstype, cp);
48614626Sasami		}
4871558Srgrimes		exit(1);
4881558Srgrimes		/* NOTREACHED */
4891558Srgrimes	default:				/* Parent. */
4901558Srgrimes		free(optbuf);
4911558Srgrimes
4921558Srgrimes		if (waitpid(pid, &status, 0) < 0) {
4931558Srgrimes			warn("waitpid");
4941558Srgrimes			return (1);
4951558Srgrimes		}
4961558Srgrimes
4971558Srgrimes		if (WIFEXITED(status)) {
4981558Srgrimes			if (WEXITSTATUS(status) != 0)
4991558Srgrimes				return (WEXITSTATUS(status));
5001558Srgrimes		} else if (WIFSIGNALED(status)) {
5011558Srgrimes			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
5021558Srgrimes			return (1);
5031558Srgrimes		}
5041558Srgrimes
5051558Srgrimes		if (verbose) {
5061558Srgrimes			if (statfs(name, &sf) < 0) {
50723678Speter				warn("statfs %s", name);
5081558Srgrimes				return (1);
5091558Srgrimes			}
51017243Sjkh			if (fstab_style)
51123805Sbde				putfsent(&sf);
51217243Sjkh			else
51323678Speter				prmount(&sf);
5141558Srgrimes		}
5151558Srgrimes		break;
5161558Srgrimes	}
5171558Srgrimes
5181558Srgrimes	return (0);
5191558Srgrimes}
5201558Srgrimes
5211558Srgrimesvoid
52223678Speterprmount(sfp)
52323678Speter	struct statfs *sfp;
52423678Speter{
5251558Srgrimes	int flags;
5261558Srgrimes	struct opt *o;
52723678Speter	struct passwd *pw;
5281558Srgrimes
52952036Sn_hibma	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
53052036Sn_hibma	    sfp->f_fstypename);
5311558Srgrimes
53223678Speter	flags = sfp->f_flags & MNT_VISFLAGMASK;
53352036Sn_hibma	for (o = optnames; flags && o->o_opt; o++)
5341558Srgrimes		if (flags & o->o_opt) {
53552036Sn_hibma			(void)printf(", %s", o->o_name);
5361558Srgrimes			flags &= ~o->o_opt;
5371558Srgrimes		}
53823678Speter	if (sfp->f_owner) {
53952036Sn_hibma		(void)printf(", mounted by ");
54023678Speter		if ((pw = getpwuid(sfp->f_owner)) != NULL)
54123678Speter			(void)printf("%s", pw->pw_name);
54223678Speter		else
54323678Speter			(void)printf("%d", sfp->f_owner);
54423678Speter	}
54565023Ssheldonh	if (verbose) {
54665023Ssheldonh		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
54765023Ssheldonh			(void)printf(", writes: sync %ld async %ld",
54865023Ssheldonh			    sfp->f_syncwrites, sfp->f_asyncwrites);
54965023Ssheldonh		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
55065023Ssheldonh			(void)printf(", reads: sync %ld async %ld",
55165023Ssheldonh			    sfp->f_syncreads, sfp->f_asyncreads);
55265023Ssheldonh	}
55352036Sn_hibma	(void)printf(")\n");
5541558Srgrimes}
5551558Srgrimes
5561558Srgrimesstruct statfs *
5571558Srgrimesgetmntpt(name)
5581558Srgrimes	const char *name;
5591558Srgrimes{
5601558Srgrimes	struct statfs *mntbuf;
5611558Srgrimes	int i, mntsize;
5621558Srgrimes
5631558Srgrimes	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
56452678Sgreen	for (i = mntsize - 1; i >= 0; i--) {
5651558Srgrimes		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
5661558Srgrimes		    strcmp(mntbuf[i].f_mntonname, name) == 0)
5671558Srgrimes			return (&mntbuf[i]);
56852678Sgreen	}
5691558Srgrimes	return (NULL);
5701558Srgrimes}
5711558Srgrimes
5721558Srgrimeschar *
5731558Srgrimescatopt(s0, s1)
5741558Srgrimes	char *s0;
5751558Srgrimes	const char *s1;
5761558Srgrimes{
5771558Srgrimes	size_t i;
5781558Srgrimes	char *cp;
5791558Srgrimes
58046619Sjkoshy	if (s1 == NULL || *s1 == '\0')
58146619Sjkoshy		return s0;
58246619Sjkoshy
5831558Srgrimes	if (s0 && *s0) {
5841558Srgrimes		i = strlen(s0) + strlen(s1) + 1 + 1;
5851558Srgrimes		if ((cp = malloc(i)) == NULL)
58637425Scharnier			errx(1, "malloc failed");
5871558Srgrimes		(void)snprintf(cp, i, "%s,%s", s0, s1);
5881558Srgrimes	} else
5891558Srgrimes		cp = strdup(s1);
5901558Srgrimes
5911558Srgrimes	if (s0)
5921558Srgrimes		free(s0);
5931558Srgrimes	return (cp);
5941558Srgrimes}
5951558Srgrimes
5961558Srgrimesvoid
5971558Srgrimesmangle(options, argcp, argv)
5981558Srgrimes	char *options;
5991558Srgrimes	int *argcp;
6001558Srgrimes	const char **argv;
6011558Srgrimes{
6021558Srgrimes	char *p, *s;
6031558Srgrimes	int argc;
6041558Srgrimes
6051558Srgrimes	argc = *argcp;
6061558Srgrimes	for (s = options; (p = strsep(&s, ",")) != NULL;)
60746619Sjkoshy		if (*p != '\0') {
6081558Srgrimes			if (*p == '-') {
6091558Srgrimes				argv[argc++] = p;
6101558Srgrimes				p = strchr(p, '=');
6111558Srgrimes				if (p) {
6121558Srgrimes					*p = '\0';
6131558Srgrimes					argv[argc++] = p+1;
6141558Srgrimes				}
6151558Srgrimes			} else if (strcmp(p, "rw") != 0) {
6161558Srgrimes				argv[argc++] = "-o";
6171558Srgrimes				argv[argc++] = p;
6181558Srgrimes			}
61946619Sjkoshy		}
6201558Srgrimes
6211558Srgrimes	*argcp = argc;
6221558Srgrimes}
6231558Srgrimes
62446619Sjkoshy
62546619Sjkoshychar *
62646619Sjkoshyupdate_options(opts, fstab, curflags)
62746619Sjkoshy	char *opts;
62846619Sjkoshy	char *fstab;
62946619Sjkoshy	int curflags;
63046619Sjkoshy{
63146619Sjkoshy	char *o, *p;
63246619Sjkoshy	char *cur;
63346619Sjkoshy	char *expopt, *newopt, *tmpopt;
63446619Sjkoshy
63546619Sjkoshy	if (opts == NULL)
63646619Sjkoshy		return strdup("");
63746619Sjkoshy
63846619Sjkoshy	/* remove meta options from list */
63946619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_FSTAB);
64046619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_CURRENT);
64146619Sjkoshy	cur = flags2opts(curflags);
64246619Sjkoshy
64346619Sjkoshy	/*
64446619Sjkoshy	 * Expand all meta-options passed to us first.
64546619Sjkoshy	 */
64646619Sjkoshy	expopt = NULL;
64746619Sjkoshy	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
64846619Sjkoshy		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
64946619Sjkoshy			expopt = catopt(expopt, fstab);
65046619Sjkoshy		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
65146619Sjkoshy			expopt = catopt(expopt, cur);
65246619Sjkoshy		else
65346619Sjkoshy			expopt = catopt(expopt, o);
65446619Sjkoshy	}
65546619Sjkoshy	free(cur);
65646619Sjkoshy	free(opts);
65746619Sjkoshy
65846619Sjkoshy	/*
65946619Sjkoshy	 * Remove previous contradictory arguments. Given option "foo" we
66046619Sjkoshy	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
66146619Sjkoshy	 * and "foo" - so we can deal with possible options like "notice".
66246619Sjkoshy	 */
66346619Sjkoshy	newopt = NULL;
66446619Sjkoshy	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
66546619Sjkoshy		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
66646619Sjkoshy			errx(1, "malloc failed");
66746619Sjkoshy
66846619Sjkoshy		strcpy(tmpopt, "no");
66946619Sjkoshy		strcat(tmpopt, o);
67046619Sjkoshy		remopt(newopt, tmpopt);
67146619Sjkoshy		free(tmpopt);
67246619Sjkoshy
67346619Sjkoshy		if (strncmp("no", o, 2) == 0)
67446619Sjkoshy			remopt(newopt, o+2);
67546619Sjkoshy
67646619Sjkoshy		newopt = catopt(newopt, o);
67746619Sjkoshy	}
67846619Sjkoshy	free(expopt);
67946619Sjkoshy
68046619Sjkoshy	return newopt;
68146619Sjkoshy}
68246619Sjkoshy
6831558Srgrimesvoid
68446619Sjkoshyremopt(string, opt)
68546619Sjkoshy	char *string;
68646619Sjkoshy 	const char *opt;
68746619Sjkoshy{
68846619Sjkoshy	char *o, *p, *r;
68946619Sjkoshy
69046619Sjkoshy	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
69146619Sjkoshy		return;
69246619Sjkoshy
69346619Sjkoshy	r = string;
69446619Sjkoshy
69546619Sjkoshy	for (p = string; (o = strsep(&p, ",")) != NULL;) {
69646619Sjkoshy		if (strcmp(opt, o) != 0) {
69746619Sjkoshy			if (*r == ',' && *o != '\0')
69846619Sjkoshy				r++;
69946619Sjkoshy			while ((*r++ = *o++) != '\0')
70046619Sjkoshy			    ;
70146619Sjkoshy			*--r = ',';
70246619Sjkoshy		}
70346619Sjkoshy	}
70446619Sjkoshy	*r = '\0';
70546619Sjkoshy}
70646619Sjkoshy
70746619Sjkoshyvoid
7081558Srgrimesusage()
7091558Srgrimes{
7101558Srgrimes
71137425Scharnier	(void)fprintf(stderr, "%s\n%s\n%s\n",
71237425Scharnier"usage: mount [-dfpruvw] [-o options] [-t ufs | external_type] special node",
71337425Scharnier"       mount [-adfpruvw] [-t ufs | external_type]",
71437425Scharnier"       mount [-dfpruvw] special | node");
7151558Srgrimes	exit(1);
7161558Srgrimes}
71717243Sjkh
71817243Sjkhvoid
71923805Sbdeputfsent(ent)
72023805Sbde	const struct statfs *ent;
72117243Sjkh{
72223805Sbde	struct fstab *fst;
72346619Sjkoshy	char *opts;
72446619Sjkoshy
72546619Sjkoshy	opts = flags2opts(ent->f_flags);
72623805Sbde	printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
72746619Sjkoshy	    ent->f_fstypename, opts);
72846619Sjkoshy	free(opts);
72917243Sjkh
73028671Ssteve	if ((fst = getfsspec(ent->f_mntfromname)))
73123805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
73228671Ssteve	else if ((fst = getfsfile(ent->f_mntonname)))
73323805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
73456038Sgreen	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
73556038Sgreen		if (strcmp(ent->f_mntonname, "/") == 0)
73656038Sgreen			printf("\t1 1\n");
73756038Sgreen		else
73856038Sgreen			printf("\t2 2\n");
73956038Sgreen	} else
74023805Sbde		printf("\t0 0\n");
74117243Sjkh}
74246619Sjkoshy
74346619Sjkoshy
74446619Sjkoshychar *
74546619Sjkoshyflags2opts(flags)
74646619Sjkoshy	int flags;
74746619Sjkoshy{
74846619Sjkoshy	char *res;
74946619Sjkoshy
75046619Sjkoshy	res = NULL;
75146619Sjkoshy
75246619Sjkoshy	res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
75346619Sjkoshy
75446619Sjkoshy	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
75546619Sjkoshy	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
75646619Sjkoshy	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
75746619Sjkoshy	if (flags & MNT_NODEV)		res = catopt(res, "nodev");
75846619Sjkoshy	if (flags & MNT_UNION)		res = catopt(res, "union");
75946619Sjkoshy	if (flags & MNT_ASYNC)		res = catopt(res, "async");
76046619Sjkoshy	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
76146619Sjkoshy	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
76246619Sjkoshy	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
76346619Sjkoshy	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
76446619Sjkoshy	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
76546619Sjkoshy
76646619Sjkoshy	return res;
76746619Sjkoshy}
768