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