xref: /freebsd/usr.sbin/quotaon/quotaon.c (revision 7aa383846770374466b1dcb2cefd71bde9acf463)
1 /*
2  * Copyright (c) 1980, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Robert Elz at The University of Melbourne.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #if 0
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1990, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 static char sccsid[] = "@(#)quotaon.c	8.1 (Berkeley) 6/6/93";
42 #endif /* not lint */
43 #endif
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46 
47 /*
48  * Turn quota on/off for a filesystem.
49  */
50 #include <sys/param.h>
51 #include <sys/file.h>
52 #include <sys/mount.h>
53 #include <ufs/ufs/quota.h>
54 #include <err.h>
55 #include <fstab.h>
56 #include <libutil.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 
62 const char *qfname = QUOTAFILENAME;
63 const char *qfextension[] = INITQFNAMES;
64 
65 int	aflag;		/* all filesystems */
66 int	gflag;		/* operate on group quotas */
67 int	uflag;		/* operate on user quotas */
68 int	vflag;		/* verbose */
69 
70 int oneof(char *, char *[], int);
71 int quotaonoff(struct fstab *fs, int, int);
72 static void usage(void);
73 
74 int
75 main(int argc, char **argv)
76 {
77 	struct fstab *fs;
78 	char *whoami;
79 	long argnum, done = 0;
80 	int ch, i, offmode = 0, errs = 0;
81 
82 	whoami = rindex(*argv, '/') + 1;
83 	if (whoami == (char *)1)
84 		whoami = *argv;
85 	if (strcmp(whoami, "quotaoff") == 0)
86 		offmode++;
87 	else if (strcmp(whoami, "quotaon") != 0)
88 		errx(1, "name must be quotaon or quotaoff");
89 	while ((ch = getopt(argc, argv, "avug")) != -1) {
90 		switch(ch) {
91 		case 'a':
92 			aflag++;
93 			break;
94 		case 'g':
95 			gflag++;
96 			break;
97 		case 'u':
98 			uflag++;
99 			break;
100 		case 'v':
101 			vflag++;
102 			break;
103 		default:
104 			usage();
105 		}
106 	}
107 	argc -= optind;
108 	argv += optind;
109 	if (argc <= 0 && !aflag)
110 		usage();
111 	if (!gflag && !uflag) {
112 		gflag++;
113 		uflag++;
114 	}
115 	setfsent();
116 	while ((fs = getfsent()) != NULL) {
117 		if (strcmp(fs->fs_vfstype, "ufs") ||
118 		    strcmp(fs->fs_type, FSTAB_RW))
119 			continue;
120 		if (aflag) {
121 			if (gflag)
122 				errs += quotaonoff(fs, offmode, GRPQUOTA);
123 			if (uflag)
124 				errs += quotaonoff(fs, offmode, USRQUOTA);
125 			continue;
126 		}
127 		if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
128 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
129 			done |= 1 << argnum;
130 			if (gflag)
131 				errs += quotaonoff(fs, offmode, GRPQUOTA);
132 			if (uflag)
133 				errs += quotaonoff(fs, offmode, USRQUOTA);
134 		}
135 	}
136 	endfsent();
137 	for (i = 0; i < argc; i++)
138 		if ((done & (1 << i)) == 0)
139 			warnx("%s not found in fstab", argv[i]);
140 	exit(errs);
141 }
142 
143 static void
144 usage(void)
145 {
146 
147 	fprintf(stderr, "%s\n%s\n%s\n%s\n",
148 		"usage: quotaon [-g] [-u] [-v] -a",
149 		"       quotaon [-g] [-u] [-v] filesystem ...",
150 		"       quotaoff [-g] [-u] [-v] -a",
151 		"       quotaoff [-g] [-u] [-v] filesystem ...");
152 	exit(1);
153 }
154 
155 int
156 quotaonoff(struct fstab *fs, int offmode, int type)
157 {
158 	struct quotafile *qf;
159 
160 	if ((qf = quota_open(fs, type, O_RDONLY)) == NULL)
161 		return (0);
162 	if (offmode) {
163 		if (quota_off(qf) != 0) {
164 			warn("%s", quota_fsname(qf));
165 			return (1);
166 		}
167 		if (vflag)
168 			printf("%s: quotas turned off\n", quota_fsname(qf));
169 		quota_close(qf);
170 		return(0);
171 	}
172 	if (quota_on(qf) != 0) {
173 		warn("using %s on %s", quota_qfname(qf), quota_fsname(qf));
174 		return (1);
175 	}
176 	if (vflag)
177 		printf("%s: %s quotas turned on with data file %s\n",
178 		    quota_fsname(qf), qfextension[type], quota_qfname(qf));
179 	quota_close(qf);
180 	return(0);
181 }
182 
183 /*
184  * Check to see if target appears in list of size cnt.
185  */
186 int
187 oneof(char *target, char *list[], int cnt)
188 {
189 	int i;
190 
191 	for (i = 0; i < cnt; i++)
192 		if (strcmp(target, list[i]) == 0)
193 			return (i);
194 	return (-1);
195 }
196