xref: /freebsd/usr.sbin/repquota/repquota.c (revision 6bfca4dcab07dad45a805879d954876b353c0810)
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 /*
36  * Quota report
37  */
38 #include <sys/param.h>
39 #include <sys/mount.h>
40 
41 #include <ufs/ufs/quota.h>
42 
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <fstab.h>
47 #include <grp.h>
48 #include <libutil.h>
49 #include <pwd.h>
50 #include <stdint.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <time.h>
55 #include <unistd.h>
56 
57 /* Let's be paranoid about block size */
58 #if 10 > DEV_BSHIFT
59 #define dbtokb(db) \
60 	((off_t)(db) >> (10-DEV_BSHIFT))
61 #elif 10 < DEV_BSHIFT
62 #define dbtokb(db) \
63 	((off_t)(db) << (DEV_BSHIFT-10))
64 #else
65 #define dbtokb(db)	(db)
66 #endif
67 
68 #define max(a,b) ((a) >= (b) ? (a) : (b))
69 
70 static const char *qfextension[] = INITQFNAMES;
71 
72 struct fileusage {
73 	struct	fileusage *fu_next;
74 	u_long	fu_id;
75 	char	fu_name[1];
76 	/* actually bigger */
77 };
78 #define FUHASH 1024	/* must be power of two */
79 static struct fileusage *fuhead[MAXQUOTAS][FUHASH];
80 static struct fileusage *lookup(u_long, int);
81 static struct fileusage *addid(u_long, int, char *);
82 static u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
83 
84 static int	vflag;		/* verbose */
85 static int	aflag;		/* all filesystems */
86 static int	nflag;		/* display user/group by id */
87 static int	hflag;		/* display in human readable format */
88 
89 int oneof(char *, char *[], int);
90 int repquota(struct fstab *, int);
91 char *timeprt(time_t);
92 static void prthumanval(int64_t bytes);
93 static void usage(void) __dead2;
94 
95 int
96 main(int argc, char *argv[])
97 {
98 	struct fstab *fs;
99 	struct passwd *pw;
100 	struct group *gr;
101 	int ch, gflag = 0, uflag = 0, errs = 0;
102 	long i, argnum, done = 0;
103 
104 	while ((ch = getopt(argc, argv, "aghnuv")) != -1) {
105 		switch(ch) {
106 		case 'a':
107 			aflag++;
108 			break;
109 		case 'g':
110 			gflag++;
111 			break;
112 		case 'h':
113 			hflag++;
114 			break;
115 		case 'n':
116 			nflag++;
117 			break;
118 		case 'u':
119 			uflag++;
120 			break;
121 		case 'v':
122 			vflag++;
123 			break;
124 		default:
125 			usage();
126 		}
127 	}
128 	argc -= optind;
129 	argv += optind;
130 	if (argc == 0 && !aflag)
131 		usage();
132 	if (!gflag && !uflag) {
133 		if (aflag)
134 			gflag++;
135 		uflag++;
136 	}
137 	if (gflag && !nflag) {
138 		setgrent();
139 		while ((gr = getgrent()) != 0)
140 			(void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
141 		endgrent();
142 	}
143 	if (uflag && !nflag) {
144 		setpwent();
145 		while ((pw = getpwent()) != 0)
146 			(void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
147 		endpwent();
148 	}
149 	setfsent();
150 	while ((fs = getfsent()) != NULL) {
151 		if (strcmp(fs->fs_vfstype, "ufs"))
152 			continue;
153 		if (aflag) {
154 			if (gflag)
155 				errs += repquota(fs, GRPQUOTA);
156 			if (uflag)
157 				errs += repquota(fs, USRQUOTA);
158 			continue;
159 		}
160 		if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
161 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
162 			done |= 1 << argnum;
163 			if (gflag)
164 				errs += repquota(fs, GRPQUOTA);
165 			if (uflag)
166 				errs += repquota(fs, USRQUOTA);
167 		}
168 	}
169 	endfsent();
170 	for (i = 0; i < argc; i++)
171 		if ((done & (1 << i)) == 0)
172 			warnx("%s not found in fstab", argv[i]);
173 	exit(errs);
174 }
175 
176 static void
177 usage(void)
178 {
179 	fprintf(stderr, "%s\n%s\n",
180 		"usage: repquota [-h] [-v] [-g] [-n] [-u] -a",
181 		"       repquota [-h] [-v] [-g] [-n] [-u] filesystem ...");
182 	exit(1);
183 }
184 
185 int
186 repquota(struct fstab *fs, int type)
187 {
188 	struct fileusage *fup;
189 	struct quotafile *qf;
190 	u_long id, maxid;
191 	struct dqblk dqbuf;
192 	static int multiple = 0;
193 
194 	if ((qf = quota_open(fs, type, O_RDONLY)) == NULL) {
195 		if (vflag && !aflag) {
196 			if (multiple++)
197 				printf("\n");
198 			fprintf(stdout, "*** No %s quotas on %s (%s)\n",
199 			    qfextension[type], fs->fs_file, fs->fs_spec);
200 			return(1);
201 		}
202 		return(0);
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 	printf("%*s           Block  limits                    File  limits\n",
210 		max(MAXLOGNAME - 1, 10), " ");
211 	printf("User%*s  used   soft   hard  grace     used    soft    hard  grace\n",
212 		max(MAXLOGNAME - 1, 10), " ");
213 	maxid = quota_maxid(qf);
214 	for (id = 0; id <= maxid; id++) {
215 		if (quota_read(qf, &dqbuf, id) != 0)
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 		printf("%-*s ", max(MAXLOGNAME - 1, 10), fup->fu_name);
222 		printf("%c%c",
223 		    dqbuf.dqb_bsoftlimit &&
224 		    dqbuf.dqb_curblocks >=
225 		    dqbuf.dqb_bsoftlimit ? '+' : '-',
226 		    dqbuf.dqb_isoftlimit &&
227 		    dqbuf.dqb_curinodes >=
228 		    dqbuf.dqb_isoftlimit ? '+' : '-');
229 		prthumanval(dqbuf.dqb_curblocks);
230 		prthumanval(dqbuf.dqb_bsoftlimit);
231 		prthumanval(dqbuf.dqb_bhardlimit);
232 		printf(" %6s",
233 		    dqbuf.dqb_bsoftlimit &&
234 		    dqbuf.dqb_curblocks >=
235 		    dqbuf.dqb_bsoftlimit ?
236 		    timeprt(dqbuf.dqb_btime) : "-");
237 		printf("  %7ju %7ju %7ju %6s\n",
238 		    (uintmax_t)dqbuf.dqb_curinodes,
239 		    (uintmax_t)dqbuf.dqb_isoftlimit,
240 		    (uintmax_t)dqbuf.dqb_ihardlimit,
241 		    dqbuf.dqb_isoftlimit &&
242 		    dqbuf.dqb_curinodes >=
243 		    dqbuf.dqb_isoftlimit ?
244 		    timeprt(dqbuf.dqb_itime) : "-");
245 	}
246 	quota_close(qf);
247 	return (0);
248 }
249 
250 static void
251 prthumanval(int64_t blocks)
252 {
253 	char buf[7];
254 	int flags;
255 
256 	if (!hflag) {
257 		printf(" %6ju", (uintmax_t)dbtokb(blocks));
258 		return;
259 	}
260 	flags = HN_NOSPACE | HN_DECIMAL;
261 	if (blocks != 0)
262 		flags |= HN_B;
263 	humanize_number(buf, sizeof(buf) - (blocks < 0 ? 0 : 1),
264 	    dbtob(blocks), "", HN_AUTOSCALE, flags);
265 	(void)printf("%7s", buf);
266 }
267 
268 /*
269  * Check to see if target appears in list of size cnt.
270  */
271 int
272 oneof(char *target, char *list[], int cnt)
273 {
274 	int i;
275 
276 	for (i = 0; i < cnt; i++)
277 		if (strcmp(target, list[i]) == 0)
278 			return (i);
279 	return (-1);
280 }
281 
282 /*
283  * Routines to manage the file usage table.
284  *
285  * Lookup an id of a specific type.
286  */
287 struct fileusage *
288 lookup(u_long id, int type)
289 {
290 	struct fileusage *fup;
291 
292 	for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
293 		if (fup->fu_id == id)
294 			return (fup);
295 	return ((struct fileusage *)0);
296 }
297 
298 /*
299  * Add a new file usage id if it does not already exist.
300  */
301 struct fileusage *
302 addid(u_long id, int type, char *name)
303 {
304 	struct fileusage *fup, **fhp;
305 	int len;
306 
307 	if ((fup = lookup(id, type)))
308 		return (fup);
309 	if (name)
310 		len = strlen(name);
311 	else
312 		len = 10;
313 	if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL)
314 		errx(1, "out of memory for fileusage structures");
315 	fhp = &fuhead[type][id & (FUHASH - 1)];
316 	fup->fu_next = *fhp;
317 	*fhp = fup;
318 	fup->fu_id = id;
319 	if (id > highid[type])
320 		highid[type] = id;
321 	if (name) {
322 		bcopy(name, fup->fu_name, len + 1);
323 	} else {
324 		sprintf(fup->fu_name, "%lu", id);
325 	}
326 	return (fup);
327 }
328 
329 /*
330  * Calculate the grace period and return a printable string for it.
331  */
332 char *
333 timeprt(time_t seconds)
334 {
335 	time_t hours, minutes;
336 	static char buf[20];
337 	static time_t now;
338 
339 	if (now == 0)
340 		time(&now);
341 	if (now > seconds) {
342 		strlcpy(buf, "none", sizeof (buf));
343 		return (buf);
344 	}
345 	seconds -= now;
346 	minutes = (seconds + 30) / 60;
347 	hours = (minutes + 30) / 60;
348 	if (hours >= 36) {
349 		sprintf(buf, "%lddays", (long)(hours + 12) / 24);
350 		return (buf);
351 	}
352 	if (minutes >= 60) {
353 		sprintf(buf, "%2ld:%ld", (long)minutes / 60,
354 		    (long)minutes % 60);
355 		return (buf);
356 	}
357 	sprintf(buf, "%2ld", (long)minutes);
358 	return (buf);
359 }
360