mount.c revision 92882
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 92882 2002-03-21 13:14:21Z imp $";
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
531558Srgrimes#include <err.h>
541558Srgrimes#include <errno.h>
551558Srgrimes#include <fstab.h>
5623678Speter#include <pwd.h>
571558Srgrimes#include <signal.h>
581558Srgrimes#include <stdio.h>
591558Srgrimes#include <stdlib.h>
601558Srgrimes#include <string.h>
611558Srgrimes#include <unistd.h>
621558Srgrimes
6328671Ssteve#include "extern.h"
6452055Sphk#include "mntopts.h"
651558Srgrimes#include "pathnames.h"
661558Srgrimes
6746619Sjkoshy/* `meta' options */
6846619Sjkoshy#define MOUNT_META_OPTION_FSTAB		"fstab"
6946619Sjkoshy#define MOUNT_META_OPTION_CURRENT	"current"
7046619Sjkoshy
7123805Sbdeint debug, fstab_style, verbose;
721558Srgrimes
7392882Simpchar   *catopt(char *, const char *);
7492882Simpstruct statfs *getmntpt(const char *);
7592882Simpint	hasopt(const char *, const char *);
7692882Simpint	ismounted(struct fstab *, struct statfs *, int);
7792882Simpint	isremountable(const char *);
7892882Simpvoid	mangle(char *, int *, const char **);
7992882Simpchar   *update_options(char *, char *, int);
8092882Simpint	mountfs(const char *, const char *, const char *,
8192882Simp			int, const char *, const char *);
8292882Simpvoid	remopt(char *, const char *);
8392882Simpvoid	prmount(struct statfs *);
8492882Simpvoid	putfsent(const struct statfs *);
8592882Simpvoid	usage(void);
8692882Simpchar   *flags2opts(int);
871558Srgrimes
8837425Scharnier/* Map from mount options to printable formats. */
891558Srgrimesstatic struct opt {
901558Srgrimes	int o_opt;
911558Srgrimes	const char *o_name;
921558Srgrimes} optnames[] = {
931558Srgrimes	{ MNT_ASYNC,		"asynchronous" },
941558Srgrimes	{ MNT_EXPORTED,		"NFS exported" },
951558Srgrimes	{ MNT_LOCAL,		"local" },
9618007Sdg	{ MNT_NOATIME,		"noatime" },
971558Srgrimes	{ MNT_NODEV,		"nodev" },
981558Srgrimes	{ MNT_NOEXEC,		"noexec" },
991558Srgrimes	{ MNT_NOSUID,		"nosuid" },
10035105Swosch	{ MNT_NOSYMFOLLOW,	"nosymfollow" },
1011558Srgrimes	{ MNT_QUOTA,		"with quotas" },
1021558Srgrimes	{ MNT_RDONLY,		"read-only" },
1031558Srgrimes	{ MNT_SYNCHRONOUS,	"synchronous" },
1041558Srgrimes	{ MNT_UNION,		"union" },
10529890Skato	{ MNT_NOCLUSTERR,	"noclusterr" },
10629890Skato	{ MNT_NOCLUSTERW,	"noclusterw" },
10731144Sjulian	{ MNT_SUIDDIR,		"suiddir" },
10834266Sjulian	{ MNT_SOFTDEP,		"soft-updates" },
10946619Sjkoshy	{ 0, NULL }
1101558Srgrimes};
1111558Srgrimes
11228671Ssteve/*
11328671Ssteve * List of VFS types that can be remounted without becoming mounted on top
11428671Ssteve * of each other.
11528671Ssteve * XXX Is this list correct?
11628671Ssteve */
11728671Sstevestatic const char *
11828671Ssteveremountable_fs_names[] = {
11969056Sphantom	"ufs", "ffs", "ext2fs",
12028671Ssteve	0
12128671Ssteve};
12228671Ssteve
1231558Srgrimesint
1241558Srgrimesmain(argc, argv)
1251558Srgrimes	int argc;
1261558Srgrimes	char * const argv[];
1271558Srgrimes{
12823678Speter	const char *mntfromname, **vfslist, *vfstype;
1291558Srgrimes	struct fstab *fs;
1301558Srgrimes	struct statfs *mntbuf;
1311558Srgrimes	FILE *mountdfp;
1321558Srgrimes	pid_t pid;
13352678Sgreen	int all, ch, i, init_flags, mntsize, rval, have_fstab;
1341558Srgrimes	char *options;
1351558Srgrimes
1361558Srgrimes	all = init_flags = 0;
1371558Srgrimes	options = NULL;
1381558Srgrimes	vfslist = NULL;
1391558Srgrimes	vfstype = "ufs";
14023805Sbde	while ((ch = getopt(argc, argv, "adfo:prwt:uv")) != -1)
1411558Srgrimes		switch (ch) {
1421558Srgrimes		case 'a':
1431558Srgrimes			all = 1;
1441558Srgrimes			break;
1451558Srgrimes		case 'd':
1461558Srgrimes			debug = 1;
1471558Srgrimes			break;
1481558Srgrimes		case 'f':
1491558Srgrimes			init_flags |= MNT_FORCE;
1501558Srgrimes			break;
1511558Srgrimes		case 'o':
1521558Srgrimes			if (*optarg)
1531558Srgrimes				options = catopt(options, optarg);
1541558Srgrimes			break;
15523805Sbde		case 'p':
15623805Sbde			fstab_style = 1;
15723805Sbde			verbose = 1;
15823805Sbde			break;
1591558Srgrimes		case 'r':
16076198Sdd			options = catopt(options, "ro");
1611558Srgrimes			break;
1621558Srgrimes		case 't':
1631558Srgrimes			if (vfslist != NULL)
16437425Scharnier				errx(1, "only one -t option may be specified");
1651558Srgrimes			vfslist = makevfslist(optarg);
1661558Srgrimes			vfstype = optarg;
1671558Srgrimes			break;
1681558Srgrimes		case 'u':
1691558Srgrimes			init_flags |= MNT_UPDATE;
1701558Srgrimes			break;
1711558Srgrimes		case 'v':
1721558Srgrimes			verbose = 1;
1731558Srgrimes			break;
1741558Srgrimes		case 'w':
17576198Sdd			options = catopt(options, "noro");
1761558Srgrimes			break;
1771558Srgrimes		case '?':
1781558Srgrimes		default:
1791558Srgrimes			usage();
1801558Srgrimes			/* NOTREACHED */
1811558Srgrimes		}
1821558Srgrimes	argc -= optind;
1831558Srgrimes	argv += optind;
1841558Srgrimes
1851558Srgrimes#define	BADTYPE(type)							\
1861558Srgrimes	(strcmp(type, FSTAB_RO) &&					\
1871558Srgrimes	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
1881558Srgrimes
1891558Srgrimes	rval = 0;
1901558Srgrimes	switch (argc) {
1911558Srgrimes	case 0:
19228671Ssteve		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
19328671Ssteve			err(1, "getmntinfo");
19428671Ssteve		if (all) {
1951558Srgrimes			while ((fs = getfsent()) != NULL) {
1961558Srgrimes				if (BADTYPE(fs->fs_type))
1971558Srgrimes					continue;
19823678Speter				if (checkvfsname(fs->fs_vfstype, vfslist))
1991558Srgrimes					continue;
20023678Speter				if (hasopt(fs->fs_mntops, "noauto"))
20110288Sdg					continue;
20244811Sbde				if (!(init_flags & MNT_UPDATE) &&
20344811Sbde				    ismounted(fs, mntbuf, mntsize))
20428671Ssteve					continue;
2051558Srgrimes				if (mountfs(fs->fs_vfstype, fs->fs_spec,
20623805Sbde				    fs->fs_file, init_flags, options,
20723805Sbde				    fs->fs_mntops))
20823805Sbde					rval = 1;
2091558Srgrimes			}
21028671Ssteve		} else if (fstab_style) {
21117243Sjkh			for (i = 0; i < mntsize; i++) {
21223678Speter				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
21317243Sjkh					continue;
21423805Sbde				putfsent(&mntbuf[i]);
21517243Sjkh			}
21623805Sbde		} else {
2171558Srgrimes			for (i = 0; i < mntsize; i++) {
21823805Sbde				if (checkvfsname(mntbuf[i].f_fstypename,
21923805Sbde				    vfslist))
2201558Srgrimes					continue;
22123678Speter				prmount(&mntbuf[i]);
2221558Srgrimes			}
2231558Srgrimes		}
2241558Srgrimes		exit(rval);
2251558Srgrimes	case 1:
2261558Srgrimes		if (vfslist != NULL)
2271558Srgrimes			usage();
2281558Srgrimes
2291558Srgrimes		if (init_flags & MNT_UPDATE) {
23052678Sgreen			mntfromname = NULL;
23152678Sgreen			have_fstab = 0;
2321558Srgrimes			if ((mntbuf = getmntpt(*argv)) == NULL)
23352678Sgreen				errx(1, "not currently mounted %s", *argv);
23452678Sgreen			/*
23552678Sgreen			 * Only get the mntflags from fstab if both mntpoint
23652678Sgreen			 * and mntspec are identical. Also handle the special
23752678Sgreen			 * case where just '/' is mounted and 'spec' is not
23852678Sgreen			 * identical with the one from fstab ('/dev' is missing
23952678Sgreen			 * in the spec-string at boot-time).
24052678Sgreen			 */
24146619Sjkoshy			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
24252678Sgreen				if (strcmp(fs->fs_spec,
24352678Sgreen				    mntbuf->f_mntfromname) == 0 &&
24452678Sgreen				    strcmp(fs->fs_file,
24552678Sgreen				    mntbuf->f_mntonname) == 0) {
24652678Sgreen					have_fstab = 1;
24752678Sgreen					mntfromname = mntbuf->f_mntfromname;
24852678Sgreen				} else if (argv[0][0] == '/' &&
24952678Sgreen				    argv[0][1] == '\0') {
25052678Sgreen					fs = getfsfile("/");
25152678Sgreen					have_fstab = 1;
25252678Sgreen					mntfromname = fs->fs_spec;
25352678Sgreen				}
25452678Sgreen			}
25552678Sgreen			if (have_fstab) {
25646619Sjkoshy				options = update_options(options, fs->fs_mntops,
25746619Sjkoshy				    mntbuf->f_flags);
25846619Sjkoshy			} else {
25923678Speter				mntfromname = mntbuf->f_mntfromname;
26046619Sjkoshy				options = update_options(options, NULL,
26146619Sjkoshy				    mntbuf->f_flags);
26246619Sjkoshy			}
26323678Speter			rval = mountfs(mntbuf->f_fstypename, mntfromname,
26423678Speter			    mntbuf->f_mntonname, init_flags, options, 0);
26523678Speter			break;
2661558Srgrimes		}
26755118Seivind		rmslashes(*argv, *argv);
26823678Speter		if ((fs = getfsfile(*argv)) == NULL &&
26923678Speter		    (fs = getfsspec(*argv)) == NULL)
27037425Scharnier			errx(1, "%s: unknown special file or file system",
27123678Speter			    *argv);
27223678Speter		if (BADTYPE(fs->fs_type))
27337425Scharnier			errx(1, "%s has unknown file system type",
27423678Speter			    *argv);
27523678Speter		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
27623678Speter		    init_flags, options, fs->fs_mntops);
2771558Srgrimes		break;
2781558Srgrimes	case 2:
2791558Srgrimes		/*
28052678Sgreen		 * If -t flag has not been specified, the path cannot be
28152678Sgreen		 * found, spec contains either a ':' or a '@', and the
28252678Sgreen		 * spec is not a file with those characters, then assume
28352678Sgreen		 * that an NFS filesystem is being specified ala Sun.
2841558Srgrimes		 */
28552095Sgreen		if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL &&
28652095Sgreen		    access(argv[0], 0) == -1)
2871558Srgrimes			vfstype = "nfs";
2881558Srgrimes		rval = mountfs(vfstype,
2891558Srgrimes		    argv[0], argv[1], init_flags, options, NULL);
2901558Srgrimes		break;
2911558Srgrimes	default:
2921558Srgrimes		usage();
2931558Srgrimes		/* NOTREACHED */
2941558Srgrimes	}
2951558Srgrimes
2961558Srgrimes	/*
2971558Srgrimes	 * If the mount was successfully, and done by root, tell mountd the
2981558Srgrimes	 * good news.  Pid checks are probably unnecessary, but don't hurt.
2991558Srgrimes	 */
3001558Srgrimes	if (rval == 0 && getuid() == 0 &&
3011558Srgrimes	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
30228671Ssteve		if (fscanf(mountdfp, "%d", &pid) == 1 &&
3031558Srgrimes		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
3041558Srgrimes			err(1, "signal mountd");
3051558Srgrimes		(void)fclose(mountdfp);
3061558Srgrimes	}
3071558Srgrimes
3081558Srgrimes	exit(rval);
3091558Srgrimes}
3101558Srgrimes
3111558Srgrimesint
31228671Ssteveismounted(fs, mntbuf, mntsize)
31328671Ssteve	struct fstab *fs;
31428671Ssteve	struct statfs *mntbuf;
31528671Ssteve	int mntsize;
31628671Ssteve{
31728671Ssteve	int i;
31828671Ssteve
31928671Ssteve	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
32028671Ssteve		/* the root file system can always be remounted */
32128671Ssteve		return (0);
32228671Ssteve
32328671Ssteve	for (i = mntsize - 1; i >= 0; --i)
32428671Ssteve		if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
32528671Ssteve		    (!isremountable(fs->fs_vfstype) ||
32628671Ssteve		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
32728671Ssteve			return (1);
32828671Ssteve	return (0);
32928671Ssteve}
33028671Ssteve
33128671Ssteveint
33228671Ssteveisremountable(vfsname)
33328671Ssteve	const char *vfsname;
33428671Ssteve{
33528671Ssteve	const char **cp;
33628671Ssteve
33728671Ssteve	for (cp = remountable_fs_names; *cp; cp++)
33828671Ssteve		if (strcmp(*cp, vfsname) == 0)
33928671Ssteve			return (1);
34028671Ssteve	return (0);
34128671Ssteve}
34228671Ssteve
34328671Ssteveint
34423678Speterhasopt(mntopts, option)
34523678Speter	const char *mntopts, *option;
34623678Speter{
34723678Speter	int negative, found;
34823678Speter	char *opt, *optbuf;
34923678Speter
35023678Speter	if (option[0] == 'n' && option[1] == 'o') {
35123678Speter		negative = 1;
35223678Speter		option += 2;
35323678Speter	} else
35423678Speter		negative = 0;
35523678Speter	optbuf = strdup(mntopts);
35623678Speter	found = 0;
35723678Speter	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
35823678Speter		if (opt[0] == 'n' && opt[1] == 'o') {
35923678Speter			if (!strcasecmp(opt + 2, option))
36023678Speter				found = negative;
36123678Speter		} else if (!strcasecmp(opt, option))
36223678Speter			found = !negative;
36323678Speter	}
36423678Speter	free(optbuf);
36523678Speter	return (found);
36623678Speter}
36723678Speter
36823678Speterint
3691558Srgrimesmountfs(vfstype, spec, name, flags, options, mntopts)
3701558Srgrimes	const char *vfstype, *spec, *name, *options, *mntopts;
3711558Srgrimes	int flags;
3721558Srgrimes{
3731558Srgrimes	/* List of directories containing mount_xxx subcommands. */
3741558Srgrimes	static const char *edirs[] = {
3751558Srgrimes		_PATH_SBIN,
3761558Srgrimes		_PATH_USRSBIN,
3771558Srgrimes		NULL
3781558Srgrimes	};
3791558Srgrimes	const char *argv[100], **edir;
3801558Srgrimes	struct statfs sf;
3811558Srgrimes	pid_t pid;
3821558Srgrimes	int argc, i, status;
3831558Srgrimes	char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
3841558Srgrimes
38528671Ssteve#if __GNUC__
38628671Ssteve	(void)&optbuf;
38728671Ssteve	(void)&name;
38828671Ssteve#endif
38928671Ssteve
39052055Sphk	/* resolve the mountpoint with realpath(3) */
39152055Sphk	(void)checkpath(name, mntpath);
3921558Srgrimes	name = mntpath;
3931558Srgrimes
39423678Speter	if (mntopts == NULL)
39523678Speter		mntopts = "";
3961558Srgrimes	if (options == NULL) {
39723678Speter		if (*mntopts == '\0') {
3981558Srgrimes			options = "rw";
39923678Speter		} else {
4001558Srgrimes			options = mntopts;
40123678Speter			mntopts = "";
40223678Speter		}
4031558Srgrimes	}
4041558Srgrimes	optbuf = catopt(strdup(mntopts), options);
4051558Srgrimes
4061558Srgrimes	if (strcmp(name, "/") == 0)
4071558Srgrimes		flags |= MNT_UPDATE;
4081558Srgrimes	if (flags & MNT_FORCE)
4091558Srgrimes		optbuf = catopt(optbuf, "force");
4101558Srgrimes	if (flags & MNT_RDONLY)
4111558Srgrimes		optbuf = catopt(optbuf, "ro");
4121558Srgrimes	/*
4131558Srgrimes	 * XXX
4141558Srgrimes	 * The mount_mfs (newfs) command uses -o to select the
41537425Scharnier	 * optimization mode.  We don't pass the default "-o rw"
4161558Srgrimes	 * for that reason.
4171558Srgrimes	 */
4181558Srgrimes	if (flags & MNT_UPDATE)
4191558Srgrimes		optbuf = catopt(optbuf, "update");
4201558Srgrimes
42177577Sru	/* Compatibility glue. */
42277577Sru	if (strcmp(vfstype, "msdos") == 0)
42377577Sru		vfstype = "msdosfs";
42477577Sru
4251558Srgrimes	argc = 0;
4261558Srgrimes	argv[argc++] = vfstype;
4271558Srgrimes	mangle(optbuf, &argc, argv);
4281558Srgrimes	argv[argc++] = spec;
4291558Srgrimes	argv[argc++] = name;
4301558Srgrimes	argv[argc] = NULL;
4311558Srgrimes
4321558Srgrimes	if (debug) {
4331558Srgrimes		(void)printf("exec: mount_%s", vfstype);
4341558Srgrimes		for (i = 1; i < argc; i++)
4351558Srgrimes			(void)printf(" %s", argv[i]);
4361558Srgrimes		(void)printf("\n");
4371558Srgrimes		return (0);
4381558Srgrimes	}
4391558Srgrimes
44025120Sache	switch (pid = fork()) {
4411558Srgrimes	case -1:				/* Error. */
44225120Sache		warn("fork");
4431558Srgrimes		free(optbuf);
4441558Srgrimes		return (1);
4451558Srgrimes	case 0:					/* Child. */
4461558Srgrimes		if (strcmp(vfstype, "ufs") == 0)
4471558Srgrimes			exit(mount_ufs(argc, (char * const *) argv));
4481558Srgrimes
4491558Srgrimes		/* Go find an executable. */
45014626Sasami		for (edir = edirs; *edir; edir++) {
4511558Srgrimes			(void)snprintf(execname,
4521558Srgrimes			    sizeof(execname), "%s/mount_%s", *edir, vfstype);
4531558Srgrimes			execv(execname, (char * const *)argv);
45414626Sasami		}
45514626Sasami		if (errno == ENOENT) {
45614626Sasami			int len = 0;
45714626Sasami			char *cp;
45814626Sasami			for (edir = edirs; *edir; edir++)
45914626Sasami				len += strlen(*edir) + 2;	/* ", " */
46037425Scharnier			if ((cp = malloc(len)) == NULL)
46137425Scharnier				errx(1, "malloc failed");
46214626Sasami			cp[0] = '\0';
46314626Sasami			for (edir = edirs; *edir; edir++) {
46414626Sasami				strcat(cp, *edir);
46514626Sasami				if (edir[1] != NULL)
46614626Sasami					strcat(cp, ", ");
46714626Sasami			}
46814626Sasami			warn("exec mount_%s not found in %s", vfstype, cp);
46914626Sasami		}
4701558Srgrimes		exit(1);
4711558Srgrimes		/* NOTREACHED */
4721558Srgrimes	default:				/* Parent. */
4731558Srgrimes		free(optbuf);
4741558Srgrimes
4751558Srgrimes		if (waitpid(pid, &status, 0) < 0) {
4761558Srgrimes			warn("waitpid");
4771558Srgrimes			return (1);
4781558Srgrimes		}
4791558Srgrimes
4801558Srgrimes		if (WIFEXITED(status)) {
4811558Srgrimes			if (WEXITSTATUS(status) != 0)
4821558Srgrimes				return (WEXITSTATUS(status));
4831558Srgrimes		} else if (WIFSIGNALED(status)) {
4841558Srgrimes			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
4851558Srgrimes			return (1);
4861558Srgrimes		}
4871558Srgrimes
4881558Srgrimes		if (verbose) {
4891558Srgrimes			if (statfs(name, &sf) < 0) {
49023678Speter				warn("statfs %s", name);
4911558Srgrimes				return (1);
4921558Srgrimes			}
49317243Sjkh			if (fstab_style)
49423805Sbde				putfsent(&sf);
49517243Sjkh			else
49623678Speter				prmount(&sf);
4971558Srgrimes		}
4981558Srgrimes		break;
4991558Srgrimes	}
5001558Srgrimes
5011558Srgrimes	return (0);
5021558Srgrimes}
5031558Srgrimes
5041558Srgrimesvoid
50523678Speterprmount(sfp)
50623678Speter	struct statfs *sfp;
50723678Speter{
5081558Srgrimes	int flags;
5091558Srgrimes	struct opt *o;
51023678Speter	struct passwd *pw;
5111558Srgrimes
51252036Sn_hibma	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
51352036Sn_hibma	    sfp->f_fstypename);
5141558Srgrimes
51523678Speter	flags = sfp->f_flags & MNT_VISFLAGMASK;
51652036Sn_hibma	for (o = optnames; flags && o->o_opt; o++)
5171558Srgrimes		if (flags & o->o_opt) {
51852036Sn_hibma			(void)printf(", %s", o->o_name);
5191558Srgrimes			flags &= ~o->o_opt;
5201558Srgrimes		}
52123678Speter	if (sfp->f_owner) {
52252036Sn_hibma		(void)printf(", mounted by ");
52323678Speter		if ((pw = getpwuid(sfp->f_owner)) != NULL)
52423678Speter			(void)printf("%s", pw->pw_name);
52523678Speter		else
52623678Speter			(void)printf("%d", sfp->f_owner);
52723678Speter	}
52865023Ssheldonh	if (verbose) {
52965023Ssheldonh		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
53065023Ssheldonh			(void)printf(", writes: sync %ld async %ld",
53165023Ssheldonh			    sfp->f_syncwrites, sfp->f_asyncwrites);
53265023Ssheldonh		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
53365023Ssheldonh			(void)printf(", reads: sync %ld async %ld",
53465023Ssheldonh			    sfp->f_syncreads, sfp->f_asyncreads);
53565023Ssheldonh	}
53652036Sn_hibma	(void)printf(")\n");
5371558Srgrimes}
5381558Srgrimes
5391558Srgrimesstruct statfs *
5401558Srgrimesgetmntpt(name)
5411558Srgrimes	const char *name;
5421558Srgrimes{
5431558Srgrimes	struct statfs *mntbuf;
5441558Srgrimes	int i, mntsize;
5451558Srgrimes
5461558Srgrimes	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
54752678Sgreen	for (i = mntsize - 1; i >= 0; i--) {
5481558Srgrimes		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
5491558Srgrimes		    strcmp(mntbuf[i].f_mntonname, name) == 0)
5501558Srgrimes			return (&mntbuf[i]);
55152678Sgreen	}
5521558Srgrimes	return (NULL);
5531558Srgrimes}
5541558Srgrimes
5551558Srgrimeschar *
5561558Srgrimescatopt(s0, s1)
5571558Srgrimes	char *s0;
5581558Srgrimes	const char *s1;
5591558Srgrimes{
5601558Srgrimes	size_t i;
5611558Srgrimes	char *cp;
5621558Srgrimes
56346619Sjkoshy	if (s1 == NULL || *s1 == '\0')
56446619Sjkoshy		return s0;
56546619Sjkoshy
5661558Srgrimes	if (s0 && *s0) {
5671558Srgrimes		i = strlen(s0) + strlen(s1) + 1 + 1;
5681558Srgrimes		if ((cp = malloc(i)) == NULL)
56937425Scharnier			errx(1, "malloc failed");
5701558Srgrimes		(void)snprintf(cp, i, "%s,%s", s0, s1);
5711558Srgrimes	} else
5721558Srgrimes		cp = strdup(s1);
5731558Srgrimes
5741558Srgrimes	if (s0)
5751558Srgrimes		free(s0);
5761558Srgrimes	return (cp);
5771558Srgrimes}
5781558Srgrimes
5791558Srgrimesvoid
5801558Srgrimesmangle(options, argcp, argv)
5811558Srgrimes	char *options;
5821558Srgrimes	int *argcp;
5831558Srgrimes	const char **argv;
5841558Srgrimes{
5851558Srgrimes	char *p, *s;
5861558Srgrimes	int argc;
5871558Srgrimes
5881558Srgrimes	argc = *argcp;
5891558Srgrimes	for (s = options; (p = strsep(&s, ",")) != NULL;)
59046619Sjkoshy		if (*p != '\0') {
5911558Srgrimes			if (*p == '-') {
5921558Srgrimes				argv[argc++] = p;
5931558Srgrimes				p = strchr(p, '=');
5941558Srgrimes				if (p) {
5951558Srgrimes					*p = '\0';
5961558Srgrimes					argv[argc++] = p+1;
5971558Srgrimes				}
5981558Srgrimes			} else if (strcmp(p, "rw") != 0) {
5991558Srgrimes				argv[argc++] = "-o";
6001558Srgrimes				argv[argc++] = p;
6011558Srgrimes			}
60246619Sjkoshy		}
6031558Srgrimes
6041558Srgrimes	*argcp = argc;
6051558Srgrimes}
6061558Srgrimes
60746619Sjkoshy
60846619Sjkoshychar *
60946619Sjkoshyupdate_options(opts, fstab, curflags)
61046619Sjkoshy	char *opts;
61146619Sjkoshy	char *fstab;
61246619Sjkoshy	int curflags;
61346619Sjkoshy{
61446619Sjkoshy	char *o, *p;
61546619Sjkoshy	char *cur;
61646619Sjkoshy	char *expopt, *newopt, *tmpopt;
61746619Sjkoshy
61846619Sjkoshy	if (opts == NULL)
61946619Sjkoshy		return strdup("");
62046619Sjkoshy
62146619Sjkoshy	/* remove meta options from list */
62246619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_FSTAB);
62346619Sjkoshy	remopt(fstab, MOUNT_META_OPTION_CURRENT);
62446619Sjkoshy	cur = flags2opts(curflags);
62546619Sjkoshy
62646619Sjkoshy	/*
62746619Sjkoshy	 * Expand all meta-options passed to us first.
62846619Sjkoshy	 */
62946619Sjkoshy	expopt = NULL;
63046619Sjkoshy	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
63146619Sjkoshy		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
63246619Sjkoshy			expopt = catopt(expopt, fstab);
63346619Sjkoshy		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
63446619Sjkoshy			expopt = catopt(expopt, cur);
63546619Sjkoshy		else
63646619Sjkoshy			expopt = catopt(expopt, o);
63746619Sjkoshy	}
63846619Sjkoshy	free(cur);
63946619Sjkoshy	free(opts);
64046619Sjkoshy
64146619Sjkoshy	/*
64246619Sjkoshy	 * Remove previous contradictory arguments. Given option "foo" we
64346619Sjkoshy	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
64446619Sjkoshy	 * and "foo" - so we can deal with possible options like "notice".
64546619Sjkoshy	 */
64646619Sjkoshy	newopt = NULL;
64746619Sjkoshy	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
64846619Sjkoshy		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
64946619Sjkoshy			errx(1, "malloc failed");
65046619Sjkoshy
65146619Sjkoshy		strcpy(tmpopt, "no");
65246619Sjkoshy		strcat(tmpopt, o);
65346619Sjkoshy		remopt(newopt, tmpopt);
65446619Sjkoshy		free(tmpopt);
65546619Sjkoshy
65646619Sjkoshy		if (strncmp("no", o, 2) == 0)
65746619Sjkoshy			remopt(newopt, o+2);
65846619Sjkoshy
65946619Sjkoshy		newopt = catopt(newopt, o);
66046619Sjkoshy	}
66146619Sjkoshy	free(expopt);
66246619Sjkoshy
66346619Sjkoshy	return newopt;
66446619Sjkoshy}
66546619Sjkoshy
6661558Srgrimesvoid
66746619Sjkoshyremopt(string, opt)
66846619Sjkoshy	char *string;
66946619Sjkoshy 	const char *opt;
67046619Sjkoshy{
67146619Sjkoshy	char *o, *p, *r;
67246619Sjkoshy
67346619Sjkoshy	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
67446619Sjkoshy		return;
67546619Sjkoshy
67646619Sjkoshy	r = string;
67746619Sjkoshy
67846619Sjkoshy	for (p = string; (o = strsep(&p, ",")) != NULL;) {
67946619Sjkoshy		if (strcmp(opt, o) != 0) {
68046619Sjkoshy			if (*r == ',' && *o != '\0')
68146619Sjkoshy				r++;
68246619Sjkoshy			while ((*r++ = *o++) != '\0')
68346619Sjkoshy			    ;
68446619Sjkoshy			*--r = ',';
68546619Sjkoshy		}
68646619Sjkoshy	}
68746619Sjkoshy	*r = '\0';
68846619Sjkoshy}
68946619Sjkoshy
69046619Sjkoshyvoid
6911558Srgrimesusage()
6921558Srgrimes{
6931558Srgrimes
69437425Scharnier	(void)fprintf(stderr, "%s\n%s\n%s\n",
69537425Scharnier"usage: mount [-dfpruvw] [-o options] [-t ufs | external_type] special node",
69637425Scharnier"       mount [-adfpruvw] [-t ufs | external_type]",
69737425Scharnier"       mount [-dfpruvw] special | node");
6981558Srgrimes	exit(1);
6991558Srgrimes}
70017243Sjkh
70117243Sjkhvoid
70223805Sbdeputfsent(ent)
70323805Sbde	const struct statfs *ent;
70417243Sjkh{
70523805Sbde	struct fstab *fst;
70646619Sjkoshy	char *opts;
70746619Sjkoshy
70846619Sjkoshy	opts = flags2opts(ent->f_flags);
70923805Sbde	printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
71046619Sjkoshy	    ent->f_fstypename, opts);
71146619Sjkoshy	free(opts);
71217243Sjkh
71328671Ssteve	if ((fst = getfsspec(ent->f_mntfromname)))
71423805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
71528671Ssteve	else if ((fst = getfsfile(ent->f_mntonname)))
71623805Sbde		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
71756038Sgreen	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
71856038Sgreen		if (strcmp(ent->f_mntonname, "/") == 0)
71956038Sgreen			printf("\t1 1\n");
72056038Sgreen		else
72156038Sgreen			printf("\t2 2\n");
72256038Sgreen	} else
72323805Sbde		printf("\t0 0\n");
72417243Sjkh}
72546619Sjkoshy
72646619Sjkoshy
72746619Sjkoshychar *
72846619Sjkoshyflags2opts(flags)
72946619Sjkoshy	int flags;
73046619Sjkoshy{
73146619Sjkoshy	char *res;
73246619Sjkoshy
73346619Sjkoshy	res = NULL;
73446619Sjkoshy
73546619Sjkoshy	res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
73646619Sjkoshy
73746619Sjkoshy	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
73846619Sjkoshy	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
73946619Sjkoshy	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
74046619Sjkoshy	if (flags & MNT_NODEV)		res = catopt(res, "nodev");
74146619Sjkoshy	if (flags & MNT_UNION)		res = catopt(res, "union");
74246619Sjkoshy	if (flags & MNT_ASYNC)		res = catopt(res, "async");
74346619Sjkoshy	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
74446619Sjkoshy	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
74546619Sjkoshy	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
74646619Sjkoshy	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
74746619Sjkoshy	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
74846619Sjkoshy
74946619Sjkoshy	return res;
75046619Sjkoshy}
751