repquota.c revision 1.3 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
38 char 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
44 /*static char sccsid[] = "from: @(#)repquota.c 5.11 (Berkeley) 9/27/90";*/
45 static char rcsid[] = "$Id: repquota.c,v 1.3 1994/06/13 20:52:39 cgd Exp $";
46 #endif /* not lint */
47
48 /*
49 * Quota report
50 */
51 #include <sys/param.h>
52 #include <sys/stat.h>
53 #include <ufs/ufs/quota.h>
54 #include <fstab.h>
55 #include <pwd.h>
56 #include <grp.h>
57 #include <stdio.h>
58 #include <errno.h>
59
60 char *qfname = QUOTAFILENAME;
61 char *qfextension[] = INITQFNAMES;
62
63 struct fileusage {
64 struct fileusage *fu_next;
65 struct dqblk fu_dqblk;
66 u_long fu_id;
67 char fu_name[1];
68 /* actually bigger */
69 };
70 #define FUHASH 1024 /* must be power of two */
71 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
72 struct fileusage *lookup();
73 struct fileusage *addid();
74 u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
75
76 int vflag; /* verbose */
77 int aflag; /* all file systems */
78
79 main(argc, argv)
80 int argc;
81 char **argv;
82 {
83 register struct fstab *fs;
84 register struct passwd *pw;
85 register struct group *gr;
86 int gflag = 0, uflag = 0, errs = 0;
87 long i, argnum, done = 0;
88 extern char *optarg;
89 extern int optind;
90 char ch, *qfnp;
91
92 while ((ch = getopt(argc, argv, "aguv")) != EOF) {
93 switch(ch) {
94 case 'a':
95 aflag++;
96 break;
97 case 'g':
98 gflag++;
99 break;
100 case 'u':
101 uflag++;
102 break;
103 case 'v':
104 vflag++;
105 break;
106 default:
107 usage();
108 }
109 }
110 argc -= optind;
111 argv += optind;
112 if (argc == 0 && !aflag)
113 usage();
114 if (!gflag && !uflag) {
115 if (aflag)
116 gflag++;
117 uflag++;
118 }
119 if (gflag) {
120 setgrent();
121 while ((gr = getgrent()) != 0)
122 (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
123 endgrent();
124 }
125 if (uflag) {
126 setpwent();
127 while ((pw = getpwent()) != 0)
128 (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
129 endpwent();
130 }
131 setfsent();
132 while ((fs = getfsent()) != NULL) {
133 if (strcmp(fs->fs_vfstype, "ufs"))
134 continue;
135 if (aflag) {
136 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
137 errs += repquota(fs, GRPQUOTA, qfnp);
138 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
139 errs += repquota(fs, USRQUOTA, qfnp);
140 continue;
141 }
142 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
143 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
144 done |= 1 << argnum;
145 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
146 errs += repquota(fs, GRPQUOTA, qfnp);
147 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
148 errs += repquota(fs, USRQUOTA, qfnp);
149 }
150 }
151 endfsent();
152 for (i = 0; i < argc; i++)
153 if ((done & (1 << i)) == 0)
154 fprintf(stderr, "%s not found in fstab\n", argv[i]);
155 exit(errs);
156 }
157
158 usage()
159 {
160 fprintf(stderr, "Usage:\n\t%s\n\t%s\n",
161 "repquota [-v] [-g] [-u] -a",
162 "repquota [-v] [-g] [-u] filesys ...");
163 exit(1);
164 }
165
166 repquota(fs, type, qfpathname)
167 register struct fstab *fs;
168 int type;
169 char *qfpathname;
170 {
171 register struct fileusage *fup;
172 FILE *qf;
173 u_long id;
174 struct dqblk dqbuf;
175 char *timeprt();
176 static struct dqblk zerodqblk;
177 static int warned = 0;
178 static int multiple = 0;
179 extern int errno;
180
181 if (quotactl(fs->fs_file, QCMD(Q_SYNC, type), 0, 0) < 0 &&
182 errno == EOPNOTSUPP && !warned && vflag) {
183 warned++;
184 fprintf(stdout,
185 "*** Warning: Quotas are not compiled into this kernel\n");
186 }
187 if (multiple++)
188 printf("\n");
189 if (vflag)
190 fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
191 qfextension[type], fs->fs_file, fs->fs_spec);
192 if ((qf = fopen(qfpathname, "r")) == NULL) {
193 perror(qfpathname);
194 return (1);
195 }
196 for (id = 0; ; id++) {
197 fread(&dqbuf, sizeof(struct dqblk), 1, qf);
198 if (feof(qf))
199 break;
200 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
201 continue;
202 if ((fup = lookup(id, type)) == 0)
203 fup = addid(id, type, (char *)0);
204 fup->fu_dqblk = dqbuf;
205 }
206 fclose(qf);
207 printf(" Block limits File limits\n");
208 printf("User used soft hard grace used soft hard grace\n");
209 for (id = 0; id <= highid[type]; id++) {
210 fup = lookup(id, type);
211 if (fup == 0)
212 continue;
213 if (fup->fu_dqblk.dqb_curinodes == 0 &&
214 fup->fu_dqblk.dqb_curblocks == 0)
215 continue;
216 printf("%-10s", fup->fu_name);
217 printf("%c%c%8d%8d%8d%7s",
218 fup->fu_dqblk.dqb_bsoftlimit &&
219 fup->fu_dqblk.dqb_curblocks >=
220 fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-',
221 fup->fu_dqblk.dqb_isoftlimit &&
222 fup->fu_dqblk.dqb_curinodes >=
223 fup->fu_dqblk.dqb_isoftlimit ? '+' : '-',
224 dbtob(fup->fu_dqblk.dqb_curblocks) / 1024,
225 dbtob(fup->fu_dqblk.dqb_bsoftlimit) / 1024,
226 dbtob(fup->fu_dqblk.dqb_bhardlimit) / 1024,
227 fup->fu_dqblk.dqb_bsoftlimit &&
228 fup->fu_dqblk.dqb_curblocks >=
229 fup->fu_dqblk.dqb_bsoftlimit ?
230 timeprt(fup->fu_dqblk.dqb_btime) : "");
231 printf(" %6d%6d%6d%7s\n",
232 fup->fu_dqblk.dqb_curinodes,
233 fup->fu_dqblk.dqb_isoftlimit,
234 fup->fu_dqblk.dqb_ihardlimit,
235 fup->fu_dqblk.dqb_isoftlimit &&
236 fup->fu_dqblk.dqb_curinodes >=
237 fup->fu_dqblk.dqb_isoftlimit ?
238 timeprt(fup->fu_dqblk.dqb_itime) : "");
239 fup->fu_dqblk = zerodqblk;
240 }
241 return (0);
242 }
243
244 /*
245 * Check to see if target appears in list of size cnt.
246 */
247 oneof(target, list, cnt)
248 register char *target, *list[];
249 int cnt;
250 {
251 register int i;
252
253 for (i = 0; i < cnt; i++)
254 if (strcmp(target, list[i]) == 0)
255 return (i);
256 return (-1);
257 }
258
259 /*
260 * Check to see if a particular quota is to be enabled.
261 */
262 hasquota(fs, type, qfnamep)
263 register struct fstab *fs;
264 int type;
265 char **qfnamep;
266 {
267 register char *opt;
268 char *cp, *index(), *strtok();
269 static char initname, usrname[100], grpname[100];
270 static char buf[BUFSIZ];
271
272 if (!initname) {
273 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
274 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
275 initname = 1;
276 }
277 strcpy(buf, fs->fs_mntops);
278 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
279 if (cp = index(opt, '='))
280 *cp++ = '\0';
281 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
282 break;
283 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
284 break;
285 }
286 if (!opt)
287 return (0);
288 if (cp) {
289 *qfnamep = cp;
290 return (1);
291 }
292 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
293 *qfnamep = buf;
294 return (1);
295 }
296
297 /*
298 * Routines to manage the file usage table.
299 *
300 * Lookup an id of a specific type.
301 */
302 struct fileusage *
303 lookup(id, type)
304 u_long id;
305 int type;
306 {
307 register struct fileusage *fup;
308
309 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
310 if (fup->fu_id == id)
311 return (fup);
312 return ((struct fileusage *)0);
313 }
314
315 /*
316 * Add a new file usage id if it does not already exist.
317 */
318 struct fileusage *
319 addid(id, type, name)
320 u_long id;
321 int type;
322 char *name;
323 {
324 struct fileusage *fup, **fhp;
325 int len;
326 extern char *calloc();
327
328 if (fup = lookup(id, type))
329 return (fup);
330 if (name)
331 len = strlen(name);
332 else
333 len = 10;
334 if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL) {
335 fprintf(stderr, "out of memory for fileusage structures\n");
336 exit(1);
337 }
338 fhp = &fuhead[type][id & (FUHASH - 1)];
339 fup->fu_next = *fhp;
340 *fhp = fup;
341 fup->fu_id = id;
342 if (id > highid[type])
343 highid[type] = id;
344 if (name) {
345 bcopy(name, fup->fu_name, len + 1);
346 } else {
347 sprintf(fup->fu_name, "%u", id);
348 }
349 return (fup);
350 }
351
352 /*
353 * Calculate the grace period and return a printable string for it.
354 */
355 char *
356 timeprt(seconds)
357 time_t seconds;
358 {
359 time_t hours, minutes;
360 static char buf[20];
361 static time_t now;
362
363 if (now == 0)
364 time(&now);
365 if (now > seconds)
366 return ("none");
367 seconds -= now;
368 minutes = (seconds + 30) / 60;
369 hours = (minutes + 30) / 60;
370 if (hours >= 36) {
371 sprintf(buf, "%ddays", (hours + 12) / 24);
372 return (buf);
373 }
374 if (minutes >= 60) {
375 sprintf(buf, "%2d:%d", minutes / 60, minutes % 60);
376 return (buf);
377 }
378 sprintf(buf, "%2d", minutes);
379 return (buf);
380 }
381