]>
git.cameronkatri.com Git - apple_cmds.git/blob - diskdev_cmds/quotaon.tproj/quotaon.c
2 * Copyright (c) 1999, 2002, 2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1980, 1990, 1993
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
28 * Robert Elz at The University of Melbourne.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/cdefs.h>
62 __unused
static char copyright
[] =
63 "@(#) Copyright (c) 1980, 1990, 1993\n\
64 The Regents of the University of California. All rights reserved.\n";
68 __unused
static char sccsid
[] = "@(#)quotaon.c 8.1 (Berkeley) 6/6/93";
72 * Turn quota on/off for a filesystem.
74 #include <sys/param.h>
76 #include <sys/mount.h>
79 #endif /* __APPLE__ */
80 #include <sys/appleapiopts.h>
81 #include <sys/quota.h>
88 /* Internal functions */
89 void usage(char *whoami
);
91 char *qfname
= QUOTAFILENAME
;
92 char *qfextension
[] = INITQFNAMES
;
94 int aflag
; /* all file systems */
95 int gflag
; /* operate on group quotas */
96 int uflag
; /* operate on user quotas */
97 int vflag
; /* verbose */
99 /* Function prototypes */
101 int hasquota(register struct statfs
*, int, char **);
102 int quotaonoff(register struct statfs
*, int, int, char *);
104 int hasquota(register struct fstab
*, int, char **);
105 int quotaonoff(register struct fstab
*, int, int, char *);
106 #endif /* __APPLE__ */
108 int oneof(register char *, register char **, int);
114 char ch
, *qfnp
, *whoami
, *rindex();
115 long argnum
, done
= 0;
116 int i
, offmode
= 0, errs
= 0;
120 #endif /* __APPLE__ */
122 whoami
= rindex(*argv
, '/') + 1;
123 if (whoami
== (char *)1)
125 if (strcmp(whoami
, "quotaoff") == 0)
127 else if (strcmp(whoami
, "quotaon") != 0) {
128 fprintf(stderr
, "Name must be quotaon or quotaoff not %s\n",
132 while ((ch
= getopt(argc
, argv
, "avug")) != EOF
) {
152 if (argc
<= 0 && !aflag
)
154 if (!gflag
&& !uflag
) {
160 nfst
= getmntinfo(&fst
, MNT_WAIT
);
162 fprintf(stderr
, "no filesystems mounted");
166 for (i
=0; i
<nfst
; i
++) {
167 if(strcmp(fst
[i
].f_fstypename
, "hfs")) {
170 if(fst
[i
].f_flags
& MNT_RDONLY
) {
176 if (gflag
&& hasquota(&fst
[i
], GRPQUOTA
, &qfnp
))
177 errs
+= quotaonoff(&fst
[i
], offmode
, GRPQUOTA
, qfnp
);
178 if (uflag
&& hasquota(&fst
[i
], USRQUOTA
, &qfnp
))
179 errs
+= quotaonoff(&fst
[i
], offmode
, USRQUOTA
, qfnp
);
182 if ((argnum
= oneof(fst
[i
].f_mntonname
, argv
, argc
)) >= 0 ||
183 (argnum
= oneof(fst
[i
].f_mntfromname
, argv
, argc
)) >= 0) {
185 if (gflag
&& hasquota(&fst
[i
], GRPQUOTA
, &qfnp
))
186 errs
+= quotaonoff(&fst
[i
], offmode
, GRPQUOTA
, qfnp
);
187 if (uflag
&& hasquota(&fst
[i
], USRQUOTA
, &qfnp
))
188 errs
+= quotaonoff(&fst
[i
], offmode
, USRQUOTA
, qfnp
);
192 register struct fstab
*fs
;
194 while ((fs
= getfsent()) != NULL
) {
195 if (strcmp(fs
->fs_vfstype
, "ufs") ||
196 strcmp(fs
->fs_type
, FSTAB_RW
))
199 if (gflag
&& hasquota(fs
, GRPQUOTA
, &qfnp
))
200 errs
+= quotaonoff(fs
, offmode
, GRPQUOTA
, qfnp
);
201 if (uflag
&& hasquota(fs
, USRQUOTA
, &qfnp
))
202 errs
+= quotaonoff(fs
, offmode
, USRQUOTA
, qfnp
);
205 if ((argnum
= oneof(fs
->fs_file
, argv
, argc
)) >= 0 ||
206 (argnum
= oneof(fs
->fs_spec
, argv
, argc
)) >= 0) {
208 if (gflag
&& hasquota(fs
, GRPQUOTA
, &qfnp
))
209 errs
+= quotaonoff(fs
, offmode
, GRPQUOTA
, qfnp
);
210 if (uflag
&& hasquota(fs
, USRQUOTA
, &qfnp
))
211 errs
+= quotaonoff(fs
, offmode
, USRQUOTA
, qfnp
);
215 #endif /* __APPLE__ */
216 for (i
= 0; i
< argc
; i
++)
217 if ((done
& (1 << i
)) == 0)
218 fprintf(stderr
, "%s not found in fstab\n",
227 fprintf(stderr
, "Usage:\n\t%s [-g] [-u] [-v] -a\n", whoami
);
228 fprintf(stderr
, "\t%s [-g] [-u] [-v] filesys ...\n", whoami
);
233 int quotaonoff(fst
, offmode
, type
, qfpathname
)
234 register struct statfs
*fst
;
238 if (strcmp(fst
->f_mntonname
, "/") && (fst
->f_flags
& MNT_RDONLY
))
241 if (quotactl(fst
->f_mntonname
, QCMD(Q_QUOTAOFF
, type
), 0, 0) < 0) {
242 fprintf(stderr
, "quotaoff: ");
243 perror(fst
->f_mntonname
);
247 printf("%s: %s quotas turned off\n", fst
->f_mntonname
,
251 if (quotactl(fst
->f_mntonname
, QCMD(Q_QUOTAON
, type
), 0, qfpathname
) < 0) {
252 fprintf(stderr
, "quotaon: using %s on ", qfpathname
);
253 perror(fst
->f_mntonname
);
257 printf("%s: %s quotas turned on\n", fst
->f_mntonname
,
262 int quotaonoff(fs
, offmode
, type
, qfpathname
)
263 register struct fstab
*fs
;
268 if (strcmp(fs
->fs_file
, "/") && readonly(fs
))
271 if (quotactl(fs
->fs_file
, QCMD(Q_QUOTAOFF
, type
), 0, 0) < 0) {
272 fprintf(stderr
, "quotaoff: ");
277 printf("%s: quotas turned off\n", fs
->fs_file
);
280 if (quotactl(fs
->fs_file
, QCMD(Q_QUOTAON
, type
), 0, qfpathname
) < 0) {
281 fprintf(stderr
, "quotaon: using %s on", qfpathname
);
286 printf("%s: %s quotas turned on\n", fs
->fs_file
,
290 #endif /* __APPLE__ */
293 * Check to see if target appears in list of size cnt.
295 int oneof(target
, list
, cnt
)
296 register char *target
, *list
[];
301 for (i
= 0; i
< cnt
; i
++)
302 if (strcmp(target
, list
[i
]) == 0)
308 * Check to see if a particular quota is to be enabled.
311 int hasquota(fst
, type
, qfnamep
)
312 register struct statfs
*fst
;
317 static char initname
, usrname
[100], grpname
[100];
318 static char buf
[BUFSIZ
];
321 snprintf(usrname
, sizeof(usrname
), "%s%s", qfextension
[USRQUOTA
], qfname
);
322 snprintf(grpname
, sizeof(grpname
), "%s%s", qfextension
[GRPQUOTA
], qfname
);
327 We only support the default path to the
331 (void)snprintf(buf
, sizeof(buf
), "%s/%s.%s", fst
->f_mntonname
,
332 QUOTAOPSNAME
, qfextension
[type
] );
333 if (stat(buf
, &sb
) != 0) {
334 /* There appears to be no mount option file */
339 (void) snprintf(buf
, sizeof(buf
), "%s/%s.%s", fst
->f_mntonname
, qfname
, qfextension
[type
]);
344 int hasquota(fs
, type
, qfnamep
)
345 register struct fstab
*fs
;
350 char *cp
, *index(), *strtok();
351 static char initname
, usrname
[100], grpname
[100];
352 static char buf
[BUFSIZ
];
355 snprintf(usrname
, sizeof(usrname
), "%s%s", qfextension
[USRQUOTA
], qfname
);
356 snprintf(grpname
, sizeof(grpname
), "%s%s", qfextension
[GRPQUOTA
], qfname
);
359 strlcpy(buf
, fs
->fs_mntops
, sizeof(buf
));
360 for (opt
= strtok(buf
, ","); opt
; opt
= strtok(NULL
, ",")) {
361 if (cp
= index(opt
, '='))
363 if (type
== USRQUOTA
&& strcmp(opt
, usrname
) == 0)
365 if (type
== GRPQUOTA
&& strcmp(opt
, grpname
) == 0)
374 (void) snprintf(buf
, sizeof(buf
), "%s/%s.%s", fs
->fs_file
, qfname
, qfextension
[type
]);
378 #endif /* __APPLE__ */
382 * Verify file system is mounted and not readonly.
385 register struct fstab
*fs
;
389 if (statfs(fs
->fs_file
, &fsbuf
) < 0 ||
390 strcmp(fsbuf
.f_mntonname
, fs
->fs_file
) ||
391 strcmp(fsbuf
.f_mntfromname
, fs
->fs_spec
)) {
392 printf("%s: not mounted\n", fs
->fs_file
);
395 if (fsbuf
.f_flags
& MNT_RDONLY
) {
396 printf("%s: mounted read-only\n", fs
->fs_file
);