]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/repquota/repquota.c
This commit was generated by cvs2svn to compensate for changes in r156251,
[FreeBSD/FreeBSD.git] / usr.sbin / repquota / repquota.c
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 <ufs/ufs/quota.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <fstab.h>
55 #include <grp.h>
56 #include <pwd.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <time.h>
61 #include <unistd.h>
62 #include <utmp.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         register struct fstab *fs;
107         register struct passwd *pw;
108         register 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()
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(fs, type, qfpathname)
193         register struct fstab *fs;
194         int type;
195         char *qfpathname;
196 {
197         register struct fileusage *fup;
198         FILE *qf;
199         u_long id;
200         struct dqblk dqbuf;
201         static struct dqblk zerodqblk;
202         static int warned = 0;
203         static int multiple = 0;
204
205         if (quotactl(fs->fs_file, QCMD(Q_SYNC, type), 0, 0) < 0 &&
206             errno == EOPNOTSUPP && !warned && vflag) {
207                 warned++;
208                 fprintf(stdout,
209                     "*** Warning: Quotas are not compiled into this kernel\n");
210         }
211         if (multiple++)
212                 printf("\n");
213         if (vflag)
214                 fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
215                     qfextension[type], fs->fs_file, fs->fs_spec);
216         if ((qf = fopen(qfpathname, "r")) == NULL) {
217                 warn("%s", qfpathname);
218                 return (1);
219         }
220         for (id = 0; ; id++) {
221                 fread(&dqbuf, sizeof(struct dqblk), 1, qf);
222                 if (feof(qf))
223                         break;
224                 if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
225                         continue;
226                 if ((fup = lookup(id, type)) == 0)
227                         fup = addid(id, type, (char *)0);
228                 fup->fu_dqblk = dqbuf;
229         }
230         fclose(qf);
231         printf("%*s                 Block  limits                    File  limits\n",
232                 max(UT_NAMESIZE,10), " ");
233         printf("User%*s    used     soft     hard  grace     used    soft    hard  grace\n",
234                 max(UT_NAMESIZE,10), " ");
235         for (id = 0; id <= highid[type]; id++) {
236                 fup = lookup(id, type);
237                 if (fup == 0)
238                         continue;
239                 if (fup->fu_dqblk.dqb_curinodes == 0 &&
240                     fup->fu_dqblk.dqb_curblocks == 0)
241                         continue;
242                 printf("%-*s ", max(UT_NAMESIZE,10), fup->fu_name);
243                 printf("%c%c %8lu %8lu %8lu %6s",
244                         fup->fu_dqblk.dqb_bsoftlimit &&
245                             fup->fu_dqblk.dqb_curblocks >=
246                             fup->fu_dqblk.dqb_bsoftlimit ? '+' : '-',
247                         fup->fu_dqblk.dqb_isoftlimit &&
248                             fup->fu_dqblk.dqb_curinodes >=
249                             fup->fu_dqblk.dqb_isoftlimit ? '+' : '-',
250                         (u_long)(dbtokb(fup->fu_dqblk.dqb_curblocks)),
251                         (u_long)(dbtokb(fup->fu_dqblk.dqb_bsoftlimit)),
252                         (u_long)(dbtokb(fup->fu_dqblk.dqb_bhardlimit)),
253                         fup->fu_dqblk.dqb_bsoftlimit &&
254                             fup->fu_dqblk.dqb_curblocks >=
255                             fup->fu_dqblk.dqb_bsoftlimit ?
256                             timeprt(fup->fu_dqblk.dqb_btime) : "-");
257                 printf("  %7lu %7lu %7lu %6s\n",
258                         (u_long)fup->fu_dqblk.dqb_curinodes,
259                         (u_long)fup->fu_dqblk.dqb_isoftlimit,
260                         (u_long)fup->fu_dqblk.dqb_ihardlimit,
261                         fup->fu_dqblk.dqb_isoftlimit &&
262                             fup->fu_dqblk.dqb_curinodes >=
263                             fup->fu_dqblk.dqb_isoftlimit ?
264                             timeprt(fup->fu_dqblk.dqb_itime) : "-");
265                 fup->fu_dqblk = zerodqblk;
266         }
267         return (0);
268 }
269
270 /*
271  * Check to see if target appears in list of size cnt.
272  */
273 int
274 oneof(target, list, cnt)
275         register char *target, *list[];
276         int cnt;
277 {
278         register int i;
279
280         for (i = 0; i < cnt; i++)
281                 if (strcmp(target, list[i]) == 0)
282                         return (i);
283         return (-1);
284 }
285
286 /*
287  * Check to see if a particular quota is to be enabled.
288  */
289 int
290 hasquota(fs, type, qfnamep)
291         register struct fstab *fs;
292         int type;
293         char **qfnamep;
294 {
295         register char *opt;
296         char *cp;
297         static char initname, usrname[100], grpname[100];
298         static char buf[BUFSIZ];
299
300         if (!initname) {
301                 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
302                 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
303                 initname = 1;
304         }
305         strcpy(buf, fs->fs_mntops);
306         for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
307                 if ((cp = index(opt, '=')))
308                         *cp++ = '\0';
309                 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
310                         break;
311                 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
312                         break;
313         }
314         if (!opt)
315                 return (0);
316         if (cp) {
317                 *qfnamep = cp;
318                 return (1);
319         }
320         (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
321         *qfnamep = buf;
322         return (1);
323 }
324
325 /*
326  * Routines to manage the file usage table.
327  *
328  * Lookup an id of a specific type.
329  */
330 struct fileusage *
331 lookup(id, type)
332         u_long id;
333         int type;
334 {
335         register struct fileusage *fup;
336
337         for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
338                 if (fup->fu_id == id)
339                         return (fup);
340         return ((struct fileusage *)0);
341 }
342
343 /*
344  * Add a new file usage id if it does not already exist.
345  */
346 struct fileusage *
347 addid(id, type, name)
348         u_long id;
349         int type;
350         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(seconds)
382         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 }