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