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";
45 static char sccsid[] = "from: @(#)quota.c 8.1 (Berkeley) 6/6/93";
47 static const char rcsid[] =
52 * Disk quota reporting program.
54 #include <sys/param.h>
55 #include <sys/types.h>
58 #include <sys/mount.h>
59 #include <sys/socket.h>
60 #include <ufs/ufs/quota.h>
73 #include <rpc/pmap_prot.h>
74 #include <rpcsvc/rquota.h>
76 char *qfname = QUOTAFILENAME;
77 char *qfextension[] = INITQFNAMES;
80 struct quotause *next;
83 char fsname[MAXPATHLEN + 1];
87 static char *timeprt __P((time_t seconds));
88 static struct quotause *getprivs __P((long id, int quotatype));
90 static void showuid(u_long uid);
91 static void showgid(u_long gid);
92 static int alldigits(char *s);
93 static void showusrname(char *name);
94 static void showgrpname(char *name);
95 static void showquotas(int type, u_long id, char *name);
96 static void heading(int type, u_long id, char *name, char *tag);
97 static char *timeprt(time_t seconds);
98 static struct quotause *getprivs(long id, int quotatype);
99 static int ufshasquota(struct fstab *fs, int type, char **qfnamep);
100 static int getufsquota(struct statfs *fst, struct fstab *fs,
101 struct quotause *qup, long id, int quotatype);
102 static int getnfsquota(struct statfs *fst, struct fstab *fs,
103 struct quotause *qup, long id, int quotatype);
104 static int callaurpc(char *host, int prognum, int versnum, int procnum,
105 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out);
106 static int alldigits(char *s);
117 gid_t mygid, gidset[NGROUPS];
118 int i, gflag = 0, uflag = 0;
121 while ((ch = getopt(argc, argv, "ugvq")) != -1) {
141 if (!uflag && !gflag)
148 ngroups = getgroups(NGROUPS, gidset);
152 for (i = 0; i < ngroups; i++)
153 if (gidset[i] != mygid)
161 for (; argc > 0; argc--, argv++) {
162 if (alldigits(*argv))
163 showuid(atoi(*argv));
170 for (; argc > 0; argc--, argv++) {
171 if (alldigits(*argv))
172 showgid(atoi(*argv));
184 fprintf(stderr, "%s\n%s\n%s\n",
185 "usage: quota [-guqv]",
186 " quota [-qv] -u username ...",
187 " quota [-qv] -g groupname ...");
192 * Print out quotas for a specified user identifier.
198 struct passwd *pwd = getpwuid(uid);
203 name = "(no account)";
207 if (uid != myuid && myuid != 0) {
208 printf("quota: %s (uid %lu): permission denied\n", name, uid);
211 showquotas(USRQUOTA, uid, name);
215 * Print out quotas for a specifed user name.
221 struct passwd *pwd = getpwnam(name);
225 warnx("%s: unknown user", name);
229 if (pwd->pw_uid != myuid && myuid != 0) {
230 warnx("%s (uid %u): permission denied", name, pwd->pw_uid);
233 showquotas(USRQUOTA, pwd->pw_uid, name);
237 * Print out quotas for a specified group identifier.
243 struct group *grp = getgrgid(gid);
245 gid_t mygid, gidset[NGROUPS];
254 ngroups = getgroups(NGROUPS, gidset);
260 for (i = 0; i < ngroups; i++)
261 if (gid == gidset[i])
263 if (i >= ngroups && getuid() != 0) {
264 warnx("%s (gid %lu): permission denied", name, gid);
268 showquotas(GRPQUOTA, gid, name);
272 * Print out quotas for a specifed group name.
278 struct group *grp = getgrnam(name);
280 gid_t mygid, gidset[NGROUPS];
284 warnx("%s: unknown group", name);
288 ngroups = getgroups(NGROUPS, gidset);
293 if (grp->gr_gid != mygid) {
294 for (i = 0; i < ngroups; i++)
295 if (grp->gr_gid == gidset[i])
297 if (i >= ngroups && getuid() != 0) {
298 warnx("%s (gid %u): permission denied", name,
303 showquotas(GRPQUOTA, grp->gr_gid, name);
307 showquotas(type, id, name)
312 register struct quotause *qup;
313 struct quotause *quplist;
314 char *msgi, *msgb, *nam;
320 quplist = getprivs(id, type);
321 for (qup = quplist; qup; qup = qup->next) {
323 qup->dqblk.dqb_isoftlimit == 0 &&
324 qup->dqblk.dqb_ihardlimit == 0 &&
325 qup->dqblk.dqb_bsoftlimit == 0 &&
326 qup->dqblk.dqb_bhardlimit == 0)
329 if (qup->dqblk.dqb_ihardlimit &&
330 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_ihardlimit)
331 msgi = "File limit reached on";
332 else if (qup->dqblk.dqb_isoftlimit &&
333 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_isoftlimit)
334 if (qup->dqblk.dqb_itime > now)
335 msgi = "In file grace period on";
337 msgi = "Over file quota on";
339 if (qup->dqblk.dqb_bhardlimit &&
340 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bhardlimit)
341 msgb = "Block limit reached on";
342 else if (qup->dqblk.dqb_bsoftlimit &&
343 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit)
344 if (qup->dqblk.dqb_btime > now)
345 msgb = "In block grace period on";
347 msgb = "Over block quota on";
349 if ((msgi != (char *)0 || msgb != (char *)0) &&
351 heading(type, id, name, "");
352 if (msgi != (char *)0)
353 printf("\t%s %s\n", msgi, qup->fsname);
354 if (msgb != (char *)0)
355 printf("\t%s %s\n", msgb, qup->fsname);
359 qup->dqblk.dqb_curblocks ||
360 qup->dqblk.dqb_curinodes) {
362 heading(type, id, name, "");
364 if (strlen(qup->fsname) > 15) {
365 printf("%s\n", qup->fsname);
368 printf("%15s%8lu%c%7lu%8lu%8s"
370 , (u_long) (dbtob(qup->dqblk.dqb_curblocks)
372 , (msgb == (char *)0) ? ' ' : '*'
373 , (u_long) (dbtob(qup->dqblk.dqb_bsoftlimit)
375 , (u_long) (dbtob(qup->dqblk.dqb_bhardlimit)
377 , (msgb == (char *)0) ? ""
378 :timeprt(qup->dqblk.dqb_btime));
379 printf("%8lu%c%7lu%8lu%8s\n"
380 , qup->dqblk.dqb_curinodes
381 , (msgi == (char *)0) ? ' ' : '*'
382 , qup->dqblk.dqb_isoftlimit
383 , qup->dqblk.dqb_ihardlimit
384 , (msgi == (char *)0) ? ""
385 : timeprt(qup->dqblk.dqb_itime)
390 if (!qflag && lines == 0)
391 heading(type, id, name, "none");
395 heading(type, id, name, tag)
401 printf("Disk quotas for %s %s (%cid %lu): %s\n", qfextension[type],
402 name, *qfextension[type], id, tag);
403 if (!qflag && tag[0] == '\0') {
404 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n"
419 * Calculate the grace period and return a printable string for it.
425 time_t hours, minutes;
434 minutes = (seconds + 30) / 60;
435 hours = (minutes + 30) / 60;
437 sprintf(buf, "%lddays", (hours + 12) / 24);
441 sprintf(buf, "%2ld:%ld", minutes / 60, minutes % 60);
444 sprintf(buf, "%2ld", minutes);
449 * Collect the requested quota information.
451 static struct quotause *
452 getprivs(id, quotatype)
456 register struct quotause *qup, *quptail;
457 register struct fstab *fs;
458 struct quotause *quphead;
462 qup = quphead = (struct quotause *)0;
464 nfst = getmntinfo(&fst, MNT_WAIT);
466 errx(2, "no filesystems mounted!");
468 for (i=0; i<nfst; i++) {
470 if ((qup = (struct quotause *)malloc(sizeof *qup))
472 errx(2, "out of memory");
474 if (strcmp(fst[i].f_fstypename, "nfs") == 0) {
475 if (getnfsquota(&fst[i], NULL, qup, id, quotatype)
478 } else if (strcmp(fst[i].f_fstypename, "ufs") == 0) {
481 * UFS filesystems must be in /etc/fstab, and must
482 * indicate that they have quotas on (?!) This is quite
483 * unlike SunOS where quotas can be enabled/disabled
484 * on a filesystem independent of /etc/fstab, and it
485 * will still print quotas for them.
487 if ((fs = getfsspec(fst[i].f_mntfromname)) == NULL)
489 if (getufsquota(&fst[i], fs, qup, id, quotatype) == 0)
493 strcpy(qup->fsname, fst[i].f_mntonname);
509 * Check to see if a particular quota is to be enabled.
512 ufshasquota(fs, type, qfnamep)
513 register struct fstab *fs;
517 static char initname, usrname[100], grpname[100];
518 static char buf[BUFSIZ];
522 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
523 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
526 strcpy(buf, fs->fs_mntops);
527 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
528 if ((cp = index(opt, '=')))
530 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
532 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
541 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
547 getufsquota(fst, fs, qup, id, quotatype)
550 struct quotause *qup;
557 qcmd = QCMD(Q_GETQUOTA, quotatype);
558 if (!ufshasquota(fs, quotatype, &qfpathname))
561 if (quotactl(fs->fs_file, qcmd, id, (char *)&qup->dqblk) != 0) {
562 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
563 warn("%s", qfpathname);
566 (void) lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET);
567 switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
570 * Convert implicit 0 quota (EOF)
571 * into an explicit one (zero'ed dqblk)
573 bzero((caddr_t)&qup->dqblk, sizeof(struct dqblk));
575 case sizeof(struct dqblk): /* OK */
578 warn("read error: %s", qfpathname);
588 getnfsquota(fst, fs, qup, id, quotatype)
591 struct quotause *qup;
595 struct getquota_args gq_args;
596 struct getquota_rslt gq_rslt;
597 struct dqblk *dqp = &qup->dqblk;
601 if (fst->f_flags & MNT_LOCAL)
605 * rpc.rquotad does not support group quotas
607 if (quotatype != USRQUOTA)
611 * must be some form of "hostname:/path"
613 cp = strchr(fst->f_mntfromname, ':');
615 warnx("cannot find hostname for %s", fst->f_mntfromname);
620 if (*(cp+1) != '/') {
625 gq_args.gqa_pathp = cp + 1;
626 gq_args.gqa_uid = id;
627 if (callaurpc(fst->f_mntfromname, RQUOTAPROG, RQUOTAVERS,
628 RQUOTAPROC_GETQUOTA, xdr_getquota_args, (char *)&gq_args,
629 xdr_getquota_rslt, (char *)&gq_rslt) != 0) {
634 switch (gq_rslt.status) {
638 warnx("quota permission error, host: %s",
642 gettimeofday(&tv, NULL);
644 dqp->dqb_bhardlimit =
645 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit *
646 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE;
647 dqp->dqb_bsoftlimit =
648 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit *
649 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE;
651 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks *
652 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE;
654 dqp->dqb_ihardlimit =
655 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit;
656 dqp->dqb_isoftlimit =
657 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit;
659 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles;
662 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft;
664 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft;
668 warnx("bad rpc result, host: %s", fst->f_mntfromname);
676 callaurpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
678 xdrproc_t inproc, outproc;
680 int prognum, versnum, procnum;
682 struct sockaddr_in server_addr;
683 enum clnt_stat clnt_stat;
685 struct timeval timeout, tottimeout;
687 CLIENT *client = NULL;
688 int socket = RPC_ANYSOCK;
690 if ((hp = gethostbyname(host)) == NULL)
691 return ((int) RPC_UNKNOWNHOST);
694 bcopy(hp->h_addr, &server_addr.sin_addr,
695 MIN(hp->h_length,sizeof(server_addr.sin_addr)));
696 server_addr.sin_family = AF_INET;
697 server_addr.sin_port = 0;
699 if ((client = clntudp_create(&server_addr, prognum,
700 versnum, timeout, &socket)) == NULL)
701 return ((int) rpc_createerr.cf_stat);
703 client->cl_auth = authunix_create_default();
704 tottimeout.tv_sec = 25;
705 tottimeout.tv_usec = 0;
706 clnt_stat = clnt_call(client, procnum, inproc, in,
707 outproc, out, tottimeout);
709 return ((int) clnt_stat);
722 } while ((c = *s++));