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