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
|
/*
* 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.
*/
/*
* Quota report
*/
#include <sys/param.h> /* dbtob */
#include <sys/stat.h>
#include <ufs/ufs/quota.h>
#include <fstab.h>
#include <pwd.h>
#include <grp.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
char *qfname = QUOTAFILENAME;
char *qfextension[] = INITQFNAMES;
struct fileusage {
struct fileusage *fu_next;
struct dqblk fu_dqblk;
uid_t fu_id;
char fu_name[1];
/* actually bigger */
};
#define FUHASH 1024 /* must be power of two */
struct fileusage *fuhead[MAXQUOTAS][FUHASH];
struct fileusage *lookup(uid_t, int);
struct fileusage *addid(uid_t id, int type, char *name);
uid_t highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
int vflag; /* verbose */
int aflag; /* all file systems */
void usage(void);
int repquota(struct fstab *, int, char *);
int hasquota(struct fstab *, int, char **);
int oneof(char *, char *[], int);
char *timeprt(time_t);
int
main(int argc, char *argv[])
{
struct fstab *fs;
struct passwd *pw;
struct group *gr;
int gflag = 0, uflag = 0, errs = 0;
long i, argnum, done = 0;
extern char *optarg;
extern int optind;
char *qfnp;
int ch;
while ((ch = getopt(argc, argv, "aguv")) != -1) {
switch(ch) {
case 'a':
aflag++;
break;
case 'g':
gflag++;
break;
case 'u':
uflag++;
break;
case 'v':
vflag++;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if ((argc == 0) == (aflag == 0))
usage();
if (!gflag && !uflag) {
if (aflag)
gflag++;
uflag++;
}
if (gflag) {
setgrent();
while ((gr = getgrent()) != 0)
(void) addid((uid_t)gr->gr_gid, GRPQUOTA, gr->gr_name);
endgrent();
}
if (uflag) {
setpwent();
while ((pw = getpwent()) != 0)
(void) addid(pw->pw_uid, USRQUOTA, pw->pw_name);
endpwent();
}
setfsent();
while ((fs = getfsent()) != NULL) {
if (strcmp(fs->fs_vfstype, "ffs") &&
strcmp(fs->fs_vfstype, "ufs") &&
strcmp(fs->fs_vfstype, "mfs"))
continue;
if (aflag) {
if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
errs += repquota(fs, GRPQUOTA, qfnp);
if (uflag && hasquota(fs, USRQUOTA, &qfnp))
errs += repquota(fs, USRQUOTA, qfnp);
continue;
}
if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
(argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
done |= 1 << argnum;
if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
errs += repquota(fs, GRPQUOTA, qfnp);
if (uflag && hasquota(fs, USRQUOTA, &qfnp))
errs += repquota(fs, USRQUOTA, qfnp);
}
}
endfsent();
for (i = 0; i < argc; i++)
if ((done & (1 << i)) == 0)
fprintf(stderr, "%s not found in fstab\n", argv[i]);
exit(errs);
}
void
usage(void)
{
extern char *__progname;
fprintf(stderr, "usage: %s [-aguv] filesystem ...\n", __progname);
exit(1);
}
int
repquota(struct fstab *fs, int type, char *qfpathname)
{
struct fileusage *fup;
FILE *qf;
uid_t id;
struct dqblk dqbuf;
char *timeprt(time_t);
static struct dqblk zerodqblk;
static int warned = 0;
static int multiple = 0;
if (quotactl(fs->fs_file, QCMD(Q_SYNC, type), 0, 0) < 0 &&
errno == EOPNOTSUPP && !warned && vflag) {
warned++;
fprintf(stdout,
"*** Warning: Quotas are not compiled into this kernel\n");
}
if (multiple++)
printf("\n");
if (vflag)
fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
qfextension[type], fs->fs_file, fs->fs_spec);
if ((qf = fopen(qfpathname, "r")) == NULL) {
perror(qfpathname);
return (1);
}
for (id = 0; ; id++) {
fread(&dqbuf, sizeof(struct dqblk), 1, qf);
if (feof(qf))
break;
if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
continue;
if ((fup = lookup(id, type)) == 0)
fup = addid(id, type, NULL);
fup->fu_dqblk = dqbuf;
}
fclose(qf);
printf(" KByte limits File limits\n");
printf("User used soft hard grace used soft hard grace\n");
for (id = 0; id <= highid[type]; id++) {
fup = lookup(id, type);
if (fup == 0)
continue;
if (fup->fu_dqblk.dqb_curinodes == 0 &&
fup->fu_dqblk.dqb_curblocks == 0)
continue;
printf("%-10s", fup->fu_name);
printf("%c%c %7d %7d %7d %6s",
fup->fu_dqblk.dqb_bsoftlimit &&
fup->fu_dqblk.dqb_curblocks >=
fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-',
fup->fu_dqblk.dqb_isoftlimit &&
fup->fu_dqblk.dqb_curinodes >=
fup->fu_dqblk.dqb_isoftlimit ? '+' : '-',
(int)(dbtob((u_quad_t)fup->fu_dqblk.dqb_curblocks)
/ 1024),
(int)(dbtob((u_quad_t)fup->fu_dqblk.dqb_bsoftlimit)
/ 1024),
(int)(dbtob((u_quad_t)fup->fu_dqblk.dqb_bhardlimit)
/ 1024),
fup->fu_dqblk.dqb_bsoftlimit &&
fup->fu_dqblk.dqb_curblocks >=
fup->fu_dqblk.dqb_bsoftlimit ?
timeprt(fup->fu_dqblk.dqb_btime) : "");
printf(" %6d %5d %5d %6s\n",
fup->fu_dqblk.dqb_curinodes,
fup->fu_dqblk.dqb_isoftlimit,
fup->fu_dqblk.dqb_ihardlimit,
fup->fu_dqblk.dqb_isoftlimit &&
fup->fu_dqblk.dqb_curinodes >=
fup->fu_dqblk.dqb_isoftlimit ?
timeprt(fup->fu_dqblk.dqb_itime) : "");
fup->fu_dqblk = zerodqblk;
}
return (0);
}
/*
* Check to see if target appears in list of size cnt.
*/
int
oneof(char *target, char *list[], int cnt)
{
int i;
for (i = 0; i < cnt; i++)
if (strcmp(target, list[i]) == 0)
return (i);
return (-1);
}
/*
* Check to see if a particular quota is to be enabled.
*/
int
hasquota(struct fstab *fs, int type, char **qfnamep)
{
char *opt;
char *cp;
static char initname, usrname[100], grpname[100];
static char buf[BUFSIZ];
if (!initname) {
(void)snprintf(usrname, sizeof usrname, "%s%s",
qfextension[USRQUOTA], qfname);
(void)snprintf(grpname, sizeof grpname, "%s%s",
qfextension[GRPQUOTA], qfname);
initname = 1;
}
strlcpy(buf, fs->fs_mntops, sizeof buf);
for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
if ((cp = strchr(opt, '=')))
*cp++ = '\0';
if (type == USRQUOTA && strcmp(opt, usrname) == 0)
break;
if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
break;
}
if (!opt)
return (0);
if (cp) {
*qfnamep = cp;
return (1);
}
(void)snprintf(buf, sizeof buf, "%s/%s.%s",
fs->fs_file, qfname, qfextension[type]);
*qfnamep = buf;
return (1);
}
/*
* Routines to manage the file usage table.
*
* Lookup an id of a specific type.
*/
struct fileusage *
lookup(uid_t id, int type)
{
struct fileusage *fup;
for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
if (fup->fu_id == id)
return (fup);
return (NULL);
}
/*
* Add a new file usage id if it does not already exist.
*/
struct fileusage *
addid(uid_t id, int type, char *name)
{
struct fileusage *fup, **fhp;
size_t len;
if ((fup = lookup(id, type)))
return (fup);
if (name)
len = strlen(name);
else
len = 10;
if ((fup = calloc(1, sizeof(*fup) + len)) == NULL) {
fprintf(stderr, "out of memory for fileusage structures\n");
exit(1);
}
fhp = &fuhead[type][id & (FUHASH - 1)];
fup->fu_next = *fhp;
*fhp = fup;
fup->fu_id = id;
if (id > highid[type])
highid[type] = id;
if (name) {
bcopy(name, fup->fu_name, len + 1);
} else {
snprintf(fup->fu_name, len, "%u", id);
}
return (fup);
}
/*
* Calculate the grace period and return a printable string for it.
*/
char *
timeprt(time_t seconds)
{
int hours, minutes;
static char buf[20];
static time_t now;
if (now == 0)
time(&now);
if (now > seconds)
return ("none");
seconds -= now;
minutes = (seconds + 30) / 60;
hours = (minutes + 30) / 60;
if (hours >= 36) {
snprintf(buf, sizeof buf, "%ddays", (hours + 12) / 24);
return (buf);
}
if (minutes >= 60) {
snprintf(buf, sizeof buf, "%2d:%d", minutes / 60,
minutes % 60);
return (buf);
}
snprintf(buf, sizeof buf, "%2d", minutes);
return (buf);
}
|