Home | History | Annotate | Line # | Download | only in quotaon
quotaon.c revision 1.7
      1 /*
      2  * Copyright (c) 1980, 1990, 1993
      3  *	The Regents of the University of California.  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, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #ifndef lint
     38 static char copyright[] =
     39 "@(#) Copyright (c) 1980, 1990, 1993\n\
     40 	The Regents of the University of California.  All rights reserved.\n";
     41 #endif /* not lint */
     42 
     43 #ifndef lint
     44 /*static char sccsid[] = "from: @(#)quotaon.c	8.1 (Berkeley) 6/6/93";*/
     45 static char *rcsid = "$Id: quotaon.c,v 1.7 1996/03/30 23:46:04 mark Exp $";
     46 #endif /* not lint */
     47 
     48 /*
     49  * Turn quota on/off for a filesystem.
     50  */
     51 #include <sys/param.h>
     52 #include <sys/file.h>
     53 #include <sys/mount.h>
     54 #include <ufs/ufs/quota.h>
     55 #include <stdio.h>
     56 #include <string.h>
     57 #include <fstab.h>
     58 
     59 char *qfname = QUOTAFILENAME;
     60 char *qfextension[] = INITQFNAMES;
     61 
     62 int	aflag;		/* all file systems */
     63 int	gflag;		/* operate on group quotas */
     64 int	uflag;		/* operate on user quotas */
     65 int	vflag;		/* verbose */
     66 
     67 main(argc, argv)
     68 	int argc;
     69 	char **argv;
     70 {
     71 	register struct fstab *fs;
     72 	char *qfnp, *whoami, *rindex();
     73 	long argnum, done = 0;
     74 	int i, offmode = 0, errs = 0;
     75 	extern char *optarg;
     76 	extern int optind;
     77 	int ch;
     78 
     79 	whoami = rindex(*argv, '/') + 1;
     80 	if (whoami == (char *)1)
     81 		whoami = *argv;
     82 	if (strcmp(whoami, "quotaoff") == 0)
     83 		offmode++;
     84 	else if (strcmp(whoami, "quotaon") != 0) {
     85 		fprintf(stderr, "Name must be quotaon or quotaoff not %s\n",
     86 			whoami);
     87 		exit(1);
     88 	}
     89 	while ((ch = getopt(argc, argv, "avug")) != -1) {
     90 		switch(ch) {
     91 		case 'a':
     92 			aflag++;
     93 			break;
     94 		case 'g':
     95 			gflag++;
     96 			break;
     97 		case 'u':
     98 			uflag++;
     99 			break;
    100 		case 'v':
    101 			vflag++;
    102 			break;
    103 		default:
    104 			usage(whoami);
    105 		}
    106 	}
    107 	argc -= optind;
    108 	argv += optind;
    109 	if (argc <= 0 && !aflag)
    110 		usage(whoami);
    111 	if (!gflag && !uflag) {
    112 		gflag++;
    113 		uflag++;
    114 	}
    115 	setfsent();
    116 	while ((fs = getfsent()) != NULL) {
    117 		if (strcmp(fs->fs_vfstype, "ffs") ||
    118 		    strcmp(fs->fs_type, FSTAB_RW))
    119 			continue;
    120 		if (aflag) {
    121 			if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
    122 				errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
    123 			if (uflag && hasquota(fs, USRQUOTA, &qfnp))
    124 				errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
    125 			continue;
    126 		}
    127 		if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
    128 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
    129 			done |= 1 << argnum;
    130 			if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
    131 				errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
    132 			if (uflag && hasquota(fs, USRQUOTA, &qfnp))
    133 				errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
    134 		}
    135 	}
    136 	endfsent();
    137 	for (i = 0; i < argc; i++)
    138 		if ((done & (1 << i)) == 0)
    139 			fprintf(stderr, "%s not found in fstab\n",
    140 				argv[i]);
    141 	exit(errs);
    142 }
    143 
    144 usage(whoami)
    145 	char *whoami;
    146 {
    147 
    148 	fprintf(stderr, "Usage:\n\t%s [-g] [-u] [-v] -a\n", whoami);
    149 	fprintf(stderr, "\t%s [-g] [-u] [-v] filesys ...\n", whoami);
    150 	exit(1);
    151 }
    152 
    153 quotaonoff(fs, offmode, type, qfpathname)
    154 	register struct fstab *fs;
    155 	int offmode, type;
    156 	char *qfpathname;
    157 {
    158 
    159 	if (strcmp(fs->fs_file, "/") && readonly(fs))
    160 		return (1);
    161 	if (offmode) {
    162 		if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0) {
    163 			fprintf(stderr, "quotaoff: ");
    164 			perror(fs->fs_file);
    165 			return (1);
    166 		}
    167 		if (vflag)
    168 			printf("%s: quotas turned off\n", fs->fs_file);
    169 		return (0);
    170 	}
    171 	if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, qfpathname) < 0) {
    172 		fprintf(stderr, "quotaon: using %s on", qfpathname);
    173 		perror(fs->fs_file);
    174 		return (1);
    175 	}
    176 	if (vflag)
    177 		printf("%s: %s quotas turned on\n", fs->fs_file,
    178 		    qfextension[type]);
    179 	return (0);
    180 }
    181 
    182 /*
    183  * Check to see if target appears in list of size cnt.
    184  */
    185 oneof(target, list, cnt)
    186 	register char *target, *list[];
    187 	int cnt;
    188 {
    189 	register int i;
    190 
    191 	for (i = 0; i < cnt; i++)
    192 		if (strcmp(target, list[i]) == 0)
    193 			return (i);
    194 	return (-1);
    195 }
    196 
    197 /*
    198  * Check to see if a particular quota is to be enabled.
    199  */
    200 hasquota(fs, type, qfnamep)
    201 	register struct fstab *fs;
    202 	int type;
    203 	char **qfnamep;
    204 {
    205 	register char *opt;
    206 	char *cp, *index(), *strtok();
    207 	static char initname, usrname[100], grpname[100];
    208 	static char buf[BUFSIZ];
    209 
    210 	if (!initname) {
    211 		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
    212 		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
    213 		initname = 1;
    214 	}
    215 	strcpy(buf, fs->fs_mntops);
    216 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
    217 		if (cp = index(opt, '='))
    218 			*cp++ = '\0';
    219 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
    220 			break;
    221 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
    222 			break;
    223 	}
    224 	if (!opt)
    225 		return (0);
    226 	if (cp) {
    227 		*qfnamep = cp;
    228 		return (1);
    229 	}
    230 	(void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
    231 	*qfnamep = buf;
    232 	return (1);
    233 }
    234 
    235 /*
    236  * Verify file system is mounted and not readonly.
    237  */
    238 readonly(fs)
    239 	register struct fstab *fs;
    240 {
    241 	struct statfs fsbuf;
    242 
    243 	if (statfs(fs->fs_file, &fsbuf) < 0 ||
    244 	    strcmp(fsbuf.f_mntonname, fs->fs_file) ||
    245 	    strcmp(fsbuf.f_mntfromname, fs->fs_spec)) {
    246 		printf("%s: not mounted\n", fs->fs_file);
    247 		return (1);
    248 	}
    249 	if (fsbuf.f_flags & MNT_RDONLY) {
    250 		printf("%s: mounted read-only\n", fs->fs_file);
    251 		return (1);
    252 	}
    253 	return (0);
    254 }
    255