quotaon.c revision 1.3 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1980, 1990 Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Robert Elz at The University of Melbourne.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #ifndef lint
38 1.1 cgd char copyright[] =
39 1.1 cgd "@(#) Copyright (c) 1980, 1990 Regents of the University of California.\n\
40 1.1 cgd All rights reserved.\n";
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd #ifndef lint
44 1.2 mycroft /*static char sccsid[] = "from: @(#)quotaon.c 5.12 (Berkeley) 9/27/90";*/
45 1.3 cgd static char rcsid[] = "$Id: quotaon.c,v 1.3 1994/06/13 20:54:27 cgd Exp $";
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.1 cgd /*
49 1.1 cgd * Turn quota on/off for a filesystem.
50 1.1 cgd */
51 1.1 cgd #include <sys/param.h>
52 1.1 cgd #include <sys/file.h>
53 1.1 cgd #include <sys/mount.h>
54 1.3 cgd #include <ufs/ufs/quota.h>
55 1.1 cgd #include <stdio.h>
56 1.1 cgd #include <fstab.h>
57 1.1 cgd
58 1.1 cgd char *qfname = QUOTAFILENAME;
59 1.1 cgd char *qfextension[] = INITQFNAMES;
60 1.1 cgd
61 1.1 cgd int aflag; /* all file systems */
62 1.1 cgd int gflag; /* operate on group quotas */
63 1.1 cgd int uflag; /* operate on user quotas */
64 1.1 cgd int vflag; /* verbose */
65 1.1 cgd
66 1.1 cgd main(argc, argv)
67 1.1 cgd int argc;
68 1.1 cgd char **argv;
69 1.1 cgd {
70 1.1 cgd register struct fstab *fs;
71 1.1 cgd char ch, *qfnp, *whoami, *rindex();
72 1.1 cgd long argnum, done = 0;
73 1.1 cgd int i, offmode = 0, errs = 0;
74 1.1 cgd extern char *optarg;
75 1.1 cgd extern int optind;
76 1.1 cgd
77 1.1 cgd whoami = rindex(*argv, '/') + 1;
78 1.1 cgd if (whoami == (char *)1)
79 1.1 cgd whoami = *argv;
80 1.1 cgd if (strcmp(whoami, "quotaoff") == 0)
81 1.1 cgd offmode++;
82 1.1 cgd else if (strcmp(whoami, "quotaon") != 0) {
83 1.1 cgd fprintf(stderr, "Name must be quotaon or quotaoff not %s\n",
84 1.1 cgd whoami);
85 1.1 cgd exit(1);
86 1.1 cgd }
87 1.1 cgd while ((ch = getopt(argc, argv, "avug")) != EOF) {
88 1.1 cgd switch(ch) {
89 1.1 cgd case 'a':
90 1.1 cgd aflag++;
91 1.1 cgd break;
92 1.1 cgd case 'g':
93 1.1 cgd gflag++;
94 1.1 cgd break;
95 1.1 cgd case 'u':
96 1.1 cgd uflag++;
97 1.1 cgd break;
98 1.1 cgd case 'v':
99 1.1 cgd vflag++;
100 1.1 cgd break;
101 1.1 cgd default:
102 1.1 cgd usage(whoami);
103 1.1 cgd }
104 1.1 cgd }
105 1.1 cgd argc -= optind;
106 1.1 cgd argv += optind;
107 1.1 cgd if (argc <= 0 && !aflag)
108 1.1 cgd usage(whoami);
109 1.1 cgd if (!gflag && !uflag) {
110 1.1 cgd gflag++;
111 1.1 cgd uflag++;
112 1.1 cgd }
113 1.1 cgd setfsent();
114 1.1 cgd while ((fs = getfsent()) != NULL) {
115 1.1 cgd if (strcmp(fs->fs_vfstype, "ufs") ||
116 1.1 cgd strcmp(fs->fs_type, FSTAB_RW))
117 1.1 cgd continue;
118 1.1 cgd if (aflag) {
119 1.1 cgd if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
120 1.1 cgd errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
121 1.1 cgd if (uflag && hasquota(fs, USRQUOTA, &qfnp))
122 1.1 cgd errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
123 1.1 cgd continue;
124 1.1 cgd }
125 1.1 cgd if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
126 1.1 cgd (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
127 1.1 cgd done |= 1 << argnum;
128 1.1 cgd if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
129 1.1 cgd errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
130 1.1 cgd if (uflag && hasquota(fs, USRQUOTA, &qfnp))
131 1.1 cgd errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
132 1.1 cgd }
133 1.1 cgd }
134 1.1 cgd endfsent();
135 1.1 cgd for (i = 0; i < argc; i++)
136 1.1 cgd if ((done & (1 << i)) == 0)
137 1.1 cgd fprintf(stderr, "%s not found in fstab\n",
138 1.1 cgd argv[i]);
139 1.1 cgd exit(errs);
140 1.1 cgd }
141 1.1 cgd
142 1.1 cgd usage(whoami)
143 1.1 cgd char *whoami;
144 1.1 cgd {
145 1.1 cgd
146 1.1 cgd fprintf(stderr, "Usage:\n\t%s [-g] [-u] [-v] -a\n", whoami);
147 1.1 cgd fprintf(stderr, "\t%s [-g] [-u] [-v] filesys ...\n", whoami);
148 1.1 cgd exit(1);
149 1.1 cgd }
150 1.1 cgd
151 1.1 cgd quotaonoff(fs, offmode, type, qfpathname)
152 1.1 cgd register struct fstab *fs;
153 1.1 cgd int offmode, type;
154 1.1 cgd char *qfpathname;
155 1.1 cgd {
156 1.1 cgd
157 1.1 cgd if (strcmp(fs->fs_file, "/") && readonly(fs))
158 1.1 cgd return (1);
159 1.1 cgd if (offmode) {
160 1.1 cgd if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0) {
161 1.1 cgd fprintf(stderr, "quotaoff: ");
162 1.1 cgd perror(fs->fs_file);
163 1.1 cgd return (1);
164 1.1 cgd }
165 1.1 cgd if (vflag)
166 1.1 cgd printf("%s: quotas turned off\n", fs->fs_file);
167 1.1 cgd return (0);
168 1.1 cgd }
169 1.1 cgd if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, qfpathname) < 0) {
170 1.1 cgd fprintf(stderr, "quotaon: using %s on", qfpathname);
171 1.1 cgd perror(fs->fs_file);
172 1.1 cgd return (1);
173 1.1 cgd }
174 1.1 cgd if (vflag)
175 1.1 cgd printf("%s: %s quotas turned on\n", fs->fs_file,
176 1.1 cgd qfextension[type]);
177 1.1 cgd return (0);
178 1.1 cgd }
179 1.1 cgd
180 1.1 cgd /*
181 1.1 cgd * Check to see if target appears in list of size cnt.
182 1.1 cgd */
183 1.1 cgd oneof(target, list, cnt)
184 1.1 cgd register char *target, *list[];
185 1.1 cgd int cnt;
186 1.1 cgd {
187 1.1 cgd register int i;
188 1.1 cgd
189 1.1 cgd for (i = 0; i < cnt; i++)
190 1.1 cgd if (strcmp(target, list[i]) == 0)
191 1.1 cgd return (i);
192 1.1 cgd return (-1);
193 1.1 cgd }
194 1.1 cgd
195 1.1 cgd /*
196 1.1 cgd * Check to see if a particular quota is to be enabled.
197 1.1 cgd */
198 1.1 cgd hasquota(fs, type, qfnamep)
199 1.1 cgd register struct fstab *fs;
200 1.1 cgd int type;
201 1.1 cgd char **qfnamep;
202 1.1 cgd {
203 1.1 cgd register char *opt;
204 1.1 cgd char *cp, *index(), *strtok();
205 1.1 cgd static char initname, usrname[100], grpname[100];
206 1.1 cgd static char buf[BUFSIZ];
207 1.1 cgd
208 1.1 cgd if (!initname) {
209 1.1 cgd sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname);
210 1.1 cgd sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname);
211 1.1 cgd initname = 1;
212 1.1 cgd }
213 1.1 cgd strcpy(buf, fs->fs_mntops);
214 1.1 cgd for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
215 1.1 cgd if (cp = index(opt, '='))
216 1.1 cgd *cp++ = '\0';
217 1.1 cgd if (type == USRQUOTA && strcmp(opt, usrname) == 0)
218 1.1 cgd break;
219 1.1 cgd if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
220 1.1 cgd break;
221 1.1 cgd }
222 1.1 cgd if (!opt)
223 1.1 cgd return (0);
224 1.1 cgd if (cp) {
225 1.1 cgd *qfnamep = cp;
226 1.1 cgd return (1);
227 1.1 cgd }
228 1.1 cgd (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
229 1.1 cgd *qfnamep = buf;
230 1.1 cgd return (1);
231 1.1 cgd }
232 1.1 cgd
233 1.1 cgd /*
234 1.1 cgd * Verify file system is mounted and not readonly.
235 1.1 cgd */
236 1.1 cgd readonly(fs)
237 1.1 cgd register struct fstab *fs;
238 1.1 cgd {
239 1.1 cgd struct statfs fsbuf;
240 1.1 cgd
241 1.1 cgd if (statfs(fs->fs_file, &fsbuf) < 0 ||
242 1.1 cgd strcmp(fsbuf.f_mntonname, fs->fs_file) ||
243 1.1 cgd strcmp(fsbuf.f_mntfromname, fs->fs_spec)) {
244 1.1 cgd printf("%s: not mounted\n", fs->fs_file);
245 1.1 cgd return (1);
246 1.1 cgd }
247 1.1 cgd if (fsbuf.f_flags & MNT_RDONLY) {
248 1.1 cgd printf("%s: mounted read-only\n", fs->fs_file);
249 1.1 cgd return (1);
250 1.1 cgd }
251 1.1 cgd return (0);
252 1.1 cgd }
253