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>
77 const char *qfname = QUOTAFILENAME;
78 const char *qfextension[] = INITQFNAMES;
81 struct quotause *next;
84 char fsname[MAXPATHLEN + 1];
87 static const char *timeprt(time_t seconds);
88 static struct quotause *getprivs(long id, int quotatype);
89 static void usage(void);
90 static void showuid(u_long uid);
91 static void showgid(u_long gid);
92 static void showusrname(char *name);
93 static void showgrpname(char *name);
94 static void showquotas(int type, u_long id, const char *name);
95 static void heading(int type, u_long id, const char *name, const char *tag);
96 static int ufshasquota(struct fstab *fs, int type, char **qfnamep);
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 int 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 main(int argc, char *argv[])
113 gid_t mygid, gidset[NGROUPS];
114 int i, ch, gflag = 0, uflag = 0;
116 while ((ch = getopt(argc, argv, "glquv")) != -1) {
139 if (!uflag && !gflag)
146 ngroups = getgroups(NGROUPS, gidset);
150 for (i = 0; i < ngroups; i++)
151 if (gidset[i] != mygid)
159 for (; argc > 0; argc--, argv++) {
160 if (alldigits(*argv))
161 showuid(atoi(*argv));
168 for (; argc > 0; argc--, argv++) {
169 if (alldigits(*argv))
170 showgid(atoi(*argv));
182 fprintf(stderr, "%s\n%s\n%s\n",
183 "usage: quota [-glu] [-v | -q]",
184 " quota [-lu] [-v | -q] user ...",
185 " quota -g [-l] [-v | -q] group ...");
190 * Print out quotas for a specified user identifier.
195 struct passwd *pwd = getpwuid(uid);
199 name = "(no account)";
202 showquotas(USRQUOTA, uid, name);
206 * Print out quotas for a specifed user name.
209 showusrname(char *name)
211 struct passwd *pwd = getpwnam(name);
214 warnx("%s: unknown user", name);
217 showquotas(USRQUOTA, pwd->pw_uid, name);
221 * Print out quotas for a specified group identifier.
226 struct group *grp = getgrgid(gid);
233 showquotas(GRPQUOTA, gid, name);
237 * Print out quotas for a specifed group name.
240 showgrpname(char *name)
242 struct group *grp = getgrnam(name);
245 warnx("%s: unknown group", name);
248 showquotas(GRPQUOTA, grp->gr_gid, name);
252 showquotas(int type, u_long id, const char *name)
254 struct quotause *qup;
255 struct quotause *quplist;
256 const char *msgi, *msgb;
263 quplist = getprivs(id, type);
264 for (qup = quplist; qup; qup = qup->next) {
266 qup->dqblk.dqb_isoftlimit == 0 &&
267 qup->dqblk.dqb_ihardlimit == 0 &&
268 qup->dqblk.dqb_bsoftlimit == 0 &&
269 qup->dqblk.dqb_bhardlimit == 0)
272 if (qup->dqblk.dqb_ihardlimit &&
273 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_ihardlimit)
274 msgi = "File limit reached on";
275 else if (qup->dqblk.dqb_isoftlimit &&
276 qup->dqblk.dqb_curinodes >= qup->dqblk.dqb_isoftlimit) {
277 if (qup->dqblk.dqb_itime > now)
278 msgi = "In file grace period on";
280 msgi = "Over file quota on";
283 if (qup->dqblk.dqb_bhardlimit &&
284 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bhardlimit)
285 msgb = "Block limit reached on";
286 else if (qup->dqblk.dqb_bsoftlimit &&
287 qup->dqblk.dqb_curblocks >= qup->dqblk.dqb_bsoftlimit) {
288 if (qup->dqblk.dqb_btime > now)
289 msgb = "In block grace period on";
291 msgb = "Over block quota on";
294 if ((msgi != (char *)0 || msgb != (char *)0) &&
296 heading(type, id, name, "");
297 if (msgi != (char *)0)
298 printf("\t%s %s\n", msgi, qup->fsname);
299 if (msgb != (char *)0)
300 printf("\t%s %s\n", msgb, qup->fsname);
304 qup->dqblk.dqb_curblocks ||
305 qup->dqblk.dqb_curinodes) {
307 heading(type, id, name, "");
309 if (strlen(qup->fsname) > 15) {
310 printf("%s\n", qup->fsname);
313 printf("%15s%8lu%c%7lu%8lu%8s"
315 , (u_long) (dbtob(qup->dqblk.dqb_curblocks)
317 , (msgb == (char *)0) ? ' ' : '*'
318 , (u_long) (dbtob(qup->dqblk.dqb_bsoftlimit)
320 , (u_long) (dbtob(qup->dqblk.dqb_bhardlimit)
322 , (msgb == (char *)0) ? ""
323 :timeprt(qup->dqblk.dqb_btime));
324 printf("%8lu%c%7lu%8lu%8s\n"
325 , (u_long)qup->dqblk.dqb_curinodes
326 , (msgi == (char *)0) ? ' ' : '*'
327 , (u_long)qup->dqblk.dqb_isoftlimit
328 , (u_long)qup->dqblk.dqb_ihardlimit
329 , (msgi == (char *)0) ? ""
330 : timeprt(qup->dqblk.dqb_itime)
335 if (!qflag && lines == 0)
336 heading(type, id, name, "none");
340 heading(int type, u_long id, const char *name, const char *tag)
343 printf("Disk quotas for %s %s (%cid %lu): %s\n", qfextension[type],
344 name, *qfextension[type], id, tag);
345 if (!qflag && tag[0] == '\0') {
346 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n"
361 * Calculate the grace period and return a printable string for it.
364 timeprt(time_t seconds)
366 time_t hours, minutes;
375 minutes = (seconds + 30) / 60;
376 hours = (minutes + 30) / 60;
378 sprintf(buf, "%lddays", ((long)hours + 12) / 24);
382 sprintf(buf, "%2ld:%ld", (long)minutes / 60,
386 sprintf(buf, "%2ld", (long)minutes);
391 * Collect the requested quota information.
393 static struct quotause *
394 getprivs(long id, int quotatype)
396 struct quotause *qup, *quptail = NULL;
398 struct quotause *quphead;
402 qup = quphead = (struct quotause *)0;
404 nfst = getmntinfo(&fst, MNT_NOWAIT);
406 errx(2, "no filesystems mounted!");
408 for (i=0; i<nfst; i++) {
410 if ((qup = (struct quotause *)malloc(sizeof *qup))
412 errx(2, "out of memory");
414 if (strcmp(fst[i].f_fstypename, "nfs") == 0) {
417 if (getnfsquota(&fst[i], qup, id, quotatype)
420 } else if (strcmp(fst[i].f_fstypename, "ufs") == 0) {
423 * UFS filesystems must be in /etc/fstab, and must
424 * indicate that they have quotas on (?!) This is quite
425 * unlike SunOS where quotas can be enabled/disabled
426 * on a filesystem independent of /etc/fstab, and it
427 * will still print quotas for them.
429 if ((fs = getfsspec(fst[i].f_mntfromname)) == NULL)
431 if (getufsquota(fs, qup, id, quotatype) == 0)
435 strcpy(qup->fsname, fst[i].f_mntonname);
451 * Check to see if a particular quota is to be enabled.
454 ufshasquota(struct fstab *fs, int type, char **qfnamep)
456 static char initname, usrname[100], grpname[100];
457 static char buf[BUFSIZ];
461 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
462 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
465 strcpy(buf, fs->fs_mntops);
466 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
467 if ((cp = index(opt, '=')))
469 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
471 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
480 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
486 getufsquota(struct fstab *fs, struct quotause *qup, long id, int quotatype)
491 qcmd = QCMD(Q_GETQUOTA, quotatype);
492 if (!ufshasquota(fs, quotatype, &qfpathname))
495 if (quotactl(fs->fs_file, qcmd, id, (char *)&qup->dqblk) != 0) {
496 if ((fd = open(qfpathname, O_RDONLY)) < 0) {
497 warn("%s", qfpathname);
500 (void) lseek(fd, (off_t)(id * sizeof(struct dqblk)), L_SET);
501 switch (read(fd, &qup->dqblk, sizeof(struct dqblk))) {
504 * Convert implicit 0 quota (EOF)
505 * into an explicit one (zero'ed dqblk)
507 bzero((caddr_t)&qup->dqblk, sizeof(struct dqblk));
509 case sizeof(struct dqblk): /* OK */
512 warn("read error: %s", qfpathname);
522 getnfsquota(struct statfs *fst, struct quotause *qup, long id, int quotatype)
524 struct getquota_args gq_args;
525 struct getquota_rslt gq_rslt;
526 struct dqblk *dqp = &qup->dqblk;
530 if (fst->f_flags & MNT_LOCAL)
534 * rpc.rquotad does not support group quotas
536 if (quotatype != USRQUOTA)
540 * must be some form of "hostname:/path"
542 cp = strchr(fst->f_mntfromname, ':');
544 warnx("cannot find hostname for %s", fst->f_mntfromname);
549 if (*(cp+1) != '/') {
554 /* Avoid attempting the RPC for special amd(8) filesystems. */
555 if (strncmp(fst->f_mntfromname, "pid", 3) == 0 &&
556 strchr(fst->f_mntfromname, '@') != NULL) {
561 gq_args.gqa_pathp = cp + 1;
562 gq_args.gqa_uid = id;
563 if (callaurpc(fst->f_mntfromname, RQUOTAPROG, RQUOTAVERS,
564 RQUOTAPROC_GETQUOTA, (xdrproc_t)xdr_getquota_args, (char *)&gq_args,
565 (xdrproc_t)xdr_getquota_rslt, (char *)&gq_rslt) != 0) {
570 switch (gq_rslt.status) {
574 warnx("quota permission error, host: %s",
578 gettimeofday(&tv, NULL);
580 dqp->dqb_bhardlimit =
581 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bhardlimit *
582 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
583 dqp->dqb_bsoftlimit =
584 gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsoftlimit *
585 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
587 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curblocks *
588 (gq_rslt.getquota_rslt_u.gqr_rquota.rq_bsize / DEV_BSIZE);
590 dqp->dqb_ihardlimit =
591 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fhardlimit;
592 dqp->dqb_isoftlimit =
593 gq_rslt.getquota_rslt_u.gqr_rquota.rq_fsoftlimit;
595 gq_rslt.getquota_rslt_u.gqr_rquota.rq_curfiles;
598 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_btimeleft;
600 tv.tv_sec + gq_rslt.getquota_rslt_u.gqr_rquota.rq_ftimeleft;
604 warnx("bad rpc result, host: %s", fst->f_mntfromname);
612 callaurpc(char *host, int prognum, int versnum, int procnum,
613 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
615 struct sockaddr_in server_addr;
616 enum clnt_stat clnt_stat;
618 struct timeval timeout, tottimeout;
620 CLIENT *client = NULL;
621 int sock = RPC_ANYSOCK;
623 if ((hp = gethostbyname(host)) == NULL)
624 return ((int) RPC_UNKNOWNHOST);
627 bcopy(hp->h_addr, &server_addr.sin_addr,
628 MIN(hp->h_length,(int)sizeof(server_addr.sin_addr)));
629 server_addr.sin_family = AF_INET;
630 server_addr.sin_port = 0;
632 if ((client = clntudp_create(&server_addr, prognum,
633 versnum, timeout, &sock)) == NULL)
634 return ((int) rpc_createerr.cf_stat);
636 client->cl_auth = authunix_create_default();
637 tottimeout.tv_sec = 25;
638 tottimeout.tv_usec = 0;
639 clnt_stat = clnt_call(client, procnum, inproc, in,
640 outproc, out, tottimeout);
642 return ((int) clnt_stat);
654 } while ((c = *s++));