/
repquota.c
573 lines (512 loc) · 13.4 KB
/
repquota.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/* $NetBSD: repquota.c,v 1.44 2012/05/12 19:47:26 dholland Exp $ */
/*
* Copyright (c) 1980, 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Robert Elz at The University of Melbourne.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)repquota.c 8.2 (Berkeley) 11/22/94";
#else
__RCSID("$NetBSD: repquota.c,v 1.44 2012/05/12 19:47:26 dholland Exp $");
#endif
#endif /* not lint */
/*
* Quota report
*/
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/statvfs.h>
#include <errno.h>
#include <err.h>
#include <fstab.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <quota.h>
#include "printquota.h"
/*
* XXX. Ideally we shouldn't compile either of these in, but it's a
* nontrivial rework to avoid it and it'll work ok for now.
*/
#define REPQUOTA_NUMIDTYPES 2
#define REPQUOTA_NUMOBJTYPES 2
struct fileusage {
struct fileusage *fu_next;
struct quotaval fu_qv[REPQUOTA_NUMOBJTYPES];
uint32_t fu_id;
char fu_name[1];
/* actually bigger */
};
#define FUHASH 1024 /* must be power of two */
static struct fileusage *fuhead[REPQUOTA_NUMIDTYPES][FUHASH];
/* highest addid()'ed identifier per idtype */
static uint32_t highid[REPQUOTA_NUMIDTYPES];
int valid[REPQUOTA_NUMIDTYPES];
static struct quotaval defaultqv[REPQUOTA_NUMIDTYPES][REPQUOTA_NUMOBJTYPES];
static int vflag = 0; /* verbose */
static int aflag = 0; /* all file systems */
static int hflag = 0; /* humanize */
static int xflag = 0; /* export */
/*
* XXX this should go away and be replaced with a call to
* quota_idtype_getname(), but that needs a quotahandle and requires
* the same nontrivial rework as getting rid of REPQUOTA_NUMIDTYPES.
*/
static const char *const repquota_idtype_names[REPQUOTA_NUMIDTYPES] = {
"user",
"group",
};
static struct fileusage *addid(uint32_t, int, const char *);
static struct fileusage *lookup(uint32_t, int);
static struct fileusage *qremove(uint32_t, int);
static int repquota(struct quotahandle *, int);
static void usage(void) __attribute__((__noreturn__));
static void printquotas(int, struct quotahandle *);
static void exportquotas(void);
static int oneof(const char *, char *[], int cnt);
static int isover(struct quotaval *qv, time_t now);
int
main(int argc, char **argv)
{
int gflag = 0, uflag = 0, errs = 0;
long i, argnum, done = 0;
int ch;
struct statvfs *fst;
int nfst;
struct quotahandle *qh;
if (!strcmp(getprogname(), "quotadump")) {
xflag = 1;
}
while ((ch = getopt(argc, argv, "aguhvx")) != -1) {
switch(ch) {
case 'a':
aflag++;
break;
case 'g':
gflag++;
break;
case 'u':
uflag++;
break;
case 'h':
hflag++;
break;
case 'v':
vflag++;
break;
case 'x':
xflag++;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (xflag && (argc != 1 || aflag))
usage();
if (argc == 0 && !aflag)
usage();
if (!gflag && !uflag) {
if (aflag)
gflag++;
uflag++;
}
nfst = getmntinfo(&fst, MNT_WAIT);
if (nfst == 0)
errx(1, "no filesystems mounted!");
for (i = 0; i < nfst; i++) {
if ((fst[i].f_flag & ST_QUOTA) == 0)
continue;
/* check if we want this volume */
if (!aflag) {
argnum = oneof(fst[i].f_mntonname, argv, argc);
if (argnum < 0) {
argnum = oneof(fst[i].f_mntfromname,
argv, argc);
}
if (argnum < 0) {
continue;
}
done |= 1U << argnum;
}
qh = quota_open(fst[i].f_mntonname);
if (qh == NULL) {
/* XXX: check this errno */
if (errno == EOPNOTSUPP || errno == ENXIO) {
continue;
}
warn("%s: quota_open", fst[i].f_mntonname);
continue;
}
if (gflag)
errs += repquota(qh, QUOTA_IDTYPE_GROUP);
if (uflag)
errs += repquota(qh, QUOTA_IDTYPE_USER);
quota_close(qh);
}
if (xflag)
exportquotas();
for (i = 0; i < argc; i++)
if ((done & (1U << i)) == 0)
warnx("%s not mounted", argv[i]);
return errs;
}
static void
usage(void)
{
const char *p = getprogname();
fprintf(stderr, "usage: %s [-D] [-v] [-g] [-u] -a\n"
"\t%s [-D] [-v] [-g] [-u] filesys ...\n"
"\t%s -x [-D] [-g] [-u] filesys\n", p, p, p);
exit(1);
}
static int
repquota(struct quotahandle *qh, int idtype)
{
struct quotacursor *qc;
struct quotakey qk;
struct quotaval qv;
struct quotaval *qvp;
struct fileusage *fup;
qc = quota_opencursor(qh);
if (qc == NULL) {
return 1;
}
if (idtype == QUOTA_IDTYPE_USER) {
quotacursor_skipidtype(qc, QUOTA_IDTYPE_GROUP);
}
if (idtype == QUOTA_IDTYPE_GROUP) {
quotacursor_skipidtype(qc, QUOTA_IDTYPE_USER);
}
valid[idtype] = 0;
while (!quotacursor_atend(qc)) {
if (quotacursor_get(qc, &qk, &qv)) {
err(1, "%s: quotacursor_get", quota_getmountpoint(qh));
}
if (qk.qk_idtype != idtype) {
continue;
}
valid[idtype] = 1;
if (qk.qk_id == QUOTA_DEFAULTID) {
qvp = defaultqv[idtype];
} else {
if ((fup = lookup(qk.qk_id, idtype)) == 0)
fup = addid(qk.qk_id, idtype, (char *)0);
qvp = fup->fu_qv;
}
if (qk.qk_objtype == QUOTA_OBJTYPE_BLOCKS) {
qvp[QUOTA_OBJTYPE_BLOCKS] = qv;
} else if (qk.qk_objtype == QUOTA_OBJTYPE_FILES) {
qvp[QUOTA_OBJTYPE_FILES] = qv;
}
}
if (xflag == 0 && valid[idtype])
printquotas(idtype, qh);
return 0;
}
static void
printquotas(int idtype, struct quotahandle *qh)
{
static int multiple = 0;
uint32_t id;
int i;
struct fileusage *fup;
struct quotaval *q;
const char *timemsg[REPQUOTA_NUMOBJTYPES];
char overchar[REPQUOTA_NUMOBJTYPES];
time_t now;
char b0[2][20], b1[20], b2[20], b3[20];
int ok, objtype;
int isbytes, width;
switch (idtype) {
case QUOTA_IDTYPE_GROUP:
{
struct group *gr;
setgrent();
while ((gr = getgrent()) != 0)
(void)addid(gr->gr_gid, idtype, gr->gr_name);
endgrent();
break;
}
case QUOTA_IDTYPE_USER:
{
struct passwd *pw;
setpwent();
while ((pw = getpwent()) != 0)
(void)addid(pw->pw_uid, idtype, pw->pw_name);
endpwent();
break;
}
default:
errx(1, "Unknown quota ID type %d", idtype);
}
time(&now);
if (multiple++)
printf("\n");
if (vflag)
printf("*** Report for %s quotas on %s (%s: %s)\n",
repquota_idtype_names[idtype], quota_getmountpoint(qh),
quota_getmountdevice(qh), quota_getimplname(qh));
printf(" Block limits "
"File limits\n");
printf(idtype == QUOTA_IDTYPE_USER ? "User " : "Group");
printf(" used soft hard grace used"
" soft hard grace\n");
for (id = 0; id <= highid[idtype]; id++) {
fup = qremove(id, idtype);
q = fup->fu_qv;
if (fup == 0)
continue;
for (i = 0; i < REPQUOTA_NUMOBJTYPES; i++) {
if (isover(&q[i], now)) {
timemsg[i] = timeprt(b0[i], 8, now,
q[i].qv_expiretime);
overchar[i] = '+';
} else {
if (vflag && q[i].qv_grace != QUOTA_NOTIME) {
timemsg[i] = timeprt(b0[i], 8, 0,
q[i].qv_grace);
} else {
timemsg[i] = "";
}
overchar[i] = '-';
}
}
ok = 1;
for (objtype = 0; objtype < REPQUOTA_NUMOBJTYPES; objtype++) {
if (q[objtype].qv_usage != 0 ||
overchar[objtype] != '-') {
ok = 0;
}
}
if (ok && vflag == 0)
continue;
if (strlen(fup->fu_name) > 9)
printf("%s ", fup->fu_name);
else
printf("%-10s", fup->fu_name);
for (objtype = 0; objtype < REPQUOTA_NUMOBJTYPES; objtype++) {
printf("%c", overchar[objtype]);
}
for (objtype = 0; objtype < REPQUOTA_NUMOBJTYPES; objtype++) {
isbytes = quota_objtype_isbytes(qh, objtype);
width = isbytes ? 9 : 8;
printf("%*s%*s%*s%7s",
width,
intprt(b1, width+1, q[objtype].qv_usage,
isbytes ? HN_B : 0, hflag),
width,
intprt(b2, width+1, q[objtype].qv_softlimit,
isbytes ? HN_B : 0, hflag),
width,
intprt(b3, width+1, q[objtype].qv_hardlimit,
isbytes ? HN_B : 0, hflag),
timemsg[objtype]);
if (objtype + 1 < REPQUOTA_NUMOBJTYPES) {
printf(" ");
} else {
printf("\n");
}
}
free(fup);
}
}
static void
exportquotaval(const struct quotaval *qv)
{
if (qv->qv_hardlimit == QUOTA_NOLIMIT) {
printf(" -");
} else {
printf(" %llu", (unsigned long long)qv->qv_hardlimit);
}
if (qv->qv_softlimit == QUOTA_NOLIMIT) {
printf(" -");
} else {
printf(" %llu", (unsigned long long)qv->qv_softlimit);
}
printf(" %llu", (unsigned long long)qv->qv_usage);
if (qv->qv_expiretime == QUOTA_NOTIME) {
printf(" -");
} else {
printf(" %lld", (long long)qv->qv_expiretime);
}
if (qv->qv_grace == QUOTA_NOTIME) {
printf(" -");
} else {
printf(" %lld", (long long)qv->qv_grace);
}
}
static void
exportquotas(void)
{
int idtype;
id_t id;
struct fileusage *fup;
/* header */
printf("@format netbsd-quota-dump v1\n");
printf("# idtype id objtype hard soft usage expire grace\n");
for (idtype = 0; idtype < REPQUOTA_NUMIDTYPES; idtype++) {
if (valid[idtype] == 0)
continue;
printf("%s default block ", repquota_idtype_names[idtype]);
exportquotaval(&defaultqv[idtype][QUOTA_OBJTYPE_BLOCKS]);
printf("\n");
printf("%s default file ", repquota_idtype_names[idtype]);
exportquotaval(&defaultqv[idtype][QUOTA_OBJTYPE_FILES]);
printf("\n");
for (id = 0; id <= highid[idtype]; id++) {
fup = qremove(id, idtype);
if (fup == 0)
continue;
printf("%s %u block ", repquota_idtype_names[idtype],
id);
exportquotaval(&fup->fu_qv[QUOTA_OBJTYPE_BLOCKS]);
printf("\n");
printf("%s %u file ", repquota_idtype_names[idtype],
id);
exportquotaval(&fup->fu_qv[QUOTA_OBJTYPE_FILES]);
printf("\n");
free(fup);
}
}
printf("@end\n");
}
/*
* Routines to manage the file usage table.
*
* Lookup an id of a specific id type.
*/
struct fileusage *
lookup(uint32_t id, int idtype)
{
struct fileusage *fup;
for (fup = fuhead[idtype][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
if (fup->fu_id == id)
return fup;
return NULL;
}
/*
* Lookup and remove an id of a specific id type.
*/
static struct fileusage *
qremove(uint32_t id, int idtype)
{
struct fileusage *fup, **fupp;
for (fupp = &fuhead[idtype][id & (FUHASH-1)]; *fupp != 0;) {
fup = *fupp;
if (fup->fu_id == id) {
*fupp = fup->fu_next;
return fup;
}
fupp = &fup->fu_next;
}
return NULL;
}
/*
* Add a new file usage id if it does not already exist.
*/
static struct fileusage *
addid(uint32_t id, int idtype, const char *name)
{
struct fileusage *fup, **fhp;
struct group *gr = NULL;
struct passwd *pw = NULL;
size_t len;
if ((fup = lookup(id, idtype)) != NULL) {
return fup;
}
if (name == NULL) {
switch(idtype) {
case QUOTA_IDTYPE_GROUP:
gr = getgrgid(id);
if (gr != NULL)
name = gr->gr_name;
break;
case QUOTA_IDTYPE_USER:
pw = getpwuid(id);
if (pw)
name = pw->pw_name;
break;
default:
errx(1, "Unknown quota ID type %d\n", idtype);
}
}
if (name)
len = strlen(name);
else
len = 10;
if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
err(1, "out of memory for fileusage structures");
fhp = &fuhead[idtype][id & (FUHASH - 1)];
fup->fu_next = *fhp;
*fhp = fup;
fup->fu_id = id;
if (id > highid[idtype])
highid[idtype] = id;
if (name) {
memmove(fup->fu_name, name, len + 1);
} else {
snprintf(fup->fu_name, len + 1, "%u", id);
}
/*
* XXX nothing guarantees the default limits have been loaded yet
*/
fup->fu_qv[QUOTA_OBJTYPE_BLOCKS] = defaultqv[idtype][QUOTA_OBJTYPE_BLOCKS];
fup->fu_qv[QUOTA_OBJTYPE_FILES] = defaultqv[idtype][QUOTA_OBJTYPE_FILES];
return fup;
}
/*
* Check to see if target appears in list of size cnt.
*/
static int
oneof(const char *target, char *list[], int cnt)
{
int i;
for (i = 0; i < cnt; i++)
if (strcmp(target, list[i]) == 0)
return i;
return -1;
}
static int
isover(struct quotaval *qv, time_t now)
{
return (qv->qv_usage >= qv->qv_hardlimit ||
qv->qv_usage >= qv->qv_softlimit);
}