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*68907Smckusick static char sccsid[] = "@(#)mount.c 8.23 (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 { 76*68907Smckusick const char **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++) { 15368897Smckusick 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); 168*68907Smckusick rval = mountfs(mntbuf->f_fstypename, 169*68907Smckusick mntbuf->f_mntfromname, mntbuf->f_mntonname, 170*68907Smckusick init_flags, options, 0); 171*68907Smckusick break; 17253711Smckusick } 173*68907Smckusick if ((fs = getfsfile(*argv)) == NULL && 174*68907Smckusick (fs = getfsspec(*argv)) == NULL) 175*68907Smckusick errx(1, "%s: unknown special file or file system.", 176*68907Smckusick *argv); 177*68907Smckusick if (BADTYPE(fs->fs_type)) 178*68907Smckusick errx(1, "%s has unknown file system type.", 179*68907Smckusick *argv); 180*68907Smckusick rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file, 181*68907Smckusick init_flags, options, fs->fs_mntops); 18266459Sbostic break; 18366459Sbostic case 2: 18442857Smckusick /* 18566138Sbostic * If -t flag has not been specified, and spec contains either 18666138Sbostic * a ':' or a '@' then assume that an NFS filesystem is being 18766138Sbostic * specified ala Sun. 18842857Smckusick */ 18966628Sbostic if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL) 19066459Sbostic vfstype = "nfs"; 19166459Sbostic rval = mountfs(vfstype, 19266459Sbostic argv[0], argv[1], init_flags, options, NULL); 19366459Sbostic break; 19466459Sbostic default: 19566459Sbostic usage(); 19666459Sbostic /* NOTREACHED */ 19712808Ssam } 19866459Sbostic 19966174Spendry /* 20066459Sbostic * If the mount was successfully, and done by root, tell mountd the 20166459Sbostic * good news. Pid checks are probably unnecessary, but don't hurt. 20266174Spendry */ 20366177Sbostic if (rval == 0 && getuid() == 0 && 20466177Sbostic (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) { 20566177Sbostic if (fscanf(mountdfp, "%ld", &pid) == 1 && 20667013Smckusick pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH) 20766177Sbostic err(1, "signal mountd"); 20866177Sbostic (void)fclose(mountdfp); 20940496Smckusick } 21066138Sbostic 21166177Sbostic exit(rval); 21212808Ssam } 21312808Ssam 21466138Sbostic int 21567412Smckusick hasopt(mntopts, option) 21667412Smckusick const char *mntopts, *option; 21767412Smckusick { 21867412Smckusick int negative, found; 21967412Smckusick char *opt, *optbuf; 22067412Smckusick 22167412Smckusick if (option[0] == 'n' && option[1] == 'o') { 22267412Smckusick negative = 1; 22367412Smckusick option += 2; 22467412Smckusick } else 22567412Smckusick negative = 0; 22667412Smckusick optbuf = strdup(mntopts); 22767412Smckusick found = 0; 22867412Smckusick for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 22967412Smckusick if (opt[0] == 'n' && opt[1] == 'o') { 23067412Smckusick if (!strcasecmp(opt + 2, option)) 23167412Smckusick found = negative; 23267412Smckusick } else if (!strcasecmp(opt, option)) 23367412Smckusick found = !negative; 23467412Smckusick } 23567412Smckusick free(optbuf); 23667412Smckusick return (found); 23767412Smckusick } 23867412Smckusick 23967412Smckusick int 24066459Sbostic mountfs(vfstype, spec, name, flags, options, mntopts) 24166459Sbostic const char *vfstype, *spec, *name, *options, *mntopts; 24239333Smckusick int flags; 24312808Ssam { 24466459Sbostic /* List of directories containing mount_xxx subcommands. */ 24566459Sbostic static const char *edirs[] = { 24666459Sbostic _PATH_SBIN, 24766459Sbostic _PATH_USRSBIN, 24866459Sbostic NULL 24966459Sbostic }; 25066459Sbostic const char *argv[100], **edir; 25166459Sbostic struct statfs sf; 25242253Sbostic pid_t pid; 25366138Sbostic int argc, i, status; 25466459Sbostic char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN]; 2551057Sbill 25666459Sbostic if (realpath(name, mntpath) == NULL) { 25767412Smckusick warn("realpath %s", mntpath); 25866133Spendry return (1); 25966133Spendry } 26066133Spendry 26166133Spendry name = mntpath; 26266133Spendry 26367412Smckusick if (mntopts == NULL) 26467412Smckusick mntopts = ""; 26566628Sbostic if (options == NULL) { 26667412Smckusick if (*mntopts == '\0') { 26766459Sbostic options = "rw"; 26867412Smckusick } else { 26966459Sbostic options = mntopts; 27067412Smckusick mntopts = ""; 27167412Smckusick } 27266459Sbostic } 27366459Sbostic optbuf = catopt(strdup(mntopts), options); 27466459Sbostic 27566131Spendry if (strcmp(name, "/") == 0) 27666131Spendry flags |= MNT_UPDATE; 27766459Sbostic if (flags & MNT_FORCE) 27866459Sbostic optbuf = catopt(optbuf, "force"); 27966459Sbostic if (flags & MNT_RDONLY) 28066459Sbostic optbuf = catopt(optbuf, "ro"); 28166459Sbostic /* 28266459Sbostic * XXX 28366459Sbostic * The mount_mfs (newfs) command uses -o to select the 28466459Sbostic * optimisation mode. We don't pass the default "-o rw" 28566459Sbostic * for that reason. 28666459Sbostic */ 28766459Sbostic if (flags & MNT_UPDATE) 28866459Sbostic optbuf = catopt(optbuf, "update"); 28966131Spendry 29066459Sbostic argc = 0; 29166459Sbostic argv[argc++] = vfstype; 29266459Sbostic mangle(optbuf, &argc, argv); 29366459Sbostic argv[argc++] = spec; 29466459Sbostic argv[argc++] = name; 29566459Sbostic argv[argc] = NULL; 29666459Sbostic 29766459Sbostic if (debug) { 29866459Sbostic (void)printf("exec: mount_%s", vfstype); 29966459Sbostic for (i = 1; i < argc; i++) 30066459Sbostic (void)printf(" %s", argv[i]); 30166459Sbostic (void)printf("\n"); 30266459Sbostic return (0); 30366459Sbostic } 30466459Sbostic 30566459Sbostic switch (pid = vfork()) { 30666459Sbostic case -1: /* Error. */ 30766459Sbostic warn("vfork"); 30866459Sbostic free(optbuf); 30966459Sbostic return (1); 31066459Sbostic case 0: /* Child. */ 31166522Spendry if (strcmp(vfstype, "ufs") == 0) 31266522Spendry exit(mount_ufs(argc, (char * const *) argv)); 31366522Spendry 31466459Sbostic /* Go find an executable. */ 31566459Sbostic edir = edirs; 31666459Sbostic do { 31766459Sbostic (void)snprintf(execname, 31866459Sbostic sizeof(execname), "%s/mount_%s", *edir, vfstype); 31966459Sbostic execv(execname, (char * const *)argv); 32066459Sbostic if (errno != ENOENT) 32166459Sbostic warn("exec %s for %s", execname, name); 32266459Sbostic } while (*++edir != NULL); 32366459Sbostic 32466459Sbostic if (errno == ENOENT) 32566459Sbostic warn("exec %s for %s", execname, name); 32666459Sbostic exit(1); 32766459Sbostic /* NOTREACHED */ 32866459Sbostic default: /* Parent. */ 32966459Sbostic free(optbuf); 33066459Sbostic 33166459Sbostic if (waitpid(pid, &status, 0) < 0) { 33266459Sbostic warn("waitpid"); 33366459Sbostic return (1); 33439322Smckusick } 33566459Sbostic 33666459Sbostic if (WIFEXITED(status)) { 33766459Sbostic if (WEXITSTATUS(status) != 0) 33866459Sbostic return (WEXITSTATUS(status)); 33966459Sbostic } else if (WIFSIGNALED(status)) { 34066459Sbostic warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 34166459Sbostic return (1); 34266459Sbostic } 34366459Sbostic 34439316Smckusick if (verbose) { 34566459Sbostic if (statfs(name, &sf) < 0) { 34667412Smckusick warn("statfs %s", name); 34739316Smckusick return (1); 34839131Smckusick } 34967531Smckusick prmount(&sf); 35039316Smckusick } 35166459Sbostic break; 35239316Smckusick } 35335339Sbostic 35466130Spendry return (0); 3551057Sbill } 35635339Sbostic 35766138Sbostic void 35867531Smckusick prmount(sfp) 35967531Smckusick struct statfs *sfp; 36067531Smckusick { 36166130Spendry int flags; 36266459Sbostic struct opt *o; 36367531Smckusick struct passwd *pw; 36466459Sbostic int f; 36538632Smckusick 36667531Smckusick (void)printf("%s on %s", sfp->f_mntfromname, sfp->f_mntonname); 36735339Sbostic 36867531Smckusick flags = sfp->f_flags & MNT_VISFLAGMASK; 36966459Sbostic for (f = 0, o = optnames; flags && o->o_opt; o++) 37066459Sbostic if (flags & o->o_opt) { 37166459Sbostic (void)printf("%s%s", !f++ ? " (" : ", ", o->o_name); 37266459Sbostic flags &= ~o->o_opt; 37339333Smckusick } 37467531Smckusick if (sfp->f_owner) { 37567531Smckusick (void)printf("%smounted by ", !f++ ? " (" : ", "); 37667531Smckusick if ((pw = getpwuid(sfp->f_owner)) != NULL) 37767531Smckusick (void)printf("%s", pw->pw_name); 37867531Smckusick else 37967531Smckusick (void)printf("%d", sfp->f_owner); 38067531Smckusick } 38166459Sbostic (void)printf(f ? ")\n" : "\n"); 38239316Smckusick } 38339316Smckusick 38466138Sbostic struct statfs * 38539465Smckusick getmntpt(name) 38666459Sbostic const char *name; 38739465Smckusick { 38839465Smckusick struct statfs *mntbuf; 38966459Sbostic int i, mntsize; 39039465Smckusick 39140337Smckusick mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 39266459Sbostic for (i = 0; i < mntsize; i++) 39366459Sbostic if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 39466459Sbostic strcmp(mntbuf[i].f_mntonname, name) == 0) 39539465Smckusick return (&mntbuf[i]); 39666138Sbostic return (NULL); 39739465Smckusick } 39839465Smckusick 39966138Sbostic int 40066459Sbostic badvfsname(vfsname, vfslist) 40166459Sbostic const char *vfsname; 40266459Sbostic const char **vfslist; 40340051Smckusick { 40440051Smckusick 40566138Sbostic if (vfslist == NULL) 40666130Spendry return (0); 40766138Sbostic while (*vfslist != NULL) { 40866459Sbostic if (strcmp(vfsname, *vfslist) == 0) 40966130Spendry return (skipvfs); 41066459Sbostic ++vfslist; 41140051Smckusick } 41240051Smckusick return (!skipvfs); 41340051Smckusick } 41440051Smckusick 41566459Sbostic const char ** 41640051Smckusick makevfslist(fslist) 41740051Smckusick char *fslist; 41840051Smckusick { 41966459Sbostic const char **av; 42066138Sbostic int i; 42166459Sbostic char *nextcp; 42240051Smckusick 42340051Smckusick if (fslist == NULL) 42440051Smckusick return (NULL); 42540051Smckusick if (fslist[0] == 'n' && fslist[1] == 'o') { 42640051Smckusick fslist += 2; 42740051Smckusick skipvfs = 1; 42840051Smckusick } 42940051Smckusick for (i = 0, nextcp = fslist; *nextcp; nextcp++) 43040051Smckusick if (*nextcp == ',') 43140051Smckusick i++; 43266459Sbostic if ((av = malloc((size_t)(i + 2) * sizeof(char *))) == NULL) { 43366459Sbostic warn(NULL); 43440051Smckusick return (NULL); 43566459Sbostic } 43640051Smckusick nextcp = fslist; 43740051Smckusick i = 0; 43840051Smckusick av[i++] = nextcp; 43966459Sbostic while ((nextcp = strchr(nextcp, ',')) != NULL) { 44040051Smckusick *nextcp++ = '\0'; 44140051Smckusick av[i++] = nextcp; 44240051Smckusick } 44366138Sbostic av[i++] = NULL; 44440051Smckusick return (av); 44540051Smckusick } 44666138Sbostic 44766459Sbostic char * 44866459Sbostic catopt(s0, s1) 44966459Sbostic char *s0; 45066459Sbostic const char *s1; 45166459Sbostic { 45266459Sbostic size_t i; 45366459Sbostic char *cp; 45466459Sbostic 45566459Sbostic if (s0 && *s0) { 45666459Sbostic i = strlen(s0) + strlen(s1) + 1 + 1; 45766459Sbostic if ((cp = malloc(i)) == NULL) 45866459Sbostic err(1, NULL); 45966459Sbostic (void)snprintf(cp, i, "%s,%s", s0, s1); 46066459Sbostic } else 46166459Sbostic cp = strdup(s1); 46266459Sbostic 46366459Sbostic if (s0) 46466459Sbostic free(s0); 46566459Sbostic return (cp); 46666459Sbostic } 46766459Sbostic 46866138Sbostic void 46966459Sbostic mangle(options, argcp, argv) 47066459Sbostic char *options; 47166459Sbostic int *argcp; 47266459Sbostic const char **argv; 47366459Sbostic { 47466459Sbostic char *p, *s; 47566459Sbostic int argc; 47666459Sbostic 47766459Sbostic argc = *argcp; 47866459Sbostic for (s = options; (p = strsep(&s, ",")) != NULL;) 47966459Sbostic if (*p != '\0') 48066459Sbostic if (*p == '-') { 48166459Sbostic argv[argc++] = p; 48266459Sbostic p = strchr(p, '='); 48366459Sbostic if (p) { 48466459Sbostic *p = '\0'; 48566459Sbostic argv[argc++] = p+1; 48666459Sbostic } 48766459Sbostic } else if (strcmp(p, "rw") != 0) { 48866459Sbostic argv[argc++] = "-o"; 48966459Sbostic argv[argc++] = p; 49066459Sbostic } 49166459Sbostic 49266459Sbostic *argcp = argc; 49366459Sbostic } 49466459Sbostic 49566459Sbostic void 49666138Sbostic usage() 49766138Sbostic { 49866138Sbostic 49966138Sbostic (void)fprintf(stderr, 50066459Sbostic "usage: mount %s %s\n mount %s\n mount %s\n", 50166459Sbostic "[-dfruvw] [-o options] [-t ufs | external_type]", 50266459Sbostic "special node", 50366459Sbostic "[-adfruvw] [-t ufs | external_type]", 50466459Sbostic "[-dfruvw] special | node"); 50566138Sbostic exit(1); 50666138Sbostic } 507