xref: /freebsd/usr.sbin/quotaon/quotaon.c (revision 118d0ff54d4f7cffe6a13a1e0cdb2fe23fbfce74)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Robert Elz at The University of Melbourne.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Turn quota on/off for a filesystem.
37  */
38 #include <sys/param.h>
39 #include <sys/file.h>
40 #include <sys/mount.h>
41 #include <ufs/ufs/quota.h>
42 #include <err.h>
43 #include <fstab.h>
44 #include <libutil.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 static const char *qfextension[] = INITQFNAMES;
51 
52 static int	aflag;		/* all filesystems */
53 static int	gflag;		/* operate on group quotas */
54 static int	uflag;		/* operate on user quotas */
55 static int	vflag;		/* verbose */
56 
57 static int oneof(char *, char *[], int);
58 static int quotaonoff(struct fstab *fs, int, int);
59 static void usage(void) __dead2;
60 
61 int
62 main(int argc, char **argv)
63 {
64 	struct fstab *fs;
65 	const char *whoami;
66 	long argnum, done = 0;
67 	int ch, i, offmode = 0, errs = 0;
68 
69 	whoami = getprogname();
70 	if (strcmp(whoami, "quotaoff") == 0)
71 		offmode++;
72 	else if (strcmp(whoami, "quotaon") != 0)
73 		errx(1, "name must be quotaon or quotaoff");
74 	while ((ch = getopt(argc, argv, "avug")) != -1) {
75 		switch(ch) {
76 		case 'a':
77 			aflag++;
78 			break;
79 		case 'g':
80 			gflag++;
81 			break;
82 		case 'u':
83 			uflag++;
84 			break;
85 		case 'v':
86 			vflag++;
87 			break;
88 		default:
89 			usage();
90 		}
91 	}
92 	argc -= optind;
93 	argv += optind;
94 	if (argc <= 0 && !aflag)
95 		usage();
96 	if (!gflag && !uflag) {
97 		gflag++;
98 		uflag++;
99 	}
100 	setfsent();
101 	while ((fs = getfsent()) != NULL) {
102 		if (strcmp(fs->fs_vfstype, "ufs") ||
103 		    strcmp(fs->fs_type, FSTAB_RW))
104 			continue;
105 		if (aflag) {
106 			if (gflag)
107 				errs += quotaonoff(fs, offmode, GRPQUOTA);
108 			if (uflag)
109 				errs += quotaonoff(fs, offmode, USRQUOTA);
110 			continue;
111 		}
112 		if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
113 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
114 			done |= 1 << argnum;
115 			if (gflag)
116 				errs += quotaonoff(fs, offmode, GRPQUOTA);
117 			if (uflag)
118 				errs += quotaonoff(fs, offmode, USRQUOTA);
119 		}
120 	}
121 	endfsent();
122 	for (i = 0; i < argc; i++)
123 		if ((done & (1 << i)) == 0)
124 			warnx("%s not found in fstab", argv[i]);
125 	exit(errs);
126 }
127 
128 static void
129 usage(void)
130 {
131 
132 	fprintf(stderr, "%s\n%s\n%s\n%s\n",
133 		"usage: quotaon [-g] [-u] [-v] -a",
134 		"       quotaon [-g] [-u] [-v] filesystem ...",
135 		"       quotaoff [-g] [-u] [-v] -a",
136 		"       quotaoff [-g] [-u] [-v] filesystem ...");
137 	exit(1);
138 }
139 
140 static int
141 quotaonoff(struct fstab *fs, int offmode, int type)
142 {
143 	struct quotafile *qf;
144 
145 	if ((qf = quota_open(fs, type, O_RDONLY)) == NULL)
146 		return (0);
147 	if (offmode) {
148 		if (quota_off(qf) != 0) {
149 			warn("%s", quota_fsname(qf));
150 			return (1);
151 		}
152 		if (vflag)
153 			printf("%s: quotas turned off\n", quota_fsname(qf));
154 		quota_close(qf);
155 		return(0);
156 	}
157 	if (quota_on(qf) != 0) {
158 		warn("using %s on %s", quota_qfname(qf), quota_fsname(qf));
159 		return (1);
160 	}
161 	if (vflag)
162 		printf("%s: %s quotas turned on with data file %s\n",
163 		    quota_fsname(qf), qfextension[type], quota_qfname(qf));
164 	quota_close(qf);
165 	return(0);
166 }
167 
168 /*
169  * Check to see if target appears in list of size cnt.
170  */
171 static int
172 oneof(char *target, char *list[], int cnt)
173 {
174 	int i;
175 
176 	for (i = 0; i < cnt; i++)
177 		if (strcmp(target, list[i]) == 0)
178 			return (i);
179 	return (-1);
180 }
181