mount.c revision 125365
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 125365 2004-02-03 04:03:19Z nectar $";
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>
57117031Sgordon#include <paths.h>
5823678Speter#include <pwd.h>
591558Srgrimes#include <signal.h>
60123242Siedowse#include <stdint.h>
611558Srgrimes#include <stdio.h>
621558Srgrimes#include <stdlib.h>
631558Srgrimes#include <string.h>
641558Srgrimes#include <unistd.h>
651558Srgrimes
6628671Ssteve#include "extern.h"
6752055Sphk#include "mntopts.h"
681558Srgrimes#include "pathnames.h"
691558Srgrimes
7046619Sjkoshy/* `meta' options */
7146619Sjkoshy#define MOUNT_META_OPTION_FSTAB		"fstab"
7246619Sjkoshy#define MOUNT_META_OPTION_CURRENT	"current"
7346619Sjkoshy
7423805Sbdeint debug, fstab_style, verbose;
751558Srgrimes
7692882Simpchar   *catopt(char *, const char *);
7792882Simpstruct statfs *getmntpt(const char *);
7892882Simpint	hasopt(const char *, const char *);
7992882Simpint	ismounted(struct fstab *, struct statfs *, int);
8092882Simpint	isremountable(const char *);
8192882Simpvoid	mangle(char *, int *, const char **);
8292882Simpchar   *update_options(char *, char *, int);
8392882Simpint	mountfs(const char *, const char *, const char *,
8492882Simp			int, const char *, const char *);
8592882Simpvoid	remopt(char *, const char *);
8692882Simpvoid	prmount(struct statfs *);
8792882Simpvoid	putfsent(const struct statfs *);
8892882Simpvoid	usage(void);
8992882Simpchar   *flags2opts(int);
901558Srgrimes
9137425Scharnier/* Map from mount options to printable formats. */
921558Srgrimesstatic struct opt {
931558Srgrimes	int o_opt;
941558Srgrimes	const char *o_name;
951558Srgrimes} optnames[] = {
961558Srgrimes	{ MNT_ASYNC,		"asynchronous" },
971558Srgrimes	{ MNT_EXPORTED,		"NFS exported" },
981558Srgrimes	{ MNT_LOCAL,		"local" },
9918007Sdg	{ MNT_NOATIME,		"noatime" },
1001558Srgrimes	{ MNT_NODEV,		"nodev" },
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" },
112101205Srwatson	{ MNT_MULTILABEL,	"multilabel" },
113105118Srwatson	{ MNT_ACLS,		"acls" },
11446619Sjkoshy	{ 0, NULL }
1151558Srgrimes};
1161558Srgrimes
11728671Ssteve/*
11828671Ssteve * List of VFS types that can be remounted without becoming mounted on top
11928671Ssteve * of each other.
12028671Ssteve * XXX Is this list correct?
12128671Ssteve */
12228671Sstevestatic const char *
12328671Ssteveremountable_fs_names[] = {
12469056Sphantom	"ufs", "ffs", "ext2fs",
12528671Ssteve	0
12628671Ssteve};
12728671Ssteve
1281558Srgrimesint
1291558Srgrimesmain(argc, argv)
1301558Srgrimes	int argc;
1311558Srgrimes	char * const argv[];
1321558Srgrimes{
13323678Speter	const char *mntfromname, **vfslist, *vfstype;
1341558Srgrimes	struct fstab *fs;
1351558Srgrimes	struct statfs *mntbuf;
1361558Srgrimes	FILE *mountdfp;
1371558Srgrimes	pid_t pid;
13852678Sgreen	int all, ch, i, init_flags, mntsize, rval, have_fstab;
13995289Smux	char *cp, *ep, *options;
1401558Srgrimes
1411558Srgrimes	all = init_flags = 0;
1421558Srgrimes	options = NULL;
1431558Srgrimes	vfslist = NULL;
1441558Srgrimes	vfstype = "ufs";
145113220Smdodd	while ((ch = getopt(argc, argv, "adF:fo:prwt:uv")) != -1)
1461558Srgrimes		switch (ch) {
1471558Srgrimes		case 'a':
1481558Srgrimes			all = 1;
1491558Srgrimes			break;
1501558Srgrimes		case 'd':
1511558Srgrimes			debug = 1;
1521558Srgrimes			break;
153113220Smdodd		case 'F':
154113220Smdodd			setfstab(optarg);
155113220Smdodd			break;
1561558Srgrimes		case 'f':
1571558Srgrimes			init_flags |= MNT_FORCE;
1581558Srgrimes			break;
1591558Srgrimes		case 'o':
1601558Srgrimes			if (*optarg)
1611558Srgrimes				options = catopt(options, optarg);
1621558Srgrimes			break;
16323805Sbde		case 'p':
16423805Sbde			fstab_style = 1;
16523805Sbde			verbose = 1;
16623805Sbde			break;
1671558Srgrimes		case 'r':
16876198Sdd			options = catopt(options, "ro");
1691558Srgrimes			break;
1701558Srgrimes		case 't':
1711558Srgrimes			if (vfslist != NULL)
17237425Scharnier				errx(1, "only one -t option may be specified");
1731558Srgrimes			vfslist = makevfslist(optarg);
1741558Srgrimes			vfstype = optarg;
1751558Srgrimes			break;
1761558Srgrimes		case 'u':
1771558Srgrimes			init_flags |= MNT_UPDATE;
1781558Srgrimes			break;
1791558Srgrimes		case 'v':
1801558Srgrimes			verbose = 1;
1811558Srgrimes			break;
1821558Srgrimes		case 'w':
18376198Sdd			options = catopt(options, "noro");
1841558Srgrimes			break;
1851558Srgrimes		case '?':
1861558Srgrimes		default:
1871558Srgrimes			usage();
1881558Srgrimes			/* NOTREACHED */
1891558Srgrimes		}
1901558Srgrimes	argc -= optind;
1911558Srgrimes	argv += optind;
1921558Srgrimes
1931558Srgrimes#define	BADTYPE(type)							\
1941558Srgrimes	(strcmp(type, FSTAB_RO) &&					\
1951558Srgrimes	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
1961558Srgrimes
1971558Srgrimes	rval = 0;
1981558Srgrimes	switch (argc) {
1991558Srgrimes	case 0:
20028671Ssteve		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
20128671Ssteve			err(1, "getmntinfo");
20228671Ssteve		if (all) {
2031558Srgrimes			while ((fs = getfsent()) != NULL) {
2041558Srgrimes				if (BADTYPE(fs->fs_type))
2051558Srgrimes					continue;
20623678Speter				if (checkvfsname(fs->fs_vfstype, vfslist))
2071558Srgrimes					continue;
20823678Speter				if (hasopt(fs->fs_mntops, "noauto"))
20910288Sdg					continue;
21044811Sbde				if (!(init_flags & MNT_UPDATE) &&
21144811Sbde				    ismounted(fs, mntbuf, mntsize))
21228671Ssteve					continue;
213125194Sguido				options = update_options(options, fs->fs_mntops,
214125194Sguido				    mntbuf->f_flags);
2151558Srgrimes				if (mountfs(fs->fs_vfstype, fs->fs_spec,
21623805Sbde				    fs->fs_file, init_flags, options,
21723805Sbde				    fs->fs_mntops))
21823805Sbde					rval = 1;
2191558Srgrimes			}
22028671Ssteve		} else if (fstab_style) {
22117243Sjkh			for (i = 0; i < mntsize; i++) {
22223678Speter				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
22317243Sjkh					continue;
22423805Sbde				putfsent(&mntbuf[i]);
22517243Sjkh			}
22623805Sbde		} else {
2271558Srgrimes			for (i = 0; i < mntsize; i++) {
22823805Sbde				if (checkvfsname(mntbuf[i].f_fstypename,
22923805Sbde				    vfslist))
2301558Srgrimes					continue;
23123678Speter				prmount(&mntbuf[i]);
2321558Srgrimes			}
2331558Srgrimes		}
2341558Srgrimes		exit(rval);
2351558Srgrimes	case 1:
2361558Srgrimes		if (vfslist != NULL)
2371558Srgrimes			usage();
2381558Srgrimes
239124201Sanholt		rmslashes(*argv, *argv);
2401558Srgrimes		if (init_flags & MNT_UPDATE) {
24152678Sgreen			mntfromname = NULL;
24252678Sgreen			have_fstab = 0;
2431558Srgrimes			if ((mntbuf = getmntpt(*argv)) == NULL)
24452678Sgreen				errx(1, "not currently mounted %s", *argv);
24552678Sgreen			/*
24652678Sgreen			 * Only get the mntflags from fstab if both mntpoint
24752678Sgreen			 * and mntspec are identical. Also handle the special
24852678Sgreen			 * case where just '/' is mounted and 'spec' is not
24952678Sgreen			 * identical with the one from fstab ('/dev' is missing
25052678Sgreen			 * in the spec-string at boot-time).
25152678Sgreen			 */
25246619Sjkoshy			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
25352678Sgreen				if (strcmp(fs->fs_spec,
25452678Sgreen				    mntbuf->f_mntfromname) == 0 &&
25552678Sgreen				    strcmp(fs->fs_file,
25652678Sgreen				    mntbuf->f_mntonname) == 0) {
25752678Sgreen					have_fstab = 1;
25852678Sgreen					mntfromname = mntbuf->f_mntfromname;
25952678Sgreen				} else if (argv[0][0] == '/' &&
26052678Sgreen				    argv[0][1] == '\0') {
26152678Sgreen					fs = getfsfile("/");
26252678Sgreen					have_fstab = 1;
26352678Sgreen					mntfromname = fs->fs_spec;
26452678Sgreen				}
26552678Sgreen			}
26652678Sgreen			if (have_fstab) {
26746619Sjkoshy				options = update_options(options, fs->fs_mntops,
26846619Sjkoshy				    mntbuf->f_flags);
26946619Sjkoshy			} else {
27023678Speter				mntfromname = mntbuf->f_mntfromname;
27146619Sjkoshy				options = update_options(options, NULL,
27246619Sjkoshy				    mntbuf->f_flags);
27346619Sjkoshy			}
27423678Speter			rval = mountfs(mntbuf->f_fstypename, mntfromname,
27523678Speter			    mntbuf->f_mntonname, init_flags, options, 0);
27623678Speter			break;
2771558Srgrimes		}
27823678Speter		if ((fs = getfsfile(*argv)) == NULL &&
27923678Speter		    (fs = getfsspec(*argv)) == NULL)
280102231Strhodes			errx(1, "%s: unknown special file or file system",
28123678Speter			    *argv);
28223678Speter		if (BADTYPE(fs->fs_type))
283102231Strhodes			errx(1, "%s has unknown file system type",
28423678Speter			    *argv);
28523678Speter		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
28623678Speter		    init_flags, options, fs->fs_mntops);
2871558Srgrimes		break;
2881558Srgrimes	case 2:
2891558Srgrimes		/*
29052678Sgreen		 * If -t flag has not been specified, the path cannot be
29195289Smux		 * found, spec contains either a ':' or a '@', then assume
292102231Strhodes		 * that an NFS file system is being specified ala Sun.
29395289Smux		 * Check if the hostname contains only allowed characters
29495289Smux		 * to reduce false positives.  IPv6 addresses containing
29595289Smux		 * ':' will be correctly parsed only if the separator is '@'.
29695289Smux		 * The definition of a valid hostname is taken from RFC 1034.
2971558Srgrimes		 */
298123162Siedowse		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
299123162Siedowse		    (ep = strchr(argv[0], ':')) != NULL)) {
30095318Smux			if (*ep == '@') {
30195318Smux				cp = ep + 1;
30295318Smux				ep = cp + strlen(cp);
30395318Smux			} else
30495318Smux				cp = argv[0];
30595289Smux			while (cp != ep) {
30695289Smux				if (!isdigit(*cp) && !isalpha(*cp) &&
30795289Smux				    *cp != '.' && *cp != '-' && *cp != ':')
30895289Smux					break;
30995289Smux				cp++;
31095289Smux			}
31195289Smux			if (cp == ep)
31295289Smux				vfstype = "nfs";
31395289Smux		}
3141558Srgrimes		rval = mountfs(vfstype,
3151558Srgrimes		    argv[0], argv[1], init_flags, options, NULL);
3161558Srgrimes		break;
3171558Srgrimes	default:
3181558Srgrimes		usage();
3191558Srgrimes		/* NOTREACHED */
3201558Srgrimes	}
3211558Srgrimes
3221558Srgrimes	/*
3231558Srgrimes	 * If the mount was successfully, and done by root, tell mountd the
3241558Srgrimes	 * good news.  Pid checks are probably unnecessary, but don't hurt.
3251558Srgrimes	 */
3261558Srgrimes	if (rval == 0 && getuid() == 0 &&
3271558Srgrimes	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
32828671Ssteve		if (fscanf(mountdfp, "%d", &pid) == 1 &&
3291558Srgrimes		     pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
3301558Srgrimes			err(1, "signal mountd");
3311558Srgrimes		(void)fclose(mountdfp);
3321558Srgrimes	}
3331558Srgrimes
3341558Srgrimes	exit(rval);
3351558Srgrimes}
3361558Srgrimes
3371558Srgrimesint
33828671Ssteveismounted(fs, mntbuf, mntsize)
33928671Ssteve	struct fstab *fs;
34028671Ssteve	struct statfs *mntbuf;
34128671Ssteve	int mntsize;
34228671Ssteve{
34328671Ssteve	int i;
34428671Ssteve
34528671Ssteve	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
346102231Strhodes		/* the root file system can always be remounted */
34728671Ssteve		return (0);
34828671Ssteve
34928671Ssteve	for (i = mntsize - 1; i >= 0; --i)
35028671Ssteve		if (strcmp(fs->fs_file, mntbuf[i].f_mntonname) == 0 &&
35128671Ssteve		    (!isremountable(fs->fs_vfstype) ||
35228671Ssteve		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
35328671Ssteve			return (1);
35428671Ssteve	return (0);
35528671Ssteve}
35628671Ssteve
35728671Ssteveint
35828671Ssteveisremountable(vfsname)
35928671Ssteve	const char *vfsname;
36028671Ssteve{
36128671Ssteve	const char **cp;
36228671Ssteve
36328671Ssteve	for (cp = remountable_fs_names; *cp; cp++)
36428671Ssteve		if (strcmp(*cp, vfsname) == 0)
36528671Ssteve			return (1);
36628671Ssteve	return (0);
36728671Ssteve}
36828671Ssteve
36928671Ssteveint
37023678Speterhasopt(mntopts, option)
37123678Speter	const char *mntopts, *option;
37223678Speter{
37323678Speter	int negative, found;
37423678Speter	char *opt, *optbuf;
37523678Speter
37623678Speter	if (option[0] == 'n' && option[1] == 'o') {
37723678Speter		negative = 1;
37823678Speter		option += 2;
37923678Speter	} else
38023678Speter		negative = 0;
38123678Speter	optbuf = strdup(mntopts);
38223678Speter	found = 0;
38323678Speter	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
38423678Speter		if (opt[0] == 'n' && opt[1] == 'o') {
38523678Speter			if (!strcasecmp(opt + 2, option))
38623678Speter				found = negative;
38723678Speter		} else if (!strcasecmp(opt, option))
38823678Speter			found = !negative;
38923678Speter	}
39023678Speter	free(optbuf);
39123678Speter	return (found);
39223678Speter}
39323678Speter
39423678Speterint
3951558Srgrimesmountfs(vfstype, spec, name, flags, options, mntopts)
3961558Srgrimes	const char *vfstype, *spec, *name, *options, *mntopts;
3971558Srgrimes	int flags;
3981558Srgrimes{
399123162Siedowse	const char *argv[100];
4001558Srgrimes	struct statfs sf;
4011558Srgrimes	pid_t pid;
4021558Srgrimes	int argc, i, status;
403118580Simp	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
4041558Srgrimes
40528671Ssteve#if __GNUC__
40628671Ssteve	(void)&optbuf;
40728671Ssteve	(void)&name;
40828671Ssteve#endif
40928671Ssteve
41052055Sphk	/* resolve the mountpoint with realpath(3) */
41152055Sphk	(void)checkpath(name, mntpath);
4121558Srgrimes	name = mntpath;
4131558Srgrimes
41423678Speter	if (mntopts == NULL)
41523678Speter		mntopts = "";
4161558Srgrimes	if (options == NULL) {
41723678Speter		if (*mntopts == '\0') {
4181558Srgrimes			options = "rw";
41923678Speter		} else {
4201558Srgrimes			options = mntopts;
42123678Speter			mntopts = "";
42223678Speter		}
4231558Srgrimes	}
4241558Srgrimes	optbuf = catopt(strdup(mntopts), options);
4251558Srgrimes
4261558Srgrimes	if (strcmp(name, "/") == 0)
4271558Srgrimes		flags |= MNT_UPDATE;
4281558Srgrimes	if (flags & MNT_FORCE)
4291558Srgrimes		optbuf = catopt(optbuf, "force");
4301558Srgrimes	if (flags & MNT_RDONLY)
4311558Srgrimes		optbuf = catopt(optbuf, "ro");
4321558Srgrimes	/*
4331558Srgrimes	 * XXX
4341558Srgrimes	 * The mount_mfs (newfs) command uses -o to select the
43537425Scharnier	 * optimization mode.  We don't pass the default "-o rw"
4361558Srgrimes	 * for that reason.
4371558Srgrimes	 */
4381558Srgrimes	if (flags & MNT_UPDATE)
4391558Srgrimes		optbuf = catopt(optbuf, "update");
4401558Srgrimes
44177577Sru	/* Compatibility glue. */
44277577Sru	if (strcmp(vfstype, "msdos") == 0)
44377577Sru		vfstype = "msdosfs";
44477577Sru
4451558Srgrimes	argc = 0;
4461558Srgrimes	argv[argc++] = vfstype;
4471558Srgrimes	mangle(optbuf, &argc, argv);
4481558Srgrimes	argv[argc++] = spec;
4491558Srgrimes	argv[argc++] = name;
4501558Srgrimes	argv[argc] = NULL;
4511558Srgrimes
4521558Srgrimes	if (debug) {
4531558Srgrimes		(void)printf("exec: mount_%s", vfstype);
4541558Srgrimes		for (i = 1; i < argc; i++)
4551558Srgrimes			(void)printf(" %s", argv[i]);
4561558Srgrimes		(void)printf("\n");
4571558Srgrimes		return (0);
4581558Srgrimes	}
4591558Srgrimes
46025120Sache	switch (pid = fork()) {
4611558Srgrimes	case -1:				/* Error. */
46225120Sache		warn("fork");
4631558Srgrimes		free(optbuf);
4641558Srgrimes		return (1);
4651558Srgrimes	case 0:					/* Child. */
4661558Srgrimes		if (strcmp(vfstype, "ufs") == 0)
4671558Srgrimes			exit(mount_ufs(argc, (char * const *) argv));
4681558Srgrimes
4691558Srgrimes		/* Go find an executable. */
470117031Sgordon		(void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
471117031Sgordon		execvP(execname, _PATH_SYSPATH, (char * const *)argv);
47214626Sasami		if (errno == ENOENT) {
473123162Siedowse			warn("exec mount_%s not found in %s", vfstype,
474123162Siedowse			    _PATH_SYSPATH);
47514626Sasami		}
4761558Srgrimes		exit(1);
4771558Srgrimes		/* NOTREACHED */
4781558Srgrimes	default:				/* Parent. */
4791558Srgrimes		free(optbuf);
4801558Srgrimes
4811558Srgrimes		if (waitpid(pid, &status, 0) < 0) {
4821558Srgrimes			warn("waitpid");
4831558Srgrimes			return (1);
4841558Srgrimes		}
4851558Srgrimes
4861558Srgrimes		if (WIFEXITED(status)) {
4871558Srgrimes			if (WEXITSTATUS(status) != 0)
4881558Srgrimes				return (WEXITSTATUS(status));
4891558Srgrimes		} else if (WIFSIGNALED(status)) {
4901558Srgrimes			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
4911558Srgrimes			return (1);
4921558Srgrimes		}
4931558Srgrimes
4941558Srgrimes		if (verbose) {
4951558Srgrimes			if (statfs(name, &sf) < 0) {
49623678Speter				warn("statfs %s", name);
4971558Srgrimes				return (1);
4981558Srgrimes			}
49917243Sjkh			if (fstab_style)
50023805Sbde				putfsent(&sf);
50117243Sjkh			else
50223678Speter				prmount(&sf);
5031558Srgrimes		}
5041558Srgrimes		break;
5051558Srgrimes	}
5061558Srgrimes
5071558Srgrimes	return (0);
5081558Srgrimes}
5091558Srgrimes
5101558Srgrimesvoid
51123678Speterprmount(sfp)
51223678Speter	struct statfs *sfp;
51323678Speter{
514117742Siedowse	int flags, i;
5151558Srgrimes	struct opt *o;
51623678Speter	struct passwd *pw;
5171558Srgrimes
51852036Sn_hibma	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
51952036Sn_hibma	    sfp->f_fstypename);
5201558Srgrimes
52123678Speter	flags = sfp->f_flags & MNT_VISFLAGMASK;
52252036Sn_hibma	for (o = optnames; flags && o->o_opt; o++)
5231558Srgrimes		if (flags & o->o_opt) {
52452036Sn_hibma			(void)printf(", %s", o->o_name);
5251558Srgrimes			flags &= ~o->o_opt;
5261558Srgrimes		}
527125339Spjd	/*
528125339Spjd	 * Inform when file system is mounted by an unprivileged user
529125339Spjd	 * or privileged non-root user.
530125339Spjd	 */
531125365Snectar	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
53252036Sn_hibma		(void)printf(", mounted by ");
53323678Speter		if ((pw = getpwuid(sfp->f_owner)) != NULL)
53423678Speter			(void)printf("%s", pw->pw_name);
53523678Speter		else
53623678Speter			(void)printf("%d", sfp->f_owner);
53723678Speter	}
53865023Ssheldonh	if (verbose) {
53965023Ssheldonh		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
540123242Siedowse			(void)printf(", writes: sync %ju async %ju",
541123242Siedowse			    (uintmax_t)sfp->f_syncwrites,
542123242Siedowse			    (uintmax_t)sfp->f_asyncwrites);
54365023Ssheldonh		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
544123242Siedowse			(void)printf(", reads: sync %ju async %ju",
545123242Siedowse			    (uintmax_t)sfp->f_syncreads,
546123242Siedowse			    (uintmax_t)sfp->f_asyncreads);
547123162Siedowse		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
548123162Siedowse			printf(", fsid ");
549123162Siedowse			for (i = 0; i < sizeof(sfp->f_fsid); i++)
550123162Siedowse				printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
551123162Siedowse		}
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, '=');
611123268Strhodes				if (p != NULL) {
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",
713125200Sguido"       mount [-adfpruvw] [ -F fstab] [-o options] [-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");
765101205Srwatson	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
766105118Srwatson	if (flags & MNT_ACLS)		res = catopt(res, "acls");
76746619Sjkoshy
76846619Sjkoshy	return res;
76946619Sjkoshy}
770