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
36 * @(#) Copyright (c) 1980, 1990, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)quota.c 8.1 (Berkeley) 6/6/93
38 * $FreeBSD: src/usr.bin/quota/quota.c,v 1.11.2.5 2002/11/30 23:54:21 iedowse Exp $
39 * $DragonFly: src/usr.bin/quota/quota.c,v 1.5 2005/03/18 00:57:10 joerg Exp $
43 * Disk quota reporting program.
46 #include <sys/param.h>
47 #include <sys/types.h>
50 #include <sys/mount.h>
51 #include <sys/socket.h>
54 #include <rpc/pmap_prot.h>
55 #include <rpcsvc/rquota.h>
57 #include <vfs/ufs/quota.h>
70 const char *qfname = QUOTAFILENAME;
71 const char *qfextension[] = INITQFNAMES;
74 struct quotause *next;
77 char fsname[MAXPATHLEN + 1];
81 static const char *timeprt(time_t seconds);
82 static struct quotause *getprivs(long id, int quotatype);
83 static void usage(void);
84 static void showuid(u_long uid);
85 static void showgid(u_long gid);
86 static int alldigits(char *s);
87 static void showusrname(char *name);
88 static void showgrpname(char *name);
89 static void showquotas(int type, u_long id, const char *name);
90 static void heading(int type, u_long id, const char *name, const char *tag);
91 static int ufshasquota(struct fstab *fs, int type, char **qfnamep);
92 static int getufsquota(struct fstab *fs, struct quotause *qup, long id,
94 static int getnfsquota(struct statfs *fst, struct quotause *qup, long id,
96 static int callaurpc(char *host, int prognum, int versnum, int procnum,
97 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out);
104 main(int argc, char *argv[])
107 gid_t mygid, gidset[NGROUPS];
108 int i, gflag = 0, uflag = 0;
111 while ((ch = getopt(argc, argv, "glquv")) != -1) {
134 if (!uflag && !gflag)
141 ngroups = getgroups(NGROUPS, gidset);
145 for (i = 0; i < ngroups; i++)
146 if (gidset[i] != mygid)
154 for (; argc > 0; argc--, argv++) {
155 if (alldigits(*argv))
156 showuid(atoi(*argv));
163 for (; argc > 0; argc--, argv++) {
164 if (alldigits(*argv))
165 showgid(atoi(*argv));
177 fprintf(stderr, "%s\n%s\n%s\n",
178 "usage: quota [-glu] [-v | -q]",
179 " quota [-lu] [-v | -q] user ...",
180 " quota -g [-l] [-v | -q] group ...");
185 * Print out quotas for a specified user identifier.
190 struct passwd *pwd = getpwuid(uid);
195 name = "(no account)";
199 if (uid != myuid && myuid != 0) {
200 printf("quota: %s (uid %lu): permission denied\n", name, uid);
203 showquotas(USRQUOTA, uid, name);
207 * Print out quotas for a specifed user name.
210 showusrname(char *name)
212 struct passwd *pwd = getpwnam(name);
216 warnx("%s: unknown user", name);
220 if (pwd->pw_uid != myuid && myuid != 0) {
221 warnx("%s (uid %u): permission denied", name, pwd->pw_uid);
224 showquotas(USRQUOTA, pwd->pw_uid, name);
228 * Print out quotas for a specified group identifier.
233 struct group *grp = getgrgid(gid);
235 gid_t mygid, gidset[NGROUPS];
244 ngroups = getgroups(NGROUPS, gidset);
250 for (i = 0; i < ngroups; i++)
251 if (gid == gidset[i])
253 if (i >= ngroups && getuid() != 0) {
254 warnx("%s (gid %lu): permission denied", name, gid);
258 showquotas(GRPQUOTA, gid, name);
262 * Print out quotas for a specifed group name.
265 showgrpname(char *name)
267 struct group *grp = getgrnam(name);
269 gid_t mygid, gidset[NGROUPS];
273 warnx("%s: unknown group", name);
277 ngroups = getgroups(NGROUPS, gidset);
282 if (grp->gr_gid != mygid) {
283 for (i = 0; i < ngroups; i++)
284 if (grp->gr_gid == gidset[i])
286 if (i >= ngroups && getuid() != 0) {
287 warnx("%s (gid %u): permission denied", name,
292 showquotas(GRPQUOTA, grp->gr_gid, name);
296 showquotas(int type, u_long id, const char *name)
298 struct quotause *qup;
299 struct quotause *quplist;
300 const char *msgi, *msgb;
307 quplist = getprivs(id, type);
308 for (qup = quplist; qup; qup = qup->next) {
310 qup->dqblk.dqb_isoftlimit == 0 &&
311 qup->dqblk.dqb_ihardlimit == 0 &&
312 qup->dqblk.dqb_bsoftlimit == 0 &&
313 qup->dqblk.dqb_bhardlimit == 0)
316 if (qup->dqblk.dqb_ihardlimit &&
317 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_ihardlimit)
318 msgi = "File limit reached on";
319 else if (qup->dqblk.dqb_isoftlimit &&
320 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_isoftlimit) {
321 if (qup->dqblk.dqb_itime > now)
322 msgi = "In file grace period on";
324 msgi = "Over file quota on";
327 if (qup->dqblk.dqb_bhardlimit &&
328 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bhardlimit)
329 msgb = "Block limit reached on";
330 else if (qup->dqblk.dqb_bsoftlimit &&
331 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit) {
332 if (qup->dqblk.dqb_btime > now)
333 msgb = "In block grace period on";
335 msgb = "Over block quota on";
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);
357 printf("%15s%8lu%c%7lu%8lu%8s"
359 , (u_long) (dbtob(qup->dqblk.dqb_curblocks)
361 , (msgb == (char *)0) ? ' ' : '*'
362 , (u_long) (dbtob(qup->dqblk.dqb_bsoftlimit)
364 , (u_long) (dbtob(qup->dqblk.dqb_bhardlimit)
366 , (msgb == (char *)0) ? ""
367 :timeprt(qup->dqblk.dqb_btime));
368 printf("%8lu%c%7lu%8lu%8s\n"
369 , (u_long)qup->dqblk.dqb_curinodes
370 , (msgi == (char *)0) ? ' ' : '*'
371 , (u_long)qup->dqblk.dqb_isoftlimit
372 , (u_long)qup->dqblk.dqb_ihardlimit
373 , (msgi == (char *)0) ? ""
374 : timeprt(qup->dqblk.dqb_itime)
379 if (!qflag && lines == 0)
380 heading(type, id, name, "none");
384 heading(int type, u_long id, const char *name, const char *tag)
387 printf("Disk quotas for %s %s (%cid %lu): %s\n", qfextension[type],
388 name, *qfextension[type], id, tag);
389 if (!qflag && tag[0] == '\0') {
390 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n"
405 * Calculate the grace period and return a printable string for it.
408 timeprt(time_t seconds)
410 time_t hours, minutes;
419 minutes = (seconds + 30) / 60;
420 hours = (minutes + 30) / 60;
422 sprintf(buf, "%lddays", ((long)hours + 12) / 24);
426 sprintf(buf, "%2ld:%ld", (long)minutes / 60,
430 sprintf(buf, "%2ld", (long)minutes);
435 * Collect the requested quota information.
437 static struct quotause *
438 getprivs(long id, int quotatype)
440 struct quotause *qup, *quptail = NULL;
442 struct quotause *quphead;
446 qup = quphead = (struct quotause *)0;
448 nfst = getmntinfo(&fst, MNT_NOWAIT);
450 errx(2, "no filesystems mounted!");
452 for (i=0; i<nfst; i++) {
454 if ((qup = (struct quotause *)malloc(sizeof *qup))
456 errx(2, "out of memory");
458 if (strcmp(fst[i].f_fstypename, "nfs") == 0) {
461 if (getnfsquota(&fst[i], qup, id, quotatype)
464 } else if (strcmp(fst[i].f_fstypename, "ufs") == 0) {
467 * UFS filesystems must be in /etc/fstab, and must
468 * indicate that they have quotas on (?!) This is quite
469 * unlike SunOS where quotas can be enabled/disabled
470 * on a filesystem independent of /etc/fstab, and it
471 * will still print quotas for them.
473 if ((fs = getfsspec(fst[i].f_mntfromname)) == NULL)
475 if (getufsquota(fs, qup, id, quotatype) == 0)
479 strcpy(qup->fsname, fst[i].f_mntonname);
495 * Check to see if a particular quota is to be enabled.
498 ufshasquota(struct fstab *fs, int type, char **qfnamep)
500 static char initname, usrname[100], grpname[100];
501 static char buf[BUFSIZ];
505 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
506 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
509 strcpy(buf, fs->fs_mntops);
510 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
511 if ((cp = strchr(opt, '=')))
513 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
515 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
524 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
530 getufsquota(struct fstab *fs, struct quotause *qup, long id, int quotatype)
535 qcmd = QCMD(Q_GETQUOTA, quotatype);
536 if (!ufshasquota(fs, quotatype, &qfpathname))
539 if (quotactl(fs->fs_file, qcmd, id, (char *)&qup->dqblk) != 0) {
540 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
541 warn("%s", qfpathname);
544 (void) lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET);
545 switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
548 * Convert implicit 0 quota (EOF)
549 * into an explicit one (zero'ed dqblk)
551 bzero((caddr_t)&qup->dqblk, sizeof(struct dqblk));
553 case sizeof(struct dqblk): /* OK */
556 warn("read error: %s", qfpathname);
566 getnfsquota(struct statfs *fst, struct quotause *qup, long id, int quotatype)
568 struct getquota_args gq_args;
569 struct getquota_rslt gq_rslt;
570 struct dqblk *dqp = &qup->dqblk;
574 if (fst->f_flags & MNT_LOCAL)
578 * rpc.rquotad does not support group quotas
580 if (quotatype != USRQUOTA)
584 * must be some form of "hostname:/path"
586 cp = strchr(fst->f_mntfromname, ':');
588 warnx("cannot find hostname for %s", fst->f_mntfromname);
593 if (*(cp+1) != '/') {
598 /* Avoid attempting the RPC for special amd(8) filesystems. */
599 if (strncmp(fst->f_mntfromname, "pid", 3) == 0 &&
600 strchr(fst->f_mntfromname, '@') != NULL) {
605 gq_args.gqa_pathp = cp + 1;
606 gq_args.gqa_uid = id;
607 if (callaurpc(fst->f_mntfromname, RQUOTAPROG, RQUOTAVERS,
608 RQUOTAPROC_GETQUOTA, (xdrproc_t)xdr_getquota_args, (char *)&gq_args,
609 (xdrproc_t)xdr_getquota_rslt, (char *)&gq_rslt) != 0) {
614 switch (gq_rslt.status) {
618 warnx("quota permission error, host: %s",
622 gettimeofday(&tv, NULL);
624 dqp->dqb_bhardlimit =
625 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit *
626 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE;
627 dqp->dqb_bsoftlimit =
628 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit *
629 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE;
631 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks *
632 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE;
634 dqp->dqb_ihardlimit =
635 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit;
636 dqp->dqb_isoftlimit =
637 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit;
639 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles;
642 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft;
644 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft;
648 warnx("bad rpc result, host: %s", fst->f_mntfromname);
656 callaurpc(char *host, int prognum, int versnum, int procnum,
657 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
659 struct sockaddr_in server_addr;
660 enum clnt_stat clnt_stat;
662 struct timeval timeout, tottimeout;
664 CLIENT *client = NULL;
665 int sock = RPC_ANYSOCK;
667 if ((hp = gethostbyname(host)) == NULL)
668 return ((int) RPC_UNKNOWNHOST);
671 bcopy(hp->h_addr, &server_addr.sin_addr,
672 MIN(hp->h_length,(int)sizeof(server_addr.sin_addr)));
673 server_addr.sin_family = AF_INET;
674 server_addr.sin_port = 0;
676 if ((client = clntudp_create(&server_addr, prognum,
677 versnum, timeout, &sock)) == NULL)
678 return ((int) rpc_createerr.cf_stat);
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);
686 return ((int) clnt_stat);
698 } while ((c = *s++));