repquota.c revision 180187
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 35static 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 41static char sccsid[] = "@(#)repquota.c 8.1 (Berkeley) 6/6/93"; 42#endif /* not lint */ 43#endif 44#include <sys/cdefs.h> 45__FBSDID("$FreeBSD: head/usr.sbin/repquota/repquota.c 180187 2008-07-02 15:51:59Z des $"); 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 78const char *qfname = QUOTAFILENAME; 79const char *qfextension[] = INITQFNAMES; 80 81struct 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 */ 89struct fileusage *fuhead[MAXQUOTAS][FUHASH]; 90struct fileusage *lookup(u_long, int); 91struct fileusage *addid(u_long, int, char *); 92u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */ 93 94int vflag; /* verbose */ 95int aflag; /* all filesystems */ 96int nflag; /* display user/group by id */ 97 98int hasquota(struct fstab *, int, char **); 99int oneof(char *, char *[], int); 100int repquota(struct fstab *, int, char *); 101char *timeprt(time_t); 102static void usage(void); 103 104int 105main(int argc, char *argv[]) 106{ 107 struct fstab *fs; 108 struct passwd *pw; 109 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 183static void 184usage(void) 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 192int 193repquota(struct fstab *fs, int type, char *qfpathname) 194{ 195 struct fileusage *fup; 196 FILE *qf; 197 u_long id; 198 struct dqblk dqbuf; 199 static struct dqblk zerodqblk; 200 static int warned = 0; 201 static int multiple = 0; 202 203 if (quotactl(fs->fs_file, QCMD(Q_SYNC, type), 0, 0) < 0 && 204 errno == EOPNOTSUPP && !warned && vflag) { 205 warned++; 206 fprintf(stdout, 207 "*** Warning: Quotas are not compiled into this kernel\n"); 208 } 209 if (multiple++) 210 printf("\n"); 211 if (vflag) 212 fprintf(stdout, "*** Report for %s quotas on %s (%s)\n", 213 qfextension[type], fs->fs_file, fs->fs_spec); 214 if ((qf = fopen(qfpathname, "r")) == NULL) { 215 warn("%s", qfpathname); 216 return (1); 217 } 218 for (id = 0; ; id++) { 219 fread(&dqbuf, sizeof(struct dqblk), 1, qf); 220 if (feof(qf)) 221 break; 222 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0) 223 continue; 224 if ((fup = lookup(id, type)) == 0) 225 fup = addid(id, type, (char *)0); 226 fup->fu_dqblk = dqbuf; 227 } 228 fclose(qf); 229 printf("%*s Block limits File limits\n", 230 max(UT_NAMESIZE,10), " "); 231 printf("%s%*s used soft hard grace used soft hard grace\n", 232 type == USRQUOTA ? "User " : "Group", max(UT_NAMESIZE,10), " "); 233 for (id = 0; id <= highid[type]; id++) { 234 fup = lookup(id, type); 235 if (fup == 0) 236 continue; 237 if (fup->fu_dqblk.dqb_curinodes == 0 && 238 fup->fu_dqblk.dqb_curblocks == 0) 239 continue; 240 printf("%-*s ", max(UT_NAMESIZE,10), fup->fu_name); 241 printf("%c%c %8lu %8lu %8lu %6s", 242 fup->fu_dqblk.dqb_bsoftlimit && 243 fup->fu_dqblk.dqb_curblocks >= 244 fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-', 245 fup->fu_dqblk.dqb_isoftlimit && 246 fup->fu_dqblk.dqb_curinodes >= 247 fup->fu_dqblk.dqb_isoftlimit ? '+' : '-', 248 (u_long)(dbtokb(fup->fu_dqblk.dqb_curblocks)), 249 (u_long)(dbtokb(fup->fu_dqblk.dqb_bsoftlimit)), 250 (u_long)(dbtokb(fup->fu_dqblk.dqb_bhardlimit)), 251 fup->fu_dqblk.dqb_bsoftlimit && 252 fup->fu_dqblk.dqb_curblocks >= 253 fup->fu_dqblk.dqb_bsoftlimit ? 254 timeprt(fup->fu_dqblk.dqb_btime) : "-"); 255 printf(" %7lu %7lu %7lu %6s\n", 256 (u_long)fup->fu_dqblk.dqb_curinodes, 257 (u_long)fup->fu_dqblk.dqb_isoftlimit, 258 (u_long)fup->fu_dqblk.dqb_ihardlimit, 259 fup->fu_dqblk.dqb_isoftlimit && 260 fup->fu_dqblk.dqb_curinodes >= 261 fup->fu_dqblk.dqb_isoftlimit ? 262 timeprt(fup->fu_dqblk.dqb_itime) : "-"); 263 fup->fu_dqblk = zerodqblk; 264 } 265 return (0); 266} 267 268/* 269 * Check to see if target appears in list of size cnt. 270 */ 271int 272oneof(char *target, char *list[], int cnt) 273{ 274 int i; 275 276 for (i = 0; i < cnt; i++) 277 if (strcmp(target, list[i]) == 0) 278 return (i); 279 return (-1); 280} 281 282/* 283 * Check to see if a particular quota is to be enabled. 284 */ 285int 286hasquota(struct fstab *fs, int type, char **qfnamep) 287{ 288 char *opt; 289 char *cp; 290 struct statfs sfb; 291 static char initname, usrname[100], grpname[100]; 292 static char buf[BUFSIZ]; 293 294 if (!initname) { 295 (void)snprintf(usrname, sizeof(usrname), "%s%s", 296 qfextension[USRQUOTA], qfname); 297 (void)snprintf(grpname, sizeof(grpname), "%s%s", 298 qfextension[GRPQUOTA], qfname); 299 initname = 1; 300 } 301 strcpy(buf, fs->fs_mntops); 302 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 303 if ((cp = index(opt, '='))) 304 *cp++ = '\0'; 305 if (type == USRQUOTA && strcmp(opt, usrname) == 0) 306 break; 307 if (type == GRPQUOTA && strcmp(opt, grpname) == 0) 308 break; 309 } 310 if (!opt) 311 return (0); 312 if (cp) 313 *qfnamep = cp; 314 else { 315 (void)snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file, 316 qfname, qfextension[type]); 317 *qfnamep = buf; 318 } 319 if (statfs(fs->fs_file, &sfb) != 0) { 320 warn("cannot statfs mount point %s", fs->fs_file); 321 return (0); 322 } 323 if (strcmp(fs->fs_file, sfb.f_mntonname)) { 324 warnx("%s not mounted for %s quotas", fs->fs_file, 325 type == USRQUOTA ? "user" : "group"); 326 return (0); 327 } 328 return (1); 329} 330 331/* 332 * Routines to manage the file usage table. 333 * 334 * Lookup an id of a specific type. 335 */ 336struct fileusage * 337lookup(u_long id, int type) 338{ 339 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 */ 350struct fileusage * 351addid(u_long id, int type, char *name) 352{ 353 struct fileusage *fup, **fhp; 354 int len; 355 356 if ((fup = lookup(id, type))) 357 return (fup); 358 if (name) 359 len = strlen(name); 360 else 361 len = 10; 362 if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL) 363 errx(1, "out of memory for fileusage structures"); 364 fhp = &fuhead[type][id & (FUHASH - 1)]; 365 fup->fu_next = *fhp; 366 *fhp = fup; 367 fup->fu_id = id; 368 if (id > highid[type]) 369 highid[type] = id; 370 if (name) { 371 bcopy(name, fup->fu_name, len + 1); 372 } else { 373 sprintf(fup->fu_name, "%lu", id); 374 } 375 return (fup); 376} 377 378/* 379 * Calculate the grace period and return a printable string for it. 380 */ 381char * 382timeprt(time_t seconds) 383{ 384 time_t hours, minutes; 385 static char buf[20]; 386 static time_t now; 387 388 if (now == 0) 389 time(&now); 390 if (now > seconds) { 391 strlcpy(buf, "none", sizeof (buf)); 392 return (buf); 393 } 394 seconds -= now; 395 minutes = (seconds + 30) / 60; 396 hours = (minutes + 30) / 60; 397 if (hours >= 36) { 398 sprintf(buf, "%lddays", (long)(hours + 12) / 24); 399 return (buf); 400 } 401 if (minutes >= 60) { 402 sprintf(buf, "%2ld:%ld", (long)minutes / 60, 403 (long)minutes % 60); 404 return (buf); 405 } 406 sprintf(buf, "%2ld", (long)minutes); 407 return (buf); 408} 409