Annotation of 43BSDReno/usr.sbin/quotaon/quotaon.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1980, 1990 Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * This code is derived from software contributed to Berkeley by
                      6:  * Robert Elz at The University of Melbourne.
                      7:  *
                      8:  * Redistribution and use in source and binary forms are permitted provided
                      9:  * that: (1) source distributions retain this entire copyright notice and
                     10:  * comment, and (2) distributions including binaries display the following
                     11:  * acknowledgement:  ``This product includes software developed by the
                     12:  * University of California, Berkeley and its contributors'' in the
                     13:  * documentation or other materials provided with the distribution and in
                     14:  * all advertising materials mentioning features or use of this software.
                     15:  * Neither the name of the University nor the names of its contributors may
                     16:  * be used to endorse or promote products derived from this software without
                     17:  * specific prior written permission.
                     18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
                     19:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
                     20:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     21:  */
                     22: 
                     23: #ifndef lint
                     24: char copyright[] =
                     25: "@(#) Copyright (c) 1980, 1990 Regents of the University of California.\n\
                     26:  All rights reserved.\n";
                     27: #endif /* not lint */
                     28: 
                     29: #ifndef lint
                     30: static char sccsid[] = "@(#)quotaon.c  5.11 (Berkeley) 6/1/90";
                     31: #endif /* not lint */
                     32: 
                     33: /*
                     34:  * Turn quota on/off for a filesystem.
                     35:  */
                     36: #include <sys/param.h>
                     37: #include <sys/file.h>
                     38: #include <sys/mount.h>
                     39: #include <ufs/quota.h>
                     40: #include <stdio.h>
                     41: #include <fstab.h>
                     42: 
                     43: int    aflag;          /* all file systems */
                     44: int    gflag;          /* operate on group quotas */
                     45: int    uflag;          /* operate on user quotas */
                     46: int    vflag;          /* verbose */
                     47: 
                     48: main(argc, argv)
                     49:        int argc;
                     50:        char **argv;
                     51: {
                     52:        register struct fstab *fs;
                     53:        char ch, *qfnp, *whoami, *rindex();
                     54:        long argnum, done = 0;
                     55:        int i, offmode = 0, errs = 0;
                     56:        extern char *optarg;
                     57:        extern int optind;
                     58: 
                     59:        whoami = rindex(*argv, '/') + 1;
                     60:        if (whoami == (char *)1)
                     61:                whoami = *argv;
                     62:        if (strcmp(whoami, "quotaoff") == 0)
                     63:                offmode++;
                     64:        else if (strcmp(whoami, "quotaon") != 0) {
                     65:                fprintf(stderr, "Name must be quotaon or quotaoff not %s\n",
                     66:                        whoami);
                     67:                exit(1);
                     68:        }
                     69:        while ((ch = getopt(argc, argv, "avug")) != EOF) {
                     70:                switch(ch) {
                     71:                case 'a':
                     72:                        aflag++;
                     73:                        break;
                     74:                case 'g':
                     75:                        gflag++;
                     76:                        break;
                     77:                case 'u':
                     78:                        uflag++;
                     79:                        break;
                     80:                case 'v':
                     81:                        vflag++;
                     82:                        break;
                     83:                default:
                     84:                        usage(whoami);
                     85:                }
                     86:        }
                     87:        argc -= optind;
                     88:        argv += optind;
                     89:        if (argc <= 0 && !aflag)
                     90:                usage(whoami);
                     91:        if (!gflag && !uflag) {
                     92:                gflag++;
                     93:                uflag++;
                     94:        }
                     95:        setfsent();
                     96:        while ((fs = getfsent()) != NULL) {
                     97:                if (strcmp(fs->fs_vfstype, "ufs") ||
                     98:                    strcmp(fs->fs_type, FSTAB_RW))
                     99:                        continue;
                    100:                if (aflag) {
                    101:                        if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
                    102:                                errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
                    103:                        if (uflag && hasquota(fs, USRQUOTA, &qfnp))
                    104:                                errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
                    105:                        continue;
                    106:                }
                    107:                if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
                    108:                    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
                    109:                        done |= 1 << argnum;
                    110:                        if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
                    111:                                errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
                    112:                        if (uflag && hasquota(fs, USRQUOTA, &qfnp))
                    113:                                errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
                    114:                }
                    115:        }
                    116:        endfsent();
                    117:        for (i = 0; i < argc; i++)
                    118:                if ((done & (1 << i)) == 0)
                    119:                        fprintf(stderr, "%s not found in fstab\n",
                    120:                                argv[i]);
                    121:        exit(errs);
                    122: }
                    123: 
                    124: usage(whoami)
                    125:        char *whoami;
                    126: {
                    127: 
                    128:        fprintf(stderr, "Usage:\n\t%s [-g] [-u] [-v] -a\n", whoami);
                    129:        fprintf(stderr, "\t%s [-g] [-u] [-v] filesys ...\n", whoami);
                    130:        exit(1);
                    131: }
                    132: 
                    133: quotaonoff(fs, offmode, type, qfpathname)
                    134:        register struct fstab *fs;
                    135:        int offmode, type;
                    136:        char *qfpathname;
                    137: {
                    138: 
                    139:        if (strcmp(fs->fs_file, "/") && readonly(fs))
                    140:                return (1);
                    141:        if (offmode) {
                    142:                if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0) {
                    143:                        fprintf(stderr, "quotaoff: ");
                    144:                        perror(fs->fs_file);
                    145:                        return (1);
                    146:                }
                    147:                if (vflag)
                    148:                        printf("%s: quotas turned off\n", fs->fs_file);
                    149:                return (0);
                    150:        }
                    151:        if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, qfpathname) < 0) {
                    152:                fprintf(stderr, "quotaon: using %s on", qfpathname);
                    153:                perror(fs->fs_file);
                    154:                return (1);
                    155:        }
                    156:        if (vflag)
                    157:                printf("%s: %s quotas turned on\n", fs->fs_file,
                    158:                    qfextension[type]);
                    159:        return (0);
                    160: }
                    161: 
                    162: /*
                    163:  * Check to see if target appears in list of size cnt.
                    164:  */
                    165: oneof(target, list, cnt)
                    166:        register char *target, *list[];
                    167:        int cnt;
                    168: {
                    169:        register int i;
                    170: 
                    171:        for (i = 0; i < cnt; i++)
                    172:                if (strcmp(target, list[i]) == 0)
                    173:                        return (i);
                    174:        return (-1);
                    175: }
                    176: 
                    177: /*
                    178:  * Check to see if a particular quota is to be enabled.
                    179:  */
                    180: hasquota(fs, type, qfnamep)
                    181:        register struct fstab *fs;
                    182:        int type;
                    183:        char **qfnamep;
                    184: {
                    185:        register char *opt;
                    186:        char *cp, *index(), *strtok();
                    187:        static char initname, usrname[100], grpname[100];
                    188:        static char buf[BUFSIZ];
                    189: 
                    190:        if (!initname) {
                    191:                sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
                    192:                sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
                    193:                initname = 1;
                    194:        }
                    195:        strcpy(buf, fs->fs_mntops);
                    196:        for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
                    197:                if (cp = index(opt, '='))
                    198:                        *cp++ = '\0';
                    199:                if (type == USRQUOTA && strcmp(opt, usrname) == 0)
                    200:                        break;
                    201:                if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
                    202:                        break;
                    203:        }
                    204:        if (!opt)
                    205:                return (0);
                    206:        if (cp) {
                    207:                *qfnamep = cp;
                    208:                return (1);
                    209:        }
                    210:        (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
                    211:        *qfnamep = buf;
                    212:        return (1);
                    213: }
                    214: 
                    215: /*
                    216:  * Verify file system is mounted and not readonly.
                    217:  */
                    218: readonly(fs)
                    219:        register struct fstab *fs;
                    220: {
                    221:        struct statfs fsbuf;
                    222: 
                    223:        if (statfs(fs->fs_file, &fsbuf) < 0 ||
                    224:            strcmp(fsbuf.f_mntonname, fs->fs_file) ||
                    225:            strcmp(fsbuf.f_mntfromname, fs->fs_spec)) {
                    226:                printf("%s: not mounted\n", fs->fs_file);
                    227:                return (1);
                    228:        }
                    229:        if (fsbuf.f_flags & MNT_RDONLY) {
                    230:                printf("%s: mounted read-only\n", fs->fs_file);
                    231:                return (1);
                    232:        }
                    233:        return (0);
                    234: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.