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 * 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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1990, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)edquota.c 8.1 (Berkeley) 6/6/93";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
52 #include <sys/param.h>
55 #include <sys/mount.h>
57 #include <ufs/ufs/quota.h>
73 #include "pathnames.h"
75 /* Let's be paranoid about block size */
78 ((off_t)(db) >> (10-DEV_BSHIFT))
81 ((off_t)(db) << (DEV_BSHIFT-10))
83 #define dbtokb(db) (db)
86 const char *qfextension[] = INITQFNAMES;
87 char tmpfil[] = _PATH_TMP;
91 struct quotause *next;
95 char fsname[MAXPATHLEN + 1];
99 int alldigits(const char *s);
100 int cvtatos(uint64_t, char *, uint64_t *);
101 char *cvtstoa(uint64_t);
102 uint64_t cvtblkval(uint64_t, char, const char *);
103 uint64_t cvtinoval(uint64_t, char, const char *);
105 char *fmthumanvalblks(int64_t);
106 char *fmthumanvalinos(int64_t);
107 void freeprivs(struct quotause *);
108 int getentry(const char *, int);
109 struct quotause *getprivs(long, int, char *);
110 void putprivs(long, struct quotause *);
111 int readprivs(struct quotause *, char *);
112 int readtimes(struct quotause *, char *);
113 static void usage(void);
114 int writetimes(struct quotause *, int, int);
115 int writeprivs(struct quotause *, int, char *, int);
118 main(int argc, char *argv[])
120 struct quotause *qup, *protoprivs, *curprivs;
122 int i, quotatype, range, tmpfd;
123 uid_t startuid, enduid;
125 char *protoname, *cp, *endpt, *oldoptarg;
126 int eflag = 0, tflag = 0, pflag = 0, ch;
128 char buf[MAXLOGNAME];
133 errx(1, "permission denied");
134 quotatype = USRQUOTA;
138 while ((ch = getopt(argc, argv, "ughtf:p:e:")) != -1) {
145 warnx("cannot specify both -e and -p");
153 quotatype = GRPQUOTA;
159 quotatype = USRQUOTA;
166 warnx("cannot specify both -e and -p");
170 if ((qup = calloc(1, sizeof(*qup))) == NULL)
171 errx(2, "out of memory");
173 for (i = 0, cp = optarg;
174 (cp = strsep(&optarg, ":")) != NULL; i++) {
177 if (i > 0 && !isdigit(*cp)) {
178 warnx("incorrect quota specification: "
185 strlcpy(qup->fsname, cp,
186 sizeof(qup->fsname));
189 lim = strtoll(cp, &endpt, 10);
190 qup->dqblk.dqb_bsoftlimit =
191 cvtblkval(lim, *endpt,
195 lim = strtoll(cp, &endpt, 10);
196 qup->dqblk.dqb_bhardlimit =
197 cvtblkval(lim, *endpt,
201 lim = strtoll(cp, &endpt, 10);
202 qup->dqblk.dqb_isoftlimit =
203 cvtinoval(lim, *endpt,
207 lim = strtoll(cp, &endpt, 10);
208 qup->dqblk.dqb_ihardlimit =
209 cvtinoval(lim, *endpt,
213 warnx("incorrect quota specification: "
219 if (protoprivs == NULL) {
220 protoprivs = curprivs = qup;
222 curprivs->next = qup;
234 if (pflag || eflag) {
236 if ((protoid = getentry(protoname, quotatype)) == -1)
238 protoprivs = getprivs(protoid, quotatype, fspath);
239 if (protoprivs == NULL)
241 for (qup = protoprivs; qup; qup = qup->next) {
242 qup->dqblk.dqb_btime = 0;
243 qup->dqblk.dqb_itime = 0;
246 for (; argc-- > 0; argv++) {
247 if (strspn(*argv, "0123456789-") == strlen(*argv) &&
248 (cp = strchr(*argv, '-')) != NULL) {
250 startuid = atoi(*argv);
252 if (enduid < startuid)
254 "ending uid (%d) must be >= starting uid (%d) when using uid ranges",
258 startuid = enduid = 0;
261 for ( ; startuid <= enduid; startuid++) {
263 snprintf(buf, sizeof(buf), "%d",
266 snprintf(buf, sizeof(buf), "%s",
268 if ((id = getentry(buf, quotatype)) < 0)
271 putprivs(id, protoprivs);
274 for (qup = protoprivs; qup; qup = qup->next) {
275 curprivs = getprivs(id, quotatype,
277 if (curprivs == NULL)
279 curprivs->dqblk = qup->dqblk;
280 putprivs(id, curprivs);
286 freeprivs(protoprivs);
289 tmpfd = mkstemp(tmpfil);
290 fchown(tmpfd, getuid(), getgid());
292 if ((protoprivs = getprivs(0, quotatype, fspath)) != NULL) {
293 if (writetimes(protoprivs, tmpfd, quotatype) != 0 &&
294 editit(tmpfil) && readtimes(protoprivs, tmpfil))
295 putprivs(0L, protoprivs);
296 freeprivs(protoprivs);
302 for ( ; argc > 0; argc--, argv++) {
303 if ((id = getentry(*argv, quotatype)) == -1)
305 if ((curprivs = getprivs(id, quotatype, fspath)) == NULL)
307 if (writeprivs(curprivs, tmpfd, *argv, quotatype) == 0)
309 if (editit(tmpfil) && readprivs(curprivs, tmpfil))
310 putprivs(id, curprivs);
321 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
322 "usage: edquota [-uh] [-f fspath] [-p username] username ...",
323 " edquota [-u] -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
325 " edquota -g [-h] [-f fspath] [-p groupname] groupname ...",
326 " edquota -g -e fspath[:bslim[:bhlim[:islim[:ihlim]]]] [-e ...]",
328 " edquota [-u] -t [-f fspath]",
329 " edquota -g -t [-f fspath]");
334 * This routine converts a name for a particular quota type to
335 * an identifier. This routine must agree with the kernel routine
336 * getinoquota as to the interpretation of quota types.
339 getentry(const char *name, int quotatype)
348 if ((pw = getpwnam(name)))
350 warnx("%s: no such user", name);
354 if ((gr = getgrnam(name)))
356 warnx("%s: no such group", name);
360 warnx("%d: unknown quota type", quotatype);
369 * Collect the requested quota information.
372 getprivs(long id, int quotatype, char *fspath)
374 struct quotafile *qf;
376 struct quotause *qup, *quptail;
377 struct quotause *quphead;
380 quphead = quptail = NULL;
381 while ((fs = getfsent())) {
382 if (fspath && *fspath && strcmp(fspath, fs->fs_spec) &&
383 strcmp(fspath, fs->fs_file))
385 if (strcmp(fs->fs_vfstype, "ufs"))
387 if ((qf = quota_open(fs, quotatype, O_CREAT|O_RDWR)) == NULL) {
388 if (errno != EOPNOTSUPP)
389 warn("cannot open quotas on %s", fs->fs_file);
392 if ((qup = (struct quotause *)calloc(1, sizeof(*qup))) == NULL)
393 errx(2, "out of memory");
395 strncpy(qup->fsname, fs->fs_file, sizeof(qup->fsname));
396 if (quota_read(qf, &qup->dqblk, id) == -1) {
397 warn("cannot read quotas on %s", fs->fs_file);
408 if (quphead == NULL) {
409 warnx("No quotas on %s", fspath ? fspath : "any filesystems");
416 * Store the requested quota information.
419 putprivs(long id, struct quotause *quplist)
421 struct quotause *qup;
423 for (qup = quplist; qup; qup = qup->next)
424 if (quota_write_limits(qup->qf, &qup->dqblk, id) == -1)
425 warn("%s", qup->fsname);
429 * Take a list of priviledges and get it edited.
437 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
439 if ((pid = fork()) < 0) {
441 if (errno == EPROCLIM) {
442 warnx("you have too many processes");
445 if (errno == EAGAIN) {
456 if (setgid(getgid()) != 0)
457 err(1, "setgid failed");
458 if (setuid(getuid()) != 0)
459 err(1, "setuid failed");
460 if ((ed = getenv("EDITOR")) == (char *)0)
462 execlp(ed, ed, tmpf, (char *)0);
465 waitpid(pid, &status, 0);
467 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
473 * Convert a quotause list to an ASCII file.
476 writeprivs(struct quotause *quplist, int outfd, char *name, int quotatype)
478 struct quotause *qup;
482 lseek(outfd, 0, L_SET);
483 if ((fd = fdopen(dup(outfd), "w")) == NULL)
484 err(1, "%s", tmpfil);
485 fprintf(fd, "Quotas for %s %s:\n", qfextension[quotatype], name);
486 for (qup = quplist; qup; qup = qup->next) {
487 fprintf(fd, "%s: in use: %s, ", qup->fsname,
488 fmthumanvalblks(qup->dqblk.dqb_curblocks));
489 fprintf(fd, "limits (soft = %s, ",
490 fmthumanvalblks(qup->dqblk.dqb_bsoftlimit));
491 fprintf(fd, "hard = %s)\n",
492 fmthumanvalblks(qup->dqblk.dqb_bhardlimit));
493 fprintf(fd, "\tinodes in use: %s, ",
494 fmthumanvalinos(qup->dqblk.dqb_curinodes));
495 fprintf(fd, "limits (soft = %s, ",
496 fmthumanvalinos(qup->dqblk.dqb_isoftlimit));
497 fprintf(fd, "hard = %s)\n",
498 fmthumanvalinos(qup->dqblk.dqb_ihardlimit));
505 fmthumanvalblks(int64_t blocks)
507 static char numbuf[20];
510 humanize_number(numbuf, blocks < 0 ? 7 : 6,
511 dbtob(blocks), "", HN_AUTOSCALE, HN_NOSPACE);
514 snprintf(numbuf, sizeof(numbuf), "%juk", (uintmax_t)dbtokb(blocks));
519 fmthumanvalinos(int64_t inos)
521 static char numbuf[20];
524 humanize_number(numbuf, inos < 0 ? 7 : 6,
525 inos, "", HN_AUTOSCALE, HN_NOSPACE | HN_DIVISOR_1000);
528 snprintf(numbuf, sizeof(numbuf), "%ju", (uintmax_t)inos);
533 * Merge changes to an ASCII file into a quotause list.
536 readprivs(struct quotause *quplist, char *inname)
538 struct quotause *qup;
540 uintmax_t hardlimit, softlimit, curitems;
541 char hardunits, softunits, curitemunits;
545 char *fsp, line1[BUFSIZ], line2[BUFSIZ];
547 fd = fopen(inname, "r");
549 warnx("can't re-read temp file!!");
553 * Discard title line, then read pairs of lines to process.
555 (void) fgets(line1, sizeof (line1), fd);
556 while (fgets(line1, sizeof (line1), fd) != NULL &&
557 fgets(line2, sizeof (line2), fd) != NULL) {
558 if ((fsp = strtok(line1, " \t:")) == NULL) {
559 warnx("%s: bad format", line1);
562 if ((cp = strtok((char *)0, "\n")) == NULL) {
563 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
567 " in use: %ju%c, limits (soft = %ju%c, hard = %ju%c)",
568 &curitems, &curitemunits, &softlimit, &softunits,
569 &hardlimit, &hardunits);
571 * The next three check for old-style input formats.
575 " in use: %ju%c, limits (soft = %ju%c hard = %ju%c",
576 &curitems, &curitemunits, &softlimit,
577 &softunits, &hardlimit, &hardunits);
580 " in use: %ju%c, limits (soft = %ju%c hard = %ju%c)",
581 &curitems, &curitemunits, &softlimit,
582 &softunits, &hardlimit, &hardunits);
585 " in use: %ju%c, limits (soft = %ju%c, hard = %ju%c",
586 &curitems, &curitemunits, &softlimit,
587 &softunits, &hardlimit, &hardunits);
589 warnx("%s:%s: bad format", fsp, cp);
592 dqblk.dqb_curblocks = cvtblkval(curitems, curitemunits,
593 "current block count");
594 dqblk.dqb_bsoftlimit = cvtblkval(softlimit, softunits,
596 dqblk.dqb_bhardlimit = cvtblkval(hardlimit, hardunits,
598 if ((cp = strtok(line2, "\n")) == NULL) {
599 warnx("%s: %s: bad format", fsp, line2);
603 " in use: %ju%c limits (soft = %ju%c, hard = %ju%c)",
604 &curitems, &curitemunits, &softlimit,
605 &softunits, &hardlimit, &hardunits);
607 * The next three check for old-style input formats.
611 " in use: %ju%c limits (soft = %ju%c hard = %ju%c",
612 &curitems, &curitemunits, &softlimit,
613 &softunits, &hardlimit, &hardunits);
616 " in use: %ju%c limits (soft = %ju%c hard = %ju%c)",
617 &curitems, &curitemunits, &softlimit,
618 &softunits, &hardlimit, &hardunits);
621 " in use: %ju%c limits (soft = %ju%c, hard = %ju%c",
622 &curitems, &curitemunits, &softlimit,
623 &softunits, &hardlimit, &hardunits);
625 warnx("%s: %s: bad format cnt %d", fsp, &cp[7], cnt);
628 dqblk.dqb_curinodes = cvtinoval(curitems, curitemunits,
629 "current inode count");
630 dqblk.dqb_isoftlimit = cvtinoval(softlimit, softunits,
632 dqblk.dqb_ihardlimit = cvtinoval(hardlimit, hardunits,
634 for (qup = quplist; qup; qup = qup->next) {
635 if (strcmp(fsp, qup->fsname))
638 * Cause time limit to be reset when the quota
639 * is next used if previously had no soft limit
640 * or were under it, but now have a soft limit
643 if (dqblk.dqb_bsoftlimit &&
644 qup->dqblk.dqb_curblocks >= dqblk.dqb_bsoftlimit &&
645 (qup->dqblk.dqb_bsoftlimit == 0 ||
646 qup->dqblk.dqb_curblocks <
647 qup->dqblk.dqb_bsoftlimit))
648 qup->dqblk.dqb_btime = 0;
649 if (dqblk.dqb_isoftlimit &&
650 qup->dqblk.dqb_curinodes >= dqblk.dqb_isoftlimit &&
651 (qup->dqblk.dqb_isoftlimit == 0 ||
652 qup->dqblk.dqb_curinodes <
653 qup->dqblk.dqb_isoftlimit))
654 qup->dqblk.dqb_itime = 0;
655 qup->dqblk.dqb_bsoftlimit = dqblk.dqb_bsoftlimit;
656 qup->dqblk.dqb_bhardlimit = dqblk.dqb_bhardlimit;
657 qup->dqblk.dqb_isoftlimit = dqblk.dqb_isoftlimit;
658 qup->dqblk.dqb_ihardlimit = dqblk.dqb_ihardlimit;
660 /* Humanized input returns only approximate counts */
662 (dqblk.dqb_curblocks == qup->dqblk.dqb_curblocks &&
663 dqblk.dqb_curinodes == qup->dqblk.dqb_curinodes))
665 warnx("%s: cannot change current allocation", fsp);
671 * Disable quotas for any filesystems that have not been found.
673 for (qup = quplist; qup; qup = qup->next) {
674 if (qup->flags & FOUND) {
675 qup->flags &= ~FOUND;
678 qup->dqblk.dqb_bsoftlimit = 0;
679 qup->dqblk.dqb_bhardlimit = 0;
680 qup->dqblk.dqb_isoftlimit = 0;
681 qup->dqblk.dqb_ihardlimit = 0;
687 * Convert a quotause list to an ASCII file of grace times.
690 writetimes(struct quotause *quplist, int outfd, int quotatype)
692 struct quotause *qup;
696 lseek(outfd, 0, L_SET);
697 if ((fd = fdopen(dup(outfd), "w")) == NULL)
698 err(1, "%s", tmpfil);
699 fprintf(fd, "Time units may be: days, hours, minutes, or seconds\n");
700 fprintf(fd, "Grace period before enforcing soft limits for %ss:\n",
701 qfextension[quotatype]);
702 for (qup = quplist; qup; qup = qup->next) {
703 fprintf(fd, "%s: block grace period: %s, ",
704 qup->fsname, cvtstoa(qup->dqblk.dqb_btime));
705 fprintf(fd, "file grace period: %s\n",
706 cvtstoa(qup->dqblk.dqb_itime));
713 * Merge changes of grace times in an ASCII file into a quotause list.
716 readtimes(struct quotause *quplist, char *inname)
718 struct quotause *qup;
722 uintmax_t itime, btime, iseconds, bseconds;
723 char *fsp, bunits[10], iunits[10], line1[BUFSIZ];
725 fd = fopen(inname, "r");
727 warnx("can't re-read temp file!!");
731 * Discard two title lines, then read lines to process.
733 (void) fgets(line1, sizeof (line1), fd);
734 (void) fgets(line1, sizeof (line1), fd);
735 while (fgets(line1, sizeof (line1), fd) != NULL) {
736 if ((fsp = strtok(line1, " \t:")) == NULL) {
737 warnx("%s: bad format", line1);
740 if ((cp = strtok((char *)0, "\n")) == NULL) {
741 warnx("%s: %s: bad format", fsp, &fsp[strlen(fsp) + 1]);
745 " block grace period: %ju %s file grace period: %ju %s",
746 &btime, bunits, &itime, iunits);
748 warnx("%s:%s: bad format", fsp, cp);
751 if (cvtatos(btime, bunits, &bseconds) == 0)
753 if (cvtatos(itime, iunits, &iseconds) == 0)
755 for (qup = quplist; qup; qup = qup->next) {
756 if (strcmp(fsp, qup->fsname))
758 qup->dqblk.dqb_btime = bseconds;
759 qup->dqblk.dqb_itime = iseconds;
766 * reset default grace periods for any filesystems
767 * that have not been found.
769 for (qup = quplist; qup; qup = qup->next) {
770 if (qup->flags & FOUND) {
771 qup->flags &= ~FOUND;
774 qup->dqblk.dqb_btime = 0;
775 qup->dqblk.dqb_itime = 0;
781 * Convert seconds to ASCII times.
784 cvtstoa(uint64_t secs)
788 if (secs % (24 * 60 * 60) == 0) {
789 secs /= 24 * 60 * 60;
790 sprintf(buf, "%ju day%s", (uintmax_t)secs,
791 secs == 1 ? "" : "s");
792 } else if (secs % (60 * 60) == 0) {
794 sprintf(buf, "%ju hour%s", (uintmax_t)secs,
795 secs == 1 ? "" : "s");
796 } else if (secs % 60 == 0) {
798 sprintf(buf, "%ju minute%s", (uintmax_t)secs,
799 secs == 1 ? "" : "s");
801 sprintf(buf, "%ju second%s", (uintmax_t)secs,
802 secs == 1 ? "" : "s");
807 * Convert ASCII input times to seconds.
810 cvtatos(uint64_t period, char *units, uint64_t *seconds)
813 if (bcmp(units, "second", 6) == 0)
815 else if (bcmp(units, "minute", 6) == 0)
816 *seconds = period * 60;
817 else if (bcmp(units, "hour", 4) == 0)
818 *seconds = period * 60 * 60;
819 else if (bcmp(units, "day", 3) == 0)
820 *seconds = period * 24 * 60 * 60;
822 warnx("%s: bad units, specify %s\n", units,
823 "days, hours, minutes, or seconds");
830 * Convert a limit to number of disk blocks.
833 cvtblkval(uint64_t limit, char units, const char *itemname)
839 limit = btodb(limit);
841 case '\0': /* historic behavior */
842 case ',': /* historic behavior */
843 case ')': /* historic behavior */
846 limit *= btodb(1024);
850 limit *= btodb(1048576);
854 limit *= btodb(1073741824);
858 limit *= btodb(1099511627776);
862 limit *= btodb(1125899906842624);
866 limit *= btodb(1152921504606846976);
869 errx(2, "No space permitted between value and units for %s\n",
873 errx(2, "%ju%c: unknown units for %s, specify "
874 "none, K, M, G, T, P, or E\n",
875 (uintmax_t)limit, units, itemname);
882 * Convert a limit to number of inodes.
885 cvtinoval(uint64_t limit, char units, const char *itemname)
891 case '\0': /* historic behavior */
892 case ',': /* historic behavior */
893 case ')': /* historic behavior */
909 limit *= 1000000000000;
913 limit *= 1000000000000000;
917 limit *= 1000000000000000000;
920 errx(2, "No space permitted between value and units for %s\n",
924 errx(2, "%ju%c: unknown units for %s, specify "
925 "none, K, M, G, T, P, or E\n",
926 (uintmax_t)limit, units, itemname);
933 * Free a list of quotause structures.
936 freeprivs(struct quotause *quplist)
938 struct quotause *qup, *nextqup;
940 for (qup = quplist; qup; qup = nextqup) {
941 quota_close(qup->qf);
948 * Check whether a string is completely composed of digits.
951 alldigits(const char *s)
959 } while ((c = *s++));