quota.c revision 1.37.4.1 1 1.37.4.1 yamt /* $NetBSD: quota.c,v 1.37.4.1 2012/04/17 00:09:38 yamt Exp $ */
2 1.12 tls
3 1.1 cgd /*
4 1.5 mycroft * Copyright (c) 1980, 1990, 1993
5 1.5 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Robert Elz at The University of Melbourne.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.26 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.16 lukem #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.32 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
38 1.32 lukem The Regents of the University of California. All rights reserved.");
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #ifndef lint
42 1.15 mrg #if 0
43 1.15 mrg static char sccsid[] = "@(#)quota.c 8.4 (Berkeley) 4/28/95";
44 1.15 mrg #else
45 1.37.4.1 yamt __RCSID("$NetBSD: quota.c,v 1.37.4.1 2012/04/17 00:09:38 yamt Exp $");
46 1.15 mrg #endif
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.1 cgd /*
50 1.1 cgd * Disk quota reporting program.
51 1.1 cgd */
52 1.1 cgd #include <sys/param.h>
53 1.6 deraadt #include <sys/types.h>
54 1.1 cgd #include <sys/file.h>
55 1.1 cgd #include <sys/stat.h>
56 1.6 deraadt #include <sys/mount.h>
57 1.6 deraadt #include <sys/socket.h>
58 1.15 mrg
59 1.37.4.1 yamt #include <assert.h>
60 1.15 mrg #include <ctype.h>
61 1.16 lukem #include <err.h>
62 1.15 mrg #include <errno.h>
63 1.15 mrg #include <fstab.h>
64 1.15 mrg #include <grp.h>
65 1.15 mrg #include <netdb.h>
66 1.15 mrg #include <pwd.h>
67 1.1 cgd #include <stdio.h>
68 1.7 cgd #include <stdlib.h>
69 1.16 lukem #include <string.h>
70 1.18 kleink #include <time.h>
71 1.16 lukem #include <unistd.h>
72 1.1 cgd
73 1.37.4.1 yamt #include <quota.h>
74 1.6 deraadt
75 1.36 christos #include "printquota.h"
76 1.1 cgd
77 1.1 cgd struct quotause {
78 1.1 cgd struct quotause *next;
79 1.37 bouyer uid_t id;
80 1.37.4.1 yamt struct quotaval *qvs;
81 1.37.4.1 yamt unsigned numqvs;
82 1.1 cgd char fsname[MAXPATHLEN + 1];
83 1.37.4.1 yamt struct quotahandle *qh;
84 1.6 deraadt };
85 1.1 cgd
86 1.37.4.1 yamt static int anyusage(struct quotaval *, unsigned);
87 1.37.4.1 yamt static int anyover(struct quotaval *, unsigned, time_t);
88 1.37.4.1 yamt static const char *getovermsg(struct quotaval *, const char *, time_t);
89 1.37.4.1 yamt static struct quotause *getprivs(id_t, int);
90 1.37.4.1 yamt static void heading(int, const char *, id_t, const char *, const char *);
91 1.37.4.1 yamt static int isover(struct quotaval *qv, time_t now);
92 1.37.4.1 yamt static void printqv(struct quotaval *, int, time_t);
93 1.35 christos static void showgid(gid_t);
94 1.35 christos static void showgrpname(const char *);
95 1.37.4.1 yamt static void showonequota(int, const char *, id_t, const char *,
96 1.37.4.1 yamt struct quotause *);
97 1.37.4.1 yamt static void showquotas(int, const char *, id_t, const char *);
98 1.35 christos static void showuid(uid_t);
99 1.35 christos static void showusrname(const char *);
100 1.37.4.1 yamt static int unlimited(struct quotaval *qvs, unsigned numqvs);
101 1.37.4.1 yamt static void usage(void) __dead;
102 1.35 christos
103 1.35 christos static int qflag = 0;
104 1.35 christos static int vflag = 0;
105 1.35 christos static int hflag = 0;
106 1.35 christos static int dflag = 0;
107 1.35 christos static int Dflag = 0;
108 1.35 christos static uid_t myuid;
109 1.37.4.1 yamt static int needheading;
110 1.1 cgd
111 1.16 lukem int
112 1.35 christos main(int argc, char *argv[])
113 1.1 cgd {
114 1.3 jtc int ngroups;
115 1.8 mycroft gid_t mygid, gidset[NGROUPS];
116 1.1 cgd int i, gflag = 0, uflag = 0;
117 1.11 mark int ch;
118 1.1 cgd
119 1.19 mrg myuid = getuid();
120 1.34 bouyer while ((ch = getopt(argc, argv, "Ddhugvq")) != -1) {
121 1.1 cgd switch(ch) {
122 1.1 cgd case 'g':
123 1.1 cgd gflag++;
124 1.1 cgd break;
125 1.1 cgd case 'u':
126 1.1 cgd uflag++;
127 1.1 cgd break;
128 1.1 cgd case 'v':
129 1.1 cgd vflag++;
130 1.1 cgd break;
131 1.1 cgd case 'q':
132 1.1 cgd qflag++;
133 1.1 cgd break;
134 1.34 bouyer case 'h':
135 1.34 bouyer hflag++;
136 1.34 bouyer break;
137 1.34 bouyer case 'd':
138 1.34 bouyer dflag++;
139 1.34 bouyer break;
140 1.34 bouyer case 'D':
141 1.34 bouyer Dflag++;
142 1.34 bouyer break;
143 1.1 cgd default:
144 1.1 cgd usage();
145 1.1 cgd }
146 1.1 cgd }
147 1.1 cgd argc -= optind;
148 1.1 cgd argv += optind;
149 1.1 cgd if (!uflag && !gflag)
150 1.1 cgd uflag++;
151 1.34 bouyer if (dflag) {
152 1.34 bouyer #if 0
153 1.35 christos if (myuid != 0)
154 1.35 christos errx(1, "-d: permission denied");
155 1.34 bouyer #endif
156 1.34 bouyer if (uflag)
157 1.37.4.1 yamt showquotas(QUOTA_IDTYPE_USER, "user", 0, "");
158 1.34 bouyer if (gflag)
159 1.37.4.1 yamt showquotas(QUOTA_IDTYPE_GROUP, "group", 0, "");
160 1.35 christos return 0;
161 1.34 bouyer }
162 1.1 cgd if (argc == 0) {
163 1.1 cgd if (uflag)
164 1.19 mrg showuid(myuid);
165 1.1 cgd if (gflag) {
166 1.34 bouyer if (dflag)
167 1.34 bouyer showgid(0);
168 1.34 bouyer else {
169 1.34 bouyer mygid = getgid();
170 1.34 bouyer ngroups = getgroups(NGROUPS, gidset);
171 1.34 bouyer if (ngroups < 0)
172 1.34 bouyer err(1, "getgroups");
173 1.34 bouyer showgid(mygid);
174 1.34 bouyer for (i = 0; i < ngroups; i++)
175 1.34 bouyer if (gidset[i] != mygid)
176 1.34 bouyer showgid(gidset[i]);
177 1.34 bouyer }
178 1.1 cgd }
179 1.35 christos return 0;
180 1.1 cgd }
181 1.1 cgd if (uflag && gflag)
182 1.1 cgd usage();
183 1.1 cgd if (uflag) {
184 1.1 cgd for (; argc > 0; argc--, argv++) {
185 1.1 cgd if (alldigits(*argv))
186 1.35 christos showuid((uid_t)atoi(*argv));
187 1.1 cgd else
188 1.1 cgd showusrname(*argv);
189 1.1 cgd }
190 1.35 christos return 0;
191 1.1 cgd }
192 1.1 cgd if (gflag) {
193 1.1 cgd for (; argc > 0; argc--, argv++) {
194 1.1 cgd if (alldigits(*argv))
195 1.35 christos showgid((gid_t)atoi(*argv));
196 1.1 cgd else
197 1.1 cgd showgrpname(*argv);
198 1.1 cgd }
199 1.35 christos return 0;
200 1.1 cgd }
201 1.16 lukem /* NOTREACHED */
202 1.35 christos return 0;
203 1.1 cgd }
204 1.1 cgd
205 1.35 christos static void
206 1.35 christos usage(void)
207 1.1 cgd {
208 1.35 christos const char *p = getprogname();
209 1.35 christos fprintf(stderr, "Usage: %s [-Dhguqv]\n"
210 1.35 christos "\t%s [-Dhqv] -u username ...\n"
211 1.35 christos "\t%s [-Dhqv] -g groupname ...\n"
212 1.35 christos "\t%s -d [-Dhguqv]\n", p, p, p, p);
213 1.1 cgd exit(1);
214 1.1 cgd }
215 1.1 cgd
216 1.1 cgd /*
217 1.1 cgd * Print out quotas for a specified user identifier.
218 1.1 cgd */
219 1.35 christos static void
220 1.35 christos showuid(uid_t uid)
221 1.1 cgd {
222 1.1 cgd struct passwd *pwd = getpwuid(uid);
223 1.20 mycroft const char *name;
224 1.1 cgd
225 1.1 cgd if (pwd == NULL)
226 1.1 cgd name = "(no account)";
227 1.1 cgd else
228 1.1 cgd name = pwd->pw_name;
229 1.1 cgd if (uid != myuid && myuid != 0) {
230 1.35 christos warnx("%s (uid %d): permission denied", name, uid);
231 1.1 cgd return;
232 1.1 cgd }
233 1.37.4.1 yamt showquotas(QUOTA_IDTYPE_USER, "user", uid, name);
234 1.1 cgd }
235 1.1 cgd
236 1.1 cgd /*
237 1.24 wiz * Print out quotas for a specified user name.
238 1.1 cgd */
239 1.35 christos static void
240 1.35 christos showusrname(const char *name)
241 1.1 cgd {
242 1.1 cgd struct passwd *pwd = getpwnam(name);
243 1.1 cgd
244 1.1 cgd if (pwd == NULL) {
245 1.16 lukem warnx("%s: unknown user", name);
246 1.1 cgd return;
247 1.1 cgd }
248 1.1 cgd if (pwd->pw_uid != myuid && myuid != 0) {
249 1.16 lukem warnx("%s (uid %d): permission denied", name, pwd->pw_uid);
250 1.1 cgd return;
251 1.1 cgd }
252 1.37.4.1 yamt showquotas(QUOTA_IDTYPE_USER, "user", pwd->pw_uid, name);
253 1.1 cgd }
254 1.1 cgd
255 1.1 cgd /*
256 1.1 cgd * Print out quotas for a specified group identifier.
257 1.1 cgd */
258 1.35 christos static void
259 1.35 christos showgid(gid_t gid)
260 1.1 cgd {
261 1.1 cgd struct group *grp = getgrgid(gid);
262 1.3 jtc int ngroups;
263 1.8 mycroft gid_t mygid, gidset[NGROUPS];
264 1.16 lukem int i;
265 1.20 mycroft const char *name;
266 1.1 cgd
267 1.1 cgd if (grp == NULL)
268 1.1 cgd name = "(no entry)";
269 1.1 cgd else
270 1.1 cgd name = grp->gr_name;
271 1.8 mycroft mygid = getgid();
272 1.1 cgd ngroups = getgroups(NGROUPS, gidset);
273 1.1 cgd if (ngroups < 0) {
274 1.16 lukem warn("getgroups");
275 1.1 cgd return;
276 1.1 cgd }
277 1.8 mycroft if (gid != mygid) {
278 1.8 mycroft for (i = 0; i < ngroups; i++)
279 1.8 mycroft if (gid == gidset[i])
280 1.8 mycroft break;
281 1.19 mrg if (i >= ngroups && myuid != 0) {
282 1.16 lukem warnx("%s (gid %d): permission denied", name, gid);
283 1.8 mycroft return;
284 1.8 mycroft }
285 1.1 cgd }
286 1.37.4.1 yamt showquotas(QUOTA_IDTYPE_GROUP, "group", gid, name);
287 1.1 cgd }
288 1.1 cgd
289 1.1 cgd /*
290 1.24 wiz * Print out quotas for a specified group name.
291 1.1 cgd */
292 1.35 christos static void
293 1.35 christos showgrpname(const char *name)
294 1.1 cgd {
295 1.1 cgd struct group *grp = getgrnam(name);
296 1.3 jtc int ngroups;
297 1.8 mycroft gid_t mygid, gidset[NGROUPS];
298 1.16 lukem int i;
299 1.1 cgd
300 1.1 cgd if (grp == NULL) {
301 1.16 lukem warnx("%s: unknown group", name);
302 1.1 cgd return;
303 1.1 cgd }
304 1.8 mycroft mygid = getgid();
305 1.1 cgd ngroups = getgroups(NGROUPS, gidset);
306 1.1 cgd if (ngroups < 0) {
307 1.16 lukem warn("getgroups");
308 1.1 cgd return;
309 1.1 cgd }
310 1.8 mycroft if (grp->gr_gid != mygid) {
311 1.8 mycroft for (i = 0; i < ngroups; i++)
312 1.8 mycroft if (grp->gr_gid == gidset[i])
313 1.8 mycroft break;
314 1.19 mrg if (i >= ngroups && myuid != 0) {
315 1.16 lukem warnx("%s (gid %d): permission denied",
316 1.8 mycroft name, grp->gr_gid);
317 1.8 mycroft return;
318 1.8 mycroft }
319 1.1 cgd }
320 1.37.4.1 yamt showquotas(QUOTA_IDTYPE_GROUP, "group", grp->gr_gid, name);
321 1.1 cgd }
322 1.1 cgd
323 1.35 christos static void
324 1.37.4.1 yamt showquotas(int idtype, const char *idtypename, id_t id, const char *idname)
325 1.1 cgd {
326 1.16 lukem struct quotause *qup;
327 1.6 deraadt struct quotause *quplist;
328 1.37.4.1 yamt
329 1.37.4.1 yamt needheading = 1;
330 1.37.4.1 yamt
331 1.37.4.1 yamt quplist = getprivs(id, idtype);
332 1.37.4.1 yamt for (qup = quplist; qup; qup = qup->next) {
333 1.37.4.1 yamt showonequota(idtype, idtypename, id, idname, qup);
334 1.37.4.1 yamt }
335 1.37.4.1 yamt if (!qflag) {
336 1.37.4.1 yamt /* In case nothing printed, issue a header saying "none" */
337 1.37.4.1 yamt heading(idtype, idtypename, id, idname, "none");
338 1.37.4.1 yamt }
339 1.37.4.1 yamt }
340 1.37.4.1 yamt
341 1.37.4.1 yamt static void
342 1.37.4.1 yamt showonequota(int idtype, const char *idtypename, id_t id, const char *idname,
343 1.37.4.1 yamt struct quotause *qup)
344 1.37.4.1 yamt {
345 1.1 cgd static time_t now;
346 1.37.4.1 yamt struct quotaval *qvs;
347 1.37.4.1 yamt unsigned numqvs, i;
348 1.37.4.1 yamt const char *msg;
349 1.1 cgd
350 1.37.4.1 yamt qvs = qup->qvs;
351 1.37.4.1 yamt numqvs = qup->numqvs;
352 1.37.4.1 yamt
353 1.37.4.1 yamt if (now == 0) {
354 1.1 cgd time(&now);
355 1.37.4.1 yamt }
356 1.37.4.1 yamt
357 1.37.4.1 yamt if (!vflag && unlimited(qvs, numqvs)) {
358 1.37.4.1 yamt return;
359 1.37.4.1 yamt }
360 1.37.4.1 yamt
361 1.37.4.1 yamt if (qflag) {
362 1.37.4.1 yamt for (i=0; i<numqvs; i++) {
363 1.37.4.1 yamt msg = getovermsg(&qvs[i],
364 1.37.4.1 yamt quota_idtype_getname(qup->qh, i),
365 1.37.4.1 yamt now);
366 1.37.4.1 yamt if (msg != NULL) {
367 1.37.4.1 yamt heading(idtype, idtypename, id, idname, "");
368 1.37.4.1 yamt printf("\t%s %s\n", msg, qup->fsname);
369 1.37.4.1 yamt }
370 1.1 cgd }
371 1.37.4.1 yamt return;
372 1.37.4.1 yamt }
373 1.37.4.1 yamt
374 1.37.4.1 yamt /*
375 1.37.4.1 yamt * XXX this behavior appears to be demanded by the ATF tests,
376 1.37.4.1 yamt * although it seems to be at variance with the preexisting
377 1.37.4.1 yamt * logic in quota.c.
378 1.37.4.1 yamt */
379 1.37.4.1 yamt if (unlimited(qvs, numqvs) && !anyusage(qvs, numqvs)) {
380 1.37.4.1 yamt return;
381 1.37.4.1 yamt }
382 1.37.4.1 yamt
383 1.37.4.1 yamt /*
384 1.37.4.1 yamt * XXX: anyover can in fact be true if anyusage is not true,
385 1.37.4.1 yamt * if there's a quota of zero set on some volume. This is
386 1.37.4.1 yamt * because the check we do checks if adding one more thing
387 1.37.4.1 yamt * will go over. That is reasonable, I suppose, but arguably
388 1.37.4.1 yamt * the resulting behavior with usage 0 is a bug. (Also, what
389 1.37.4.1 yamt * reason do we have to believe that the reported grace expire
390 1.37.4.1 yamt * time is valid if we aren't in fact over yet?)
391 1.37.4.1 yamt */
392 1.37.4.1 yamt
393 1.37.4.1 yamt if (vflag || dflag || anyover(qvs, numqvs, now) ||
394 1.37.4.1 yamt anyusage(qvs, numqvs)) {
395 1.37.4.1 yamt heading(idtype, idtypename, id, idname, "");
396 1.37.4.1 yamt if (strlen(qup->fsname) > 4) {
397 1.37.4.1 yamt printf("%s\n", qup->fsname);
398 1.37.4.1 yamt printf("%12s", "");
399 1.37.4.1 yamt } else {
400 1.37.4.1 yamt printf("%12s", qup->fsname);
401 1.37.4.1 yamt }
402 1.37.4.1 yamt
403 1.37.4.1 yamt for (i=0; i<numqvs; i++) {
404 1.37.4.1 yamt printqv(&qvs[i],
405 1.37.4.1 yamt quota_objtype_isbytes(qup->qh, i), now);
406 1.1 cgd }
407 1.37.4.1 yamt printf("\n");
408 1.1 cgd }
409 1.1 cgd }
410 1.1 cgd
411 1.35 christos static void
412 1.37.4.1 yamt heading(int idtype, const char *idtypename, id_t id, const char *idname,
413 1.37.4.1 yamt const char *tag)
414 1.1 cgd {
415 1.37.4.1 yamt if (needheading == 0)
416 1.37.4.1 yamt return;
417 1.37.4.1 yamt needheading = 0;
418 1.37.4.1 yamt
419 1.34 bouyer if (dflag)
420 1.37.4.1 yamt printf("Default %s disk quotas: %s\n", idtypename, tag);
421 1.34 bouyer else
422 1.35 christos printf("Disk quotas for %s %s (%cid %u): %s\n",
423 1.37.4.1 yamt idtypename, idname, idtypename[0], id, tag);
424 1.1 cgd
425 1.1 cgd if (!qflag && tag[0] == '\0') {
426 1.22 bouyer printf("%12s%9s %8s%9s%8s%8s %7s%8s%8s\n"
427 1.22 bouyer , "Filesystem"
428 1.22 bouyer , "blocks"
429 1.22 bouyer , "quota"
430 1.22 bouyer , "limit"
431 1.22 bouyer , "grace"
432 1.22 bouyer , "files"
433 1.22 bouyer , "quota"
434 1.22 bouyer , "limit"
435 1.22 bouyer , "grace"
436 1.1 cgd );
437 1.1 cgd }
438 1.1 cgd }
439 1.1 cgd
440 1.37.4.1 yamt static void
441 1.37.4.1 yamt printqv(struct quotaval *qv, int isbytes, time_t now)
442 1.37.4.1 yamt {
443 1.37.4.1 yamt char buf[20];
444 1.37.4.1 yamt const char *str;
445 1.37.4.1 yamt int intprtflags, over, width;
446 1.37.4.1 yamt
447 1.37.4.1 yamt /*
448 1.37.4.1 yamt * The assorted finagling of width is to match the previous
449 1.37.4.1 yamt * open-coded formatting for exactly two quota object types,
450 1.37.4.1 yamt * which was chosen to make the default report fit in 80
451 1.37.4.1 yamt * columns.
452 1.37.4.1 yamt */
453 1.37.4.1 yamt
454 1.37.4.1 yamt width = isbytes ? 9 : 8;
455 1.37.4.1 yamt intprtflags = isbytes ? HN_B : 0;
456 1.37.4.1 yamt over = isover(qv, now);
457 1.37.4.1 yamt
458 1.37.4.1 yamt str = intprt(buf, width, qv->qv_usage, intprtflags, hflag);
459 1.37.4.1 yamt printf("%*s", width, str);
460 1.37.4.1 yamt
461 1.37.4.1 yamt printf("%c", over ? '*' : ' ');
462 1.37.4.1 yamt
463 1.37.4.1 yamt str = intprt(buf, width, qv->qv_softlimit, intprtflags, hflag);
464 1.37.4.1 yamt printf("%*s", width-1, str);
465 1.37.4.1 yamt
466 1.37.4.1 yamt str = intprt(buf, width, qv->qv_hardlimit, intprtflags, hflag);
467 1.37.4.1 yamt printf("%*s", width, str);
468 1.37.4.1 yamt
469 1.37.4.1 yamt if (over) {
470 1.37.4.1 yamt str = timeprt(buf, 9, now, qv->qv_expiretime);
471 1.37.4.1 yamt } else if (vflag && qv->qv_grace != QUOTA_NOTIME) {
472 1.37.4.1 yamt str = timeprt(buf, 9, 0, qv->qv_grace);
473 1.37.4.1 yamt } else {
474 1.37.4.1 yamt str = "";
475 1.37.4.1 yamt }
476 1.37.4.1 yamt printf("%8s", str);
477 1.37.4.1 yamt }
478 1.37.4.1 yamt
479 1.1 cgd /*
480 1.1 cgd * Collect the requested quota information.
481 1.1 cgd */
482 1.35 christos static struct quotause *
483 1.37.4.1 yamt getprivs(id_t id, int idtype)
484 1.1 cgd {
485 1.16 lukem struct quotause *qup, *quptail;
486 1.1 cgd struct quotause *quphead;
487 1.29 christos struct statvfs *fst;
488 1.37.4.1 yamt struct quotakey qk;
489 1.6 deraadt int nfst, i;
490 1.37.4.1 yamt unsigned j;
491 1.6 deraadt
492 1.16 lukem qup = quphead = quptail = NULL;
493 1.1 cgd
494 1.6 deraadt nfst = getmntinfo(&fst, MNT_WAIT);
495 1.16 lukem if (nfst == 0)
496 1.16 lukem errx(2, "no filesystems mounted!");
497 1.19 mrg for (i = 0; i < nfst; i++) {
498 1.6 deraadt if (qup == NULL) {
499 1.35 christos if ((qup = malloc(sizeof *qup)) == NULL)
500 1.37.4.1 yamt err(1, "Out of memory");
501 1.37.4.1 yamt }
502 1.37.4.1 yamt qup->qh = quota_open(fst[i].f_mntonname);
503 1.37.4.1 yamt if (qup->qh == NULL) {
504 1.37.4.1 yamt if (errno == EOPNOTSUPP || errno == ENXIO) {
505 1.37.4.1 yamt continue;
506 1.37.4.1 yamt }
507 1.37.4.1 yamt err(1, "%s: quota_open", fst[i].f_mntonname);
508 1.1 cgd }
509 1.37.4.1 yamt #if 0
510 1.31 christos if (strncmp(fst[i].f_fstypename, "nfs",
511 1.31 christos sizeof(fst[i].f_fstypename)) == 0) {
512 1.34 bouyer version = 0;
513 1.37.4.1 yamt qup->numqvs = QUOTA_NLIMITS;
514 1.37.4.1 yamt qup->qvs = malloc(qup->numqvs * sizeof(qup->qvs[0]));
515 1.37.4.1 yamt if (qup->qvs == NULL) {
516 1.37.4.1 yamt err(1, "Out of memory");
517 1.37.4.1 yamt }
518 1.37 bouyer if (getnfsquota(fst[i].f_mntfromname,
519 1.37.4.1 yamt qup->qvs, id, ufs_quota_class_names[idtype]) != 1)
520 1.6 deraadt continue;
521 1.34 bouyer } else if ((fst[i].f_flag & ST_QUOTA) != 0) {
522 1.37.4.1 yamt qup->numqvs = QUOTA_NLIMITS;
523 1.37.4.1 yamt qup->qvs = malloc(qup->numqvs * sizeof(qup->qvs[0]));
524 1.37.4.1 yamt if (qup->qvs == NULL) {
525 1.37.4.1 yamt err(1, "Out of memory");
526 1.37.4.1 yamt }
527 1.37.4.1 yamt if (getvfsquota(fst[i].f_mntonname, qup->qvs, &version,
528 1.37.4.1 yamt id, idtype, dflag, Dflag) != 1)
529 1.1 cgd continue;
530 1.6 deraadt } else
531 1.6 deraadt continue;
532 1.37.4.1 yamt #else
533 1.37.4.1 yamt qup->numqvs = quota_getnumidtypes(qup->qh);
534 1.37.4.1 yamt qup->qvs = malloc(qup->numqvs * sizeof(qup->qvs[0]));
535 1.37.4.1 yamt if (qup->qvs == NULL) {
536 1.37.4.1 yamt err(1, "Out of memory");
537 1.37.4.1 yamt }
538 1.37.4.1 yamt qk.qk_idtype = idtype;
539 1.37.4.1 yamt if (dflag) {
540 1.37.4.1 yamt qk.qk_id = QUOTA_DEFAULTID;
541 1.37.4.1 yamt } else {
542 1.37.4.1 yamt qk.qk_id = id;
543 1.37.4.1 yamt }
544 1.37.4.1 yamt for (j=0; j<qup->numqvs; j++) {
545 1.37.4.1 yamt qk.qk_objtype = j;
546 1.37.4.1 yamt if (quota_get(qup->qh, &qk, &qup->qvs[j]) < 0) {
547 1.37.4.1 yamt if (errno != ENOENT && errno != ENODEV) {
548 1.37.4.1 yamt warn("%s: quota_get (objtype %u)",
549 1.37.4.1 yamt fst[i].f_mntonname, j);
550 1.37.4.1 yamt }
551 1.37.4.1 yamt quotaval_clear(&qup->qvs[j]);
552 1.37.4.1 yamt }
553 1.37.4.1 yamt }
554 1.37.4.1 yamt #endif
555 1.37.4.1 yamt (void)strlcpy(qup->fsname, fst[i].f_mntonname,
556 1.37.4.1 yamt sizeof(qup->fsname));
557 1.1 cgd if (quphead == NULL)
558 1.1 cgd quphead = qup;
559 1.1 cgd else
560 1.1 cgd quptail->next = qup;
561 1.1 cgd quptail = qup;
562 1.6 deraadt quptail->next = 0;
563 1.6 deraadt qup = NULL;
564 1.1 cgd }
565 1.35 christos free(qup);
566 1.35 christos return quphead;
567 1.1 cgd }
568 1.37.4.1 yamt
569 1.37.4.1 yamt static int
570 1.37.4.1 yamt unlimited(struct quotaval *qvs, unsigned numqvs)
571 1.37.4.1 yamt {
572 1.37.4.1 yamt unsigned i;
573 1.37.4.1 yamt
574 1.37.4.1 yamt for (i=0; i<numqvs; i++) {
575 1.37.4.1 yamt if (qvs[i].qv_softlimit != QUOTA_NOLIMIT ||
576 1.37.4.1 yamt qvs[i].qv_hardlimit != QUOTA_NOLIMIT) {
577 1.37.4.1 yamt return 0;
578 1.37.4.1 yamt }
579 1.37.4.1 yamt }
580 1.37.4.1 yamt return 1;
581 1.37.4.1 yamt }
582 1.37.4.1 yamt
583 1.37.4.1 yamt static int
584 1.37.4.1 yamt anyusage(struct quotaval *qvs, unsigned numqvs)
585 1.37.4.1 yamt {
586 1.37.4.1 yamt unsigned i;
587 1.37.4.1 yamt
588 1.37.4.1 yamt for (i=0; i<numqvs; i++) {
589 1.37.4.1 yamt if (qvs[i].qv_usage > 0) {
590 1.37.4.1 yamt return 1;
591 1.37.4.1 yamt }
592 1.37.4.1 yamt }
593 1.37.4.1 yamt return 0;
594 1.37.4.1 yamt }
595 1.37.4.1 yamt
596 1.37.4.1 yamt static int
597 1.37.4.1 yamt anyover(struct quotaval *qvs, unsigned numqvs, time_t now)
598 1.37.4.1 yamt {
599 1.37.4.1 yamt unsigned i;
600 1.37.4.1 yamt
601 1.37.4.1 yamt for (i=0; i<numqvs; i++) {
602 1.37.4.1 yamt if (isover(&qvs[i], now)) {
603 1.37.4.1 yamt return 1;
604 1.37.4.1 yamt }
605 1.37.4.1 yamt }
606 1.37.4.1 yamt return 0;
607 1.37.4.1 yamt }
608 1.37.4.1 yamt
609 1.37.4.1 yamt static int
610 1.37.4.1 yamt isover(struct quotaval *qv, time_t now)
611 1.37.4.1 yamt {
612 1.37.4.1 yamt return (qv->qv_usage >= qv->qv_hardlimit ||
613 1.37.4.1 yamt qv->qv_usage >= qv->qv_softlimit);
614 1.37.4.1 yamt }
615 1.37.4.1 yamt
616 1.37.4.1 yamt static const char *
617 1.37.4.1 yamt getovermsg(struct quotaval *qv, const char *what, time_t now)
618 1.37.4.1 yamt {
619 1.37.4.1 yamt static char buf[64];
620 1.37.4.1 yamt
621 1.37.4.1 yamt if (qv->qv_usage >= qv->qv_hardlimit) {
622 1.37.4.1 yamt snprintf(buf, sizeof(buf), "%c%s limit reached on",
623 1.37.4.1 yamt toupper((unsigned char)what[0]), what+1);
624 1.37.4.1 yamt return buf;
625 1.37.4.1 yamt }
626 1.37.4.1 yamt
627 1.37.4.1 yamt if (qv->qv_usage < qv->qv_softlimit) {
628 1.37.4.1 yamt /* Ok */
629 1.37.4.1 yamt return NULL;
630 1.37.4.1 yamt }
631 1.37.4.1 yamt
632 1.37.4.1 yamt if (now > qv->qv_expiretime) {
633 1.37.4.1 yamt snprintf(buf, sizeof(buf), "Over %s quota on", what);
634 1.37.4.1 yamt return buf;
635 1.37.4.1 yamt }
636 1.37.4.1 yamt
637 1.37.4.1 yamt snprintf(buf, sizeof(buf), "In %s grace period on", what);
638 1.37.4.1 yamt return buf;
639 1.37.4.1 yamt }
640