Home | History | Annotate | Line # | Download | only in repquota
repquota.c revision 1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1980, 1990 Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * This code is derived from software contributed to Berkeley by
      6  1.1  cgd  * Robert Elz at The University of Melbourne.
      7  1.1  cgd  *
      8  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1  cgd  * modification, are permitted provided that the following conditions
     10  1.1  cgd  * are met:
     11  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1  cgd  *    must display the following acknowledgement:
     18  1.1  cgd  *	This product includes software developed by the University of
     19  1.1  cgd  *	California, Berkeley and its contributors.
     20  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  cgd  *    may be used to endorse or promote products derived from this software
     22  1.1  cgd  *    without specific prior written permission.
     23  1.1  cgd  *
     24  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  cgd  * SUCH DAMAGE.
     35  1.1  cgd  */
     36  1.1  cgd 
     37  1.1  cgd #ifndef lint
     38  1.1  cgd char copyright[] =
     39  1.1  cgd "@(#) Copyright (c) 1980, 1990 Regents of the University of California.\n\
     40  1.1  cgd  All rights reserved.\n";
     41  1.1  cgd #endif /* not lint */
     42  1.1  cgd 
     43  1.1  cgd #ifndef lint
     44  1.1  cgd static char sccsid[] = "@(#)repquota.c	5.11 (Berkeley) 9/27/90";
     45  1.1  cgd #endif /* not lint */
     46  1.1  cgd 
     47  1.1  cgd /*
     48  1.1  cgd  * Quota report
     49  1.1  cgd  */
     50  1.1  cgd #include <sys/param.h>
     51  1.1  cgd #include <sys/stat.h>
     52  1.1  cgd #include <ufs/quota.h>
     53  1.1  cgd #include <fstab.h>
     54  1.1  cgd #include <pwd.h>
     55  1.1  cgd #include <grp.h>
     56  1.1  cgd #include <stdio.h>
     57  1.1  cgd #include <errno.h>
     58  1.1  cgd 
     59  1.1  cgd char *qfname = QUOTAFILENAME;
     60  1.1  cgd char *qfextension[] = INITQFNAMES;
     61  1.1  cgd 
     62  1.1  cgd struct fileusage {
     63  1.1  cgd 	struct	fileusage *fu_next;
     64  1.1  cgd 	struct	dqblk fu_dqblk;
     65  1.1  cgd 	u_long	fu_id;
     66  1.1  cgd 	char	fu_name[1];
     67  1.1  cgd 	/* actually bigger */
     68  1.1  cgd };
     69  1.1  cgd #define FUHASH 1024	/* must be power of two */
     70  1.1  cgd struct fileusage *fuhead[MAXQUOTAS][FUHASH];
     71  1.1  cgd struct fileusage *lookup();
     72  1.1  cgd struct fileusage *addid();
     73  1.1  cgd u_long highid[MAXQUOTAS];	/* highest addid()'ed identifier per type */
     74  1.1  cgd 
     75  1.1  cgd int	vflag;			/* verbose */
     76  1.1  cgd int	aflag;			/* all file systems */
     77  1.1  cgd 
     78  1.1  cgd main(argc, argv)
     79  1.1  cgd 	int argc;
     80  1.1  cgd 	char **argv;
     81  1.1  cgd {
     82  1.1  cgd 	register struct fstab *fs;
     83  1.1  cgd 	register struct passwd *pw;
     84  1.1  cgd 	register struct group *gr;
     85  1.1  cgd 	int gflag = 0, uflag = 0, errs = 0;
     86  1.1  cgd 	long i, argnum, done = 0;
     87  1.1  cgd 	extern char *optarg;
     88  1.1  cgd 	extern int optind;
     89  1.1  cgd 	char ch, *qfnp;
     90  1.1  cgd 
     91  1.1  cgd 	while ((ch = getopt(argc, argv, "aguv")) != EOF) {
     92  1.1  cgd 		switch(ch) {
     93  1.1  cgd 		case 'a':
     94  1.1  cgd 			aflag++;
     95  1.1  cgd 			break;
     96  1.1  cgd 		case 'g':
     97  1.1  cgd 			gflag++;
     98  1.1  cgd 			break;
     99  1.1  cgd 		case 'u':
    100  1.1  cgd 			uflag++;
    101  1.1  cgd 			break;
    102  1.1  cgd 		case 'v':
    103  1.1  cgd 			vflag++;
    104  1.1  cgd 			break;
    105  1.1  cgd 		default:
    106  1.1  cgd 			usage();
    107  1.1  cgd 		}
    108  1.1  cgd 	}
    109  1.1  cgd 	argc -= optind;
    110  1.1  cgd 	argv += optind;
    111  1.1  cgd 	if (argc == 0 && !aflag)
    112  1.1  cgd 		usage();
    113  1.1  cgd 	if (!gflag && !uflag) {
    114  1.1  cgd 		if (aflag)
    115  1.1  cgd 			gflag++;
    116  1.1  cgd 		uflag++;
    117  1.1  cgd 	}
    118  1.1  cgd 	if (gflag) {
    119  1.1  cgd 		setgrent();
    120  1.1  cgd 		while ((gr = getgrent()) != 0)
    121  1.1  cgd 			(void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
    122  1.1  cgd 		endgrent();
    123  1.1  cgd 	}
    124  1.1  cgd 	if (uflag) {
    125  1.1  cgd 		setpwent();
    126  1.1  cgd 		while ((pw = getpwent()) != 0)
    127  1.1  cgd 			(void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
    128  1.1  cgd 		endpwent();
    129  1.1  cgd 	}
    130  1.1  cgd 	setfsent();
    131  1.1  cgd 	while ((fs = getfsent()) != NULL) {
    132  1.1  cgd 		if (strcmp(fs->fs_vfstype, "ufs"))
    133  1.1  cgd 			continue;
    134  1.1  cgd 		if (aflag) {
    135  1.1  cgd 			if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
    136  1.1  cgd 				errs += repquota(fs, GRPQUOTA, qfnp);
    137  1.1  cgd 			if (uflag && hasquota(fs, USRQUOTA, &qfnp))
    138  1.1  cgd 				errs += repquota(fs, USRQUOTA, qfnp);
    139  1.1  cgd 			continue;
    140  1.1  cgd 		}
    141  1.1  cgd 		if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
    142  1.1  cgd 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
    143  1.1  cgd 			done |= 1 << argnum;
    144  1.1  cgd 			if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
    145  1.1  cgd 				errs += repquota(fs, GRPQUOTA, qfnp);
    146  1.1  cgd 			if (uflag && hasquota(fs, USRQUOTA, &qfnp))
    147  1.1  cgd 				errs += repquota(fs, USRQUOTA, qfnp);
    148  1.1  cgd 		}
    149  1.1  cgd 	}
    150  1.1  cgd 	endfsent();
    151  1.1  cgd 	for (i = 0; i < argc; i++)
    152  1.1  cgd 		if ((done & (1 << i)) == 0)
    153  1.1  cgd 			fprintf(stderr, "%s not found in fstab\n", argv[i]);
    154  1.1  cgd 	exit(errs);
    155  1.1  cgd }
    156  1.1  cgd 
    157  1.1  cgd usage()
    158  1.1  cgd {
    159  1.1  cgd 	fprintf(stderr, "Usage:\n\t%s\n\t%s\n",
    160  1.1  cgd 		"repquota [-v] [-g] [-u] -a",
    161  1.1  cgd 		"repquota [-v] [-g] [-u] filesys ...");
    162  1.1  cgd 	exit(1);
    163  1.1  cgd }
    164  1.1  cgd 
    165  1.1  cgd repquota(fs, type, qfpathname)
    166  1.1  cgd 	register struct fstab *fs;
    167  1.1  cgd 	int type;
    168  1.1  cgd 	char *qfpathname;
    169  1.1  cgd {
    170  1.1  cgd 	register struct fileusage *fup;
    171  1.1  cgd 	FILE *qf;
    172  1.1  cgd 	u_long id;
    173  1.1  cgd 	struct dqblk dqbuf;
    174  1.1  cgd 	char *timeprt();
    175  1.1  cgd 	static struct dqblk zerodqblk;
    176  1.1  cgd 	static int warned = 0;
    177  1.1  cgd 	static int multiple = 0;
    178  1.1  cgd 	extern int errno;
    179  1.1  cgd 
    180  1.1  cgd 	if (quotactl(fs->fs_file, QCMD(Q_SYNC, type), 0, 0) < 0 &&
    181  1.1  cgd 	    errno == EOPNOTSUPP && !warned && vflag) {
    182  1.1  cgd 		warned++;
    183  1.1  cgd 		fprintf(stdout,
    184  1.1  cgd 		    "*** Warning: Quotas are not compiled into this kernel\n");
    185  1.1  cgd 	}
    186  1.1  cgd 	if (multiple++)
    187  1.1  cgd 		printf("\n");
    188  1.1  cgd 	if (vflag)
    189  1.1  cgd 		fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
    190  1.1  cgd 		    qfextension[type], fs->fs_file, fs->fs_spec);
    191  1.1  cgd 	if ((qf = fopen(qfpathname, "r")) == NULL) {
    192  1.1  cgd 		perror(qfpathname);
    193  1.1  cgd 		return (1);
    194  1.1  cgd 	}
    195  1.1  cgd 	for (id = 0; ; id++) {
    196  1.1  cgd 		fread(&dqbuf, sizeof(struct dqblk), 1, qf);
    197  1.1  cgd 		if (feof(qf))
    198  1.1  cgd 			break;
    199  1.1  cgd 		if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
    200  1.1  cgd 			continue;
    201  1.1  cgd 		if ((fup = lookup(id, type)) == 0)
    202  1.1  cgd 			fup = addid(id, type, (char *)0);
    203  1.1  cgd 		fup->fu_dqblk = dqbuf;
    204  1.1  cgd 	}
    205  1.1  cgd 	fclose(qf);
    206  1.1  cgd 	printf("                        Block limits               File limits\n");
    207  1.1  cgd 	printf("User            used    soft    hard  grace    used  soft  hard  grace\n");
    208  1.1  cgd 	for (id = 0; id <= highid[type]; id++) {
    209  1.1  cgd 		fup = lookup(id, type);
    210  1.1  cgd 		if (fup == 0)
    211  1.1  cgd 			continue;
    212  1.1  cgd 		if (fup->fu_dqblk.dqb_curinodes == 0 &&
    213  1.1  cgd 		    fup->fu_dqblk.dqb_curblocks == 0)
    214  1.1  cgd 			continue;
    215  1.1  cgd 		printf("%-10s", fup->fu_name);
    216  1.1  cgd 		printf("%c%c%8d%8d%8d%7s",
    217  1.1  cgd 			fup->fu_dqblk.dqb_bsoftlimit &&
    218  1.1  cgd 			    fup->fu_dqblk.dqb_curblocks >=
    219  1.1  cgd 			    fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-',
    220  1.1  cgd 			fup->fu_dqblk.dqb_isoftlimit &&
    221  1.1  cgd 			    fup->fu_dqblk.dqb_curinodes >=
    222  1.1  cgd 			    fup->fu_dqblk.dqb_isoftlimit ? '+' : '-',
    223  1.1  cgd 			dbtob(fup->fu_dqblk.dqb_curblocks) / 1024,
    224  1.1  cgd 			dbtob(fup->fu_dqblk.dqb_bsoftlimit) / 1024,
    225  1.1  cgd 			dbtob(fup->fu_dqblk.dqb_bhardlimit) / 1024,
    226  1.1  cgd 			fup->fu_dqblk.dqb_bsoftlimit &&
    227  1.1  cgd 			    fup->fu_dqblk.dqb_curblocks >=
    228  1.1  cgd 			    fup->fu_dqblk.dqb_bsoftlimit ?
    229  1.1  cgd 			    timeprt(fup->fu_dqblk.dqb_btime) : "");
    230  1.1  cgd 		printf("  %6d%6d%6d%7s\n",
    231  1.1  cgd 			fup->fu_dqblk.dqb_curinodes,
    232  1.1  cgd 			fup->fu_dqblk.dqb_isoftlimit,
    233  1.1  cgd 			fup->fu_dqblk.dqb_ihardlimit,
    234  1.1  cgd 			fup->fu_dqblk.dqb_isoftlimit &&
    235  1.1  cgd 			    fup->fu_dqblk.dqb_curinodes >=
    236  1.1  cgd 			    fup->fu_dqblk.dqb_isoftlimit ?
    237  1.1  cgd 			    timeprt(fup->fu_dqblk.dqb_itime) : "");
    238  1.1  cgd 		fup->fu_dqblk = zerodqblk;
    239  1.1  cgd 	}
    240  1.1  cgd 	return (0);
    241  1.1  cgd }
    242  1.1  cgd 
    243  1.1  cgd /*
    244  1.1  cgd  * Check to see if target appears in list of size cnt.
    245  1.1  cgd  */
    246  1.1  cgd oneof(target, list, cnt)
    247  1.1  cgd 	register char *target, *list[];
    248  1.1  cgd 	int cnt;
    249  1.1  cgd {
    250  1.1  cgd 	register int i;
    251  1.1  cgd 
    252  1.1  cgd 	for (i = 0; i < cnt; i++)
    253  1.1  cgd 		if (strcmp(target, list[i]) == 0)
    254  1.1  cgd 			return (i);
    255  1.1  cgd 	return (-1);
    256  1.1  cgd }
    257  1.1  cgd 
    258  1.1  cgd /*
    259  1.1  cgd  * Check to see if a particular quota is to be enabled.
    260  1.1  cgd  */
    261  1.1  cgd hasquota(fs, type, qfnamep)
    262  1.1  cgd 	register struct fstab *fs;
    263  1.1  cgd 	int type;
    264  1.1  cgd 	char **qfnamep;
    265  1.1  cgd {
    266  1.1  cgd 	register char *opt;
    267  1.1  cgd 	char *cp, *index(), *strtok();
    268  1.1  cgd 	static char initname, usrname[100], grpname[100];
    269  1.1  cgd 	static char buf[BUFSIZ];
    270  1.1  cgd 
    271  1.1  cgd 	if (!initname) {
    272  1.1  cgd 		sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
    273  1.1  cgd 		sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
    274  1.1  cgd 		initname = 1;
    275  1.1  cgd 	}
    276  1.1  cgd 	strcpy(buf, fs->fs_mntops);
    277  1.1  cgd 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
    278  1.1  cgd 		if (cp = index(opt, '='))
    279  1.1  cgd 			*cp++ = '\0';
    280  1.1  cgd 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
    281  1.1  cgd 			break;
    282  1.1  cgd 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
    283  1.1  cgd 			break;
    284  1.1  cgd 	}
    285  1.1  cgd 	if (!opt)
    286  1.1  cgd 		return (0);
    287  1.1  cgd 	if (cp) {
    288  1.1  cgd 		*qfnamep = cp;
    289  1.1  cgd 		return (1);
    290  1.1  cgd 	}
    291  1.1  cgd 	(void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
    292  1.1  cgd 	*qfnamep = buf;
    293  1.1  cgd 	return (1);
    294  1.1  cgd }
    295  1.1  cgd 
    296  1.1  cgd /*
    297  1.1  cgd  * Routines to manage the file usage table.
    298  1.1  cgd  *
    299  1.1  cgd  * Lookup an id of a specific type.
    300  1.1  cgd  */
    301  1.1  cgd struct fileusage *
    302  1.1  cgd lookup(id, type)
    303  1.1  cgd 	u_long id;
    304  1.1  cgd 	int type;
    305  1.1  cgd {
    306  1.1  cgd 	register struct fileusage *fup;
    307  1.1  cgd 
    308  1.1  cgd 	for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
    309  1.1  cgd 		if (fup->fu_id == id)
    310  1.1  cgd 			return (fup);
    311  1.1  cgd 	return ((struct fileusage *)0);
    312  1.1  cgd }
    313  1.1  cgd 
    314  1.1  cgd /*
    315  1.1  cgd  * Add a new file usage id if it does not already exist.
    316  1.1  cgd  */
    317  1.1  cgd struct fileusage *
    318  1.1  cgd addid(id, type, name)
    319  1.1  cgd 	u_long id;
    320  1.1  cgd 	int type;
    321  1.1  cgd 	char *name;
    322  1.1  cgd {
    323  1.1  cgd 	struct fileusage *fup, **fhp;
    324  1.1  cgd 	int len;
    325  1.1  cgd 	extern char *calloc();
    326  1.1  cgd 
    327  1.1  cgd 	if (fup = lookup(id, type))
    328  1.1  cgd 		return (fup);
    329  1.1  cgd 	if (name)
    330  1.1  cgd 		len = strlen(name);
    331  1.1  cgd 	else
    332  1.1  cgd 		len = 10;
    333  1.1  cgd 	if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL) {
    334  1.1  cgd 		fprintf(stderr, "out of memory for fileusage structures\n");
    335  1.1  cgd 		exit(1);
    336  1.1  cgd 	}
    337  1.1  cgd 	fhp = &fuhead[type][id & (FUHASH - 1)];
    338  1.1  cgd 	fup->fu_next = *fhp;
    339  1.1  cgd 	*fhp = fup;
    340  1.1  cgd 	fup->fu_id = id;
    341  1.1  cgd 	if (id > highid[type])
    342  1.1  cgd 		highid[type] = id;
    343  1.1  cgd 	if (name) {
    344  1.1  cgd 		bcopy(name, fup->fu_name, len + 1);
    345  1.1  cgd 	} else {
    346  1.1  cgd 		sprintf(fup->fu_name, "%u", id);
    347  1.1  cgd 	}
    348  1.1  cgd 	return (fup);
    349  1.1  cgd }
    350  1.1  cgd 
    351  1.1  cgd /*
    352  1.1  cgd  * Calculate the grace period and return a printable string for it.
    353  1.1  cgd  */
    354  1.1  cgd char *
    355  1.1  cgd timeprt(seconds)
    356  1.1  cgd 	time_t seconds;
    357  1.1  cgd {
    358  1.1  cgd 	time_t hours, minutes;
    359  1.1  cgd 	static char buf[20];
    360  1.1  cgd 	static time_t now;
    361  1.1  cgd 
    362  1.1  cgd 	if (now == 0)
    363  1.1  cgd 		time(&now);
    364  1.1  cgd 	if (now > seconds)
    365  1.1  cgd 		return ("none");
    366  1.1  cgd 	seconds -= now;
    367  1.1  cgd 	minutes = (seconds + 30) / 60;
    368  1.1  cgd 	hours = (minutes + 30) / 60;
    369  1.1  cgd 	if (hours >= 36) {
    370  1.1  cgd 		sprintf(buf, "%ddays", (hours + 12) / 24);
    371  1.1  cgd 		return (buf);
    372  1.1  cgd 	}
    373  1.1  cgd 	if (minutes >= 60) {
    374  1.1  cgd 		sprintf(buf, "%2d:%d", minutes / 60, minutes % 60);
    375  1.1  cgd 		return (buf);
    376  1.1  cgd 	}
    377  1.1  cgd 	sprintf(buf, "%2d", minutes);
    378  1.1  cgd 	return (buf);
    379  1.1  cgd }
    380