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*69298Smckusick static char sccsid[] = "@(#)mount.c 8.25 (Berkeley) 05/08/95";
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>
2567531Smckusick #include <pwd.h>
2666138Sbostic #include <signal.h>
2766131Spendry #include <stdio.h>
2866131Spendry #include <stdlib.h>
2966138Sbostic #include <string.h>
3066131Spendry #include <unistd.h>
3166138Sbostic
3245524Sbostic #include "pathnames.h"
331057Sbill
34*69298Smckusick int debug, verbose;
3540369Smckusick
36*69298Smckusick int checkvfsname __P((const char *, const char **));
3766459Sbostic char *catopt __P((char *, const char *));
3866138Sbostic struct statfs
3966459Sbostic *getmntpt __P((const char *));
4067412Smckusick int hasopt __P((const char *, const char *));
4166459Sbostic const char
4266459Sbostic **makevfslist __P((char *));
4366459Sbostic void mangle __P((char *, int *, const char **));
4466459Sbostic int mountfs __P((const char *, const char *, const char *,
4566459Sbostic int, const char *, const char *));
4667531Smckusick void prmount __P((struct statfs *));
4766459Sbostic void usage __P((void));
4866138Sbostic
4966459Sbostic /* From mount_ufs.c. */
5066459Sbostic int mount_ufs __P((int, char * const *));
5166459Sbostic
5266459Sbostic /* Map from mount otions to printable formats. */
5366459Sbostic static struct opt {
5466459Sbostic int o_opt;
5566459Sbostic const char *o_name;
5666459Sbostic } optnames[] = {
5766459Sbostic { MNT_ASYNC, "asynchronous" },
5866459Sbostic { MNT_EXPORTED, "NFS exported" },
5966459Sbostic { MNT_LOCAL, "local" },
6066459Sbostic { MNT_NODEV, "nodev" },
6166459Sbostic { MNT_NOEXEC, "noexec" },
6266459Sbostic { MNT_NOSUID, "nosuid" },
6366459Sbostic { MNT_QUOTA, "with quotas" },
6466459Sbostic { MNT_RDONLY, "read-only" },
6566459Sbostic { MNT_SYNCHRONOUS, "synchronous" },
6666459Sbostic { MNT_UNION, "union" },
6766459Sbostic { NULL }
6866459Sbostic };
6966459Sbostic
7066130Spendry int
main(argc,argv)7166130Spendry main(argc, argv)
725073Sroot int argc;
7366459Sbostic char * const argv[];
741057Sbill {
7568965Smckusick const char *mntfromname, **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;
139*69298Smckusick if (checkvfsname(fs->fs_vfstype, vfslist))
14066459Sbostic continue;
14167412Smckusick if (hasopt(fs->fs_mntops, "noauto"))
14267412Smckusick continue;
14366459Sbostic if (mountfs(fs->fs_vfstype, fs->fs_spec,
14466459Sbostic fs->fs_file, init_flags, options,
14566459Sbostic fs->fs_mntops))
14666459Sbostic rval = 1;
14766459Sbostic }
14866459Sbostic else {
14966459Sbostic if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
15066459Sbostic err(1, "getmntinfo");
15166459Sbostic for (i = 0; i < mntsize; i++) {
152*69298Smckusick if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
15366459Sbostic continue;
15467531Smckusick prmount(&mntbuf[i]);
15566459Sbostic }
15635372Sbostic }
15735341Sbostic exit(rval);
15866459Sbostic case 1:
15966628Sbostic if (vfslist != NULL)
16035339Sbostic usage();
16112808Ssam
16266459Sbostic if (init_flags & MNT_UPDATE) {
16366459Sbostic if ((mntbuf = getmntpt(*argv)) == NULL)
16466459Sbostic errx(1,
16566459Sbostic "unknown special file or file system %s.",
16666459Sbostic *argv);
16768965Smckusick if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL)
16868965Smckusick mntfromname = fs->fs_spec;
16968965Smckusick else
17068965Smckusick mntfromname = mntbuf->f_mntfromname;
17168965Smckusick rval = mountfs(mntbuf->f_fstypename, mntfromname,
17268965Smckusick mntbuf->f_mntonname, init_flags, options, 0);
17368907Smckusick break;
17453711Smckusick }
17568907Smckusick if ((fs = getfsfile(*argv)) == NULL &&
17668907Smckusick (fs = getfsspec(*argv)) == NULL)
17768907Smckusick errx(1, "%s: unknown special file or file system.",
17868907Smckusick *argv);
17968907Smckusick if (BADTYPE(fs->fs_type))
18068907Smckusick errx(1, "%s has unknown file system type.",
18168907Smckusick *argv);
18268907Smckusick rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
18368907Smckusick init_flags, options, fs->fs_mntops);
18466459Sbostic break;
18566459Sbostic case 2:
18642857Smckusick /*
18766138Sbostic * If -t flag has not been specified, and spec contains either
18866138Sbostic * a ':' or a '@' then assume that an NFS filesystem is being
18966138Sbostic * specified ala Sun.
19042857Smckusick */
19166628Sbostic if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL)
19266459Sbostic vfstype = "nfs";
19366459Sbostic rval = mountfs(vfstype,
19466459Sbostic argv[0], argv[1], init_flags, options, NULL);
19566459Sbostic break;
19666459Sbostic default:
19766459Sbostic usage();
19866459Sbostic /* NOTREACHED */
19912808Ssam }
20066459Sbostic
20166174Spendry /*
20266459Sbostic * If the mount was successfully, and done by root, tell mountd the
20366459Sbostic * good news. Pid checks are probably unnecessary, but don't hurt.
20466174Spendry */
20566177Sbostic if (rval == 0 && getuid() == 0 &&
20666177Sbostic (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
20766177Sbostic if (fscanf(mountdfp, "%ld", &pid) == 1 &&
20867013Smckusick pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
20966177Sbostic err(1, "signal mountd");
21066177Sbostic (void)fclose(mountdfp);
21140496Smckusick }
21266138Sbostic
21366177Sbostic exit(rval);
21412808Ssam }
21512808Ssam
21666138Sbostic int
hasopt(mntopts,option)21767412Smckusick hasopt(mntopts, option)
21867412Smckusick const char *mntopts, *option;
21967412Smckusick {
22067412Smckusick int negative, found;
22167412Smckusick char *opt, *optbuf;
22267412Smckusick
22367412Smckusick if (option[0] == 'n' && option[1] == 'o') {
22467412Smckusick negative = 1;
22567412Smckusick option += 2;
22667412Smckusick } else
22767412Smckusick negative = 0;
22867412Smckusick optbuf = strdup(mntopts);
22967412Smckusick found = 0;
23067412Smckusick for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
23167412Smckusick if (opt[0] == 'n' && opt[1] == 'o') {
23267412Smckusick if (!strcasecmp(opt + 2, option))
23367412Smckusick found = negative;
23467412Smckusick } else if (!strcasecmp(opt, option))
23567412Smckusick found = !negative;
23667412Smckusick }
23767412Smckusick free(optbuf);
23867412Smckusick return (found);
23967412Smckusick }
24067412Smckusick
24167412Smckusick int
mountfs(vfstype,spec,name,flags,options,mntopts)24266459Sbostic mountfs(vfstype, spec, name, flags, options, mntopts)
24366459Sbostic const char *vfstype, *spec, *name, *options, *mntopts;
24439333Smckusick int flags;
24512808Ssam {
24666459Sbostic /* List of directories containing mount_xxx subcommands. */
24766459Sbostic static const char *edirs[] = {
24866459Sbostic _PATH_SBIN,
24966459Sbostic _PATH_USRSBIN,
25066459Sbostic NULL
25166459Sbostic };
25266459Sbostic const char *argv[100], **edir;
25366459Sbostic struct statfs sf;
25442253Sbostic pid_t pid;
25566138Sbostic int argc, i, status;
25666459Sbostic char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN];
2571057Sbill
25866459Sbostic if (realpath(name, mntpath) == NULL) {
25967412Smckusick warn("realpath %s", mntpath);
26066133Spendry return (1);
26166133Spendry }
26266133Spendry
26366133Spendry name = mntpath;
26466133Spendry
26567412Smckusick if (mntopts == NULL)
26667412Smckusick mntopts = "";
26766628Sbostic if (options == NULL) {
26867412Smckusick if (*mntopts == '\0') {
26966459Sbostic options = "rw";
27067412Smckusick } else {
27166459Sbostic options = mntopts;
27267412Smckusick mntopts = "";
27367412Smckusick }
27466459Sbostic }
27566459Sbostic optbuf = catopt(strdup(mntopts), options);
27666459Sbostic
27766131Spendry if (strcmp(name, "/") == 0)
27866131Spendry flags |= MNT_UPDATE;
27966459Sbostic if (flags & MNT_FORCE)
28066459Sbostic optbuf = catopt(optbuf, "force");
28166459Sbostic if (flags & MNT_RDONLY)
28266459Sbostic optbuf = catopt(optbuf, "ro");
28366459Sbostic /*
28466459Sbostic * XXX
28566459Sbostic * The mount_mfs (newfs) command uses -o to select the
28666459Sbostic * optimisation mode. We don't pass the default "-o rw"
28766459Sbostic * for that reason.
28866459Sbostic */
28966459Sbostic if (flags & MNT_UPDATE)
29066459Sbostic optbuf = catopt(optbuf, "update");
29166131Spendry
29266459Sbostic argc = 0;
29366459Sbostic argv[argc++] = vfstype;
29466459Sbostic mangle(optbuf, &argc, argv);
29566459Sbostic argv[argc++] = spec;
29666459Sbostic argv[argc++] = name;
29766459Sbostic argv[argc] = NULL;
29866459Sbostic
29966459Sbostic if (debug) {
30066459Sbostic (void)printf("exec: mount_%s", vfstype);
30166459Sbostic for (i = 1; i < argc; i++)
30266459Sbostic (void)printf(" %s", argv[i]);
30366459Sbostic (void)printf("\n");
30466459Sbostic return (0);
30566459Sbostic }
30666459Sbostic
30766459Sbostic switch (pid = vfork()) {
30866459Sbostic case -1: /* Error. */
30966459Sbostic warn("vfork");
31066459Sbostic free(optbuf);
31166459Sbostic return (1);
31266459Sbostic case 0: /* Child. */
31366522Spendry if (strcmp(vfstype, "ufs") == 0)
31466522Spendry exit(mount_ufs(argc, (char * const *) argv));
31566522Spendry
31666459Sbostic /* Go find an executable. */
31766459Sbostic edir = edirs;
31866459Sbostic do {
31966459Sbostic (void)snprintf(execname,
32066459Sbostic sizeof(execname), "%s/mount_%s", *edir, vfstype);
32166459Sbostic execv(execname, (char * const *)argv);
32266459Sbostic if (errno != ENOENT)
32366459Sbostic warn("exec %s for %s", execname, name);
32466459Sbostic } while (*++edir != NULL);
32566459Sbostic
32666459Sbostic if (errno == ENOENT)
32766459Sbostic warn("exec %s for %s", execname, name);
32866459Sbostic exit(1);
32966459Sbostic /* NOTREACHED */
33066459Sbostic default: /* Parent. */
33166459Sbostic free(optbuf);
33266459Sbostic
33366459Sbostic if (waitpid(pid, &status, 0) < 0) {
33466459Sbostic warn("waitpid");
33566459Sbostic return (1);
33639322Smckusick }
33766459Sbostic
33866459Sbostic if (WIFEXITED(status)) {
33966459Sbostic if (WEXITSTATUS(status) != 0)
34066459Sbostic return (WEXITSTATUS(status));
34166459Sbostic } else if (WIFSIGNALED(status)) {
34266459Sbostic warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
34366459Sbostic return (1);
34466459Sbostic }
34566459Sbostic
34639316Smckusick if (verbose) {
34766459Sbostic if (statfs(name, &sf) < 0) {
34867412Smckusick warn("statfs %s", name);
34939316Smckusick return (1);
35039131Smckusick }
35167531Smckusick prmount(&sf);
35239316Smckusick }
35366459Sbostic break;
35439316Smckusick }
35535339Sbostic
35666130Spendry return (0);
3571057Sbill }
35835339Sbostic
35966138Sbostic void
prmount(sfp)36067531Smckusick prmount(sfp)
36167531Smckusick struct statfs *sfp;
36267531Smckusick {
36366130Spendry int flags;
36466459Sbostic struct opt *o;
36567531Smckusick struct passwd *pw;
36666459Sbostic int f;
36738632Smckusick
36867531Smckusick (void)printf("%s on %s", sfp->f_mntfromname, sfp->f_mntonname);
36935339Sbostic
37067531Smckusick flags = sfp->f_flags & MNT_VISFLAGMASK;
37166459Sbostic for (f = 0, o = optnames; flags && o->o_opt; o++)
37266459Sbostic if (flags & o->o_opt) {
37366459Sbostic (void)printf("%s%s", !f++ ? " (" : ", ", o->o_name);
37466459Sbostic flags &= ~o->o_opt;
37539333Smckusick }
37667531Smckusick if (sfp->f_owner) {
37767531Smckusick (void)printf("%smounted by ", !f++ ? " (" : ", ");
37867531Smckusick if ((pw = getpwuid(sfp->f_owner)) != NULL)
37967531Smckusick (void)printf("%s", pw->pw_name);
38067531Smckusick else
38167531Smckusick (void)printf("%d", sfp->f_owner);
38267531Smckusick }
38366459Sbostic (void)printf(f ? ")\n" : "\n");
38439316Smckusick }
38539316Smckusick
38666138Sbostic struct statfs *
getmntpt(name)38739465Smckusick getmntpt(name)
38866459Sbostic const char *name;
38939465Smckusick {
39039465Smckusick struct statfs *mntbuf;
39166459Sbostic int i, mntsize;
39239465Smckusick
39340337Smckusick mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
39466459Sbostic for (i = 0; i < mntsize; i++)
39566459Sbostic if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
39666459Sbostic strcmp(mntbuf[i].f_mntonname, name) == 0)
39739465Smckusick return (&mntbuf[i]);
39866138Sbostic return (NULL);
39939465Smckusick }
40039465Smckusick
40166459Sbostic char *
catopt(s0,s1)40266459Sbostic catopt(s0, s1)
40366459Sbostic char *s0;
40466459Sbostic const char *s1;
40566459Sbostic {
40666459Sbostic size_t i;
40766459Sbostic char *cp;
40866459Sbostic
40966459Sbostic if (s0 && *s0) {
41066459Sbostic i = strlen(s0) + strlen(s1) + 1 + 1;
41166459Sbostic if ((cp = malloc(i)) == NULL)
41266459Sbostic err(1, NULL);
41366459Sbostic (void)snprintf(cp, i, "%s,%s", s0, s1);
41466459Sbostic } else
41566459Sbostic cp = strdup(s1);
41666459Sbostic
41766459Sbostic if (s0)
41866459Sbostic free(s0);
41966459Sbostic return (cp);
42066459Sbostic }
42166459Sbostic
42266138Sbostic void
mangle(options,argcp,argv)42366459Sbostic mangle(options, argcp, argv)
42466459Sbostic char *options;
42566459Sbostic int *argcp;
42666459Sbostic const char **argv;
42766459Sbostic {
42866459Sbostic char *p, *s;
42966459Sbostic int argc;
43066459Sbostic
43166459Sbostic argc = *argcp;
43266459Sbostic for (s = options; (p = strsep(&s, ",")) != NULL;)
43366459Sbostic if (*p != '\0')
43466459Sbostic if (*p == '-') {
43566459Sbostic argv[argc++] = p;
43666459Sbostic p = strchr(p, '=');
43766459Sbostic if (p) {
43866459Sbostic *p = '\0';
43966459Sbostic argv[argc++] = p+1;
44066459Sbostic }
44166459Sbostic } else if (strcmp(p, "rw") != 0) {
44266459Sbostic argv[argc++] = "-o";
44366459Sbostic argv[argc++] = p;
44466459Sbostic }
44566459Sbostic
44666459Sbostic *argcp = argc;
44766459Sbostic }
44866459Sbostic
44966459Sbostic void
usage()45066138Sbostic usage()
45166138Sbostic {
45266138Sbostic
45366138Sbostic (void)fprintf(stderr,
45466459Sbostic "usage: mount %s %s\n mount %s\n mount %s\n",
45566459Sbostic "[-dfruvw] [-o options] [-t ufs | external_type]",
45666459Sbostic "special node",
45766459Sbostic "[-adfruvw] [-t ufs | external_type]",
45866459Sbostic "[-dfruvw] special | node");
45966138Sbostic exit(1);
46066138Sbostic }
461