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