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