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