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[])
121 gid_t mygid, *gidset;
122 int i, ch, gflag = 0, uflag = 0, errflag = 0;
124 while ((ch = getopt(argc, argv, "f:ghlrquv")) != -1) {
156 if (!uflag && !gflag)
160 errflag += showuid(getuid());
163 ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
164 if ((gidset = malloc(sizeof(gid_t) * ngroups_max))
167 ngroups = getgroups(ngroups_max, gidset);
170 errflag += showgid(mygid);
171 for (i = 0; i < ngroups; i++)
172 if (gidset[i] != mygid)
173 errflag += showgid(gidset[i]);
181 for (; argc > 0; argc--, argv++) {
182 if (alldigits(*argv))
183 errflag += showuid(atoi(*argv));
185 errflag += showusrname(*argv);
190 for (; argc > 0; argc--, argv++) {
191 if (alldigits(*argv))
192 errflag += showgid(atoi(*argv));
194 errflag += showgrpname(*argv);
204 fprintf(stderr, "%s\n%s\n%s\n",
205 "usage: quota [-ghlu] [-f path] [-v | -q | -r]",
206 " quota [-hlu] [-f path] [-v | -q | -r] user ...",
207 " quota -g [-hl] [-f path] [-v | -q | -r] group ...");
212 * Print out quotas for a specified user identifier.
217 struct passwd *pwd = getpwuid(uid);
221 name = "(no account)";
224 return(showquotas(USRQUOTA, uid, name));
228 * Print out quotas for a specifed user name.
231 showusrname(char *name)
233 struct passwd *pwd = getpwnam(name);
236 warnx("%s: unknown user", name);
239 return(showquotas(USRQUOTA, pwd->pw_uid, name));
243 * Print out quotas for a specified group identifier.
248 struct group *grp = getgrgid(gid);
255 return(showquotas(GRPQUOTA, gid, name));
259 * Print out quotas for a specifed group name.
262 showgrpname(char *name)
264 struct group *grp = getgrnam(name);
267 warnx("%s: unknown group", name);
270 return(showquotas(GRPQUOTA, grp->gr_gid, name));
274 prthumanval(int len, int64_t bytes)
278 humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE,
279 HN_B | HN_NOSPACE | HN_DECIMAL);
281 (void)printf(" %*s", len, buf);
285 showquotas(int type, u_long id, const char *name)
287 struct quotause *qup;
288 struct quotause *quplist;
289 const char *msgi, *msgb;
291 char *bgrace = NULL, *igrace = NULL;
292 int lines = 0, overquota = 0;
297 quplist = getprivs(id, type);
298 for (qup = quplist; qup; qup = qup->next) {
300 if (qup->dqblk.dqb_ihardlimit &&
301 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_ihardlimit) {
303 msgi = "File limit reached on";
305 else if (qup->dqblk.dqb_isoftlimit &&
306 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_isoftlimit) {
308 if (qup->dqblk.dqb_itime > now)
309 msgi = "In file grace period on";
311 msgi = "Over file quota on";
314 if (qup->dqblk.dqb_bhardlimit &&
315 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bhardlimit) {
317 msgb = "Block limit reached on";
319 else if (qup->dqblk.dqb_bsoftlimit &&
320 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit) {
322 if (qup->dqblk.dqb_btime > now)
323 msgb = "In block grace period on";
325 msgb = "Over block quota on";
328 showrawquotas(type, id, qup);
332 qup->dqblk.dqb_isoftlimit == 0 &&
333 qup->dqblk.dqb_ihardlimit == 0 &&
334 qup->dqblk.dqb_bsoftlimit == 0 &&
335 qup->dqblk.dqb_bhardlimit == 0)
338 if ((msgi != (char *)0 || msgb != (char *)0) &&
340 heading(type, id, name, "");
341 if (msgi != (char *)0)
342 printf("\t%s %s\n", msgi, qup->fsname);
343 if (msgb != (char *)0)
344 printf("\t%s %s\n", msgb, qup->fsname);
348 qup->dqblk.dqb_curblocks ||
349 qup->dqblk.dqb_curinodes) {
351 heading(type, id, name, "");
353 if (strlen(qup->fsname) > 15) {
354 printf("%s\n", qup->fsname);
360 prthumanval(4, dbtob(qup->dqblk.dqb_curblocks));
361 printf("%c ", (msgb == (char *)0) ? ' ' : '*');
362 prthumanval(4, dbtob(qup->dqblk.dqb_bsoftlimit));
364 prthumanval(4, dbtob(qup->dqblk.dqb_bhardlimit));
366 printf(" %7ju%c %6ju %7ju",
367 (uintmax_t)(dbtob(qup->dqblk.dqb_curblocks)
369 (msgb == NULL) ? ' ' : '*',
370 (uintmax_t)(dbtob(qup->dqblk.dqb_bsoftlimit)
372 (uintmax_t)(dbtob(qup->dqblk.dqb_bhardlimit)
376 bgrace = timeprt(qup->dqblk.dqb_btime);
378 igrace = timeprt(qup->dqblk.dqb_itime);
379 printf(" %7s %7ju%c %6ju %7ju %7s\n",
380 (msgb == NULL) ? "" : bgrace,
381 (uintmax_t)qup->dqblk.dqb_curinodes,
382 (msgi == NULL) ? ' ' : '*',
383 (uintmax_t)qup->dqblk.dqb_isoftlimit,
384 (uintmax_t)qup->dqblk.dqb_ihardlimit,
385 (msgi == NULL) ? "" : igrace
394 if (!qflag && !rflag && lines == 0)
395 heading(type, id, name, "none");
400 showrawquotas(int type, u_long id, struct quotause *qup)
403 printf("Raw %s quota information for id %lu on %s\n",
404 type == USRQUOTA ? "user" : "group", id, qup->fsname);
405 printf("block hard limit: %ju\n", (uintmax_t)qup->dqblk.dqb_bhardlimit);
406 printf("block soft limit: %ju\n", (uintmax_t)qup->dqblk.dqb_bsoftlimit);
407 printf("current block count: %ju\n", (uintmax_t)qup->dqblk.dqb_curblocks);
408 printf("i-node hard limit: %ju\n", (uintmax_t)qup->dqblk.dqb_ihardlimit);
409 printf("i-node soft limit: %ju\n", (uintmax_t)qup->dqblk.dqb_isoftlimit);
410 printf("current i-node count: %ju\n", (uintmax_t)qup->dqblk.dqb_curinodes);
411 printf("block grace time: %jd", (intmax_t)qup->dqblk.dqb_btime);
412 if (qup->dqblk.dqb_btime != 0) {
413 tt = qup->dqblk.dqb_btime;
414 printf(" %s", ctime(&tt));
417 printf("i-node grace time: %jd", (intmax_t)qup->dqblk.dqb_itime);
418 if (qup->dqblk.dqb_itime != 0) {
419 tt = qup->dqblk.dqb_itime;
420 printf(" %s", ctime(&tt));
427 heading(int type, u_long id, const char *name, const char *tag)
430 printf("Disk quotas for %s %s (%cid %lu): %s\n", qfextension[type],
431 name, *qfextension[type], id, tag);
432 if (!qflag && tag[0] == '\0') {
433 printf("%15s %7s %6s %7s %7s %7s %6s %7s %7s\n"
448 * Calculate the grace period and return a printable string for it.
451 timeprt(time_t seconds)
453 time_t hours, minutes;
460 return strdup("none");
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 to be enabled.
554 ufshasquota(struct fstab *fs, int type, char **qfnamep)
559 static char initname, usrname[100], grpname[100];
560 static char buf[BUFSIZ];
563 (void)snprintf(usrname, sizeof(usrname), "%s%s",
564 qfextension[USRQUOTA], qfname);
565 (void)snprintf(grpname, sizeof(grpname), "%s%s",
566 qfextension[GRPQUOTA], qfname);
569 strcpy(buf, fs->fs_mntops);
570 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
571 if ((cp = index(opt, '=')))
573 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
575 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
583 (void)snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file,
584 qfname, qfextension[type]);
587 if (statfs(fs->fs_file, &sfb) != 0) {
588 warn("cannot statfs mount point %s", fs->fs_file);
591 if (strcmp(fs->fs_file, sfb.f_mntonname)) {
592 warnx("%s not mounted for %s quotas", fs->fs_file,
593 type == USRQUOTA ? "user" : "group");
600 getufsquota(struct fstab *fs, struct quotause *qup, long id, int quotatype)
605 qcmd = QCMD(Q_GETQUOTA, quotatype);
606 if (!ufshasquota(fs, quotatype, &qfpathname))
609 if (quotactl(fs->fs_file, qcmd, id, (char *)&qup->dqblk) != 0) {
610 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
611 warn("%s", qfpathname);
614 (void) lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET);
615 switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
618 * Convert implicit 0 quota (EOF)
619 * into an explicit one (zero'ed dqblk)
621 bzero((caddr_t)&qup->dqblk, sizeof(struct dqblk));
623 case sizeof(struct dqblk): /* OK */
626 warn("read error: %s", qfpathname);
636 getnfsquota(struct statfs *fst, struct quotause *qup, long id, int quotatype)
638 struct getquota_args gq_args;
639 struct getquota_rslt gq_rslt;
640 struct dqblk *dqp = &qup->dqblk;
644 if (fst->f_flags & MNT_LOCAL)
648 * rpc.rquotad does not support group quotas
650 if (quotatype != USRQUOTA)
654 * must be some form of "hostname:/path"
656 cp = strchr(fst->f_mntfromname, ':');
658 warnx("cannot find hostname for %s", fst->f_mntfromname);
663 if (*(cp+1) != '/') {
668 /* Avoid attempting the RPC for special amd(8) filesystems. */
669 if (strncmp(fst->f_mntfromname, "pid", 3) == 0 &&
670 strchr(fst->f_mntfromname, '@') != NULL) {
675 gq_args.gqa_pathp = cp + 1;
676 gq_args.gqa_uid = id;
677 if (callaurpc(fst->f_mntfromname, RQUOTAPROG, RQUOTAVERS,
678 RQUOTAPROC_GETQUOTA, (xdrproc_t)xdr_getquota_args, (char *)&gq_args,
679 (xdrproc_t)xdr_getquota_rslt, (char *)&gq_rslt) != 0) {
684 switch (gq_rslt.status) {
688 warnx("quota permission error, host: %s",
692 gettimeofday(&tv, NULL);
694 dqp->dqb_bhardlimit =
695 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit *
696 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
697 dqp->dqb_bsoftlimit =
698 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit *
699 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
701 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks *
702 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
704 dqp->dqb_ihardlimit =
705 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit;
706 dqp->dqb_isoftlimit =
707 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit;
709 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles;
712 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft;
714 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft;
718 warnx("bad rpc result, host: %s", fst->f_mntfromname);
726 callaurpc(char *host, int prognum, int versnum, int procnum,
727 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
729 struct sockaddr_in server_addr;
730 enum clnt_stat clnt_stat;
732 struct timeval timeout, tottimeout;
734 CLIENT *client = NULL;
735 int sock = RPC_ANYSOCK;
737 if ((hp = gethostbyname(host)) == NULL)
738 return ((int) RPC_UNKNOWNHOST);
741 bcopy(hp->h_addr, &server_addr.sin_addr,
742 MIN(hp->h_length,(int)sizeof(server_addr.sin_addr)));
743 server_addr.sin_family = AF_INET;
744 server_addr.sin_port = 0;
746 if ((client = clntudp_create(&server_addr, prognum,
747 versnum, timeout, &sock)) == NULL)
748 return ((int) rpc_createerr.cf_stat);
750 client->cl_auth = authunix_create_default();
751 tottimeout.tv_sec = 25;
752 tottimeout.tv_usec = 0;
753 clnt_stat = clnt_call(client, procnum, inproc, in,
754 outproc, out, tottimeout);
756 return ((int) clnt_stat);
768 } while ((c = *s++));