xref: /csrg-svn/usr.sbin/quotaon/quotaon.c (revision 42816)
121518Smckusick /*
241409Smckusick  * Copyright (c) 1980, 1990 Regents of the University of California.
334365Sbostic  * All rights reserved.
434365Sbostic  *
541409Smckusick  * This code is derived from software contributed to Berkeley by
641409Smckusick  * Robert Elz at The University of Melbourne.
741409Smckusick  *
8*42816Sbostic  * %sccs.include.redist.c%
921518Smckusick  */
1021518Smckusick 
1112662Smckusick #ifndef lint
1221518Smckusick char copyright[] =
1341409Smckusick "@(#) Copyright (c) 1980, 1990 Regents of the University of California.\n\
1421518Smckusick  All rights reserved.\n";
1534365Sbostic #endif /* not lint */
1612809Ssam 
1721518Smckusick #ifndef lint
18*42816Sbostic static char sccsid[] = "@(#)quotaon.c	5.11 (Berkeley) 06/01/90";
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>
2741409Smckusick #include <ufs/quota.h>
2812662Smckusick #include <stdio.h>
2912662Smckusick #include <fstab.h>
3012662Smckusick 
3141409Smckusick int	aflag;		/* all file systems */
3241409Smckusick int	gflag;		/* operate on group quotas */
3341409Smckusick int	uflag;		/* operate on user quotas */
3412662Smckusick int	vflag;		/* verbose */
3512662Smckusick 
3612662Smckusick main(argc, argv)
3712670Smckusick 	int argc;
3812662Smckusick 	char **argv;
3912662Smckusick {
4012662Smckusick 	register struct fstab *fs;
4141437Smckusick 	char ch, *qfnp, *whoami, *rindex();
4241409Smckusick 	long argnum, done = 0;
4341409Smckusick 	int i, offmode = 0, errs = 0;
4441409Smckusick 	extern char *optarg;
4541409Smckusick 	extern int optind;
4612662Smckusick 
4712670Smckusick 	whoami = rindex(*argv, '/') + 1;
4812670Smckusick 	if (whoami == (char *)1)
4912670Smckusick 		whoami = *argv;
5012670Smckusick 	if (strcmp(whoami, "quotaoff") == 0)
5112670Smckusick 		offmode++;
5212670Smckusick 	else if (strcmp(whoami, "quotaon") != 0) {
5312670Smckusick 		fprintf(stderr, "Name must be quotaon or quotaoff not %s\n",
5412670Smckusick 			whoami);
5512670Smckusick 		exit(1);
5612670Smckusick 	}
5741409Smckusick 	while ((ch = getopt(argc, argv, "avug")) != EOF) {
5841409Smckusick 		switch(ch) {
5941409Smckusick 		case 'a':
6041409Smckusick 			aflag++;
6141409Smckusick 			break;
6241409Smckusick 		case 'g':
6341409Smckusick 			gflag++;
6441409Smckusick 			break;
6541409Smckusick 		case 'u':
6641409Smckusick 			uflag++;
6741409Smckusick 			break;
6841409Smckusick 		case 'v':
6941409Smckusick 			vflag++;
7041409Smckusick 			break;
7141409Smckusick 		default:
7241409Smckusick 			usage(whoami);
7341409Smckusick 		}
7412662Smckusick 	}
7541409Smckusick 	argc -= optind;
7641409Smckusick 	argv += optind;
7741409Smckusick 	if (argc <= 0 && !aflag)
7841409Smckusick 		usage(whoami);
7941409Smckusick 	if (!gflag && !uflag) {
8041409Smckusick 		gflag++;
8141409Smckusick 		uflag++;
8212662Smckusick 	}
8312662Smckusick 	setfsent();
8412662Smckusick 	while ((fs = getfsent()) != NULL) {
8541437Smckusick 		if (strcmp(fs->fs_vfstype, "ufs") ||
8641437Smckusick 		    strcmp(fs->fs_type, FSTAB_RW))
8741437Smckusick 			continue;
8841409Smckusick 		if (aflag) {
8941437Smckusick 			if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
9041437Smckusick 				errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
9141437Smckusick 			if (uflag && hasquota(fs, USRQUOTA, &qfnp))
9241437Smckusick 				errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
9312662Smckusick 			continue;
9441409Smckusick 		}
9541423Smckusick 		if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
9641409Smckusick 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
9741409Smckusick 			done |= 1 << argnum;
9841437Smckusick 			if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
9941437Smckusick 				errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
10041437Smckusick 			if (uflag && hasquota(fs, USRQUOTA, &qfnp))
10141437Smckusick 				errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
10241409Smckusick 		}
10312662Smckusick 	}
10412662Smckusick 	endfsent();
10512723Smckusick 	for (i = 0; i < argc; i++)
10612723Smckusick 		if ((done & (1 << i)) == 0)
10737983Sbostic 			fprintf(stderr, "%s not found in fstab\n",
10812723Smckusick 				argv[i]);
10912670Smckusick 	exit(errs);
11012662Smckusick }
11112662Smckusick 
11241409Smckusick usage(whoami)
11341409Smckusick 	char *whoami;
11441409Smckusick {
11541409Smckusick 
11641409Smckusick 	fprintf(stderr, "Usage:\n\t%s [-g] [-u] [-v] -a\n", whoami);
11741409Smckusick 	fprintf(stderr, "\t%s [-g] [-u] [-v] filesys ...\n", whoami);
11841409Smckusick 	exit(1);
11941409Smckusick }
12041409Smckusick 
12141437Smckusick quotaonoff(fs, offmode, type, qfpathname)
12212809Ssam 	register struct fstab *fs;
12341409Smckusick 	int offmode, type;
12441437Smckusick 	char *qfpathname;
12512809Ssam {
12612809Ssam 
12712809Ssam 	if (strcmp(fs->fs_file, "/") && readonly(fs))
12812809Ssam 		return (1);
12912809Ssam 	if (offmode) {
13041437Smckusick 		if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0) {
13141437Smckusick 			fprintf(stderr, "quotaoff: ");
13241437Smckusick 			perror(fs->fs_file);
13341437Smckusick 			return (1);
13441437Smckusick 		}
13512809Ssam 		if (vflag)
13612809Ssam 			printf("%s: quotas turned off\n", fs->fs_file);
13712809Ssam 		return (0);
13812809Ssam 	}
13941437Smckusick 	if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, qfpathname) < 0) {
14041437Smckusick 		fprintf(stderr, "quotaon: using %s on", qfpathname);
14141437Smckusick 		perror(fs->fs_file);
14241437Smckusick 		return (1);
14341437Smckusick 	}
14412809Ssam 	if (vflag)
14541409Smckusick 		printf("%s: %s quotas turned on\n", fs->fs_file,
14641409Smckusick 		    qfextension[type]);
14712809Ssam 	return (0);
14812809Ssam }
14912809Ssam 
15041409Smckusick /*
15141409Smckusick  * Check to see if target appears in list of size cnt.
15241409Smckusick  */
15341409Smckusick oneof(target, list, cnt)
15441409Smckusick 	register char *target, *list[];
15541409Smckusick 	int cnt;
15612662Smckusick {
15712662Smckusick 	register int i;
15812662Smckusick 
15941409Smckusick 	for (i = 0; i < cnt; i++)
16041409Smckusick 		if (strcmp(target, list[i]) == 0)
16141409Smckusick 			return (i);
16241409Smckusick 	return (-1);
16341409Smckusick }
16441409Smckusick 
16541409Smckusick /*
16641409Smckusick  * Check to see if a particular quota is to be enabled.
16741409Smckusick  */
16841437Smckusick hasquota(fs, type, qfnamep)
16941437Smckusick 	register struct fstab *fs;
17041409Smckusick 	int type;
17141437Smckusick 	char **qfnamep;
17241409Smckusick {
17341409Smckusick 	register char *opt;
17441437Smckusick 	char *cp, *index(), *strtok();
17541409Smckusick 	static char initname, usrname[100], grpname[100];
17641437Smckusick 	static char buf[BUFSIZ];
17741409Smckusick 
17841409Smckusick 	if (!initname) {
17941409Smckusick 		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
18041409Smckusick 		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
18141409Smckusick 		initname = 1;
18241409Smckusick 	}
18341437Smckusick 	strcpy(buf, fs->fs_mntops);
18441409Smckusick 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
18541437Smckusick 		if (cp = index(opt, '='))
18641437Smckusick 			*cp++ = '\0';
18741409Smckusick 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
18841437Smckusick 			break;
18941409Smckusick 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
19041437Smckusick 			break;
19141409Smckusick 	}
19241437Smckusick 	if (!opt)
19341437Smckusick 		return (0);
19441437Smckusick 	if (cp) {
19541437Smckusick 		*qfnamep = cp;
19641437Smckusick 		return (1);
19741437Smckusick 	}
19841437Smckusick 	(void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
19941437Smckusick 	*qfnamep = buf;
20041437Smckusick 	return (1);
20112662Smckusick }
20212809Ssam 
20312809Ssam /*
20412809Ssam  * Verify file system is mounted and not readonly.
20512809Ssam  */
20612809Ssam readonly(fs)
20712809Ssam 	register struct fstab *fs;
20812809Ssam {
20939844Smckusick 	struct statfs fsbuf;
21012809Ssam 
21139844Smckusick 	if (statfs(fs->fs_file, &fsbuf) < 0 ||
21239844Smckusick 	    strcmp(fsbuf.f_mntonname, fs->fs_file) ||
21339844Smckusick 	    strcmp(fsbuf.f_mntfromname, fs->fs_spec)) {
21439844Smckusick 		printf("%s: not mounted\n", fs->fs_file);
21539844Smckusick 		return (1);
21612809Ssam 	}
21741410Smckusick 	if (fsbuf.f_flags & MNT_RDONLY) {
21839844Smckusick 		printf("%s: mounted read-only\n", fs->fs_file);
21939844Smckusick 		return (1);
22039844Smckusick 	}
22139844Smckusick 	return (0);
22212809Ssam }
223