| @@ -1,640 +1,639 @@ | | | @@ -1,640 +1,639 @@ |
1 | /* $NetBSD: quota.c,v 1.47 2012/02/01 17:48:10 dholland Exp $ */ | | 1 | /* $NetBSD: quota.c,v 1.48 2012/02/05 14:14:44 dholland Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 1980, 1990, 1993 | | 4 | * Copyright (c) 1980, 1990, 1993 |
5 | * The Regents of the University of California. All rights reserved. | | 5 | * The Regents of the University of California. All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to Berkeley by | | 7 | * This code is derived from software contributed to Berkeley by |
8 | * Robert Elz at The University of Melbourne. | | 8 | * Robert Elz at The University of Melbourne. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright | | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the | | 16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. | | 17 | * documentation and/or other materials provided with the distribution. |
18 | * 3. Neither the name of the University nor the names of its contributors | | 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 | | 19 | * may be used to endorse or promote products derived from this software |
20 | * without specific prior written permission. | | 20 | * without specific prior written permission. |
21 | * | | 21 | * |
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | | 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 | | 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | | 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 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 | | 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 | | 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 | | 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
32 | * SUCH DAMAGE. | | 32 | * SUCH DAMAGE. |
33 | */ | | 33 | */ |
34 | | | 34 | |
35 | #include <sys/cdefs.h> | | 35 | #include <sys/cdefs.h> |
36 | #ifndef lint | | 36 | #ifndef lint |
37 | __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\ | | 37 | __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\ |
38 | The Regents of the University of California. All rights reserved."); | | 38 | The Regents of the University of California. All rights reserved."); |
39 | #endif /* not lint */ | | 39 | #endif /* not lint */ |
40 | | | 40 | |
41 | #ifndef lint | | 41 | #ifndef lint |
42 | #if 0 | | 42 | #if 0 |
43 | static char sccsid[] = "@(#)quota.c 8.4 (Berkeley) 4/28/95"; | | 43 | static char sccsid[] = "@(#)quota.c 8.4 (Berkeley) 4/28/95"; |
44 | #else | | 44 | #else |
45 | __RCSID("$NetBSD: quota.c,v 1.47 2012/02/01 17:48:10 dholland Exp $"); | | 45 | __RCSID("$NetBSD: quota.c,v 1.48 2012/02/05 14:14:44 dholland Exp $"); |
46 | #endif | | 46 | #endif |
47 | #endif /* not lint */ | | 47 | #endif /* not lint */ |
48 | | | 48 | |
49 | /* | | 49 | /* |
50 | * Disk quota reporting program. | | 50 | * Disk quota reporting program. |
51 | */ | | 51 | */ |
52 | #include <sys/param.h> | | 52 | #include <sys/param.h> |
53 | #include <sys/types.h> | | 53 | #include <sys/types.h> |
54 | #include <sys/file.h> | | 54 | #include <sys/file.h> |
55 | #include <sys/stat.h> | | 55 | #include <sys/stat.h> |
56 | #include <sys/mount.h> | | 56 | #include <sys/mount.h> |
57 | #include <sys/socket.h> | | 57 | #include <sys/socket.h> |
58 | | | 58 | |
59 | #include <assert.h> | | 59 | #include <assert.h> |
60 | #include <ctype.h> | | 60 | #include <ctype.h> |
61 | #include <err.h> | | 61 | #include <err.h> |
62 | #include <errno.h> | | 62 | #include <errno.h> |
63 | #include <fstab.h> | | 63 | #include <fstab.h> |
64 | #include <grp.h> | | 64 | #include <grp.h> |
65 | #include <netdb.h> | | 65 | #include <netdb.h> |
66 | #include <pwd.h> | | 66 | #include <pwd.h> |
67 | #include <stdio.h> | | 67 | #include <stdio.h> |
68 | #include <stdlib.h> | | 68 | #include <stdlib.h> |
69 | #include <string.h> | | 69 | #include <string.h> |
70 | #include <time.h> | | 70 | #include <time.h> |
71 | #include <unistd.h> | | 71 | #include <unistd.h> |
72 | | | 72 | |
73 | #include <quota.h> | | 73 | #include <quota.h> |
74 | #include <quota/quota.h> | | | |
75 | | | 74 | |
76 | #include "printquota.h" | | 75 | #include "printquota.h" |
77 | | | 76 | |
78 | struct quotause { | | 77 | struct quotause { |
79 | struct quotause *next; | | 78 | struct quotause *next; |
80 | uid_t id; | | 79 | uid_t id; |
81 | struct quotaval *qvs; | | 80 | struct quotaval *qvs; |
82 | unsigned numqvs; | | 81 | unsigned numqvs; |
83 | char fsname[MAXPATHLEN + 1]; | | 82 | char fsname[MAXPATHLEN + 1]; |
84 | struct quotahandle *qh; | | 83 | struct quotahandle *qh; |
85 | }; | | 84 | }; |
86 | | | 85 | |
87 | static int anyusage(struct quotaval *, unsigned); | | 86 | static int anyusage(struct quotaval *, unsigned); |
88 | static int anyover(struct quotaval *, unsigned, time_t); | | 87 | static int anyover(struct quotaval *, unsigned, time_t); |
89 | static const char *getovermsg(struct quotaval *, const char *, time_t); | | 88 | static const char *getovermsg(struct quotaval *, const char *, time_t); |
90 | static struct quotause *getprivs(id_t, int); | | 89 | static struct quotause *getprivs(id_t, int); |
91 | static void heading(int, const char *, id_t, const char *, const char *); | | 90 | static void heading(int, const char *, id_t, const char *, const char *); |
92 | static int isover(struct quotaval *qv, time_t now); | | 91 | static int isover(struct quotaval *qv, time_t now); |
93 | static void printqv(struct quotaval *, int, time_t); | | 92 | static void printqv(struct quotaval *, int, time_t); |
94 | static void showgid(gid_t); | | 93 | static void showgid(gid_t); |
95 | static void showgrpname(const char *); | | 94 | static void showgrpname(const char *); |
96 | static void showonequota(int, const char *, id_t, const char *, | | 95 | static void showonequota(int, const char *, id_t, const char *, |
97 | struct quotause *); | | 96 | struct quotause *); |
98 | static void showquotas(int, const char *, id_t, const char *); | | 97 | static void showquotas(int, const char *, id_t, const char *); |
99 | static void showuid(uid_t); | | 98 | static void showuid(uid_t); |
100 | static void showusrname(const char *); | | 99 | static void showusrname(const char *); |
101 | static int unlimited(struct quotaval *qvs, unsigned numqvs); | | 100 | static int unlimited(struct quotaval *qvs, unsigned numqvs); |
102 | static void usage(void) __dead; | | 101 | static void usage(void) __dead; |
103 | | | 102 | |
104 | static int qflag = 0; | | 103 | static int qflag = 0; |
105 | static int vflag = 0; | | 104 | static int vflag = 0; |
106 | static int hflag = 0; | | 105 | static int hflag = 0; |
107 | static int dflag = 0; | | 106 | static int dflag = 0; |
108 | static int Dflag = 0; | | 107 | static int Dflag = 0; |
109 | static uid_t myuid; | | 108 | static uid_t myuid; |
110 | static int needheading; | | 109 | static int needheading; |
111 | | | 110 | |
112 | int | | 111 | int |
113 | main(int argc, char *argv[]) | | 112 | main(int argc, char *argv[]) |
114 | { | | 113 | { |
115 | int ngroups; | | 114 | int ngroups; |
116 | gid_t mygid, gidset[NGROUPS]; | | 115 | gid_t mygid, gidset[NGROUPS]; |
117 | int i, gflag = 0, uflag = 0; | | 116 | int i, gflag = 0, uflag = 0; |
118 | int ch; | | 117 | int ch; |
119 | | | 118 | |
120 | myuid = getuid(); | | 119 | myuid = getuid(); |
121 | while ((ch = getopt(argc, argv, "Ddhugvq")) != -1) { | | 120 | while ((ch = getopt(argc, argv, "Ddhugvq")) != -1) { |
122 | switch(ch) { | | 121 | switch(ch) { |
123 | case 'g': | | 122 | case 'g': |
124 | gflag++; | | 123 | gflag++; |
125 | break; | | 124 | break; |
126 | case 'u': | | 125 | case 'u': |
127 | uflag++; | | 126 | uflag++; |
128 | break; | | 127 | break; |
129 | case 'v': | | 128 | case 'v': |
130 | vflag++; | | 129 | vflag++; |
131 | break; | | 130 | break; |
132 | case 'q': | | 131 | case 'q': |
133 | qflag++; | | 132 | qflag++; |
134 | break; | | 133 | break; |
135 | case 'h': | | 134 | case 'h': |
136 | hflag++; | | 135 | hflag++; |
137 | break; | | 136 | break; |
138 | case 'd': | | 137 | case 'd': |
139 | dflag++; | | 138 | dflag++; |
140 | break; | | 139 | break; |
141 | case 'D': | | 140 | case 'D': |
142 | Dflag++; | | 141 | Dflag++; |
143 | break; | | 142 | break; |
144 | default: | | 143 | default: |
145 | usage(); | | 144 | usage(); |
146 | } | | 145 | } |
147 | } | | 146 | } |
148 | argc -= optind; | | 147 | argc -= optind; |
149 | argv += optind; | | 148 | argv += optind; |
150 | if (!uflag && !gflag) | | 149 | if (!uflag && !gflag) |
151 | uflag++; | | 150 | uflag++; |
152 | if (dflag) { | | 151 | if (dflag) { |
153 | #if 0 | | 152 | #if 0 |
154 | if (myuid != 0) | | 153 | if (myuid != 0) |
155 | errx(1, "-d: permission denied"); | | 154 | errx(1, "-d: permission denied"); |
156 | #endif | | 155 | #endif |
157 | if (uflag) | | 156 | if (uflag) |
158 | showquotas(QUOTA_IDTYPE_USER, "user", 0, ""); | | 157 | showquotas(QUOTA_IDTYPE_USER, "user", 0, ""); |
159 | if (gflag) | | 158 | if (gflag) |
160 | showquotas(QUOTA_IDTYPE_GROUP, "group", 0, ""); | | 159 | showquotas(QUOTA_IDTYPE_GROUP, "group", 0, ""); |
161 | return 0; | | 160 | return 0; |
162 | } | | 161 | } |
163 | if (argc == 0) { | | 162 | if (argc == 0) { |
164 | if (uflag) | | 163 | if (uflag) |
165 | showuid(myuid); | | 164 | showuid(myuid); |
166 | if (gflag) { | | 165 | if (gflag) { |
167 | if (dflag) | | 166 | if (dflag) |
168 | showgid(0); | | 167 | showgid(0); |
169 | else { | | 168 | else { |
170 | mygid = getgid(); | | 169 | mygid = getgid(); |
171 | ngroups = getgroups(NGROUPS, gidset); | | 170 | ngroups = getgroups(NGROUPS, gidset); |
172 | if (ngroups < 0) | | 171 | if (ngroups < 0) |
173 | err(1, "getgroups"); | | 172 | err(1, "getgroups"); |
174 | showgid(mygid); | | 173 | showgid(mygid); |
175 | for (i = 0; i < ngroups; i++) | | 174 | for (i = 0; i < ngroups; i++) |
176 | if (gidset[i] != mygid) | | 175 | if (gidset[i] != mygid) |
177 | showgid(gidset[i]); | | 176 | showgid(gidset[i]); |
178 | } | | 177 | } |
179 | } | | 178 | } |
180 | return 0; | | 179 | return 0; |
181 | } | | 180 | } |
182 | if (uflag && gflag) | | 181 | if (uflag && gflag) |
183 | usage(); | | 182 | usage(); |
184 | if (uflag) { | | 183 | if (uflag) { |
185 | for (; argc > 0; argc--, argv++) { | | 184 | for (; argc > 0; argc--, argv++) { |
186 | if (alldigits(*argv)) | | 185 | if (alldigits(*argv)) |
187 | showuid((uid_t)atoi(*argv)); | | 186 | showuid((uid_t)atoi(*argv)); |
188 | else | | 187 | else |
189 | showusrname(*argv); | | 188 | showusrname(*argv); |
190 | } | | 189 | } |
191 | return 0; | | 190 | return 0; |
192 | } | | 191 | } |
193 | if (gflag) { | | 192 | if (gflag) { |
194 | for (; argc > 0; argc--, argv++) { | | 193 | for (; argc > 0; argc--, argv++) { |
195 | if (alldigits(*argv)) | | 194 | if (alldigits(*argv)) |
196 | showgid((gid_t)atoi(*argv)); | | 195 | showgid((gid_t)atoi(*argv)); |
197 | else | | 196 | else |
198 | showgrpname(*argv); | | 197 | showgrpname(*argv); |
199 | } | | 198 | } |
200 | return 0; | | 199 | return 0; |
201 | } | | 200 | } |
202 | /* NOTREACHED */ | | 201 | /* NOTREACHED */ |
203 | return 0; | | 202 | return 0; |
204 | } | | 203 | } |
205 | | | 204 | |
206 | static void | | 205 | static void |
207 | usage(void) | | 206 | usage(void) |
208 | { | | 207 | { |
209 | const char *p = getprogname(); | | 208 | const char *p = getprogname(); |
210 | fprintf(stderr, "Usage: %s [-Dhguqv]\n" | | 209 | fprintf(stderr, "Usage: %s [-Dhguqv]\n" |
211 | "\t%s [-Dhqv] -u username ...\n" | | 210 | "\t%s [-Dhqv] -u username ...\n" |
212 | "\t%s [-Dhqv] -g groupname ...\n" | | 211 | "\t%s [-Dhqv] -g groupname ...\n" |
213 | "\t%s -d [-Dhguqv]\n", p, p, p, p); | | 212 | "\t%s -d [-Dhguqv]\n", p, p, p, p); |
214 | exit(1); | | 213 | exit(1); |
215 | } | | 214 | } |
216 | | | 215 | |
217 | /* | | 216 | /* |
218 | * Print out quotas for a specified user identifier. | | 217 | * Print out quotas for a specified user identifier. |
219 | */ | | 218 | */ |
220 | static void | | 219 | static void |
221 | showuid(uid_t uid) | | 220 | showuid(uid_t uid) |
222 | { | | 221 | { |
223 | struct passwd *pwd = getpwuid(uid); | | 222 | struct passwd *pwd = getpwuid(uid); |
224 | const char *name; | | 223 | const char *name; |
225 | | | 224 | |
226 | if (pwd == NULL) | | 225 | if (pwd == NULL) |
227 | name = "(no account)"; | | 226 | name = "(no account)"; |
228 | else | | 227 | else |
229 | name = pwd->pw_name; | | 228 | name = pwd->pw_name; |
230 | if (uid != myuid && myuid != 0) { | | 229 | if (uid != myuid && myuid != 0) { |
231 | warnx("%s (uid %d): permission denied", name, uid); | | 230 | warnx("%s (uid %d): permission denied", name, uid); |
232 | return; | | 231 | return; |
233 | } | | 232 | } |
234 | showquotas(QUOTA_IDTYPE_USER, "user", uid, name); | | 233 | showquotas(QUOTA_IDTYPE_USER, "user", uid, name); |
235 | } | | 234 | } |
236 | | | 235 | |
237 | /* | | 236 | /* |
238 | * Print out quotas for a specified user name. | | 237 | * Print out quotas for a specified user name. |
239 | */ | | 238 | */ |
240 | static void | | 239 | static void |
241 | showusrname(const char *name) | | 240 | showusrname(const char *name) |
242 | { | | 241 | { |
243 | struct passwd *pwd = getpwnam(name); | | 242 | struct passwd *pwd = getpwnam(name); |
244 | | | 243 | |
245 | if (pwd == NULL) { | | 244 | if (pwd == NULL) { |
246 | warnx("%s: unknown user", name); | | 245 | warnx("%s: unknown user", name); |
247 | return; | | 246 | return; |
248 | } | | 247 | } |
249 | if (pwd->pw_uid != myuid && myuid != 0) { | | 248 | if (pwd->pw_uid != myuid && myuid != 0) { |
250 | warnx("%s (uid %d): permission denied", name, pwd->pw_uid); | | 249 | warnx("%s (uid %d): permission denied", name, pwd->pw_uid); |
251 | return; | | 250 | return; |
252 | } | | 251 | } |
253 | showquotas(QUOTA_IDTYPE_USER, "user", pwd->pw_uid, name); | | 252 | showquotas(QUOTA_IDTYPE_USER, "user", pwd->pw_uid, name); |
254 | } | | 253 | } |
255 | | | 254 | |
256 | /* | | 255 | /* |
257 | * Print out quotas for a specified group identifier. | | 256 | * Print out quotas for a specified group identifier. |
258 | */ | | 257 | */ |
259 | static void | | 258 | static void |
260 | showgid(gid_t gid) | | 259 | showgid(gid_t gid) |
261 | { | | 260 | { |
262 | struct group *grp = getgrgid(gid); | | 261 | struct group *grp = getgrgid(gid); |
263 | int ngroups; | | 262 | int ngroups; |
264 | gid_t mygid, gidset[NGROUPS]; | | 263 | gid_t mygid, gidset[NGROUPS]; |
265 | int i; | | 264 | int i; |
266 | const char *name; | | 265 | const char *name; |
267 | | | 266 | |
268 | if (grp == NULL) | | 267 | if (grp == NULL) |
269 | name = "(no entry)"; | | 268 | name = "(no entry)"; |
270 | else | | 269 | else |
271 | name = grp->gr_name; | | 270 | name = grp->gr_name; |
272 | mygid = getgid(); | | 271 | mygid = getgid(); |
273 | ngroups = getgroups(NGROUPS, gidset); | | 272 | ngroups = getgroups(NGROUPS, gidset); |
274 | if (ngroups < 0) { | | 273 | if (ngroups < 0) { |
275 | warn("getgroups"); | | 274 | warn("getgroups"); |
276 | return; | | 275 | return; |
277 | } | | 276 | } |
278 | if (gid != mygid) { | | 277 | if (gid != mygid) { |
279 | for (i = 0; i < ngroups; i++) | | 278 | for (i = 0; i < ngroups; i++) |
280 | if (gid == gidset[i]) | | 279 | if (gid == gidset[i]) |
281 | break; | | 280 | break; |
282 | if (i >= ngroups && myuid != 0) { | | 281 | if (i >= ngroups && myuid != 0) { |
283 | warnx("%s (gid %d): permission denied", name, gid); | | 282 | warnx("%s (gid %d): permission denied", name, gid); |
284 | return; | | 283 | return; |
285 | } | | 284 | } |
286 | } | | 285 | } |
287 | showquotas(QUOTA_IDTYPE_GROUP, "group", gid, name); | | 286 | showquotas(QUOTA_IDTYPE_GROUP, "group", gid, name); |
288 | } | | 287 | } |
289 | | | 288 | |
290 | /* | | 289 | /* |
291 | * Print out quotas for a specified group name. | | 290 | * Print out quotas for a specified group name. |
292 | */ | | 291 | */ |
293 | static void | | 292 | static void |
294 | showgrpname(const char *name) | | 293 | showgrpname(const char *name) |
295 | { | | 294 | { |
296 | struct group *grp = getgrnam(name); | | 295 | struct group *grp = getgrnam(name); |
297 | int ngroups; | | 296 | int ngroups; |
298 | gid_t mygid, gidset[NGROUPS]; | | 297 | gid_t mygid, gidset[NGROUPS]; |
299 | int i; | | 298 | int i; |
300 | | | 299 | |
301 | if (grp == NULL) { | | 300 | if (grp == NULL) { |
302 | warnx("%s: unknown group", name); | | 301 | warnx("%s: unknown group", name); |
303 | return; | | 302 | return; |
304 | } | | 303 | } |
305 | mygid = getgid(); | | 304 | mygid = getgid(); |
306 | ngroups = getgroups(NGROUPS, gidset); | | 305 | ngroups = getgroups(NGROUPS, gidset); |
307 | if (ngroups < 0) { | | 306 | if (ngroups < 0) { |
308 | warn("getgroups"); | | 307 | warn("getgroups"); |
309 | return; | | 308 | return; |
310 | } | | 309 | } |
311 | if (grp->gr_gid != mygid) { | | 310 | if (grp->gr_gid != mygid) { |
312 | for (i = 0; i < ngroups; i++) | | 311 | for (i = 0; i < ngroups; i++) |
313 | if (grp->gr_gid == gidset[i]) | | 312 | if (grp->gr_gid == gidset[i]) |
314 | break; | | 313 | break; |
315 | if (i >= ngroups && myuid != 0) { | | 314 | if (i >= ngroups && myuid != 0) { |
316 | warnx("%s (gid %d): permission denied", | | 315 | warnx("%s (gid %d): permission denied", |
317 | name, grp->gr_gid); | | 316 | name, grp->gr_gid); |
318 | return; | | 317 | return; |
319 | } | | 318 | } |
320 | } | | 319 | } |
321 | showquotas(QUOTA_IDTYPE_GROUP, "group", grp->gr_gid, name); | | 320 | showquotas(QUOTA_IDTYPE_GROUP, "group", grp->gr_gid, name); |
322 | } | | 321 | } |
323 | | | 322 | |
324 | static void | | 323 | static void |
325 | showquotas(int idtype, const char *idtypename, id_t id, const char *idname) | | 324 | showquotas(int idtype, const char *idtypename, id_t id, const char *idname) |
326 | { | | 325 | { |
327 | struct quotause *qup; | | 326 | struct quotause *qup; |
328 | struct quotause *quplist; | | 327 | struct quotause *quplist; |
329 | | | 328 | |
330 | needheading = 1; | | 329 | needheading = 1; |
331 | | | 330 | |
332 | quplist = getprivs(id, idtype); | | 331 | quplist = getprivs(id, idtype); |
333 | for (qup = quplist; qup; qup = qup->next) { | | 332 | for (qup = quplist; qup; qup = qup->next) { |
334 | showonequota(idtype, idtypename, id, idname, qup); | | 333 | showonequota(idtype, idtypename, id, idname, qup); |
335 | } | | 334 | } |
336 | if (!qflag) { | | 335 | if (!qflag) { |
337 | /* In case nothing printed, issue a header saying "none" */ | | 336 | /* In case nothing printed, issue a header saying "none" */ |
338 | heading(idtype, idtypename, id, idname, "none"); | | 337 | heading(idtype, idtypename, id, idname, "none"); |
339 | } | | 338 | } |
340 | } | | 339 | } |
341 | | | 340 | |
342 | static void | | 341 | static void |
343 | showonequota(int idtype, const char *idtypename, id_t id, const char *idname, | | 342 | showonequota(int idtype, const char *idtypename, id_t id, const char *idname, |
344 | struct quotause *qup) | | 343 | struct quotause *qup) |
345 | { | | 344 | { |
346 | static time_t now; | | 345 | static time_t now; |
347 | struct quotaval *qvs; | | 346 | struct quotaval *qvs; |
348 | unsigned numqvs, i; | | 347 | unsigned numqvs, i; |
349 | const char *msg; | | 348 | const char *msg; |
350 | | | 349 | |
351 | qvs = qup->qvs; | | 350 | qvs = qup->qvs; |
352 | numqvs = qup->numqvs; | | 351 | numqvs = qup->numqvs; |
353 | | | 352 | |
354 | if (now == 0) { | | 353 | if (now == 0) { |
355 | time(&now); | | 354 | time(&now); |
356 | } | | 355 | } |
357 | | | 356 | |
358 | if (!vflag && unlimited(qvs, numqvs)) { | | 357 | if (!vflag && unlimited(qvs, numqvs)) { |
359 | return; | | 358 | return; |
360 | } | | 359 | } |
361 | | | 360 | |
362 | if (qflag) { | | 361 | if (qflag) { |
363 | for (i=0; i<numqvs; i++) { | | 362 | for (i=0; i<numqvs; i++) { |
364 | msg = getovermsg(&qvs[i], | | 363 | msg = getovermsg(&qvs[i], |
365 | quota_idtype_getname(qup->qh, i), | | 364 | quota_idtype_getname(qup->qh, i), |
366 | now); | | 365 | now); |
367 | if (msg != NULL) { | | 366 | if (msg != NULL) { |
368 | heading(idtype, idtypename, id, idname, ""); | | 367 | heading(idtype, idtypename, id, idname, ""); |
369 | printf("\t%s %s\n", msg, qup->fsname); | | 368 | printf("\t%s %s\n", msg, qup->fsname); |
370 | } | | 369 | } |
371 | } | | 370 | } |
372 | return; | | 371 | return; |
373 | } | | 372 | } |
374 | | | 373 | |
375 | /* | | 374 | /* |
376 | * XXX this behavior appears to be demanded by the ATF tests, | | 375 | * XXX this behavior appears to be demanded by the ATF tests, |
377 | * although it seems to be at variance with the preexisting | | 376 | * although it seems to be at variance with the preexisting |
378 | * logic in quota.c. | | 377 | * logic in quota.c. |
379 | */ | | 378 | */ |
380 | if (unlimited(qvs, numqvs) && !anyusage(qvs, numqvs)) { | | 379 | if (unlimited(qvs, numqvs) && !anyusage(qvs, numqvs)) { |
381 | return; | | 380 | return; |
382 | } | | 381 | } |
383 | | | 382 | |
384 | /* | | 383 | /* |
385 | * XXX: anyover can in fact be true if anyusage is not true, | | 384 | * XXX: anyover can in fact be true if anyusage is not true, |
386 | * if there's a quota of zero set on some volume. This is | | 385 | * if there's a quota of zero set on some volume. This is |
387 | * because the check we do checks if adding one more thing | | 386 | * because the check we do checks if adding one more thing |
388 | * will go over. That is reasonable, I suppose, but arguably | | 387 | * will go over. That is reasonable, I suppose, but arguably |
389 | * the resulting behavior with usage 0 is a bug. (Also, what | | 388 | * the resulting behavior with usage 0 is a bug. (Also, what |
390 | * reason do we have to believe that the reported grace expire | | 389 | * reason do we have to believe that the reported grace expire |
391 | * time is valid if we aren't in fact over yet?) | | 390 | * time is valid if we aren't in fact over yet?) |
392 | */ | | 391 | */ |
393 | | | 392 | |
394 | if (vflag || dflag || anyover(qvs, numqvs, now) || | | 393 | if (vflag || dflag || anyover(qvs, numqvs, now) || |
395 | anyusage(qvs, numqvs)) { | | 394 | anyusage(qvs, numqvs)) { |
396 | heading(idtype, idtypename, id, idname, ""); | | 395 | heading(idtype, idtypename, id, idname, ""); |
397 | if (strlen(qup->fsname) > 4) { | | 396 | if (strlen(qup->fsname) > 4) { |
398 | printf("%s\n", qup->fsname); | | 397 | printf("%s\n", qup->fsname); |
399 | printf("%12s", ""); | | 398 | printf("%12s", ""); |
400 | } else { | | 399 | } else { |
401 | printf("%12s", qup->fsname); | | 400 | printf("%12s", qup->fsname); |
402 | } | | 401 | } |
403 | | | 402 | |
404 | for (i=0; i<numqvs; i++) { | | 403 | for (i=0; i<numqvs; i++) { |
405 | printqv(&qvs[i], | | 404 | printqv(&qvs[i], |
406 | quota_objtype_isbytes(qup->qh, i), now); | | 405 | quota_objtype_isbytes(qup->qh, i), now); |
407 | } | | 406 | } |
408 | printf("\n"); | | 407 | printf("\n"); |
409 | } | | 408 | } |
410 | } | | 409 | } |
411 | | | 410 | |
412 | static void | | 411 | static void |
413 | heading(int idtype, const char *idtypename, id_t id, const char *idname, | | 412 | heading(int idtype, const char *idtypename, id_t id, const char *idname, |
414 | const char *tag) | | 413 | const char *tag) |
415 | { | | 414 | { |
416 | if (needheading == 0) | | 415 | if (needheading == 0) |
417 | return; | | 416 | return; |
418 | needheading = 0; | | 417 | needheading = 0; |
419 | | | 418 | |
420 | if (dflag) | | 419 | if (dflag) |
421 | printf("Default %s disk quotas: %s\n", idtypename, tag); | | 420 | printf("Default %s disk quotas: %s\n", idtypename, tag); |
422 | else | | 421 | else |
423 | printf("Disk quotas for %s %s (%cid %u): %s\n", | | 422 | printf("Disk quotas for %s %s (%cid %u): %s\n", |
424 | idtypename, idname, idtypename[0], id, tag); | | 423 | idtypename, idname, idtypename[0], id, tag); |
425 | | | 424 | |
426 | if (!qflag && tag[0] == '\0') { | | 425 | if (!qflag && tag[0] == '\0') { |
427 | printf("%12s%9s %8s%9s%8s%8s %7s%8s%8s\n" | | 426 | printf("%12s%9s %8s%9s%8s%8s %7s%8s%8s\n" |
428 | , "Filesystem" | | 427 | , "Filesystem" |
429 | , "blocks" | | 428 | , "blocks" |
430 | , "quota" | | 429 | , "quota" |
431 | , "limit" | | 430 | , "limit" |
432 | , "grace" | | 431 | , "grace" |
433 | , "files" | | 432 | , "files" |
434 | , "quota" | | 433 | , "quota" |
435 | , "limit" | | 434 | , "limit" |
436 | , "grace" | | 435 | , "grace" |
437 | ); | | 436 | ); |
438 | } | | 437 | } |
439 | } | | 438 | } |
440 | | | 439 | |
441 | static void | | 440 | static void |
442 | printqv(struct quotaval *qv, int isbytes, time_t now) | | 441 | printqv(struct quotaval *qv, int isbytes, time_t now) |
443 | { | | 442 | { |
444 | char buf[20]; | | 443 | char buf[20]; |
445 | const char *str; | | 444 | const char *str; |
446 | int intprtflags, over, width; | | 445 | int intprtflags, over, width; |
447 | | | 446 | |
448 | /* | | 447 | /* |
449 | * The assorted finagling of width is to match the previous | | 448 | * The assorted finagling of width is to match the previous |
450 | * open-coded formatting for exactly two quota object types, | | 449 | * open-coded formatting for exactly two quota object types, |
451 | * which was chosen to make the default report fit in 80 | | 450 | * which was chosen to make the default report fit in 80 |
452 | * columns. | | 451 | * columns. |
453 | */ | | 452 | */ |
454 | | | 453 | |
455 | width = isbytes ? 9 : 8; | | 454 | width = isbytes ? 9 : 8; |
456 | intprtflags = isbytes ? HN_B : 0; | | 455 | intprtflags = isbytes ? HN_B : 0; |
457 | over = isover(qv, now); | | 456 | over = isover(qv, now); |
458 | | | 457 | |
459 | str = intprt(buf, width, qv->qv_usage, intprtflags, hflag); | | 458 | str = intprt(buf, width, qv->qv_usage, intprtflags, hflag); |
460 | printf("%*s", width, str); | | 459 | printf("%*s", width, str); |
461 | | | 460 | |
462 | printf("%c", over ? '*' : ' '); | | 461 | printf("%c", over ? '*' : ' '); |
463 | | | 462 | |
464 | str = intprt(buf, width, qv->qv_softlimit, intprtflags, hflag); | | 463 | str = intprt(buf, width, qv->qv_softlimit, intprtflags, hflag); |
465 | printf("%*s", width-1, str); | | 464 | printf("%*s", width-1, str); |
466 | | | 465 | |
467 | str = intprt(buf, width, qv->qv_hardlimit, intprtflags, hflag); | | 466 | str = intprt(buf, width, qv->qv_hardlimit, intprtflags, hflag); |
468 | printf("%*s", width, str); | | 467 | printf("%*s", width, str); |
469 | | | 468 | |
470 | if (over) { | | 469 | if (over) { |
471 | str = timeprt(buf, 9, now, qv->qv_expiretime); | | 470 | str = timeprt(buf, 9, now, qv->qv_expiretime); |
472 | } else if (vflag && qv->qv_grace != QUOTA_NOTIME) { | | 471 | } else if (vflag && qv->qv_grace != QUOTA_NOTIME) { |
473 | str = timeprt(buf, 9, 0, qv->qv_grace); | | 472 | str = timeprt(buf, 9, 0, qv->qv_grace); |
474 | } else { | | 473 | } else { |
475 | str = ""; | | 474 | str = ""; |
476 | } | | 475 | } |
477 | printf("%8s", str); | | 476 | printf("%8s", str); |
478 | } | | 477 | } |
479 | | | 478 | |
480 | /* | | 479 | /* |
481 | * Collect the requested quota information. | | 480 | * Collect the requested quota information. |
482 | */ | | 481 | */ |
483 | static struct quotause * | | 482 | static struct quotause * |
484 | getprivs(id_t id, int idtype) | | 483 | getprivs(id_t id, int idtype) |
485 | { | | 484 | { |
486 | struct quotause *qup, *quptail; | | 485 | struct quotause *qup, *quptail; |
487 | struct quotause *quphead; | | 486 | struct quotause *quphead; |
488 | struct statvfs *fst; | | 487 | struct statvfs *fst; |
489 | struct quotakey qk; | | 488 | struct quotakey qk; |
490 | int nfst, i; | | 489 | int nfst, i; |
491 | unsigned j; | | 490 | unsigned j; |
492 | | | 491 | |
493 | qup = quphead = quptail = NULL; | | 492 | qup = quphead = quptail = NULL; |
494 | | | 493 | |
495 | nfst = getmntinfo(&fst, MNT_WAIT); | | 494 | nfst = getmntinfo(&fst, MNT_WAIT); |
496 | if (nfst == 0) | | 495 | if (nfst == 0) |
497 | errx(2, "no filesystems mounted!"); | | 496 | errx(2, "no filesystems mounted!"); |
498 | for (i = 0; i < nfst; i++) { | | 497 | for (i = 0; i < nfst; i++) { |
499 | if (qup == NULL) { | | 498 | if (qup == NULL) { |
500 | if ((qup = malloc(sizeof *qup)) == NULL) | | 499 | if ((qup = malloc(sizeof *qup)) == NULL) |
501 | err(1, "Out of memory"); | | 500 | err(1, "Out of memory"); |
502 | } | | 501 | } |
503 | qup->qh = quota_open(fst[i].f_mntonname); | | 502 | qup->qh = quota_open(fst[i].f_mntonname); |
504 | if (qup->qh == NULL) { | | 503 | if (qup->qh == NULL) { |
505 | if (errno == EOPNOTSUPP || errno == ENXIO) { | | 504 | if (errno == EOPNOTSUPP || errno == ENXIO) { |
506 | continue; | | 505 | continue; |
507 | } | | 506 | } |
508 | err(1, "%s: quota_open", fst[i].f_mntonname); | | 507 | err(1, "%s: quota_open", fst[i].f_mntonname); |
509 | } | | 508 | } |
510 | #if 0 | | 509 | #if 0 |
511 | if (strncmp(fst[i].f_fstypename, "nfs", | | 510 | if (strncmp(fst[i].f_fstypename, "nfs", |
512 | sizeof(fst[i].f_fstypename)) == 0) { | | 511 | sizeof(fst[i].f_fstypename)) == 0) { |
513 | version = 0; | | 512 | version = 0; |
514 | qup->numqvs = QUOTA_NLIMITS; | | 513 | qup->numqvs = QUOTA_NLIMITS; |
515 | qup->qvs = malloc(qup->numqvs * sizeof(qup->qvs[0])); | | 514 | qup->qvs = malloc(qup->numqvs * sizeof(qup->qvs[0])); |
516 | if (qup->qvs == NULL) { | | 515 | if (qup->qvs == NULL) { |
517 | err(1, "Out of memory"); | | 516 | err(1, "Out of memory"); |
518 | } | | 517 | } |
519 | if (getnfsquota(fst[i].f_mntfromname, | | 518 | if (getnfsquota(fst[i].f_mntfromname, |
520 | qup->qvs, id, ufs_quota_class_names[idtype]) != 1) | | 519 | qup->qvs, id, ufs_quota_class_names[idtype]) != 1) |
521 | continue; | | 520 | continue; |
522 | } else if ((fst[i].f_flag & ST_QUOTA) != 0) { | | 521 | } else if ((fst[i].f_flag & ST_QUOTA) != 0) { |
523 | qup->numqvs = QUOTA_NLIMITS; | | 522 | qup->numqvs = QUOTA_NLIMITS; |
524 | qup->qvs = malloc(qup->numqvs * sizeof(qup->qvs[0])); | | 523 | qup->qvs = malloc(qup->numqvs * sizeof(qup->qvs[0])); |
525 | if (qup->qvs == NULL) { | | 524 | if (qup->qvs == NULL) { |
526 | err(1, "Out of memory"); | | 525 | err(1, "Out of memory"); |
527 | } | | 526 | } |
528 | if (getvfsquota(fst[i].f_mntonname, qup->qvs, &version, | | 527 | if (getvfsquota(fst[i].f_mntonname, qup->qvs, &version, |
529 | id, idtype, dflag, Dflag) != 1) | | 528 | id, idtype, dflag, Dflag) != 1) |
530 | continue; | | 529 | continue; |
531 | } else | | 530 | } else |
532 | continue; | | 531 | continue; |
533 | #else | | 532 | #else |
534 | qup->numqvs = quota_getnumidtypes(qup->qh); | | 533 | qup->numqvs = quota_getnumidtypes(qup->qh); |
535 | qup->qvs = malloc(qup->numqvs * sizeof(qup->qvs[0])); | | 534 | qup->qvs = malloc(qup->numqvs * sizeof(qup->qvs[0])); |
536 | if (qup->qvs == NULL) { | | 535 | if (qup->qvs == NULL) { |
537 | err(1, "Out of memory"); | | 536 | err(1, "Out of memory"); |
538 | } | | 537 | } |
539 | qk.qk_idtype = idtype; | | 538 | qk.qk_idtype = idtype; |
540 | if (dflag) { | | 539 | if (dflag) { |
541 | qk.qk_id = QUOTA_DEFAULTID; | | 540 | qk.qk_id = QUOTA_DEFAULTID; |
542 | } else { | | 541 | } else { |
543 | qk.qk_id = id; | | 542 | qk.qk_id = id; |
544 | } | | 543 | } |
545 | for (j=0; j<qup->numqvs; j++) { | | 544 | for (j=0; j<qup->numqvs; j++) { |
546 | qk.qk_objtype = j; | | 545 | qk.qk_objtype = j; |
547 | if (quota_get(qup->qh, &qk, &qup->qvs[j]) < 0) { | | 546 | if (quota_get(qup->qh, &qk, &qup->qvs[j]) < 0) { |
548 | if (errno != ENOENT && errno != ENODEV) { | | 547 | if (errno != ENOENT && errno != ENODEV) { |
549 | warn("%s: quota_get (objtype %u)", | | 548 | warn("%s: quota_get (objtype %u)", |
550 | fst[i].f_mntonname, j); | | 549 | fst[i].f_mntonname, j); |
551 | } | | 550 | } |
552 | quotaval_clear(&qup->qvs[j]); | | 551 | quotaval_clear(&qup->qvs[j]); |
553 | } | | 552 | } |
554 | } | | 553 | } |
555 | #endif | | 554 | #endif |
556 | (void)strlcpy(qup->fsname, fst[i].f_mntonname, | | 555 | (void)strlcpy(qup->fsname, fst[i].f_mntonname, |
557 | sizeof(qup->fsname)); | | 556 | sizeof(qup->fsname)); |
558 | if (quphead == NULL) | | 557 | if (quphead == NULL) |
559 | quphead = qup; | | 558 | quphead = qup; |
560 | else | | 559 | else |
561 | quptail->next = qup; | | 560 | quptail->next = qup; |
562 | quptail = qup; | | 561 | quptail = qup; |
563 | quptail->next = 0; | | 562 | quptail->next = 0; |
564 | qup = NULL; | | 563 | qup = NULL; |
565 | } | | 564 | } |
566 | free(qup); | | 565 | free(qup); |
567 | return quphead; | | 566 | return quphead; |
568 | } | | 567 | } |
569 | | | 568 | |
570 | static int | | 569 | static int |
571 | unlimited(struct quotaval *qvs, unsigned numqvs) | | 570 | unlimited(struct quotaval *qvs, unsigned numqvs) |
572 | { | | 571 | { |
573 | unsigned i; | | 572 | unsigned i; |
574 | | | 573 | |
575 | for (i=0; i<numqvs; i++) { | | 574 | for (i=0; i<numqvs; i++) { |
576 | if (qvs[i].qv_softlimit != QUOTA_NOLIMIT || | | 575 | if (qvs[i].qv_softlimit != QUOTA_NOLIMIT || |
577 | qvs[i].qv_hardlimit != QUOTA_NOLIMIT) { | | 576 | qvs[i].qv_hardlimit != QUOTA_NOLIMIT) { |
578 | return 0; | | 577 | return 0; |
579 | } | | 578 | } |
580 | } | | 579 | } |
581 | return 1; | | 580 | return 1; |
582 | } | | 581 | } |
583 | | | 582 | |
584 | static int | | 583 | static int |
585 | anyusage(struct quotaval *qvs, unsigned numqvs) | | 584 | anyusage(struct quotaval *qvs, unsigned numqvs) |
586 | { | | 585 | { |
587 | unsigned i; | | 586 | unsigned i; |
588 | | | 587 | |
589 | for (i=0; i<numqvs; i++) { | | 588 | for (i=0; i<numqvs; i++) { |
590 | if (qvs[i].qv_usage > 0) { | | 589 | if (qvs[i].qv_usage > 0) { |
591 | return 1; | | 590 | return 1; |
592 | } | | 591 | } |
593 | } | | 592 | } |
594 | return 0; | | 593 | return 0; |
595 | } | | 594 | } |
596 | | | 595 | |
597 | static int | | 596 | static int |
598 | anyover(struct quotaval *qvs, unsigned numqvs, time_t now) | | 597 | anyover(struct quotaval *qvs, unsigned numqvs, time_t now) |
599 | { | | 598 | { |
600 | unsigned i; | | 599 | unsigned i; |
601 | | | 600 | |
602 | for (i=0; i<numqvs; i++) { | | 601 | for (i=0; i<numqvs; i++) { |
603 | if (isover(&qvs[i], now)) { | | 602 | if (isover(&qvs[i], now)) { |
604 | return 1; | | 603 | return 1; |
605 | } | | 604 | } |
606 | } | | 605 | } |
607 | return 0; | | 606 | return 0; |
608 | } | | 607 | } |
609 | | | 608 | |
610 | static int | | 609 | static int |
611 | isover(struct quotaval *qv, time_t now) | | 610 | isover(struct quotaval *qv, time_t now) |
612 | { | | 611 | { |
613 | return (qv->qv_usage >= qv->qv_hardlimit || | | 612 | return (qv->qv_usage >= qv->qv_hardlimit || |
614 | qv->qv_usage >= qv->qv_softlimit); | | 613 | qv->qv_usage >= qv->qv_softlimit); |
615 | } | | 614 | } |
616 | | | 615 | |
617 | static const char * | | 616 | static const char * |
618 | getovermsg(struct quotaval *qv, const char *what, time_t now) | | 617 | getovermsg(struct quotaval *qv, const char *what, time_t now) |
619 | { | | 618 | { |
620 | static char buf[64]; | | 619 | static char buf[64]; |
621 | | | 620 | |
622 | if (qv->qv_usage >= qv->qv_hardlimit) { | | 621 | if (qv->qv_usage >= qv->qv_hardlimit) { |
623 | snprintf(buf, sizeof(buf), "%c%s limit reached on", | | 622 | snprintf(buf, sizeof(buf), "%c%s limit reached on", |
624 | toupper((unsigned char)what[0]), what+1); | | 623 | toupper((unsigned char)what[0]), what+1); |
625 | return buf; | | 624 | return buf; |
626 | } | | 625 | } |
627 | | | 626 | |
628 | if (qv->qv_usage < qv->qv_softlimit) { | | 627 | if (qv->qv_usage < qv->qv_softlimit) { |
629 | /* Ok */ | | 628 | /* Ok */ |
630 | return NULL; | | 629 | return NULL; |
631 | } | | 630 | } |
632 | | | 631 | |
633 | if (now > qv->qv_expiretime) { | | 632 | if (now > qv->qv_expiretime) { |
634 | snprintf(buf, sizeof(buf), "Over %s quota on", what); | | 633 | snprintf(buf, sizeof(buf), "Over %s quota on", what); |
635 | return buf; | | 634 | return buf; |
636 | } | | 635 | } |
637 | | | 636 | |
638 | snprintf(buf, sizeof(buf), "In %s grace period on", what); | | 637 | snprintf(buf, sizeof(buf), "In %s grace period on", what); |
639 | return buf; | | 638 | return buf; |
640 | } | | 639 | } |