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