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.6 1995/11/28 19:43:45 jtc 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 <string.h> 57 #include <fstab.h> 58 59 char *qfname = QUOTAFILENAME; 60 char *qfextension[] = INITQFNAMES; 61 62 int aflag; /* all file systems */ 63 int gflag; /* operate on group quotas */ 64 int uflag; /* operate on user quotas */ 65 int vflag; /* verbose */ 66 67 main(argc, argv) 68 int argc; 69 char **argv; 70 { 71 register struct fstab *fs; 72 char ch, *qfnp, *whoami, *rindex(); 73 long argnum, done = 0; 74 int i, offmode = 0, errs = 0; 75 extern char *optarg; 76 extern int optind; 77 78 whoami = rindex(*argv, '/') + 1; 79 if (whoami == (char *)1) 80 whoami = *argv; 81 if (strcmp(whoami, "quotaoff") == 0) 82 offmode++; 83 else if (strcmp(whoami, "quotaon") != 0) { 84 fprintf(stderr, "Name must be quotaon or quotaoff not %s\n", 85 whoami); 86 exit(1); 87 } 88 while ((ch = getopt(argc, argv, "avug")) != EOF) { 89 switch(ch) { 90 case 'a': 91 aflag++; 92 break; 93 case 'g': 94 gflag++; 95 break; 96 case 'u': 97 uflag++; 98 break; 99 case 'v': 100 vflag++; 101 break; 102 default: 103 usage(whoami); 104 } 105 } 106 argc -= optind; 107 argv += optind; 108 if (argc <= 0 && !aflag) 109 usage(whoami); 110 if (!gflag && !uflag) { 111 gflag++; 112 uflag++; 113 } 114 setfsent(); 115 while ((fs = getfsent()) != NULL) { 116 if (strcmp(fs->fs_vfstype, "ffs") || 117 strcmp(fs->fs_type, FSTAB_RW)) 118 continue; 119 if (aflag) { 120 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) 121 errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp); 122 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) 123 errs += quotaonoff(fs, offmode, USRQUOTA, qfnp); 124 continue; 125 } 126 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 || 127 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) { 128 done |= 1 << argnum; 129 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) 130 errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp); 131 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) 132 errs += quotaonoff(fs, offmode, USRQUOTA, qfnp); 133 } 134 } 135 endfsent(); 136 for (i = 0; i < argc; i++) 137 if ((done & (1 << i)) == 0) 138 fprintf(stderr, "%s not found in fstab\n", 139 argv[i]); 140 exit(errs); 141 } 142 143 usage(whoami) 144 char *whoami; 145 { 146 147 fprintf(stderr, "Usage:\n\t%s [-g] [-u] [-v] -a\n", whoami); 148 fprintf(stderr, "\t%s [-g] [-u] [-v] filesys ...\n", whoami); 149 exit(1); 150 } 151 152 quotaonoff(fs, offmode, type, qfpathname) 153 register struct fstab *fs; 154 int offmode, type; 155 char *qfpathname; 156 { 157 158 if (strcmp(fs->fs_file, "/") && readonly(fs)) 159 return (1); 160 if (offmode) { 161 if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0) { 162 fprintf(stderr, "quotaoff: "); 163 perror(fs->fs_file); 164 return (1); 165 } 166 if (vflag) 167 printf("%s: quotas turned off\n", fs->fs_file); 168 return (0); 169 } 170 if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, qfpathname) < 0) { 171 fprintf(stderr, "quotaon: using %s on", qfpathname); 172 perror(fs->fs_file); 173 return (1); 174 } 175 if (vflag) 176 printf("%s: %s quotas turned on\n", fs->fs_file, 177 qfextension[type]); 178 return (0); 179 } 180 181 /* 182 * Check to see if target appears in list of size cnt. 183 */ 184 oneof(target, list, cnt) 185 register char *target, *list[]; 186 int cnt; 187 { 188 register int i; 189 190 for (i = 0; i < cnt; i++) 191 if (strcmp(target, list[i]) == 0) 192 return (i); 193 return (-1); 194 } 195 196 /* 197 * Check to see if a particular quota is to be enabled. 198 */ 199 hasquota(fs, type, qfnamep) 200 register struct fstab *fs; 201 int type; 202 char **qfnamep; 203 { 204 register char *opt; 205 char *cp, *index(), *strtok(); 206 static char initname, usrname[100], grpname[100]; 207 static char buf[BUFSIZ]; 208 209 if (!initname) { 210 sprintf(usrname, "%s%s", qfextension[USRQUOTA], qfname); 211 sprintf(grpname, "%s%s", qfextension[GRPQUOTA], qfname); 212 initname = 1; 213 } 214 strcpy(buf, fs->fs_mntops); 215 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 216 if (cp = index(opt, '=')) 217 *cp++ = '\0'; 218 if (type == USRQUOTA && strcmp(opt, usrname) == 0) 219 break; 220 if (type == GRPQUOTA && strcmp(opt, grpname) == 0) 221 break; 222 } 223 if (!opt) 224 return (0); 225 if (cp) { 226 *qfnamep = cp; 227 return (1); 228 } 229 (void) sprintf(buf, "%s/%s.%s", fs->fs_file, qfname, qfextension[type]); 230 *qfnamep = buf; 231 return (1); 232 } 233 234 /* 235 * Verify file system is mounted and not readonly. 236 */ 237 readonly(fs) 238 register struct fstab *fs; 239 { 240 struct statfs fsbuf; 241 242 if (statfs(fs->fs_file, &fsbuf) < 0 || 243 strcmp(fsbuf.f_mntonname, fs->fs_file) || 244 strcmp(fsbuf.f_mntfromname, fs->fs_spec)) { 245 printf("%s: not mounted\n", fs->fs_file); 246 return (1); 247 } 248 if (fsbuf.f_flags & MNT_RDONLY) { 249 printf("%s: mounted read-only\n", fs->fs_file); 250 return (1); 251 } 252 return (0); 253 } 254