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 const char *qfname = QUOTAFILENAME;
77 const char *qfextension[] = INITQFNAMES;
80 struct quotause *next;
83 char fsname[MAXPATHLEN + 1];
86 static char *timeprt(int64_t seconds);
87 static struct quotause *getprivs(long id, int quotatype);
88 static void usage(void);
89 static int showuid(u_long uid);
90 static int showgid(u_long gid);
91 static int showusrname(char *name);
92 static int showgrpname(char *name);
93 static int showquotas(int type, u_long id, const char *name);
94 static void showrawquotas(int type, u_long id, struct quotause *qup);
95 static void heading(int type, u_long id, const char *name, const char *tag);
96 static int getufsquota(struct fstab *fs, struct quotause *qup, long id,
98 static int getnfsquota(struct statfs *fst, struct quotause *qup, long id,
100 static int callaurpc(char *host, int prognum, int versnum, int procnum,
101 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out);
102 static int alldigits(char *s);
109 char *filename = NULL;
112 main(int argc, char *argv[])
115 gid_t mygid, gidset[NGROUPS];
116 int i, ch, gflag = 0, uflag = 0, errflag = 0;
118 while ((ch = getopt(argc, argv, "f:ghlrquv")) != -1) {
150 if (!uflag && !gflag)
154 errflag += showuid(getuid());
157 ngroups = getgroups(NGROUPS, gidset);
160 errflag += showgid(mygid);
161 for (i = 0; i < ngroups; i++)
162 if (gidset[i] != mygid)
163 errflag += showgid(gidset[i]);
170 for (; argc > 0; argc--, argv++) {
171 if (alldigits(*argv))
172 errflag += showuid(atoi(*argv));
174 errflag += showusrname(*argv);
179 for (; argc > 0; argc--, argv++) {
180 if (alldigits(*argv))
181 errflag += showgid(atoi(*argv));
183 errflag += showgrpname(*argv);
193 fprintf(stderr, "%s\n%s\n%s\n",
194 "usage: quota [-ghlu] [-f path] [-v | -q | -r]",
195 " quota [-hlu] [-f path] [-v | -q | -r] user ...",
196 " quota -g [-hl] [-f path] [-v | -q | -r] group ...");
201 * Print out quotas for a specified user identifier.
206 struct passwd *pwd = getpwuid(uid);
210 name = "(no account)";
213 return(showquotas(USRQUOTA, uid, name));
217 * Print out quotas for a specifed user name.
220 showusrname(char *name)
222 struct passwd *pwd = getpwnam(name);
225 warnx("%s: unknown user", name);
228 return(showquotas(USRQUOTA, pwd->pw_uid, name));
232 * Print out quotas for a specified group identifier.
237 struct group *grp = getgrgid(gid);
244 return(showquotas(GRPQUOTA, gid, name));
248 * Print out quotas for a specifed group name.
251 showgrpname(char *name)
253 struct group *grp = getgrnam(name);
256 warnx("%s: unknown group", name);
259 return(showquotas(GRPQUOTA, grp->gr_gid, name));
263 prthumanval(int len, u_int64_t bytes)
267 humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE,
268 HN_B | HN_NOSPACE | HN_DECIMAL);
270 (void)printf(" %*s", len, buf);
274 showquotas(int type, u_long id, const char *name)
276 struct quotause *qup;
277 struct quotause *quplist;
278 const char *msgi, *msgb;
280 char *bgrace = NULL, *igrace = NULL;
281 int lines = 0, overquota = 0;
286 quplist = getprivs(id, type);
287 for (qup = quplist; qup; qup = qup->next) {
289 if (qup->dqblk.dqb_ihardlimit &&
290 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_ihardlimit) {
292 msgi = "File limit reached on";
294 else if (qup->dqblk.dqb_isoftlimit &&
295 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_isoftlimit) {
297 if (qup->dqblk.dqb_itime > now)
298 msgi = "In file grace period on";
300 msgi = "Over file quota on";
303 if (qup->dqblk.dqb_bhardlimit &&
304 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bhardlimit) {
306 msgb = "Block limit reached on";
308 else if (qup->dqblk.dqb_bsoftlimit &&
309 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit) {
311 if (qup->dqblk.dqb_btime > now)
312 msgb = "In block grace period on";
314 msgb = "Over block quota on";
317 showrawquotas(type, id, qup);
321 qup->dqblk.dqb_isoftlimit == 0 &&
322 qup->dqblk.dqb_ihardlimit == 0 &&
323 qup->dqblk.dqb_bsoftlimit == 0 &&
324 qup->dqblk.dqb_bhardlimit == 0)
327 if ((msgi != NULL || msgb != NULL) &&
329 heading(type, id, name, "");
331 printf("\t%s %s\n", msgi, qup->fsname);
333 printf("\t%s %s\n", msgb, qup->fsname);
337 qup->dqblk.dqb_curblocks == 0 &&
338 qup->dqblk.dqb_curinodes == 0)
341 heading(type, id, name, "");
343 if (strlen(qup->fsname) > 15) {
344 printf("%s\n", qup->fsname);
347 printf("%-15s", nam);
349 prthumanval(7, dbtob(qup->dqblk.dqb_curblocks));
350 printf("%c", (msgb == NULL) ? ' ' : '*');
351 prthumanval(7, dbtob(qup->dqblk.dqb_bsoftlimit));
352 prthumanval(7, dbtob(qup->dqblk.dqb_bhardlimit));
354 printf(" %7ju%c %7ju %7ju",
355 dbtob(1024) * (uintmax_t)qup->dqblk.dqb_curblocks,
356 (msgb == NULL) ? ' ' : '*',
357 dbtob(1024) * (uintmax_t)qup->dqblk.dqb_bsoftlimit,
358 dbtob(1024) * (uintmax_t)qup->dqblk.dqb_bhardlimit);
361 bgrace = timeprt(qup->dqblk.dqb_btime);
363 igrace = timeprt(qup->dqblk.dqb_itime);
364 printf("%8s %6ju%c %6ju %6ju%8s\n"
365 , (msgb == NULL) ? "" : bgrace
366 , (uintmax_t)qup->dqblk.dqb_curinodes
367 , (msgi == NULL) ? ' ' : '*'
368 , (uintmax_t)qup->dqblk.dqb_isoftlimit
369 , (uintmax_t)qup->dqblk.dqb_ihardlimit
370 , (msgi == NULL) ? "" : igrace
377 if (!qflag && !rflag && lines == 0)
378 heading(type, id, name, "none");
383 showrawquotas(int type, u_long id, struct quotause *qup)
387 printf("Raw %s quota information for id %lu on %s\n",
388 type == USRQUOTA ? "user" : "group", id, qup->fsname);
389 printf("block hard limit: %ju\n",
390 (uintmax_t)qup->dqblk.dqb_bhardlimit);
391 printf("block soft limit: %ju\n",
392 (uintmax_t)qup->dqblk.dqb_bsoftlimit);
393 printf("current block count: %ju\n",
394 (uintmax_t)qup->dqblk.dqb_curblocks);
395 printf("i-node hard limit: %ju\n",
396 (uintmax_t)qup->dqblk.dqb_ihardlimit);
397 printf("i-node soft limit: %ju\n",
398 (uintmax_t)qup->dqblk.dqb_isoftlimit);
399 printf("current i-node count: %ju\n",
400 (uintmax_t)qup->dqblk.dqb_curinodes);
401 printf("block grace time: %jd",
402 (intmax_t)qup->dqblk.dqb_btime);
403 if (qup->dqblk.dqb_btime != 0) {
404 t = qup->dqblk.dqb_btime;
405 printf(" %s", ctime(&t));
409 printf("i-node grace time: %jd", (intmax_t)qup->dqblk.dqb_itime);
410 if (qup->dqblk.dqb_itime != 0) {
411 t = qup->dqblk.dqb_itime;
412 printf(" %s", ctime(&t));
420 heading(int type, u_long id, const char *name, const char *tag)
423 printf("Disk quotas for %s %s (%cid %lu): %s\n", qfextension[type],
424 name, *qfextension[type], id, tag);
425 if (!qflag && tag[0] == '\0') {
426 printf("%-15s %7s %8s %7s %7s %6s %7s %6s%8s\n"
441 * Calculate the grace period and return a printable string for it.
444 timeprt(int64_t seconds)
446 time_t hours, minutes;
453 if ((buf = strdup("none")) == NULL)
454 errx(1, "strdup() failed in timeprt()");
458 minutes = (seconds + 30) / 60;
459 hours = (minutes + 30) / 60;
461 if (asprintf(&buf, "%lddays", ((long)hours + 12) / 24) < 0)
462 errx(1, "asprintf() failed in timeprt(1)");
466 if (asprintf(&buf, "%2ld:%ld", (long)minutes / 60,
467 (long)minutes % 60) < 0)
468 errx(1, "asprintf() failed in timeprt(2)");
471 if (asprintf(&buf, "%2ld", (long)minutes) < 0)
472 errx(1, "asprintf() failed in timeprt(3)");
477 * Collect the requested quota information.
479 static struct quotause *
480 getprivs(long id, int quotatype)
482 struct quotause *qup, *quptail = NULL;
484 struct quotause *quphead;
489 qup = quphead = (struct quotause *)0;
491 if (filename != NULL && statfs(filename, &sfb) != 0)
492 err(1, "cannot statfs %s", filename);
493 nfst = getmntinfo(&fst, MNT_NOWAIT);
495 errx(2, "no filesystems mounted!");
497 for (i = 0; i < nfst; i++) {
499 if ((qup = (struct quotause *)malloc(sizeof *qup))
501 errx(2, "out of memory");
504 * See if the user requested a specific file system
505 * or specified a file inside a mounted file system.
507 if (filename != NULL &&
508 strcmp(sfb.f_mntonname, fst[i].f_mntonname) != 0)
510 if (strcmp(fst[i].f_fstypename, "nfs") == 0) {
513 if (getnfsquota(&fst[i], qup, id, quotatype) == 0)
515 } else if (strcmp(fst[i].f_fstypename, "ufs") == 0) {
518 * UFS filesystems must be in /etc/fstab, and must
519 * indicate that they have quotas on (?!) This is quite
520 * unlike SunOS where quotas can be enabled/disabled
521 * on a filesystem independent of /etc/fstab, and it
522 * will still print quotas for them.
524 if ((fs = getfsspec(fst[i].f_mntfromname)) == NULL)
526 if (getufsquota(fs, qup, id, quotatype) == 0)
530 strcpy(qup->fsname, fst[i].f_mntonname);
546 * Check to see if a particular quota is available.
549 getufsquota(struct fstab *fs, struct quotause *qup, long id, int quotatype)
551 struct quotafile *qf;
553 if ((qf = quota_open(fs, quotatype, O_RDONLY)) == NULL)
555 if (quota_read(qf, &qup->dqblk, id) != 0)
562 getnfsquota(struct statfs *fst, struct quotause *qup, long id, int quotatype)
564 struct getquota_args gq_args;
565 struct getquota_rslt gq_rslt;
566 struct dqblk *dqp = &qup->dqblk;
570 if (fst->f_flags & MNT_LOCAL)
574 * rpc.rquotad does not support group quotas
576 if (quotatype != USRQUOTA)
580 * must be some form of "hostname:/path"
582 cp = strchr(fst->f_mntfromname, ':');
584 warnx("cannot find hostname for %s", fst->f_mntfromname);
589 if (*(cp+1) != '/') {
594 /* Avoid attempting the RPC for special amd(8) filesystems. */
595 if (strncmp(fst->f_mntfromname, "pid", 3) == 0 &&
596 strchr(fst->f_mntfromname, '@') != NULL) {
601 gq_args.gqa_pathp = cp + 1;
602 gq_args.gqa_uid = id;
603 if (callaurpc(fst->f_mntfromname, RQUOTAPROG, RQUOTAVERS,
604 RQUOTAPROC_GETQUOTA, (xdrproc_t)xdr_getquota_args, (char *)&gq_args,
605 (xdrproc_t)xdr_getquota_rslt, (char *)&gq_rslt) != 0) {
610 switch (gq_rslt.status) {
614 warnx("quota permission error, host: %s",
618 gettimeofday(&tv, NULL);
620 dqp->dqb_bhardlimit =
621 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit *
622 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
623 dqp->dqb_bsoftlimit =
624 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit *
625 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
627 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks *
628 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
630 dqp->dqb_ihardlimit =
631 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit;
632 dqp->dqb_isoftlimit =
633 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit;
635 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles;
638 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft;
640 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 struct sockaddr_in server_addr;
656 enum clnt_stat clnt_stat;
658 struct timeval timeout, tottimeout;
660 CLIENT *client = NULL;
661 int sock = RPC_ANYSOCK;
663 if ((hp = gethostbyname(host)) == NULL)
664 return ((int) RPC_UNKNOWNHOST);
667 bcopy(hp->h_addr, &server_addr.sin_addr,
668 MIN(hp->h_length,(int)sizeof(server_addr.sin_addr)));
669 server_addr.sin_family = AF_INET;
670 server_addr.sin_port = 0;
672 if ((client = clntudp_create(&server_addr, prognum,
673 versnum, timeout, &sock)) == NULL)
674 return ((int) rpc_createerr.cf_stat);
676 client->cl_auth = authunix_create_default();
677 tottimeout.tv_sec = 25;
678 tottimeout.tv_usec = 0;
679 clnt_stat = clnt_call(client, procnum, inproc, in,
680 outproc, out, tottimeout);
682 return ((int) clnt_stat);
694 } while ((c = *s++));