freebsd-skq/usr.sbin/repquota/repquota.c

372 lines
8.9 KiB
C
Raw Normal View History

1994-05-26 05:23:31 +00:00
/*
* Copyright (c) 1980, 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Robert Elz at The University of Melbourne.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#if 0
1994-05-26 05:23:31 +00:00
#ifndef lint
1997-10-13 11:05:07 +00:00
static const char copyright[] =
1994-05-26 05:23:31 +00:00
"@(#) Copyright (c) 1980, 1990, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char sccsid[] = "@(#)repquota.c 8.1 (Berkeley) 6/6/93";
#endif /* not lint */
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
1994-05-26 05:23:31 +00:00
/*
* Quota report
*/
#include <sys/param.h>
#include <sys/mount.h>
2010-03-04 10:57:52 +00:00
1994-05-26 05:23:31 +00:00
#include <ufs/ufs/quota.h>
2010-03-04 10:57:52 +00:00
1997-10-13 11:05:07 +00:00
#include <err.h>
#include <errno.h>
#include <fcntl.h>
1994-05-26 05:23:31 +00:00
#include <fstab.h>
#include <grp.h>
#include <libutil.h>
1997-10-13 11:05:07 +00:00
#include <pwd.h>
2010-03-04 10:57:52 +00:00
#include <stdint.h>
1994-05-26 05:23:31 +00:00
#include <stdio.h>
1997-10-13 11:05:07 +00:00
#include <stdlib.h>
#include <string.h>
#include <time.h>
1997-10-13 11:05:07 +00:00
#include <unistd.h>
1998-06-14 22:56:31 +00:00
/* Let's be paranoid about block size */
#if 10 > DEV_BSHIFT
#define dbtokb(db) \
((off_t)(db) >> (10-DEV_BSHIFT))
#elif 10 < DEV_BSHIFT
#define dbtokb(db) \
((off_t)(db) << (DEV_BSHIFT-10))
#else
#define dbtokb(db) (db)
#endif
1998-06-14 22:56:31 +00:00
#define max(a,b) ((a) >= (b) ? (a) : (b))
1994-05-26 05:23:31 +00:00
static const char *qfextension[] = INITQFNAMES;
1994-05-26 05:23:31 +00:00
struct fileusage {
struct fileusage *fu_next;
u_long fu_id;
char fu_name[1];
/* actually bigger */
};
#define FUHASH 1024 /* must be power of two */
static struct fileusage *fuhead[MAXQUOTAS][FUHASH];
static struct fileusage *lookup(u_long, int);
static struct fileusage *addid(u_long, int, char *);
static u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
1994-05-26 05:23:31 +00:00
static int vflag; /* verbose */
static int aflag; /* all filesystems */
static int nflag; /* display user/group by id */
static int hflag; /* display in human readable format */
1994-05-26 05:23:31 +00:00
2002-07-11 21:26:41 +00:00
int oneof(char *, char *[], int);
int repquota(struct fstab *, int);
2002-07-11 21:26:41 +00:00
char *timeprt(time_t);
static void prthumanval(int64_t bytes);
2002-07-11 21:26:41 +00:00
static void usage(void);
1997-10-13 11:05:07 +00:00
int
main(int argc, char *argv[])
1994-05-26 05:23:31 +00:00
{
struct fstab *fs;
struct passwd *pw;
struct group *gr;
int ch, gflag = 0, uflag = 0, errs = 0;
1994-05-26 05:23:31 +00:00
long i, argnum, done = 0;
while ((ch = getopt(argc, argv, "aghnuv")) != -1) {
1994-05-26 05:23:31 +00:00
switch(ch) {
case 'a':
aflag++;
break;
case 'g':
gflag++;
break;
case 'h':
hflag++;
break;
case 'n':
nflag++;
break;
1994-05-26 05:23:31 +00:00
case 'u':
uflag++;
break;
case 'v':
vflag++;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc == 0 && !aflag)
usage();
if (!gflag && !uflag) {
if (aflag)
gflag++;
uflag++;
}
if (gflag && !nflag) {
1994-05-26 05:23:31 +00:00
setgrent();
while ((gr = getgrent()) != 0)
(void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
endgrent();
}
if (uflag && !nflag) {
1994-05-26 05:23:31 +00:00
setpwent();
while ((pw = getpwent()) != 0)
(void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
endpwent();
}
setfsent();
while ((fs = getfsent()) != NULL) {
if (strcmp(fs->fs_vfstype, "ufs"))
continue;
if (aflag) {
if (gflag)
errs += repquota(fs, GRPQUOTA);
if (uflag)
errs += repquota(fs, USRQUOTA);
1994-05-26 05:23:31 +00:00
continue;
}
if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
(argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
done |= 1 << argnum;
if (gflag)
errs += repquota(fs, GRPQUOTA);
if (uflag)
errs += repquota(fs, USRQUOTA);
1994-05-26 05:23:31 +00:00
}
}
endfsent();
for (i = 0; i < argc; i++)
if ((done & (1 << i)) == 0)
1997-10-13 11:05:07 +00:00
warnx("%s not found in fstab", argv[i]);
1994-05-26 05:23:31 +00:00
exit(errs);
}
1997-10-13 11:05:07 +00:00
static void
usage(void)
1994-05-26 05:23:31 +00:00
{
1997-10-13 11:05:07 +00:00
fprintf(stderr, "%s\n%s\n",
"usage: repquota [-h] [-v] [-g] [-n] [-u] -a",
" repquota [-h] [-v] [-g] [-n] [-u] filesystem ...");
1994-05-26 05:23:31 +00:00
exit(1);
}
1997-10-13 11:05:07 +00:00
int
repquota(struct fstab *fs, int type)
1994-05-26 05:23:31 +00:00
{
struct fileusage *fup;
struct quotafile *qf;
u_long id, maxid;
1994-05-26 05:23:31 +00:00
struct dqblk dqbuf;
static int multiple = 0;
if ((qf = quota_open(fs, type, O_RDONLY)) == NULL) {
if (vflag && !aflag) {
if (multiple++)
printf("\n");
fprintf(stdout, "*** No %s quotas on %s (%s)\n",
qfextension[type], fs->fs_file, fs->fs_spec);
return(1);
}
return(0);
1994-05-26 05:23:31 +00:00
}
if (multiple++)
printf("\n");
if (vflag)
fprintf(stdout, "*** Report for %s quotas on %s (%s)\n",
qfextension[type], fs->fs_file, fs->fs_spec);
printf("%*s Block limits File limits\n",
2010-03-04 10:57:52 +00:00
max(MAXLOGNAME - 1, 10), " ");
printf("User%*s used soft hard grace used soft hard grace\n",
2010-03-04 10:57:52 +00:00
max(MAXLOGNAME - 1, 10), " ");
maxid = quota_maxid(qf);
for (id = 0; id <= maxid; id++) {
if (quota_read(qf, &dqbuf, id) != 0)
1994-05-26 05:23:31 +00:00
break;
if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0)
continue;
if ((fup = lookup(id, type)) == 0)
fup = addid(id, type, (char *)0);
2010-03-04 10:57:52 +00:00
printf("%-*s ", max(MAXLOGNAME - 1, 10), fup->fu_name);
printf("%c%c",
2010-03-04 10:57:52 +00:00
dqbuf.dqb_bsoftlimit &&
dqbuf.dqb_curblocks >=
dqbuf.dqb_bsoftlimit ? '+' : '-',
dqbuf.dqb_isoftlimit &&
dqbuf.dqb_curinodes >=
dqbuf.dqb_isoftlimit ? '+' : '-');
prthumanval(dqbuf.dqb_curblocks);
prthumanval(dqbuf.dqb_bsoftlimit);
prthumanval(dqbuf.dqb_bhardlimit);
printf(" %6s",
2010-03-04 10:57:52 +00:00
dqbuf.dqb_bsoftlimit &&
dqbuf.dqb_curblocks >=
dqbuf.dqb_bsoftlimit ?
timeprt(dqbuf.dqb_btime) : "-");
printf(" %7ju %7ju %7ju %6s\n",
(uintmax_t)dqbuf.dqb_curinodes,
(uintmax_t)dqbuf.dqb_isoftlimit,
(uintmax_t)dqbuf.dqb_ihardlimit,
dqbuf.dqb_isoftlimit &&
dqbuf.dqb_curinodes >=
dqbuf.dqb_isoftlimit ?
timeprt(dqbuf.dqb_itime) : "-");
1994-05-26 05:23:31 +00:00
}
quota_close(qf);
1994-05-26 05:23:31 +00:00
return (0);
}
static void
prthumanval(int64_t blocks)
{
char buf[7];
int flags;
if (!hflag) {
2010-03-04 10:57:52 +00:00
printf(" %6ju", (uintmax_t)dbtokb(blocks));
return;
}
flags = HN_NOSPACE | HN_DECIMAL;
if (blocks != 0)
flags |= HN_B;
humanize_number(buf, sizeof(buf) - (blocks < 0 ? 0 : 1),
dbtob(blocks), "", HN_AUTOSCALE, flags);
(void)printf("%7s", buf);
}
1994-05-26 05:23:31 +00:00
/*
* Check to see if target appears in list of size cnt.
*/
1997-10-13 11:05:07 +00:00
int
oneof(char *target, char *list[], int cnt)
1994-05-26 05:23:31 +00:00
{
int i;
1994-05-26 05:23:31 +00:00
for (i = 0; i < cnt; i++)
if (strcmp(target, list[i]) == 0)
return (i);
return (-1);
}
/*
* Routines to manage the file usage table.
*
* Lookup an id of a specific type.
*/
struct fileusage *
lookup(u_long id, int type)
1994-05-26 05:23:31 +00:00
{
struct fileusage *fup;
1994-05-26 05:23:31 +00:00
for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
if (fup->fu_id == id)
return (fup);
return ((struct fileusage *)0);
}
/*
* Add a new file usage id if it does not already exist.
*/
struct fileusage *
addid(u_long id, int type, char *name)
1994-05-26 05:23:31 +00:00
{
struct fileusage *fup, **fhp;
int len;
1997-10-13 11:05:07 +00:00
if ((fup = lookup(id, type)))
1994-05-26 05:23:31 +00:00
return (fup);
if (name)
len = strlen(name);
else
len = 10;
1997-10-13 11:05:07 +00:00
if ((fup = (struct fileusage *)calloc(1, sizeof(*fup) + len)) == NULL)
errx(1, "out of memory for fileusage structures");
1994-05-26 05:23:31 +00:00
fhp = &fuhead[type][id & (FUHASH - 1)];
fup->fu_next = *fhp;
*fhp = fup;
fup->fu_id = id;
if (id > highid[type])
highid[type] = id;
if (name) {
bcopy(name, fup->fu_name, len + 1);
} else {
sprintf(fup->fu_name, "%lu", id);
1994-05-26 05:23:31 +00:00
}
return (fup);
}
/*
* Calculate the grace period and return a printable string for it.
*/
char *
timeprt(time_t seconds)
1994-05-26 05:23:31 +00:00
{
time_t hours, minutes;
static char buf[20];
static time_t now;
if (now == 0)
time(&now);
if (now > seconds) {
strlcpy(buf, "none", sizeof (buf));
return (buf);
}
1994-05-26 05:23:31 +00:00
seconds -= now;
minutes = (seconds + 30) / 60;
hours = (minutes + 30) / 60;
if (hours >= 36) {
sprintf(buf, "%lddays", (long)(hours + 12) / 24);
1994-05-26 05:23:31 +00:00
return (buf);
}
if (minutes >= 60) {
sprintf(buf, "%2ld:%ld", (long)minutes / 60,
(long)minutes % 60);
1994-05-26 05:23:31 +00:00
return (buf);
}
sprintf(buf, "%2ld", (long)minutes);
1994-05-26 05:23:31 +00:00
return (buf);
}