This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / usr.sbin / quotaon / quotaon.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1980, 1990 Regents of the University of California.
3 * 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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38char copyright[] =
39"@(#) Copyright (c) 1980, 1990 Regents of the University of California.\n\
40 All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44static char sccsid[] = "@(#)quotaon.c 5.12 (Berkeley) 9/27/90";
45#endif /* not lint */
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/quota.h>
54#include <stdio.h>
55#include <fstab.h>
56
57char *qfname = QUOTAFILENAME;
58char *qfextension[] = INITQFNAMES;
59
60int aflag; /* all file systems */
61int gflag; /* operate on group quotas */
62int uflag; /* operate on user quotas */
63int vflag; /* verbose */
64
65main(argc, argv)
66 int argc;
67 char **argv;
68{
69 register struct fstab *fs;
70 char ch, *qfnp, *whoami, *rindex();
71 long argnum, done = 0;
72 int i, offmode = 0, errs = 0;
73 extern char *optarg;
74 extern int optind;
75
76 whoami = rindex(*argv, '/') + 1;
77 if (whoami == (char *)1)
78 whoami = *argv;
79 if (strcmp(whoami, "quotaoff") == 0)
80 offmode++;
81 else if (strcmp(whoami, "quotaon") != 0) {
82 fprintf(stderr, "Name must be quotaon or quotaoff not %s\n",
83 whoami);
84 exit(1);
85 }
86 while ((ch = getopt(argc, argv, "avug")) != EOF) {
87 switch(ch) {
88 case 'a':
89 aflag++;
90 break;
91 case 'g':
92 gflag++;
93 break;
94 case 'u':
95 uflag++;
96 break;
97 case 'v':
98 vflag++;
99 break;
100 default:
101 usage(whoami);
102 }
103 }
104 argc -= optind;
105 argv += optind;
106 if (argc <= 0 && !aflag)
107 usage(whoami);
108 if (!gflag && !uflag) {
109 gflag++;
110 uflag++;
111 }
112 setfsent();
113 while ((fs = getfsent()) != NULL) {
114 if (strcmp(fs->fs_vfstype, "ufs") ||
115 strcmp(fs->fs_type, FSTAB_RW))
116 continue;
117 if (aflag) {
118 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
119 errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
120 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
121 errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
122 continue;
123 }
124 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
125 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
126 done |= 1 << argnum;
127 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
128 errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
129 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
130 errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
131 }
132 }
133 endfsent();
134 for (i = 0; i < argc; i++)
135 if ((done & (1 << i)) == 0)
136 fprintf(stderr, "%s not found in fstab\n",
137 argv[i]);
138 exit(errs);
139}
140
141usage(whoami)
142 char *whoami;
143{
144
145 fprintf(stderr, "Usage:\n\t%s [-g] [-u] [-v] -a\n", whoami);
146 fprintf(stderr, "\t%s [-g] [-u] [-v] filesys ...\n", whoami);
147 exit(1);
148}
149
150quotaonoff(fs, offmode, type, qfpathname)
151 register struct fstab *fs;
152 int offmode, type;
153 char *qfpathname;
154{
155
156 if (strcmp(fs->fs_file, "/") && readonly(fs))
157 return (1);
158 if (offmode) {
159 if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0) {
160 fprintf(stderr, "quotaoff: ");
161 perror(fs->fs_file);
162 return (1);
163 }
164 if (vflag)
165 printf("%s: quotas turned off\n", fs->fs_file);
166 return (0);
167 }
168 if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, qfpathname) < 0) {
169 fprintf(stderr, "quotaon: using %s on", qfpathname);
170 perror(fs->fs_file);
171 return (1);
172 }
173 if (vflag)
174 printf("%s: %s quotas turned on\n", fs->fs_file,
175 qfextension[type]);
176 return (0);
177}
178
179/*
180 * Check to see if target appears in list of size cnt.
181 */
182oneof(target, list, cnt)
183 register char *target, *list[];
184 int cnt;
185{
186 register int i;
187
188 for (i = 0; i < cnt; i++)
189 if (strcmp(target, list[i]) == 0)
190 return (i);
191 return (-1);
192}
193
194/*
195 * Check to see if a particular quota is to be enabled.
196 */
197hasquota(fs, type, qfnamep)
198 register struct fstab *fs;
199 int type;
200 char **qfnamep;
201{
202 register char *opt;
203 char *cp, *index(), *strtok();
204 static char initname, usrname[100], grpname[100];
205 static char buf[BUFSIZ];
206
207 if (!initname) {
208 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
209 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
210 initname = 1;
211 }
212 strcpy(buf, fs->fs_mntops);
213 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
214 if (cp = index(opt, '='))
215 *cp++ = '\0';
216 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
217 break;
218 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
219 break;
220 }
221 if (!opt)
222 return (0);
223 if (cp) {
224 *qfnamep = cp;
225 return (1);
226 }
227 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
228 *qfnamep = buf;
229 return (1);
230}
231
232/*
233 * Verify file system is mounted and not readonly.
234 */
235readonly(fs)
236 register struct fstab *fs;
237{
238 struct statfs fsbuf;
239
240 if (statfs(fs->fs_file, &fsbuf) < 0 ||
241 strcmp(fsbuf.f_mntonname, fs->fs_file) ||
242 strcmp(fsbuf.f_mntfromname, fs->fs_spec)) {
243 printf("%s: not mounted\n", fs->fs_file);
244 return (1);
245 }
246 if (fsbuf.f_flags & MNT_RDONLY) {
247 printf("%s: mounted read-only\n", fs->fs_file);
248 return (1);
249 }
250 return (0);
251}