121156Sdist /* 239322Smckusick * Copyright (c) 1980, 1989 The Regents of the University of California. 339322Smckusick * All rights reserved. 439322Smckusick * 542702Sbostic * %sccs.include.redist.c% 621156Sdist */ 721156Sdist 810811Ssam #ifndef lint 921156Sdist char copyright[] = 1039322Smckusick "@(#) Copyright (c) 1980, 1989 The Regents of the University of California.\n\ 1121156Sdist All rights reserved.\n"; 1239322Smckusick #endif /* not lint */ 131057Sbill 1421156Sdist #ifndef lint 15*53711Smckusick static char sccsid[] = "@(#)mount.c 5.49 (Berkeley) 05/28/92"; 1639322Smckusick #endif /* not lint */ 1721156Sdist 1812808Ssam #include <sys/param.h> 1935339Sbostic #include <sys/file.h> 2038445Smckusick #include <sys/time.h> 2139133Smckusick #include <sys/wait.h> 2245524Sbostic #include <sys/errno.h> 2345524Sbostic #include <sys/signal.h> 2452110Smckusick #include <sys/ucred.h> 2538445Smckusick #include <sys/mount.h> 2645524Sbostic #include <fstab.h> 2745524Sbostic #include <string.h> 2845524Sbostic #include <stdio.h> 2945524Sbostic #include <stdlib.h> 3045524Sbostic #include "pathnames.h" 311057Sbill 3240369Smckusick #define DEFAULT_ROOTUID -2 3340369Smckusick 3435339Sbostic #define BADTYPE(type) \ 3535339Sbostic (strcmp(type, FSTAB_RO) && strcmp(type, FSTAB_RW) && \ 3635339Sbostic strcmp(type, FSTAB_RQ)) 3735339Sbostic #define SETTYPE(type) \ 3835339Sbostic (!strcmp(type, FSTAB_RW) || !strcmp(type, FSTAB_RQ)) 391057Sbill 4039333Smckusick int fake, verbose, updateflg, mnttype; 4139322Smckusick char *mntname, **envp; 4240051Smckusick char **vfslist, **makevfslist(); 4346708Sbostic static void prmount(); 4439131Smckusick 4539131Smckusick main(argc, argv, arge) 465073Sroot int argc; 475073Sroot char **argv; 4839131Smckusick char **arge; 491057Sbill { 5035339Sbostic extern char *optarg; 5135339Sbostic extern int optind; 5235339Sbostic register struct fstab *fs; 5340496Smckusick int all, ch, rval, flags, ret, pid, i; 5438632Smckusick long mntsize; 5539465Smckusick struct statfs *mntbuf, *getmntpt(); 5639131Smckusick char *type, *options = NULL; 5740496Smckusick FILE *pidfile; 581057Sbill 5939131Smckusick envp = arge; 6035339Sbostic all = 0; 6135339Sbostic type = NULL; 6238445Smckusick mnttype = MOUNT_UFS; 6339322Smckusick mntname = "ufs"; 6439333Smckusick while ((ch = getopt(argc, argv, "afrwuvt:o:")) != EOF) 6535339Sbostic switch((char)ch) { 6635339Sbostic case 'a': 6735339Sbostic all = 1; 6835339Sbostic break; 6935339Sbostic case 'f': 7035339Sbostic fake = 1; 7135339Sbostic break; 7235339Sbostic case 'r': 7312808Ssam type = FSTAB_RO; 7435339Sbostic break; 7539333Smckusick case 'u': 7641403Smckusick updateflg = MNT_UPDATE; 7739333Smckusick break; 7835339Sbostic case 'v': 7935339Sbostic verbose = 1; 8035339Sbostic break; 8135339Sbostic case 'w': 8235339Sbostic type = FSTAB_RW; 8335339Sbostic break; 8439131Smckusick case 'o': 8539131Smckusick options = optarg; 8639131Smckusick break; 8738445Smckusick case 't': 8840051Smckusick vfslist = makevfslist(optarg); 8939322Smckusick mnttype = getmnttype(optarg); 9039322Smckusick break; 9135339Sbostic case '?': 9235339Sbostic default: 9335339Sbostic usage(); 9439131Smckusick /* NOTREACHED */ 955073Sroot } 9635339Sbostic argc -= optind; 9735339Sbostic argv += optind; 9835339Sbostic 9935339Sbostic /* NOSTRICT */ 10035339Sbostic 1014460Sroot if (all) { 10235369Sbostic rval = 0; 10339333Smckusick while (fs = getfsent()) { 10435372Sbostic if (BADTYPE(fs->fs_type)) 10535372Sbostic continue; 10640844Smckusick if (badvfsname(fs->fs_vfstype, vfslist)) 10740051Smckusick continue; 10835372Sbostic /* `/' is special, it's always mounted */ 10935372Sbostic if (!strcmp(fs->fs_file, "/")) 11041403Smckusick flags = MNT_UPDATE; 11139333Smckusick else 11239333Smckusick flags = updateflg; 11339333Smckusick mnttype = getmnttype(fs->fs_vfstype); 11439333Smckusick rval |= mountfs(fs->fs_spec, fs->fs_file, flags, 11539333Smckusick type, options, fs->fs_mntops); 11635372Sbostic } 11735341Sbostic exit(rval); 11835339Sbostic } 1195073Sroot 12035339Sbostic if (argc == 0) { 12135339Sbostic if (verbose || fake || type) 12235339Sbostic usage(); 12340337Smckusick if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) { 12445689Smckusick (void) fprintf(stderr, 12539319Smckusick "mount: cannot get mount information\n"); 12638632Smckusick exit(1); 12738632Smckusick } 12840844Smckusick for (i = 0; i < mntsize; i++) { 12940844Smckusick if (badvfstype(mntbuf[i].f_type, vfslist)) 13040844Smckusick continue; 13138632Smckusick prmount(mntbuf[i].f_mntfromname, mntbuf[i].f_mntonname, 13239316Smckusick mntbuf[i].f_flags); 13340844Smckusick } 1344460Sroot exit(0); 1351057Sbill } 13612808Ssam 13739465Smckusick if (argc == 1 && updateflg) { 13839465Smckusick if ((mntbuf = getmntpt(*argv)) == NULL) { 13945689Smckusick (void) fprintf(stderr, 14039465Smckusick "mount: unknown special file or file system %s.\n", 14139465Smckusick *argv); 14239465Smckusick exit(1); 14339465Smckusick } 14439465Smckusick mnttype = mntbuf->f_type; 145*53711Smckusick if ((fs = getfsfile(mntbuf->f_mntonname)) == NULL) { 146*53711Smckusick (void) fprintf(stderr, 147*53711Smckusick "mount: can't find fstab entry for %s.\n", *argv); 148*53711Smckusick exit(1); 14940124Smckusick } 150*53711Smckusick mntname = fs->fs_vfstype; 151*53711Smckusick 152*53711Smckusick /* 153*53711Smckusick * Default type to fstab version if none specified on the 154*53711Smckusick * command line. 155*53711Smckusick */ 156*53711Smckusick if (type == NULL) 157*53711Smckusick type = fs->fs_type; 158*53711Smckusick 159*53711Smckusick /* 160*53711Smckusick * Default options to fstab version if none specified on the 161*53711Smckusick * command line. 162*53711Smckusick */ 163*53711Smckusick if (options == NULL) 164*53711Smckusick options = fs->fs_mntops; 165*53711Smckusick else { 166*53711Smckusick register char *cp; 167*53711Smckusick 168*53711Smckusick /* 169*53711Smckusick * Concat the two strings with the command line 170*53711Smckusick * options last so that they will override the 171*53711Smckusick * fstab options. 172*53711Smckusick */ 173*53711Smckusick i = strlen(fs->fs_mntops) + strlen(options) + 2; 174*53711Smckusick if ((cp = malloc((size_t)i)) == NULL) { 175*53711Smckusick (void) fprintf(stderr, 176*53711Smckusick "mount: -u malloc failed\n"); 177*53711Smckusick exit(1); 178*53711Smckusick } 179*53711Smckusick sprintf(cp, "%s,%s", fs->fs_mntops, options); 180*53711Smckusick options = cp; 181*53711Smckusick } 182*53711Smckusick ret = mountfs(fs->fs_spec, mntbuf->f_mntonname, 18342253Sbostic updateflg, type, options, (char *)NULL); 18440496Smckusick } else if (argc == 1) { 18535339Sbostic if (!(fs = getfsfile(*argv)) && !(fs = getfsspec(*argv))) { 18645689Smckusick (void) fprintf(stderr, 18735339Sbostic "mount: unknown special file or file system %s.\n", 18835339Sbostic *argv); 18935339Sbostic exit(1); 19035339Sbostic } 19135339Sbostic if (BADTYPE(fs->fs_type)) { 19245689Smckusick (void) fprintf(stderr, 19335339Sbostic "mount: %s has unknown file system type.\n", *argv); 19435339Sbostic exit(1); 19535339Sbostic } 19639333Smckusick mnttype = getmnttype(fs->fs_vfstype); 19740496Smckusick ret = mountfs(fs->fs_spec, fs->fs_file, updateflg, 19840496Smckusick type, options, fs->fs_mntops); 19940496Smckusick } else if (argc != 2) { 20040496Smckusick usage(); 20140496Smckusick ret = 1; 20240496Smckusick } else { 20342857Smckusick /* 20442857Smckusick * If -t flag has not been specified, and spec 20542857Smckusick * contains either a ':' or a '@' then assume that 20642857Smckusick * an NFS filesystem is being specified ala Sun. 20742857Smckusick */ 20842857Smckusick if (vfslist == (char **)0 && 20952182Smckusick (index(argv[0], ':') || index(argv[0], '@'))) { 21042857Smckusick mnttype = MOUNT_NFS; 21152182Smckusick mntname = "nfs"; 21252182Smckusick } 21342253Sbostic ret = mountfs(argv[0], argv[1], updateflg, type, options, 21442253Sbostic (char *)NULL); 21512808Ssam } 21640496Smckusick if ((pidfile = fopen(_PATH_MOUNTDPID, "r")) != NULL) { 21740496Smckusick pid = 0; 21840496Smckusick fscanf(pidfile, "%d", &pid); 21940496Smckusick fclose(pidfile); 22040496Smckusick if (pid > 0) 22140496Smckusick kill(pid, SIGHUP); 22240496Smckusick } 22340496Smckusick exit (ret); 22412808Ssam } 22512808Ssam 22639333Smckusick mountfs(spec, name, flags, type, options, mntopts) 22739131Smckusick char *spec, *name, *type, *options, *mntopts; 22839333Smckusick int flags; 22912808Ssam { 23042253Sbostic union wait status; 23142253Sbostic pid_t pid; 23242253Sbostic int argc, i; 23338070Smckusick struct ufs_args args; 23439131Smckusick char *argp, *argv[50]; 23539322Smckusick char execname[MAXPATHLEN + 1], flagval[12]; 2361057Sbill 23739333Smckusick if (mntopts) 23839333Smckusick getstdopts(mntopts, &flags); 23939316Smckusick if (options) 24039316Smckusick getstdopts(options, &flags); 24139333Smckusick if (type) 24239333Smckusick getstdopts(type, &flags); 24339316Smckusick switch (mnttype) { 24452097Sbostic case MOUNT_LFS: 24539316Smckusick case MOUNT_UFS: 24639333Smckusick if (mntopts) 24739333Smckusick getufsopts(mntopts, &flags); 24839316Smckusick if (options) 24939316Smckusick getufsopts(options, &flags); 25039316Smckusick args.fspec = spec; 25140369Smckusick args.exroot = DEFAULT_ROOTUID; 25241403Smckusick if (flags & MNT_RDONLY) 25341403Smckusick args.exflags = MNT_EXRDONLY; 25440496Smckusick else 25540496Smckusick args.exflags = 0; 25639316Smckusick argp = (caddr_t)&args; 25739316Smckusick break; 25838632Smckusick 25952110Smckusick case MOUNT_MFS: 26039316Smckusick case MOUNT_NFS: 26139322Smckusick default: 26239322Smckusick argv[0] = mntname; 26339329Smckusick argc = 1; 26439322Smckusick if (flags) { 26539322Smckusick argv[argc++] = "-F"; 26639322Smckusick sprintf(flagval, "%d", flags); 26739322Smckusick argv[argc++] = flagval; 26839322Smckusick } 26939333Smckusick if (mntopts) 27039333Smckusick argc += getexecopts(mntopts, &argv[argc]); 27139329Smckusick if (options) 27239329Smckusick argc += getexecopts(options, &argv[argc]); 27339316Smckusick argv[argc++] = spec; 27439316Smckusick argv[argc++] = name; 27539521Smckusick argv[argc++] = NULL; 27639603Smckusick sprintf(execname, "%s/mount_%s", _PATH_EXECDIR, mntname); 27739316Smckusick if (verbose) { 27842253Sbostic (void)printf("exec: %s", execname); 27942253Sbostic for (i = 1; i < argc - 1; i++) 28042253Sbostic (void)printf(" %s", argv[i]); 28142253Sbostic (void)printf("\n"); 28239316Smckusick } 28339316Smckusick if (fake) 28439316Smckusick break; 28542253Sbostic if (pid = vfork()) { 28642253Sbostic if (pid == -1) { 28739322Smckusick perror("mount: vfork starting file system"); 28839316Smckusick return (1); 28939131Smckusick } 29046708Sbostic if (waitpid(pid, (int *)&status, 0) != -1 && 29139316Smckusick WIFEXITED(status) && 29239316Smckusick WEXITSTATUS(status) != 0) 29339316Smckusick return (WEXITSTATUS(status)); 29439322Smckusick spec = mntname; 29539316Smckusick goto out; 29639316Smckusick } 29739322Smckusick execve(execname, argv, envp); 29845689Smckusick (void) fprintf(stderr, "mount: cannot exec %s for %s: ", 29939603Smckusick execname, name); 30042253Sbostic perror((char *)NULL); 30139316Smckusick exit (1); 30239316Smckusick /* NOTREACHED */ 30338632Smckusick 30439316Smckusick } 30539316Smckusick if (!fake && mount(mnttype, name, flags, argp)) { 30645689Smckusick (void) fprintf(stderr, "%s on %s: ", spec, name); 30739316Smckusick switch (errno) { 30839316Smckusick case EMFILE: 30945689Smckusick (void) fprintf(stderr, "Mount table full\n"); 31039316Smckusick break; 31139316Smckusick case EINVAL: 31241403Smckusick if (flags & MNT_UPDATE) 31345689Smckusick (void) fprintf(stderr, "Specified device %s\n", 31445689Smckusick "does not match mounted device"); 31545569Skarels else if (mnttype == MOUNT_UFS) 31645689Smckusick (void) fprintf(stderr, "Bogus super block\n"); 31739333Smckusick else 31845569Skarels perror((char *)NULL); 31939316Smckusick break; 32039316Smckusick default: 32139316Smckusick perror((char *)NULL); 32239316Smckusick break; 3234460Sroot } 32439316Smckusick return(1); 3254460Sroot } 32635339Sbostic 32739131Smckusick out: 32812808Ssam if (verbose) 32939316Smckusick prmount(spec, name, flags); 33035339Sbostic 33142253Sbostic return(0); 3321057Sbill } 33335339Sbostic 33446708Sbostic static void 33539316Smckusick prmount(spec, name, flags) 33639316Smckusick char *spec, *name; 33742253Sbostic register short flags; 33835339Sbostic { 33942253Sbostic register int first; 34038632Smckusick 34142253Sbostic (void)printf("%s on %s", spec, name); 34242253Sbostic if (!(flags & MNT_VISFLAGMASK)) { 34342253Sbostic (void)printf("\n"); 34442253Sbostic return; 34542253Sbostic } 34642253Sbostic first = 0; 34742253Sbostic #define PR(msg) (void)printf("%s%s", !first++ ? " (" : ", ", msg) 34841403Smckusick if (flags & MNT_RDONLY) 34942253Sbostic PR("read-only"); 35041403Smckusick if (flags & MNT_NOEXEC) 35142253Sbostic PR("noexec"); 35241403Smckusick if (flags & MNT_NOSUID) 35342253Sbostic PR("nosuid"); 35441403Smckusick if (flags & MNT_NODEV) 35542253Sbostic PR("nodev"); 35641403Smckusick if (flags & MNT_SYNCHRONOUS) 35742253Sbostic PR("synchronous"); 35841403Smckusick if (flags & MNT_QUOTA) 35942253Sbostic PR("with quotas"); 36041403Smckusick if (flags & MNT_LOCAL) 36142253Sbostic PR("local"); 36241403Smckusick if (flags & MNT_EXPORTED) 36352110Smckusick PR("NFS exported"); 36442253Sbostic (void)printf(")\n"); 36535339Sbostic } 36635339Sbostic 36739133Smckusick getmnttype(fstype) 36839133Smckusick char *fstype; 36939133Smckusick { 37039133Smckusick 37139322Smckusick mntname = fstype; 37239133Smckusick if (!strcmp(fstype, "ufs")) 37339133Smckusick return (MOUNT_UFS); 37439133Smckusick if (!strcmp(fstype, "nfs")) 37539133Smckusick return (MOUNT_NFS); 37639133Smckusick if (!strcmp(fstype, "mfs")) 37739133Smckusick return (MOUNT_MFS); 37852097Sbostic if (!strcmp(fstype, "lfs")) 37952097Sbostic return (MOUNT_LFS); 38039133Smckusick return (0); 38139133Smckusick } 38239133Smckusick 38338632Smckusick usage() 38435339Sbostic { 38540871Smckusick 38645689Smckusick (void) fprintf(stderr, 38745689Smckusick "usage:\n mount %s %s\n mount %s\n mount %s\n", 38852110Smckusick "[ -frwu ] [ -t ufs | external_type ]", 38940871Smckusick "[ -o options ] special node", 39052110Smckusick "[ -afrwu ] [ -t ufs | external_type ]", 39140871Smckusick "[ -frwu ] special | node"); 39235339Sbostic exit(1); 39335339Sbostic } 39435339Sbostic 39539316Smckusick getstdopts(options, flagp) 39639316Smckusick char *options; 39742253Sbostic int *flagp; 39839316Smckusick { 39939316Smckusick register char *opt; 40039316Smckusick int negative; 40142253Sbostic char optbuf[BUFSIZ]; 40239316Smckusick 40342253Sbostic (void)strcpy(optbuf, options); 40442253Sbostic for (opt = strtok(optbuf, ","); opt; opt = strtok((char *)NULL, ",")) { 40539316Smckusick if (opt[0] == 'n' && opt[1] == 'o') { 40639316Smckusick negative++; 40739316Smckusick opt += 2; 40839316Smckusick } else { 40939316Smckusick negative = 0; 41039316Smckusick } 41139333Smckusick if (!negative && !strcasecmp(opt, FSTAB_RO)) { 41241403Smckusick *flagp |= MNT_RDONLY; 41339333Smckusick continue; 41439333Smckusick } 41539333Smckusick if (!negative && !strcasecmp(opt, FSTAB_RW)) { 41641403Smckusick *flagp &= ~MNT_RDONLY; 41739333Smckusick continue; 41839333Smckusick } 41939316Smckusick if (!strcasecmp(opt, "exec")) { 42039316Smckusick if (negative) 42141403Smckusick *flagp |= MNT_NOEXEC; 42239333Smckusick else 42341403Smckusick *flagp &= ~MNT_NOEXEC; 42439316Smckusick continue; 42539316Smckusick } 42639316Smckusick if (!strcasecmp(opt, "suid")) { 42739316Smckusick if (negative) 42841403Smckusick *flagp |= MNT_NOSUID; 42939333Smckusick else 43041403Smckusick *flagp &= ~MNT_NOSUID; 43139316Smckusick continue; 43239316Smckusick } 43339316Smckusick if (!strcasecmp(opt, "dev")) { 43439316Smckusick if (negative) 43541403Smckusick *flagp |= MNT_NODEV; 43639333Smckusick else 43741403Smckusick *flagp &= ~MNT_NODEV; 43839316Smckusick continue; 43939316Smckusick } 44039316Smckusick if (!strcasecmp(opt, "synchronous")) { 44139316Smckusick if (!negative) 44241403Smckusick *flagp |= MNT_SYNCHRONOUS; 44339333Smckusick else 44441403Smckusick *flagp &= ~MNT_SYNCHRONOUS; 44539316Smckusick continue; 44639316Smckusick } 44739316Smckusick } 44839316Smckusick } 44939316Smckusick 45042253Sbostic /* ARGSUSED */ 45139131Smckusick getufsopts(options, flagp) 45239131Smckusick char *options; 45342253Sbostic int *flagp; 45439131Smckusick { 45539131Smckusick return; 45639131Smckusick } 45739131Smckusick 45839329Smckusick getexecopts(options, argv) 45939131Smckusick char *options; 46039131Smckusick char **argv; 46139131Smckusick { 46239131Smckusick register int argc = 0; 46339131Smckusick register char *opt; 46439131Smckusick 46542253Sbostic for (opt = strtok(options, ","); opt; opt = strtok((char *)NULL, ",")) { 46639131Smckusick if (opt[0] != '-') 46739131Smckusick continue; 46839131Smckusick argv[argc++] = opt; 46939131Smckusick if (opt[2] == '\0' || opt[2] != '=') 47039131Smckusick continue; 47139131Smckusick opt[2] = '\0'; 47239131Smckusick argv[argc++] = &opt[3]; 47339131Smckusick } 47439131Smckusick return (argc); 47539131Smckusick } 47639131Smckusick 47739465Smckusick struct statfs * 47839465Smckusick getmntpt(name) 47939465Smckusick char *name; 48039465Smckusick { 48139465Smckusick long mntsize; 48239465Smckusick register long i; 48339465Smckusick struct statfs *mntbuf; 48439465Smckusick 48540337Smckusick mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 48639465Smckusick for (i = 0; i < mntsize; i++) { 48739465Smckusick if (!strcmp(mntbuf[i].f_mntfromname, name) || 48839465Smckusick !strcmp(mntbuf[i].f_mntonname, name)) 48939465Smckusick return (&mntbuf[i]); 49039465Smckusick } 49139465Smckusick return ((struct statfs *)0); 49239465Smckusick } 49339465Smckusick 49440051Smckusick static int skipvfs; 49540051Smckusick 49640051Smckusick badvfstype(vfstype, vfslist) 49742253Sbostic short vfstype; 49840051Smckusick char **vfslist; 49940051Smckusick { 50040051Smckusick 50140051Smckusick if (vfslist == 0) 50240051Smckusick return(0); 50340051Smckusick while (*vfslist) { 50440844Smckusick if (vfstype == getmnttype(*vfslist)) 50540051Smckusick return(skipvfs); 50640051Smckusick vfslist++; 50740051Smckusick } 50840051Smckusick return (!skipvfs); 50940051Smckusick } 51040051Smckusick 51140844Smckusick badvfsname(vfsname, vfslist) 51240844Smckusick char *vfsname; 51340844Smckusick char **vfslist; 51440844Smckusick { 51540844Smckusick 51640844Smckusick if (vfslist == 0) 51740844Smckusick return(0); 51840844Smckusick while (*vfslist) { 51940844Smckusick if (strcmp(vfsname, *vfslist) == 0) 52040844Smckusick return(skipvfs); 52140844Smckusick vfslist++; 52240844Smckusick } 52340844Smckusick return (!skipvfs); 52440844Smckusick } 52540844Smckusick 52640051Smckusick char ** 52740051Smckusick makevfslist(fslist) 52840051Smckusick char *fslist; 52940051Smckusick { 53040051Smckusick register char **av, *nextcp; 53140051Smckusick register int i; 53240051Smckusick 53340051Smckusick if (fslist == NULL) 53440051Smckusick return (NULL); 53540051Smckusick if (fslist[0] == 'n' && fslist[1] == 'o') { 53640051Smckusick fslist += 2; 53740051Smckusick skipvfs = 1; 53840051Smckusick } 53940051Smckusick for (i = 0, nextcp = fslist; *nextcp; nextcp++) 54040051Smckusick if (*nextcp == ',') 54140051Smckusick i++; 54242253Sbostic av = (char **)malloc((size_t)(i+2) * sizeof(char *)); 54340051Smckusick if (av == NULL) 54440051Smckusick return (NULL); 54540051Smckusick nextcp = fslist; 54640051Smckusick i = 0; 54740051Smckusick av[i++] = nextcp; 54840051Smckusick while (nextcp = index(nextcp, ',')) { 54940051Smckusick *nextcp++ = '\0'; 55040051Smckusick av[i++] = nextcp; 55140051Smckusick } 55240051Smckusick av[i++] = 0; 55340051Smckusick return (av); 55440051Smckusick } 555