8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / fs.d / ufs / quotaon / quotaon.c
blobbd93f45812410d95985fa8e2b3bb94aace469202
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * University Copyright- Copyright (c) 1982, 1986, 1988
31 * The Regents of the University of California
32 * All Rights Reserved
34 * University Acknowledgment- Portions of this document are derived from
35 * software developed by the University of California, Berkeley, and its
36 * contributors.
39 #pragma ident "%Z%%M% %I% %E% SMI"
42 * Turn quota on/off for a filesystem.
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/mntent.h>
49 #define bcopy(f, t, n) memcpy(t, f, n)
50 #define bzero(s, n) memset(s, 0, n)
51 #define bcmp(s, d, n) memcmp(s, d, n)
53 #define index(s, r) strchr(s, r)
54 #define rindex(s, r) strrchr(s, r)
56 #include <string.h>
57 #include <stdlib.h>
58 #include <unistd.h>
59 #include <sys/file.h>
60 #include <signal.h>
61 #include <fcntl.h>
62 #include <sys/fs/ufs_quota.h>
63 #include <stdio.h>
64 #include <sys/mnttab.h>
65 #include <errno.h>
66 #include <sys/vfstab.h>
68 int vflag; /* verbose */
69 int aflag; /* all file systems */
71 #define QFNAME "quotas"
72 #define CHUNK 50
73 char **listbuf;
74 char *mntopt(), *hasvfsopt(), *hasmntopt();
75 char *whoami;
77 static void fixmntent();
78 static void mnterror();
79 static void usage(char *);
80 static int oneof();
81 static int quotaonoff();
82 static int quotactl(int, char *, uid_t, caddr_t);
84 extern int optind;
85 extern char *optarg;
87 int
88 main(int argc, char **argv)
90 struct mnttab mntp;
91 struct vfstab vfsbuf;
92 char **listp;
93 int listcnt;
94 FILE *mtab, *vfstab, *tmp;
95 int offmode = 0;
96 int listmax = 0;
97 int errs = 0;
98 char *tmpname = "/etc/mnttab.temp";
99 int status;
100 int opt;
101 mode_t oldumask;
102 struct stat statbuf;
104 whoami = (char *)rindex(*argv, '/') + 1;
105 if (whoami == (char *)1)
106 whoami = *argv;
107 if (strcmp(whoami, "quotaoff") == 0)
108 offmode++;
109 else if (strcmp(whoami, "quotaon") != 0) {
110 fprintf(stderr, "Name must be quotaon or quotaoff not %s\n",
111 whoami);
112 exit(31+1);
114 if ((listbuf = (char **)malloc(sizeof (char *) * CHUNK)) == NULL) {
115 fprintf(stderr, "Can't alloc lisbuf array.");
116 exit(31+1);
118 listmax = CHUNK;
119 while ((opt = getopt(argc, argv, "avV")) != EOF) {
120 switch (opt) {
122 case 'v':
123 vflag++;
124 break;
126 case 'a':
127 aflag++;
128 break;
130 case 'V': /* Print command line */
132 char *opt_text;
133 int opt_cnt;
135 (void) fprintf(stdout, "%s -F UFS ", whoami);
136 for (opt_cnt = 1; opt_cnt < argc; opt_cnt++) {
137 opt_text = argv[opt_cnt];
138 if (opt_text)
139 (void) fprintf(stdout, " %s ",
140 opt_text);
142 (void) fprintf(stdout, "\n");
144 break;
146 case '?':
147 usage(whoami);
150 if (argc <= optind && !aflag) {
151 usage(whoami);
154 * If aflag go through vfstab and make a list of appropriate
155 * filesystems.
157 if (aflag) {
159 listp = listbuf;
160 listcnt = 0;
162 vfstab = fopen(VFSTAB, "r");
163 if (vfstab == NULL) {
164 fprintf(stderr, "Can't open %s\n", VFSTAB);
165 perror(VFSTAB);
166 exit(31+1);
169 while ((status = getvfsent(vfstab, &vfsbuf)) == NULL) {
170 if (strcmp(vfsbuf.vfs_fstype, MNTTYPE_UFS) != 0 ||
171 (vfsbuf.vfs_mntopts == 0) ||
172 hasvfsopt(&vfsbuf, MNTOPT_RO) ||
173 (!hasvfsopt(&vfsbuf, MNTOPT_RQ) &&
174 !hasvfsopt(&vfsbuf, MNTOPT_QUOTA)))
175 continue;
176 *listp = malloc(strlen(vfsbuf.vfs_special) + 1);
177 strcpy(*listp, vfsbuf.vfs_special);
178 listp++;
179 listcnt++;
180 /* grow listbuf if needed */
181 if (listcnt >= listmax) {
182 listmax += CHUNK;
183 listbuf = (char **)realloc(listbuf,
184 sizeof (char *) * listmax);
185 if (listbuf == NULL) {
186 fprintf(stderr,
187 "Can't grow listbuf.\n");
188 exit(31+1);
190 listp = &listbuf[listcnt];
193 fclose(vfstab);
194 *listp = (char *)0;
195 listp = listbuf;
196 } else {
197 listp = &argv[optind];
198 listcnt = argc - optind;
202 * Open real mnttab
204 mtab = fopen(MNTTAB, "r");
205 if (mtab == NULL) {
206 fprintf(stderr, "Can't open %s\n", MNTTAB);
207 perror(whoami);
208 exit(31+1);
210 /* check every entry for validity before we change mnttab */
211 while ((status = getmntent(mtab, &mntp)) == 0)
213 if (status > 0)
214 mnterror(status);
215 rewind(mtab);
217 signal(SIGHUP, SIG_IGN);
218 signal(SIGQUIT, SIG_IGN);
219 signal(SIGINT, SIG_IGN);
222 * Loop through mnttab, if a file system gets turned on or off
223 * do the quota call.
225 while ((status = getmntent(mtab, &mntp)) == NULL) {
226 if (strcmp(mntp.mnt_fstype, MNTTYPE_UFS) == 0 &&
227 !hasmntopt(&mntp, MNTOPT_RO) &&
228 (oneof(mntp.mnt_special, listp, listcnt) ||
229 oneof(mntp.mnt_mountp, listp, listcnt))) {
230 errs += quotaonoff(&mntp, offmode);
233 fclose(mtab);
235 while (listcnt--) {
236 if (*listp) {
237 fprintf(stderr, "Cannot do %s\n", *listp);
238 errs++;
240 listp++;
242 if (errs > 0)
243 errs += 31;
244 return (errs);
248 quotaonoff(struct mnttab *mntp, int offmode)
251 if (offmode) {
252 if (quotactl(Q_QUOTAOFF, mntp->mnt_mountp, (uid_t)0, NULL) < 0)
253 goto bad;
254 if (vflag)
255 printf("%s: quotas turned off\n", mntp->mnt_mountp);
256 } else {
257 if (quotactl(Q_QUOTAON, mntp->mnt_mountp, (uid_t)0, NULL) <
259 goto bad;
260 if (vflag)
261 printf("%s: quotas turned on\n", mntp->mnt_mountp);
263 return (0);
264 bad:
265 fprintf(stderr, "quotactl: ");
266 perror(mntp->mnt_special);
267 return (1);
271 oneof(char *target, char **olistp, int on)
273 int n = on;
274 char **listp = olistp;
276 while (n--) {
277 if (*listp && strcmp(target, *listp) == 0) {
278 *listp = (char *)0;
279 return (1);
281 listp++;
283 return (0);
286 void
287 usage(char *whoami)
290 fprintf(stderr, "ufs usage:\n");
291 fprintf(stderr, "\t%s [-v] -a\n", whoami);
292 fprintf(stderr, "\t%s [-v] filesys ...\n", whoami);
293 exit(31+1);
298 quotactl(int cmd, char *mountpt, uid_t uid, caddr_t addr)
300 int fd;
301 int status;
302 struct quotctl quota;
303 char qfile[MAXPATHLEN];
305 if (mountpt == NULL || mountpt[0] == '\0') {
306 errno = ENOENT;
307 return (-1);
309 if ((strlcpy(qfile, mountpt, sizeof (qfile)) >= sizeof (qfile)) ||
310 (strlcat(qfile, "/" QFNAME, sizeof (qfile)) >= sizeof (qfile))) {
311 errno = ENOENT;
312 return (-1);
314 if ((fd = open64(qfile, O_RDWR)) < 0) {
315 fprintf(stderr, "quotactl: %s ", qfile);
316 perror("open");
317 exit(31+1);
320 quota.op = cmd;
321 quota.uid = uid;
322 quota.addr = addr;
323 status = ioctl(fd, Q_QUOTACTL, &quota);
324 close(fd);
325 return (status);
328 char *
329 hasvfsopt(struct vfstab *vfs, char *opt)
331 char *f, *opts;
332 static char *tmpopts;
334 if (tmpopts == 0) {
335 tmpopts = (char *)calloc(256, sizeof (char));
336 if (tmpopts == 0)
337 return (0);
339 strcpy(tmpopts, vfs->vfs_mntopts);
340 opts = tmpopts;
341 f = mntopt(&opts);
342 for (; *f; f = mntopt(&opts)) {
343 if (strncmp(opt, f, strlen(opt)) == 0)
344 return (f - tmpopts + vfs->vfs_mntopts);
346 return (NULL);
349 void
350 mnterror(int flag)
352 switch (flag) {
353 case MNT_TOOLONG:
354 fprintf(stderr, "%s: line in mnttab exceeds %d characters\n",
355 whoami, MNT_LINE_MAX-2);
356 break;
357 case MNT_TOOFEW:
358 fprintf(stderr, "%s: line in mnttab has too few entries\n",
359 whoami);
360 break;
361 case MNT_TOOMANY:
362 fprintf(stderr, "%s: line in mnttab has too many entries\n",
363 whoami);
364 break;
366 exit(1);