xref: /csrg-svn/sbin/mount/mount.c (revision 66522)
121156Sdist /*
266459Sbostic  * Copyright (c) 1980, 1989, 1993, 1994
361503Sbostic  *	The Regents of the University of California.  All rights reserved.
439322Smckusick  *
542702Sbostic  * %sccs.include.redist.c%
621156Sdist  */
721156Sdist 
810811Ssam #ifndef lint
961503Sbostic static char copyright[] =
1066459Sbostic "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
1161503Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1239322Smckusick #endif /* not lint */
131057Sbill 
1421156Sdist #ifndef lint
15*66522Spendry static char sccsid[] = "@(#)mount.c	8.17 (Berkeley) 03/31/94";
1639322Smckusick #endif /* not lint */
1721156Sdist 
1866138Sbostic #include <sys/param.h>
1966459Sbostic #include <sys/mount.h>
2066138Sbostic #include <sys/wait.h>
2166131Spendry 
2266138Sbostic #include <err.h>
2366138Sbostic #include <errno.h>
2466138Sbostic #include <fstab.h>
2566138Sbostic #include <signal.h>
2666131Spendry #include <stdio.h>
2766131Spendry #include <stdlib.h>
2866138Sbostic #include <string.h>
2966131Spendry #include <unistd.h>
3066138Sbostic 
3145524Sbostic #include "pathnames.h"
321057Sbill 
3366459Sbostic int debug, verbose, skipvfs;
3440369Smckusick 
3566459Sbostic int	badvfsname __P((const char *, const char **));
3666459Sbostic int	badvfstype __P((int, const char **));
3766459Sbostic char   *catopt __P((char *, const char *));
3866138Sbostic struct statfs
3966459Sbostic        *getmntpt __P((const char *));
4066459Sbostic const char
4166459Sbostic       **makevfslist __P((char *));
4266459Sbostic void	mangle __P((char *, int *, const char **));
4366459Sbostic int	mountfs __P((const char *, const char *, const char *,
4466459Sbostic 			int, const char *, const char *));
4566459Sbostic void	prmount __P((const char *, const char *, int));
4666459Sbostic void	usage __P((void));
4766138Sbostic 
4866459Sbostic /* From mount_ufs.c. */
4966459Sbostic int	mount_ufs __P((int, char * const *));
5066459Sbostic 
5166459Sbostic /* Map from mount otions to printable formats. */
5266459Sbostic static struct opt {
5366459Sbostic 	int o_opt;
5466459Sbostic 	const char *o_name;
5566459Sbostic } optnames[] = {
5666459Sbostic 	{ MNT_ASYNC,		"asynchronous" },
5766459Sbostic 	{ MNT_EXPORTED,		"NFS exported" },
5866459Sbostic 	{ MNT_LOCAL,		"local" },
5966459Sbostic 	{ MNT_NODEV,		"nodev" },
6066459Sbostic 	{ MNT_NOEXEC,		"noexec" },
6166459Sbostic 	{ MNT_NOSUID,		"nosuid" },
6266459Sbostic 	{ MNT_QUOTA,		"with quotas" },
6366459Sbostic 	{ MNT_RDONLY,		"read-only" },
6466459Sbostic 	{ MNT_SYNCHRONOUS,	"synchronous" },
6566459Sbostic 	{ MNT_UNION,		"union" },
6666459Sbostic 	{ MNT_USER,		"user mount" },
6766459Sbostic 	{ NULL }
6866459Sbostic };
6966459Sbostic 
7066130Spendry int
7166130Spendry main(argc, argv)
725073Sroot 	int argc;
7366459Sbostic 	char * const argv[];
741057Sbill {
7566459Sbostic 	const char *mntonname, **vfslist, *vfstype;
7666130Spendry 	struct fstab *fs;
7766130Spendry 	struct statfs *mntbuf;
7866177Sbostic 	FILE *mountdfp;
7966177Sbostic 	pid_t pid;
8066459Sbostic 	int all, ch, i, init_flags, mntsize, rval;
8166459Sbostic 	char *options;
821057Sbill 
8366459Sbostic 	all = init_flags = 0;
8466459Sbostic 	options = NULL;
8566138Sbostic 	vfslist = NULL;
8666459Sbostic 	vfstype = "ufs";
8766138Sbostic 	while ((ch = getopt(argc, argv, "adfo:rwt:uv")) != EOF)
8866459Sbostic 		switch (ch) {
8935339Sbostic 		case 'a':
9035339Sbostic 			all = 1;
9135339Sbostic 			break;
9256841Smckusick 		case 'd':
9356841Smckusick 			debug = 1;
9456841Smckusick 			break;
9535339Sbostic 		case 'f':
9666459Sbostic 			init_flags |= MNT_FORCE;
9735339Sbostic 			break;
9866138Sbostic 		case 'o':
9966459Sbostic 			if (*optarg)
10066459Sbostic 				options = catopt(options, optarg);
10166138Sbostic 			break;
10235339Sbostic 		case 'r':
10366459Sbostic 			init_flags |= MNT_RDONLY;
10435339Sbostic 			break;
10566138Sbostic 		case 't':
10666459Sbostic 			if (vfslist != NULL)
10766459Sbostic 				errx(1, "only one -t option may be specified.");
10866138Sbostic 			vfslist = makevfslist(optarg);
10966459Sbostic 			vfstype = optarg;
11066138Sbostic 			break;
11139333Smckusick 		case 'u':
11266459Sbostic 			init_flags |= MNT_UPDATE;
11339333Smckusick 			break;
11435339Sbostic 		case 'v':
11535339Sbostic 			verbose = 1;
11635339Sbostic 			break;
11735339Sbostic 		case 'w':
11866459Sbostic 			init_flags &= ~MNT_RDONLY;
11935339Sbostic 			break;
12035339Sbostic 		case '?':
12135339Sbostic 		default:
12235339Sbostic 			usage();
12339131Smckusick 			/* NOTREACHED */
1245073Sroot 		}
12535339Sbostic 	argc -= optind;
12635339Sbostic 	argv += optind;
12735339Sbostic 
12866138Sbostic #define	BADTYPE(type)							\
12966138Sbostic 	(strcmp(type, FSTAB_RO) &&					\
13066138Sbostic 	    strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
13135339Sbostic 
13266177Sbostic 	rval = 0;
13366459Sbostic 	switch (argc) {
13466459Sbostic 	case 0:
13566459Sbostic 		if (all)
13666459Sbostic 			while ((fs = getfsent()) != NULL) {
13766459Sbostic 				if (BADTYPE(fs->fs_type))
13866459Sbostic 					continue;
13966459Sbostic 				if (badvfsname(fs->fs_vfstype, vfslist))
14066459Sbostic 					continue;
14166459Sbostic 				if (mountfs(fs->fs_vfstype, fs->fs_spec,
14266459Sbostic 				    fs->fs_file, init_flags, options,
14366459Sbostic 				    fs->fs_mntops))
14466459Sbostic 					rval = 1;
14566459Sbostic 			}
14666459Sbostic 		else {
14766459Sbostic 			if (verbose) {
14866459Sbostic 				usage();
14966459Sbostic 				/* NOTREACHED */
15066459Sbostic 			}
15166459Sbostic 
15266459Sbostic 			if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
15366459Sbostic 				err(1, "getmntinfo");
15466459Sbostic 			for (i = 0; i < mntsize; i++) {
15566459Sbostic 				if (badvfstype(mntbuf[i].f_type, vfslist))
15666459Sbostic 					continue;
15766459Sbostic 				prmount(mntbuf[i].f_mntfromname,
15866459Sbostic 				     mntbuf[i].f_mntonname, mntbuf[i].f_flags);
15966459Sbostic 			}
16035372Sbostic 		}
16135341Sbostic 		exit(rval);
16266459Sbostic 	case 1:
16366459Sbostic 		if (vfslist != NULL) {
16435339Sbostic 			usage();
16566459Sbostic 			/* NOTREACHED */
16640844Smckusick 		}
16712808Ssam 
16866459Sbostic 		if (init_flags & MNT_UPDATE) {
16966459Sbostic 			if ((mntbuf = getmntpt(*argv)) == NULL)
17066459Sbostic 				errx(1,
17166459Sbostic 				    "unknown special file or file system %s.",
17266459Sbostic 				    *argv);
17366459Sbostic 			if ((fs = getfsfile(mntbuf->f_mntonname)) == NULL)
17466459Sbostic 				errx(1, "can't find fstab entry for %s.",
17566459Sbostic 				    *argv);
17666459Sbostic 			mntonname = mntbuf->f_mntonname;
17766459Sbostic 		} else {
17866459Sbostic 			if ((fs = getfsfile(*argv)) == NULL &&
17966459Sbostic 			    (fs = getfsspec(*argv)) == NULL)
18066459Sbostic 				errx(1,
18166459Sbostic 				    "%s: unknown special file or file system.",
18266459Sbostic 				    *argv);
18366459Sbostic 			if (BADTYPE(fs->fs_type))
18466459Sbostic 				errx(1, "%s has unknown file system type.",
18566459Sbostic 				    *argv);
18666459Sbostic 			mntonname = fs->fs_file;
18753711Smckusick 		}
18866459Sbostic 		rval = mountfs(fs->fs_vfstype, fs->fs_spec,
18966459Sbostic 		    mntonname, init_flags, options, fs->fs_mntops);
19066459Sbostic 		break;
19166459Sbostic 	case 2:
19242857Smckusick 		/*
19366138Sbostic 		 * If -t flag has not been specified, and spec contains either
19466138Sbostic 		 * a ':' or a '@' then assume that an NFS filesystem is being
19566138Sbostic 		 * specified ala Sun.
19642857Smckusick 		 */
19766138Sbostic 		if (vfslist == NULL &&
19866459Sbostic 		    (strchr(argv[0], ':') != NULL ||
19966459Sbostic 		    strchr(argv[0], '@') != NULL))
20066459Sbostic 			vfstype = "nfs";
20166459Sbostic 		rval = mountfs(vfstype,
20266459Sbostic 		    argv[0], argv[1], init_flags, options, NULL);
20366459Sbostic 		break;
20466459Sbostic 	default:
20566459Sbostic 		usage();
20666459Sbostic 		/* NOTREACHED */
20712808Ssam 	}
20866459Sbostic 
20966174Spendry 	/*
21066459Sbostic 	 * If the mount was successfully, and done by root, tell mountd the
21166459Sbostic 	 * good news.  Pid checks are probably unnecessary, but don't hurt.
21266174Spendry 	 */
21366177Sbostic 	if (rval == 0 && getuid() == 0 &&
21466177Sbostic 	    (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
21566177Sbostic 		if (fscanf(mountdfp, "%ld", &pid) == 1 &&
21666459Sbostic 		     pid > 0 && kill(pid, SIGHUP))
21766177Sbostic 			err(1, "signal mountd");
21866177Sbostic 		(void)fclose(mountdfp);
21940496Smckusick 	}
22066138Sbostic 
22166177Sbostic 	exit(rval);
22212808Ssam }
22312808Ssam 
22466138Sbostic int
22566459Sbostic mountfs(vfstype, spec, name, flags, options, mntopts)
22666459Sbostic 	const char *vfstype, *spec, *name, *options, *mntopts;
22739333Smckusick 	int flags;
22812808Ssam {
22966459Sbostic 	/* List of directories containing mount_xxx subcommands. */
23066459Sbostic 	static const char *edirs[] = {
23166459Sbostic 		_PATH_SBIN,
23266459Sbostic 		_PATH_USRSBIN,
23366459Sbostic 		NULL
23466459Sbostic 	};
23566459Sbostic 	const char *argv[100], **edir;
23666459Sbostic 	struct statfs sf;
23742253Sbostic 	pid_t pid;
23866138Sbostic 	int argc, i, status;
23966459Sbostic 	char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
2401057Sbill 
24166459Sbostic 	if (realpath(name, mntpath) == NULL) {
24266133Spendry 		warn("%s", mntpath);
24366133Spendry 		return (1);
24466133Spendry 	}
24566133Spendry 
24666133Spendry 	name = mntpath;
24766133Spendry 
24866459Sbostic 	if (options == 0) {
24966459Sbostic 		if (!mntopts || !*mntopts)
25066459Sbostic 			options = "rw";
25166459Sbostic 		else
25266459Sbostic 			options = mntopts;
25366459Sbostic 		mntopts = "";
25466459Sbostic 
25566459Sbostic 	}
25666459Sbostic 	optbuf = catopt(strdup(mntopts), options);
25766459Sbostic 
25866131Spendry 	if (strcmp(name, "/") == 0)
25966131Spendry 		flags |= MNT_UPDATE;
26066459Sbostic 	if (flags & MNT_FORCE)
26166459Sbostic 		optbuf = catopt(optbuf, "force");
26266459Sbostic 	if (flags & MNT_RDONLY)
26366459Sbostic 		optbuf = catopt(optbuf, "ro");
26466459Sbostic 	/*
26566459Sbostic 	 * XXX
26666459Sbostic 	 * The mount_mfs (newfs) command uses -o to select the
26766459Sbostic 	 * optimisation mode.  We don't pass the default "-o rw"
26866459Sbostic 	 * for that reason.
26966459Sbostic 	 */
27066459Sbostic 	if (flags & MNT_UPDATE)
27166459Sbostic 		optbuf = catopt(optbuf, "update");
27266131Spendry 
27366459Sbostic 	argc = 0;
27466459Sbostic 	argv[argc++] = vfstype;
27566459Sbostic 	mangle(optbuf, &argc, argv);
27666459Sbostic 	argv[argc++] = spec;
27766459Sbostic 	argv[argc++] = name;
27866459Sbostic 	argv[argc] = NULL;
27966459Sbostic 
28066459Sbostic 	if (debug) {
28166459Sbostic 		(void)printf("exec: mount_%s", vfstype);
28266459Sbostic 		for (i = 1; i < argc; i++)
28366459Sbostic 			(void)printf(" %s", argv[i]);
28466459Sbostic 		(void)printf("\n");
28566459Sbostic 		return (0);
28666459Sbostic 	}
28766459Sbostic 
28866459Sbostic 	switch (pid = vfork()) {
28966459Sbostic 	case -1:				/* Error. */
29066459Sbostic 		warn("vfork");
29166459Sbostic 		free(optbuf);
29266459Sbostic 		return (1);
29366459Sbostic 	case 0:					/* Child. */
294*66522Spendry 		if (strcmp(vfstype, "ufs") == 0)
295*66522Spendry 			exit(mount_ufs(argc, (char * const *) argv));
296*66522Spendry 
29766459Sbostic 		/* Go find an executable. */
29866459Sbostic 		edir = edirs;
29966459Sbostic 		do {
30066459Sbostic 			(void)snprintf(execname,
30166459Sbostic 			    sizeof(execname), "%s/mount_%s", *edir, vfstype);
30266459Sbostic 			execv(execname, (char * const *)argv);
30366459Sbostic 			if (errno != ENOENT)
30466459Sbostic 				warn("exec %s for %s", execname, name);
30566459Sbostic 		} while (*++edir != NULL);
30666459Sbostic 
30766459Sbostic 		if (errno == ENOENT)
30866459Sbostic 			warn("exec %s for %s", execname, name);
30966459Sbostic 		exit(1);
31066459Sbostic 		/* NOTREACHED */
31166459Sbostic 	default:				/* Parent. */
31266459Sbostic 		free(optbuf);
31366459Sbostic 
31466459Sbostic 		if (waitpid(pid, &status, 0) < 0) {
31566459Sbostic 			warn("waitpid");
31666459Sbostic 			return (1);
31739322Smckusick 		}
31866459Sbostic 
31966459Sbostic 		if (WIFEXITED(status)) {
32066459Sbostic 			if (WEXITSTATUS(status) != 0)
32166459Sbostic 				return (WEXITSTATUS(status));
32266459Sbostic 		} else if (WIFSIGNALED(status)) {
32366459Sbostic 			warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
32466459Sbostic 			return (1);
32566459Sbostic 		}
32666459Sbostic 
32739316Smckusick 		if (verbose) {
32866459Sbostic 			if (statfs(name, &sf) < 0) {
32966459Sbostic 				warn("%s", name);
33039316Smckusick 				return (1);
33139131Smckusick 			}
33266459Sbostic 			prmount(sf.f_mntfromname, sf.f_mntonname, sf.f_flags);
33339316Smckusick 		}
33466459Sbostic 		break;
33539316Smckusick 	}
33635339Sbostic 
33766130Spendry 	return (0);
3381057Sbill }
33935339Sbostic 
34066138Sbostic void
34139316Smckusick prmount(spec, name, flags)
34266459Sbostic 	const char *spec, *name;
34366130Spendry 	int flags;
34435339Sbostic {
34566459Sbostic 	struct opt *o;
34666459Sbostic 	int f;
34738632Smckusick 
34842253Sbostic 	(void)printf("%s on %s", spec, name);
34935339Sbostic 
35066459Sbostic 	flags &= MNT_VISFLAGMASK;
35166459Sbostic 	for (f = 0, o = optnames; flags && o->o_opt; o++)
35266459Sbostic 		if (flags & o->o_opt) {
35366459Sbostic 			(void)printf("%s%s", !f++ ? " (" : ", ", o->o_name);
35466459Sbostic 			flags &= ~o->o_opt;
35539333Smckusick 		}
35666459Sbostic 	(void)printf(f ? ")\n" : "\n");
35739316Smckusick }
35839316Smckusick 
35966138Sbostic struct statfs *
36039465Smckusick getmntpt(name)
36166459Sbostic 	const char *name;
36239465Smckusick {
36339465Smckusick 	struct statfs *mntbuf;
36466459Sbostic 	int i, mntsize;
36539465Smckusick 
36640337Smckusick 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
36766459Sbostic 	for (i = 0; i < mntsize; i++)
36866459Sbostic 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
36966459Sbostic 		    strcmp(mntbuf[i].f_mntonname, name) == 0)
37039465Smckusick 			return (&mntbuf[i]);
37166138Sbostic 	return (NULL);
37239465Smckusick }
37339465Smckusick 
37466138Sbostic int
37566459Sbostic badvfsname(vfsname, vfslist)
37666459Sbostic 	const char *vfsname;
37766459Sbostic 	const char **vfslist;
37840051Smckusick {
37940051Smckusick 
38066138Sbostic 	if (vfslist == NULL)
38166130Spendry 		return (0);
38266138Sbostic 	while (*vfslist != NULL) {
38366459Sbostic 		if (strcmp(vfsname, *vfslist) == 0)
38466130Spendry 			return (skipvfs);
38566459Sbostic 		++vfslist;
38640051Smckusick 	}
38740051Smckusick 	return (!skipvfs);
38840051Smckusick }
38940051Smckusick 
39066138Sbostic int
39166459Sbostic badvfstype(vfstype, vfslist)
39266459Sbostic 	int vfstype;
39366459Sbostic 	const char **vfslist;
39440844Smckusick {
39566459Sbostic static const char *vfsnames[] = INITMOUNTNAMES;
39640844Smckusick 
39766459Sbostic 	if ((vfstype < 0) || (vfstype > MOUNT_MAXTYPE))
39866130Spendry 		return (0);
39966459Sbostic 
40066459Sbostic 	return (badvfsname(vfsnames[vfstype], vfslist));
40140844Smckusick }
40240844Smckusick 
40366459Sbostic const char **
40440051Smckusick makevfslist(fslist)
40540051Smckusick 	char *fslist;
40640051Smckusick {
40766459Sbostic 	const char **av;
40866138Sbostic 	int i;
40966459Sbostic 	char *nextcp;
41040051Smckusick 
41140051Smckusick 	if (fslist == NULL)
41240051Smckusick 		return (NULL);
41340051Smckusick 	if (fslist[0] == 'n' && fslist[1] == 'o') {
41440051Smckusick 		fslist += 2;
41540051Smckusick 		skipvfs = 1;
41640051Smckusick 	}
41740051Smckusick 	for (i = 0, nextcp = fslist; *nextcp; nextcp++)
41840051Smckusick 		if (*nextcp == ',')
41940051Smckusick 			i++;
42066459Sbostic 	if ((av = malloc((size_t)(i + 2) * sizeof(char *))) == NULL) {
42166459Sbostic 		warn(NULL);
42240051Smckusick 		return (NULL);
42366459Sbostic 	}
42440051Smckusick 	nextcp = fslist;
42540051Smckusick 	i = 0;
42640051Smckusick 	av[i++] = nextcp;
42766459Sbostic 	while ((nextcp = strchr(nextcp, ',')) != NULL) {
42840051Smckusick 		*nextcp++ = '\0';
42940051Smckusick 		av[i++] = nextcp;
43040051Smckusick 	}
43166138Sbostic 	av[i++] = NULL;
43240051Smckusick 	return (av);
43340051Smckusick }
43466138Sbostic 
43566459Sbostic char *
43666459Sbostic catopt(s0, s1)
43766459Sbostic 	char *s0;
43866459Sbostic 	const char *s1;
43966459Sbostic {
44066459Sbostic 	size_t i;
44166459Sbostic 	char *cp;
44266459Sbostic 
44366459Sbostic 	if (s0 && *s0) {
44466459Sbostic 		i = strlen(s0) + strlen(s1) + 1 + 1;
44566459Sbostic 		if ((cp = malloc(i)) == NULL)
44666459Sbostic 			err(1, NULL);
44766459Sbostic 		(void)snprintf(cp, i, "%s,%s", s0, s1);
44866459Sbostic 	} else
44966459Sbostic 		cp = strdup(s1);
45066459Sbostic 
45166459Sbostic 	if (s0)
45266459Sbostic 		free(s0);
45366459Sbostic 	return (cp);
45466459Sbostic }
45566459Sbostic 
45666138Sbostic void
45766459Sbostic mangle(options, argcp, argv)
45866459Sbostic 	char *options;
45966459Sbostic 	int *argcp;
46066459Sbostic 	const char **argv;
46166459Sbostic {
46266459Sbostic 	char *p, *s;
46366459Sbostic 	int argc;
46466459Sbostic 
46566459Sbostic 	argc = *argcp;
46666459Sbostic 	for (s = options; (p = strsep(&s, ",")) != NULL;)
46766459Sbostic 		if (*p != '\0')
46866459Sbostic 			if (*p == '-') {
46966459Sbostic 				argv[argc++] = p;
47066459Sbostic 				p = strchr(p, '=');
47166459Sbostic 				if (p) {
47266459Sbostic 					*p = '\0';
47366459Sbostic 					argv[argc++] = p+1;
47466459Sbostic 				}
47566459Sbostic 			} else if (strcmp(p, "rw") != 0) {
47666459Sbostic 				argv[argc++] = "-o";
47766459Sbostic 				argv[argc++] = p;
47866459Sbostic 			}
47966459Sbostic 
48066459Sbostic 	*argcp = argc;
48166459Sbostic }
48266459Sbostic 
48366459Sbostic void
48466138Sbostic usage()
48566138Sbostic {
48666138Sbostic 
48766138Sbostic 	(void)fprintf(stderr,
48866459Sbostic 		"usage: mount %s %s\n       mount %s\n       mount %s\n",
48966459Sbostic 		"[-dfruvw] [-o options] [-t ufs | external_type]",
49066459Sbostic 			"special node",
49166459Sbostic 		"[-adfruvw] [-t ufs | external_type]",
49266459Sbostic 		"[-dfruvw] special | node");
49366138Sbostic 	exit(1);
49466138Sbostic }
495