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