* 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.
* %sccs.include.redist.c%
static char copyright
[] =
"@(#) Copyright (c) 1980, 1990, 1993\n\
The Regents of the University of California. All rights reserved.\n";
static char sccsid
[] = "@(#)edquota.c 8.3 (Berkeley) %G%";
#include <ufs/ufs/quota.h>
char *qfname
= QUOTAFILENAME
;
char *qfextension
[] = INITQFNAMES
;
char *quotagroup
= QUOTAGROUP
;
char tmpfil
[] = _PATH_TMP
;
char fsname
[MAXPATHLEN
+ 1];
char qfname
[1]; /* actually longer */
register struct quotause
*qup
, *protoprivs
, *curprivs
;
register long id
, protoid
;
register int quotatype
, tmpfd
;
int tflag
= 0, pflag
= 0;
fprintf(stderr
, "edquota: permission denied\n");
while ((ch
= getopt(argc
, argv
, "ugtp:")) != EOF
) {
if ((protoid
= getentry(protoname
, quotatype
)) == -1)
protoprivs
= getprivs(protoid
, quotatype
);
for (qup
= protoprivs
; qup
; qup
= qup
->next
) {
qup
->dqblk
.dqb_btime
= 0;
qup
->dqblk
.dqb_itime
= 0;
if ((id
= getentry(*argv
++, quotatype
)) < 0)
putprivs(id
, quotatype
, protoprivs
);
fchown(tmpfd
, getuid(), getgid());
protoprivs
= getprivs(0, quotatype
);
if (writetimes(protoprivs
, tmpfd
, quotatype
) == 0)
if (editit(tmpfil
) && readtimes(protoprivs
, tmpfd
))
putprivs(0, quotatype
, protoprivs
);
for ( ; argc
> 0; argc
--, argv
++) {
if ((id
= getentry(*argv
, quotatype
)) == -1)
curprivs
= getprivs(id
, quotatype
);
if (writeprivs(curprivs
, tmpfd
, *argv
, quotatype
) == 0)
if (editit(tmpfil
) && readprivs(curprivs
, tmpfd
))
putprivs(id
, quotatype
, curprivs
);
fprintf(stderr
, "%s%s%s%s",
"Usage: edquota [-u] [-p username] username ...\n",
"\tedquota -g [-p groupname] groupname ...\n",
"\tedquota [-u] -t\n", "\tedquota -g -t\n");
* This routine converts a name for a particular quota type to
* an identifier. This routine must agree with the kernel routine
* getinoquota as to the interpretation of quota types.
getentry(name
, quotatype
)
fprintf(stderr
, "%s: no such user\n", name
);
fprintf(stderr
, "%s: no such group\n", name
);
fprintf(stderr
, "%d: unknown quota type\n", quotatype
);
* Collect the requested quota information.
register struct fstab
*fs
;
register struct quotause
*qup
, *quptail
;
struct quotause
*quphead
;
quphead
= (struct quotause
*)0;
qcmd
= QCMD(Q_GETQUOTA
, quotatype
);
while (fs
= getfsent()) {
if (strcmp(fs
->fs_vfstype
, "ufs"))
if (!hasquota(fs
, quotatype
, &qfpathname
))
qupsize
= sizeof(*qup
) + strlen(qfpathname
);
if ((qup
= (struct quotause
*)malloc(qupsize
)) == NULL
) {
fprintf(stderr
, "edquota: out of memory\n");
if (quotactl(fs
->fs_file
, qcmd
, id
, &qup
->dqblk
) != 0) {
if (errno
== EOPNOTSUPP
&& !warned
) {
fprintf(stderr
, "Warning: %s\n",
"Quotas are not compiled into this kernel");
if ((fd
= open(qfpathname
, O_RDONLY
)) < 0) {
fd
= open(qfpathname
, O_RDWR
|O_CREAT
, 0640);
if (fd
< 0 && errno
!= ENOENT
) {
fprintf(stderr
, "Creating quota file %s\n",
(void) fchown(fd
, getuid(),
getentry(quotagroup
, GRPQUOTA
));
lseek(fd
, (off_t
)(id
* sizeof(struct dqblk
)), L_SET
);
switch (read(fd
, &qup
->dqblk
, sizeof(struct dqblk
))) {
* Convert implicit 0 quota (EOF)
* into an explicit one (zero'ed dqblk)
bzero((caddr_t
)&qup
->dqblk
,
case sizeof(struct dqblk
): /* OK */
fprintf(stderr
, "edquota: read error in ");
strcpy(qup
->qfname
, qfpathname
);
strcpy(qup
->fsname
, fs
->fs_file
);
* Store the requested quota information.
putprivs(id
, quotatype
, quplist
)
struct quotause
*quplist
;
register struct quotause
*qup
;
qcmd
= QCMD(Q_SETQUOTA
, quotatype
);
for (qup
= quplist
; qup
; qup
= qup
->next
) {
if (quotactl(qup
->fsname
, qcmd
, id
, &qup
->dqblk
) == 0)
if ((fd
= open(qup
->qfname
, O_WRONLY
)) < 0) {
(off_t
)(id
* (long)sizeof (struct dqblk
)), L_SET
);
if (write(fd
, &qup
->dqblk
, sizeof (struct dqblk
)) !=
fprintf(stderr
, "edquota: ");
* Take a list of priviledges and get it edited.
omask
= sigblock(sigmask(SIGINT
)|sigmask(SIGQUIT
)|sigmask(SIGHUP
));
if ((pid
= fork()) < 0) {
fprintf(stderr
, "You have too many processes\n");
if ((ed
= getenv("EDITOR")) == (char *)0)
execlp(ed
, ed
, tmpfile
, 0);
if (!WIFEXITED(stat
) || WEXITSTATUS(stat
) != 0)
* Convert a quotause list to an ASCII file.
writeprivs(quplist
, outfd
, name
, quotatype
)
struct quotause
*quplist
;
register struct quotause
*qup
;
if ((fd
= fdopen(dup(outfd
), "w")) == NULL
) {
fprintf(stderr
, "edquota: ");
fprintf(fd
, "Quotas for %s %s:\n", qfextension
[quotatype
], name
);
for (qup
= quplist
; qup
; qup
= qup
->next
) {
fprintf(fd
, "%s: %s %d, limits (soft = %d, hard = %d)\n",
qup
->fsname
, "blocks in use:",
dbtob(qup
->dqblk
.dqb_curblocks
) / 1024,
dbtob(qup
->dqblk
.dqb_bsoftlimit
) / 1024,
dbtob(qup
->dqblk
.dqb_bhardlimit
) / 1024);
fprintf(fd
, "%s %d, limits (soft = %d, hard = %d)\n",
"\tinodes in use:", qup
->dqblk
.dqb_curinodes
,
qup
->dqblk
.dqb_isoftlimit
, qup
->dqblk
.dqb_ihardlimit
);
* Merge changes to an ASCII file into a quotause list.
struct quotause
*quplist
;
register struct quotause
*qup
;
char *fsp
, line1
[BUFSIZ
], line2
[BUFSIZ
];
fd
= fdopen(dup(infd
), "r");
fprintf(stderr
, "Can't re-read temp file!!\n");
* Discard title line, then read pairs of lines to process.
(void) fgets(line1
, sizeof (line1
), fd
);
while (fgets(line1
, sizeof (line1
), fd
) != NULL
&&
fgets(line2
, sizeof (line2
), fd
) != NULL
) {
if ((fsp
= strtok(line1
, " \t:")) == NULL
) {
fprintf(stderr
, "%s: bad format\n", line1
);
if ((cp
= strtok((char *)0, "\n")) == NULL
) {
fprintf(stderr
, "%s: %s: bad format\n", fsp
,
" blocks in use: %d, limits (soft = %d, hard = %d)",
&dqblk
.dqb_curblocks
, &dqblk
.dqb_bsoftlimit
,
fprintf(stderr
, "%s:%s: bad format\n", fsp
, cp
);
dqblk
.dqb_curblocks
= btodb(dqblk
.dqb_curblocks
* 1024);
dqblk
.dqb_bsoftlimit
= btodb(dqblk
.dqb_bsoftlimit
* 1024);
dqblk
.dqb_bhardlimit
= btodb(dqblk
.dqb_bhardlimit
* 1024);
if ((cp
= strtok(line2
, "\n")) == NULL
) {
fprintf(stderr
, "%s: %s: bad format\n", fsp
, line2
);
"\tinodes in use: %d, limits (soft = %d, hard = %d)",
&dqblk
.dqb_curinodes
, &dqblk
.dqb_isoftlimit
,
fprintf(stderr
, "%s: %s: bad format\n", fsp
, line2
);
for (qup
= quplist
; qup
; qup
= qup
->next
) {
if (strcmp(fsp
, qup
->fsname
))
* Cause time limit to be reset when the quota
* is next used if previously had no soft limit
* or were under it, but now have a soft limit
if (dqblk
.dqb_bsoftlimit
&&
qup
->dqblk
.dqb_curblocks
>= dqblk
.dqb_bsoftlimit
&&
(qup
->dqblk
.dqb_bsoftlimit
== 0 ||
qup
->dqblk
.dqb_curblocks
<
qup
->dqblk
.dqb_bsoftlimit
))
qup
->dqblk
.dqb_btime
= 0;
if (dqblk
.dqb_isoftlimit
&&
qup
->dqblk
.dqb_curinodes
>= dqblk
.dqb_isoftlimit
&&
(qup
->dqblk
.dqb_isoftlimit
== 0 ||
qup
->dqblk
.dqb_curinodes
<
qup
->dqblk
.dqb_isoftlimit
))
qup
->dqblk
.dqb_itime
= 0;
qup
->dqblk
.dqb_bsoftlimit
= dqblk
.dqb_bsoftlimit
;
qup
->dqblk
.dqb_bhardlimit
= dqblk
.dqb_bhardlimit
;
qup
->dqblk
.dqb_isoftlimit
= dqblk
.dqb_isoftlimit
;
qup
->dqblk
.dqb_ihardlimit
= dqblk
.dqb_ihardlimit
;
if (dqblk
.dqb_curblocks
== qup
->dqblk
.dqb_curblocks
&&
dqblk
.dqb_curinodes
== qup
->dqblk
.dqb_curinodes
)
"%s: cannot change current allocation\n", fsp
);
* Disable quotas for any filesystems that have not been found.
for (qup
= quplist
; qup
; qup
= qup
->next
) {
if (qup
->flags
& FOUND
) {
qup
->dqblk
.dqb_bsoftlimit
= 0;
qup
->dqblk
.dqb_bhardlimit
= 0;
qup
->dqblk
.dqb_isoftlimit
= 0;
qup
->dqblk
.dqb_ihardlimit
= 0;
* Convert a quotause list to an ASCII file of grace times.
writetimes(quplist
, outfd
, quotatype
)
struct quotause
*quplist
;
register struct quotause
*qup
;
if ((fd
= fdopen(dup(outfd
), "w")) == NULL
) {
fprintf(stderr
, "edquota: ");
fprintf(fd
, "Time units may be: days, hours, minutes, or seconds\n");
fprintf(fd
, "Grace period before enforcing soft limits for %ss:\n",
for (qup
= quplist
; qup
; qup
= qup
->next
) {
fprintf(fd
, "%s: block grace period: %s, ",
qup
->fsname
, cvtstoa(qup
->dqblk
.dqb_btime
));
fprintf(fd
, "file grace period: %s\n",
cvtstoa(qup
->dqblk
.dqb_itime
));
* Merge changes of grace times in an ASCII file into a quotause list.
struct quotause
*quplist
;
register struct quotause
*qup
;
time_t itime
, btime
, iseconds
, bseconds
;
char *fsp
, bunits
[10], iunits
[10], line1
[BUFSIZ
];
fd
= fdopen(dup(infd
), "r");
fprintf(stderr
, "Can't re-read temp file!!\n");
* Discard two title lines, then read lines to process.
(void) fgets(line1
, sizeof (line1
), fd
);
(void) fgets(line1
, sizeof (line1
), fd
);
while (fgets(line1
, sizeof (line1
), fd
) != NULL
) {
if ((fsp
= strtok(line1
, " \t:")) == NULL
) {
fprintf(stderr
, "%s: bad format\n", line1
);
if ((cp
= strtok((char *)0, "\n")) == NULL
) {
fprintf(stderr
, "%s: %s: bad format\n", fsp
,
" block grace period: %d %s file grace period: %d %s",
&btime
, bunits
, &itime
, iunits
);
fprintf(stderr
, "%s:%s: bad format\n", fsp
, cp
);
if (cvtatos(btime
, bunits
, &bseconds
) == 0)
if (cvtatos(itime
, iunits
, &iseconds
) == 0)
for (qup
= quplist
; qup
; qup
= qup
->next
) {
if (strcmp(fsp
, qup
->fsname
))
qup
->dqblk
.dqb_btime
= bseconds
;
qup
->dqblk
.dqb_itime
= iseconds
;
* reset default grace periods for any filesystems
* that have not been found.
for (qup
= quplist
; qup
; qup
= qup
->next
) {
if (qup
->flags
& FOUND
) {
qup
->dqblk
.dqb_btime
= 0;
qup
->dqblk
.dqb_itime
= 0;
* Convert seconds to ASCII times.
if (time
% (24 * 60 * 60) == 0) {
sprintf(buf
, "%d day%s", time
, time
== 1 ? "" : "s");
} else if (time
% (60 * 60) == 0) {
sprintf(buf
, "%d hour%s", time
, time
== 1 ? "" : "s");
} else if (time
% 60 == 0) {
sprintf(buf
, "%d minute%s", time
, time
== 1 ? "" : "s");
sprintf(buf
, "%d second%s", time
, time
== 1 ? "" : "s");
* Convert ASCII input times to seconds.
cvtatos(time
, units
, seconds
)
if (bcmp(units
, "second", 6) == 0)
else if (bcmp(units
, "minute", 6) == 0)
else if (bcmp(units
, "hour", 4) == 0)
*seconds
= time
* 60 * 60;
else if (bcmp(units
, "day", 3) == 0)
*seconds
= time
* 24 * 60 * 60;
printf("%s: bad units, specify %s\n", units
,
"days, hours, minutes, or seconds");
* Free a list of quotause structures.
struct quotause
*quplist
;
register struct quotause
*qup
, *nextqup
;
for (qup
= quplist
; qup
; qup
= nextqup
) {
* Check whether a string is completely composed of digits.
* Check to see if a particular quota is to be enabled.
hasquota(fs
, type
, qfnamep
)
register struct fstab
*fs
;
char *cp
, *index(), *strtok();
static char initname
, usrname
[100], grpname
[100];
sprintf(usrname
, "%s%s", qfextension
[USRQUOTA
], qfname
);
sprintf(grpname
, "%s%s", qfextension
[GRPQUOTA
], qfname
);
strcpy(buf
, fs
->fs_mntops
);
for (opt
= strtok(buf
, ","); opt
; opt
= strtok(NULL
, ",")) {
if (cp
= index(opt
, '='))
if (type
== USRQUOTA
&& strcmp(opt
, usrname
) == 0)
if (type
== GRPQUOTA
&& strcmp(opt
, grpname
) == 0)
(void) sprintf(buf
, "%s/%s.%s", fs
->fs_file
, qfname
, qfextension
[type
]);