2 * Copyright (c) 1980, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Robert Elz at The University of Melbourne.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1980, 1990, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static const char sccsid[] = "from: @(#)quota.c 8.1 (Berkeley) 6/6/93";
44 * Disk quota reporting program.
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/param.h>
50 #include <sys/types.h>
53 #include <sys/mount.h>
54 #include <sys/socket.h>
57 #include <rpc/pmap_prot.h>
58 #include <rpcsvc/rquota.h>
60 #include <ufs/ufs/quota.h>
76 static const char *qfextension[] = INITQFNAMES;
79 struct quotause *next;
82 char fsname[MAXPATHLEN + 1];
85 static char *timeprt(int64_t seconds);
86 static struct quotause *getprivs(long id, int quotatype);
87 static void usage(void);
88 static int showuid(u_long uid);
89 static int showgid(u_long gid);
90 static int showusrname(char *name);
91 static int showgrpname(char *name);
92 static int showquotas(int type, u_long id, const char *name);
93 static void showrawquotas(int type, u_long id, struct quotause *qup);
94 static void heading(int type, u_long id, const char *name, const char *tag);
95 static int getufsquota(struct fstab *fs, struct quotause *qup, long id,
97 static int getnfsquota(struct statfs *fst, struct quotause *qup, long id,
99 static int callaurpc(char *host, int prognum, int versnum, int procnum,
100 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out);
101 static int alldigits(char *s);
108 static char *filename = NULL;
111 main(int argc, char *argv[])
114 gid_t mygid, gidset[NGROUPS];
115 int i, ch, gflag = 0, uflag = 0, errflag = 0;
117 while ((ch = getopt(argc, argv, "f:ghlrquv")) != -1) {
149 if (!uflag && !gflag)
153 errflag += showuid(getuid());
156 ngroups = getgroups(NGROUPS, gidset);
159 errflag += showgid(mygid);
160 for (i = 0; i < ngroups; i++)
161 if (gidset[i] != mygid)
162 errflag += showgid(gidset[i]);
169 for (; argc > 0; argc--, argv++) {
170 if (alldigits(*argv))
171 errflag += showuid(atoi(*argv));
173 errflag += showusrname(*argv);
178 for (; argc > 0; argc--, argv++) {
179 if (alldigits(*argv))
180 errflag += showgid(atoi(*argv));
182 errflag += showgrpname(*argv);
192 fprintf(stderr, "%s\n%s\n%s\n",
193 "usage: quota [-ghlu] [-f path] [-v | -q | -r]",
194 " quota [-hlu] [-f path] [-v | -q | -r] user ...",
195 " quota -g [-hl] [-f path] [-v | -q | -r] group ...");
200 * Print out quotas for a specified user identifier.
205 struct passwd *pwd = getpwuid(uid);
209 name = "(no account)";
212 return(showquotas(USRQUOTA, uid, name));
216 * Print out quotas for a specifed user name.
219 showusrname(char *name)
221 struct passwd *pwd = getpwnam(name);
224 warnx("%s: unknown user", name);
227 return(showquotas(USRQUOTA, pwd->pw_uid, name));
231 * Print out quotas for a specified group identifier.
236 struct group *grp = getgrgid(gid);
243 return(showquotas(GRPQUOTA, gid, name));
247 * Print out quotas for a specifed group name.
250 showgrpname(char *name)
252 struct group *grp = getgrnam(name);
255 warnx("%s: unknown group", name);
258 return(showquotas(GRPQUOTA, grp->gr_gid, name));
262 prthumanval(int len, u_int64_t bytes)
267 * Limit the width to 5 bytes as that is what users expect.
269 humanize_number(buf, sizeof(buf) < 5 ? sizeof(buf) : 5, bytes, "",
270 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
272 (void)printf(" %*s", len, buf);
276 showquotas(int type, u_long id, const char *name)
278 struct quotause *qup;
279 struct quotause *quplist;
280 const char *msgi, *msgb;
282 char *bgrace = NULL, *igrace = NULL;
283 int lines = 0, overquota = 0;
288 quplist = getprivs(id, type);
289 for (qup = quplist; qup; qup = qup->next) {
291 if (qup->dqblk.dqb_ihardlimit &&
292 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_ihardlimit) {
294 msgi = "File limit reached on";
296 else if (qup->dqblk.dqb_isoftlimit &&
297 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_isoftlimit) {
299 if (qup->dqblk.dqb_itime > now)
300 msgi = "In file grace period on";
302 msgi = "Over file quota on";
305 if (qup->dqblk.dqb_bhardlimit &&
306 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bhardlimit) {
308 msgb = "Block limit reached on";
310 else if (qup->dqblk.dqb_bsoftlimit &&
311 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit) {
313 if (qup->dqblk.dqb_btime > now)
314 msgb = "In block grace period on";
316 msgb = "Over block quota on";
319 showrawquotas(type, id, qup);
323 qup->dqblk.dqb_isoftlimit == 0 &&
324 qup->dqblk.dqb_ihardlimit == 0 &&
325 qup->dqblk.dqb_bsoftlimit == 0 &&
326 qup->dqblk.dqb_bhardlimit == 0)
329 if ((msgi != NULL || msgb != NULL) &&
331 heading(type, id, name, "");
333 printf("\t%s %s\n", msgi, qup->fsname);
335 printf("\t%s %s\n", msgb, qup->fsname);
339 qup->dqblk.dqb_curblocks == 0 &&
340 qup->dqblk.dqb_curinodes == 0)
343 heading(type, id, name, "");
345 if (strlen(qup->fsname) > 15) {
346 printf("%s\n", qup->fsname);
349 printf("%-15s", nam);
351 prthumanval(7, dbtob(qup->dqblk.dqb_curblocks));
352 printf("%c", (msgb == NULL) ? ' ' : '*');
353 prthumanval(7, dbtob(qup->dqblk.dqb_bsoftlimit));
354 prthumanval(7, dbtob(qup->dqblk.dqb_bhardlimit));
356 printf(" %7ju%c %7ju %7ju",
357 (uintmax_t)dbtob(qup->dqblk.dqb_curblocks)
359 (msgb == NULL) ? ' ' : '*',
360 (uintmax_t)dbtob(qup->dqblk.dqb_bsoftlimit)
362 (uintmax_t)dbtob(qup->dqblk.dqb_bhardlimit)
366 bgrace = timeprt(qup->dqblk.dqb_btime);
368 igrace = timeprt(qup->dqblk.dqb_itime);
369 printf("%8s %6ju%c %6ju %6ju%8s\n"
370 , (msgb == NULL) ? "" : bgrace
371 , (uintmax_t)qup->dqblk.dqb_curinodes
372 , (msgi == NULL) ? ' ' : '*'
373 , (uintmax_t)qup->dqblk.dqb_isoftlimit
374 , (uintmax_t)qup->dqblk.dqb_ihardlimit
375 , (msgi == NULL) ? "" : igrace
382 if (!qflag && !rflag && lines == 0)
383 heading(type, id, name, "none");
388 showrawquotas(int type, u_long id, struct quotause *qup)
392 printf("Raw %s quota information for id %lu on %s\n",
393 type == USRQUOTA ? "user" : "group", id, qup->fsname);
394 printf("block hard limit: %ju\n",
395 (uintmax_t)qup->dqblk.dqb_bhardlimit);
396 printf("block soft limit: %ju\n",
397 (uintmax_t)qup->dqblk.dqb_bsoftlimit);
398 printf("current block count: %ju\n",
399 (uintmax_t)qup->dqblk.dqb_curblocks);
400 printf("i-node hard limit: %ju\n",
401 (uintmax_t)qup->dqblk.dqb_ihardlimit);
402 printf("i-node soft limit: %ju\n",
403 (uintmax_t)qup->dqblk.dqb_isoftlimit);
404 printf("current i-node count: %ju\n",
405 (uintmax_t)qup->dqblk.dqb_curinodes);
406 printf("block grace time: %jd",
407 (intmax_t)qup->dqblk.dqb_btime);
408 if (qup->dqblk.dqb_btime != 0) {
409 t = qup->dqblk.dqb_btime;
410 printf(" %s", ctime(&t));
414 printf("i-node grace time: %jd", (intmax_t)qup->dqblk.dqb_itime);
415 if (qup->dqblk.dqb_itime != 0) {
416 t = qup->dqblk.dqb_itime;
417 printf(" %s", ctime(&t));
425 heading(int type, u_long id, const char *name, const char *tag)
428 printf("Disk quotas for %s %s (%cid %lu): %s\n", qfextension[type],
429 name, *qfextension[type], id, tag);
430 if (!qflag && tag[0] == '\0') {
431 printf("%-15s %7s %8s %7s %7s %6s %7s %6s%8s\n"
446 * Calculate the grace period and return a printable string for it.
449 timeprt(int64_t seconds)
451 time_t hours, minutes;
458 if ((buf = strdup("none")) == NULL)
459 errx(1, "strdup() failed in timeprt()");
463 minutes = (seconds + 30) / 60;
464 hours = (minutes + 30) / 60;
466 if (asprintf(&buf, "%lddays", ((long)hours + 12) / 24) < 0)
467 errx(1, "asprintf() failed in timeprt(1)");
471 if (asprintf(&buf, "%2ld:%ld", (long)minutes / 60,
472 (long)minutes % 60) < 0)
473 errx(1, "asprintf() failed in timeprt(2)");
476 if (asprintf(&buf, "%2ld", (long)minutes) < 0)
477 errx(1, "asprintf() failed in timeprt(3)");
482 * Collect the requested quota information.
484 static struct quotause *
485 getprivs(long id, int quotatype)
487 struct quotause *qup, *quptail = NULL;
489 struct quotause *quphead;
494 qup = quphead = (struct quotause *)0;
496 if (filename != NULL && statfs(filename, &sfb) != 0)
497 err(1, "cannot statfs %s", filename);
498 nfst = getmntinfo(&fst, MNT_NOWAIT);
500 errx(2, "no filesystems mounted!");
502 for (i = 0; i < nfst; i++) {
504 if ((qup = (struct quotause *)malloc(sizeof *qup))
506 errx(2, "out of memory");
509 * See if the user requested a specific file system
510 * or specified a file inside a mounted file system.
512 if (filename != NULL &&
513 strcmp(sfb.f_mntonname, fst[i].f_mntonname) != 0)
515 if (strcmp(fst[i].f_fstypename, "nfs") == 0) {
518 if (getnfsquota(&fst[i], qup, id, quotatype) == 0)
520 } else if (strcmp(fst[i].f_fstypename, "ufs") == 0) {
523 * UFS filesystems must be in /etc/fstab, and must
524 * indicate that they have quotas on (?!) This is quite
525 * unlike SunOS where quotas can be enabled/disabled
526 * on a filesystem independent of /etc/fstab, and it
527 * will still print quotas for them.
529 if ((fs = getfsspec(fst[i].f_mntfromname)) == NULL)
531 if (getufsquota(fs, qup, id, quotatype) == 0)
535 strcpy(qup->fsname, fst[i].f_mntonname);
551 * Check to see if a particular quota is available.
554 getufsquota(struct fstab *fs, struct quotause *qup, long id, int quotatype)
556 struct quotafile *qf;
558 if ((qf = quota_open(fs, quotatype, O_RDONLY)) == NULL)
560 if (quota_read(qf, &qup->dqblk, id) != 0)
567 getnfsquota(struct statfs *fst, struct quotause *qup, long id, int quotatype)
569 struct getquota_args gq_args;
570 struct getquota_rslt gq_rslt;
571 struct dqblk *dqp = &qup->dqblk;
573 char *cp, host[NI_MAXHOST];
575 if (fst->f_flags & MNT_LOCAL)
579 * rpc.rquotad does not support group quotas
581 if (quotatype != USRQUOTA)
585 * must be some form of "hostname:/path"
587 cp = fst->f_mntfromname;
589 cp = strrchr(cp, ':');
590 } while (cp != NULL && *(cp + 1) != '/');
592 warnx("cannot find hostname for %s", fst->f_mntfromname);
595 memset(host, 0, sizeof(host));
596 memcpy(host, fst->f_mntfromname, cp - fst->f_mntfromname);
597 host[sizeof(host) - 1] = '\0';
599 /* Avoid attempting the RPC for special amd(8) filesystems. */
600 if (strncmp(fst->f_mntfromname, "pid", 3) == 0 &&
601 strchr(fst->f_mntfromname, '@') != NULL)
604 gq_args.gqa_pathp = cp + 1;
605 gq_args.gqa_uid = id;
606 if (callaurpc(host, RQUOTAPROG, RQUOTAVERS,
607 RQUOTAPROC_GETQUOTA, (xdrproc_t)xdr_getquota_args, (char *)&gq_args,
608 (xdrproc_t)xdr_getquota_rslt, (char *)&gq_rslt) != 0)
611 switch (gq_rslt.status) {
615 warnx("quota permission error, host: %s",
619 gettimeofday(&tv, NULL);
621 dqp->dqb_bhardlimit =
622 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit *
623 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
624 dqp->dqb_bsoftlimit =
625 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit *
626 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
628 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks *
629 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
631 dqp->dqb_ihardlimit =
632 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit;
633 dqp->dqb_isoftlimit =
634 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit;
636 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles;
639 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft;
641 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft;
644 warnx("bad rpc result, host: %s", fst->f_mntfromname);
652 callaurpc(char *host, int prognum, int versnum, int procnum,
653 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
655 enum clnt_stat clnt_stat;
656 struct timeval timeout, tottimeout;
658 CLIENT *client = NULL;
660 client = clnt_create(host, prognum, versnum, "udp");
662 return ((int)rpc_createerr.cf_stat);
665 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout);
667 client->cl_auth = authunix_create_default();
668 tottimeout.tv_sec = 25;
669 tottimeout.tv_usec = 0;
670 clnt_stat = clnt_call(client, procnum, inproc, in,
671 outproc, out, tottimeout);
673 return ((int) clnt_stat);
685 } while ((c = *s++));