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