#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
The Regents of the University of California. All rights reserved.");
#endif
#ifndef lint
#if 0
static char sccsid[] = "@(#)quotaon.c 8.1 (Berkeley) 6/6/93";
#else
__RCSID("$NetBSD: quotaon.c,v 1.31 2022/04/26 15:39:00 hannken Exp $");
#endif
#endif
#include <sys/param.h>
#include <sys/file.h>
#include <sys/mount.h>
#include <quota.h>
#include <ufs/ufs/quota1.h>
#include <err.h>
#include <fstab.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>
static int vflag;
static void usage(void) __dead;
static int quotaonoff(struct fstab *, struct quotahandle *, int, int, int,
const char *);
static int readonly(struct fstab *, const char *);
static int oneof(const char *target, char *list[], int cnt);
int
main(int argc, char *argv[])
{
struct fstab *fs;
struct quotahandle *qh;
long argnum, done = 0;
int i, offmode = 0, errs = 0;
unsigned restrictions;
int ch;
int aflag = 0;
int gflag = 0;
int uflag = 0;
int noguflag = 0;
if (strcmp(getprogname(), "quotaoff") == 0)
offmode++;
else if (strcmp(getprogname(), "quotaon") != 0)
errx(1, "Name must be quotaon or quotaoff");
while ((ch = getopt(argc, argv, "avug")) != -1) {
switch(ch) {
case 'a':
aflag++;
break;
case 'g':
gflag++;
break;
case 'u':
uflag++;
break;
case 'v':
vflag++;
break;
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
if (argc <= 0 && !aflag)
usage();
if (!gflag && !uflag) {
noguflag = 1;
}
qh = quota_open("/");
if (qh != NULL) {
quota_close(qh);
}
setfsent();
while ((fs = getfsent()) != NULL) {
char buf[MAXPATHLEN];
const char *fsspec;
if ((strcmp(fs->fs_vfstype, "ffs") &&
strcmp(fs->fs_vfstype, "lfs")) ||
strcmp(fs->fs_type, FSTAB_RW))
continue;
fsspec = getfsspecname(buf, sizeof(buf), fs->fs_spec);
if (fsspec == NULL) {
warn("%s", buf);
continue;
}
if (!aflag) {
if ((argnum = oneof(fs->fs_file, argv, argc)) < 0 &&
(argnum = oneof(fsspec, argv, argc)) < 0) {
continue;
}
done |= 1U << argnum;
}
qh = quota_open(fs->fs_file);
if (qh == NULL) {
if (!aflag) {
warn("quota_open");
errs++;
}
continue;
}
restrictions = quota_getrestrictions(qh);
if ((restrictions & QUOTA_RESTRICT_NEEDSQUOTACHECK) == 0) {
if (!aflag) {
errno = EBUSY;
warn("%s", fs->fs_file);
errs++;
}
quota_close(qh);
continue;
}
if (noguflag) {
errs += quotaonoff(fs, qh, offmode, GRPQUOTA, 0, fsspec);
errs += quotaonoff(fs, qh, offmode, USRQUOTA, 0, fsspec);
}
if (gflag) {
errs += quotaonoff(fs, qh, offmode, GRPQUOTA, 1, fsspec);
}
if (uflag) {
errs += quotaonoff(fs, qh, offmode, USRQUOTA, 1, fsspec);
}
quota_close(qh);
}
endfsent();
for (i = 0; i < argc; i++)
if ((done & (1U << i)) == 0)
warnx("%s not found in fstab", argv[i]);
return errs;
}
static void
usage(void)
{
const char *p = getprogname();
(void) fprintf(stderr, "Usage: %s [-g] [-u] [-v] -a\n"
"\t%s [-g] [-u] [-v] filesys ...\n", p, p);
exit(1);
}
static int
quotaonoff(struct fstab *fs, struct quotahandle *qh, int offmode, int idtype,
int warn_on_enxio, const char *fsspec)
{
const char *mode = (offmode == 1) ? "off" : "on";
const char *type;
if (strcmp(fs->fs_file, "/") && readonly(fs, fsspec)) {
return 1;
}
if (offmode) {
type = quota_idtype_getname(qh, idtype);
if (quota_quotaoff(qh, idtype)) {
if (warn_on_enxio || errno != ENXIO) {
warn("quota%s for %s", mode, fs->fs_file);
}
return 1;
}
} else {
if (quota_quotaon(qh, idtype)) {
if (warn_on_enxio || errno != ENXIO) {
warn("quota%s for %s", mode, fs->fs_file);
}
return 1;
}
type = quota_idtype_getname(qh, idtype);
}
if (vflag) {
printf("%s: %s quotas turned %s\n",
fs->fs_file, type, mode);
}
return 0;
}
static int
readonly(struct fstab *fs, const char *fsspec)
{
struct statvfs fsbuf;
if (statvfs(fs->fs_file, &fsbuf) < 0 ||
strcmp(fsbuf.f_mntonname, fs->fs_file) ||
strcmp(fsbuf.f_mntfromname, fsspec)) {
printf("%s: not mounted\n", fs->fs_file);
return 1;
}
if (fsbuf.f_flag & MNT_RDONLY) {
printf("%s: mounted read-only\n", fs->fs_file);
return 1;
}
return 0;
}
static int
oneof(const char *target, char *list[], int cnt)
{
int i;
for (i = 0; i < cnt; i++)
if (strcmp(target, list[i]) == 0)
return i;
return -1;
}