121156Sdist /* 2*66459Sbostic * 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[] = 10*66459Sbostic "@(#) 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*66459Sbostic static char sccsid[] = "@(#)mount.c 8.16 (Berkeley) 03/27/94"; 1639322Smckusick #endif /* not lint */ 1721156Sdist 1866138Sbostic #include <sys/param.h> 19*66459Sbostic #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 33*66459Sbostic int debug, verbose, skipvfs; 3440369Smckusick 35*66459Sbostic int badvfsname __P((const char *, const char **)); 36*66459Sbostic int badvfstype __P((int, const char **)); 37*66459Sbostic char *catopt __P((char *, const char *)); 3866138Sbostic struct statfs 39*66459Sbostic *getmntpt __P((const char *)); 40*66459Sbostic const char 41*66459Sbostic **makevfslist __P((char *)); 42*66459Sbostic void mangle __P((char *, int *, const char **)); 43*66459Sbostic int mountfs __P((const char *, const char *, const char *, 44*66459Sbostic int, const char *, const char *)); 45*66459Sbostic void prmount __P((const char *, const char *, int)); 46*66459Sbostic void usage __P((void)); 4766138Sbostic 48*66459Sbostic /* From mount_ufs.c. */ 49*66459Sbostic int mount_ufs __P((int, char * const *)); 50*66459Sbostic 51*66459Sbostic /* Map from mount otions to printable formats. */ 52*66459Sbostic static struct opt { 53*66459Sbostic int o_opt; 54*66459Sbostic const char *o_name; 55*66459Sbostic } optnames[] = { 56*66459Sbostic { MNT_ASYNC, "asynchronous" }, 57*66459Sbostic { MNT_EXPORTED, "NFS exported" }, 58*66459Sbostic { MNT_LOCAL, "local" }, 59*66459Sbostic { MNT_NODEV, "nodev" }, 60*66459Sbostic { MNT_NOEXEC, "noexec" }, 61*66459Sbostic { MNT_NOSUID, "nosuid" }, 62*66459Sbostic { MNT_QUOTA, "with quotas" }, 63*66459Sbostic { MNT_RDONLY, "read-only" }, 64*66459Sbostic { MNT_SYNCHRONOUS, "synchronous" }, 65*66459Sbostic { MNT_UNION, "union" }, 66*66459Sbostic { MNT_USER, "user mount" }, 67*66459Sbostic { NULL } 68*66459Sbostic }; 69*66459Sbostic 7066130Spendry int 7166130Spendry main(argc, argv) 725073Sroot int argc; 73*66459Sbostic char * const argv[]; 741057Sbill { 75*66459Sbostic const char *mntonname, **vfslist, *vfstype; 7666130Spendry struct fstab *fs; 7766130Spendry struct statfs *mntbuf; 7866177Sbostic FILE *mountdfp; 7966177Sbostic pid_t pid; 80*66459Sbostic int all, ch, i, init_flags, mntsize, rval; 81*66459Sbostic char *options; 821057Sbill 83*66459Sbostic all = init_flags = 0; 84*66459Sbostic options = NULL; 8566138Sbostic vfslist = NULL; 86*66459Sbostic vfstype = "ufs"; 8766138Sbostic while ((ch = getopt(argc, argv, "adfo:rwt:uv")) != EOF) 88*66459Sbostic switch (ch) { 8935339Sbostic case 'a': 9035339Sbostic all = 1; 9135339Sbostic break; 9256841Smckusick case 'd': 9356841Smckusick debug = 1; 9456841Smckusick break; 9535339Sbostic case 'f': 96*66459Sbostic init_flags |= MNT_FORCE; 9735339Sbostic break; 9866138Sbostic case 'o': 99*66459Sbostic if (*optarg) 100*66459Sbostic options = catopt(options, optarg); 10166138Sbostic break; 10235339Sbostic case 'r': 103*66459Sbostic init_flags |= MNT_RDONLY; 10435339Sbostic break; 10566138Sbostic case 't': 106*66459Sbostic if (vfslist != NULL) 107*66459Sbostic errx(1, "only one -t option may be specified."); 10866138Sbostic vfslist = makevfslist(optarg); 109*66459Sbostic vfstype = optarg; 11066138Sbostic break; 11139333Smckusick case 'u': 112*66459Sbostic init_flags |= MNT_UPDATE; 11339333Smckusick break; 11435339Sbostic case 'v': 11535339Sbostic verbose = 1; 11635339Sbostic break; 11735339Sbostic case 'w': 118*66459Sbostic 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; 133*66459Sbostic switch (argc) { 134*66459Sbostic case 0: 135*66459Sbostic if (all) 136*66459Sbostic while ((fs = getfsent()) != NULL) { 137*66459Sbostic if (BADTYPE(fs->fs_type)) 138*66459Sbostic continue; 139*66459Sbostic if (badvfsname(fs->fs_vfstype, vfslist)) 140*66459Sbostic continue; 141*66459Sbostic if (mountfs(fs->fs_vfstype, fs->fs_spec, 142*66459Sbostic fs->fs_file, init_flags, options, 143*66459Sbostic fs->fs_mntops)) 144*66459Sbostic rval = 1; 145*66459Sbostic } 146*66459Sbostic else { 147*66459Sbostic if (verbose) { 148*66459Sbostic usage(); 149*66459Sbostic /* NOTREACHED */ 150*66459Sbostic } 151*66459Sbostic 152*66459Sbostic if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 153*66459Sbostic err(1, "getmntinfo"); 154*66459Sbostic for (i = 0; i < mntsize; i++) { 155*66459Sbostic if (badvfstype(mntbuf[i].f_type, vfslist)) 156*66459Sbostic continue; 157*66459Sbostic prmount(mntbuf[i].f_mntfromname, 158*66459Sbostic mntbuf[i].f_mntonname, mntbuf[i].f_flags); 159*66459Sbostic } 16035372Sbostic } 16135341Sbostic exit(rval); 162*66459Sbostic case 1: 163*66459Sbostic if (vfslist != NULL) { 16435339Sbostic usage(); 165*66459Sbostic /* NOTREACHED */ 16640844Smckusick } 16712808Ssam 168*66459Sbostic if (init_flags & MNT_UPDATE) { 169*66459Sbostic if ((mntbuf = getmntpt(*argv)) == NULL) 170*66459Sbostic errx(1, 171*66459Sbostic "unknown special file or file system %s.", 172*66459Sbostic *argv); 173*66459Sbostic if ((fs = getfsfile(mntbuf->f_mntonname)) == NULL) 174*66459Sbostic errx(1, "can't find fstab entry for %s.", 175*66459Sbostic *argv); 176*66459Sbostic mntonname = mntbuf->f_mntonname; 177*66459Sbostic } else { 178*66459Sbostic if ((fs = getfsfile(*argv)) == NULL && 179*66459Sbostic (fs = getfsspec(*argv)) == NULL) 180*66459Sbostic errx(1, 181*66459Sbostic "%s: unknown special file or file system.", 182*66459Sbostic *argv); 183*66459Sbostic if (BADTYPE(fs->fs_type)) 184*66459Sbostic errx(1, "%s has unknown file system type.", 185*66459Sbostic *argv); 186*66459Sbostic mntonname = fs->fs_file; 18753711Smckusick } 188*66459Sbostic rval = mountfs(fs->fs_vfstype, fs->fs_spec, 189*66459Sbostic mntonname, init_flags, options, fs->fs_mntops); 190*66459Sbostic break; 191*66459Sbostic case 2: 19242857Smckusick /* 19366138Sbostic * If -t flag has not been specified, and spec contains either 19466138Sbostic * a ':' or a '@' then assume that an NFS filesystem is being 19566138Sbostic * specified ala Sun. 19642857Smckusick */ 19766138Sbostic if (vfslist == NULL && 198*66459Sbostic (strchr(argv[0], ':') != NULL || 199*66459Sbostic strchr(argv[0], '@') != NULL)) 200*66459Sbostic vfstype = "nfs"; 201*66459Sbostic rval = mountfs(vfstype, 202*66459Sbostic argv[0], argv[1], init_flags, options, NULL); 203*66459Sbostic break; 204*66459Sbostic default: 205*66459Sbostic usage(); 206*66459Sbostic /* NOTREACHED */ 20712808Ssam } 208*66459Sbostic 20966174Spendry /* 210*66459Sbostic * If the mount was successfully, and done by root, tell mountd the 211*66459Sbostic * good news. Pid checks are probably unnecessary, but don't hurt. 21266174Spendry */ 21366177Sbostic if (rval == 0 && getuid() == 0 && 21466177Sbostic (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) { 21566177Sbostic if (fscanf(mountdfp, "%ld", &pid) == 1 && 216*66459Sbostic pid > 0 && kill(pid, SIGHUP)) 21766177Sbostic err(1, "signal mountd"); 21866177Sbostic (void)fclose(mountdfp); 21940496Smckusick } 22066138Sbostic 22166177Sbostic exit(rval); 22212808Ssam } 22312808Ssam 22466138Sbostic int 225*66459Sbostic mountfs(vfstype, spec, name, flags, options, mntopts) 226*66459Sbostic const char *vfstype, *spec, *name, *options, *mntopts; 22739333Smckusick int flags; 22812808Ssam { 229*66459Sbostic /* List of directories containing mount_xxx subcommands. */ 230*66459Sbostic static const char *edirs[] = { 231*66459Sbostic _PATH_SBIN, 232*66459Sbostic _PATH_USRSBIN, 233*66459Sbostic NULL 234*66459Sbostic }; 235*66459Sbostic const char *argv[100], **edir; 236*66459Sbostic struct statfs sf; 23742253Sbostic pid_t pid; 23866138Sbostic int argc, i, status; 239*66459Sbostic char *optbuf, execname[MAXPATHLEN + 1], mntpath[MAXPATHLEN]; 2401057Sbill 241*66459Sbostic if (realpath(name, mntpath) == NULL) { 24266133Spendry warn("%s", mntpath); 24366133Spendry return (1); 24466133Spendry } 24566133Spendry 24666133Spendry name = mntpath; 24766133Spendry 248*66459Sbostic if (options == 0) { 249*66459Sbostic if (!mntopts || !*mntopts) 250*66459Sbostic options = "rw"; 251*66459Sbostic else 252*66459Sbostic options = mntopts; 253*66459Sbostic mntopts = ""; 254*66459Sbostic 255*66459Sbostic } 256*66459Sbostic optbuf = catopt(strdup(mntopts), options); 257*66459Sbostic 25866131Spendry if (strcmp(name, "/") == 0) 25966131Spendry flags |= MNT_UPDATE; 260*66459Sbostic if (flags & MNT_FORCE) 261*66459Sbostic optbuf = catopt(optbuf, "force"); 262*66459Sbostic if (flags & MNT_RDONLY) 263*66459Sbostic optbuf = catopt(optbuf, "ro"); 264*66459Sbostic /* 265*66459Sbostic * XXX 266*66459Sbostic * The mount_mfs (newfs) command uses -o to select the 267*66459Sbostic * optimisation mode. We don't pass the default "-o rw" 268*66459Sbostic * for that reason. 269*66459Sbostic */ 270*66459Sbostic if (flags & MNT_UPDATE) 271*66459Sbostic optbuf = catopt(optbuf, "update"); 27266131Spendry 273*66459Sbostic argc = 0; 274*66459Sbostic argv[argc++] = vfstype; 275*66459Sbostic mangle(optbuf, &argc, argv); 276*66459Sbostic argv[argc++] = spec; 277*66459Sbostic argv[argc++] = name; 278*66459Sbostic argv[argc] = NULL; 279*66459Sbostic 280*66459Sbostic if (debug) { 281*66459Sbostic (void)printf("exec: mount_%s", vfstype); 282*66459Sbostic for (i = 1; i < argc; i++) 283*66459Sbostic (void)printf(" %s", argv[i]); 284*66459Sbostic (void)printf("\n"); 285*66459Sbostic return (0); 286*66459Sbostic } 287*66459Sbostic 288*66459Sbostic switch (pid = vfork()) { 289*66459Sbostic case -1: /* Error. */ 290*66459Sbostic warn("vfork"); 291*66459Sbostic free(optbuf); 292*66459Sbostic return (1); 293*66459Sbostic case 0: /* Child. */ 294*66459Sbostic /* Go find an executable. */ 295*66459Sbostic edir = edirs; 296*66459Sbostic do { 297*66459Sbostic (void)snprintf(execname, 298*66459Sbostic sizeof(execname), "%s/mount_%s", *edir, vfstype); 299*66459Sbostic execv(execname, (char * const *)argv); 300*66459Sbostic if (errno != ENOENT) 301*66459Sbostic warn("exec %s for %s", execname, name); 302*66459Sbostic } while (*++edir != NULL); 303*66459Sbostic 304*66459Sbostic if (errno == ENOENT) 305*66459Sbostic warn("exec %s for %s", execname, name); 306*66459Sbostic exit(1); 307*66459Sbostic /* NOTREACHED */ 308*66459Sbostic default: /* Parent. */ 309*66459Sbostic free(optbuf); 310*66459Sbostic 311*66459Sbostic if (waitpid(pid, &status, 0) < 0) { 312*66459Sbostic warn("waitpid"); 313*66459Sbostic return (1); 31439322Smckusick } 315*66459Sbostic 316*66459Sbostic if (WIFEXITED(status)) { 317*66459Sbostic if (WEXITSTATUS(status) != 0) 318*66459Sbostic return (WEXITSTATUS(status)); 319*66459Sbostic } else if (WIFSIGNALED(status)) { 320*66459Sbostic warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]); 321*66459Sbostic return (1); 322*66459Sbostic } 323*66459Sbostic 32439316Smckusick if (verbose) { 325*66459Sbostic if (statfs(name, &sf) < 0) { 326*66459Sbostic warn("%s", name); 32739316Smckusick return (1); 32839131Smckusick } 329*66459Sbostic prmount(sf.f_mntfromname, sf.f_mntonname, sf.f_flags); 33039316Smckusick } 331*66459Sbostic break; 33239316Smckusick } 33335339Sbostic 33466130Spendry return (0); 3351057Sbill } 33635339Sbostic 33766138Sbostic void 33839316Smckusick prmount(spec, name, flags) 339*66459Sbostic const char *spec, *name; 34066130Spendry int flags; 34135339Sbostic { 342*66459Sbostic struct opt *o; 343*66459Sbostic int f; 34438632Smckusick 34542253Sbostic (void)printf("%s on %s", spec, name); 34635339Sbostic 347*66459Sbostic flags &= MNT_VISFLAGMASK; 348*66459Sbostic for (f = 0, o = optnames; flags && o->o_opt; o++) 349*66459Sbostic if (flags & o->o_opt) { 350*66459Sbostic (void)printf("%s%s", !f++ ? " (" : ", ", o->o_name); 351*66459Sbostic flags &= ~o->o_opt; 35239333Smckusick } 353*66459Sbostic (void)printf(f ? ")\n" : "\n"); 35439316Smckusick } 35539316Smckusick 35666138Sbostic struct statfs * 35739465Smckusick getmntpt(name) 358*66459Sbostic const char *name; 35939465Smckusick { 36039465Smckusick struct statfs *mntbuf; 361*66459Sbostic int i, mntsize; 36239465Smckusick 36340337Smckusick mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 364*66459Sbostic for (i = 0; i < mntsize; i++) 365*66459Sbostic if (strcmp(mntbuf[i].f_mntfromname, name) == 0 || 366*66459Sbostic strcmp(mntbuf[i].f_mntonname, name) == 0) 36739465Smckusick return (&mntbuf[i]); 36866138Sbostic return (NULL); 36939465Smckusick } 37039465Smckusick 37166138Sbostic int 372*66459Sbostic badvfsname(vfsname, vfslist) 373*66459Sbostic const char *vfsname; 374*66459Sbostic const char **vfslist; 37540051Smckusick { 37640051Smckusick 37766138Sbostic if (vfslist == NULL) 37866130Spendry return (0); 37966138Sbostic while (*vfslist != NULL) { 380*66459Sbostic if (strcmp(vfsname, *vfslist) == 0) 38166130Spendry return (skipvfs); 382*66459Sbostic ++vfslist; 38340051Smckusick } 38440051Smckusick return (!skipvfs); 38540051Smckusick } 38640051Smckusick 38766138Sbostic int 388*66459Sbostic badvfstype(vfstype, vfslist) 389*66459Sbostic int vfstype; 390*66459Sbostic const char **vfslist; 39140844Smckusick { 392*66459Sbostic static const char *vfsnames[] = INITMOUNTNAMES; 39340844Smckusick 394*66459Sbostic if ((vfstype < 0) || (vfstype > MOUNT_MAXTYPE)) 39566130Spendry return (0); 396*66459Sbostic 397*66459Sbostic return (badvfsname(vfsnames[vfstype], vfslist)); 39840844Smckusick } 39940844Smckusick 400*66459Sbostic const char ** 40140051Smckusick makevfslist(fslist) 40240051Smckusick char *fslist; 40340051Smckusick { 404*66459Sbostic const char **av; 40566138Sbostic int i; 406*66459Sbostic char *nextcp; 40740051Smckusick 40840051Smckusick if (fslist == NULL) 40940051Smckusick return (NULL); 41040051Smckusick if (fslist[0] == 'n' && fslist[1] == 'o') { 41140051Smckusick fslist += 2; 41240051Smckusick skipvfs = 1; 41340051Smckusick } 41440051Smckusick for (i = 0, nextcp = fslist; *nextcp; nextcp++) 41540051Smckusick if (*nextcp == ',') 41640051Smckusick i++; 417*66459Sbostic if ((av = malloc((size_t)(i + 2) * sizeof(char *))) == NULL) { 418*66459Sbostic warn(NULL); 41940051Smckusick return (NULL); 420*66459Sbostic } 42140051Smckusick nextcp = fslist; 42240051Smckusick i = 0; 42340051Smckusick av[i++] = nextcp; 424*66459Sbostic while ((nextcp = strchr(nextcp, ',')) != NULL) { 42540051Smckusick *nextcp++ = '\0'; 42640051Smckusick av[i++] = nextcp; 42740051Smckusick } 42866138Sbostic av[i++] = NULL; 42940051Smckusick return (av); 43040051Smckusick } 43166138Sbostic 432*66459Sbostic char * 433*66459Sbostic catopt(s0, s1) 434*66459Sbostic char *s0; 435*66459Sbostic const char *s1; 436*66459Sbostic { 437*66459Sbostic size_t i; 438*66459Sbostic char *cp; 439*66459Sbostic 440*66459Sbostic if (s0 && *s0) { 441*66459Sbostic i = strlen(s0) + strlen(s1) + 1 + 1; 442*66459Sbostic if ((cp = malloc(i)) == NULL) 443*66459Sbostic err(1, NULL); 444*66459Sbostic (void)snprintf(cp, i, "%s,%s", s0, s1); 445*66459Sbostic } else 446*66459Sbostic cp = strdup(s1); 447*66459Sbostic 448*66459Sbostic if (s0) 449*66459Sbostic free(s0); 450*66459Sbostic return (cp); 451*66459Sbostic } 452*66459Sbostic 45366138Sbostic void 454*66459Sbostic mangle(options, argcp, argv) 455*66459Sbostic char *options; 456*66459Sbostic int *argcp; 457*66459Sbostic const char **argv; 458*66459Sbostic { 459*66459Sbostic char *p, *s; 460*66459Sbostic int argc; 461*66459Sbostic 462*66459Sbostic argc = *argcp; 463*66459Sbostic for (s = options; (p = strsep(&s, ",")) != NULL;) 464*66459Sbostic if (*p != '\0') 465*66459Sbostic if (*p == '-') { 466*66459Sbostic argv[argc++] = p; 467*66459Sbostic p = strchr(p, '='); 468*66459Sbostic if (p) { 469*66459Sbostic *p = '\0'; 470*66459Sbostic argv[argc++] = p+1; 471*66459Sbostic } 472*66459Sbostic } else if (strcmp(p, "rw") != 0) { 473*66459Sbostic argv[argc++] = "-o"; 474*66459Sbostic argv[argc++] = p; 475*66459Sbostic } 476*66459Sbostic 477*66459Sbostic *argcp = argc; 478*66459Sbostic } 479*66459Sbostic 480*66459Sbostic void 48166138Sbostic usage() 48266138Sbostic { 48366138Sbostic 48466138Sbostic (void)fprintf(stderr, 485*66459Sbostic "usage: mount %s %s\n mount %s\n mount %s\n", 486*66459Sbostic "[-dfruvw] [-o options] [-t ufs | external_type]", 487*66459Sbostic "special node", 488*66459Sbostic "[-adfruvw] [-t ufs | external_type]", 489*66459Sbostic "[-dfruvw] special | node"); 49066138Sbostic exit(1); 49166138Sbostic } 492