Home | History | Annotate | Line # | Download | only in quota
quota.c revision 1.44
      1  1.44  dholland /*	$NetBSD: quota.c,v 1.44 2012/01/09 15:35:44 dholland Exp $	*/
      2  1.12       tls 
      3   1.1       cgd /*
      4   1.5   mycroft  * Copyright (c) 1980, 1990, 1993
      5   1.5   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Robert Elz at The University of Melbourne.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.26       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.16     lukem #include <sys/cdefs.h>
     36   1.1       cgd #ifndef lint
     37  1.32     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
     38  1.32     lukem  The Regents of the University of California.  All rights reserved.");
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #ifndef lint
     42  1.15       mrg #if 0
     43  1.15       mrg static char sccsid[] = "@(#)quota.c	8.4 (Berkeley) 4/28/95";
     44  1.15       mrg #else
     45  1.44  dholland __RCSID("$NetBSD: quota.c,v 1.44 2012/01/09 15:35:44 dholland Exp $");
     46  1.15       mrg #endif
     47   1.1       cgd #endif /* not lint */
     48   1.1       cgd 
     49   1.1       cgd /*
     50   1.1       cgd  * Disk quota reporting program.
     51   1.1       cgd  */
     52   1.1       cgd #include <sys/param.h>
     53   1.6   deraadt #include <sys/types.h>
     54   1.1       cgd #include <sys/file.h>
     55   1.1       cgd #include <sys/stat.h>
     56   1.6   deraadt #include <sys/mount.h>
     57   1.6   deraadt #include <sys/socket.h>
     58  1.15       mrg 
     59  1.42  dholland #include <assert.h>
     60  1.15       mrg #include <ctype.h>
     61  1.16     lukem #include <err.h>
     62  1.15       mrg #include <errno.h>
     63  1.15       mrg #include <fstab.h>
     64  1.15       mrg #include <grp.h>
     65  1.15       mrg #include <netdb.h>
     66  1.15       mrg #include <pwd.h>
     67   1.1       cgd #include <stdio.h>
     68   1.7       cgd #include <stdlib.h>
     69  1.16     lukem #include <string.h>
     70  1.18    kleink #include <time.h>
     71  1.16     lukem #include <unistd.h>
     72   1.1       cgd 
     73  1.44  dholland #include <quota.h>
     74  1.37    bouyer #include <quota/quota.h>
     75   1.6   deraadt 
     76  1.36  christos #include "printquota.h"
     77  1.36  christos #include "getvfsquota.h"
     78   1.1       cgd 
     79   1.1       cgd struct quotause {
     80   1.1       cgd 	struct	quotause *next;
     81  1.37    bouyer 	uid_t	id;
     82  1.43  dholland 	struct	quotaval *qvs;
     83  1.43  dholland 	unsigned numqvs;
     84   1.1       cgd 	char	fsname[MAXPATHLEN + 1];
     85  1.44  dholland 	struct	quotahandle *qh;
     86   1.6   deraadt };
     87   1.1       cgd 
     88  1.42  dholland static int	anyusage(struct quotaval *, unsigned);
     89  1.42  dholland static int	anyover(struct quotaval *, unsigned, time_t);
     90  1.42  dholland static const char *getovermsg(struct quotaval *, const char *, time_t);
     91  1.39  dholland static struct quotause	*getprivs(id_t, int);
     92  1.44  dholland static void	heading(int, const char *, id_t, const char *, const char *);
     93  1.42  dholland static int	isover(struct quotaval *qv, time_t now);
     94  1.44  dholland static void	printqv(struct quotaval *, int, time_t);
     95  1.35  christos static void	showgid(gid_t);
     96  1.35  christos static void	showgrpname(const char *);
     97  1.44  dholland static void	showonequota(int, const char *, id_t, const char *,
     98  1.44  dholland 			     struct quotause *);
     99  1.44  dholland static void	showquotas(int, const char *, id_t, const char *);
    100  1.35  christos static void	showuid(uid_t);
    101  1.35  christos static void	showusrname(const char *);
    102  1.42  dholland static int	unlimited(struct quotaval *qvs, unsigned numqvs);
    103  1.40  dholland static void	usage(void) __dead;
    104  1.35  christos 
    105  1.35  christos static int	qflag = 0;
    106  1.35  christos static int	vflag = 0;
    107  1.35  christos static int	hflag = 0;
    108  1.35  christos static int	dflag = 0;
    109  1.35  christos static int	Dflag = 0;
    110  1.35  christos static uid_t	myuid;
    111  1.41  dholland static int needheading;
    112   1.1       cgd 
    113  1.16     lukem int
    114  1.35  christos main(int argc, char *argv[])
    115   1.1       cgd {
    116   1.3       jtc 	int ngroups;
    117   1.8   mycroft 	gid_t mygid, gidset[NGROUPS];
    118   1.1       cgd 	int i, gflag = 0, uflag = 0;
    119  1.11      mark 	int ch;
    120   1.1       cgd 
    121  1.19       mrg 	myuid = getuid();
    122  1.34    bouyer 	while ((ch = getopt(argc, argv, "Ddhugvq")) != -1) {
    123   1.1       cgd 		switch(ch) {
    124   1.1       cgd 		case 'g':
    125   1.1       cgd 			gflag++;
    126   1.1       cgd 			break;
    127   1.1       cgd 		case 'u':
    128   1.1       cgd 			uflag++;
    129   1.1       cgd 			break;
    130   1.1       cgd 		case 'v':
    131   1.1       cgd 			vflag++;
    132   1.1       cgd 			break;
    133   1.1       cgd 		case 'q':
    134   1.1       cgd 			qflag++;
    135   1.1       cgd 			break;
    136  1.34    bouyer 		case 'h':
    137  1.34    bouyer 			hflag++;
    138  1.34    bouyer 			break;
    139  1.34    bouyer 		case 'd':
    140  1.34    bouyer 			dflag++;
    141  1.34    bouyer 			break;
    142  1.34    bouyer 		case 'D':
    143  1.34    bouyer 			Dflag++;
    144  1.34    bouyer 			break;
    145   1.1       cgd 		default:
    146   1.1       cgd 			usage();
    147   1.1       cgd 		}
    148   1.1       cgd 	}
    149   1.1       cgd 	argc -= optind;
    150   1.1       cgd 	argv += optind;
    151   1.1       cgd 	if (!uflag && !gflag)
    152   1.1       cgd 		uflag++;
    153  1.34    bouyer 	if (dflag) {
    154  1.34    bouyer #if 0
    155  1.35  christos 		if (myuid != 0)
    156  1.35  christos 			errx(1, "-d: permission denied");
    157  1.34    bouyer #endif
    158  1.34    bouyer 		if (uflag)
    159  1.44  dholland 			showquotas(QUOTA_IDTYPE_USER, "user", 0, "");
    160  1.34    bouyer 		if (gflag)
    161  1.44  dholland 			showquotas(QUOTA_IDTYPE_GROUP, "group", 0, "");
    162  1.35  christos 		return 0;
    163  1.34    bouyer 	}
    164   1.1       cgd 	if (argc == 0) {
    165   1.1       cgd 		if (uflag)
    166  1.19       mrg 			showuid(myuid);
    167   1.1       cgd 		if (gflag) {
    168  1.34    bouyer 			if (dflag)
    169  1.34    bouyer 				showgid(0);
    170  1.34    bouyer 			else {
    171  1.34    bouyer 				mygid = getgid();
    172  1.34    bouyer 				ngroups = getgroups(NGROUPS, gidset);
    173  1.34    bouyer 				if (ngroups < 0)
    174  1.34    bouyer 					err(1, "getgroups");
    175  1.34    bouyer 				showgid(mygid);
    176  1.34    bouyer 				for (i = 0; i < ngroups; i++)
    177  1.34    bouyer 					if (gidset[i] != mygid)
    178  1.34    bouyer 						showgid(gidset[i]);
    179  1.34    bouyer 			}
    180   1.1       cgd 		}
    181  1.35  christos 		return 0;
    182   1.1       cgd 	}
    183   1.1       cgd 	if (uflag && gflag)
    184   1.1       cgd 		usage();
    185   1.1       cgd 	if (uflag) {
    186   1.1       cgd 		for (; argc > 0; argc--, argv++) {
    187   1.1       cgd 			if (alldigits(*argv))
    188  1.35  christos 				showuid((uid_t)atoi(*argv));
    189   1.1       cgd 			else
    190   1.1       cgd 				showusrname(*argv);
    191   1.1       cgd 		}
    192  1.35  christos 		return 0;
    193   1.1       cgd 	}
    194   1.1       cgd 	if (gflag) {
    195   1.1       cgd 		for (; argc > 0; argc--, argv++) {
    196   1.1       cgd 			if (alldigits(*argv))
    197  1.35  christos 				showgid((gid_t)atoi(*argv));
    198   1.1       cgd 			else
    199   1.1       cgd 				showgrpname(*argv);
    200   1.1       cgd 		}
    201  1.35  christos 		return 0;
    202   1.1       cgd 	}
    203  1.16     lukem 	/* NOTREACHED */
    204  1.35  christos 	return 0;
    205   1.1       cgd }
    206   1.1       cgd 
    207  1.35  christos static void
    208  1.35  christos usage(void)
    209   1.1       cgd {
    210  1.35  christos 	const char *p = getprogname();
    211  1.35  christos 	fprintf(stderr, "Usage: %s [-Dhguqv]\n"
    212  1.35  christos 	    "\t%s [-Dhqv] -u username ...\n"
    213  1.35  christos 	    "\t%s [-Dhqv] -g groupname ...\n"
    214  1.35  christos 	    "\t%s -d [-Dhguqv]\n", p, p, p, p);
    215   1.1       cgd 	exit(1);
    216   1.1       cgd }
    217   1.1       cgd 
    218   1.1       cgd /*
    219   1.1       cgd  * Print out quotas for a specified user identifier.
    220   1.1       cgd  */
    221  1.35  christos static void
    222  1.35  christos showuid(uid_t uid)
    223   1.1       cgd {
    224   1.1       cgd 	struct passwd *pwd = getpwuid(uid);
    225  1.20   mycroft 	const char *name;
    226   1.1       cgd 
    227   1.1       cgd 	if (pwd == NULL)
    228   1.1       cgd 		name = "(no account)";
    229   1.1       cgd 	else
    230   1.1       cgd 		name = pwd->pw_name;
    231   1.1       cgd 	if (uid != myuid && myuid != 0) {
    232  1.35  christos 		warnx("%s (uid %d): permission denied", name, uid);
    233   1.1       cgd 		return;
    234   1.1       cgd 	}
    235  1.44  dholland 	showquotas(QUOTA_IDTYPE_USER, "user", uid, name);
    236   1.1       cgd }
    237   1.1       cgd 
    238   1.1       cgd /*
    239  1.24       wiz  * Print out quotas for a specified user name.
    240   1.1       cgd  */
    241  1.35  christos static void
    242  1.35  christos showusrname(const char *name)
    243   1.1       cgd {
    244   1.1       cgd 	struct passwd *pwd = getpwnam(name);
    245   1.1       cgd 
    246   1.1       cgd 	if (pwd == NULL) {
    247  1.16     lukem 		warnx("%s: unknown user", name);
    248   1.1       cgd 		return;
    249   1.1       cgd 	}
    250   1.1       cgd 	if (pwd->pw_uid != myuid && myuid != 0) {
    251  1.16     lukem 		warnx("%s (uid %d): permission denied", name, pwd->pw_uid);
    252   1.1       cgd 		return;
    253   1.1       cgd 	}
    254  1.44  dholland 	showquotas(QUOTA_IDTYPE_USER, "user", pwd->pw_uid, name);
    255   1.1       cgd }
    256   1.1       cgd 
    257   1.1       cgd /*
    258   1.1       cgd  * Print out quotas for a specified group identifier.
    259   1.1       cgd  */
    260  1.35  christos static void
    261  1.35  christos showgid(gid_t gid)
    262   1.1       cgd {
    263   1.1       cgd 	struct group *grp = getgrgid(gid);
    264   1.3       jtc 	int ngroups;
    265   1.8   mycroft 	gid_t mygid, gidset[NGROUPS];
    266  1.16     lukem 	int i;
    267  1.20   mycroft 	const char *name;
    268   1.1       cgd 
    269   1.1       cgd 	if (grp == NULL)
    270   1.1       cgd 		name = "(no entry)";
    271   1.1       cgd 	else
    272   1.1       cgd 		name = grp->gr_name;
    273   1.8   mycroft 	mygid = getgid();
    274   1.1       cgd 	ngroups = getgroups(NGROUPS, gidset);
    275   1.1       cgd 	if (ngroups < 0) {
    276  1.16     lukem 		warn("getgroups");
    277   1.1       cgd 		return;
    278   1.1       cgd 	}
    279   1.8   mycroft 	if (gid != mygid) {
    280   1.8   mycroft 		for (i = 0; i < ngroups; i++)
    281   1.8   mycroft 			if (gid == gidset[i])
    282   1.8   mycroft 				break;
    283  1.19       mrg 		if (i >= ngroups && myuid != 0) {
    284  1.16     lukem 			warnx("%s (gid %d): permission denied", name, gid);
    285   1.8   mycroft 			return;
    286   1.8   mycroft 		}
    287   1.1       cgd 	}
    288  1.44  dholland 	showquotas(QUOTA_IDTYPE_GROUP, "group", gid, name);
    289   1.1       cgd }
    290   1.1       cgd 
    291   1.1       cgd /*
    292  1.24       wiz  * Print out quotas for a specified group name.
    293   1.1       cgd  */
    294  1.35  christos static void
    295  1.35  christos showgrpname(const char *name)
    296   1.1       cgd {
    297   1.1       cgd 	struct group *grp = getgrnam(name);
    298   1.3       jtc 	int ngroups;
    299   1.8   mycroft 	gid_t mygid, gidset[NGROUPS];
    300  1.16     lukem 	int i;
    301   1.1       cgd 
    302   1.1       cgd 	if (grp == NULL) {
    303  1.16     lukem 		warnx("%s: unknown group", name);
    304   1.1       cgd 		return;
    305   1.1       cgd 	}
    306   1.8   mycroft 	mygid = getgid();
    307   1.1       cgd 	ngroups = getgroups(NGROUPS, gidset);
    308   1.1       cgd 	if (ngroups < 0) {
    309  1.16     lukem 		warn("getgroups");
    310   1.1       cgd 		return;
    311   1.1       cgd 	}
    312   1.8   mycroft 	if (grp->gr_gid != mygid) {
    313   1.8   mycroft 		for (i = 0; i < ngroups; i++)
    314   1.8   mycroft 			if (grp->gr_gid == gidset[i])
    315   1.8   mycroft 				break;
    316  1.19       mrg 		if (i >= ngroups && myuid != 0) {
    317  1.16     lukem 			warnx("%s (gid %d): permission denied",
    318   1.8   mycroft 			    name, grp->gr_gid);
    319   1.8   mycroft 			return;
    320   1.8   mycroft 		}
    321   1.1       cgd 	}
    322  1.44  dholland 	showquotas(QUOTA_IDTYPE_GROUP, "group", grp->gr_gid, name);
    323   1.1       cgd }
    324   1.1       cgd 
    325  1.35  christos static void
    326  1.44  dholland showquotas(int idtype, const char *idtypename, id_t id, const char *idname)
    327   1.1       cgd {
    328  1.16     lukem 	struct quotause *qup;
    329   1.6   deraadt 	struct quotause *quplist;
    330  1.41  dholland 
    331  1.41  dholland 	needheading = 1;
    332  1.41  dholland 
    333  1.44  dholland 	quplist = getprivs(id, idtype);
    334  1.41  dholland 	for (qup = quplist; qup; qup = qup->next) {
    335  1.44  dholland 		showonequota(idtype, idtypename, id, idname, qup);
    336  1.41  dholland 	}
    337  1.41  dholland 	if (!qflag) {
    338  1.41  dholland 		/* In case nothing printed, issue a header saying "none" */
    339  1.44  dholland 		heading(idtype, idtypename, id, idname, "none");
    340  1.41  dholland 	}
    341  1.41  dholland }
    342  1.41  dholland 
    343  1.41  dholland static void
    344  1.44  dholland showonequota(int idtype, const char *idtypename, id_t id, const char *idname,
    345  1.44  dholland 	     struct quotause *qup)
    346  1.41  dholland {
    347   1.1       cgd 	static time_t now;
    348  1.42  dholland 	struct quotaval *qvs;
    349  1.42  dholland 	unsigned numqvs, i;
    350  1.42  dholland 	const char *msg;
    351  1.42  dholland 
    352  1.43  dholland 	qvs = qup->qvs;
    353  1.43  dholland 	numqvs = qup->numqvs;
    354   1.1       cgd 
    355  1.41  dholland 	if (now == 0) {
    356   1.1       cgd 		time(&now);
    357  1.41  dholland 	}
    358  1.41  dholland 
    359  1.42  dholland 	if (!vflag && unlimited(qvs, numqvs)) {
    360  1.41  dholland 		return;
    361  1.41  dholland 	}
    362  1.42  dholland 
    363  1.41  dholland 	if (qflag) {
    364  1.42  dholland 		for (i=0; i<numqvs; i++) {
    365  1.44  dholland 			msg = getovermsg(&qvs[i],
    366  1.44  dholland 					 quota_idtype_getname(qup->qh, i),
    367  1.42  dholland 					 now);
    368  1.42  dholland 			if (msg != NULL) {
    369  1.44  dholland 				heading(idtype, idtypename, id, idname, "");
    370  1.42  dholland 				printf("\t%s %s\n", msg, qup->fsname);
    371  1.42  dholland 			}
    372  1.21      ross 		}
    373  1.41  dholland 		return;
    374  1.41  dholland 	}
    375  1.42  dholland 
    376  1.42  dholland 	/*
    377  1.44  dholland 	 * XXX this behavior appears to be demanded by the ATF tests,
    378  1.44  dholland 	 * although it seems to be at variance with the preexisting
    379  1.44  dholland 	 * logic in quota.c.
    380  1.44  dholland 	 */
    381  1.44  dholland 	if (unlimited(qvs, numqvs) && !anyusage(qvs, numqvs)) {
    382  1.44  dholland 		return;
    383  1.44  dholland 	}
    384  1.44  dholland 
    385  1.44  dholland 	/*
    386  1.42  dholland 	 * XXX: anyover can in fact be true if anyusage is not true,
    387  1.42  dholland 	 * if there's a quota of zero set on some volume. This is
    388  1.42  dholland 	 * because the check we do checks if adding one more thing
    389  1.44  dholland 	 * will go over. That is reasonable, I suppose, but arguably
    390  1.42  dholland 	 * the resulting behavior with usage 0 is a bug. (Also, what
    391  1.42  dholland 	 * reason do we have to believe that the reported grace expire
    392  1.42  dholland 	 * time is valid if we aren't in fact over yet?)
    393  1.42  dholland 	 */
    394  1.42  dholland 
    395  1.42  dholland 	if (vflag || dflag || anyover(qvs, numqvs, now) ||
    396  1.42  dholland 	    anyusage(qvs, numqvs)) {
    397  1.44  dholland 		heading(idtype, idtypename, id, idname, "");
    398  1.41  dholland 		if (strlen(qup->fsname) > 4) {
    399  1.41  dholland 			printf("%s\n", qup->fsname);
    400  1.42  dholland 			printf("%12s", "");
    401  1.42  dholland 		} else {
    402  1.42  dholland 			printf("%12s", qup->fsname);
    403  1.42  dholland 		}
    404  1.42  dholland 
    405  1.42  dholland 		for (i=0; i<numqvs; i++) {
    406  1.44  dholland 			printqv(&qvs[i],
    407  1.44  dholland 				quota_objtype_isbytes(qup->qh, i), now);
    408  1.42  dholland 		}
    409  1.42  dholland 		printf("\n");
    410   1.1       cgd 	}
    411   1.1       cgd }
    412   1.1       cgd 
    413  1.35  christos static void
    414  1.44  dholland heading(int idtype, const char *idtypename, id_t id, const char *idname,
    415  1.44  dholland 	const char *tag)
    416   1.1       cgd {
    417  1.41  dholland 	if (needheading == 0)
    418  1.41  dholland 		return;
    419  1.41  dholland 	needheading = 0;
    420  1.41  dholland 
    421  1.34    bouyer 	if (dflag)
    422  1.44  dholland 		printf("Default %s disk quotas: %s\n", idtypename, tag);
    423  1.34    bouyer 	else
    424  1.35  christos 		printf("Disk quotas for %s %s (%cid %u): %s\n",
    425  1.44  dholland 		       idtypename, idname, idtypename[0], id, tag);
    426   1.1       cgd 
    427   1.1       cgd 	if (!qflag && tag[0] == '\0') {
    428  1.22    bouyer 		printf("%12s%9s %8s%9s%8s%8s %7s%8s%8s\n"
    429  1.22    bouyer 		    , "Filesystem"
    430  1.22    bouyer 		    , "blocks"
    431  1.22    bouyer 		    , "quota"
    432  1.22    bouyer 		    , "limit"
    433  1.22    bouyer 		    , "grace"
    434  1.22    bouyer 		    , "files"
    435  1.22    bouyer 		    , "quota"
    436  1.22    bouyer 		    , "limit"
    437  1.22    bouyer 		    , "grace"
    438   1.1       cgd 		);
    439   1.1       cgd 	}
    440   1.1       cgd }
    441   1.1       cgd 
    442  1.42  dholland static void
    443  1.44  dholland printqv(struct quotaval *qv, int isbytes, time_t now)
    444  1.42  dholland {
    445  1.42  dholland 	char buf[20];
    446  1.42  dholland 	const char *str;
    447  1.42  dholland 	int intprtflags, over, width;
    448  1.42  dholland 
    449  1.42  dholland 	/*
    450  1.42  dholland 	 * The assorted finagling of width is to match the previous
    451  1.42  dholland 	 * open-coded formatting for exactly two quota object types,
    452  1.42  dholland 	 * which was chosen to make the default report fit in 80
    453  1.42  dholland 	 * columns.
    454  1.42  dholland 	 */
    455  1.42  dholland 
    456  1.42  dholland 	width = isbytes ? 9 : 8;
    457  1.42  dholland 	intprtflags = isbytes ? HN_B : 0;
    458  1.42  dholland 	over = isover(qv, now);
    459  1.42  dholland 
    460  1.42  dholland 	str = intprt(buf, width, qv->qv_usage, intprtflags, hflag);
    461  1.42  dholland 	printf("%*s", width, str);
    462  1.42  dholland 
    463  1.42  dholland 	printf("%c", over ? '*' : ' ');
    464  1.42  dholland 
    465  1.42  dholland 	str = intprt(buf, width, qv->qv_softlimit, intprtflags, hflag);
    466  1.42  dholland 	printf("%*s", width-1, str);
    467  1.42  dholland 
    468  1.42  dholland 	str = intprt(buf, width, qv->qv_hardlimit, intprtflags, hflag);
    469  1.42  dholland 	printf("%*s", width, str);
    470  1.42  dholland 
    471  1.42  dholland 	if (over) {
    472  1.42  dholland 		str = timeprt(buf, 9, now, qv->qv_expiretime);
    473  1.44  dholland 	} else if (vflag && qv->qv_grace != QUOTA_NOTIME) {
    474  1.42  dholland 		str = timeprt(buf, 9, 0, qv->qv_grace);
    475  1.42  dholland 	} else {
    476  1.42  dholland 		str = "";
    477  1.42  dholland 	}
    478  1.42  dholland 	printf("%8s", str);
    479  1.42  dholland }
    480  1.42  dholland 
    481   1.1       cgd /*
    482   1.1       cgd  * Collect the requested quota information.
    483   1.1       cgd  */
    484  1.35  christos static struct quotause *
    485  1.44  dholland getprivs(id_t id, int idtype)
    486   1.1       cgd {
    487  1.16     lukem 	struct quotause *qup, *quptail;
    488   1.1       cgd 	struct quotause *quphead;
    489  1.29  christos 	struct statvfs *fst;
    490  1.44  dholland 	struct quotakey qk;
    491   1.6   deraadt 	int nfst, i;
    492  1.44  dholland 	unsigned j;
    493   1.6   deraadt 
    494  1.16     lukem 	qup = quphead = quptail = NULL;
    495   1.1       cgd 
    496   1.6   deraadt 	nfst = getmntinfo(&fst, MNT_WAIT);
    497  1.16     lukem 	if (nfst == 0)
    498  1.16     lukem 		errx(2, "no filesystems mounted!");
    499  1.19       mrg 	for (i = 0; i < nfst; i++) {
    500   1.6   deraadt 		if (qup == NULL) {
    501  1.35  christos 			if ((qup = malloc(sizeof *qup)) == NULL)
    502  1.43  dholland 				err(1, "Out of memory");
    503   1.1       cgd 		}
    504  1.44  dholland 		qup->qh = quota_open(fst[i].f_mntonname);
    505  1.44  dholland 		if (qup->qh == NULL) {
    506  1.44  dholland 			if (errno == EOPNOTSUPP || errno == ENXIO) {
    507  1.44  dholland 				continue;
    508  1.44  dholland 			}
    509  1.44  dholland 			err(1, "%s: quota_open", fst[i].f_mntonname);
    510  1.44  dholland 		}
    511  1.44  dholland #if 0
    512  1.31  christos 		if (strncmp(fst[i].f_fstypename, "nfs",
    513  1.31  christos 		    sizeof(fst[i].f_fstypename)) == 0) {
    514  1.34    bouyer 			version = 0;
    515  1.43  dholland 			qup->numqvs = QUOTA_NLIMITS;
    516  1.43  dholland 			qup->qvs = malloc(qup->numqvs * sizeof(qup->qvs[0]));
    517  1.43  dholland 			if (qup->qvs == NULL) {
    518  1.43  dholland 				err(1, "Out of memory");
    519  1.43  dholland 			}
    520  1.37    bouyer 			if (getnfsquota(fst[i].f_mntfromname,
    521  1.44  dholland 			    qup->qvs, id, ufs_quota_class_names[idtype]) != 1)
    522   1.6   deraadt 				continue;
    523  1.34    bouyer 		} else if ((fst[i].f_flag & ST_QUOTA) != 0) {
    524  1.43  dholland 			qup->numqvs = QUOTA_NLIMITS;
    525  1.43  dholland 			qup->qvs = malloc(qup->numqvs * sizeof(qup->qvs[0]));
    526  1.43  dholland 			if (qup->qvs == NULL) {
    527  1.43  dholland 				err(1, "Out of memory");
    528  1.43  dholland 			}
    529  1.43  dholland 			if (getvfsquota(fst[i].f_mntonname, qup->qvs, &version,
    530  1.44  dholland 			    id, idtype, dflag, Dflag) != 1)
    531   1.1       cgd 				continue;
    532   1.6   deraadt 		} else
    533   1.6   deraadt 			continue;
    534  1.44  dholland #else
    535  1.44  dholland 		qup->numqvs = quota_getnumidtypes(qup->qh);
    536  1.44  dholland 		qup->qvs = malloc(qup->numqvs * sizeof(qup->qvs[0]));
    537  1.44  dholland 		if (qup->qvs == NULL) {
    538  1.44  dholland 			err(1, "Out of memory");
    539  1.44  dholland 		}
    540  1.44  dholland 		qk.qk_idtype = idtype;
    541  1.44  dholland 		if (dflag) {
    542  1.44  dholland 			qk.qk_id = QUOTA_DEFAULTID;
    543  1.44  dholland 		} else {
    544  1.44  dholland 			qk.qk_id = id;
    545  1.44  dholland 		}
    546  1.44  dholland 		for (j=0; j<qup->numqvs; j++) {
    547  1.44  dholland 			qk.qk_objtype = j;
    548  1.44  dholland 			if (quota_get(qup->qh, &qk, &qup->qvs[j]) < 0) {
    549  1.44  dholland 				if (errno != ENOENT) {
    550  1.44  dholland 					warn("%s: quota_get (objtype %u)",
    551  1.44  dholland 					     fst[i].f_mntonname, j);
    552  1.44  dholland 				}
    553  1.44  dholland 				quotaval_clear(&qup->qvs[j]);
    554  1.44  dholland 			}
    555  1.44  dholland 		}
    556  1.44  dholland #endif
    557  1.44  dholland 		(void)strlcpy(qup->fsname, fst[i].f_mntonname,
    558  1.44  dholland 		    sizeof(qup->fsname));
    559   1.1       cgd 		if (quphead == NULL)
    560   1.1       cgd 			quphead = qup;
    561   1.1       cgd 		else
    562   1.1       cgd 			quptail->next = qup;
    563   1.1       cgd 		quptail = qup;
    564   1.6   deraadt 		quptail->next = 0;
    565   1.6   deraadt 		qup = NULL;
    566   1.1       cgd 	}
    567  1.35  christos 	free(qup);
    568  1.35  christos 	return quphead;
    569   1.1       cgd }
    570  1.42  dholland 
    571  1.42  dholland static int
    572  1.42  dholland unlimited(struct quotaval *qvs, unsigned numqvs)
    573  1.42  dholland {
    574  1.42  dholland 	unsigned i;
    575  1.42  dholland 
    576  1.42  dholland 	for (i=0; i<numqvs; i++) {
    577  1.44  dholland 		if (qvs[i].qv_softlimit != QUOTA_NOLIMIT ||
    578  1.44  dholland 		    qvs[i].qv_hardlimit != QUOTA_NOLIMIT) {
    579  1.42  dholland 			return 0;
    580  1.42  dholland 		}
    581  1.42  dholland 	}
    582  1.42  dholland 	return 1;
    583  1.42  dholland }
    584  1.42  dholland 
    585  1.42  dholland static int
    586  1.42  dholland anyusage(struct quotaval *qvs, unsigned numqvs)
    587  1.42  dholland {
    588  1.42  dholland 	unsigned i;
    589  1.42  dholland 
    590  1.42  dholland 	for (i=0; i<numqvs; i++) {
    591  1.42  dholland 		if (qvs[i].qv_usage > 0) {
    592  1.42  dholland 			return 1;
    593  1.42  dholland 		}
    594  1.42  dholland 	}
    595  1.42  dholland 	return 0;
    596  1.42  dholland }
    597  1.42  dholland 
    598  1.42  dholland static int
    599  1.42  dholland anyover(struct quotaval *qvs, unsigned numqvs, time_t now)
    600  1.42  dholland {
    601  1.42  dholland 	unsigned i;
    602  1.42  dholland 
    603  1.42  dholland 	for (i=0; i<numqvs; i++) {
    604  1.42  dholland 		if (isover(&qvs[i], now)) {
    605  1.42  dholland 			return 1;
    606  1.42  dholland 		}
    607  1.42  dholland 	}
    608  1.42  dholland 	return 0;
    609  1.42  dholland }
    610  1.42  dholland 
    611  1.42  dholland static int
    612  1.42  dholland isover(struct quotaval *qv, time_t now)
    613  1.42  dholland {
    614  1.42  dholland 	int ql_stat;
    615  1.42  dholland 
    616  1.42  dholland 	ql_stat = quota_check_limit(qv->qv_usage, 1,
    617  1.42  dholland 				    qv->qv_softlimit,
    618  1.42  dholland 				    qv->qv_hardlimit,
    619  1.42  dholland 				    qv->qv_expiretime, now);
    620  1.42  dholland 	switch(QL_STATUS(ql_stat)) {
    621  1.42  dholland 	    case QL_S_DENY_HARD:
    622  1.42  dholland 	    case QL_S_DENY_GRACE:
    623  1.42  dholland 	    case QL_S_ALLOW_SOFT:
    624  1.42  dholland 		return 1;
    625  1.42  dholland 	    default:
    626  1.42  dholland 		break;
    627  1.42  dholland 	}
    628  1.42  dholland 	return 0;
    629  1.42  dholland }
    630  1.42  dholland 
    631  1.42  dholland static const char *
    632  1.42  dholland getovermsg(struct quotaval *qv, const char *what, time_t now)
    633  1.42  dholland {
    634  1.42  dholland 	static char buf[64];
    635  1.42  dholland 	int ql_stat;
    636  1.42  dholland 
    637  1.42  dholland 	ql_stat = quota_check_limit(qv->qv_usage, 1,
    638  1.42  dholland 				    qv->qv_softlimit,
    639  1.42  dholland 				    qv->qv_hardlimit,
    640  1.42  dholland 				    qv->qv_expiretime, now);
    641  1.42  dholland 	switch(QL_STATUS(ql_stat)) {
    642  1.42  dholland 	    case QL_S_DENY_HARD:
    643  1.42  dholland 		snprintf(buf, sizeof(buf), "%c%s limit reached on",
    644  1.42  dholland 			 toupper((unsigned char)what[0]), what+1);
    645  1.42  dholland 		break;
    646  1.42  dholland 	    case QL_S_DENY_GRACE:
    647  1.42  dholland 		snprintf(buf, sizeof(buf), "Over %s quota on", what);
    648  1.42  dholland 		break;
    649  1.42  dholland 	    case QL_S_ALLOW_SOFT:
    650  1.42  dholland 		snprintf(buf, sizeof(buf), "In %s grace period on", what);
    651  1.42  dholland 		break;
    652  1.42  dholland 	    default:
    653  1.42  dholland 		return NULL;
    654  1.42  dholland 	}
    655  1.42  dholland 	return buf;
    656  1.42  dholland }
    657