xref: /freebsd/usr.sbin/repquota/repquota.c (revision 3416500aef140042c64bc149cb1ec6620483bc44)
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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #if 0
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1990, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 static char sccsid[] = "@(#)repquota.c	8.1 (Berkeley) 6/6/93";
42 #endif /* not lint */
43 #endif
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46 
47 /*
48  * Quota report
49  */
50 #include <sys/param.h>
51 #include <sys/mount.h>
52 
53 #include <ufs/ufs/quota.h>
54 
55 #include <err.h>
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <fstab.h>
59 #include <grp.h>
60 #include <libutil.h>
61 #include <pwd.h>
62 #include <stdint.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <time.h>
67 #include <unistd.h>
68 
69 /* Let's be paranoid about block size */
70 #if 10 > DEV_BSHIFT
71 #define dbtokb(db) \
72 	((off_t)(db) >> (10-DEV_BSHIFT))
73 #elif 10 < DEV_BSHIFT
74 #define dbtokb(db) \
75 	((off_t)(db) << (DEV_BSHIFT-10))
76 #else
77 #define dbtokb(db)	(db)
78 #endif
79 
80 #define max(a,b) ((a) >= (b) ? (a) : (b))
81 
82 static const char *qfextension[] = INITQFNAMES;
83 
84 struct fileusage {
85 	struct	fileusage *fu_next;
86 	u_long	fu_id;
87 	char	fu_name[1];
88 	/* actually bigger */
89 };
90 #define FUHASH 1024	/* must be power of two */
91 static struct fileusage *fuhead[MAXQUOTAS][FUHASH];
92 static struct fileusage *lookup(u_long, int);
93 static struct fileusage *addid(u_long, int, char *);
94 static u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
95 
96 static int	vflag;		/* verbose */
97 static int	aflag;		/* all filesystems */
98 static int	nflag;		/* display user/group by id */
99 static int	hflag;		/* display in human readable format */
100 
101 int oneof(char *, char *[], int);
102 int repquota(struct fstab *, int);
103 char *timeprt(time_t);
104 static void prthumanval(int64_t bytes);
105 static void usage(void);
106 
107 int
108 main(int argc, char *argv[])
109 {
110 	struct fstab *fs;
111 	struct passwd *pw;
112 	struct group *gr;
113 	int ch, gflag = 0, uflag = 0, errs = 0;
114 	long i, argnum, done = 0;
115 
116 	while ((ch = getopt(argc, argv, "aghnuv")) != -1) {
117 		switch(ch) {
118 		case 'a':
119 			aflag++;
120 			break;
121 		case 'g':
122 			gflag++;
123 			break;
124 		case 'h':
125 			hflag++;
126 			break;
127 		case 'n':
128 			nflag++;
129 			break;
130 		case 'u':
131 			uflag++;
132 			break;
133 		case 'v':
134 			vflag++;
135 			break;
136 		default:
137 			usage();
138 		}
139 	}
140 	argc -= optind;
141 	argv += optind;
142 	if (argc == 0 && !aflag)
143 		usage();
144 	if (!gflag && !uflag) {
145 		if (aflag)
146 			gflag++;
147 		uflag++;
148 	}
149 	if (gflag && !nflag) {
150 		setgrent();
151 		while ((gr = getgrent()) != 0)
152 			(void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
153 		endgrent();
154 	}
155 	if (uflag && !nflag) {
156 		setpwent();
157 		while ((pw = getpwent()) != 0)
158 			(void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
159 		endpwent();
160 	}
161 	setfsent();
162 	while ((fs = getfsent()) != NULL) {
163 		if (strcmp(fs->fs_vfstype, "ufs"))
164 			continue;
165 		if (aflag) {
166 			if (gflag)
167 				errs += repquota(fs, GRPQUOTA);
168 			if (uflag)
169 				errs += repquota(fs, USRQUOTA);
170 			continue;
171 		}
172 		if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
173 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
174 			done |= 1 << argnum;
175 			if (gflag)
176 				errs += repquota(fs, GRPQUOTA);
177 			if (uflag)
178 				errs += repquota(fs, USRQUOTA);
179 		}
180 	}
181 	endfsent();
182 	for (i = 0; i < argc; i++)
183 		if ((done & (1 << i)) == 0)
184 			warnx("%s not found in fstab", argv[i]);
185 	exit(errs);
186 }
187 
188 static void
189 usage(void)
190 {
191 	fprintf(stderr, "%s\n%s\n",
192 		"usage: repquota [-h] [-v] [-g] [-n] [-u] -a",
193 		"       repquota [-h] [-v] [-g] [-n] [-u] filesystem ...");
194 	exit(1);
195 }
196 
197 int
198 repquota(struct fstab *fs, int type)
199 {
200 	struct fileusage *fup;
201 	struct quotafile *qf;
202 	u_long id, maxid;
203 	struct dqblk dqbuf;
204 	static int multiple = 0;
205 
206 	if ((qf = quota_open(fs, type, O_RDONLY)) == NULL) {
207 		if (vflag && !aflag) {
208 			if (multiple++)
209 				printf("\n");
210 			fprintf(stdout, "*** No %s quotas on %s (%s)\n",
211 			    qfextension[type], fs->fs_file, fs->fs_spec);
212 			return(1);
213 		}
214 		return(0);
215 	}
216 	if (multiple++)
217 		printf("\n");
218 	if (vflag)
219 		fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
220 		    qfextension[type], fs->fs_file, fs->fs_spec);
221 	printf("%*s           Block  limits                    File  limits\n",
222 		max(MAXLOGNAME - 1, 10), " ");
223 	printf("User%*s  used   soft   hard  grace     used    soft    hard  grace\n",
224 		max(MAXLOGNAME - 1, 10), " ");
225 	maxid = quota_maxid(qf);
226 	for (id = 0; id <= maxid; id++) {
227 		if (quota_read(qf, &dqbuf, id) != 0)
228 			break;
229 		if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
230 			continue;
231 		if ((fup = lookup(id, type)) == 0)
232 			fup = addid(id, type, (char *)0);
233 		printf("%-*s ", max(MAXLOGNAME - 1, 10), fup->fu_name);
234 		printf("%c%c",
235 		    dqbuf.dqb_bsoftlimit &&
236 		    dqbuf.dqb_curblocks >=
237 		    dqbuf.dqb_bsoftlimit ? '+' : '-',
238 		    dqbuf.dqb_isoftlimit &&
239 		    dqbuf.dqb_curinodes >=
240 		    dqbuf.dqb_isoftlimit ? '+' : '-');
241 		prthumanval(dqbuf.dqb_curblocks);
242 		prthumanval(dqbuf.dqb_bsoftlimit);
243 		prthumanval(dqbuf.dqb_bhardlimit);
244 		printf(" %6s",
245 		    dqbuf.dqb_bsoftlimit &&
246 		    dqbuf.dqb_curblocks >=
247 		    dqbuf.dqb_bsoftlimit ?
248 		    timeprt(dqbuf.dqb_btime) : "-");
249 		printf("  %7ju %7ju %7ju %6s\n",
250 		    (uintmax_t)dqbuf.dqb_curinodes,
251 		    (uintmax_t)dqbuf.dqb_isoftlimit,
252 		    (uintmax_t)dqbuf.dqb_ihardlimit,
253 		    dqbuf.dqb_isoftlimit &&
254 		    dqbuf.dqb_curinodes >=
255 		    dqbuf.dqb_isoftlimit ?
256 		    timeprt(dqbuf.dqb_itime) : "-");
257 	}
258 	quota_close(qf);
259 	return (0);
260 }
261 
262 static void
263 prthumanval(int64_t blocks)
264 {
265 	char buf[7];
266 	int flags;
267 
268 	if (!hflag) {
269 		printf(" %6ju", (uintmax_t)dbtokb(blocks));
270 		return;
271 	}
272 	flags = HN_NOSPACE | HN_DECIMAL;
273 	if (blocks != 0)
274 		flags |= HN_B;
275 	humanize_number(buf, sizeof(buf) - (blocks < 0 ? 0 : 1),
276 	    dbtob(blocks), "", HN_AUTOSCALE, flags);
277 	(void)printf("%7s", buf);
278 }
279 
280 /*
281  * Check to see if target appears in list of size cnt.
282  */
283 int
284 oneof(char *target, char *list[], int cnt)
285 {
286 	int i;
287 
288 	for (i = 0; i < cnt; i++)
289 		if (strcmp(target, list[i]) == 0)
290 			return (i);
291 	return (-1);
292 }
293 
294 /*
295  * Routines to manage the file usage table.
296  *
297  * Lookup an id of a specific type.
298  */
299 struct fileusage *
300 lookup(u_long id, int type)
301 {
302 	struct fileusage *fup;
303 
304 	for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
305 		if (fup->fu_id == id)
306 			return (fup);
307 	return ((struct fileusage *)0);
308 }
309 
310 /*
311  * Add a new file usage id if it does not already exist.
312  */
313 struct fileusage *
314 addid(u_long id, int type, char *name)
315 {
316 	struct fileusage *fup, **fhp;
317 	int len;
318 
319 	if ((fup = lookup(id, type)))
320 		return (fup);
321 	if (name)
322 		len = strlen(name);
323 	else
324 		len = 10;
325 	if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL)
326 		errx(1, "out of memory for fileusage structures");
327 	fhp = &fuhead[type][id & (FUHASH - 1)];
328 	fup->fu_next = *fhp;
329 	*fhp = fup;
330 	fup->fu_id = id;
331 	if (id > highid[type])
332 		highid[type] = id;
333 	if (name) {
334 		bcopy(name, fup->fu_name, len + 1);
335 	} else {
336 		sprintf(fup->fu_name, "%lu", id);
337 	}
338 	return (fup);
339 }
340 
341 /*
342  * Calculate the grace period and return a printable string for it.
343  */
344 char *
345 timeprt(time_t seconds)
346 {
347 	time_t hours, minutes;
348 	static char buf[20];
349 	static time_t now;
350 
351 	if (now == 0)
352 		time(&now);
353 	if (now > seconds) {
354 		strlcpy(buf, "none", sizeof (buf));
355 		return (buf);
356 	}
357 	seconds -= now;
358 	minutes = (seconds + 30) / 60;
359 	hours = (minutes + 30) / 60;
360 	if (hours >= 36) {
361 		sprintf(buf, "%lddays", (long)(hours + 12) / 24);
362 		return (buf);
363 	}
364 	if (minutes >= 60) {
365 		sprintf(buf, "%2ld:%ld", (long)minutes / 60,
366 		    (long)minutes % 60);
367 		return (buf);
368 	}
369 	sprintf(buf, "%2ld", (long)minutes);
370 	return (buf);
371 }
372