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 $
42 * Disk quota reporting program.
45 #include <sys/param.h>
46 #include <sys/types.h>
49 #include <sys/mount.h>
50 #include <sys/socket.h>
53 #include <rpc/pmap_prot.h>
54 #include <rpcsvc/rquota.h>
56 #include <vfs/ufs/quota.h>
69 const char *qfname = QUOTAFILENAME;
70 const char *qfextension[] = INITQFNAMES;
73 struct quotause *next;
75 struct ufs_dqblk dqblk;
76 char fsname[MAXPATHLEN + 1];
80 static const char *timeprt(time_t);
81 static struct quotause *getprivs(long, int);
82 static void usage(void);
83 static void showuid(u_long);
84 static void showgid(u_long);
85 static int alldigits(char *);
86 static void showusrname(char *);
87 static void showgrpname(char *);
88 static void showquotas(int, u_long, const char *);
89 static void heading(int, u_long, const char *, const char *);
90 static int ufshasquota(struct fstab *, int, char **);
91 static int getufsquota(struct fstab *, struct quotause *, long, int);
92 static int getnfsquota(struct statfs *, struct quotause *, long, int);
93 static int callaurpc(char *, int, int, int, xdrproc_t, char *, xdrproc_t,
101 main(int argc, char *argv[])
104 gid_t mygid, gidset[NGROUPS];
105 int i, gflag = 0, uflag = 0;
108 while ((ch = getopt(argc, argv, "glquv")) != -1) {
131 if (!uflag && !gflag)
138 ngroups = getgroups(NGROUPS, gidset);
142 for (i = 0; i < ngroups; i++)
143 if (gidset[i] != mygid)
151 for (; argc > 0; argc--, argv++) {
152 if (alldigits(*argv))
153 showuid(atoi(*argv));
160 for (; argc > 0; argc--, argv++) {
161 if (alldigits(*argv))
162 showgid(atoi(*argv));
174 fprintf(stderr, "%s\n%s\n%s\n",
175 "usage: quota [-glu] [-v | -q]",
176 " quota [-lu] [-v | -q] user ...",
177 " quota -g [-l] [-v | -q] group ...");
182 * Print out quotas for a specified user identifier.
187 struct passwd *pwd = getpwuid(uid);
192 name = "(no account)";
196 if (uid != myuid && myuid != 0) {
197 printf("quota: %s (uid %lu): permission denied\n", name, uid);
200 showquotas(USRQUOTA, uid, name);
204 * Print out quotas for a specifed user name.
207 showusrname(char *name)
209 struct passwd *pwd = getpwnam(name);
213 warnx("%s: unknown user", name);
217 if (pwd->pw_uid != myuid && myuid != 0) {
218 warnx("%s (uid %u): permission denied", name, pwd->pw_uid);
221 showquotas(USRQUOTA, pwd->pw_uid, name);
225 * Print out quotas for a specified group identifier.
230 struct group *grp = getgrgid(gid);
232 gid_t mygid, gidset[NGROUPS];
241 ngroups = getgroups(NGROUPS, gidset);
247 for (i = 0; i < ngroups; i++)
248 if (gid == gidset[i])
250 if (i >= ngroups && getuid() != 0) {
251 warnx("%s (gid %lu): permission denied", name, gid);
255 showquotas(GRPQUOTA, gid, name);
259 * Print out quotas for a specifed group name.
262 showgrpname(char *name)
264 struct group *grp = getgrnam(name);
266 gid_t mygid, gidset[NGROUPS];
270 warnx("%s: unknown group", name);
274 ngroups = getgroups(NGROUPS, gidset);
279 if (grp->gr_gid != mygid) {
280 for (i = 0; i < ngroups; i++)
281 if (grp->gr_gid == gidset[i])
283 if (i >= ngroups && getuid() != 0) {
284 warnx("%s (gid %u): permission denied", name,
289 showquotas(GRPQUOTA, grp->gr_gid, name);
293 showquotas(int type, u_long id, const char *name)
295 struct quotause *qup;
296 struct quotause *quplist;
297 const char *msgi, *msgb;
304 quplist = getprivs(id, type);
305 for (qup = quplist; qup; qup = qup->next) {
307 qup->dqblk.dqb_isoftlimit == 0 &&
308 qup->dqblk.dqb_ihardlimit == 0 &&
309 qup->dqblk.dqb_bsoftlimit == 0 &&
310 qup->dqblk.dqb_bhardlimit == 0)
313 if (qup->dqblk.dqb_ihardlimit &&
314 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_ihardlimit)
315 msgi = "File limit reached on";
316 else if (qup->dqblk.dqb_isoftlimit &&
317 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_isoftlimit) {
318 if (qup->dqblk.dqb_itime > now)
319 msgi = "In file grace period on";
321 msgi = "Over file quota on";
324 if (qup->dqblk.dqb_bhardlimit &&
325 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bhardlimit)
326 msgb = "Block limit reached on";
327 else if (qup->dqblk.dqb_bsoftlimit &&
328 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit) {
329 if (qup->dqblk.dqb_btime > now)
330 msgb = "In block grace period on";
332 msgb = "Over block quota on";
335 if ((msgi != NULL || msgb != NULL) &&
337 heading(type, id, name, "");
339 printf("\t%s %s\n", msgi, qup->fsname);
341 printf("\t%s %s\n", msgb, qup->fsname);
345 qup->dqblk.dqb_curblocks ||
346 qup->dqblk.dqb_curinodes) {
348 heading(type, id, name, "");
350 if (strlen(qup->fsname) > 15) {
351 printf("%s\n", qup->fsname);
354 printf("%15s%8lu%c%7lu%8lu%8s"
356 , (u_long) (dbtob(qup->dqblk.dqb_curblocks)
358 , (msgb == NULL) ? ' ' : '*'
359 , (u_long) (dbtob(qup->dqblk.dqb_bsoftlimit)
361 , (u_long) (dbtob(qup->dqblk.dqb_bhardlimit)
363 , (msgb == NULL) ? ""
364 :timeprt(qup->dqblk.dqb_btime));
365 printf("%8lu%c%7lu%8lu%8s\n"
366 , (u_long)qup->dqblk.dqb_curinodes
367 , (msgi == NULL) ? ' ' : '*'
368 , (u_long)qup->dqblk.dqb_isoftlimit
369 , (u_long)qup->dqblk.dqb_ihardlimit
370 , (msgi == NULL) ? ""
371 : timeprt(qup->dqblk.dqb_itime)
376 if (!qflag && lines == 0)
377 heading(type, id, name, "none");
381 heading(int type, u_long id, const char *name, const char *tag)
384 printf("Disk quotas for %s %s (%cid %lu): %s\n", qfextension[type],
385 name, *qfextension[type], id, tag);
386 if (!qflag && tag[0] == '\0') {
387 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n"
402 * Calculate the grace period and return a printable string for it.
405 timeprt(time_t seconds)
407 time_t hours, minutes;
416 minutes = (seconds + 30) / 60;
417 hours = (minutes + 30) / 60;
419 sprintf(buf, "%lddays", ((long)hours + 12) / 24);
423 sprintf(buf, "%2ld:%ld", (long)minutes / 60,
427 sprintf(buf, "%2ld", (long)minutes);
432 * Collect the requested quota information.
434 static struct quotause *
435 getprivs(long id, int quotatype)
437 struct quotause *qup, *quptail = NULL;
439 struct quotause *quphead;
443 qup = quphead = NULL;
445 nfst = getmntinfo(&fst, MNT_NOWAIT);
447 errx(2, "no filesystems mounted!");
449 for (i=0; i<nfst; i++) {
451 if ((qup = (struct quotause *)malloc(sizeof *qup))
453 errx(2, "out of memory");
455 if (strcmp(fst[i].f_fstypename, "nfs") == 0) {
458 if (getnfsquota(&fst[i], qup, id, quotatype)
461 } else if (strcmp(fst[i].f_fstypename, "ufs") == 0) {
464 * UFS filesystems must be in /etc/fstab, and must
465 * indicate that they have quotas on (?!) This is quite
466 * unlike SunOS where quotas can be enabled/disabled
467 * on a filesystem independent of /etc/fstab, and it
468 * will still print quotas for them.
470 if ((fs = getfsspec(fst[i].f_mntfromname)) == NULL)
472 if (getufsquota(fs, qup, id, quotatype) == 0)
476 strcpy(qup->fsname, fst[i].f_mntonname);
482 quptail->next = NULL;
492 * Check to see if a particular quota is to be enabled.
495 ufshasquota(struct fstab *fs, int type, char **qfnamep)
497 static char initname, usrname[100], grpname[100];
498 static char buf[BUFSIZ];
502 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
503 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
506 strcpy(buf, fs->fs_mntops);
507 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
508 if ((cp = strchr(opt, '=')))
510 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
512 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
521 sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
527 getufsquota(struct fstab *fs, struct quotause *qup, long id, int quotatype)
532 qcmd = QCMD(Q_GETQUOTA, quotatype);
533 if (!ufshasquota(fs, quotatype, &qfpathname))
536 if (quotactl(fs->fs_file, qcmd, id, (char *)&qup->dqblk) != 0) {
537 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
538 warn("%s", qfpathname);
541 lseek(fd, (off_t)(id * sizeof(struct ufs_dqblk)), L_SET);
542 switch (read(fd, &qup->dqblk, sizeof(struct ufs_dqblk))) {
545 * Convert implicit 0 quota (EOF)
546 * into an explicit one (zero'ed dqblk)
548 bzero(&qup->dqblk, sizeof(struct ufs_dqblk));
550 case sizeof(struct ufs_dqblk): /* OK */
553 warn("read error: %s", qfpathname);
563 getnfsquota(struct statfs *fst, struct quotause *qup, long id, int quotatype)
565 struct getquota_args gq_args;
566 struct getquota_rslt gq_rslt;
567 struct ufs_dqblk *dqp = &qup->dqblk;
571 if (fst->f_flags & MNT_LOCAL)
575 * rpc.rquotad does not support group quotas
577 if (quotatype != USRQUOTA)
581 * must be some form of "hostname:/path"
583 cp = strchr(fst->f_mntfromname, ':');
585 warnx("cannot find hostname for %s", fst->f_mntfromname);
590 if (*(cp+1) != '/') {
595 /* Avoid attempting the RPC for special amd(8) filesystems. */
596 if (strncmp(fst->f_mntfromname, "pid", 3) == 0 &&
597 strchr(fst->f_mntfromname, '@') != NULL) {
602 gq_args.gqa_pathp = cp + 1;
603 gq_args.gqa_uid = id;
604 if (callaurpc(fst->f_mntfromname, RQUOTAPROG, RQUOTAVERS,
605 RQUOTAPROC_GETQUOTA, (xdrproc_t)xdr_getquota_args, (char *)&gq_args,
606 (xdrproc_t)xdr_getquota_rslt, (char *)&gq_rslt) != 0) {
611 switch (gq_rslt.status) {
615 warnx("quota permission error, host: %s",
619 gettimeofday(&tv, NULL);
621 dqp->dqb_bhardlimit =
622 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit *
623 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE;
624 dqp->dqb_bsoftlimit =
625 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit *
626 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE;
628 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks *
629 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE;
631 dqp->dqb_ihardlimit =
632 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit;
633 dqp->dqb_isoftlimit =
634 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit;
636 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles;
639 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft;
641 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft;
645 warnx("bad rpc result, host: %s", fst->f_mntfromname);
653 callaurpc(char *host, int prognum, int versnum, int procnum,
654 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
656 struct sockaddr_in server_addr;
657 enum clnt_stat clnt_stat;
659 struct timeval timeout, tottimeout;
661 CLIENT *client = NULL;
662 int sock = RPC_ANYSOCK;
664 if ((hp = gethostbyname(host)) == NULL)
665 return ((int) RPC_UNKNOWNHOST);
668 bcopy(hp->h_addr, &server_addr.sin_addr,
669 MIN(hp->h_length,(int)sizeof(server_addr.sin_addr)));
670 server_addr.sin_family = AF_INET;
671 server_addr.sin_port = 0;
673 if ((client = clntudp_create(&server_addr, prognum,
674 versnum, timeout, &sock)) == NULL)
675 return ((int) rpc_createerr.cf_stat);
677 client->cl_auth = authunix_create_default();
678 tottimeout.tv_sec = 25;
679 tottimeout.tv_usec = 0;
680 clnt_stat = clnt_call(client, procnum, inproc, in,
681 outproc, out, tottimeout);
683 return ((int) clnt_stat);
695 } while ((c = *s++));