Home | History | Annotate | Line # | Download | only in repquota
repquota.c revision 1.38
      1  1.38  dholland /*	$NetBSD: repquota.c,v 1.38 2012/01/25 01:24:53 dholland Exp $	*/
      2  1.27  christos 
      3   1.1       cgd /*
      4   1.4   mycroft  * Copyright (c) 1980, 1990, 1993
      5   1.4   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.20       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.10       mrg #include <sys/cdefs.h>
     36   1.1       cgd #ifndef lint
     37  1.23     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
     38  1.23     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.10       mrg #if 0
     43  1.10       mrg static char sccsid[] = "@(#)repquota.c	8.2 (Berkeley) 11/22/94";
     44  1.10       mrg #else
     45  1.38  dholland __RCSID("$NetBSD: repquota.c,v 1.38 2012/01/25 01:24:53 dholland Exp $");
     46  1.10       mrg #endif
     47   1.1       cgd #endif /* not lint */
     48   1.1       cgd 
     49   1.1       cgd /*
     50   1.1       cgd  * Quota report
     51   1.1       cgd  */
     52   1.1       cgd #include <sys/param.h>
     53   1.1       cgd #include <sys/stat.h>
     54  1.26    bouyer #include <sys/types.h>
     55  1.26    bouyer #include <sys/statvfs.h>
     56  1.26    bouyer 
     57  1.11     lukem #include <errno.h>
     58  1.26    bouyer #include <err.h>
     59   1.1       cgd #include <fstab.h>
     60  1.11     lukem #include <grp.h>
     61   1.1       cgd #include <pwd.h>
     62   1.1       cgd #include <stdio.h>
     63  1.11     lukem #include <stdlib.h>
     64   1.6       cgd #include <string.h>
     65  1.11     lukem #include <unistd.h>
     66   1.1       cgd 
     67  1.30    bouyer #include <quota/quota.h>
     68  1.37  dholland #include <quota/quotaprop.h>
     69  1.34  dholland #include <quota.h>
     70  1.26    bouyer 
     71  1.27  christos #include "printquota.h"
     72   1.1       cgd 
     73  1.38  dholland /*
     74  1.38  dholland  * XXX. Ideally we shouldn't compile either of these in, but it's a
     75  1.38  dholland  * nontrivial rework to avoid it and it'll work ok for now.
     76  1.38  dholland  */
     77  1.38  dholland #define REPQUOTA_NUMIDTYPES	2
     78  1.38  dholland #define REPQUOTA_NUMOBJTYPES	2
     79  1.38  dholland 
     80   1.1       cgd struct fileusage {
     81   1.1       cgd 	struct	fileusage *fu_next;
     82  1.38  dholland 	struct	quotaval fu_qv[REPQUOTA_NUMOBJTYPES];
     83  1.27  christos 	uint32_t	fu_id;
     84   1.1       cgd 	char	fu_name[1];
     85   1.1       cgd 	/* actually bigger */
     86   1.1       cgd };
     87  1.38  dholland 
     88   1.1       cgd #define FUHASH 1024	/* must be power of two */
     89  1.38  dholland static struct fileusage *fuhead[REPQUOTA_NUMIDTYPES][FUHASH];
     90  1.38  dholland 
     91  1.38  dholland /* highest addid()'ed identifier per idtype */
     92  1.38  dholland static uint32_t highid[REPQUOTA_NUMIDTYPES];
     93  1.38  dholland 
     94  1.38  dholland int valid[REPQUOTA_NUMIDTYPES];
     95  1.38  dholland 
     96  1.38  dholland static struct quotaval defaultqv[REPQUOTA_NUMIDTYPES][REPQUOTA_NUMOBJTYPES];
     97   1.1       cgd 
     98  1.27  christos static int	vflag = 0;		/* verbose */
     99  1.27  christos static int	aflag = 0;		/* all file systems */
    100  1.27  christos static int	Dflag = 0;		/* debug */
    101  1.27  christos static int	hflag = 0;		/* humanize */
    102  1.27  christos static int	xflag = 0;		/* export */
    103  1.27  christos 
    104  1.38  dholland /*
    105  1.38  dholland  * XXX this should go away and be replaced with a call to
    106  1.38  dholland  * quota_idtype_getname(), but that needs a quotahandle and requires
    107  1.38  dholland  * the same nontrivial rework as getting rid of REPQUOTA_NUMIDTYPES.
    108  1.38  dholland  */
    109  1.38  dholland static const char *const repquota_idtype_names[REPQUOTA_NUMIDTYPES] = {
    110  1.38  dholland 	"user",
    111  1.38  dholland 	"group",
    112  1.38  dholland };
    113  1.27  christos 
    114  1.27  christos static struct fileusage *addid(uint32_t, int, const char *);
    115  1.27  christos static struct fileusage *lookup(uint32_t, int);
    116  1.27  christos static struct fileusage *qremove(uint32_t, int);
    117  1.35  dholland static int	repquota(struct quotahandle *, int);
    118  1.27  christos static void	usage(void) __attribute__((__noreturn__));
    119  1.35  dholland static void	printquotas(int, struct quotahandle *);
    120  1.27  christos static void	exportquotas(void);
    121  1.37  dholland static int	oneof(const char *, char *[], int cnt);
    122  1.11     lukem 
    123  1.11     lukem int
    124  1.27  christos main(int argc, char **argv)
    125   1.1       cgd {
    126   1.1       cgd 	int gflag = 0, uflag = 0, errs = 0;
    127   1.1       cgd 	long i, argnum, done = 0;
    128   1.9      mark 	int ch;
    129  1.26    bouyer 	struct statvfs *fst;
    130  1.26    bouyer 	int nfst;
    131  1.35  dholland 	struct quotahandle *qh;
    132   1.1       cgd 
    133  1.26    bouyer 	while ((ch = getopt(argc, argv, "Daguhvx")) != -1) {
    134   1.1       cgd 		switch(ch) {
    135   1.1       cgd 		case 'a':
    136   1.1       cgd 			aflag++;
    137   1.1       cgd 			break;
    138   1.1       cgd 		case 'g':
    139   1.1       cgd 			gflag++;
    140   1.1       cgd 			break;
    141   1.1       cgd 		case 'u':
    142   1.1       cgd 			uflag++;
    143   1.1       cgd 			break;
    144  1.26    bouyer 		case 'h':
    145  1.26    bouyer 			hflag++;
    146  1.26    bouyer 			break;
    147   1.1       cgd 		case 'v':
    148   1.1       cgd 			vflag++;
    149   1.1       cgd 			break;
    150  1.26    bouyer 		case 'D':
    151  1.26    bouyer 			Dflag++;
    152  1.26    bouyer 			break;
    153  1.26    bouyer 		case 'x':
    154  1.26    bouyer 			xflag++;
    155  1.26    bouyer 			break;
    156   1.1       cgd 		default:
    157   1.1       cgd 			usage();
    158   1.1       cgd 		}
    159   1.1       cgd 	}
    160   1.1       cgd 	argc -= optind;
    161   1.1       cgd 	argv += optind;
    162  1.26    bouyer 	if (xflag && (argc != 1 || aflag))
    163  1.26    bouyer 		usage();
    164   1.1       cgd 	if (argc == 0 && !aflag)
    165   1.1       cgd 		usage();
    166   1.1       cgd 	if (!gflag && !uflag) {
    167   1.1       cgd 		if (aflag)
    168   1.1       cgd 			gflag++;
    169   1.1       cgd 		uflag++;
    170   1.1       cgd 	}
    171  1.26    bouyer 
    172  1.26    bouyer 	nfst = getmntinfo(&fst, MNT_WAIT);
    173  1.26    bouyer 	if (nfst == 0)
    174  1.27  christos 		errx(1, "no filesystems mounted!");
    175  1.26    bouyer 	for (i = 0; i < nfst; i++) {
    176  1.26    bouyer 		if ((fst[i].f_flag & ST_QUOTA) == 0)
    177   1.1       cgd 			continue;
    178  1.35  dholland 		/* check if we want this volume */
    179  1.35  dholland 		if (!aflag) {
    180  1.35  dholland 			argnum = oneof(fst[i].f_mntonname, argv, argc);
    181  1.35  dholland 			if (argnum < 0) {
    182  1.35  dholland 				argnum = oneof(fst[i].f_mntfromname,
    183  1.35  dholland 					       argv, argc);
    184  1.35  dholland 			}
    185  1.35  dholland 			if (argnum < 0) {
    186  1.35  dholland 				continue;
    187  1.35  dholland 			}
    188  1.35  dholland 			done |= 1U << argnum;
    189  1.35  dholland 		}
    190  1.35  dholland 
    191  1.35  dholland 		qh = quota_open(fst[i].f_mntonname);
    192  1.35  dholland 		if (qh == NULL) {
    193  1.35  dholland 			/* XXX: check this errno */
    194  1.35  dholland 			if (errno == EOPNOTSUPP || errno == ENXIO) {
    195  1.35  dholland 				continue;
    196  1.35  dholland 			}
    197  1.35  dholland 			warn("%s: quota_open", fst[i].f_mntonname);
    198   1.1       cgd 			continue;
    199   1.1       cgd 		}
    200  1.35  dholland 
    201  1.35  dholland 		if (gflag)
    202  1.35  dholland 			errs += repquota(qh, QUOTA_IDTYPE_GROUP);
    203  1.35  dholland 		if (uflag)
    204  1.35  dholland 			errs += repquota(qh, QUOTA_IDTYPE_USER);
    205  1.35  dholland 
    206  1.35  dholland 		quota_close(qh);
    207   1.1       cgd 	}
    208  1.26    bouyer 	if (xflag)
    209  1.26    bouyer 		exportquotas();
    210   1.1       cgd 	for (i = 0; i < argc; i++)
    211  1.27  christos 		if ((done & (1U << i)) == 0)
    212  1.27  christos 			warnx("%s not mounted", argv[i]);
    213  1.27  christos 	return errs;
    214   1.1       cgd }
    215   1.1       cgd 
    216  1.27  christos static void
    217  1.27  christos usage(void)
    218  1.27  christos {
    219  1.27  christos 	const char *p = getprogname();
    220  1.27  christos 	fprintf(stderr, "usage: %s [-D] [-v] [-g] [-u] -a\n"
    221  1.27  christos 		"\t%s [-D] [-v] [-g] [-u] filesys ...\n"
    222  1.27  christos 		"\t%s -x [-D] [-g] [-u] filesys\n", p, p, p);
    223   1.1       cgd 	exit(1);
    224   1.1       cgd }
    225   1.1       cgd 
    226  1.27  christos static int
    227  1.35  dholland repquota(struct quotahandle *qh, int idtype)
    228  1.26    bouyer {
    229  1.36  dholland 	struct quotacursor *qc;
    230  1.36  dholland 	struct quotakey qk;
    231  1.36  dholland 	struct quotaval qv;
    232  1.35  dholland 	struct quotaval *qvp;
    233  1.35  dholland 	struct fileusage *fup;
    234  1.35  dholland 
    235  1.36  dholland 	qc = quota_opencursor(qh);
    236  1.36  dholland 	if (qc == NULL) {
    237  1.35  dholland 		return 1;
    238  1.35  dholland 	}
    239  1.26    bouyer 
    240  1.36  dholland 	if (idtype == QUOTA_IDTYPE_USER) {
    241  1.36  dholland 		quotacursor_skipidtype(qc, QUOTA_IDTYPE_GROUP);
    242  1.36  dholland 	}
    243  1.36  dholland 	if (idtype == QUOTA_IDTYPE_GROUP) {
    244  1.36  dholland 		quotacursor_skipidtype(qc, QUOTA_IDTYPE_USER);
    245  1.36  dholland 	}
    246  1.36  dholland 
    247  1.36  dholland 	valid[idtype] = 0;
    248  1.36  dholland 	while (!quotacursor_atend(qc)) {
    249  1.36  dholland 		if (quotacursor_get(qc, &qk, &qv)) {
    250  1.36  dholland 			err(1, "%s: quotacursor_get", quota_getmountpoint(qh));
    251  1.36  dholland 		}
    252  1.36  dholland 		if (qk.qk_idtype != idtype) {
    253  1.36  dholland 			continue;
    254  1.36  dholland 		}
    255  1.36  dholland 
    256  1.36  dholland 		valid[idtype] = 1;
    257  1.36  dholland 		if (qk.qk_id == QUOTA_DEFAULTID) {
    258  1.36  dholland 			qvp = defaultqv[idtype];
    259  1.36  dholland 		} else {
    260  1.36  dholland 			if ((fup = lookup(qk.qk_id, idtype)) == 0)
    261  1.36  dholland 				fup = addid(qk.qk_id, idtype, (char *)0);
    262  1.36  dholland 			qvp = fup->fu_qv;
    263  1.36  dholland 		}
    264  1.36  dholland 		if (qk.qk_objtype == QUOTA_OBJTYPE_BLOCKS) {
    265  1.36  dholland 			qvp[QUOTA_OBJTYPE_BLOCKS] = qv;
    266  1.36  dholland 		} else if (qk.qk_objtype == QUOTA_OBJTYPE_FILES) {
    267  1.36  dholland 			qvp[QUOTA_OBJTYPE_FILES] = qv;
    268  1.26    bouyer 		}
    269  1.36  dholland 	}
    270  1.35  dholland 
    271  1.34  dholland 	if (xflag == 0 && valid[idtype])
    272  1.35  dholland 		printquotas(idtype, qh);
    273  1.35  dholland 
    274  1.27  christos 	return 0;
    275  1.26    bouyer }
    276  1.26    bouyer 
    277  1.27  christos static void
    278  1.35  dholland printquotas(int idtype, struct quotahandle *qh)
    279  1.26    bouyer {
    280  1.26    bouyer 	static int multiple = 0;
    281  1.27  christos 	uint32_t id;
    282  1.26    bouyer 	int i;
    283  1.26    bouyer 	struct fileusage *fup;
    284  1.33  dholland 	struct quotaval *q;
    285  1.38  dholland 	const char *timemsg[REPQUOTA_NUMOBJTYPES];
    286  1.38  dholland 	char overchar[REPQUOTA_NUMOBJTYPES];
    287  1.27  christos 	time_t now;
    288  1.29    bouyer 	char b0[2][20], b1[20], b2[20], b3[20];
    289  1.38  dholland 	int ok, objtype;
    290  1.38  dholland 	int isbytes, width;
    291  1.26    bouyer 
    292  1.34  dholland 	switch (idtype) {
    293  1.34  dholland 	case QUOTA_IDTYPE_GROUP:
    294  1.26    bouyer 		{
    295  1.26    bouyer 		struct group *gr;
    296  1.26    bouyer 		setgrent();
    297  1.26    bouyer 		while ((gr = getgrent()) != 0)
    298  1.34  dholland 			(void)addid(gr->gr_gid, idtype, gr->gr_name);
    299  1.26    bouyer 		endgrent();
    300  1.26    bouyer 		break;
    301  1.26    bouyer 		}
    302  1.34  dholland 	case QUOTA_IDTYPE_USER:
    303  1.26    bouyer 		{
    304  1.26    bouyer 		struct passwd *pw;
    305  1.26    bouyer 		setpwent();
    306  1.26    bouyer 		while ((pw = getpwent()) != 0)
    307  1.34  dholland 			(void)addid(pw->pw_uid, idtype, pw->pw_name);
    308  1.26    bouyer 		endpwent();
    309  1.26    bouyer 		break;
    310  1.26    bouyer 		}
    311  1.26    bouyer 	default:
    312  1.34  dholland 		errx(1, "Unknown quota ID type %d", idtype);
    313   1.1       cgd 	}
    314  1.26    bouyer 
    315  1.27  christos 	time(&now);
    316  1.26    bouyer 
    317  1.26    bouyer 	if (multiple++)
    318  1.26    bouyer 		printf("\n");
    319  1.26    bouyer 	if (vflag)
    320  1.35  dholland 		printf("*** Report for %s quotas on %s (%s: %s)\n",
    321  1.38  dholland 		    repquota_idtype_names[idtype], quota_getmountpoint(qh),
    322  1.35  dholland 		    quota_getmountdevice(qh), quota_getimplname(qh));
    323  1.27  christos 	printf("                        Block limits               "
    324  1.27  christos 	    "File limits\n");
    325  1.34  dholland 	printf(idtype == QUOTA_IDTYPE_USER ? "User " : "Group");
    326  1.27  christos 	printf("            used     soft     hard  grace      used"
    327  1.27  christos 	    "soft    hard  grace\n");
    328  1.34  dholland 	for (id = 0; id <= highid[idtype]; id++) {
    329  1.34  dholland 		fup = qremove(id, idtype);
    330  1.33  dholland 		q = fup->fu_qv;
    331   1.1       cgd 		if (fup == 0)
    332   1.1       cgd 			continue;
    333  1.38  dholland 		for (i = 0; i < REPQUOTA_NUMOBJTYPES; i++) {
    334  1.33  dholland 			switch (QL_STATUS(quota_check_limit(q[i].qv_usage, 1,
    335  1.33  dholland 			    q[i].qv_softlimit, q[i].qv_hardlimit,
    336  1.33  dholland 			    q[i].qv_expiretime, now))) {
    337  1.26    bouyer 			case QL_S_DENY_HARD:
    338  1.26    bouyer 			case QL_S_DENY_GRACE:
    339  1.26    bouyer 			case QL_S_ALLOW_SOFT:
    340  1.29    bouyer 				timemsg[i] = timeprt(b0[i], 8, now,
    341  1.33  dholland 				    q[i].qv_expiretime);
    342  1.26    bouyer 				overchar[i] = '+';
    343  1.26    bouyer 				break;
    344  1.26    bouyer 			default:
    345  1.35  dholland 				if (vflag && q[i].qv_grace != QUOTA_NOTIME) {
    346  1.35  dholland 					timemsg[i] = timeprt(b0[i], 8, 0,
    347  1.35  dholland 							     q[i].qv_grace);
    348  1.35  dholland 				} else {
    349  1.35  dholland 					timemsg[i] = "";
    350  1.35  dholland 				}
    351  1.26    bouyer 				overchar[i] = '-';
    352  1.26    bouyer 				break;
    353  1.26    bouyer 			}
    354  1.26    bouyer 		}
    355  1.26    bouyer 
    356  1.38  dholland 		ok = 1;
    357  1.38  dholland 		for (objtype = 0; objtype < REPQUOTA_NUMOBJTYPES; objtype++) {
    358  1.38  dholland 			if (q[objtype].qv_usage != 0 ||
    359  1.38  dholland 			    overchar[objtype] != '-') {
    360  1.38  dholland 				ok = 0;
    361  1.38  dholland 			}
    362  1.38  dholland 		}
    363  1.38  dholland 		if (ok && vflag == 0)
    364   1.1       cgd 			continue;
    365  1.22  jdolecek 		if (strlen(fup->fu_name) > 9)
    366  1.22  jdolecek 			printf("%s ", fup->fu_name);
    367  1.22  jdolecek 		else
    368  1.22  jdolecek 			printf("%-10s", fup->fu_name);
    369  1.38  dholland 		for (objtype = 0; objtype < REPQUOTA_NUMOBJTYPES; objtype++) {
    370  1.38  dholland 			printf("%c", overchar[objtype]);
    371  1.38  dholland 		}
    372  1.38  dholland 		for (objtype = 0; objtype < REPQUOTA_NUMOBJTYPES; objtype++) {
    373  1.38  dholland 			isbytes = quota_objtype_isbytes(qh, objtype);
    374  1.38  dholland 			width = isbytes ? 9 : 8;
    375  1.38  dholland 			printf("%*s%*s%*s%7s",
    376  1.38  dholland 			       width,
    377  1.38  dholland 			       intprt(b1, width+1, q[objtype].qv_usage,
    378  1.38  dholland 				      isbytes ? HN_B : 0, hflag),
    379  1.38  dholland 			       width,
    380  1.38  dholland 			       intprt(b2, width+1, q[objtype].qv_softlimit,
    381  1.38  dholland 				      isbytes ? HN_B : 0, hflag),
    382  1.38  dholland 			       width,
    383  1.38  dholland 			       intprt(b3, width+1, q[objtype].qv_hardlimit,
    384  1.38  dholland 				      isbytes ? HN_B : 0, hflag),
    385  1.38  dholland 			       timemsg[objtype]);
    386  1.38  dholland 
    387  1.38  dholland 			if (objtype + 1 < REPQUOTA_NUMOBJTYPES) {
    388  1.38  dholland 				printf("  ");
    389  1.38  dholland 			} else {
    390  1.38  dholland 				printf("\n");
    391  1.38  dholland 			}
    392  1.38  dholland 		}
    393  1.26    bouyer 		free(fup);
    394  1.26    bouyer 	}
    395  1.26    bouyer }
    396  1.26    bouyer 
    397  1.27  christos static void
    398  1.27  christos exportquotas(void)
    399  1.26    bouyer {
    400  1.27  christos 	uint32_t id;
    401  1.26    bouyer 	struct fileusage *fup;
    402  1.26    bouyer 	prop_dictionary_t dict, data;
    403  1.26    bouyer 	prop_array_t cmds, datas;
    404  1.34  dholland 	int idtype;
    405  1.30    bouyer 	uint64_t *valuesp[QUOTA_NLIMITS];
    406  1.26    bouyer 
    407  1.30    bouyer 	dict = quota_prop_create();
    408  1.26    bouyer 	cmds = prop_array_create();
    409  1.26    bouyer 
    410  1.26    bouyer 	if (dict == NULL || cmds == NULL) {
    411  1.26    bouyer 		errx(1, "can't allocate proplist");
    412  1.26    bouyer 	}
    413  1.26    bouyer 
    414  1.26    bouyer 
    415  1.38  dholland 	for (idtype = 0; idtype < REPQUOTA_NUMIDTYPES; idtype++) {
    416  1.34  dholland 		if (valid[idtype] == 0)
    417  1.26    bouyer 			continue;
    418  1.26    bouyer 		datas = prop_array_create();
    419  1.26    bouyer 		if (datas == NULL)
    420  1.26    bouyer 			errx(1, "can't allocate proplist");
    421  1.30    bouyer 		valuesp[QUOTA_LIMIT_BLOCK] =
    422  1.34  dholland 		    &defaultqv[idtype][QUOTA_LIMIT_BLOCK].qv_hardlimit;
    423  1.30    bouyer 		valuesp[QUOTA_LIMIT_FILE] =
    424  1.34  dholland 		    &defaultqv[idtype][QUOTA_LIMIT_FILE].qv_hardlimit;
    425  1.30    bouyer 		data = quota64toprop(0, 1, valuesp,
    426  1.30    bouyer 		    ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
    427  1.30    bouyer 		    ufs_quota_limit_names, QUOTA_NLIMITS);
    428  1.26    bouyer 		if (data == NULL)
    429  1.30    bouyer 			err(1, "quota64toprop(default)");
    430  1.26    bouyer 		if (!prop_array_add_and_rel(datas, data))
    431  1.26    bouyer 			err(1, "prop_array_add(data)");
    432  1.26    bouyer 
    433  1.34  dholland 		for (id = 0; id <= highid[idtype]; id++) {
    434  1.34  dholland 			fup = qremove(id, idtype);
    435  1.26    bouyer 			if (fup == 0)
    436  1.26    bouyer 				continue;
    437  1.30    bouyer 			valuesp[QUOTA_LIMIT_BLOCK] =
    438  1.33  dholland 			    &fup->fu_qv[QUOTA_LIMIT_BLOCK].qv_hardlimit;
    439  1.30    bouyer 			valuesp[QUOTA_LIMIT_FILE] =
    440  1.33  dholland 			    &fup->fu_qv[QUOTA_LIMIT_FILE].qv_hardlimit;
    441  1.30    bouyer 			data = quota64toprop(id, 0, valuesp,
    442  1.30    bouyer 			    ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
    443  1.30    bouyer 			    ufs_quota_limit_names, QUOTA_NLIMITS);
    444  1.26    bouyer 			if (data == NULL)
    445  1.30    bouyer 				err(1, "quota64toprop(id)");
    446  1.26    bouyer 			if (!prop_array_add_and_rel(datas, data))
    447  1.26    bouyer 				err(1, "prop_array_add(data)");
    448  1.26    bouyer 			free(fup);
    449  1.26    bouyer 		}
    450  1.26    bouyer 
    451  1.30    bouyer 		if (!quota_prop_add_command(cmds, "set",
    452  1.34  dholland 		    ufs_quota_class_names[idtype], datas))
    453  1.26    bouyer 			err(1, "prop_add_command");
    454   1.1       cgd 	}
    455  1.26    bouyer 
    456  1.26    bouyer 	if (!prop_dictionary_set(dict, "commands", cmds))
    457  1.26    bouyer 		err(1, "prop_dictionary_set(command)");
    458  1.26    bouyer 
    459  1.26    bouyer 	printf("%s\n", prop_dictionary_externalize(dict));
    460  1.26    bouyer 	return;
    461   1.1       cgd }
    462   1.1       cgd 
    463   1.1       cgd /*
    464   1.1       cgd  * Routines to manage the file usage table.
    465   1.1       cgd  *
    466  1.34  dholland  * Lookup an id of a specific id type.
    467   1.1       cgd  */
    468   1.1       cgd struct fileusage *
    469  1.34  dholland lookup(uint32_t id, int idtype)
    470   1.1       cgd {
    471  1.11     lukem 	struct fileusage *fup;
    472   1.1       cgd 
    473  1.34  dholland 	for (fup = fuhead[idtype][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
    474   1.1       cgd 		if (fup->fu_id == id)
    475  1.27  christos 			return fup;
    476  1.27  christos 	return NULL;
    477   1.1       cgd }
    478  1.26    bouyer /*
    479  1.34  dholland  * Lookup and remove an id of a specific id type.
    480  1.26    bouyer  */
    481  1.27  christos static struct fileusage *
    482  1.34  dholland qremove(uint32_t id, int idtype)
    483  1.26    bouyer {
    484  1.26    bouyer 	struct fileusage *fup, **fupp;
    485  1.26    bouyer 
    486  1.34  dholland 	for (fupp = &fuhead[idtype][id & (FUHASH-1)]; *fupp != 0;) {
    487  1.26    bouyer 		fup = *fupp;
    488  1.26    bouyer 		if (fup->fu_id == id) {
    489  1.26    bouyer 			*fupp = fup->fu_next;
    490  1.27  christos 			return fup;
    491  1.26    bouyer 		}
    492  1.26    bouyer 		fupp = &fup->fu_next;
    493  1.26    bouyer 	}
    494  1.27  christos 	return NULL;
    495  1.26    bouyer }
    496   1.1       cgd 
    497   1.1       cgd /*
    498   1.1       cgd  * Add a new file usage id if it does not already exist.
    499   1.1       cgd  */
    500  1.27  christos static struct fileusage *
    501  1.34  dholland addid(uint32_t id, int idtype, const char *name)
    502   1.1       cgd {
    503   1.1       cgd 	struct fileusage *fup, **fhp;
    504  1.26    bouyer 	struct group *gr = NULL;
    505  1.26    bouyer 	struct passwd *pw = NULL;
    506  1.27  christos 	size_t len;
    507   1.1       cgd 
    508  1.34  dholland 	if ((fup = lookup(id, idtype)) != NULL) {
    509  1.27  christos 		return fup;
    510  1.26    bouyer 	}
    511  1.26    bouyer 	if (name == NULL) {
    512  1.34  dholland 		switch(idtype) {
    513  1.34  dholland 		case  QUOTA_IDTYPE_GROUP:
    514  1.26    bouyer 			gr = getgrgid(id);
    515  1.26    bouyer 
    516  1.26    bouyer 			if (gr != NULL)
    517  1.26    bouyer 				name = gr->gr_name;
    518  1.26    bouyer 			break;
    519  1.34  dholland 		case QUOTA_IDTYPE_USER:
    520  1.26    bouyer 			pw = getpwuid(id);
    521  1.26    bouyer 			if (pw)
    522  1.26    bouyer 				name = pw->pw_name;
    523  1.26    bouyer 			break;
    524  1.26    bouyer 		default:
    525  1.34  dholland 			errx(1, "Unknown quota ID type %d\n", idtype);
    526  1.26    bouyer 		}
    527  1.26    bouyer 	}
    528  1.26    bouyer 
    529   1.1       cgd 	if (name)
    530   1.1       cgd 		len = strlen(name);
    531   1.1       cgd 	else
    532   1.1       cgd 		len = 10;
    533  1.27  christos 	if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
    534  1.27  christos 		err(1, "out of memory for fileusage structures");
    535  1.34  dholland 	fhp = &fuhead[idtype][id & (FUHASH - 1)];
    536   1.1       cgd 	fup->fu_next = *fhp;
    537   1.1       cgd 	*fhp = fup;
    538   1.1       cgd 	fup->fu_id = id;
    539  1.34  dholland 	if (id > highid[idtype])
    540  1.34  dholland 		highid[idtype] = id;
    541   1.1       cgd 	if (name) {
    542  1.12     lukem 		memmove(fup->fu_name, name, len + 1);
    543   1.1       cgd 	} else {
    544  1.28  christos 		snprintf(fup->fu_name, len + 1, "%u", id);
    545   1.1       cgd 	}
    546  1.36  dholland 	/*
    547  1.36  dholland 	 * XXX nothing guarantees the default limits have been loaded yet
    548  1.36  dholland 	 */
    549  1.38  dholland 	fup->fu_qv[QUOTA_OBJTYPE_BLOCKS] = defaultqv[idtype][QUOTA_OBJTYPE_BLOCKS];
    550  1.38  dholland 	fup->fu_qv[QUOTA_OBJTYPE_FILES] = defaultqv[idtype][QUOTA_OBJTYPE_FILES];
    551  1.27  christos 	return fup;
    552   1.1       cgd }
    553  1.37  dholland 
    554  1.37  dholland /*
    555  1.37  dholland  * Check to see if target appears in list of size cnt.
    556  1.37  dholland  */
    557  1.37  dholland static int
    558  1.37  dholland oneof(const char *target, char *list[], int cnt)
    559  1.37  dholland {
    560  1.37  dholland 	int i;
    561  1.37  dholland 
    562  1.37  dholland 	for (i = 0; i < cnt; i++)
    563  1.37  dholland 		if (strcmp(target, list[i]) == 0)
    564  1.37  dholland 			return i;
    565  1.37  dholland 	return -1;
    566  1.37  dholland }
    567