xref: /csrg-svn/usr.sbin/quotaon/quotaon.c (revision 61867)
121518Smckusick /*
2*61867Sbostic  * Copyright (c) 1980, 1990, 1993
3*61867Sbostic  *	The Regents of the University of California.  All rights reserved.
434365Sbostic  *
541409Smckusick  * This code is derived from software contributed to Berkeley by
641409Smckusick  * Robert Elz at The University of Melbourne.
741409Smckusick  *
842816Sbostic  * %sccs.include.redist.c%
921518Smckusick  */
1021518Smckusick 
1112662Smckusick #ifndef lint
12*61867Sbostic static char copyright[] =
13*61867Sbostic "@(#) Copyright (c) 1980, 1990, 1993\n\
14*61867Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1534365Sbostic #endif /* not lint */
1612809Ssam 
1721518Smckusick #ifndef lint
18*61867Sbostic static char sccsid[] = "@(#)quotaon.c	8.1 (Berkeley) 06/06/93";
1934365Sbostic #endif /* not lint */
2021518Smckusick 
2112662Smckusick /*
2212670Smckusick  * Turn quota on/off for a filesystem.
2312662Smckusick  */
2412662Smckusick #include <sys/param.h>
2512809Ssam #include <sys/file.h>
2639844Smckusick #include <sys/mount.h>
2751632Sbostic #include <ufs/ufs/quota.h>
2812662Smckusick #include <stdio.h>
2912662Smckusick #include <fstab.h>
3012662Smckusick 
3145252Smckusick char *qfname = QUOTAFILENAME;
3245252Smckusick char *qfextension[] = INITQFNAMES;
3345252Smckusick 
3441409Smckusick int	aflag;		/* all file systems */
3541409Smckusick int	gflag;		/* operate on group quotas */
3641409Smckusick int	uflag;		/* operate on user quotas */
3712662Smckusick int	vflag;		/* verbose */
3812662Smckusick 
main(argc,argv)3912662Smckusick main(argc, argv)
4012670Smckusick 	int argc;
4112662Smckusick 	char **argv;
4212662Smckusick {
4312662Smckusick 	register struct fstab *fs;
4441437Smckusick 	char ch, *qfnp, *whoami, *rindex();
4541409Smckusick 	long argnum, done = 0;
4641409Smckusick 	int i, offmode = 0, errs = 0;
4741409Smckusick 	extern char *optarg;
4841409Smckusick 	extern int optind;
4912662Smckusick 
5012670Smckusick 	whoami = rindex(*argv, '/') + 1;
5112670Smckusick 	if (whoami == (char *)1)
5212670Smckusick 		whoami = *argv;
5312670Smckusick 	if (strcmp(whoami, "quotaoff") == 0)
5412670Smckusick 		offmode++;
5512670Smckusick 	else if (strcmp(whoami, "quotaon") != 0) {
5612670Smckusick 		fprintf(stderr, "Name must be quotaon or quotaoff not %s\n",
5712670Smckusick 			whoami);
5812670Smckusick 		exit(1);
5912670Smckusick 	}
6041409Smckusick 	while ((ch = getopt(argc, argv, "avug")) != EOF) {
6141409Smckusick 		switch(ch) {
6241409Smckusick 		case 'a':
6341409Smckusick 			aflag++;
6441409Smckusick 			break;
6541409Smckusick 		case 'g':
6641409Smckusick 			gflag++;
6741409Smckusick 			break;
6841409Smckusick 		case 'u':
6941409Smckusick 			uflag++;
7041409Smckusick 			break;
7141409Smckusick 		case 'v':
7241409Smckusick 			vflag++;
7341409Smckusick 			break;
7441409Smckusick 		default:
7541409Smckusick 			usage(whoami);
7641409Smckusick 		}
7712662Smckusick 	}
7841409Smckusick 	argc -= optind;
7941409Smckusick 	argv += optind;
8041409Smckusick 	if (argc <= 0 && !aflag)
8141409Smckusick 		usage(whoami);
8241409Smckusick 	if (!gflag && !uflag) {
8341409Smckusick 		gflag++;
8441409Smckusick 		uflag++;
8512662Smckusick 	}
8612662Smckusick 	setfsent();
8712662Smckusick 	while ((fs = getfsent()) != NULL) {
8841437Smckusick 		if (strcmp(fs->fs_vfstype, "ufs") ||
8941437Smckusick 		    strcmp(fs->fs_type, FSTAB_RW))
9041437Smckusick 			continue;
9141409Smckusick 		if (aflag) {
9241437Smckusick 			if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
9341437Smckusick 				errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
9441437Smckusick 			if (uflag && hasquota(fs, USRQUOTA, &qfnp))
9541437Smckusick 				errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
9612662Smckusick 			continue;
9741409Smckusick 		}
9841423Smckusick 		if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
9941409Smckusick 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
10041409Smckusick 			done |= 1 << argnum;
10141437Smckusick 			if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
10241437Smckusick 				errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
10341437Smckusick 			if (uflag && hasquota(fs, USRQUOTA, &qfnp))
10441437Smckusick 				errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
10541409Smckusick 		}
10612662Smckusick 	}
10712662Smckusick 	endfsent();
10812723Smckusick 	for (i = 0; i < argc; i++)
10912723Smckusick 		if ((done & (1 << i)) == 0)
11037983Sbostic 			fprintf(stderr, "%s not found in fstab\n",
11112723Smckusick 				argv[i]);
11212670Smckusick 	exit(errs);
11312662Smckusick }
11412662Smckusick 
usage(whoami)11541409Smckusick usage(whoami)
11641409Smckusick 	char *whoami;
11741409Smckusick {
11841409Smckusick 
11941409Smckusick 	fprintf(stderr, "Usage:\n\t%s [-g] [-u] [-v] -a\n", whoami);
12041409Smckusick 	fprintf(stderr, "\t%s [-g] [-u] [-v] filesys ...\n", whoami);
12141409Smckusick 	exit(1);
12241409Smckusick }
12341409Smckusick 
quotaonoff(fs,offmode,type,qfpathname)12441437Smckusick quotaonoff(fs, offmode, type, qfpathname)
12512809Ssam 	register struct fstab *fs;
12641409Smckusick 	int offmode, type;
12741437Smckusick 	char *qfpathname;
12812809Ssam {
12912809Ssam 
13012809Ssam 	if (strcmp(fs->fs_file, "/") && readonly(fs))
13112809Ssam 		return (1);
13212809Ssam 	if (offmode) {
13341437Smckusick 		if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0) {
13441437Smckusick 			fprintf(stderr, "quotaoff: ");
13541437Smckusick 			perror(fs->fs_file);
13641437Smckusick 			return (1);
13741437Smckusick 		}
13812809Ssam 		if (vflag)
13912809Ssam 			printf("%s: quotas turned off\n", fs->fs_file);
14012809Ssam 		return (0);
14112809Ssam 	}
14241437Smckusick 	if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, qfpathname) < 0) {
14341437Smckusick 		fprintf(stderr, "quotaon: using %s on", qfpathname);
14441437Smckusick 		perror(fs->fs_file);
14541437Smckusick 		return (1);
14641437Smckusick 	}
14712809Ssam 	if (vflag)
14841409Smckusick 		printf("%s: %s quotas turned on\n", fs->fs_file,
14941409Smckusick 		    qfextension[type]);
15012809Ssam 	return (0);
15112809Ssam }
15212809Ssam 
15341409Smckusick /*
15441409Smckusick  * Check to see if target appears in list of size cnt.
15541409Smckusick  */
oneof(target,list,cnt)15641409Smckusick oneof(target, list, cnt)
15741409Smckusick 	register char *target, *list[];
15841409Smckusick 	int cnt;
15912662Smckusick {
16012662Smckusick 	register int i;
16112662Smckusick 
16241409Smckusick 	for (i = 0; i < cnt; i++)
16341409Smckusick 		if (strcmp(target, list[i]) == 0)
16441409Smckusick 			return (i);
16541409Smckusick 	return (-1);
16641409Smckusick }
16741409Smckusick 
16841409Smckusick /*
16941409Smckusick  * Check to see if a particular quota is to be enabled.
17041409Smckusick  */
hasquota(fs,type,qfnamep)17141437Smckusick hasquota(fs, type, qfnamep)
17241437Smckusick 	register struct fstab *fs;
17341409Smckusick 	int type;
17441437Smckusick 	char **qfnamep;
17541409Smckusick {
17641409Smckusick 	register char *opt;
17741437Smckusick 	char *cp, *index(), *strtok();
17841409Smckusick 	static char initname, usrname[100], grpname[100];
17941437Smckusick 	static char buf[BUFSIZ];
18041409Smckusick 
18141409Smckusick 	if (!initname) {
18241409Smckusick 		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
18341409Smckusick 		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
18441409Smckusick 		initname = 1;
18541409Smckusick 	}
18641437Smckusick 	strcpy(buf, fs->fs_mntops);
18741409Smckusick 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
18841437Smckusick 		if (cp = index(opt, '='))
18941437Smckusick 			*cp++ = '\0';
19041409Smckusick 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
19141437Smckusick 			break;
19241409Smckusick 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
19341437Smckusick 			break;
19441409Smckusick 	}
19541437Smckusick 	if (!opt)
19641437Smckusick 		return (0);
19741437Smckusick 	if (cp) {
19841437Smckusick 		*qfnamep = cp;
19941437Smckusick 		return (1);
20041437Smckusick 	}
20141437Smckusick 	(void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
20241437Smckusick 	*qfnamep = buf;
20341437Smckusick 	return (1);
20412662Smckusick }
20512809Ssam 
20612809Ssam /*
20712809Ssam  * Verify file system is mounted and not readonly.
20812809Ssam  */
readonly(fs)20912809Ssam readonly(fs)
21012809Ssam 	register struct fstab *fs;
21112809Ssam {
21239844Smckusick 	struct statfs fsbuf;
21312809Ssam 
21439844Smckusick 	if (statfs(fs->fs_file, &fsbuf) < 0 ||
21539844Smckusick 	    strcmp(fsbuf.f_mntonname, fs->fs_file) ||
21639844Smckusick 	    strcmp(fsbuf.f_mntfromname, fs->fs_spec)) {
21739844Smckusick 		printf("%s: not mounted\n", fs->fs_file);
21839844Smckusick 		return (1);
21912809Ssam 	}
22041410Smckusick 	if (fsbuf.f_flags & MNT_RDONLY) {
22139844Smckusick 		printf("%s: mounted read-only\n", fs->fs_file);
22239844Smckusick 		return (1);
22339844Smckusick 	}
22439844Smckusick 	return (0);
22512809Ssam }
226