157429Smarkm/* 257429Smarkm * Copyright (c) 1980, 1990, 1993 357429Smarkm * The Regents of the University of California. All rights reserved. 457429Smarkm * 557429Smarkm * This code is derived from software contributed to Berkeley by 660573Skris * Robert Elz at The University of Melbourne. 765668Skris * 865668Skris * Redistribution and use in source and binary forms, with or without 965668Skris * modification, are permitted provided that the following conditions 1065668Skris * are met: 1165668Skris * 1. Redistributions of source code must retain the above copyright 1257429Smarkm * notice, this list of conditions and the following disclaimer. 1357429Smarkm * 2. Redistributions in binary form must reproduce the above copyright 1457429Smarkm * notice, this list of conditions and the following disclaimer in the 15120113Snectar * documentation and/or other materials provided with the distribution. 1657429Smarkm * 4. Neither the name of the University nor the names of its contributors 1757429Smarkm * may be used to endorse or promote products derived from this software 1857429Smarkm * without specific prior written permission. 1976259Sgreen * 2057429Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2157429Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2257429Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2360573Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2457429Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2557429Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26120489Sjoe * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27120489Sjoe * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28120489Sjoe * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29120489Sjoe * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30120489Sjoe * SUCH DAMAGE. 3157429Smarkm */ 3257429Smarkm 3357429Smarkm#if 0 3457429Smarkm#ifndef lint 3557429Smarkmstatic const char copyright[] = 3657429Smarkm"@(#) Copyright (c) 1980, 1990, 1993\n\ 3760573Skris The Regents of the University of California. All rights reserved.\n"; 3857429Smarkm#endif /* not lint */ 3957429Smarkm 40120489Sjoe#ifndef lint 41120489Sjoestatic char sccsid[] = "@(#)repquota.c 8.1 (Berkeley) 6/6/93"; 42120489Sjoe#endif /* not lint */ 43120489Sjoe#endif 4457429Smarkm#include <sys/cdefs.h> 4557429Smarkm__FBSDID("$FreeBSD: releng/11.0/usr.sbin/repquota/repquota.c 241737 2012-10-19 14:49:42Z ed $"); 4657429Smarkm 4757429Smarkm/* 4857429Smarkm * Quota report 4957429Smarkm */ 5057429Smarkm#include <sys/param.h> 5160573Skris#include <sys/mount.h> 5257429Smarkm 5357429Smarkm#include <ufs/ufs/quota.h> 5457429Smarkm 5557429Smarkm#include <err.h> 5657429Smarkm#include <errno.h> 5757429Smarkm#include <fcntl.h> 5857429Smarkm#include <fstab.h> 5957429Smarkm#include <grp.h> 6060573Skris#include <libutil.h> 6192555Sdes#include <pwd.h> 6257429Smarkm#include <stdint.h> 6392555Sdes#include <stdio.h> 6492555Sdes#include <stdlib.h> 6592555Sdes#include <string.h> 6657429Smarkm#include <time.h> 6757429Smarkm#include <unistd.h> 6857429Smarkm 6957429Smarkm/* Let's be paranoid about block size */ 7057429Smarkm#if 10 > DEV_BSHIFT 7157429Smarkm#define dbtokb(db) \ 7257429Smarkm ((off_t)(db) >> (10-DEV_BSHIFT)) 7357429Smarkm#elif 10 < DEV_BSHIFT 7492555Sdes#define dbtokb(db) \ 7592555Sdes ((off_t)(db) << (DEV_BSHIFT-10)) 7657429Smarkm#else 77120113Snectar#define dbtokb(db) (db) 7892555Sdes#endif 7992555Sdes 8099060Sdes#define max(a,b) ((a) >= (b) ? (a) : (b)) 8199060Sdes 8299060Sdesstatic const char *qfextension[] = INITQFNAMES; 8357429Smarkm 8457429Smarkmstruct fileusage { 8557429Smarkm struct fileusage *fu_next; 8657429Smarkm u_long fu_id; 8757429Smarkm char fu_name[1]; 8857429Smarkm /* actually bigger */ 8957429Smarkm}; 9057429Smarkm#define FUHASH 1024 /* must be power of two */ 9192555Sdesstatic struct fileusage *fuhead[MAXQUOTAS][FUHASH]; 9257429Smarkmstatic struct fileusage *lookup(u_long, int); 9392555Sdesstatic struct fileusage *addid(u_long, int, char *); 9457429Smarkmstatic u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */ 9557429Smarkm 9657429Smarkmstatic int vflag; /* verbose */ 9757429Smarkmstatic int aflag; /* all filesystems */ 9857429Smarkmstatic int nflag; /* display user/group by id */ 9957429Smarkmstatic int hflag; /* display in human readable format */ 10057429Smarkm 10157429Smarkmint oneof(char *, char *[], int); 10257429Smarkmint repquota(struct fstab *, int); 10357429Smarkmchar *timeprt(time_t); 10457429Smarkmstatic void prthumanval(int64_t bytes); 10557429Smarkmstatic void usage(void); 10657429Smarkm 107120113Snectarint 108120113Snectarmain(int argc, char *argv[]) 109120113Snectar{ 11099060Sdes struct fstab *fs; 111120113Snectar struct passwd *pw; 112120113Snectar struct group *gr; 113120113Snectar int ch, gflag = 0, uflag = 0, errs = 0; 11457429Smarkm long i, argnum, done = 0; 11592555Sdes 11657429Smarkm while ((ch = getopt(argc, argv, "aghnuv")) != -1) { 11757429Smarkm switch(ch) { 11857429Smarkm case 'a': 11957429Smarkm aflag++; 12076259Sgreen break; 12157429Smarkm case 'g': 12257429Smarkm gflag++; 12357429Smarkm break; 12457429Smarkm case 'h': 12557429Smarkm hflag++; 12657429Smarkm break; 12757429Smarkm case 'n': 12860573Skris nflag++; 12992555Sdes break; 13057429Smarkm case 'u': 13157429Smarkm uflag++; 13276259Sgreen break; 13376259Sgreen case 'v': 13457429Smarkm vflag++; 13557429Smarkm break; 13657429Smarkm default: 13757429Smarkm usage(); 13857429Smarkm } 13957429Smarkm } 14060573Skris argc -= optind; 14176259Sgreen argv += optind; 14257429Smarkm if (argc == 0 && !aflag) 14357429Smarkm usage(); 14460573Skris if (!gflag && !uflag) { 14557429Smarkm if (aflag) 14657429Smarkm gflag++; 14757429Smarkm uflag++; 14857429Smarkm } 14957429Smarkm if (gflag && !nflag) { 15060573Skris setgrent(); 15176259Sgreen while ((gr = getgrent()) != 0) 15257429Smarkm (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name); 15357429Smarkm endgrent(); 15460573Skris } 15557429Smarkm if (uflag && !nflag) { 15657429Smarkm setpwent(); 15757429Smarkm while ((pw = getpwent()) != 0) 15857429Smarkm (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name); 15957429Smarkm endpwent(); 16092555Sdes } 16157429Smarkm setfsent(); 16257429Smarkm while ((fs = getfsent()) != NULL) { 16357429Smarkm if (strcmp(fs->fs_vfstype, "ufs")) 16457429Smarkm continue; 16557429Smarkm if (aflag) { 16657429Smarkm if (gflag) 16757429Smarkm errs += repquota(fs, GRPQUOTA); 16860573Skris if (uflag) 16957429Smarkm errs += repquota(fs, USRQUOTA); 17057429Smarkm continue; 17157429Smarkm } 17292555Sdes if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 || 17357429Smarkm (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) { 17476259Sgreen done |= 1 << argnum; 17576259Sgreen if (gflag) 17676259Sgreen errs += repquota(fs, GRPQUOTA); 17776259Sgreen if (uflag) 17876259Sgreen errs += repquota(fs, USRQUOTA); 17976259Sgreen } 18076259Sgreen } 18176259Sgreen endfsent(); 18257429Smarkm for (i = 0; i < argc; i++) 183 if ((done & (1 << i)) == 0) 184 warnx("%s not found in fstab", argv[i]); 185 exit(errs); 186} 187 188static void 189usage(void) 190{ 191 fprintf(stderr, "%s\n%s\n", 192 "usage: repquota [-h] [-v] [-g] [-n] [-u] -a", 193 " repquota [-h] [-v] [-g] [-n] [-u] filesystem ..."); 194 exit(1); 195} 196 197int 198repquota(struct fstab *fs, int type) 199{ 200 struct fileusage *fup; 201 struct quotafile *qf; 202 u_long id, maxid; 203 struct dqblk dqbuf; 204 static int multiple = 0; 205 206 if ((qf = quota_open(fs, type, O_RDONLY)) == NULL) { 207 if (vflag && !aflag) { 208 if (multiple++) 209 printf("\n"); 210 fprintf(stdout, "*** No %s quotas on %s (%s)\n", 211 qfextension[type], fs->fs_file, fs->fs_spec); 212 return(1); 213 } 214 return(0); 215 } 216 if (multiple++) 217 printf("\n"); 218 if (vflag) 219 fprintf(stdout, "*** Report for %s quotas on %s (%s)\n", 220 qfextension[type], fs->fs_file, fs->fs_spec); 221 printf("%*s Block limits File limits\n", 222 max(MAXLOGNAME - 1, 10), " "); 223 printf("User%*s used soft hard grace used soft hard grace\n", 224 max(MAXLOGNAME - 1, 10), " "); 225 maxid = quota_maxid(qf); 226 for (id = 0; id <= maxid; id++) { 227 if (quota_read(qf, &dqbuf, id) != 0) 228 break; 229 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0) 230 continue; 231 if ((fup = lookup(id, type)) == 0) 232 fup = addid(id, type, (char *)0); 233 printf("%-*s ", max(MAXLOGNAME - 1, 10), fup->fu_name); 234 printf("%c%c", 235 dqbuf.dqb_bsoftlimit && 236 dqbuf.dqb_curblocks >= 237 dqbuf.dqb_bsoftlimit ? '+' : '-', 238 dqbuf.dqb_isoftlimit && 239 dqbuf.dqb_curinodes >= 240 dqbuf.dqb_isoftlimit ? '+' : '-'); 241 prthumanval(dqbuf.dqb_curblocks); 242 prthumanval(dqbuf.dqb_bsoftlimit); 243 prthumanval(dqbuf.dqb_bhardlimit); 244 printf(" %6s", 245 dqbuf.dqb_bsoftlimit && 246 dqbuf.dqb_curblocks >= 247 dqbuf.dqb_bsoftlimit ? 248 timeprt(dqbuf.dqb_btime) : "-"); 249 printf(" %7ju %7ju %7ju %6s\n", 250 (uintmax_t)dqbuf.dqb_curinodes, 251 (uintmax_t)dqbuf.dqb_isoftlimit, 252 (uintmax_t)dqbuf.dqb_ihardlimit, 253 dqbuf.dqb_isoftlimit && 254 dqbuf.dqb_curinodes >= 255 dqbuf.dqb_isoftlimit ? 256 timeprt(dqbuf.dqb_itime) : "-"); 257 } 258 quota_close(qf); 259 return (0); 260} 261 262static void 263prthumanval(int64_t blocks) 264{ 265 char buf[7]; 266 int flags; 267 268 if (!hflag) { 269 printf(" %6ju", (uintmax_t)dbtokb(blocks)); 270 return; 271 } 272 flags = HN_NOSPACE | HN_DECIMAL; 273 if (blocks != 0) 274 flags |= HN_B; 275 humanize_number(buf, sizeof(buf) - (blocks < 0 ? 0 : 1), 276 dbtob(blocks), "", HN_AUTOSCALE, flags); 277 (void)printf("%7s", buf); 278} 279 280/* 281 * Check to see if target appears in list of size cnt. 282 */ 283int 284oneof(char *target, char *list[], int cnt) 285{ 286 int i; 287 288 for (i = 0; i < cnt; i++) 289 if (strcmp(target, list[i]) == 0) 290 return (i); 291 return (-1); 292} 293 294/* 295 * Routines to manage the file usage table. 296 * 297 * Lookup an id of a specific type. 298 */ 299struct fileusage * 300lookup(u_long id, int type) 301{ 302 struct fileusage *fup; 303 304 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next) 305 if (fup->fu_id == id) 306 return (fup); 307 return ((struct fileusage *)0); 308} 309 310/* 311 * Add a new file usage id if it does not already exist. 312 */ 313struct fileusage * 314addid(u_long id, int type, char *name) 315{ 316 struct fileusage *fup, **fhp; 317 int len; 318 319 if ((fup = lookup(id, type))) 320 return (fup); 321 if (name) 322 len = strlen(name); 323 else 324 len = 10; 325 if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL) 326 errx(1, "out of memory for fileusage structures"); 327 fhp = &fuhead[type][id & (FUHASH - 1)]; 328 fup->fu_next = *fhp; 329 *fhp = fup; 330 fup->fu_id = id; 331 if (id > highid[type]) 332 highid[type] = id; 333 if (name) { 334 bcopy(name, fup->fu_name, len + 1); 335 } else { 336 sprintf(fup->fu_name, "%lu", id); 337 } 338 return (fup); 339} 340 341/* 342 * Calculate the grace period and return a printable string for it. 343 */ 344char * 345timeprt(time_t seconds) 346{ 347 time_t hours, minutes; 348 static char buf[20]; 349 static time_t now; 350 351 if (now == 0) 352 time(&now); 353 if (now > seconds) { 354 strlcpy(buf, "none", sizeof (buf)); 355 return (buf); 356 } 357 seconds -= now; 358 minutes = (seconds + 30) / 60; 359 hours = (minutes + 30) / 60; 360 if (hours >= 36) { 361 sprintf(buf, "%lddays", (long)(hours + 12) / 24); 362 return (buf); 363 } 364 if (minutes >= 60) { 365 sprintf(buf, "%2ld:%ld", (long)minutes / 60, 366 (long)minutes % 60); 367 return (buf); 368 } 369 sprintf(buf, "%2ld", (long)minutes); 370 return (buf); 371} 372