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