xref: /csrg-svn/usr.sbin/quotaon/quotaon.c (revision 41409)
121518Smckusick /*
2*41409Smckusick  * Copyright (c) 1980, 1990 Regents of the University of California.
334365Sbostic  * All rights reserved.
434365Sbostic  *
5*41409Smckusick  * This code is derived from software contributed to Berkeley by
6*41409Smckusick  * Robert Elz at The University of Melbourne.
7*41409Smckusick  *
834365Sbostic  * Redistribution and use in source and binary forms are permitted
934778Sbostic  * provided that the above copyright notice and this paragraph are
1034778Sbostic  * duplicated in all such forms and that any documentation,
1134778Sbostic  * advertising materials, and other materials related to such
1234778Sbostic  * distribution and use acknowledge that the software was developed
1334778Sbostic  * by the University of California, Berkeley.  The name of the
1434778Sbostic  * University may not be used to endorse or promote products derived
1534778Sbostic  * from this software without specific prior written permission.
1634778Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1734778Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1834778Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1921518Smckusick  */
2021518Smckusick 
2112662Smckusick #ifndef lint
2221518Smckusick char copyright[] =
23*41409Smckusick "@(#) Copyright (c) 1980, 1990 Regents of the University of California.\n\
2421518Smckusick  All rights reserved.\n";
2534365Sbostic #endif /* not lint */
2612809Ssam 
2721518Smckusick #ifndef lint
28*41409Smckusick static char sccsid[] = "@(#)quotaon.c	5.7 (Berkeley) 05/04/90";
2934365Sbostic #endif /* not lint */
3021518Smckusick 
3112662Smckusick /*
3212670Smckusick  * Turn quota on/off for a filesystem.
3312662Smckusick  */
3412662Smckusick #include <sys/param.h>
3512809Ssam #include <sys/file.h>
3639844Smckusick #include <sys/mount.h>
37*41409Smckusick #include <ufs/quota.h>
3812662Smckusick #include <stdio.h>
3912662Smckusick #include <fstab.h>
4012662Smckusick 
41*41409Smckusick int	aflag;		/* all file systems */
42*41409Smckusick int	gflag;		/* operate on group quotas */
43*41409Smckusick int	uflag;		/* operate on user quotas */
4412662Smckusick int	vflag;		/* verbose */
4512662Smckusick 
4612662Smckusick main(argc, argv)
4712670Smckusick 	int argc;
4812662Smckusick 	char **argv;
4912662Smckusick {
5012662Smckusick 	register struct fstab *fs;
51*41409Smckusick 	char ch, *whoami, *rindex();
52*41409Smckusick 	long argnum, done = 0;
53*41409Smckusick 	int i, offmode = 0, errs = 0;
54*41409Smckusick 	extern char *optarg;
55*41409Smckusick 	extern int optind;
5612662Smckusick 
5712670Smckusick 	whoami = rindex(*argv, '/') + 1;
5812670Smckusick 	if (whoami == (char *)1)
5912670Smckusick 		whoami = *argv;
6012670Smckusick 	if (strcmp(whoami, "quotaoff") == 0)
6112670Smckusick 		offmode++;
6212670Smckusick 	else if (strcmp(whoami, "quotaon") != 0) {
6312670Smckusick 		fprintf(stderr, "Name must be quotaon or quotaoff not %s\n",
6412670Smckusick 			whoami);
6512670Smckusick 		exit(1);
6612670Smckusick 	}
67*41409Smckusick 	while ((ch = getopt(argc, argv, "avug")) != EOF) {
68*41409Smckusick 		switch(ch) {
69*41409Smckusick 		case 'a':
70*41409Smckusick 			aflag++;
71*41409Smckusick 			break;
72*41409Smckusick 		case 'g':
73*41409Smckusick 			gflag++;
74*41409Smckusick 			break;
75*41409Smckusick 		case 'u':
76*41409Smckusick 			uflag++;
77*41409Smckusick 			break;
78*41409Smckusick 		case 'v':
79*41409Smckusick 			vflag++;
80*41409Smckusick 			break;
81*41409Smckusick 		default:
82*41409Smckusick 			usage(whoami);
83*41409Smckusick 		}
8412662Smckusick 	}
85*41409Smckusick 	argc -= optind;
86*41409Smckusick 	argv += optind;
87*41409Smckusick 	if (argc <= 0 && !aflag)
88*41409Smckusick 		usage(whoami);
89*41409Smckusick 	if (!gflag && !uflag) {
90*41409Smckusick 		gflag++;
91*41409Smckusick 		uflag++;
9212662Smckusick 	}
9312662Smckusick 	setfsent();
9412662Smckusick 	while ((fs = getfsent()) != NULL) {
95*41409Smckusick 		if (aflag) {
96*41409Smckusick 			if (gflag && hasquota(fs->fs_mntops, GRPQUOTA))
97*41409Smckusick 				errs += quotaonoff(fs, offmode, GRPQUOTA);
98*41409Smckusick 			if (uflag && hasquota(fs->fs_mntops, USRQUOTA))
99*41409Smckusick 				errs += quotaonoff(fs, offmode, USRQUOTA);
10012662Smckusick 			continue;
101*41409Smckusick 		}
102*41409Smckusick 		if ((argnum = oneof(fs->fs_file, argv, argc) >= 0) ||
103*41409Smckusick 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
104*41409Smckusick 			done |= 1 << argnum;
105*41409Smckusick 			if (gflag && hasquota(fs->fs_mntops, GRPQUOTA))
106*41409Smckusick 				errs += quotaonoff(fs, offmode, GRPQUOTA);
107*41409Smckusick 			if (uflag && hasquota(fs->fs_mntops, USRQUOTA))
108*41409Smckusick 				errs += quotaonoff(fs, offmode, USRQUOTA);
109*41409Smckusick 		}
11012662Smckusick 	}
11112662Smckusick 	endfsent();
11212723Smckusick 	for (i = 0; i < argc; i++)
11312723Smckusick 		if ((done & (1 << i)) == 0)
11437983Sbostic 			fprintf(stderr, "%s not found in fstab\n",
11512723Smckusick 				argv[i]);
11612670Smckusick 	exit(errs);
11712662Smckusick }
11812662Smckusick 
119*41409Smckusick usage(whoami)
120*41409Smckusick 	char *whoami;
121*41409Smckusick {
122*41409Smckusick 
123*41409Smckusick 	fprintf(stderr, "Usage:\n\t%s [-g] [-u] [-v] -a\n", whoami);
124*41409Smckusick 	fprintf(stderr, "\t%s [-g] [-u] [-v] filesys ...\n", whoami);
125*41409Smckusick 	exit(1);
126*41409Smckusick }
127*41409Smckusick 
128*41409Smckusick quotaonoff(fs, offmode, type)
12912809Ssam 	register struct fstab *fs;
130*41409Smckusick 	int offmode, type;
13112809Ssam {
132*41409Smckusick 	char	quotafile[MAXPATHLEN + 1];
13312809Ssam 
13412809Ssam 	if (strcmp(fs->fs_file, "/") && readonly(fs))
13512809Ssam 		return (1);
13612809Ssam 	if (offmode) {
137*41409Smckusick 		if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0)
13812809Ssam 			goto bad;
13912809Ssam 		if (vflag)
14012809Ssam 			printf("%s: quotas turned off\n", fs->fs_file);
14112809Ssam 		return (0);
14212809Ssam 	}
143*41409Smckusick 	(void) sprintf(quotafile, "%s/%s.%s", fs->fs_file, qfname,
144*41409Smckusick 	    qfextension[type]);
145*41409Smckusick 	if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, quotafile) < 0)
14612809Ssam 		goto bad;
14712809Ssam 	if (vflag)
148*41409Smckusick 		printf("%s: %s quotas turned on\n", fs->fs_file,
149*41409Smckusick 		    qfextension[type]);
15012809Ssam 	return (0);
15112809Ssam bad:
15212809Ssam 	fprintf(stderr, "setquota: ");
153*41409Smckusick 	perror(fs->fs_file);
15412809Ssam 	return (1);
15512809Ssam }
15612809Ssam 
157*41409Smckusick /*
158*41409Smckusick  * Check to see if target appears in list of size cnt.
159*41409Smckusick  */
160*41409Smckusick oneof(target, list, cnt)
161*41409Smckusick 	register char *target, *list[];
162*41409Smckusick 	int cnt;
16312662Smckusick {
16412662Smckusick 	register int i;
16512662Smckusick 
166*41409Smckusick 	for (i = 0; i < cnt; i++)
167*41409Smckusick 		if (strcmp(target, list[i]) == 0)
168*41409Smckusick 			return (i);
169*41409Smckusick 	return (-1);
170*41409Smckusick }
171*41409Smckusick 
172*41409Smckusick /*
173*41409Smckusick  * Check to see if a particular quota is to be enabled.
174*41409Smckusick  */
175*41409Smckusick hasquota(options, type)
176*41409Smckusick 	char *options;
177*41409Smckusick 	int type;
178*41409Smckusick {
179*41409Smckusick 	register char *opt;
180*41409Smckusick 	char buf[BUFSIZ];
181*41409Smckusick 	char *strtok();
182*41409Smckusick 	static char initname, usrname[100], grpname[100];
183*41409Smckusick 
184*41409Smckusick 	if (!initname) {
185*41409Smckusick 		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
186*41409Smckusick 		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
187*41409Smckusick 		initname = 1;
188*41409Smckusick 	}
189*41409Smckusick 	strcpy(buf, options);
190*41409Smckusick 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
191*41409Smckusick 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
192*41409Smckusick 			return(1);
193*41409Smckusick 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
194*41409Smckusick 			return(1);
195*41409Smckusick 	}
19612662Smckusick 	return (0);
19712662Smckusick }
19812809Ssam 
19912809Ssam /*
20012809Ssam  * Verify file system is mounted and not readonly.
20112809Ssam  */
20212809Ssam readonly(fs)
20312809Ssam 	register struct fstab *fs;
20412809Ssam {
20539844Smckusick 	struct statfs fsbuf;
20612809Ssam 
20739844Smckusick 	if (statfs(fs->fs_file, &fsbuf) < 0 ||
20839844Smckusick 	    strcmp(fsbuf.f_mntonname, fs->fs_file) ||
20939844Smckusick 	    strcmp(fsbuf.f_mntfromname, fs->fs_spec)) {
21039844Smckusick 		printf("%s: not mounted\n", fs->fs_file);
21139844Smckusick 		return (1);
21212809Ssam 	}
21339844Smckusick 	if (fsbuf.f_flags & M_RDONLY) {
21439844Smckusick 		printf("%s: mounted read-only\n", fs->fs_file);
21539844Smckusick 		return (1);
21639844Smckusick 	}
21739844Smckusick 	return (0);
21812809Ssam }
219