xref: /csrg-svn/usr.sbin/quotaon/quotaon.c (revision 39844)
121518Smckusick /*
221518Smckusick  * Copyright (c) 1980 Regents of the University of California.
334365Sbostic  * All rights reserved.
434365Sbostic  *
534365Sbostic  * Redistribution and use in source and binary forms are permitted
634778Sbostic  * provided that the above copyright notice and this paragraph are
734778Sbostic  * duplicated in all such forms and that any documentation,
834778Sbostic  * advertising materials, and other materials related to such
934778Sbostic  * distribution and use acknowledge that the software was developed
1034778Sbostic  * by the University of California, Berkeley.  The name of the
1134778Sbostic  * University may not be used to endorse or promote products derived
1234778Sbostic  * from this software without specific prior written permission.
1334778Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434778Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534778Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621518Smckusick  */
1721518Smckusick 
1812662Smckusick #ifndef lint
1921518Smckusick char copyright[] =
2021518Smckusick "@(#) Copyright (c) 1980 Regents of the University of California.\n\
2121518Smckusick  All rights reserved.\n";
2234365Sbostic #endif /* not lint */
2312809Ssam 
2421518Smckusick #ifndef lint
25*39844Smckusick static char sccsid[] = "@(#)quotaon.c	5.6 (Berkeley) 01/02/90";
2634365Sbostic #endif /* not lint */
2721518Smckusick 
2812662Smckusick /*
2912670Smckusick  * Turn quota on/off for a filesystem.
3012662Smckusick  */
3112662Smckusick #include <sys/param.h>
3212809Ssam #include <sys/file.h>
33*39844Smckusick #include <sys/mount.h>
3412662Smckusick #include <stdio.h>
3512662Smckusick #include <fstab.h>
3612662Smckusick 
3712662Smckusick int	vflag;		/* verbose */
3812662Smckusick int	aflag;		/* all file systems */
3912723Smckusick int	done;
4012662Smckusick 
4112809Ssam char	*qfname = "quotas";
4212809Ssam char	quotafile[MAXPATHLEN + 1];
4312809Ssam char	*index(), *rindex();
4412670Smckusick 
4512662Smckusick main(argc, argv)
4612670Smckusick 	int argc;
4712662Smckusick 	char **argv;
4812662Smckusick {
4912662Smckusick 	register struct fstab *fs;
5012670Smckusick 	char *whoami, *rindex();
5112723Smckusick 	int offmode = 0, errs = 0, i;
5212662Smckusick 
5312670Smckusick 	whoami = rindex(*argv, '/') + 1;
5412670Smckusick 	if (whoami == (char *)1)
5512670Smckusick 		whoami = *argv;
5612670Smckusick 	if (strcmp(whoami, "quotaoff") == 0)
5712670Smckusick 		offmode++;
5812670Smckusick 	else if (strcmp(whoami, "quotaon") != 0) {
5912670Smckusick 		fprintf(stderr, "Name must be quotaon or quotaoff not %s\n",
6012670Smckusick 			whoami);
6112670Smckusick 		exit(1);
6212670Smckusick 	}
6312662Smckusick again:
6412662Smckusick 	argc--, argv++;
6512662Smckusick 	if (argc > 0 && strcmp(*argv, "-v") == 0) {
6612662Smckusick 		vflag++;
6712662Smckusick 		goto again;
6812662Smckusick 	}
6912662Smckusick 	if (argc > 0 && strcmp(*argv, "-a") == 0) {
7012662Smckusick 		aflag++;
7112662Smckusick 		goto again;
7212662Smckusick 	}
7312670Smckusick 	if (argc <= 0 && !aflag) {
7412670Smckusick 		fprintf(stderr, "Usage:\n\t%s [-v] -a\n\t%s [-v] filesys ...\n",
7512670Smckusick 			whoami, whoami);
7612662Smckusick 		exit(1);
7712662Smckusick 	}
7812662Smckusick 	setfsent();
7912662Smckusick 	while ((fs = getfsent()) != NULL) {
8012723Smckusick 		if (aflag &&
8112723Smckusick 		    (fs->fs_type == 0 || strcmp(fs->fs_type, "rq") != 0))
8212662Smckusick 			continue;
8312723Smckusick 		if (!aflag &&
8412723Smckusick 		    !(oneof(fs->fs_file, argv, argc) ||
8512723Smckusick 		      oneof(fs->fs_spec, argv, argc)))
8612662Smckusick 			continue;
8712809Ssam 		errs += quotaonoff(fs, offmode);
8812662Smckusick 	}
8912662Smckusick 	endfsent();
9012723Smckusick 	for (i = 0; i < argc; i++)
9112723Smckusick 		if ((done & (1 << i)) == 0)
9237983Sbostic 			fprintf(stderr, "%s not found in fstab\n",
9312723Smckusick 				argv[i]);
9412670Smckusick 	exit(errs);
9512662Smckusick }
9612662Smckusick 
9712809Ssam quotaonoff(fs, offmode)
9812809Ssam 	register struct fstab *fs;
9912809Ssam 	int offmode;
10012809Ssam {
10112809Ssam 
10212809Ssam 	if (strcmp(fs->fs_file, "/") && readonly(fs))
10312809Ssam 		return (1);
10412809Ssam 	if (offmode) {
10512809Ssam 		if (setquota(fs->fs_spec, NULL) < 0)
10612809Ssam 			goto bad;
10712809Ssam 		if (vflag)
10812809Ssam 			printf("%s: quotas turned off\n", fs->fs_file);
10912809Ssam 		return (0);
11012809Ssam 	}
11112809Ssam 	(void) sprintf(quotafile, "%s/%s", fs->fs_file, qfname);
11212809Ssam 	if (setquota(fs->fs_spec, quotafile) < 0)
11312809Ssam 		goto bad;
11412809Ssam 	if (vflag)
11512809Ssam 		printf("%s: quotas turned on\n", fs->fs_file);
11612809Ssam 	return (0);
11712809Ssam bad:
11812809Ssam 	fprintf(stderr, "setquota: ");
11912809Ssam 	perror(fs->fs_spec);
12012809Ssam 	return (1);
12112809Ssam }
12212809Ssam 
12312662Smckusick oneof(target, list, n)
12412662Smckusick 	char *target, *list[];
12512662Smckusick 	register int n;
12612662Smckusick {
12712662Smckusick 	register int i;
12812662Smckusick 
12912662Smckusick 	for (i = 0; i < n; i++)
13012723Smckusick 		if (strcmp(target, list[i]) == 0) {
13112723Smckusick 			done |= 1 << i;
13212662Smckusick 			return (1);
13312723Smckusick 		}
13412662Smckusick 	return (0);
13512662Smckusick }
13612809Ssam 
13712809Ssam /*
13812809Ssam  * Verify file system is mounted and not readonly.
13912809Ssam  */
14012809Ssam readonly(fs)
14112809Ssam 	register struct fstab *fs;
14212809Ssam {
143*39844Smckusick 	struct statfs fsbuf;
14412809Ssam 
145*39844Smckusick 	if (statfs(fs->fs_file, &fsbuf) < 0 ||
146*39844Smckusick 	    strcmp(fsbuf.f_mntonname, fs->fs_file) ||
147*39844Smckusick 	    strcmp(fsbuf.f_mntfromname, fs->fs_spec)) {
148*39844Smckusick 		printf("%s: not mounted\n", fs->fs_file);
149*39844Smckusick 		return (1);
15012809Ssam 	}
151*39844Smckusick 	if (fsbuf.f_flags & M_RDONLY) {
152*39844Smckusick 		printf("%s: mounted read-only\n", fs->fs_file);
153*39844Smckusick 		return (1);
154*39844Smckusick 	}
155*39844Smckusick 	return (0);
15612809Ssam }
157