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*52110Smckusick static char sccsid[] = "@(#)mount.c 5.47 (Berkeley) 01/06/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> 24*52110Smckusick #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; 14540124Smckusick if (!strcmp(mntbuf->f_mntfromname, "root_device")) { 14640124Smckusick fs = getfsfile("/"); 14740124Smckusick strcpy(mntbuf->f_mntfromname, fs->fs_spec); 14840124Smckusick } 14940496Smckusick ret = mountfs(mntbuf->f_mntfromname, mntbuf->f_mntonname, 15042253Sbostic updateflg, type, options, (char *)NULL); 15140496Smckusick } else if (argc == 1) { 15235339Sbostic if (!(fs = getfsfile(*argv)) && !(fs = getfsspec(*argv))) { 15345689Smckusick (void) fprintf(stderr, 15435339Sbostic "mount: unknown special file or file system %s.\n", 15535339Sbostic *argv); 15635339Sbostic exit(1); 15735339Sbostic } 15835339Sbostic if (BADTYPE(fs->fs_type)) { 15945689Smckusick (void) fprintf(stderr, 16035339Sbostic "mount: %s has unknown file system type.\n", *argv); 16135339Sbostic exit(1); 16235339Sbostic } 16339333Smckusick mnttype = getmnttype(fs->fs_vfstype); 16440496Smckusick ret = mountfs(fs->fs_spec, fs->fs_file, updateflg, 16540496Smckusick type, options, fs->fs_mntops); 16640496Smckusick } else if (argc != 2) { 16740496Smckusick usage(); 16840496Smckusick ret = 1; 16940496Smckusick } else { 17042857Smckusick /* 17142857Smckusick * If -t flag has not been specified, and spec 17242857Smckusick * contains either a ':' or a '@' then assume that 17342857Smckusick * an NFS filesystem is being specified ala Sun. 17442857Smckusick */ 17542857Smckusick if (vfslist == (char **)0 && 17642857Smckusick (index(argv[0], ':') || index(argv[0], '@'))) 17742857Smckusick mnttype = MOUNT_NFS; 17842253Sbostic ret = mountfs(argv[0], argv[1], updateflg, type, options, 17942253Sbostic (char *)NULL); 18012808Ssam } 18140496Smckusick if ((pidfile = fopen(_PATH_MOUNTDPID, "r")) != NULL) { 18240496Smckusick pid = 0; 18340496Smckusick fscanf(pidfile, "%d", &pid); 18440496Smckusick fclose(pidfile); 18540496Smckusick if (pid > 0) 18640496Smckusick kill(pid, SIGHUP); 18740496Smckusick } 18840496Smckusick exit (ret); 18912808Ssam } 19012808Ssam 19139333Smckusick mountfs(spec, name, flags, type, options, mntopts) 19239131Smckusick char *spec, *name, *type, *options, *mntopts; 19339333Smckusick int flags; 19412808Ssam { 19542253Sbostic union wait status; 19642253Sbostic pid_t pid; 19742253Sbostic int argc, i; 19838070Smckusick struct ufs_args args; 19939131Smckusick char *argp, *argv[50]; 20039322Smckusick char execname[MAXPATHLEN + 1], flagval[12]; 2011057Sbill 20239333Smckusick if (mntopts) 20339333Smckusick getstdopts(mntopts, &flags); 20439316Smckusick if (options) 20539316Smckusick getstdopts(options, &flags); 20639333Smckusick if (type) 20739333Smckusick getstdopts(type, &flags); 20839316Smckusick switch (mnttype) { 20952097Sbostic case MOUNT_LFS: 21039316Smckusick case MOUNT_UFS: 21139333Smckusick if (mntopts) 21239333Smckusick getufsopts(mntopts, &flags); 21339316Smckusick if (options) 21439316Smckusick getufsopts(options, &flags); 21539316Smckusick args.fspec = spec; 21640369Smckusick args.exroot = DEFAULT_ROOTUID; 21741403Smckusick if (flags & MNT_RDONLY) 21841403Smckusick args.exflags = MNT_EXRDONLY; 21940496Smckusick else 22040496Smckusick args.exflags = 0; 22139316Smckusick argp = (caddr_t)&args; 22239316Smckusick break; 22338632Smckusick 224*52110Smckusick case MOUNT_MFS: 22539316Smckusick case MOUNT_NFS: 22639322Smckusick default: 22739322Smckusick argv[0] = mntname; 22839329Smckusick argc = 1; 22939322Smckusick if (flags) { 23039322Smckusick argv[argc++] = "-F"; 23139322Smckusick sprintf(flagval, "%d", flags); 23239322Smckusick argv[argc++] = flagval; 23339322Smckusick } 23439333Smckusick if (mntopts) 23539333Smckusick argc += getexecopts(mntopts, &argv[argc]); 23639329Smckusick if (options) 23739329Smckusick argc += getexecopts(options, &argv[argc]); 23839316Smckusick argv[argc++] = spec; 23939316Smckusick argv[argc++] = name; 24039521Smckusick argv[argc++] = NULL; 24139603Smckusick sprintf(execname, "%s/mount_%s", _PATH_EXECDIR, mntname); 24239316Smckusick if (verbose) { 24342253Sbostic (void)printf("exec: %s", execname); 24442253Sbostic for (i = 1; i < argc - 1; i++) 24542253Sbostic (void)printf(" %s", argv[i]); 24642253Sbostic (void)printf("\n"); 24739316Smckusick } 24839316Smckusick if (fake) 24939316Smckusick break; 25042253Sbostic if (pid = vfork()) { 25142253Sbostic if (pid == -1) { 25239322Smckusick perror("mount: vfork starting file system"); 25339316Smckusick return (1); 25439131Smckusick } 25546708Sbostic if (waitpid(pid, (int *)&status, 0) != -1 && 25639316Smckusick WIFEXITED(status) && 25739316Smckusick WEXITSTATUS(status) != 0) 25839316Smckusick return (WEXITSTATUS(status)); 25939322Smckusick spec = mntname; 26039316Smckusick goto out; 26139316Smckusick } 26239322Smckusick execve(execname, argv, envp); 26345689Smckusick (void) fprintf(stderr, "mount: cannot exec %s for %s: ", 26439603Smckusick execname, name); 26542253Sbostic perror((char *)NULL); 26639316Smckusick exit (1); 26739316Smckusick /* NOTREACHED */ 26838632Smckusick 26939316Smckusick } 27039316Smckusick if (!fake && mount(mnttype, name, flags, argp)) { 27145689Smckusick (void) fprintf(stderr, "%s on %s: ", spec, name); 27239316Smckusick switch (errno) { 27339316Smckusick case EMFILE: 27445689Smckusick (void) fprintf(stderr, "Mount table full\n"); 27539316Smckusick break; 27639316Smckusick case EINVAL: 27741403Smckusick if (flags & MNT_UPDATE) 27845689Smckusick (void) fprintf(stderr, "Specified device %s\n", 27945689Smckusick "does not match mounted device"); 28045569Skarels else if (mnttype == MOUNT_UFS) 28145689Smckusick (void) fprintf(stderr, "Bogus super block\n"); 28239333Smckusick else 28345569Skarels perror((char *)NULL); 28439316Smckusick break; 28539316Smckusick default: 28639316Smckusick perror((char *)NULL); 28739316Smckusick break; 2884460Sroot } 28939316Smckusick return(1); 2904460Sroot } 29135339Sbostic 29239131Smckusick out: 29312808Ssam if (verbose) 29439316Smckusick prmount(spec, name, flags); 29535339Sbostic 29642253Sbostic return(0); 2971057Sbill } 29835339Sbostic 29946708Sbostic static void 30039316Smckusick prmount(spec, name, flags) 30139316Smckusick char *spec, *name; 30242253Sbostic register short flags; 30335339Sbostic { 30442253Sbostic register int first; 30538632Smckusick 30642253Sbostic (void)printf("%s on %s", spec, name); 30742253Sbostic if (!(flags & MNT_VISFLAGMASK)) { 30842253Sbostic (void)printf("\n"); 30942253Sbostic return; 31042253Sbostic } 31142253Sbostic first = 0; 31242253Sbostic #define PR(msg) (void)printf("%s%s", !first++ ? " (" : ", ", msg) 31341403Smckusick if (flags & MNT_RDONLY) 31442253Sbostic PR("read-only"); 31541403Smckusick if (flags & MNT_NOEXEC) 31642253Sbostic PR("noexec"); 31741403Smckusick if (flags & MNT_NOSUID) 31842253Sbostic PR("nosuid"); 31941403Smckusick if (flags & MNT_NODEV) 32042253Sbostic PR("nodev"); 32141403Smckusick if (flags & MNT_SYNCHRONOUS) 32242253Sbostic PR("synchronous"); 32341403Smckusick if (flags & MNT_QUOTA) 32442253Sbostic PR("with quotas"); 32541403Smckusick if (flags & MNT_LOCAL) 32642253Sbostic PR("local"); 32741403Smckusick if (flags & MNT_EXPORTED) 328*52110Smckusick PR("NFS exported"); 32942253Sbostic (void)printf(")\n"); 33035339Sbostic } 33135339Sbostic 33239133Smckusick getmnttype(fstype) 33339133Smckusick char *fstype; 33439133Smckusick { 33539133Smckusick 33639322Smckusick mntname = fstype; 33739133Smckusick if (!strcmp(fstype, "ufs")) 33839133Smckusick return (MOUNT_UFS); 33939133Smckusick if (!strcmp(fstype, "nfs")) 34039133Smckusick return (MOUNT_NFS); 34139133Smckusick if (!strcmp(fstype, "mfs")) 34239133Smckusick return (MOUNT_MFS); 34352097Sbostic if (!strcmp(fstype, "lfs")) 34452097Sbostic return (MOUNT_LFS); 34539133Smckusick return (0); 34639133Smckusick } 34739133Smckusick 34838632Smckusick usage() 34935339Sbostic { 35040871Smckusick 35145689Smckusick (void) fprintf(stderr, 35245689Smckusick "usage:\n mount %s %s\n mount %s\n mount %s\n", 353*52110Smckusick "[ -frwu ] [ -t ufs | external_type ]", 35440871Smckusick "[ -o options ] special node", 355*52110Smckusick "[ -afrwu ] [ -t ufs | external_type ]", 35640871Smckusick "[ -frwu ] special | node"); 35735339Sbostic exit(1); 35835339Sbostic } 35935339Sbostic 36039316Smckusick getstdopts(options, flagp) 36139316Smckusick char *options; 36242253Sbostic int *flagp; 36339316Smckusick { 36439316Smckusick register char *opt; 36539316Smckusick int negative; 36642253Sbostic char optbuf[BUFSIZ]; 36739316Smckusick 36842253Sbostic (void)strcpy(optbuf, options); 36942253Sbostic for (opt = strtok(optbuf, ","); opt; opt = strtok((char *)NULL, ",")) { 37039316Smckusick if (opt[0] == 'n' && opt[1] == 'o') { 37139316Smckusick negative++; 37239316Smckusick opt += 2; 37339316Smckusick } else { 37439316Smckusick negative = 0; 37539316Smckusick } 37639333Smckusick if (!negative && !strcasecmp(opt, FSTAB_RO)) { 37741403Smckusick *flagp |= MNT_RDONLY; 37839333Smckusick continue; 37939333Smckusick } 38039333Smckusick if (!negative && !strcasecmp(opt, FSTAB_RW)) { 38141403Smckusick *flagp &= ~MNT_RDONLY; 38239333Smckusick continue; 38339333Smckusick } 38439316Smckusick if (!strcasecmp(opt, "exec")) { 38539316Smckusick if (negative) 38641403Smckusick *flagp |= MNT_NOEXEC; 38739333Smckusick else 38841403Smckusick *flagp &= ~MNT_NOEXEC; 38939316Smckusick continue; 39039316Smckusick } 39139316Smckusick if (!strcasecmp(opt, "suid")) { 39239316Smckusick if (negative) 39341403Smckusick *flagp |= MNT_NOSUID; 39439333Smckusick else 39541403Smckusick *flagp &= ~MNT_NOSUID; 39639316Smckusick continue; 39739316Smckusick } 39839316Smckusick if (!strcasecmp(opt, "dev")) { 39939316Smckusick if (negative) 40041403Smckusick *flagp |= MNT_NODEV; 40139333Smckusick else 40241403Smckusick *flagp &= ~MNT_NODEV; 40339316Smckusick continue; 40439316Smckusick } 40539316Smckusick if (!strcasecmp(opt, "synchronous")) { 40639316Smckusick if (!negative) 40741403Smckusick *flagp |= MNT_SYNCHRONOUS; 40839333Smckusick else 40941403Smckusick *flagp &= ~MNT_SYNCHRONOUS; 41039316Smckusick continue; 41139316Smckusick } 41239316Smckusick } 41339316Smckusick } 41439316Smckusick 41542253Sbostic /* ARGSUSED */ 41639131Smckusick getufsopts(options, flagp) 41739131Smckusick char *options; 41842253Sbostic int *flagp; 41939131Smckusick { 42039131Smckusick return; 42139131Smckusick } 42239131Smckusick 42339329Smckusick getexecopts(options, argv) 42439131Smckusick char *options; 42539131Smckusick char **argv; 42639131Smckusick { 42739131Smckusick register int argc = 0; 42839131Smckusick register char *opt; 42939131Smckusick 43042253Sbostic for (opt = strtok(options, ","); opt; opt = strtok((char *)NULL, ",")) { 43139131Smckusick if (opt[0] != '-') 43239131Smckusick continue; 43339131Smckusick argv[argc++] = opt; 43439131Smckusick if (opt[2] == '\0' || opt[2] != '=') 43539131Smckusick continue; 43639131Smckusick opt[2] = '\0'; 43739131Smckusick argv[argc++] = &opt[3]; 43839131Smckusick } 43939131Smckusick return (argc); 44039131Smckusick } 44139131Smckusick 44239465Smckusick struct statfs * 44339465Smckusick getmntpt(name) 44439465Smckusick char *name; 44539465Smckusick { 44639465Smckusick long mntsize; 44739465Smckusick register long i; 44839465Smckusick struct statfs *mntbuf; 44939465Smckusick 45040337Smckusick mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 45139465Smckusick for (i = 0; i < mntsize; i++) { 45239465Smckusick if (!strcmp(mntbuf[i].f_mntfromname, name) || 45339465Smckusick !strcmp(mntbuf[i].f_mntonname, name)) 45439465Smckusick return (&mntbuf[i]); 45539465Smckusick } 45639465Smckusick return ((struct statfs *)0); 45739465Smckusick } 45839465Smckusick 45940051Smckusick static int skipvfs; 46040051Smckusick 46140051Smckusick badvfstype(vfstype, vfslist) 46242253Sbostic short vfstype; 46340051Smckusick char **vfslist; 46440051Smckusick { 46540051Smckusick 46640051Smckusick if (vfslist == 0) 46740051Smckusick return(0); 46840051Smckusick while (*vfslist) { 46940844Smckusick if (vfstype == getmnttype(*vfslist)) 47040051Smckusick return(skipvfs); 47140051Smckusick vfslist++; 47240051Smckusick } 47340051Smckusick return (!skipvfs); 47440051Smckusick } 47540051Smckusick 47640844Smckusick badvfsname(vfsname, vfslist) 47740844Smckusick char *vfsname; 47840844Smckusick char **vfslist; 47940844Smckusick { 48040844Smckusick 48140844Smckusick if (vfslist == 0) 48240844Smckusick return(0); 48340844Smckusick while (*vfslist) { 48440844Smckusick if (strcmp(vfsname, *vfslist) == 0) 48540844Smckusick return(skipvfs); 48640844Smckusick vfslist++; 48740844Smckusick } 48840844Smckusick return (!skipvfs); 48940844Smckusick } 49040844Smckusick 49140051Smckusick char ** 49240051Smckusick makevfslist(fslist) 49340051Smckusick char *fslist; 49440051Smckusick { 49540051Smckusick register char **av, *nextcp; 49640051Smckusick register int i; 49740051Smckusick 49840051Smckusick if (fslist == NULL) 49940051Smckusick return (NULL); 50040051Smckusick if (fslist[0] == 'n' && fslist[1] == 'o') { 50140051Smckusick fslist += 2; 50240051Smckusick skipvfs = 1; 50340051Smckusick } 50440051Smckusick for (i = 0, nextcp = fslist; *nextcp; nextcp++) 50540051Smckusick if (*nextcp == ',') 50640051Smckusick i++; 50742253Sbostic av = (char **)malloc((size_t)(i+2) * sizeof(char *)); 50840051Smckusick if (av == NULL) 50940051Smckusick return (NULL); 51040051Smckusick nextcp = fslist; 51140051Smckusick i = 0; 51240051Smckusick av[i++] = nextcp; 51340051Smckusick while (nextcp = index(nextcp, ',')) { 51440051Smckusick *nextcp++ = '\0'; 51540051Smckusick av[i++] = nextcp; 51640051Smckusick } 51740051Smckusick av[i++] = 0; 51840051Smckusick return (av); 51940051Smckusick } 520