2 * Copyright (c) 1980, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Robert Elz at The University of Melbourne.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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. 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.
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
33 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
36 The Regents of the University of California. All rights reserved.");
41 static char sccsid
[] = "from: @(#)edquota.c 8.3 (Berkeley) 4/27/95";
43 __RCSID("$NetBSD: edquota.c,v 1.28 2008/05/02 19:59:19 xtraeme Exp $");
50 #include <sys/param.h>
54 #include <sys/queue.h>
55 #include <ufs/ufs/quota.h>
67 #include "pathnames.h"
69 const char *qfname
= QUOTAFILENAME
;
70 const char *qfextension
[] = INITQFNAMES
;
71 const char *quotagroup
= QUOTAGROUP
;
72 char tmpfil
[] = _PATH_TMP
;
75 struct quotause
*next
;
78 char fsname
[MAXPATHLEN
+ 1];
79 char qfname
[1]; /* actually longer */
83 #define MAX_TMPSTR (100+MAXPATHLEN)
85 int main
__P((int, char **));
86 void usage
__P((void));
87 int getentry
__P((const char *, int));
89 getprivs
__P((long, int, char *));
90 void putprivs
__P((long, int, struct quotause
*));
91 int editit
__P((char *));
92 int writeprivs
__P((struct quotause
*, int, char *, int));
93 int readprivs
__P((struct quotause
*, int));
94 int writetimes
__P((struct quotause
*, int, int));
95 int readtimes
__P((struct quotause
*, int));
96 char * cvtstoa
__P((time_t));
97 int cvtatos
__P((time_t, char *, time_t *));
98 void freeprivs
__P((struct quotause
*));
99 int alldigits
__P((const char *));
100 int hasquota
__P((struct fstab
*, int, char **));
107 struct quotause
*qup
, *protoprivs
, *curprivs
;
109 int quotatype
, tmpfd
;
111 char *soft
= NULL
, *hard
= NULL
;
114 int tflag
= 0, pflag
= 0;
119 errx(1, "permission denied");
121 quotatype
= USRQUOTA
;
122 while ((ch
= getopt(argc
, argv
, "ugtp:s:h:f:")) != -1) {
129 quotatype
= GRPQUOTA
;
132 quotatype
= USRQUOTA
;
155 if ((protoid
= getentry(protoname
, quotatype
)) == -1)
157 protoprivs
= getprivs(protoid
, quotatype
, fs
);
158 for (qup
= protoprivs
; qup
; qup
= qup
->next
) {
159 qup
->dqblk
.dqb_btime
= 0;
160 qup
->dqblk
.dqb_itime
= 0;
163 if ((id
= getentry(*argv
++, quotatype
)) < 0)
165 putprivs(id
, quotatype
, protoprivs
);
170 struct quotause
*lqup
;
171 u_int32_t softb
, hardb
, softi
, hardi
;
175 if (sscanf(soft
, "%d/%d", &softb
, &softi
) != 2)
177 softb
= btodb((u_quad_t
)softb
* 1024);
180 if (sscanf(hard
, "%d/%d", &hardb
, &hardi
) != 2)
182 hardb
= btodb((u_quad_t
)hardb
* 1024);
184 for ( ; argc
> 0; argc
--, argv
++) {
185 if ((id
= getentry(*argv
, quotatype
)) == -1)
187 curprivs
= getprivs(id
, quotatype
, fs
);
188 for (lqup
= curprivs
; lqup
; lqup
= lqup
->next
) {
191 lqup
->dqblk
.dqb_curblocks
>= softb
&&
192 (lqup
->dqblk
.dqb_bsoftlimit
== 0 ||
193 lqup
->dqblk
.dqb_curblocks
<
194 lqup
->dqblk
.dqb_bsoftlimit
))
195 lqup
->dqblk
.dqb_btime
= 0;
197 lqup
->dqblk
.dqb_curinodes
>= softi
&&
198 (lqup
->dqblk
.dqb_isoftlimit
== 0 ||
199 lqup
->dqblk
.dqb_curinodes
<
200 lqup
->dqblk
.dqb_isoftlimit
))
201 lqup
->dqblk
.dqb_itime
= 0;
202 lqup
->dqblk
.dqb_bsoftlimit
= softb
;
203 lqup
->dqblk
.dqb_isoftlimit
= softi
;
206 lqup
->dqblk
.dqb_bhardlimit
= hardb
;
207 lqup
->dqblk
.dqb_ihardlimit
= hardi
;
210 putprivs(id
, quotatype
, curprivs
);
215 tmpfd
= mkstemp(tmpfil
);
216 fchown(tmpfd
, getuid(), getgid());
220 protoprivs
= getprivs(0, quotatype
, fs
);
221 if (writetimes(protoprivs
, tmpfd
, quotatype
) == 0)
223 if (editit(tmpfil
) && readtimes(protoprivs
, tmpfd
))
224 putprivs(0, quotatype
, protoprivs
);
225 freeprivs(protoprivs
);
228 for ( ; argc
> 0; argc
--, argv
++) {
229 if ((id
= getentry(*argv
, quotatype
)) == -1)
231 curprivs
= getprivs(id
, quotatype
, fs
);
232 if (writeprivs(curprivs
, tmpfd
, *argv
, quotatype
) == 0)
234 if (editit(tmpfil
) && readprivs(curprivs
, tmpfd
))
235 putprivs(id
, quotatype
, curprivs
);
247 "usage: edquota [-u] [-p username] [-f filesystem] username ...\n"
248 "\tedquota -g [-p groupname] [-f filesystem] groupname ...\n"
249 "\tedquota [-u] [-f filesystem] [-s b#/i#] [-h b#/i#] username ...\n"
250 "\tedquota -g [-f filesystem] [-s b#/i#] [-h b#/i#] groupname ...\n"
251 "\tedquota [-u] [-f filesystem] -t\n"
252 "\tedquota -g [-f filesystem] -t\n"
258 * This routine converts a name for a particular quota type to
259 * an identifier. This routine must agree with the kernel routine
260 * getinoquota as to the interpretation of quota types.
263 getentry(name
, quotatype
)
274 if ((pw
= getpwnam(name
)) != NULL
)
276 warnx("%s: no such user", name
);
279 if ((gr
= getgrnam(name
)) != NULL
)
281 warnx("%s: no such group", name
);
284 warnx("%d: unknown quota type", quotatype
);
292 * Collect the requested quota information.
295 getprivs(id
, quotatype
, filesys
)
301 struct quotause
*qup
, *quptail
;
302 struct quotause
*quphead
;
303 int qcmd
, qupsize
, fd
;
305 static int warned
= 0;
309 quphead
= (struct quotause
*)0;
310 qcmd
= QCMD(Q_GETQUOTA
, quotatype
);
311 while ((fs
= getfsent()) != NULL
) {
312 if (strcmp(fs
->fs_vfstype
, "ffs"))
314 if (filesys
&& strcmp(fs
->fs_spec
, filesys
) != 0 &&
315 strcmp(fs
->fs_file
, filesys
) != 0)
317 if (!hasquota(fs
, quotatype
, &qfpathname
))
319 qupsize
= sizeof(*qup
) + strlen(qfpathname
);
320 if ((qup
= (struct quotause
*)malloc(qupsize
)) == NULL
)
321 errx(2, "out of memory");
322 if (quotactl(fs
->fs_file
, qcmd
, id
, &qup
->dqblk
) != 0) {
323 if (errno
== EOPNOTSUPP
&& !warned
) {
326 "Quotas are not compiled into this kernel");
329 if ((fd
= open(qfpathname
, O_RDONLY
)) < 0) {
330 fd
= open(qfpathname
, O_RDWR
|O_CREAT
, 0640);
331 if (fd
< 0 && errno
!= ENOENT
) {
332 warnx("open `%s'", qfpathname
);
336 warnx("Creating quota file %s", qfpathname
);
338 (void) fchown(fd
, getuid(),
339 getentry(quotagroup
, GRPQUOTA
));
340 (void) fchmod(fd
, 0640);
342 (void)lseek(fd
, (off_t
)(id
* sizeof(struct dqblk
)),
344 switch (read(fd
, &qup
->dqblk
, sizeof(struct dqblk
))) {
347 * Convert implicit 0 quota (EOF)
348 * into an explicit one (zero'ed dqblk)
350 memset((caddr_t
)&qup
->dqblk
, 0,
351 sizeof(struct dqblk
));
354 case sizeof(struct dqblk
): /* OK */
358 warn("read error in `%s'", qfpathname
);
365 strcpy(qup
->qfname
, qfpathname
);
366 strcpy(qup
->fsname
, fs
->fs_file
);
379 * Store the requested quota information.
382 putprivs(id
, quotatype
, quplist
)
385 struct quotause
*quplist
;
387 struct quotause
*qup
;
390 qcmd
= QCMD(Q_SETQUOTA
, quotatype
);
391 for (qup
= quplist
; qup
; qup
= qup
->next
) {
392 if (quotactl(qup
->fsname
, qcmd
, id
, &qup
->dqblk
) == 0)
394 if ((fd
= open(qup
->qfname
, O_WRONLY
)) < 0) {
395 warnx("open `%s'", qup
->qfname
);
398 (off_t
)(id
* (long)sizeof (struct dqblk
)),
400 if (write(fd
, &qup
->dqblk
, sizeof (struct dqblk
)) !=
401 sizeof (struct dqblk
))
402 warnx("writing `%s'", qup
->qfname
);
409 * Take a list of privileges and get it edited.
419 omask
= sigblock(sigmask(SIGINT
)|sigmask(SIGQUIT
)|sigmask(SIGHUP
));
421 if ((pid
= fork()) < 0) {
423 if (errno
== EPROCLIM
) {
424 warnx("You have too many processes");
427 if (errno
== EAGAIN
) {
440 if ((ed
= getenv("EDITOR")) == (char *)0)
442 if (strlen(ed
) + strlen(ltmpfile
) + 2 >= MAX_TMPSTR
) {
443 err (1, "%s", "editor or filename too long");
445 snprintf (p
, MAX_TMPSTR
, "%s %s", ed
, ltmpfile
);
446 execlp(_PATH_BSHELL
, _PATH_BSHELL
, "-c", p
, NULL
);
449 waitpid(pid
, &lst
, 0);
451 if (!WIFEXITED(lst
) || WEXITSTATUS(lst
) != 0)
457 * Convert a quotause list to an ASCII file.
460 writeprivs(quplist
, outfd
, name
, quotatype
)
461 struct quotause
*quplist
;
466 struct quotause
*qup
;
470 (void)lseek(outfd
, (off_t
)0, SEEK_SET
);
471 if ((fd
= fdopen(dup(outfd
), "w")) == NULL
)
472 errx(1, "fdopen `%s'", tmpfil
);
473 fprintf(fd
, "Quotas for %s %s:\n", qfextension
[quotatype
], name
);
474 for (qup
= quplist
; qup
; qup
= qup
->next
) {
475 fprintf(fd
, "%s: %s %d, limits (soft = %d, hard = %d)\n",
476 qup
->fsname
, "blocks in use:",
477 (int)(dbtob((u_quad_t
)qup
->dqblk
.dqb_curblocks
) / 1024),
478 (int)(dbtob((u_quad_t
)qup
->dqblk
.dqb_bsoftlimit
) / 1024),
479 (int)(dbtob((u_quad_t
)qup
->dqblk
.dqb_bhardlimit
) / 1024));
480 fprintf(fd
, "%s %d, limits (soft = %d, hard = %d)\n",
481 "\tinodes in use:", qup
->dqblk
.dqb_curinodes
,
482 qup
->dqblk
.dqb_isoftlimit
, qup
->dqblk
.dqb_ihardlimit
);
489 * Merge changes to an ASCII file into a quotause list.
492 readprivs(quplist
, infd
)
493 struct quotause
*quplist
;
496 struct quotause
*qup
;
501 char *fsp
, line1
[BUFSIZ
], line2
[BUFSIZ
];
503 (void)lseek(infd
, (off_t
)0, SEEK_SET
);
504 fd
= fdopen(dup(infd
), "r");
506 warn("Can't re-read temp file");
510 * Discard title line, then read pairs of lines to process.
512 (void) fgets(line1
, sizeof (line1
), fd
);
513 while (fgets(line1
, sizeof (line1
), fd
) != NULL
&&
514 fgets(line2
, sizeof (line2
), fd
) != NULL
) {
515 if ((fsp
= strtok(line1
, " \t:")) == NULL
) {
516 warnx("%s: bad format", line1
);
519 if ((cp
= strtok((char *)0, "\n")) == NULL
) {
520 warnx("%s: %s: bad format", fsp
,
521 &fsp
[strlen(fsp
) + 1]);
525 " blocks in use: %d, limits (soft = %d, hard = %d)",
526 &dqblk
.dqb_curblocks
, &dqblk
.dqb_bsoftlimit
,
527 &dqblk
.dqb_bhardlimit
);
529 warnx("%s:%s: bad format", fsp
, cp
);
532 dqblk
.dqb_curblocks
= btodb((u_quad_t
)
533 dqblk
.dqb_curblocks
* 1024);
534 dqblk
.dqb_bsoftlimit
= btodb((u_quad_t
)
535 dqblk
.dqb_bsoftlimit
* 1024);
536 dqblk
.dqb_bhardlimit
= btodb((u_quad_t
)
537 dqblk
.dqb_bhardlimit
* 1024);
538 if ((cp
= strtok(line2
, "\n")) == NULL
) {
539 warnx("%s: %s: bad format", fsp
, line2
);
543 "\tinodes in use: %d, limits (soft = %d, hard = %d)",
544 &dqblk
.dqb_curinodes
, &dqblk
.dqb_isoftlimit
,
545 &dqblk
.dqb_ihardlimit
);
547 warnx("%s: %s: bad format", fsp
, line2
);
550 for (qup
= quplist
; qup
; qup
= qup
->next
) {
551 if (strcmp(fsp
, qup
->fsname
))
554 * Cause time limit to be reset when the quota
555 * is next used if previously had no soft limit
556 * or were under it, but now have a soft limit
559 if (dqblk
.dqb_bsoftlimit
&&
560 qup
->dqblk
.dqb_curblocks
>= dqblk
.dqb_bsoftlimit
&&
561 (qup
->dqblk
.dqb_bsoftlimit
== 0 ||
562 qup
->dqblk
.dqb_curblocks
<
563 qup
->dqblk
.dqb_bsoftlimit
))
564 qup
->dqblk
.dqb_btime
= 0;
565 if (dqblk
.dqb_isoftlimit
&&
566 qup
->dqblk
.dqb_curinodes
>= dqblk
.dqb_isoftlimit
&&
567 (qup
->dqblk
.dqb_isoftlimit
== 0 ||
568 qup
->dqblk
.dqb_curinodes
<
569 qup
->dqblk
.dqb_isoftlimit
))
570 qup
->dqblk
.dqb_itime
= 0;
571 qup
->dqblk
.dqb_bsoftlimit
= dqblk
.dqb_bsoftlimit
;
572 qup
->dqblk
.dqb_bhardlimit
= dqblk
.dqb_bhardlimit
;
573 qup
->dqblk
.dqb_isoftlimit
= dqblk
.dqb_isoftlimit
;
574 qup
->dqblk
.dqb_ihardlimit
= dqblk
.dqb_ihardlimit
;
576 if (dqblk
.dqb_curblocks
== qup
->dqblk
.dqb_curblocks
&&
577 dqblk
.dqb_curinodes
== qup
->dqblk
.dqb_curinodes
)
579 warnx("%s: cannot change current allocation", fsp
);
586 * Disable quotas for any filesystems that have not been found.
588 for (qup
= quplist
; qup
; qup
= qup
->next
) {
589 if (qup
->flags
& FOUND
) {
590 qup
->flags
&= ~FOUND
;
593 qup
->dqblk
.dqb_bsoftlimit
= 0;
594 qup
->dqblk
.dqb_bhardlimit
= 0;
595 qup
->dqblk
.dqb_isoftlimit
= 0;
596 qup
->dqblk
.dqb_ihardlimit
= 0;
602 * Convert a quotause list to an ASCII file of grace times.
605 writetimes(quplist
, outfd
, quotatype
)
606 struct quotause
*quplist
;
610 struct quotause
*qup
;
614 (void)lseek(outfd
, (off_t
)0, SEEK_SET
);
615 if ((fd
= fdopen(dup(outfd
), "w")) == NULL
)
616 err(1, "fdopen `%s'", tmpfil
);
617 fprintf(fd
, "Time units may be: days, hours, minutes, or seconds\n");
618 fprintf(fd
, "Grace period before enforcing soft limits for %ss:\n",
619 qfextension
[quotatype
]);
620 for (qup
= quplist
; qup
; qup
= qup
->next
) {
621 fprintf(fd
, "%s: block grace period: %s, ",
622 qup
->fsname
, cvtstoa(qup
->dqblk
.dqb_btime
));
623 fprintf(fd
, "file grace period: %s\n",
624 cvtstoa(qup
->dqblk
.dqb_itime
));
631 * Merge changes of grace times in an ASCII file into a quotause list.
634 readtimes(quplist
, infd
)
635 struct quotause
*quplist
;
638 struct quotause
*qup
;
643 time_t itime
, btime
, iseconds
, bseconds
;
644 char *fsp
, bunits
[10], iunits
[10], line1
[BUFSIZ
];
646 (void)lseek(infd
, (off_t
)0, SEEK_SET
);
647 fd
= fdopen(dup(infd
), "r");
649 warnx("Can't re-read temp file!!");
653 * Discard two title lines, then read lines to process.
655 (void) fgets(line1
, sizeof (line1
), fd
);
656 (void) fgets(line1
, sizeof (line1
), fd
);
657 while (fgets(line1
, sizeof (line1
), fd
) != NULL
) {
658 if ((fsp
= strtok(line1
, " \t:")) == NULL
) {
659 warnx("%s: bad format", line1
);
662 if ((cp
= strtok((char *)0, "\n")) == NULL
) {
663 warnx("%s: %s: bad format", fsp
,
664 &fsp
[strlen(fsp
) + 1]);
668 " block grace period: %ld %s file grace period: %ld %s",
669 &lbtime
, bunits
, &litime
, iunits
);
671 warnx("%s:%s: bad format", fsp
, cp
);
674 itime
= (time_t)litime
;
675 btime
= (time_t)lbtime
;
676 if (cvtatos(btime
, bunits
, &bseconds
) == 0)
678 if (cvtatos(itime
, iunits
, &iseconds
) == 0) {
683 for (qup
= quplist
; qup
; qup
= qup
->next
) {
684 if (strcmp(fsp
, qup
->fsname
))
686 qup
->dqblk
.dqb_btime
= bseconds
;
687 qup
->dqblk
.dqb_itime
= iseconds
;
694 * reset default grace periods for any filesystems
695 * that have not been found.
697 for (qup
= quplist
; qup
; qup
= qup
->next
) {
698 if (qup
->flags
& FOUND
) {
699 qup
->flags
&= ~FOUND
;
702 qup
->dqblk
.dqb_btime
= 0;
703 qup
->dqblk
.dqb_itime
= 0;
709 * Convert seconds to ASCII times.
717 if (ltime
% (24 * 60 * 60) == 0) {
718 ltime
/= 24 * 60 * 60;
719 snprintf(buf
, sizeof buf
, "%ld day%s", (long)ltime
,
720 ltime
== 1 ? "" : "s");
721 } else if (ltime
% (60 * 60) == 0) {
723 sprintf(buf
, "%ld hour%s", (long)ltime
, ltime
== 1 ? "" : "s");
724 } else if (ltime
% 60 == 0) {
726 sprintf(buf
, "%ld minute%s", (long)ltime
, ltime
== 1 ? "" : "s");
728 sprintf(buf
, "%ld second%s", (long)ltime
, ltime
== 1 ? "" : "s");
733 * Convert ASCII input times to seconds.
736 cvtatos(ltime
, units
, seconds
)
742 if (memcmp(units
, "second", 6) == 0)
744 else if (memcmp(units
, "minute", 6) == 0)
745 *seconds
= ltime
* 60;
746 else if (memcmp(units
, "hour", 4) == 0)
747 *seconds
= ltime
* 60 * 60;
748 else if (memcmp(units
, "day", 3) == 0)
749 *seconds
= ltime
* 24 * 60 * 60;
751 printf("%s: bad units, specify %s\n", units
,
752 "days, hours, minutes, or seconds");
759 * Free a list of quotause structures.
763 struct quotause
*quplist
;
765 struct quotause
*qup
, *nextqup
;
767 for (qup
= quplist
; qup
; qup
= nextqup
) {
774 * Check whether a string is completely composed of digits.
786 } while ((c
= *s
++) != 0);
791 * Check to see if a particular quota is to be enabled.
794 hasquota(fs
, type
, qfnamep
)
801 static char initname
, usrname
[100], grpname
[100];
802 static char buf
[BUFSIZ
];
805 sprintf(usrname
, "%s%s", qfextension
[USRQUOTA
], qfname
);
806 sprintf(grpname
, "%s%s", qfextension
[GRPQUOTA
], qfname
);
809 strcpy(buf
, fs
->fs_mntops
);
811 for (opt
= strtok(buf
, ","); opt
; opt
= strtok(NULL
, ",")) {
812 if ((cp
= strchr(opt
, '=')) != NULL
)
814 if (type
== USRQUOTA
&& strcmp(opt
, usrname
) == 0)
816 if (type
== GRPQUOTA
&& strcmp(opt
, grpname
) == 0)
825 (void) sprintf(buf
, "%s/%s.%s", fs
->fs_file
, qfname
, qfextension
[type
]);