mount.c revision 222832
11553Srgrimes/*-
21553Srgrimes * Copyright (c) 1980, 1989, 1993, 1994
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 4. Neither the name of the University nor the names of its contributors
141553Srgrimes *    may be used to endorse or promote products derived from this software
151553Srgrimes *    without specific prior written permission.
161553Srgrimes *
171553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271553Srgrimes * SUCH DAMAGE.
281553Srgrimes */
291553Srgrimes
301553Srgrimes#ifndef lint
3130642Scharnierstatic const char copyright[] =
321553Srgrimes"@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
3330642Scharnier	The Regents of the University of California.  All rights reserved.\n";
3430642Scharnier#if 0
3550479Speterstatic char sccsid[] = "@(#)mount.c	8.25 (Berkeley) 5/8/95";
361553Srgrimes#endif
371553Srgrimes#endif /* not lint */
381553Srgrimes
391553Srgrimes#include <sys/cdefs.h>
401553Srgrimes__FBSDID("$FreeBSD: head/sbin/mount/mount.c 222832 2011-06-07 18:48:49Z delphij $");
411553Srgrimes
421553Srgrimes#include <sys/param.h>
431553Srgrimes#include <sys/mount.h>
441553Srgrimes#include <sys/stat.h>
451553Srgrimes#include <sys/wait.h>
461553Srgrimes
471553Srgrimes#include <ctype.h>
481553Srgrimes#include <err.h>
491553Srgrimes#include <errno.h>
501553Srgrimes#include <fstab.h>
511553Srgrimes#include <paths.h>
521553Srgrimes#include <pwd.h>
531553Srgrimes#include <signal.h>
541553Srgrimes#include <stdint.h>
551553Srgrimes#include <stdio.h>
561553Srgrimes#include <stdlib.h>
571553Srgrimes#include <string.h>
581553Srgrimes#include <unistd.h>
591553Srgrimes#include <libutil.h>
601553Srgrimes
611553Srgrimes#include "extern.h"
621553Srgrimes#include "mntopts.h"
631553Srgrimes#include "pathnames.h"
641553Srgrimes
651553Srgrimes/* `meta' options */
661553Srgrimes#define MOUNT_META_OPTION_FSTAB		"fstab"
671553Srgrimes#define MOUNT_META_OPTION_CURRENT	"current"
681553Srgrimes
691553Srgrimesint debug, fstab_style, verbose;
701553Srgrimes
711553Srgrimesstruct cpa {
721553Srgrimes	char	**a;
731553Srgrimes	ssize_t	sz;
741553Srgrimes	int	c;
751553Srgrimes};
761553Srgrimes
771553Srgrimeschar   *catopt(char *, const char *);
781553Srgrimesstruct statfs *getmntpt(const char *);
791553Srgrimesint	hasopt(const char *, const char *);
801553Srgrimesint	ismounted(struct fstab *, struct statfs *, int);
811553Srgrimesint	isremountable(const char *);
821553Srgrimesvoid	mangle(char *, struct cpa *);
831553Srgrimeschar   *update_options(char *, char *, int);
841553Srgrimesint	mountfs(const char *, const char *, const char *,
85209344Sgavin			int, const char *, const char *);
861553Srgrimesvoid	remopt(char *, const char *);
871553Srgrimesvoid	prmount(struct statfs *);
881553Srgrimesvoid	putfsent(struct statfs *);
891553Srgrimesvoid	usage(void);
901553Srgrimeschar   *flags2opts(int);
911553Srgrimes
921553Srgrimes/* Map from mount options to printable formats. */
931553Srgrimesstatic struct opt {
941553Srgrimes	uint64_t o_opt;
951553Srgrimes	const char *o_name;
961553Srgrimes} optnames[] = {
978857Srgrimes	{ MNT_ASYNC,		"asynchronous" },
981553Srgrimes	{ MNT_EXPORTED,		"NFS exported" },
991553Srgrimes	{ MNT_LOCAL,		"local" },
1001553Srgrimes	{ MNT_NOATIME,		"noatime" },
1011553Srgrimes	{ MNT_NOEXEC,		"noexec" },
1021553Srgrimes	{ MNT_NOSUID,		"nosuid" },
1031553Srgrimes	{ MNT_NOSYMFOLLOW,	"nosymfollow" },
1041553Srgrimes	{ MNT_QUOTA,		"with quotas" },
1051553Srgrimes	{ MNT_RDONLY,		"read-only" },
1061553Srgrimes	{ MNT_SYNCHRONOUS,	"synchronous" },
1071553Srgrimes	{ MNT_UNION,		"union" },
1081553Srgrimes	{ MNT_NOCLUSTERR,	"noclusterr" },
1091553Srgrimes	{ MNT_NOCLUSTERW,	"noclusterw" },
1101553Srgrimes	{ MNT_SUIDDIR,		"suiddir" },
1111553Srgrimes	{ MNT_SOFTDEP,		"soft-updates" },
1121553Srgrimes	{ MNT_MULTILABEL,	"multilabel" },
1131553Srgrimes	{ MNT_ACLS,		"acls" },
1141553Srgrimes	{ MNT_NFS4ACLS,		"nfsv4acls" },
1151553Srgrimes	{ MNT_GJOURNAL,		"gjournal" },
1161553Srgrimes	{ 0, NULL }
1171553Srgrimes};
1181553Srgrimes
1191553Srgrimes/*
1201553Srgrimes * List of VFS types that can be remounted without becoming mounted on top
1211553Srgrimes * of each other.
1221553Srgrimes * XXX Is this list correct?
1231553Srgrimes */
1241553Srgrimesstatic const char *
1251553Srgrimesremountable_fs_names[] = {
1261553Srgrimes	"ufs", "ffs", "ext2fs",
1271553Srgrimes	0
1281553Srgrimes};
1291553Srgrimes
1301553Srgrimesstatic const char userquotaeq[] = "userquota=";
1311553Srgrimesstatic const char groupquotaeq[] = "groupquota=";
1321553Srgrimes
1331553Srgrimesstatic char *mountprog = NULL;
1341553Srgrimes
1351553Srgrimesstatic int
1361553Srgrimesuse_mountprog(const char *vfstype)
1371553Srgrimes{
1381553Srgrimes	/* XXX: We need to get away from implementing external mount
1391553Srgrimes	 *      programs for every filesystem, and move towards having
1401553Srgrimes	 *	each filesystem properly implement the nmount() system call.
1411553Srgrimes	 */
1421553Srgrimes	unsigned int i;
1431553Srgrimes	const char *fs[] = {
1441553Srgrimes	"cd9660", "mfs", "msdosfs", "nfs", "ntfs",
1451553Srgrimes	"nwfs", "nullfs", "oldnfs", "portalfs", "smbfs", "udf", "unionfs",
14630761Scharnier	NULL
1474840Sbde	};
1481553Srgrimes
1491553Srgrimes	if (mountprog != NULL)
1501553Srgrimes		return (1);
1511553Srgrimes
1521553Srgrimes	for (i = 0; fs[i] != NULL; ++i) {
1531553Srgrimes		if (strcmp(vfstype, fs[i]) == 0)
1541553Srgrimes			return (1);
1551553Srgrimes	}
1561553Srgrimes
1571553Srgrimes	return (0);
1581553Srgrimes}
1591553Srgrimes
1601553Srgrimesstatic int
1611553Srgrimesexec_mountprog(const char *name, const char *execname, char *const argv[])
1621553Srgrimes{
1631553Srgrimes	pid_t pid;
1641553Srgrimes	int status;
1651553Srgrimes
1661553Srgrimes	switch (pid = fork()) {
1671553Srgrimes	case -1:				/* Error. */
1681553Srgrimes		warn("fork");
1691553Srgrimes		exit (1);
1701553Srgrimes	case 0:					/* Child. */
1711553Srgrimes		/* Go find an executable. */
1721553Srgrimes		execvP(execname, _PATH_SYSPATH, argv);
1731553Srgrimes		if (errno == ENOENT) {
1741553Srgrimes			warn("exec %s not found", execname);
1751553Srgrimes			if (execname[0] != '/') {
1761553Srgrimes				warnx("in path: %s", _PATH_SYSPATH);
1771553Srgrimes			}
1781553Srgrimes		}
1791553Srgrimes		exit(1);
1801553Srgrimes	default:				/* Parent. */
1811553Srgrimes		if (waitpid(pid, &status, 0) < 0) {
1821553Srgrimes			warn("waitpid");
1831553Srgrimes			return (1);
1841553Srgrimes		}
1851553Srgrimes
1868857Srgrimes		if (WIFEXITED(status)) {
1871553Srgrimes			if (WEXITSTATUS(status) != 0)
1881553Srgrimes				return (WEXITSTATUS(status));
1891553Srgrimes		} else if (WIFSIGNALED(status)) {
1901553Srgrimes			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
1911553Srgrimes			return (1);
1921553Srgrimes		}
1931553Srgrimes		break;
1941553Srgrimes	}
1951553Srgrimes
1961553Srgrimes	return (0);
1971553Srgrimes}
1981553Srgrimes
1991553Srgrimesstatic int
2001553Srgrimesspecified_ro(const char *arg)
2011553Srgrimes{
2021553Srgrimes	char *optbuf, *opt;
2031553Srgrimes	int ret = 0;
2041553Srgrimes
2051553Srgrimes	optbuf = strdup(arg);
2061553Srgrimes	if (optbuf == NULL)
2071553Srgrimes		 err(1, NULL);
2081553Srgrimes
2091553Srgrimes	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
2101553Srgrimes		if (strcmp(opt, "ro") == 0) {
2111553Srgrimes			ret = 1;
2121553Srgrimes			break;
2131553Srgrimes		}
2141553Srgrimes	}
2151553Srgrimes	free(optbuf);
2161553Srgrimes	return (ret);
2171553Srgrimes}
2181553Srgrimes
2191553Srgrimesstatic void
2201553Srgrimesrestart_mountd(void)
2211553Srgrimes{
2221553Srgrimes	struct pidfh *pfh;
2231553Srgrimes	pid_t mountdpid;
2241553Srgrimes
2251553Srgrimes	pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &mountdpid);
2261553Srgrimes	if (pfh != NULL) {
2271553Srgrimes		/* Mountd is not running. */
2281553Srgrimes		pidfile_remove(pfh);
2291553Srgrimes		return;
2301553Srgrimes	}
2311553Srgrimes	if (errno != EEXIST) {
2321553Srgrimes		/* Cannot open pidfile for some reason. */
2331553Srgrimes		return;
2341553Srgrimes	}
2351553Srgrimes	/* We have mountd(8) PID in mountdpid varible, let's signal it. */
2361553Srgrimes	if (kill(mountdpid, SIGHUP) == -1)
2371553Srgrimes		err(1, "signal mountd");
2381553Srgrimes}
2391553Srgrimes
2401553Srgrimesint
2411553Srgrimesmain(int argc, char *argv[])
2421553Srgrimes{
2431553Srgrimes	const char *mntfromname, **vfslist, *vfstype;
2441553Srgrimes	struct fstab *fs;
2451553Srgrimes	struct statfs *mntbuf;
2461553Srgrimes	int all, ch, i, init_flags, late, failok, mntsize, rval, have_fstab, ro;
2471553Srgrimes	char *cp, *ep, *options;
2481553Srgrimes
2491553Srgrimes	all = init_flags = late = 0;
2501553Srgrimes	ro = 0;
2511553Srgrimes	options = NULL;
2521553Srgrimes	vfslist = NULL;
2531553Srgrimes	vfstype = "ufs";
2541553Srgrimes	while ((ch = getopt(argc, argv, "adF:flo:prt:uvw")) != -1)
2551553Srgrimes		switch (ch) {
2561553Srgrimes		case 'a':
2571553Srgrimes			all = 1;
2581553Srgrimes			break;
2591553Srgrimes		case 'd':
2601553Srgrimes			debug = 1;
2611553Srgrimes			break;
2621553Srgrimes		case 'F':
2631553Srgrimes			setfstab(optarg);
2641553Srgrimes			break;
2651553Srgrimes		case 'f':
2661553Srgrimes			init_flags |= MNT_FORCE;
2671553Srgrimes			break;
2681553Srgrimes		case 'l':
26930642Scharnier			late = 1;
2701553Srgrimes			break;
2711553Srgrimes		case 'o':
2721553Srgrimes			if (*optarg) {
2731553Srgrimes				options = catopt(options, optarg);
2741553Srgrimes				if (specified_ro(optarg))
2751553Srgrimes					ro = 1;
2761553Srgrimes			}
2771553Srgrimes			break;
2781553Srgrimes		case 'p':
2791553Srgrimes			fstab_style = 1;
2801553Srgrimes			verbose = 1;
2811553Srgrimes			break;
2821553Srgrimes		case 'r':
2831553Srgrimes			options = catopt(options, "ro");
2841553Srgrimes			ro = 1;
2851553Srgrimes			break;
2861553Srgrimes		case 't':
2871553Srgrimes			if (vfslist != NULL)
2881553Srgrimes				errx(1, "only one -t option may be specified");
2891553Srgrimes			vfslist = makevfslist(optarg);
2901553Srgrimes			vfstype = optarg;
2911553Srgrimes			break;
2921553Srgrimes		case 'u':
2931553Srgrimes			init_flags |= MNT_UPDATE;
2941553Srgrimes			break;
2951553Srgrimes		case 'v':
2961553Srgrimes			verbose = 1;
2971553Srgrimes			break;
2981553Srgrimes		case 'w':
2991553Srgrimes			options = catopt(options, "noro");
3001553Srgrimes			break;
3011553Srgrimes		case '?':
3021553Srgrimes		default:
3031553Srgrimes			usage();
3041553Srgrimes			/* NOTREACHED */
3051553Srgrimes		}
3061553Srgrimes	argc -= optind;
3071553Srgrimes	argv += optind;
3081553Srgrimes
3091553Srgrimes#define	BADTYPE(type)							\
3101553Srgrimes	(strcmp(type, FSTAB_RO) &&					\
3111553Srgrimes	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
3121553Srgrimes
3131553Srgrimes	if ((init_flags & MNT_UPDATE) && (ro == 0))
3141553Srgrimes		options = catopt(options, "noro");
3151553Srgrimes
3161553Srgrimes	rval = 0;
3171553Srgrimes	switch (argc) {
3181553Srgrimes	case 0:
3191553Srgrimes		if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
3201553Srgrimes			err(1, "getmntinfo");
3211553Srgrimes		if (all) {
3221553Srgrimes			while ((fs = getfsent()) != NULL) {
3231553Srgrimes				if (BADTYPE(fs->fs_type))
3241553Srgrimes					continue;
3251553Srgrimes				if (checkvfsname(fs->fs_vfstype, vfslist))
3261553Srgrimes					continue;
3271553Srgrimes				if (hasopt(fs->fs_mntops, "noauto"))
3281553Srgrimes					continue;
3291553Srgrimes				if (hasopt(fs->fs_mntops, "late") && !late)
3301553Srgrimes					continue;
3311553Srgrimes				if (hasopt(fs->fs_mntops, "failok"))
3321553Srgrimes					failok = 1;
3331553Srgrimes				else
3341553Srgrimes					failok = 0;
3351553Srgrimes				if (!(init_flags & MNT_UPDATE) &&
3361553Srgrimes				    ismounted(fs, mntbuf, mntsize))
3371553Srgrimes					continue;
3381553Srgrimes				options = update_options(options, fs->fs_mntops,
3391553Srgrimes				    mntbuf->f_flags);
3401553Srgrimes				if (mountfs(fs->fs_vfstype, fs->fs_spec,
3411553Srgrimes				    fs->fs_file, init_flags, options,
3421553Srgrimes				    fs->fs_mntops) && !failok)
343					rval = 1;
344			}
345		} else if (fstab_style) {
346			for (i = 0; i < mntsize; i++) {
347				if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
348					continue;
349				putfsent(&mntbuf[i]);
350			}
351		} else {
352			for (i = 0; i < mntsize; i++) {
353				if (checkvfsname(mntbuf[i].f_fstypename,
354				    vfslist))
355					continue;
356				if (!verbose &&
357				    (mntbuf[i].f_flags & MNT_IGNORE) != 0)
358					continue;
359				prmount(&mntbuf[i]);
360			}
361		}
362		exit(rval);
363	case 1:
364		if (vfslist != NULL)
365			usage();
366
367		rmslashes(*argv, *argv);
368		if (init_flags & MNT_UPDATE) {
369			mntfromname = NULL;
370			have_fstab = 0;
371			if ((mntbuf = getmntpt(*argv)) == NULL)
372				errx(1, "not currently mounted %s", *argv);
373			/*
374			 * Only get the mntflags from fstab if both mntpoint
375			 * and mntspec are identical. Also handle the special
376			 * case where just '/' is mounted and 'spec' is not
377			 * identical with the one from fstab ('/dev' is missing
378			 * in the spec-string at boot-time).
379			 */
380			if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
381				if (strcmp(fs->fs_spec,
382				    mntbuf->f_mntfromname) == 0 &&
383				    strcmp(fs->fs_file,
384				    mntbuf->f_mntonname) == 0) {
385					have_fstab = 1;
386					mntfromname = mntbuf->f_mntfromname;
387				} else if (argv[0][0] == '/' &&
388				    argv[0][1] == '\0') {
389					fs = getfsfile("/");
390					have_fstab = 1;
391					mntfromname = fs->fs_spec;
392				}
393			}
394			if (have_fstab) {
395				options = update_options(options, fs->fs_mntops,
396				    mntbuf->f_flags);
397			} else {
398				mntfromname = mntbuf->f_mntfromname;
399				options = update_options(options, NULL,
400				    mntbuf->f_flags);
401			}
402			rval = mountfs(mntbuf->f_fstypename, mntfromname,
403			    mntbuf->f_mntonname, init_flags, options, 0);
404			break;
405		}
406		if ((fs = getfsfile(*argv)) == NULL &&
407		    (fs = getfsspec(*argv)) == NULL)
408			errx(1, "%s: unknown special file or file system",
409			    *argv);
410		if (BADTYPE(fs->fs_type))
411			errx(1, "%s has unknown file system type",
412			    *argv);
413		rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
414		    init_flags, options, fs->fs_mntops);
415		break;
416	case 2:
417		/*
418		 * If -t flag has not been specified, the path cannot be
419		 * found, spec contains either a ':' or a '@', then assume
420		 * that an NFS file system is being specified ala Sun.
421		 * Check if the hostname contains only allowed characters
422		 * to reduce false positives.  IPv6 addresses containing
423		 * ':' will be correctly parsed only if the separator is '@'.
424		 * The definition of a valid hostname is taken from RFC 1034.
425		 */
426		if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
427		    (ep = strchr(argv[0], ':')) != NULL)) {
428			if (*ep == '@') {
429				cp = ep + 1;
430				ep = cp + strlen(cp);
431			} else
432				cp = argv[0];
433			while (cp != ep) {
434				if (!isdigit(*cp) && !isalpha(*cp) &&
435				    *cp != '.' && *cp != '-' && *cp != ':')
436					break;
437				cp++;
438			}
439			if (cp == ep)
440				vfstype = "nfs";
441		}
442		rval = mountfs(vfstype,
443		    argv[0], argv[1], init_flags, options, NULL);
444		break;
445	default:
446		usage();
447		/* NOTREACHED */
448	}
449
450	/*
451	 * If the mount was successfully, and done by root, tell mountd the
452	 * good news.
453	 */
454	if (rval == 0 && getuid() == 0)
455		restart_mountd();
456
457	exit(rval);
458}
459
460int
461ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
462{
463	char realfsfile[PATH_MAX];
464	int i;
465
466	if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
467		/* the root file system can always be remounted */
468		return (0);
469
470	/* The user may have specified a symlink in fstab, resolve the path */
471	if (realpath(fs->fs_file, realfsfile) == NULL) {
472		/* Cannot resolve the path, use original one */
473		strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
474	}
475
476	for (i = mntsize - 1; i >= 0; --i)
477		if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
478		    (!isremountable(fs->fs_vfstype) ||
479		     strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
480			return (1);
481	return (0);
482}
483
484int
485isremountable(const char *vfsname)
486{
487	const char **cp;
488
489	for (cp = remountable_fs_names; *cp; cp++)
490		if (strcmp(*cp, vfsname) == 0)
491			return (1);
492	return (0);
493}
494
495int
496hasopt(const char *mntopts, const char *option)
497{
498	int negative, found;
499	char *opt, *optbuf;
500
501	if (option[0] == 'n' && option[1] == 'o') {
502		negative = 1;
503		option += 2;
504	} else
505		negative = 0;
506	optbuf = strdup(mntopts);
507	found = 0;
508	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
509		if (opt[0] == 'n' && opt[1] == 'o') {
510			if (!strcasecmp(opt + 2, option))
511				found = negative;
512		} else if (!strcasecmp(opt, option))
513			found = !negative;
514	}
515	free(optbuf);
516	return (found);
517}
518
519static void
520append_arg(struct cpa *sa, char *arg)
521{
522	if (sa->c + 1 == sa->sz) {
523		sa->sz = sa->sz == 0 ? 8 : sa->sz * 2;
524		sa->a = realloc(sa->a, sizeof(sa->a) * sa->sz);
525		if (sa->a == NULL)
526			errx(1, "realloc failed");
527	}
528	sa->a[++sa->c] = arg;
529}
530
531int
532mountfs(const char *vfstype, const char *spec, const char *name, int flags,
533	const char *options, const char *mntopts)
534{
535	struct statfs sf;
536	int i, ret;
537	char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
538	static struct cpa mnt_argv;
539
540	/* resolve the mountpoint with realpath(3) */
541	(void)checkpath(name, mntpath);
542	name = mntpath;
543
544	if (mntopts == NULL)
545		mntopts = "";
546	optbuf = catopt(strdup(mntopts), options);
547
548	if (strcmp(name, "/") == 0)
549		flags |= MNT_UPDATE;
550	if (flags & MNT_FORCE)
551		optbuf = catopt(optbuf, "force");
552	if (flags & MNT_RDONLY)
553		optbuf = catopt(optbuf, "ro");
554	/*
555	 * XXX
556	 * The mount_mfs (newfs) command uses -o to select the
557	 * optimization mode.  We don't pass the default "-o rw"
558	 * for that reason.
559	 */
560	if (flags & MNT_UPDATE)
561		optbuf = catopt(optbuf, "update");
562
563	/* Compatibility glue. */
564	if (strcmp(vfstype, "msdos") == 0) {
565		warnx(
566		    "Using \"-t msdosfs\", since \"-t msdos\" is deprecated.");
567		vfstype = "msdosfs";
568	}
569
570	/* Construct the name of the appropriate mount command */
571	(void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
572
573	mnt_argv.c = -1;
574	append_arg(&mnt_argv, execname);
575	mangle(optbuf, &mnt_argv);
576	if (mountprog != NULL)
577		strcpy(execname, mountprog);
578
579	append_arg(&mnt_argv, strdup(spec));
580	append_arg(&mnt_argv, strdup(name));
581	append_arg(&mnt_argv, NULL);
582
583	if (debug) {
584		if (use_mountprog(vfstype))
585			printf("exec: %s", execname);
586		else
587			printf("mount -t %s", vfstype);
588		for (i = 1; i < mnt_argv.c; i++)
589			(void)printf(" %s", mnt_argv.a[i]);
590		(void)printf("\n");
591		return (0);
592	}
593
594	if (use_mountprog(vfstype)) {
595		ret = exec_mountprog(name, execname, mnt_argv.a);
596	} else {
597		ret = mount_fs(vfstype, mnt_argv.c, mnt_argv.a);
598	}
599
600	free(optbuf);
601
602	if (verbose) {
603		if (statfs(name, &sf) < 0) {
604			warn("statfs %s", name);
605			return (1);
606		}
607		if (fstab_style)
608			putfsent(&sf);
609		else
610			prmount(&sf);
611	}
612
613	return (ret);
614}
615
616void
617prmount(struct statfs *sfp)
618{
619	uint64_t flags;
620	unsigned int i;
621	struct opt *o;
622	struct passwd *pw;
623
624	(void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
625	    sfp->f_fstypename);
626
627	flags = sfp->f_flags & MNT_VISFLAGMASK;
628	for (o = optnames; flags != 0 && o->o_opt != 0; o++)
629		if (flags & o->o_opt) {
630			(void)printf(", %s", o->o_name);
631			flags &= ~o->o_opt;
632		}
633	/*
634	 * Inform when file system is mounted by an unprivileged user
635	 * or privileged non-root user.
636	 */
637	if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
638		(void)printf(", mounted by ");
639		if ((pw = getpwuid(sfp->f_owner)) != NULL)
640			(void)printf("%s", pw->pw_name);
641		else
642			(void)printf("%d", sfp->f_owner);
643	}
644	if (verbose) {
645		if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
646			(void)printf(", writes: sync %ju async %ju",
647			    (uintmax_t)sfp->f_syncwrites,
648			    (uintmax_t)sfp->f_asyncwrites);
649		if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
650			(void)printf(", reads: sync %ju async %ju",
651			    (uintmax_t)sfp->f_syncreads,
652			    (uintmax_t)sfp->f_asyncreads);
653		if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
654			printf(", fsid ");
655			for (i = 0; i < sizeof(sfp->f_fsid); i++)
656				printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
657		}
658	}
659	(void)printf(")\n");
660}
661
662struct statfs *
663getmntpt(const char *name)
664{
665	struct statfs *mntbuf;
666	int i, mntsize;
667
668	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
669	for (i = mntsize - 1; i >= 0; i--) {
670		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
671		    strcmp(mntbuf[i].f_mntonname, name) == 0)
672			return (&mntbuf[i]);
673	}
674	return (NULL);
675}
676
677char *
678catopt(char *s0, const char *s1)
679{
680	size_t i;
681	char *cp;
682
683	if (s1 == NULL || *s1 == '\0')
684		return (s0);
685
686	if (s0 && *s0) {
687		i = strlen(s0) + strlen(s1) + 1 + 1;
688		if ((cp = malloc(i)) == NULL)
689			errx(1, "malloc failed");
690		(void)snprintf(cp, i, "%s,%s", s0, s1);
691	} else
692		cp = strdup(s1);
693
694	if (s0)
695		free(s0);
696	return (cp);
697}
698
699void
700mangle(char *options, struct cpa *a)
701{
702	char *p, *s, *val;
703
704	for (s = options; (p = strsep(&s, ",")) != NULL;)
705		if (*p != '\0') {
706			if (strcmp(p, "noauto") == 0) {
707				/*
708				 * Do not pass noauto option to nmount().
709				 * or external mount program.  noauto is
710				 * only used to prevent mounting a filesystem
711				 * when 'mount -a' is specified, and is
712				 * not a real mount option.
713				 */
714				continue;
715			} else if (strcmp(p, "late") == 0) {
716				/*
717				 * "late" is used to prevent certain file
718				 * systems from being mounted before late
719				 * in the boot cycle; for instance,
720				 * loopback NFS mounts can't be mounted
721				 * before mountd starts.
722				 */
723				continue;
724			} else if (strcmp(p, "failok") == 0) {
725				/*
726				 * "failok" is used to prevent certain file
727				 * systems from being causing the system to
728				 * drop into single user mode in the boot
729				 * cycle, and is not a real mount option.
730				 */
731				continue;
732			} else if (strncmp(p, "mountprog", 9) == 0) {
733				/*
734				 * "mountprog" is used to force the use of
735				 * userland mount programs.
736				 */
737				val = strchr(p, '=');
738                        	if (val != NULL) {
739                                	++val;
740					if (*val != '\0')
741						mountprog = strdup(val);
742				}
743
744				if (mountprog == NULL) {
745					errx(1, "Need value for -o mountprog");
746				}
747				continue;
748			} else if (strcmp(p, "userquota") == 0) {
749				continue;
750			} else if (strncmp(p, userquotaeq,
751			    sizeof(userquotaeq) - 1) == 0) {
752				continue;
753			} else if (strcmp(p, "groupquota") == 0) {
754				continue;
755			} else if (strncmp(p, groupquotaeq,
756			    sizeof(groupquotaeq) - 1) == 0) {
757				continue;
758			} else if (*p == '-') {
759				append_arg(a, p);
760				p = strchr(p, '=');
761				if (p != NULL) {
762					*p = '\0';
763					append_arg(a, p + 1);
764				}
765			} else {
766				append_arg(a, strdup("-o"));
767				append_arg(a, p);
768			}
769		}
770}
771
772
773char *
774update_options(char *opts, char *fstab, int curflags)
775{
776	char *o, *p;
777	char *cur;
778	char *expopt, *newopt, *tmpopt;
779
780	if (opts == NULL)
781		return (strdup(""));
782
783	/* remove meta options from list */
784	remopt(fstab, MOUNT_META_OPTION_FSTAB);
785	remopt(fstab, MOUNT_META_OPTION_CURRENT);
786	cur = flags2opts(curflags);
787
788	/*
789	 * Expand all meta-options passed to us first.
790	 */
791	expopt = NULL;
792	for (p = opts; (o = strsep(&p, ",")) != NULL;) {
793		if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
794			expopt = catopt(expopt, fstab);
795		else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
796			expopt = catopt(expopt, cur);
797		else
798			expopt = catopt(expopt, o);
799	}
800	free(cur);
801	free(opts);
802
803	/*
804	 * Remove previous contradictory arguments. Given option "foo" we
805	 * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
806	 * and "foo" - so we can deal with possible options like "notice".
807	 */
808	newopt = NULL;
809	for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
810		if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
811			errx(1, "malloc failed");
812
813		strcpy(tmpopt, "no");
814		strcat(tmpopt, o);
815		remopt(newopt, tmpopt);
816		free(tmpopt);
817
818		if (strncmp("no", o, 2) == 0)
819			remopt(newopt, o+2);
820
821		newopt = catopt(newopt, o);
822	}
823	free(expopt);
824
825	return (newopt);
826}
827
828void
829remopt(char *string, const char *opt)
830{
831	char *o, *p, *r;
832
833	if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
834		return;
835
836	r = string;
837
838	for (p = string; (o = strsep(&p, ",")) != NULL;) {
839		if (strcmp(opt, o) != 0) {
840			if (*r == ',' && *o != '\0')
841				r++;
842			while ((*r++ = *o++) != '\0')
843			    ;
844			*--r = ',';
845		}
846	}
847	*r = '\0';
848}
849
850void
851usage(void)
852{
853
854	(void)fprintf(stderr, "%s\n%s\n%s\n",
855"usage: mount [-adflpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
856"       mount [-dfpruvw] special | node",
857"       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
858	exit(1);
859}
860
861void
862putfsent(struct statfs *ent)
863{
864	struct fstab *fst;
865	char *opts, *rw;
866	int l;
867
868	opts = NULL;
869	/* flags2opts() doesn't return the "rw" option. */
870	if ((ent->f_flags & MNT_RDONLY) != 0)
871		rw = NULL;
872	else
873		rw = catopt(NULL, "rw");
874
875	opts = flags2opts(ent->f_flags);
876	opts = catopt(rw, opts);
877
878	if (strncmp(ent->f_mntfromname, "<below>", 7) == 0 ||
879	    strncmp(ent->f_mntfromname, "<above>", 7) == 0) {
880		strcpy(ent->f_mntfromname, (strnstr(ent->f_mntfromname, ":", 8)
881		    +1));
882	}
883
884	l = strlen(ent->f_mntfromname);
885	printf("%s%s%s%s", ent->f_mntfromname,
886	    l < 8 ? "\t" : "",
887	    l < 16 ? "\t" : "",
888	    l < 24 ? "\t" : " ");
889	l = strlen(ent->f_mntonname);
890	printf("%s%s%s%s", ent->f_mntonname,
891	    l < 8 ? "\t" : "",
892	    l < 16 ? "\t" : "",
893	    l < 24 ? "\t" : " ");
894	printf("%s\t", ent->f_fstypename);
895	l = strlen(opts);
896	printf("%s%s", opts,
897	    l < 8 ? "\t" : " ");
898	free(opts);
899
900	if ((fst = getfsspec(ent->f_mntfromname)))
901		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
902	else if ((fst = getfsfile(ent->f_mntonname)))
903		printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
904	else if (strcmp(ent->f_fstypename, "ufs") == 0) {
905		if (strcmp(ent->f_mntonname, "/") == 0)
906			printf("\t1 1\n");
907		else
908			printf("\t2 2\n");
909	} else
910		printf("\t0 0\n");
911}
912
913
914char *
915flags2opts(int flags)
916{
917	char *res;
918
919	res = NULL;
920
921	if (flags & MNT_RDONLY)		res = catopt(res, "ro");
922	if (flags & MNT_SYNCHRONOUS)	res = catopt(res, "sync");
923	if (flags & MNT_NOEXEC)		res = catopt(res, "noexec");
924	if (flags & MNT_NOSUID)		res = catopt(res, "nosuid");
925	if (flags & MNT_UNION)		res = catopt(res, "union");
926	if (flags & MNT_ASYNC)		res = catopt(res, "async");
927	if (flags & MNT_NOATIME)	res = catopt(res, "noatime");
928	if (flags & MNT_NOCLUSTERR)	res = catopt(res, "noclusterr");
929	if (flags & MNT_NOCLUSTERW)	res = catopt(res, "noclusterw");
930	if (flags & MNT_NOSYMFOLLOW)	res = catopt(res, "nosymfollow");
931	if (flags & MNT_SUIDDIR)	res = catopt(res, "suiddir");
932	if (flags & MNT_MULTILABEL)	res = catopt(res, "multilabel");
933	if (flags & MNT_ACLS)		res = catopt(res, "acls");
934	if (flags & MNT_NFS4ACLS)	res = catopt(res, "nfsv4acls");
935
936	return (res);
937}
938