1 /* $NetBSD: quotaon.c,v 1.23 2009/04/18 08:20:41 lukem Exp $ */ 2 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 #include <sys/cdefs.h> 36 #ifndef lint 37 __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\ 38 The Regents of the University of California. All rights reserved."); 39 #endif /* not lint */ 40 41 #ifndef lint 42 #if 0 43 static char sccsid[] = "@(#)quotaon.c 8.1 (Berkeley) 6/6/93"; 44 #else 45 __RCSID("$NetBSD: quotaon.c,v 1.23 2009/04/18 08:20:41 lukem Exp $"); 46 #endif 47 #endif /* not lint */ 48 49 /* 50 * Turn quota on/off for a filesystem. 51 */ 52 #include <sys/param.h> 53 #include <sys/file.h> 54 #include <sys/mount.h> 55 #include <ufs/ufs/quota.h> 56 57 #include <err.h> 58 #include <fstab.h> 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <string.h> 62 #include <unistd.h> 63 64 const char *qfname = QUOTAFILENAME; 65 const char *qfextension[] = INITQFNAMES; 66 67 int aflag; /* all file systems */ 68 int gflag; /* operate on group quotas */ 69 int uflag; /* operate on user quotas */ 70 int vflag; /* verbose */ 71 72 int main __P((int, char *[])); 73 74 static void usage __P((void)); 75 static int quotaonoff __P((struct fstab *, int, int, char *)); 76 static int oneof __P((const char *, char *[], int)); 77 static int hasquota __P((struct fstab *, int, char **)); 78 static int readonly __P((struct fstab *)); 79 80 int 81 main(argc, argv) 82 int argc; 83 char *argv[]; 84 { 85 struct fstab *fs; 86 char *qfnp; 87 long argnum, done = 0; 88 int i, offmode = 0, errs = 0; 89 int ch; 90 91 if (strcmp(getprogname(), "quotaoff") == 0) 92 offmode++; 93 else if (strcmp(getprogname(), "quotaon") != 0) 94 errx(1, "Name must be quotaon or quotaoff"); 95 96 while ((ch = getopt(argc, argv, "avug")) != -1) { 97 switch(ch) { 98 case 'a': 99 aflag++; 100 break; 101 case 'g': 102 gflag++; 103 break; 104 case 'u': 105 uflag++; 106 break; 107 case 'v': 108 vflag++; 109 break; 110 default: 111 usage(); 112 break; 113 } 114 } 115 argc -= optind; 116 argv += optind; 117 118 if (argc <= 0 && !aflag) 119 usage(); 120 121 if (!gflag && !uflag) { 122 gflag++; 123 uflag++; 124 } 125 setfsent(); 126 while ((fs = getfsent()) != NULL) { 127 if ((strcmp(fs->fs_vfstype, "ffs") && 128 strcmp(fs->fs_vfstype, "lfs")) || 129 strcmp(fs->fs_type, FSTAB_RW)) 130 continue; 131 if (aflag) { 132 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) 133 errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp); 134 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) 135 errs += quotaonoff(fs, offmode, USRQUOTA, qfnp); 136 continue; 137 } 138 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 || 139 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) { 140 done |= 1 << argnum; 141 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) 142 errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp); 143 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) 144 errs += quotaonoff(fs, offmode, USRQUOTA, qfnp); 145 } 146 } 147 endfsent(); 148 for (i = 0; i < argc; i++) 149 if ((done & (1 << i)) == 0) 150 warnx("%s not found in fstab", argv[i]); 151 exit(errs); 152 } 153 154 static void 155 usage() 156 { 157 158 (void) fprintf(stderr, "usage:\n\t%s [-g] [-u] [-v] -a\n", 159 getprogname()); 160 (void) fprintf(stderr, "\t%s [-g] [-u] [-v] filesys ...\n", 161 getprogname()); 162 exit(1); 163 } 164 165 static int 166 quotaonoff(fs, offmode, type, qfpathname) 167 struct fstab *fs; 168 int offmode, type; 169 char *qfpathname; 170 { 171 172 if (strcmp(fs->fs_file, "/") && readonly(fs)) 173 return (1); 174 if (offmode) { 175 if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0) { 176 warn("%s", fs->fs_file); 177 return (1); 178 } 179 if (vflag) 180 printf("%s: %s quotas turned off\n", 181 fs->fs_file, qfextension[type]); 182 return (0); 183 } 184 if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, qfpathname) < 0) { 185 warn("%s quotas using %s on %s", 186 qfextension[type], qfpathname, fs->fs_file); 187 return (1); 188 } 189 if (vflag) 190 printf("%s: %s quotas turned on\n", fs->fs_file, 191 qfextension[type]); 192 return (0); 193 } 194 195 /* 196 * Check to see if target appears in list of size cnt. 197 */ 198 static int 199 oneof(target, list, cnt) 200 const char *target; 201 char *list[]; 202 int cnt; 203 { 204 int i; 205 206 for (i = 0; i < cnt; i++) 207 if (strcmp(target, list[i]) == 0) 208 return (i); 209 return (-1); 210 } 211 212 /* 213 * Check to see if a particular quota is to be enabled. 214 */ 215 static int 216 hasquota(fs, type, qfnamep) 217 struct fstab *fs; 218 int type; 219 char **qfnamep; 220 { 221 char *opt; 222 char *cp = NULL; 223 static char initname, usrname[100], grpname[100]; 224 static char buf[BUFSIZ]; 225 226 if (!initname) { 227 (void) snprintf(usrname, sizeof(usrname), "%s%s", 228 qfextension[USRQUOTA], qfname); 229 (void) snprintf(grpname, sizeof(grpname), "%s%s", 230 qfextension[GRPQUOTA], qfname); 231 initname = 1; 232 } 233 strcpy(buf, fs->fs_mntops); 234 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 235 if ((cp = strchr(opt, '=')) != NULL) 236 *cp++ = '\0'; 237 if (type == USRQUOTA && strcmp(opt, usrname) == 0) 238 break; 239 if (type == GRPQUOTA && strcmp(opt, grpname) == 0) 240 break; 241 } 242 if (!opt) 243 return (0); 244 if (cp) { 245 *qfnamep = cp; 246 return (1); 247 } 248 (void) snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file, qfname, 249 qfextension[type]); 250 *qfnamep = buf; 251 return (1); 252 } 253 254 /* 255 * Verify file system is mounted and not readonly. 256 */ 257 static int 258 readonly(fs) 259 struct fstab *fs; 260 { 261 struct statvfs fsbuf; 262 263 if (statvfs(fs->fs_file, &fsbuf) < 0 || 264 strcmp(fsbuf.f_mntonname, fs->fs_file) || 265 strcmp(fsbuf.f_mntfromname, fs->fs_spec)) { 266 printf("%s: not mounted\n", fs->fs_file); 267 return (1); 268 } 269 if (fsbuf.f_flag & MNT_RDONLY) { 270 printf("%s: mounted read-only\n", fs->fs_file); 271 return (1); 272 } 273 return (0); 274 } 275