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*67013Smckusick static char sccsid[] = "@(#)mount.c 8.19 (Berkeley) 04/19/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 ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 14866459Sbostic err(1, "getmntinfo"); 14966459Sbostic for (i = 0; i < mntsize; i++) { 15066459Sbostic if (badvfstype(mntbuf[i].f_type, vfslist)) 15166459Sbostic continue; 15266459Sbostic prmount(mntbuf[i].f_mntfromname, 15366628Sbostic mntbuf[i].f_mntonname, mntbuf[i].f_flags); 15466459Sbostic } 15535372Sbostic } 15635341Sbostic exit(rval); 15766459Sbostic case 1: 15866628Sbostic if (vfslist != NULL) 15935339Sbostic usage(); 16012808Ssam 16166459Sbostic if (init_flags & MNT_UPDATE) { 16266459Sbostic if ((mntbuf = getmntpt(*argv)) == NULL) 16366459Sbostic errx(1, 16466459Sbostic "unknown special file or file system %s.", 16566459Sbostic *argv); 16666459Sbostic if ((fs = getfsfile(mntbuf->f_mntonname)) == NULL) 16766459Sbostic errx(1, "can't find fstab entry for %s.", 16866459Sbostic *argv); 16966628Sbostic /* If it's an update, ignore the fstab file options. */ 17066628Sbostic fs->fs_mntops = NULL; 17166459Sbostic mntonname = mntbuf->f_mntonname; 17266459Sbostic } else { 17366459Sbostic if ((fs = getfsfile(*argv)) == NULL && 17466459Sbostic (fs = getfsspec(*argv)) == NULL) 17566459Sbostic errx(1, 17666459Sbostic "%s: unknown special file or file system.", 17766459Sbostic *argv); 17866459Sbostic if (BADTYPE(fs->fs_type)) 17966459Sbostic errx(1, "%s has unknown file system type.", 18066459Sbostic *argv); 18166459Sbostic mntonname = fs->fs_file; 18253711Smckusick } 18366459Sbostic rval = mountfs(fs->fs_vfstype, fs->fs_spec, 18466459Sbostic mntonname, init_flags, options, fs->fs_mntops); 18566459Sbostic break; 18666459Sbostic case 2: 18742857Smckusick /* 18866138Sbostic * If -t flag has not been specified, and spec contains either 18966138Sbostic * a ':' or a '@' then assume that an NFS filesystem is being 19066138Sbostic * specified ala Sun. 19142857Smckusick */ 19266628Sbostic if (vfslist == NULL && strpbrk(argv[0], ":@") != NULL) 19366459Sbostic vfstype = "nfs"; 19466459Sbostic rval = mountfs(vfstype, 19566459Sbostic argv[0], argv[1], init_flags, options, NULL); 19666459Sbostic break; 19766459Sbostic default: 19866459Sbostic usage(); 19966459Sbostic /* NOTREACHED */ 20012808Ssam } 20166459Sbostic 20266174Spendry /* 20366459Sbostic * If the mount was successfully, and done by root, tell mountd the 20466459Sbostic * good news. Pid checks are probably unnecessary, but don't hurt. 20566174Spendry */ 20666177Sbostic if (rval == 0 && getuid() == 0 && 20766177Sbostic (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) { 20866177Sbostic if (fscanf(mountdfp, "%ld", &pid) == 1 && 209*67013Smckusick pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH) 21066177Sbostic err(1, "signal mountd"); 21166177Sbostic (void)fclose(mountdfp); 21240496Smckusick } 21366138Sbostic 21466177Sbostic exit(rval); 21512808Ssam } 21612808Ssam 21766138Sbostic int 21866459Sbostic mountfs(vfstype, spec, name, flags, options, mntopts) 21966459Sbostic const char *vfstype, *spec, *name, *options, *mntopts; 22039333Smckusick int flags; 22112808Ssam { 22266459Sbostic /* List of directories containing mount_xxx subcommands. */ 22366459Sbostic static const char *edirs[] = { 22466459Sbostic _PATH_SBIN, 22566459Sbostic _PATH_USRSBIN, 22666459Sbostic NULL 22766459Sbostic }; 22866459Sbostic const char *argv[100], **edir; 22966459Sbostic struct statfs sf; 23042253Sbostic pid_t pid; 23166138Sbostic int argc, i, status; 23266459Sbostic char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN]; 2331057Sbill 23466459Sbostic if (realpath(name, mntpath) == NULL) { 23566133Spendry warn("%s", mntpath); 23666133Spendry return (1); 23766133Spendry } 23866133Spendry 23966133Spendry name = mntpath; 24066133Spendry 24166628Sbostic if (options == NULL) { 24266628Sbostic if (mntopts == NULL || *mntopts == '\0') 24366459Sbostic options = "rw"; 24466459Sbostic else 24566459Sbostic options = mntopts; 24666459Sbostic mntopts = ""; 24766459Sbostic } 24866459Sbostic optbuf = catopt(strdup(mntopts), options); 24966459Sbostic 25066131Spendry if (strcmp(name, "/") == 0) 25166131Spendry flags |= MNT_UPDATE; 25266459Sbostic if (flags & MNT_FORCE) 25366459Sbostic optbuf = catopt(optbuf, "force"); 25466459Sbostic if (flags & MNT_RDONLY) 25566459Sbostic optbuf = catopt(optbuf, "ro"); 25666459Sbostic /* 25766459Sbostic * XXX 25866459Sbostic * The mount_mfs (newfs) command uses -o to select the 25966459Sbostic * optimisation mode. We don't pass the default "-o rw" 26066459Sbostic * for that reason. 26166459Sbostic */ 26266459Sbostic if (flags & MNT_UPDATE) 26366459Sbostic optbuf = catopt(optbuf, "update"); 26466131Spendry 26566459Sbostic argc = 0; 26666459Sbostic argv[argc++] = vfstype; 26766459Sbostic mangle(optbuf, &argc, argv); 26866459Sbostic argv[argc++] = spec; 26966459Sbostic argv[argc++] = name; 27066459Sbostic argv[argc] = NULL; 27166459Sbostic 27266459Sbostic if (debug) { 27366459Sbostic (void)printf("exec: mount_%s", vfstype); 27466459Sbostic for (i = 1; i < argc; i++) 27566459Sbostic (void)printf(" %s", argv[i]); 27666459Sbostic (void)printf("\n"); 27766459Sbostic return (0); 27866459Sbostic } 27966459Sbostic 28066459Sbostic switch (pid = vfork()) { 28166459Sbostic case -1: /* Error. */ 28266459Sbostic warn("vfork"); 28366459Sbostic free(optbuf); 28466459Sbostic return (1); 28566459Sbostic case 0: /* Child. */ 28666522Spendry if (strcmp(vfstype, "ufs") == 0) 28766522Spendry exit(mount_ufs(argc, (char * const *) argv)); 28866522Spendry 28966459Sbostic /* Go find an executable. */ 29066459Sbostic edir = edirs; 29166459Sbostic do { 29266459Sbostic (void)snprintf(execname, 29366459Sbostic sizeof(execname), "%s/mount_%s", *edir, vfstype); 29466459Sbostic execv(execname, (char * const *)argv); 29566459Sbostic if (errno != ENOENT) 29666459Sbostic warn("exec %s for %s", execname, name); 29766459Sbostic } while (*++edir != NULL); 29866459Sbostic 29966459Sbostic if (errno == ENOENT) 30066459Sbostic warn("exec %s for %s", execname, name); 30166459Sbostic exit(1); 30266459Sbostic /* NOTREACHED */ 30366459Sbostic default: /* Parent. */ 30466459Sbostic free(optbuf); 30566459Sbostic 30666459Sbostic if (waitpid(pid, &status, 0) < 0) { 30766459Sbostic warn("waitpid"); 30866459Sbostic return (1); 30939322Smckusick } 31066459Sbostic 31166459Sbostic if (WIFEXITED(status)) { 31266459Sbostic if (WEXITSTATUS(status) != 0) 31366459Sbostic return (WEXITSTATUS(status)); 31466459Sbostic } else if (WIFSIGNALED(status)) { 31566459Sbostic warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 31666459Sbostic return (1); 31766459Sbostic } 31866459Sbostic 31939316Smckusick if (verbose) { 32066459Sbostic if (statfs(name, &sf) < 0) { 32166459Sbostic warn("%s", name); 32239316Smckusick return (1); 32339131Smckusick } 32466459Sbostic prmount(sf.f_mntfromname, sf.f_mntonname, sf.f_flags); 32539316Smckusick } 32666459Sbostic break; 32739316Smckusick } 32835339Sbostic 32966130Spendry return (0); 3301057Sbill } 33135339Sbostic 33266138Sbostic void 33339316Smckusick prmount(spec, name, flags) 33466459Sbostic const char *spec, *name; 33566130Spendry int flags; 33635339Sbostic { 33766459Sbostic struct opt *o; 33866459Sbostic int f; 33938632Smckusick 34042253Sbostic (void)printf("%s on %s", spec, name); 34135339Sbostic 34266459Sbostic flags &= MNT_VISFLAGMASK; 34366459Sbostic for (f = 0, o = optnames; flags && o->o_opt; o++) 34466459Sbostic if (flags & o->o_opt) { 34566459Sbostic (void)printf("%s%s", !f++ ? " (" : ", ", o->o_name); 34666459Sbostic flags &= ~o->o_opt; 34739333Smckusick } 34866459Sbostic (void)printf(f ? ")\n" : "\n"); 34939316Smckusick } 35039316Smckusick 35166138Sbostic struct statfs * 35239465Smckusick getmntpt(name) 35366459Sbostic const char *name; 35439465Smckusick { 35539465Smckusick struct statfs *mntbuf; 35666459Sbostic int i, mntsize; 35739465Smckusick 35840337Smckusick mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 35966459Sbostic for (i = 0; i < mntsize; i++) 36066459Sbostic if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 36166459Sbostic strcmp(mntbuf[i].f_mntonname, name) == 0) 36239465Smckusick return (&mntbuf[i]); 36366138Sbostic return (NULL); 36439465Smckusick } 36539465Smckusick 36666138Sbostic int 36766459Sbostic badvfsname(vfsname, vfslist) 36866459Sbostic const char *vfsname; 36966459Sbostic const char **vfslist; 37040051Smckusick { 37140051Smckusick 37266138Sbostic if (vfslist == NULL) 37366130Spendry return (0); 37466138Sbostic while (*vfslist != NULL) { 37566459Sbostic if (strcmp(vfsname, *vfslist) == 0) 37666130Spendry return (skipvfs); 37766459Sbostic ++vfslist; 37840051Smckusick } 37940051Smckusick return (!skipvfs); 38040051Smckusick } 38140051Smckusick 38266138Sbostic int 38366459Sbostic badvfstype(vfstype, vfslist) 38466459Sbostic int vfstype; 38566459Sbostic const char **vfslist; 38640844Smckusick { 38766459Sbostic static const char *vfsnames[] = INITMOUNTNAMES; 38840844Smckusick 38966459Sbostic if ((vfstype < 0) || (vfstype > MOUNT_MAXTYPE)) 39066130Spendry return (0); 39166459Sbostic 39266459Sbostic return (badvfsname(vfsnames[vfstype], vfslist)); 39340844Smckusick } 39440844Smckusick 39566459Sbostic const char ** 39640051Smckusick makevfslist(fslist) 39740051Smckusick char *fslist; 39840051Smckusick { 39966459Sbostic const char **av; 40066138Sbostic int i; 40166459Sbostic char *nextcp; 40240051Smckusick 40340051Smckusick if (fslist == NULL) 40440051Smckusick return (NULL); 40540051Smckusick if (fslist[0] == 'n' && fslist[1] == 'o') { 40640051Smckusick fslist += 2; 40740051Smckusick skipvfs = 1; 40840051Smckusick } 40940051Smckusick for (i = 0, nextcp = fslist; *nextcp; nextcp++) 41040051Smckusick if (*nextcp == ',') 41140051Smckusick i++; 41266459Sbostic if ((av = malloc((size_t)(i + 2) * sizeof(char *))) == NULL) { 41366459Sbostic warn(NULL); 41440051Smckusick return (NULL); 41566459Sbostic } 41640051Smckusick nextcp = fslist; 41740051Smckusick i = 0; 41840051Smckusick av[i++] = nextcp; 41966459Sbostic while ((nextcp = strchr(nextcp, ',')) != NULL) { 42040051Smckusick *nextcp++ = '\0'; 42140051Smckusick av[i++] = nextcp; 42240051Smckusick } 42366138Sbostic av[i++] = NULL; 42440051Smckusick return (av); 42540051Smckusick } 42666138Sbostic 42766459Sbostic char * 42866459Sbostic catopt(s0, s1) 42966459Sbostic char *s0; 43066459Sbostic const char *s1; 43166459Sbostic { 43266459Sbostic size_t i; 43366459Sbostic char *cp; 43466459Sbostic 43566459Sbostic if (s0 && *s0) { 43666459Sbostic i = strlen(s0) + strlen(s1) + 1 + 1; 43766459Sbostic if ((cp = malloc(i)) == NULL) 43866459Sbostic err(1, NULL); 43966459Sbostic (void)snprintf(cp, i, "%s,%s", s0, s1); 44066459Sbostic } else 44166459Sbostic cp = strdup(s1); 44266459Sbostic 44366459Sbostic if (s0) 44466459Sbostic free(s0); 44566459Sbostic return (cp); 44666459Sbostic } 44766459Sbostic 44866138Sbostic void 44966459Sbostic mangle(options, argcp, argv) 45066459Sbostic char *options; 45166459Sbostic int *argcp; 45266459Sbostic const char **argv; 45366459Sbostic { 45466459Sbostic char *p, *s; 45566459Sbostic int argc; 45666459Sbostic 45766459Sbostic argc = *argcp; 45866459Sbostic for (s = options; (p = strsep(&s, ",")) != NULL;) 45966459Sbostic if (*p != '\0') 46066459Sbostic if (*p == '-') { 46166459Sbostic argv[argc++] = p; 46266459Sbostic p = strchr(p, '='); 46366459Sbostic if (p) { 46466459Sbostic *p = '\0'; 46566459Sbostic argv[argc++] = p+1; 46666459Sbostic } 46766459Sbostic } else if (strcmp(p, "rw") != 0) { 46866459Sbostic argv[argc++] = "-o"; 46966459Sbostic argv[argc++] = p; 47066459Sbostic } 47166459Sbostic 47266459Sbostic *argcp = argc; 47366459Sbostic } 47466459Sbostic 47566459Sbostic void 47666138Sbostic usage() 47766138Sbostic { 47866138Sbostic 47966138Sbostic (void)fprintf(stderr, 48066459Sbostic "usage: mount %s %s\n mount %s\n mount %s\n", 48166459Sbostic "[-dfruvw] [-o options] [-t ufs | external_type]", 48266459Sbostic "special node", 48366459Sbostic "[-adfruvw] [-t ufs | external_type]", 48466459Sbostic "[-dfruvw] special | node"); 48566138Sbostic exit(1); 48666138Sbostic } 487