2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Robert Elz at The University of Melbourne.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static const char copyright[] =
37 "@(#) Copyright (c) 1980, 1990, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
42 static const char sccsid[] = "from: @(#)quota.c 8.1 (Berkeley) 6/6/93";
46 * Disk quota reporting program.
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
51 #include <sys/param.h>
52 #include <sys/types.h>
55 #include <sys/mount.h>
56 #include <sys/socket.h>
59 #include <rpc/pmap_prot.h>
60 #include <rpcsvc/rquota.h>
62 #include <ufs/ufs/quota.h>
78 static const char *qfextension[] = INITQFNAMES;
81 struct quotause *next;
84 char fsname[MAXPATHLEN + 1];
87 static char *timeprt(int64_t seconds);
88 static struct quotause *getprivs(long id, int quotatype);
89 static void usage(void);
90 static int showuid(u_long uid);
91 static int showgid(u_long gid);
92 static int showusrname(char *name);
93 static int showgrpname(char *name);
94 static int showquotas(int type, u_long id, const char *name);
95 static void showrawquotas(int type, u_long id, struct quotause *qup);
96 static void heading(int type, u_long id, const char *name, const char *tag);
97 static int getufsquota(struct fstab *fs, struct quotause *qup, long id,
99 static int getnfsquota(struct statfs *fst, struct quotause *qup, long id,
101 static enum clnt_stat callaurpc(char *host, int prognum, int versnum, int procnum,
102 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out);
103 static int alldigits(char *s);
110 static char *filename = NULL;
113 main(int argc, char *argv[])
116 gid_t mygid, gidset[NGROUPS];
117 int i, ch, gflag = 0, uflag = 0, errflag = 0;
119 while ((ch = getopt(argc, argv, "f:ghlrquv")) != -1) {
151 if (!uflag && !gflag)
155 errflag += showuid(getuid());
158 ngroups = getgroups(NGROUPS, gidset);
161 errflag += showgid(mygid);
162 for (i = 0; i < ngroups; i++)
163 if (gidset[i] != mygid)
164 errflag += showgid(gidset[i]);
171 for (; argc > 0; argc--, argv++) {
172 if (alldigits(*argv))
173 errflag += showuid(atoi(*argv));
175 errflag += showusrname(*argv);
180 for (; argc > 0; argc--, argv++) {
181 if (alldigits(*argv))
182 errflag += showgid(atoi(*argv));
184 errflag += showgrpname(*argv);
194 fprintf(stderr, "%s\n%s\n%s\n",
195 "usage: quota [-ghlu] [-f path] [-v | -q | -r]",
196 " quota [-hlu] [-f path] [-v | -q | -r] user ...",
197 " quota -g [-hl] [-f path] [-v | -q | -r] group ...");
202 * Print out quotas for a specified user identifier.
207 struct passwd *pwd = getpwuid(uid);
211 name = "(no account)";
214 return(showquotas(USRQUOTA, uid, name));
218 * Print out quotas for a specifed user name.
221 showusrname(char *name)
223 struct passwd *pwd = getpwnam(name);
226 warnx("%s: unknown user", name);
229 return(showquotas(USRQUOTA, pwd->pw_uid, name));
233 * Print out quotas for a specified group identifier.
238 struct group *grp = getgrgid(gid);
245 return(showquotas(GRPQUOTA, gid, name));
249 * Print out quotas for a specifed group name.
252 showgrpname(char *name)
254 struct group *grp = getgrnam(name);
257 warnx("%s: unknown group", name);
260 return(showquotas(GRPQUOTA, grp->gr_gid, name));
264 prthumanval(int len, u_int64_t bytes)
269 * Limit the width to 5 bytes as that is what users expect.
271 humanize_number(buf, MIN(sizeof(buf), 5), bytes, "",
272 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
274 (void)printf(" %*s", len, buf);
278 showquotas(int type, u_long id, const char *name)
280 struct quotause *qup;
281 struct quotause *quplist;
282 const char *msgi, *msgb;
284 char *bgrace = NULL, *igrace = NULL;
285 int lines = 0, overquota = 0;
290 quplist = getprivs(id, type);
291 for (qup = quplist; qup; qup = qup->next) {
293 if (qup->dqblk.dqb_ihardlimit &&
294 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_ihardlimit) {
296 msgi = "File limit reached on";
298 else if (qup->dqblk.dqb_isoftlimit &&
299 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_isoftlimit) {
301 if (qup->dqblk.dqb_itime > now)
302 msgi = "In file grace period on";
304 msgi = "Over file quota on";
307 if (qup->dqblk.dqb_bhardlimit &&
308 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bhardlimit) {
310 msgb = "Block limit reached on";
312 else if (qup->dqblk.dqb_bsoftlimit &&
313 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit) {
315 if (qup->dqblk.dqb_btime > now)
316 msgb = "In block grace period on";
318 msgb = "Over block quota on";
321 showrawquotas(type, id, qup);
325 qup->dqblk.dqb_isoftlimit == 0 &&
326 qup->dqblk.dqb_ihardlimit == 0 &&
327 qup->dqblk.dqb_bsoftlimit == 0 &&
328 qup->dqblk.dqb_bhardlimit == 0)
331 if ((msgi != NULL || msgb != NULL) &&
333 heading(type, id, name, "");
335 printf("\t%s %s\n", msgi, qup->fsname);
337 printf("\t%s %s\n", msgb, qup->fsname);
341 qup->dqblk.dqb_curblocks == 0 &&
342 qup->dqblk.dqb_curinodes == 0)
345 heading(type, id, name, "");
347 if (strlen(qup->fsname) > 15) {
348 printf("%s\n", qup->fsname);
351 printf("%-15s", nam);
353 prthumanval(7, dbtob(qup->dqblk.dqb_curblocks));
354 printf("%c", (msgb == NULL) ? ' ' : '*');
355 prthumanval(7, dbtob(qup->dqblk.dqb_bsoftlimit));
356 prthumanval(7, dbtob(qup->dqblk.dqb_bhardlimit));
358 printf(" %7ju%c %7ju %7ju",
359 (uintmax_t)dbtob(qup->dqblk.dqb_curblocks)
361 (msgb == NULL) ? ' ' : '*',
362 (uintmax_t)dbtob(qup->dqblk.dqb_bsoftlimit)
364 (uintmax_t)dbtob(qup->dqblk.dqb_bhardlimit)
368 bgrace = timeprt(qup->dqblk.dqb_btime);
370 igrace = timeprt(qup->dqblk.dqb_itime);
371 printf("%8s %6ju%c %6ju %6ju%8s\n"
372 , (msgb == NULL) ? "" : bgrace
373 , (uintmax_t)qup->dqblk.dqb_curinodes
374 , (msgi == NULL) ? ' ' : '*'
375 , (uintmax_t)qup->dqblk.dqb_isoftlimit
376 , (uintmax_t)qup->dqblk.dqb_ihardlimit
377 , (msgi == NULL) ? "" : igrace
384 if (!qflag && !rflag && lines == 0)
385 heading(type, id, name, "none");
390 showrawquotas(int type, u_long id, struct quotause *qup)
394 printf("Raw %s quota information for id %lu on %s\n",
395 type == USRQUOTA ? "user" : "group", id, qup->fsname);
396 printf("block hard limit: %ju\n",
397 (uintmax_t)qup->dqblk.dqb_bhardlimit);
398 printf("block soft limit: %ju\n",
399 (uintmax_t)qup->dqblk.dqb_bsoftlimit);
400 printf("current block count: %ju\n",
401 (uintmax_t)qup->dqblk.dqb_curblocks);
402 printf("i-node hard limit: %ju\n",
403 (uintmax_t)qup->dqblk.dqb_ihardlimit);
404 printf("i-node soft limit: %ju\n",
405 (uintmax_t)qup->dqblk.dqb_isoftlimit);
406 printf("current i-node count: %ju\n",
407 (uintmax_t)qup->dqblk.dqb_curinodes);
408 printf("block grace time: %jd",
409 (intmax_t)qup->dqblk.dqb_btime);
410 if (qup->dqblk.dqb_btime != 0) {
411 t = qup->dqblk.dqb_btime;
412 printf(" %s", ctime(&t));
416 printf("i-node grace time: %jd", (intmax_t)qup->dqblk.dqb_itime);
417 if (qup->dqblk.dqb_itime != 0) {
418 t = qup->dqblk.dqb_itime;
419 printf(" %s", ctime(&t));
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 %8s %7s %7s %6s %7s %6s%8s\n"
448 * Calculate the grace period and return a printable string for it.
451 timeprt(int64_t seconds)
453 time_t hours, minutes;
460 if ((buf = strdup("none")) == NULL)
461 errx(1, "strdup() failed in timeprt()");
465 minutes = (seconds + 30) / 60;
466 hours = (minutes + 30) / 60;
468 if (asprintf(&buf, "%lddays", ((long)hours + 12) / 24) < 0)
469 errx(1, "asprintf() failed in timeprt(1)");
473 if (asprintf(&buf, "%2ld:%ld", (long)minutes / 60,
474 (long)minutes % 60) < 0)
475 errx(1, "asprintf() failed in timeprt(2)");
478 if (asprintf(&buf, "%2ld", (long)minutes) < 0)
479 errx(1, "asprintf() failed in timeprt(3)");
484 * Collect the requested quota information.
486 static struct quotause *
487 getprivs(long id, int quotatype)
489 struct quotause *qup, *quptail = NULL;
491 struct quotause *quphead;
496 qup = quphead = (struct quotause *)0;
498 if (filename != NULL && statfs(filename, &sfb) != 0)
499 err(1, "cannot statfs %s", filename);
500 nfst = getmntinfo(&fst, MNT_NOWAIT);
502 errx(2, "no filesystems mounted!");
504 for (i = 0; i < nfst; i++) {
506 if ((qup = (struct quotause *)malloc(sizeof *qup))
508 errx(2, "out of memory");
511 * See if the user requested a specific file system
512 * or specified a file inside a mounted file system.
514 if (filename != NULL &&
515 strcmp(sfb.f_mntonname, fst[i].f_mntonname) != 0)
517 if (strcmp(fst[i].f_fstypename, "nfs") == 0) {
520 if (getnfsquota(&fst[i], qup, id, quotatype) == 0)
522 } else if (strcmp(fst[i].f_fstypename, "ufs") == 0) {
525 * UFS filesystems must be in /etc/fstab, and must
526 * indicate that they have quotas on (?!) This is quite
527 * unlike SunOS where quotas can be enabled/disabled
528 * on a filesystem independent of /etc/fstab, and it
529 * will still print quotas for them.
531 if ((fs = getfsspec(fst[i].f_mntfromname)) == NULL)
533 if (getufsquota(fs, qup, id, quotatype) == 0)
537 strcpy(qup->fsname, fst[i].f_mntonname);
553 * Check to see if a particular quota is available.
556 getufsquota(struct fstab *fs, struct quotause *qup, long id, int quotatype)
558 struct quotafile *qf;
560 if ((qf = quota_open(fs, quotatype, O_RDONLY)) == NULL)
562 if (quota_read(qf, &qup->dqblk, id) != 0)
569 getnfsquota(struct statfs *fst, struct quotause *qup, long id, int quotatype)
571 struct ext_getquota_args gq_args;
572 struct getquota_args old_gq_args;
573 struct getquota_rslt gq_rslt;
574 struct dqblk *dqp = &qup->dqblk;
576 char *cp, host[NI_MAXHOST];
577 enum clnt_stat call_stat;
579 if (fst->f_flags & MNT_LOCAL)
583 * must be some form of "hostname:/path"
585 cp = fst->f_mntfromname;
587 cp = strrchr(cp, ':');
588 } while (cp != NULL && *(cp + 1) != '/');
590 warnx("cannot find hostname for %s", fst->f_mntfromname);
593 memset(host, 0, sizeof(host));
594 memcpy(host, fst->f_mntfromname, cp - fst->f_mntfromname);
595 host[sizeof(host) - 1] = '\0';
597 /* Avoid attempting the RPC for special amd(8) filesystems. */
598 if (strncmp(fst->f_mntfromname, "pid", 3) == 0 &&
599 strchr(fst->f_mntfromname, '@') != NULL)
602 gq_args.gqa_pathp = cp + 1;
604 gq_args.gqa_type = quotatype;
606 call_stat = callaurpc(host, RQUOTAPROG, EXT_RQUOTAVERS,
607 RQUOTAPROC_GETQUOTA, (xdrproc_t)xdr_ext_getquota_args, (char *)&gq_args,
608 (xdrproc_t)xdr_getquota_rslt, (char *)&gq_rslt);
609 if (call_stat == RPC_PROGVERSMISMATCH || call_stat == RPC_PROGNOTREGISTERED) {
610 if (quotatype == USRQUOTA) {
611 old_gq_args.gqa_pathp = cp + 1;
612 old_gq_args.gqa_uid = id;
613 call_stat = callaurpc(host, RQUOTAPROG, RQUOTAVERS,
614 RQUOTAPROC_GETQUOTA, (xdrproc_t)xdr_getquota_args, (char *)&old_gq_args,
615 (xdrproc_t)xdr_getquota_rslt, (char *)&gq_rslt);
617 /* Old rpc quota does not support group type */
624 switch (gq_rslt.status) {
628 warnx("quota permission error, host: %s",
632 gettimeofday(&tv, NULL);
634 dqp->dqb_bhardlimit =
635 ((uint64_t)gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit *
636 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize) / DEV_BSIZE;
637 dqp->dqb_bsoftlimit =
638 ((uint64_t)gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit *
639 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize) / DEV_BSIZE;
641 ((uint64_t)gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks *
642 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize) / DEV_BSIZE;
644 dqp->dqb_ihardlimit =
645 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit;
646 dqp->dqb_isoftlimit =
647 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit;
649 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles;
652 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft;
654 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft;
657 warnx("bad rpc result, host: %s", fst->f_mntfromname);
664 static enum clnt_stat
665 callaurpc(char *host, int prognum, int versnum, int procnum,
666 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
668 enum clnt_stat clnt_stat;
669 struct timeval timeout, tottimeout;
671 CLIENT *client = NULL;
673 client = clnt_create(host, prognum, versnum, "udp");
675 return ((int)rpc_createerr.cf_stat);
678 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout);
680 client->cl_auth = authunix_create_default();
681 tottimeout.tv_sec = 25;
682 tottimeout.tv_usec = 0;
683 clnt_stat = clnt_call(client, procnum, inproc, in,
684 outproc, out, tottimeout);
697 } while ((c = *s++));