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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static const char copyright[] =
39 "@(#) Copyright (c) 1980, 1990, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
44 static const char sccsid[] = "from: @(#)quota.c 8.1 (Berkeley) 6/6/93";
48 * Disk quota reporting program.
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 #include <sys/param.h>
54 #include <sys/types.h>
57 #include <sys/mount.h>
58 #include <sys/socket.h>
61 #include <rpc/pmap_prot.h>
62 #include <rpcsvc/rquota.h>
64 #include <ufs/ufs/quota.h>
80 const char *qfname = QUOTAFILENAME;
81 const char *qfextension[] = INITQFNAMES;
84 struct quotause *next;
87 char fsname[MAXPATHLEN + 1];
90 static char *timeprt(time_t seconds);
91 static struct quotause *getprivs(long id, int quotatype);
92 static void usage(void);
93 static int showuid(u_long uid);
94 static int showgid(u_long gid);
95 static int showusrname(char *name);
96 static int showgrpname(char *name);
97 static int showquotas(int type, u_long id, const char *name);
98 static void showrawquotas(int type, u_long id, struct quotause *qup);
99 static void heading(int type, u_long id, const char *name, const char *tag);
100 static int ufshasquota(struct fstab *fs, int type, char **qfnamep);
101 static int getufsquota(struct fstab *fs, struct quotause *qup, long id,
103 static int getnfsquota(struct statfs *fst, struct quotause *qup, long id,
105 static int callaurpc(char *host, int prognum, int versnum, int procnum,
106 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out);
107 static int alldigits(char *s);
114 char *filename = NULL;
117 main(int argc, char *argv[])
120 gid_t mygid, gidset[NGROUPS];
121 int i, ch, gflag = 0, uflag = 0, errflag = 0;
123 while ((ch = getopt(argc, argv, "f:ghlrquv")) != -1) {
155 if (!uflag && !gflag)
159 errflag += showuid(getuid());
162 ngroups = getgroups(NGROUPS, gidset);
165 errflag += showgid(mygid);
166 for (i = 0; i < ngroups; i++)
167 if (gidset[i] != mygid)
168 errflag += showgid(gidset[i]);
175 for (; argc > 0; argc--, argv++) {
176 if (alldigits(*argv))
177 errflag += showuid(atoi(*argv));
179 errflag += showusrname(*argv);
184 for (; argc > 0; argc--, argv++) {
185 if (alldigits(*argv))
186 errflag += showgid(atoi(*argv));
188 errflag += showgrpname(*argv);
198 fprintf(stderr, "%s\n%s\n%s\n",
199 "usage: quota [-ghlu] [-f path] [-v | -q | -r]",
200 " quota [-hlu] [-f path] [-v | -q | -r] user ...",
201 " quota -g [-hl] [-f path] [-v | -q | -r] group ...");
206 * Print out quotas for a specified user identifier.
211 struct passwd *pwd = getpwuid(uid);
215 name = "(no account)";
218 return(showquotas(USRQUOTA, uid, name));
222 * Print out quotas for a specifed user name.
225 showusrname(char *name)
227 struct passwd *pwd = getpwnam(name);
230 warnx("%s: unknown user", name);
233 return(showquotas(USRQUOTA, pwd->pw_uid, name));
237 * Print out quotas for a specified group identifier.
242 struct group *grp = getgrgid(gid);
249 return(showquotas(GRPQUOTA, gid, name));
253 * Print out quotas for a specifed group name.
256 showgrpname(char *name)
258 struct group *grp = getgrnam(name);
261 warnx("%s: unknown group", name);
264 return(showquotas(GRPQUOTA, grp->gr_gid, name));
268 prthumanval(int len, int64_t bytes)
272 humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE,
273 HN_B | HN_NOSPACE | HN_DECIMAL);
275 (void)printf(" %*s", len, buf);
279 showquotas(int type, u_long id, const char *name)
281 struct quotause *qup;
282 struct quotause *quplist;
283 const char *msgi, *msgb;
285 char *bgrace = NULL, *igrace = NULL;
286 int lines = 0, overquota = 0;
291 quplist = getprivs(id, type);
292 for (qup = quplist; qup; qup = qup->next) {
294 if (qup->dqblk.dqb_ihardlimit &&
295 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_ihardlimit) {
297 msgi = "File limit reached on";
299 else if (qup->dqblk.dqb_isoftlimit &&
300 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_isoftlimit) {
302 if (qup->dqblk.dqb_itime > now)
303 msgi = "In file grace period on";
305 msgi = "Over file quota on";
308 if (qup->dqblk.dqb_bhardlimit &&
309 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bhardlimit) {
311 msgb = "Block limit reached on";
313 else if (qup->dqblk.dqb_bsoftlimit &&
314 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit) {
316 if (qup->dqblk.dqb_btime > now)
317 msgb = "In block grace period on";
319 msgb = "Over block quota on";
322 showrawquotas(type, id, qup);
326 qup->dqblk.dqb_isoftlimit == 0 &&
327 qup->dqblk.dqb_ihardlimit == 0 &&
328 qup->dqblk.dqb_bsoftlimit == 0 &&
329 qup->dqblk.dqb_bhardlimit == 0)
332 if ((msgi != (char *)0 || msgb != (char *)0) &&
334 heading(type, id, name, "");
335 if (msgi != (char *)0)
336 printf("\t%s %s\n", msgi, qup->fsname);
337 if (msgb != (char *)0)
338 printf("\t%s %s\n", msgb, qup->fsname);
342 qup->dqblk.dqb_curblocks ||
343 qup->dqblk.dqb_curinodes) {
345 heading(type, id, name, "");
347 if (strlen(qup->fsname) > 15) {
348 printf("%s\n", qup->fsname);
354 prthumanval(4, dbtob(qup->dqblk.dqb_curblocks));
355 printf("%c ", (msgb == (char *)0) ? ' ' : '*');
356 prthumanval(4, dbtob(qup->dqblk.dqb_bsoftlimit));
358 prthumanval(4, dbtob(qup->dqblk.dqb_bhardlimit));
360 printf(" %7ju%c %6ju %7ju",
361 (uintmax_t)(dbtob(qup->dqblk.dqb_curblocks)
363 (msgb == NULL) ? ' ' : '*',
364 (uintmax_t)(dbtob(qup->dqblk.dqb_bsoftlimit)
366 (uintmax_t)(dbtob(qup->dqblk.dqb_bhardlimit)
370 bgrace = timeprt(qup->dqblk.dqb_btime);
372 igrace = timeprt(qup->dqblk.dqb_itime);
373 printf(" %7s %7ju%c %6ju %7ju %7s\n",
374 (msgb == NULL) ? "" : bgrace,
375 (uintmax_t)qup->dqblk.dqb_curinodes,
376 (msgi == NULL) ? ' ' : '*',
377 (uintmax_t)qup->dqblk.dqb_isoftlimit,
378 (uintmax_t)qup->dqblk.dqb_ihardlimit,
379 (msgi == NULL) ? "" : igrace
388 if (!qflag && !rflag && lines == 0)
389 heading(type, id, name, "none");
394 showrawquotas(int type, u_long id, struct quotause *qup)
397 printf("Raw %s quota information for id %lu on %s\n",
398 type == USRQUOTA ? "user" : "group", id, qup->fsname);
399 printf("block hard limit: %ju\n", (uintmax_t)qup->dqblk.dqb_bhardlimit);
400 printf("block soft limit: %ju\n", (uintmax_t)qup->dqblk.dqb_bsoftlimit);
401 printf("current block count: %ju\n", (uintmax_t)qup->dqblk.dqb_curblocks);
402 printf("i-node hard limit: %ju\n", (uintmax_t)qup->dqblk.dqb_ihardlimit);
403 printf("i-node soft limit: %ju\n", (uintmax_t)qup->dqblk.dqb_isoftlimit);
404 printf("current i-node count: %ju\n", (uintmax_t)qup->dqblk.dqb_curinodes);
405 printf("block grace time: %jd", (intmax_t)qup->dqblk.dqb_btime);
406 if (qup->dqblk.dqb_btime != 0) {
407 tt = qup->dqblk.dqb_btime;
408 printf(" %s", ctime(&tt));
411 printf("i-node grace time: %jd", (intmax_t)qup->dqblk.dqb_itime);
412 if (qup->dqblk.dqb_itime != 0) {
413 tt = qup->dqblk.dqb_itime;
414 printf(" %s", ctime(&tt));
421 heading(int type, u_long id, const char *name, const char *tag)
424 printf("Disk quotas for %s %s (%cid %lu): %s\n", qfextension[type],
425 name, *qfextension[type], id, tag);
426 if (!qflag && tag[0] == '\0') {
427 printf("%15s %7s %6s %7s %7s %7s %6s %7s %7s\n"
442 * Calculate the grace period and return a printable string for it.
445 timeprt(time_t seconds)
447 time_t hours, minutes;
454 return strdup("none");
457 minutes = (seconds + 30) / 60;
458 hours = (minutes + 30) / 60;
460 if (asprintf(&buf, "%lddays", ((long)hours + 12) / 24) < 0)
461 errx(1, "asprintf failed in timeprt(1)");
465 if (asprintf(&buf, "%2ld:%ld", (long)minutes / 60,
466 (long)minutes % 60) < 0)
467 errx(1, "asprintf failed in timeprt(2)");
470 if (asprintf(&buf, "%2ld", (long)minutes) < 0)
471 errx(1, "asprintf failed in timeprt(3)");
476 * Collect the requested quota information.
478 static struct quotause *
479 getprivs(long id, int quotatype)
481 struct quotause *qup, *quptail = NULL;
483 struct quotause *quphead;
488 qup = quphead = (struct quotause *)0;
490 if (filename != NULL && statfs(filename, &sfb) != 0)
491 err(1, "cannot statfs %s", filename);
492 nfst = getmntinfo(&fst, MNT_NOWAIT);
494 errx(2, "no filesystems mounted!");
496 for (i=0; i<nfst; i++) {
498 if ((qup = (struct quotause *)malloc(sizeof *qup))
500 errx(2, "out of memory");
503 * See if the user requested a specific file system
504 * or specified a file inside a mounted file system.
506 if (filename != NULL &&
507 strcmp(sfb.f_mntonname, fst[i].f_mntonname) != 0)
509 if (strcmp(fst[i].f_fstypename, "nfs") == 0) {
512 if (getnfsquota(&fst[i], qup, id, quotatype) == 0)
514 } else if (strcmp(fst[i].f_fstypename, "ufs") == 0) {
517 * UFS filesystems must be in /etc/fstab, and must
518 * indicate that they have quotas on (?!) This is quite
519 * unlike SunOS where quotas can be enabled/disabled
520 * on a filesystem independent of /etc/fstab, and it
521 * will still print quotas for them.
523 if ((fs = getfsspec(fst[i].f_mntfromname)) == NULL)
525 if (getufsquota(fs, qup, id, quotatype) == 0)
529 strcpy(qup->fsname, fst[i].f_mntonname);
545 * Check to see if a particular quota is to be enabled.
548 ufshasquota(struct fstab *fs, int type, char **qfnamep)
553 static char initname, usrname[100], grpname[100];
554 static char buf[BUFSIZ];
557 (void)snprintf(usrname, sizeof(usrname), "%s%s",
558 qfextension[USRQUOTA], qfname);
559 (void)snprintf(grpname, sizeof(grpname), "%s%s",
560 qfextension[GRPQUOTA], qfname);
563 strcpy(buf, fs->fs_mntops);
564 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
565 if ((cp = index(opt, '=')))
567 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
569 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
577 (void)snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file,
578 qfname, qfextension[type]);
581 if (statfs(fs->fs_file, &sfb) != 0) {
582 warn("cannot statfs mount point %s", fs->fs_file);
585 if (strcmp(fs->fs_file, sfb.f_mntonname)) {
586 warnx("%s not mounted for %s quotas", fs->fs_file,
587 type == USRQUOTA ? "user" : "group");
594 getufsquota(struct fstab *fs, struct quotause *qup, long id, int quotatype)
599 qcmd = QCMD(Q_GETQUOTA, quotatype);
600 if (!ufshasquota(fs, quotatype, &qfpathname))
603 if (quotactl(fs->fs_file, qcmd, id, (char *)&qup->dqblk) != 0) {
604 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
605 warn("%s", qfpathname);
608 (void) lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET);
609 switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
612 * Convert implicit 0 quota (EOF)
613 * into an explicit one (zero'ed dqblk)
615 bzero((caddr_t)&qup->dqblk, sizeof(struct dqblk));
617 case sizeof(struct dqblk): /* OK */
620 warn("read error: %s", qfpathname);
630 getnfsquota(struct statfs *fst, struct quotause *qup, long id, int quotatype)
632 struct getquota_args gq_args;
633 struct getquota_rslt gq_rslt;
634 struct dqblk *dqp = &qup->dqblk;
638 if (fst->f_flags & MNT_LOCAL)
642 * rpc.rquotad does not support group quotas
644 if (quotatype != USRQUOTA)
648 * must be some form of "hostname:/path"
650 cp = strchr(fst->f_mntfromname, ':');
652 warnx("cannot find hostname for %s", fst->f_mntfromname);
657 if (*(cp+1) != '/') {
662 /* Avoid attempting the RPC for special amd(8) filesystems. */
663 if (strncmp(fst->f_mntfromname, "pid", 3) == 0 &&
664 strchr(fst->f_mntfromname, '@') != NULL) {
669 gq_args.gqa_pathp = cp + 1;
670 gq_args.gqa_uid = id;
671 if (callaurpc(fst->f_mntfromname, RQUOTAPROG, RQUOTAVERS,
672 RQUOTAPROC_GETQUOTA, (xdrproc_t)xdr_getquota_args, (char *)&gq_args,
673 (xdrproc_t)xdr_getquota_rslt, (char *)&gq_rslt) != 0) {
678 switch (gq_rslt.status) {
682 warnx("quota permission error, host: %s",
686 gettimeofday(&tv, NULL);
688 dqp->dqb_bhardlimit =
689 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit *
690 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
691 dqp->dqb_bsoftlimit =
692 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit *
693 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
695 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks *
696 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
698 dqp->dqb_ihardlimit =
699 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit;
700 dqp->dqb_isoftlimit =
701 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit;
703 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles;
706 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft;
708 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft;
712 warnx("bad rpc result, host: %s", fst->f_mntfromname);
720 callaurpc(char *host, int prognum, int versnum, int procnum,
721 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
723 struct sockaddr_in server_addr;
724 enum clnt_stat clnt_stat;
726 struct timeval timeout, tottimeout;
728 CLIENT *client = NULL;
729 int sock = RPC_ANYSOCK;
731 if ((hp = gethostbyname(host)) == NULL)
732 return ((int) RPC_UNKNOWNHOST);
735 bcopy(hp->h_addr, &server_addr.sin_addr,
736 MIN(hp->h_length,(int)sizeof(server_addr.sin_addr)));
737 server_addr.sin_family = AF_INET;
738 server_addr.sin_port = 0;
740 if ((client = clntudp_create(&server_addr, prognum,
741 versnum, timeout, &sock)) == NULL)
742 return ((int) rpc_createerr.cf_stat);
744 client->cl_auth = authunix_create_default();
745 tottimeout.tv_sec = 25;
746 tottimeout.tv_usec = 0;
747 clnt_stat = clnt_call(client, procnum, inproc, in,
748 outproc, out, tottimeout);
750 return ((int) clnt_stat);
762 } while ((c = *s++));