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