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