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*67412Smckusick static char sccsid[] = "@(#)mount.c 8.20 (Berkeley) 06/16/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 *)); 40*67412Smckusick 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 *)); 4666459Sbostic void prmount __P((const char *, const char *, int)); 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 { MNT_USER, "user mount" }, 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; 142*67412Smckusick if (hasopt(fs->fs_mntops, "noauto")) 143*67412Smckusick 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++) { 15366459Sbostic if (badvfstype(mntbuf[i].f_type, vfslist)) 15466459Sbostic continue; 15566459Sbostic prmount(mntbuf[i].f_mntfromname, 15666628Sbostic mntbuf[i].f_mntonname, mntbuf[i].f_flags); 15766459Sbostic } 15835372Sbostic } 15935341Sbostic exit(rval); 16066459Sbostic case 1: 16166628Sbostic if (vfslist != NULL) 16235339Sbostic usage(); 16312808Ssam 16466459Sbostic if (init_flags & MNT_UPDATE) { 16566459Sbostic if ((mntbuf = getmntpt(*argv)) == NULL) 16666459Sbostic errx(1, 16766459Sbostic "unknown special file or file system %s.", 16866459Sbostic *argv); 16966459Sbostic if ((fs = getfsfile(mntbuf->f_mntonname)) == NULL) 17066459Sbostic errx(1, "can't find fstab entry for %s.", 17166459Sbostic *argv); 17266628Sbostic /* If it's an update, ignore the fstab file options. */ 17366628Sbostic fs->fs_mntops = NULL; 17466459Sbostic mntonname = mntbuf->f_mntonname; 17566459Sbostic } else { 17666459Sbostic if ((fs = getfsfile(*argv)) == NULL && 17766459Sbostic (fs = getfsspec(*argv)) == NULL) 17866459Sbostic errx(1, 17966459Sbostic "%s: unknown special file or file system.", 18066459Sbostic *argv); 18166459Sbostic if (BADTYPE(fs->fs_type)) 18266459Sbostic errx(1, "%s has unknown file system type.", 18366459Sbostic *argv); 18466459Sbostic mntonname = fs->fs_file; 18553711Smckusick } 18666459Sbostic rval = mountfs(fs->fs_vfstype, fs->fs_spec, 18766459Sbostic mntonname, init_flags, options, fs->fs_mntops); 18866459Sbostic break; 18966459Sbostic case 2: 19042857Smckusick /* 19166138Sbostic * If -t flag has not been specified, and spec contains either 19266138Sbostic * a ':' or a '@' then assume that an NFS filesystem is being 19366138Sbostic * specified ala Sun. 19442857Smckusick */ 19566628Sbostic if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL) 19666459Sbostic vfstype = "nfs"; 19766459Sbostic rval = mountfs(vfstype, 19866459Sbostic argv[0], argv[1], init_flags, options, NULL); 19966459Sbostic break; 20066459Sbostic default: 20166459Sbostic usage(); 20266459Sbostic /* NOTREACHED */ 20312808Ssam } 20466459Sbostic 20566174Spendry /* 20666459Sbostic * If the mount was successfully, and done by root, tell mountd the 20766459Sbostic * good news. Pid checks are probably unnecessary, but don't hurt. 20866174Spendry */ 20966177Sbostic if (rval == 0 && getuid() == 0 && 21066177Sbostic (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) { 21166177Sbostic if (fscanf(mountdfp, "%ld", &pid) == 1 && 21267013Smckusick pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH) 21366177Sbostic err(1, "signal mountd"); 21466177Sbostic (void)fclose(mountdfp); 21540496Smckusick } 21666138Sbostic 21766177Sbostic exit(rval); 21812808Ssam } 21912808Ssam 22066138Sbostic int 221*67412Smckusick hasopt(mntopts, option) 222*67412Smckusick const char *mntopts, *option; 223*67412Smckusick { 224*67412Smckusick int negative, found; 225*67412Smckusick char *opt, *optbuf; 226*67412Smckusick 227*67412Smckusick if (option[0] == 'n' && option[1] == 'o') { 228*67412Smckusick negative = 1; 229*67412Smckusick option += 2; 230*67412Smckusick } else 231*67412Smckusick negative = 0; 232*67412Smckusick optbuf = strdup(mntopts); 233*67412Smckusick found = 0; 234*67412Smckusick for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) { 235*67412Smckusick if (opt[0] == 'n' && opt[1] == 'o') { 236*67412Smckusick if (!strcasecmp(opt + 2, option)) 237*67412Smckusick found = negative; 238*67412Smckusick } else if (!strcasecmp(opt, option)) 239*67412Smckusick found = !negative; 240*67412Smckusick } 241*67412Smckusick free(optbuf); 242*67412Smckusick return (found); 243*67412Smckusick } 244*67412Smckusick 245*67412Smckusick int 24666459Sbostic mountfs(vfstype, spec, name, flags, options, mntopts) 24766459Sbostic const char *vfstype, *spec, *name, *options, *mntopts; 24839333Smckusick int flags; 24912808Ssam { 25066459Sbostic /* List of directories containing mount_xxx subcommands. */ 25166459Sbostic static const char *edirs[] = { 25266459Sbostic _PATH_SBIN, 25366459Sbostic _PATH_USRSBIN, 25466459Sbostic NULL 25566459Sbostic }; 25666459Sbostic const char *argv[100], **edir; 25766459Sbostic struct statfs sf; 25842253Sbostic pid_t pid; 25966138Sbostic int argc, i, status; 26066459Sbostic char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN]; 2611057Sbill 26266459Sbostic if (realpath(name, mntpath) == NULL) { 263*67412Smckusick warn("realpath %s", mntpath); 26466133Spendry return (1); 26566133Spendry } 26666133Spendry 26766133Spendry name = mntpath; 26866133Spendry 269*67412Smckusick if (mntopts == NULL) 270*67412Smckusick mntopts = ""; 27166628Sbostic if (options == NULL) { 272*67412Smckusick if (*mntopts == '\0') { 27366459Sbostic options = "rw"; 274*67412Smckusick } else { 27566459Sbostic options = mntopts; 276*67412Smckusick mntopts = ""; 277*67412Smckusick } 27866459Sbostic } 27966459Sbostic optbuf = catopt(strdup(mntopts), options); 28066459Sbostic 28166131Spendry if (strcmp(name, "/") == 0) 28266131Spendry flags |= MNT_UPDATE; 28366459Sbostic if (flags & MNT_FORCE) 28466459Sbostic optbuf = catopt(optbuf, "force"); 28566459Sbostic if (flags & MNT_RDONLY) 28666459Sbostic optbuf = catopt(optbuf, "ro"); 28766459Sbostic /* 28866459Sbostic * XXX 28966459Sbostic * The mount_mfs (newfs) command uses -o to select the 29066459Sbostic * optimisation mode. We don't pass the default "-o rw" 29166459Sbostic * for that reason. 29266459Sbostic */ 29366459Sbostic if (flags & MNT_UPDATE) 29466459Sbostic optbuf = catopt(optbuf, "update"); 29566131Spendry 29666459Sbostic argc = 0; 29766459Sbostic argv[argc++] = vfstype; 29866459Sbostic mangle(optbuf, &argc, argv); 29966459Sbostic argv[argc++] = spec; 30066459Sbostic argv[argc++] = name; 30166459Sbostic argv[argc] = NULL; 30266459Sbostic 30366459Sbostic if (debug) { 30466459Sbostic (void)printf("exec: mount_%s", vfstype); 30566459Sbostic for (i = 1; i < argc; i++) 30666459Sbostic (void)printf(" %s", argv[i]); 30766459Sbostic (void)printf("\n"); 30866459Sbostic return (0); 30966459Sbostic } 31066459Sbostic 31166459Sbostic switch (pid = vfork()) { 31266459Sbostic case -1: /* Error. */ 31366459Sbostic warn("vfork"); 31466459Sbostic free(optbuf); 31566459Sbostic return (1); 31666459Sbostic case 0: /* Child. */ 31766522Spendry if (strcmp(vfstype, "ufs") == 0) 31866522Spendry exit(mount_ufs(argc, (char * const *) argv)); 31966522Spendry 32066459Sbostic /* Go find an executable. */ 32166459Sbostic edir = edirs; 32266459Sbostic do { 32366459Sbostic (void)snprintf(execname, 32466459Sbostic sizeof(execname), "%s/mount_%s", *edir, vfstype); 32566459Sbostic execv(execname, (char * const *)argv); 32666459Sbostic if (errno != ENOENT) 32766459Sbostic warn("exec %s for %s", execname, name); 32866459Sbostic } while (*++edir != NULL); 32966459Sbostic 33066459Sbostic if (errno == ENOENT) 33166459Sbostic warn("exec %s for %s", execname, name); 33266459Sbostic exit(1); 33366459Sbostic /* NOTREACHED */ 33466459Sbostic default: /* Parent. */ 33566459Sbostic free(optbuf); 33666459Sbostic 33766459Sbostic if (waitpid(pid, &status, 0) < 0) { 33866459Sbostic warn("waitpid"); 33966459Sbostic return (1); 34039322Smckusick } 34166459Sbostic 34266459Sbostic if (WIFEXITED(status)) { 34366459Sbostic if (WEXITSTATUS(status) != 0) 34466459Sbostic return (WEXITSTATUS(status)); 34566459Sbostic } else if (WIFSIGNALED(status)) { 34666459Sbostic warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 34766459Sbostic return (1); 34866459Sbostic } 34966459Sbostic 35039316Smckusick if (verbose) { 35166459Sbostic if (statfs(name, &sf) < 0) { 352*67412Smckusick warn("statfs %s", name); 35339316Smckusick return (1); 35439131Smckusick } 35566459Sbostic prmount(sf.f_mntfromname, sf.f_mntonname, sf.f_flags); 35639316Smckusick } 35766459Sbostic break; 35839316Smckusick } 35935339Sbostic 36066130Spendry return (0); 3611057Sbill } 36235339Sbostic 36366138Sbostic void 36439316Smckusick prmount(spec, name, flags) 36566459Sbostic const char *spec, *name; 36666130Spendry int flags; 36735339Sbostic { 36866459Sbostic struct opt *o; 36966459Sbostic int f; 37038632Smckusick 37142253Sbostic (void)printf("%s on %s", spec, name); 37235339Sbostic 37366459Sbostic 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 } 37966459Sbostic (void)printf(f ? ")\n" : "\n"); 38039316Smckusick } 38139316Smckusick 38266138Sbostic struct statfs * 38339465Smckusick getmntpt(name) 38466459Sbostic const char *name; 38539465Smckusick { 38639465Smckusick struct statfs *mntbuf; 38766459Sbostic int i, mntsize; 38839465Smckusick 38940337Smckusick mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 39066459Sbostic for (i = 0; i < mntsize; i++) 39166459Sbostic if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 39266459Sbostic strcmp(mntbuf[i].f_mntonname, name) == 0) 39339465Smckusick return (&mntbuf[i]); 39466138Sbostic return (NULL); 39539465Smckusick } 39639465Smckusick 39766138Sbostic int 39866459Sbostic badvfsname(vfsname, vfslist) 39966459Sbostic const char *vfsname; 40066459Sbostic const char **vfslist; 40140051Smckusick { 40240051Smckusick 40366138Sbostic if (vfslist == NULL) 40466130Spendry return (0); 40566138Sbostic while (*vfslist != NULL) { 40666459Sbostic if (strcmp(vfsname, *vfslist) == 0) 40766130Spendry return (skipvfs); 40866459Sbostic ++vfslist; 40940051Smckusick } 41040051Smckusick return (!skipvfs); 41140051Smckusick } 41240051Smckusick 41366138Sbostic int 41466459Sbostic badvfstype(vfstype, vfslist) 41566459Sbostic int vfstype; 41666459Sbostic const char **vfslist; 41740844Smckusick { 41866459Sbostic static const char *vfsnames[] = INITMOUNTNAMES; 41940844Smckusick 42066459Sbostic if ((vfstype < 0) || (vfstype > MOUNT_MAXTYPE)) 42166130Spendry return (0); 42266459Sbostic 42366459Sbostic return (badvfsname(vfsnames[vfstype], vfslist)); 42440844Smckusick } 42540844Smckusick 42666459Sbostic const char ** 42740051Smckusick makevfslist(fslist) 42840051Smckusick char *fslist; 42940051Smckusick { 43066459Sbostic const char **av; 43166138Sbostic int i; 43266459Sbostic char *nextcp; 43340051Smckusick 43440051Smckusick if (fslist == NULL) 43540051Smckusick return (NULL); 43640051Smckusick if (fslist[0] == 'n' && fslist[1] == 'o') { 43740051Smckusick fslist += 2; 43840051Smckusick skipvfs = 1; 43940051Smckusick } 44040051Smckusick for (i = 0, nextcp = fslist; *nextcp; nextcp++) 44140051Smckusick if (*nextcp == ',') 44240051Smckusick i++; 44366459Sbostic if ((av = malloc((size_t)(i + 2) * sizeof(char *))) == NULL) { 44466459Sbostic warn(NULL); 44540051Smckusick return (NULL); 44666459Sbostic } 44740051Smckusick nextcp = fslist; 44840051Smckusick i = 0; 44940051Smckusick av[i++] = nextcp; 45066459Sbostic while ((nextcp = strchr(nextcp, ',')) != NULL) { 45140051Smckusick *nextcp++ = '\0'; 45240051Smckusick av[i++] = nextcp; 45340051Smckusick } 45466138Sbostic av[i++] = NULL; 45540051Smckusick return (av); 45640051Smckusick } 45766138Sbostic 45866459Sbostic char * 45966459Sbostic catopt(s0, s1) 46066459Sbostic char *s0; 46166459Sbostic const char *s1; 46266459Sbostic { 46366459Sbostic size_t i; 46466459Sbostic char *cp; 46566459Sbostic 46666459Sbostic if (s0 && *s0) { 46766459Sbostic i = strlen(s0) + strlen(s1) + 1 + 1; 46866459Sbostic if ((cp = malloc(i)) == NULL) 46966459Sbostic err(1, NULL); 47066459Sbostic (void)snprintf(cp, i, "%s,%s", s0, s1); 47166459Sbostic } else 47266459Sbostic cp = strdup(s1); 47366459Sbostic 47466459Sbostic if (s0) 47566459Sbostic free(s0); 47666459Sbostic return (cp); 47766459Sbostic } 47866459Sbostic 47966138Sbostic void 48066459Sbostic mangle(options, argcp, argv) 48166459Sbostic char *options; 48266459Sbostic int *argcp; 48366459Sbostic const char **argv; 48466459Sbostic { 48566459Sbostic char *p, *s; 48666459Sbostic int argc; 48766459Sbostic 48866459Sbostic argc = *argcp; 48966459Sbostic for (s = options; (p = strsep(&s, ",")) != NULL;) 49066459Sbostic if (*p != '\0') 49166459Sbostic if (*p == '-') { 49266459Sbostic argv[argc++] = p; 49366459Sbostic p = strchr(p, '='); 49466459Sbostic if (p) { 49566459Sbostic *p = '\0'; 49666459Sbostic argv[argc++] = p+1; 49766459Sbostic } 49866459Sbostic } else if (strcmp(p, "rw") != 0) { 49966459Sbostic argv[argc++] = "-o"; 50066459Sbostic argv[argc++] = p; 50166459Sbostic } 50266459Sbostic 50366459Sbostic *argcp = argc; 50466459Sbostic } 50566459Sbostic 50666459Sbostic void 50766138Sbostic usage() 50866138Sbostic { 50966138Sbostic 51066138Sbostic (void)fprintf(stderr, 51166459Sbostic "usage: mount %s %s\n mount %s\n mount %s\n", 51266459Sbostic "[-dfruvw] [-o options] [-t ufs | external_type]", 51366459Sbostic "special node", 51466459Sbostic "[-adfruvw] [-t ufs | external_type]", 51566459Sbostic "[-dfruvw] special | node"); 51666138Sbostic exit(1); 51766138Sbostic } 518