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