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