Home | History | Annotate | Line # | Download | only in quotaon
quotaon.c revision 1.13
      1 /*	$NetBSD: quotaon.c,v 1.13 1998/07/27 00:52:03 mycroft 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.13 1998/07/27 00:52:03 mycroft 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 <string.h>
     65 #include <unistd.h>
     66 
     67 char *qfname = QUOTAFILENAME;
     68 char *qfextension[] = INITQFNAMES;
     69 
     70 int	aflag;		/* all file systems */
     71 int	gflag;		/* operate on group quotas */
     72 int	uflag;		/* operate on user quotas */
     73 int	vflag;		/* verbose */
     74 
     75 extern char *__progname;
     76 
     77 int main __P((int, char *[]));
     78 
     79 static void usage __P((void));
     80 static int quotaonoff __P((struct fstab *, int, int, char *));
     81 static int oneof __P((const char *, char *[], int));
     82 static int hasquota __P((struct fstab *, int, char **));
     83 static int readonly __P((struct fstab *));
     84 
     85 int
     86 main(argc, argv)
     87 	int argc;
     88 	char *argv[];
     89 {
     90 	struct fstab *fs;
     91 	char *qfnp;
     92 	long argnum, done = 0;
     93 	int i, offmode = 0, errs = 0;
     94 	int ch;
     95 
     96 	if (strcmp(__progname, "quotaoff") == 0)
     97 		offmode++;
     98 	else if (strcmp(__progname, "quotaon") != 0)
     99 		errx(1, "Name must be quotaon or quotaoff\n");
    100 
    101 	while ((ch = getopt(argc, argv, "avug")) != -1) {
    102 		switch(ch) {
    103 		case 'a':
    104 			aflag++;
    105 			break;
    106 		case 'g':
    107 			gflag++;
    108 			break;
    109 		case 'u':
    110 			uflag++;
    111 			break;
    112 		case 'v':
    113 			vflag++;
    114 			break;
    115 		default:
    116 			usage();
    117 			break;
    118 		}
    119 	}
    120 	argc -= optind;
    121 	argv += optind;
    122 
    123 	if (argc <= 0 && !aflag)
    124 		usage();
    125 
    126 	if (!gflag && !uflag) {
    127 		gflag++;
    128 		uflag++;
    129 	}
    130 	setfsent();
    131 	while ((fs = getfsent()) != NULL) {
    132 		if (strcmp(fs->fs_vfstype, "ffs") ||
    133 		    strcmp(fs->fs_type, FSTAB_RW))
    134 			continue;
    135 		if (aflag) {
    136 			if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
    137 				errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
    138 			if (uflag && hasquota(fs, USRQUOTA, &qfnp))
    139 				errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
    140 			continue;
    141 		}
    142 		if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
    143 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
    144 			done |= 1 << argnum;
    145 			if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
    146 				errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
    147 			if (uflag && hasquota(fs, USRQUOTA, &qfnp))
    148 				errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
    149 		}
    150 	}
    151 	endfsent();
    152 	for (i = 0; i < argc; i++)
    153 		if ((done & (1 << i)) == 0)
    154 			warnx("%s not found in fstab", argv[i]);
    155 	exit(errs);
    156 }
    157 
    158 static void
    159 usage()
    160 {
    161 
    162 	(void) fprintf(stderr, "Usage:\n\t%s [-g] [-u] [-v] -a\n", __progname);
    163 	(void) fprintf(stderr, "\t%s [-g] [-u] [-v] filesys ...\n", __progname);
    164 	exit(1);
    165 }
    166 
    167 static int
    168 quotaonoff(fs, offmode, type, qfpathname)
    169 	struct fstab *fs;
    170 	int offmode, type;
    171 	char *qfpathname;
    172 {
    173 
    174 	if (strcmp(fs->fs_file, "/") && readonly(fs))
    175 		return (1);
    176 	if (offmode) {
    177 		if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0) {
    178 			warn(fs->fs_file);
    179 			return (1);
    180 		}
    181 		if (vflag)
    182 			printf("%s: %s quotas turned off\n",
    183 			    fs->fs_file, qfextension[type]);
    184 		return (0);
    185 	}
    186 	if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, qfpathname) < 0) {
    187 		warn("%s quotas using %s on %s",
    188 		    qfextension[type], qfpathname, fs->fs_file);
    189 		return (1);
    190 	}
    191 	if (vflag)
    192 		printf("%s: %s quotas turned on\n", fs->fs_file,
    193 		    qfextension[type]);
    194 	return (0);
    195 }
    196 
    197 /*
    198  * Check to see if target appears in list of size cnt.
    199  */
    200 static int
    201 oneof(target, list, cnt)
    202 	const char *target;
    203 	char *list[];
    204 	int cnt;
    205 {
    206 	int i;
    207 
    208 	for (i = 0; i < cnt; i++)
    209 		if (strcmp(target, list[i]) == 0)
    210 			return (i);
    211 	return (-1);
    212 }
    213 
    214 /*
    215  * Check to see if a particular quota is to be enabled.
    216  */
    217 static int
    218 hasquota(fs, type, qfnamep)
    219 	struct fstab *fs;
    220 	int type;
    221 	char **qfnamep;
    222 {
    223 	char *opt;
    224 	char *cp = NULL;
    225 	static char initname, usrname[100], grpname[100];
    226 	static char buf[BUFSIZ];
    227 
    228 	if (!initname) {
    229 		(void) snprintf(usrname, sizeof(usrname), "%s%s",
    230 		    qfextension[USRQUOTA], qfname);
    231 		(void) snprintf(grpname, sizeof(grpname), "%s%s",
    232 		    qfextension[GRPQUOTA], qfname);
    233 		initname = 1;
    234 	}
    235 	strcpy(buf, fs->fs_mntops);
    236 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
    237 		if ((cp = strchr(opt, '=')) != NULL)
    238 			*cp++ = '\0';
    239 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
    240 			break;
    241 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
    242 			break;
    243 	}
    244 	if (!opt)
    245 		return (0);
    246 	if (cp) {
    247 		*qfnamep = cp;
    248 		return (1);
    249 	}
    250 	(void) snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file, qfname,
    251 	    qfextension[type]);
    252 	*qfnamep = buf;
    253 	return (1);
    254 }
    255 
    256 /*
    257  * Verify file system is mounted and not readonly.
    258  */
    259 static int
    260 readonly(fs)
    261 	struct fstab *fs;
    262 {
    263 	struct statfs fsbuf;
    264 
    265 	if (statfs(fs->fs_file, &fsbuf) < 0 ||
    266 	    strcmp(fsbuf.f_mntonname, fs->fs_file) ||
    267 	    strcmp(fsbuf.f_mntfromname, fs->fs_spec)) {
    268 		printf("%s: not mounted\n", fs->fs_file);
    269 		return (1);
    270 	}
    271 	if (fsbuf.f_flags & MNT_RDONLY) {
    272 		printf("%s: mounted read-only\n", fs->fs_file);
    273 		return (1);
    274 	}
    275 	return (0);
    276 }
    277