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*68897Smckusick static char sccsid[] = "@(#)mount.c 8.22 (Berkeley) 04/26/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 3466459Sbostic int debug, verbose, skipvfs; 3540369Smckusick 3666459Sbostic int badvfsname __P((const char *, const char **)); 3766459Sbostic int badvfstype __P((int, const char **)); 3866459Sbostic char *catopt __P((char *, const char *)); 3966138Sbostic struct statfs 4066459Sbostic *getmntpt __P((const char *)); 4167412Smckusick int hasopt __P((const char *, const char *)); 4266459Sbostic const char 4366459Sbostic **makevfslist __P((char *)); 4466459Sbostic void mangle __P((char *, int *, const char **)); 4566459Sbostic int mountfs __P((const char *, const char *, const char *, 4666459Sbostic int, const char *, const char *)); 4767531Smckusick void prmount __P((struct statfs *)); 4866459Sbostic void usage __P((void)); 4966138Sbostic 5066459Sbostic /* From mount_ufs.c. */ 5166459Sbostic int mount_ufs __P((int, char * const *)); 5266459Sbostic 5366459Sbostic /* Map from mount otions to printable formats. */ 5466459Sbostic static struct opt { 5566459Sbostic int o_opt; 5666459Sbostic const char *o_name; 5766459Sbostic } optnames[] = { 5866459Sbostic { MNT_ASYNC, "asynchronous" }, 5966459Sbostic { MNT_EXPORTED, "NFS exported" }, 6066459Sbostic { MNT_LOCAL, "local" }, 6166459Sbostic { MNT_NODEV, "nodev" }, 6266459Sbostic { MNT_NOEXEC, "noexec" }, 6366459Sbostic { MNT_NOSUID, "nosuid" }, 6466459Sbostic { MNT_QUOTA, "with quotas" }, 6566459Sbostic { MNT_RDONLY, "read-only" }, 6666459Sbostic { MNT_SYNCHRONOUS, "synchronous" }, 6766459Sbostic { MNT_UNION, "union" }, 6866459Sbostic { NULL } 6966459Sbostic }; 7066459Sbostic 7166130Spendry int 7266130Spendry main(argc, argv) 735073Sroot int argc; 7466459Sbostic char * const argv[]; 751057Sbill { 7666459Sbostic const char *mntonname, **vfslist, *vfstype; 7766130Spendry struct fstab *fs; 7866130Spendry struct statfs *mntbuf; 7966177Sbostic FILE *mountdfp; 8066177Sbostic pid_t pid; 8166459Sbostic int all, ch, i, init_flags, mntsize, rval; 8266459Sbostic char *options; 831057Sbill 8466459Sbostic all = init_flags = 0; 8566459Sbostic options = NULL; 8666138Sbostic vfslist = NULL; 8766459Sbostic vfstype = "ufs"; 8866138Sbostic while ((ch = getopt(argc, argv, "adfo:rwt:uv")) != EOF) 8966459Sbostic switch (ch) { 9035339Sbostic case 'a': 9135339Sbostic all = 1; 9235339Sbostic break; 9356841Smckusick case 'd': 9456841Smckusick debug = 1; 9556841Smckusick break; 9635339Sbostic case 'f': 9766459Sbostic init_flags |= MNT_FORCE; 9835339Sbostic break; 9966138Sbostic case 'o': 10066459Sbostic if (*optarg) 10166459Sbostic options = catopt(options, optarg); 10266138Sbostic break; 10335339Sbostic case 'r': 10466459Sbostic init_flags |= MNT_RDONLY; 10535339Sbostic break; 10666138Sbostic case 't': 10766459Sbostic if (vfslist != NULL) 10866459Sbostic errx(1, "only one -t option may be specified."); 10966138Sbostic vfslist = makevfslist(optarg); 11066459Sbostic vfstype = optarg; 11166138Sbostic break; 11239333Smckusick case 'u': 11366459Sbostic init_flags |= MNT_UPDATE; 11439333Smckusick break; 11535339Sbostic case 'v': 11635339Sbostic verbose = 1; 11735339Sbostic break; 11835339Sbostic case 'w': 11966459Sbostic init_flags &= ~MNT_RDONLY; 12035339Sbostic break; 12135339Sbostic case '?': 12235339Sbostic default: 12335339Sbostic usage(); 12439131Smckusick /* NOTREACHED */ 1255073Sroot } 12635339Sbostic argc -= optind; 12735339Sbostic argv += optind; 12835339Sbostic 12966138Sbostic #define BADTYPE(type) \ 13066138Sbostic (strcmp(type, FSTAB_RO) && \ 13166138Sbostic strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ)) 13235339Sbostic 13366177Sbostic rval = 0; 13466459Sbostic switch (argc) { 13566459Sbostic case 0: 13666459Sbostic if (all) 13766459Sbostic while ((fs = getfsent()) != NULL) { 13866459Sbostic if (BADTYPE(fs->fs_type)) 13966459Sbostic continue; 14066459Sbostic if (badvfsname(fs->fs_vfstype, vfslist)) 14166459Sbostic continue; 14267412Smckusick if (hasopt(fs->fs_mntops, "noauto")) 14367412Smckusick continue; 14466459Sbostic if (mountfs(fs->fs_vfstype, fs->fs_spec, 14566459Sbostic fs->fs_file, init_flags, options, 14666459Sbostic fs->fs_mntops)) 14766459Sbostic rval = 1; 14866459Sbostic } 14966459Sbostic else { 15066459Sbostic if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 15166459Sbostic err(1, "getmntinfo"); 15266459Sbostic for (i = 0; i < mntsize; i++) { 153*68897Smckusick if (badvfsname(mntbuf[i].f_fstypename, vfslist)) 15466459Sbostic continue; 15567531Smckusick prmount(&mntbuf[i]); 15666459Sbostic } 15735372Sbostic } 15835341Sbostic exit(rval); 15966459Sbostic case 1: 16066628Sbostic if (vfslist != NULL) 16135339Sbostic usage(); 16212808Ssam 16366459Sbostic if (init_flags & MNT_UPDATE) { 16466459Sbostic if ((mntbuf = getmntpt(*argv)) == NULL) 16566459Sbostic errx(1, 16666459Sbostic "unknown special file or file system %s.", 16766459Sbostic *argv); 16866459Sbostic if ((fs = getfsfile(mntbuf->f_mntonname)) == NULL) 16966459Sbostic errx(1, "can't find fstab entry for %s.", 17066459Sbostic *argv); 17166628Sbostic /* If it's an update, ignore the fstab file options. */ 17266628Sbostic fs->fs_mntops = NULL; 17366459Sbostic mntonname = mntbuf->f_mntonname; 17466459Sbostic } else { 17566459Sbostic if ((fs = getfsfile(*argv)) == NULL && 17666459Sbostic (fs = getfsspec(*argv)) == NULL) 17766459Sbostic errx(1, 17866459Sbostic "%s: unknown special file or file system.", 17966459Sbostic *argv); 18066459Sbostic if (BADTYPE(fs->fs_type)) 18166459Sbostic errx(1, "%s has unknown file system type.", 18266459Sbostic *argv); 18366459Sbostic mntonname = fs->fs_file; 18453711Smckusick } 18566459Sbostic rval = mountfs(fs->fs_vfstype, fs->fs_spec, 18666459Sbostic mntonname, init_flags, options, fs->fs_mntops); 18766459Sbostic break; 18866459Sbostic case 2: 18942857Smckusick /* 19066138Sbostic * If -t flag has not been specified, and spec contains either 19166138Sbostic * a ':' or a '@' then assume that an NFS filesystem is being 19266138Sbostic * specified ala Sun. 19342857Smckusick */ 19466628Sbostic if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL) 19566459Sbostic vfstype = "nfs"; 19666459Sbostic rval = mountfs(vfstype, 19766459Sbostic argv[0], argv[1], init_flags, options, NULL); 19866459Sbostic break; 19966459Sbostic default: 20066459Sbostic usage(); 20166459Sbostic /* NOTREACHED */ 20212808Ssam } 20366459Sbostic 20466174Spendry /* 20566459Sbostic * If the mount was successfully, and done by root, tell mountd the 20666459Sbostic * good news. Pid checks are probably unnecessary, but don't hurt. 20766174Spendry */ 20866177Sbostic if (rval == 0 && getuid() == 0 && 20966177Sbostic (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) { 21066177Sbostic if (fscanf(mountdfp, "%ld", &pid) == 1 && 21167013Smckusick pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH) 21266177Sbostic err(1, "signal mountd"); 21366177Sbostic (void)fclose(mountdfp); 21440496Smckusick } 21566138Sbostic 21666177Sbostic exit(rval); 21712808Ssam } 21812808Ssam 21966138Sbostic int 22067412Smckusick hasopt(mntopts, option) 22167412Smckusick const char *mntopts, *option; 22267412Smckusick { 22367412Smckusick int negative, found; 22467412Smckusick char *opt, *optbuf; 22567412Smckusick 22667412Smckusick if (option[0] == 'n' && option[1] == 'o') { 22767412Smckusick negative = 1; 22867412Smckusick option += 2; 22967412Smckusick } else 23067412Smckusick negative = 0; 23167412Smckusick optbuf = strdup(mntopts); 23267412Smckusick found = 0; 23367412Smckusick for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 23467412Smckusick if (opt[0] == 'n' && opt[1] == 'o') { 23567412Smckusick if (!strcasecmp(opt + 2, option)) 23667412Smckusick found = negative; 23767412Smckusick } else if (!strcasecmp(opt, option)) 23867412Smckusick found = !negative; 23967412Smckusick } 24067412Smckusick free(optbuf); 24167412Smckusick return (found); 24267412Smckusick } 24367412Smckusick 24467412Smckusick int 24566459Sbostic mountfs(vfstype, spec, name, flags, options, mntopts) 24666459Sbostic const char *vfstype, *spec, *name, *options, *mntopts; 24739333Smckusick int flags; 24812808Ssam { 24966459Sbostic /* List of directories containing mount_xxx subcommands. */ 25066459Sbostic static const char *edirs[] = { 25166459Sbostic _PATH_SBIN, 25266459Sbostic _PATH_USRSBIN, 25366459Sbostic NULL 25466459Sbostic }; 25566459Sbostic const char *argv[100], **edir; 25666459Sbostic struct statfs sf; 25742253Sbostic pid_t pid; 25866138Sbostic int argc, i, status; 25966459Sbostic char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN]; 2601057Sbill 26166459Sbostic if (realpath(name, mntpath) == NULL) { 26267412Smckusick warn("realpath %s", mntpath); 26366133Spendry return (1); 26466133Spendry } 26566133Spendry 26666133Spendry name = mntpath; 26766133Spendry 26867412Smckusick if (mntopts == NULL) 26967412Smckusick mntopts = ""; 27066628Sbostic if (options == NULL) { 27167412Smckusick if (*mntopts == '\0') { 27266459Sbostic options = "rw"; 27367412Smckusick } else { 27466459Sbostic options = mntopts; 27567412Smckusick mntopts = ""; 27667412Smckusick } 27766459Sbostic } 27866459Sbostic optbuf = catopt(strdup(mntopts), options); 27966459Sbostic 28066131Spendry if (strcmp(name, "/") == 0) 28166131Spendry flags |= MNT_UPDATE; 28266459Sbostic if (flags & MNT_FORCE) 28366459Sbostic optbuf = catopt(optbuf, "force"); 28466459Sbostic if (flags & MNT_RDONLY) 28566459Sbostic optbuf = catopt(optbuf, "ro"); 28666459Sbostic /* 28766459Sbostic * XXX 28866459Sbostic * The mount_mfs (newfs) command uses -o to select the 28966459Sbostic * optimisation mode. We don't pass the default "-o rw" 29066459Sbostic * for that reason. 29166459Sbostic */ 29266459Sbostic if (flags & MNT_UPDATE) 29366459Sbostic optbuf = catopt(optbuf, "update"); 29466131Spendry 29566459Sbostic argc = 0; 29666459Sbostic argv[argc++] = vfstype; 29766459Sbostic mangle(optbuf, &argc, argv); 29866459Sbostic argv[argc++] = spec; 29966459Sbostic argv[argc++] = name; 30066459Sbostic argv[argc] = NULL; 30166459Sbostic 30266459Sbostic if (debug) { 30366459Sbostic (void)printf("exec: mount_%s", vfstype); 30466459Sbostic for (i = 1; i < argc; i++) 30566459Sbostic (void)printf(" %s", argv[i]); 30666459Sbostic (void)printf("\n"); 30766459Sbostic return (0); 30866459Sbostic } 30966459Sbostic 31066459Sbostic switch (pid = vfork()) { 31166459Sbostic case -1: /* Error. */ 31266459Sbostic warn("vfork"); 31366459Sbostic free(optbuf); 31466459Sbostic return (1); 31566459Sbostic case 0: /* Child. */ 31666522Spendry if (strcmp(vfstype, "ufs") == 0) 31766522Spendry exit(mount_ufs(argc, (char * const *) argv)); 31866522Spendry 31966459Sbostic /* Go find an executable. */ 32066459Sbostic edir = edirs; 32166459Sbostic do { 32266459Sbostic (void)snprintf(execname, 32366459Sbostic sizeof(execname), "%s/mount_%s", *edir, vfstype); 32466459Sbostic execv(execname, (char * const *)argv); 32566459Sbostic if (errno != ENOENT) 32666459Sbostic warn("exec %s for %s", execname, name); 32766459Sbostic } while (*++edir != NULL); 32866459Sbostic 32966459Sbostic if (errno == ENOENT) 33066459Sbostic warn("exec %s for %s", execname, name); 33166459Sbostic exit(1); 33266459Sbostic /* NOTREACHED */ 33366459Sbostic default: /* Parent. */ 33466459Sbostic free(optbuf); 33566459Sbostic 33666459Sbostic if (waitpid(pid, &status, 0) < 0) { 33766459Sbostic warn("waitpid"); 33866459Sbostic return (1); 33939322Smckusick } 34066459Sbostic 34166459Sbostic if (WIFEXITED(status)) { 34266459Sbostic if (WEXITSTATUS(status) != 0) 34366459Sbostic return (WEXITSTATUS(status)); 34466459Sbostic } else if (WIFSIGNALED(status)) { 34566459Sbostic warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 34666459Sbostic return (1); 34766459Sbostic } 34866459Sbostic 34939316Smckusick if (verbose) { 35066459Sbostic if (statfs(name, &sf) < 0) { 35167412Smckusick warn("statfs %s", name); 35239316Smckusick return (1); 35339131Smckusick } 35467531Smckusick prmount(&sf); 35539316Smckusick } 35666459Sbostic break; 35739316Smckusick } 35835339Sbostic 35966130Spendry return (0); 3601057Sbill } 36135339Sbostic 36266138Sbostic void 36367531Smckusick prmount(sfp) 36467531Smckusick struct statfs *sfp; 36567531Smckusick { 36666130Spendry int flags; 36766459Sbostic struct opt *o; 36867531Smckusick struct passwd *pw; 36966459Sbostic int f; 37038632Smckusick 37167531Smckusick (void)printf("%s on %s", sfp->f_mntfromname, sfp->f_mntonname); 37235339Sbostic 37367531Smckusick flags = sfp->f_flags & MNT_VISFLAGMASK; 37466459Sbostic for (f = 0, o = optnames; flags && o->o_opt; o++) 37566459Sbostic if (flags & o->o_opt) { 37666459Sbostic (void)printf("%s%s", !f++ ? " (" : ", ", o->o_name); 37766459Sbostic flags &= ~o->o_opt; 37839333Smckusick } 37967531Smckusick if (sfp->f_owner) { 38067531Smckusick (void)printf("%smounted by ", !f++ ? " (" : ", "); 38167531Smckusick if ((pw = getpwuid(sfp->f_owner)) != NULL) 38267531Smckusick (void)printf("%s", pw->pw_name); 38367531Smckusick else 38467531Smckusick (void)printf("%d", sfp->f_owner); 38567531Smckusick } 38666459Sbostic (void)printf(f ? ")\n" : "\n"); 38739316Smckusick } 38839316Smckusick 38966138Sbostic struct statfs * 39039465Smckusick getmntpt(name) 39166459Sbostic const char *name; 39239465Smckusick { 39339465Smckusick struct statfs *mntbuf; 39466459Sbostic int i, mntsize; 39539465Smckusick 39640337Smckusick mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 39766459Sbostic for (i = 0; i < mntsize; i++) 39866459Sbostic if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 39966459Sbostic strcmp(mntbuf[i].f_mntonname, name) == 0) 40039465Smckusick return (&mntbuf[i]); 40166138Sbostic return (NULL); 40239465Smckusick } 40339465Smckusick 40466138Sbostic int 40566459Sbostic badvfsname(vfsname, vfslist) 40666459Sbostic const char *vfsname; 40766459Sbostic const char **vfslist; 40840051Smckusick { 40940051Smckusick 41066138Sbostic if (vfslist == NULL) 41166130Spendry return (0); 41266138Sbostic while (*vfslist != NULL) { 41366459Sbostic if (strcmp(vfsname, *vfslist) == 0) 41466130Spendry return (skipvfs); 41566459Sbostic ++vfslist; 41640051Smckusick } 41740051Smckusick return (!skipvfs); 41840051Smckusick } 41940051Smckusick 42066459Sbostic const char ** 42140051Smckusick makevfslist(fslist) 42240051Smckusick char *fslist; 42340051Smckusick { 42466459Sbostic const char **av; 42566138Sbostic int i; 42666459Sbostic char *nextcp; 42740051Smckusick 42840051Smckusick if (fslist == NULL) 42940051Smckusick return (NULL); 43040051Smckusick if (fslist[0] == 'n' && fslist[1] == 'o') { 43140051Smckusick fslist += 2; 43240051Smckusick skipvfs = 1; 43340051Smckusick } 43440051Smckusick for (i = 0, nextcp = fslist; *nextcp; nextcp++) 43540051Smckusick if (*nextcp == ',') 43640051Smckusick i++; 43766459Sbostic if ((av = malloc((size_t)(i + 2) * sizeof(char *))) == NULL) { 43866459Sbostic warn(NULL); 43940051Smckusick return (NULL); 44066459Sbostic } 44140051Smckusick nextcp = fslist; 44240051Smckusick i = 0; 44340051Smckusick av[i++] = nextcp; 44466459Sbostic while ((nextcp = strchr(nextcp, ',')) != NULL) { 44540051Smckusick *nextcp++ = '\0'; 44640051Smckusick av[i++] = nextcp; 44740051Smckusick } 44866138Sbostic av[i++] = NULL; 44940051Smckusick return (av); 45040051Smckusick } 45166138Sbostic 45266459Sbostic char * 45366459Sbostic catopt(s0, s1) 45466459Sbostic char *s0; 45566459Sbostic const char *s1; 45666459Sbostic { 45766459Sbostic size_t i; 45866459Sbostic char *cp; 45966459Sbostic 46066459Sbostic if (s0 && *s0) { 46166459Sbostic i = strlen(s0) + strlen(s1) + 1 + 1; 46266459Sbostic if ((cp = malloc(i)) == NULL) 46366459Sbostic err(1, NULL); 46466459Sbostic (void)snprintf(cp, i, "%s,%s", s0, s1); 46566459Sbostic } else 46666459Sbostic cp = strdup(s1); 46766459Sbostic 46866459Sbostic if (s0) 46966459Sbostic free(s0); 47066459Sbostic return (cp); 47166459Sbostic } 47266459Sbostic 47366138Sbostic void 47466459Sbostic mangle(options, argcp, argv) 47566459Sbostic char *options; 47666459Sbostic int *argcp; 47766459Sbostic const char **argv; 47866459Sbostic { 47966459Sbostic char *p, *s; 48066459Sbostic int argc; 48166459Sbostic 48266459Sbostic argc = *argcp; 48366459Sbostic for (s = options; (p = strsep(&s, ",")) != NULL;) 48466459Sbostic if (*p != '\0') 48566459Sbostic if (*p == '-') { 48666459Sbostic argv[argc++] = p; 48766459Sbostic p = strchr(p, '='); 48866459Sbostic if (p) { 48966459Sbostic *p = '\0'; 49066459Sbostic argv[argc++] = p+1; 49166459Sbostic } 49266459Sbostic } else if (strcmp(p, "rw") != 0) { 49366459Sbostic argv[argc++] = "-o"; 49466459Sbostic argv[argc++] = p; 49566459Sbostic } 49666459Sbostic 49766459Sbostic *argcp = argc; 49866459Sbostic } 49966459Sbostic 50066459Sbostic void 50166138Sbostic usage() 50266138Sbostic { 50366138Sbostic 50466138Sbostic (void)fprintf(stderr, 50566459Sbostic "usage: mount %s %s\n mount %s\n mount %s\n", 50666459Sbostic "[-dfruvw] [-o options] [-t ufs | external_type]", 50766459Sbostic "special node", 50866459Sbostic "[-adfruvw] [-t ufs | external_type]", 50966459Sbostic "[-dfruvw] special | node"); 51066138Sbostic exit(1); 51166138Sbostic } 512