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