repquota.c revision 1.27 1 1.27 christos /* $NetBSD: repquota.c,v 1.27 2011/03/06 22:33:55 christos Exp $ */
2 1.27 christos
3 1.1 cgd /*
4 1.4 mycroft * Copyright (c) 1980, 1990, 1993
5 1.4 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Robert Elz at The University of Melbourne.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.20 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.10 mrg #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.23 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1990, 1993\
38 1.23 lukem The Regents of the University of California. All rights reserved.");
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #ifndef lint
42 1.10 mrg #if 0
43 1.10 mrg static char sccsid[] = "@(#)repquota.c 8.2 (Berkeley) 11/22/94";
44 1.10 mrg #else
45 1.27 christos __RCSID("$NetBSD: repquota.c,v 1.27 2011/03/06 22:33:55 christos Exp $");
46 1.10 mrg #endif
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.1 cgd /*
50 1.1 cgd * Quota report
51 1.1 cgd */
52 1.1 cgd #include <sys/param.h>
53 1.1 cgd #include <sys/stat.h>
54 1.26 bouyer #include <sys/types.h>
55 1.26 bouyer #include <sys/statvfs.h>
56 1.26 bouyer #include <prop/proplib.h>
57 1.26 bouyer #include <sys/quota.h>
58 1.26 bouyer
59 1.11 lukem #include <errno.h>
60 1.26 bouyer #include <err.h>
61 1.1 cgd #include <fstab.h>
62 1.11 lukem #include <grp.h>
63 1.1 cgd #include <pwd.h>
64 1.1 cgd #include <stdio.h>
65 1.11 lukem #include <stdlib.h>
66 1.6 cgd #include <string.h>
67 1.11 lukem #include <unistd.h>
68 1.1 cgd
69 1.26 bouyer #include <ufs/ufs/quota2_prop.h>
70 1.26 bouyer #include <ufs/ufs/quota1.h>
71 1.26 bouyer
72 1.27 christos #include "printquota.h"
73 1.27 christos #include "quotautil.h"
74 1.1 cgd
75 1.1 cgd struct fileusage {
76 1.1 cgd struct fileusage *fu_next;
77 1.26 bouyer struct quota2_entry fu_q2e;
78 1.27 christos uint32_t fu_id;
79 1.1 cgd char fu_name[1];
80 1.1 cgd /* actually bigger */
81 1.1 cgd };
82 1.1 cgd #define FUHASH 1024 /* must be power of two */
83 1.27 christos static struct fileusage *fuhead[MAXQUOTAS][FUHASH];
84 1.27 christos static uint32_t highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
85 1.26 bouyer int valid[MAXQUOTAS];
86 1.27 christos static struct quota2_entry defaultq2e[MAXQUOTAS];
87 1.1 cgd
88 1.27 christos static int vflag = 0; /* verbose */
89 1.27 christos static int aflag = 0; /* all file systems */
90 1.27 christos static int Dflag = 0; /* debug */
91 1.27 christos static int hflag = 0; /* humanize */
92 1.27 christos static int xflag = 0; /* export */
93 1.27 christos
94 1.27 christos
95 1.27 christos static struct fileusage *addid(uint32_t, int, const char *);
96 1.27 christos static struct fileusage *lookup(uint32_t, int);
97 1.27 christos static struct fileusage *qremove(uint32_t, int);
98 1.27 christos static int oneof(const char *, char **, int);
99 1.27 christos static int repquota(const struct statvfs *, int);
100 1.27 christos static int repquota2(const struct statvfs *, int);
101 1.27 christos static int repquota1(const struct statvfs *, int);
102 1.27 christos static void usage(void) __attribute__((__noreturn__));
103 1.27 christos static void printquotas(int, const struct statvfs *, int);
104 1.27 christos static void exportquotas(void);
105 1.11 lukem
106 1.11 lukem int
107 1.27 christos main(int argc, char **argv)
108 1.1 cgd {
109 1.1 cgd int gflag = 0, uflag = 0, errs = 0;
110 1.1 cgd long i, argnum, done = 0;
111 1.9 mark int ch;
112 1.26 bouyer struct statvfs *fst;
113 1.26 bouyer int nfst;
114 1.1 cgd
115 1.26 bouyer while ((ch = getopt(argc, argv, "Daguhvx")) != -1) {
116 1.1 cgd switch(ch) {
117 1.1 cgd case 'a':
118 1.1 cgd aflag++;
119 1.1 cgd break;
120 1.1 cgd case 'g':
121 1.1 cgd gflag++;
122 1.1 cgd break;
123 1.1 cgd case 'u':
124 1.1 cgd uflag++;
125 1.1 cgd break;
126 1.26 bouyer case 'h':
127 1.26 bouyer hflag++;
128 1.26 bouyer break;
129 1.1 cgd case 'v':
130 1.1 cgd vflag++;
131 1.1 cgd break;
132 1.26 bouyer case 'D':
133 1.26 bouyer Dflag++;
134 1.26 bouyer break;
135 1.26 bouyer case 'x':
136 1.26 bouyer xflag++;
137 1.26 bouyer break;
138 1.1 cgd default:
139 1.1 cgd usage();
140 1.1 cgd }
141 1.1 cgd }
142 1.1 cgd argc -= optind;
143 1.1 cgd argv += optind;
144 1.26 bouyer if (xflag && (argc != 1 || aflag))
145 1.26 bouyer usage();
146 1.1 cgd if (argc == 0 && !aflag)
147 1.1 cgd usage();
148 1.1 cgd if (!gflag && !uflag) {
149 1.1 cgd if (aflag)
150 1.1 cgd gflag++;
151 1.1 cgd uflag++;
152 1.1 cgd }
153 1.26 bouyer
154 1.26 bouyer nfst = getmntinfo(&fst, MNT_WAIT);
155 1.26 bouyer if (nfst == 0)
156 1.27 christos errx(1, "no filesystems mounted!");
157 1.26 bouyer for (i = 0; i < nfst; i++) {
158 1.26 bouyer if ((fst[i].f_flag & ST_QUOTA) == 0)
159 1.1 cgd continue;
160 1.1 cgd if (aflag) {
161 1.26 bouyer if (gflag)
162 1.26 bouyer errs += repquota(&fst[i], GRPQUOTA);
163 1.26 bouyer if (uflag)
164 1.26 bouyer errs += repquota(&fst[i], USRQUOTA);
165 1.1 cgd continue;
166 1.1 cgd }
167 1.26 bouyer if ((argnum = oneof(fst[i].f_mntonname, argv, argc)) >= 0 ||
168 1.26 bouyer (argnum = oneof(fst[i].f_mntfromname, argv, argc)) >= 0) {
169 1.27 christos done |= 1U << argnum;
170 1.26 bouyer if (gflag)
171 1.26 bouyer errs += repquota(&fst[i], GRPQUOTA);
172 1.26 bouyer if (uflag)
173 1.26 bouyer errs += repquota(&fst[i], USRQUOTA);
174 1.1 cgd }
175 1.1 cgd }
176 1.26 bouyer if (xflag)
177 1.26 bouyer exportquotas();
178 1.1 cgd for (i = 0; i < argc; i++)
179 1.27 christos if ((done & (1U << i)) == 0)
180 1.27 christos warnx("%s not mounted", argv[i]);
181 1.27 christos return errs;
182 1.1 cgd }
183 1.1 cgd
184 1.27 christos static void
185 1.27 christos usage(void)
186 1.27 christos {
187 1.27 christos const char *p = getprogname();
188 1.27 christos fprintf(stderr, "usage: %s [-D] [-v] [-g] [-u] -a\n"
189 1.27 christos "\t%s [-D] [-v] [-g] [-u] filesys ...\n"
190 1.27 christos "\t%s -x [-D] [-g] [-u] filesys\n", p, p, p);
191 1.1 cgd exit(1);
192 1.1 cgd }
193 1.1 cgd
194 1.27 christos static int
195 1.26 bouyer repquota(const struct statvfs *vfs, int type)
196 1.26 bouyer {
197 1.26 bouyer if (repquota2(vfs, type) != 0)
198 1.27 christos return repquota1(vfs, type);
199 1.26 bouyer return 0;
200 1.26 bouyer }
201 1.26 bouyer
202 1.27 christos static int
203 1.26 bouyer repquota2(const struct statvfs *vfs, int type)
204 1.26 bouyer {
205 1.26 bouyer prop_dictionary_t dict, data, cmd;
206 1.26 bouyer prop_array_t cmds, datas;
207 1.26 bouyer struct plistref pref;
208 1.26 bouyer int8_t error8, version = 0;
209 1.26 bouyer prop_object_iterator_t cmditer, dataiter;
210 1.26 bouyer struct quota2_entry *q2ep;
211 1.26 bouyer struct fileusage *fup;
212 1.26 bouyer const char *strid;
213 1.26 bouyer uint32_t id;
214 1.26 bouyer
215 1.26 bouyer dict = quota2_prop_create();
216 1.26 bouyer cmds = prop_array_create();
217 1.26 bouyer datas = prop_array_create();
218 1.26 bouyer
219 1.26 bouyer if (dict == NULL || cmds == NULL || datas == NULL)
220 1.26 bouyer errx(1, "can't allocate proplist");
221 1.26 bouyer if (!quota2_prop_add_command(cmds, "getall", qfextension[type], datas))
222 1.26 bouyer err(1, "prop_add_command");
223 1.26 bouyer if (!quota2_prop_add_command(cmds, "get version", qfextension[type],
224 1.26 bouyer prop_array_create()))
225 1.26 bouyer err(1, "prop_add_command");
226 1.26 bouyer if (!prop_dictionary_set(dict, "commands", cmds))
227 1.26 bouyer err(1, "prop_dictionary_set(command)");
228 1.26 bouyer if (Dflag)
229 1.26 bouyer printf("message to kernel:\n%s\n",
230 1.26 bouyer prop_dictionary_externalize(dict));
231 1.26 bouyer if (!prop_dictionary_send_syscall(dict, &pref))
232 1.26 bouyer err(1, "prop_dictionary_send_syscall");
233 1.26 bouyer prop_object_release(dict);
234 1.26 bouyer
235 1.26 bouyer if (quotactl(vfs->f_mntonname, &pref) != 0)
236 1.26 bouyer err(1, "quotactl");
237 1.26 bouyer
238 1.27 christos if ((errno = prop_dictionary_recv_syscall(&pref, &dict)) != 0) {
239 1.27 christos err(1, "prop_dictionary_recv_syscall");
240 1.26 bouyer }
241 1.26 bouyer if (Dflag)
242 1.26 bouyer printf("reply from kernel:\n%s\n",
243 1.26 bouyer prop_dictionary_externalize(dict));
244 1.27 christos if ((errno = quota2_get_cmds(dict, &cmds)) != 0) {
245 1.27 christos err(1, "quota2_get_cmds");
246 1.26 bouyer }
247 1.26 bouyer cmditer = prop_array_iterator(cmds);
248 1.26 bouyer if (cmditer == NULL)
249 1.26 bouyer err(1, "prop_array_iterator(cmds)");
250 1.26 bouyer
251 1.26 bouyer while ((cmd = prop_object_iterator_next(cmditer)) != NULL) {
252 1.26 bouyer const char *cmdstr;
253 1.26 bouyer if (!prop_dictionary_get_cstring_nocopy(cmd, "command",
254 1.26 bouyer &cmdstr))
255 1.26 bouyer err(1, "prop_get(command)");
256 1.26 bouyer
257 1.26 bouyer if (!prop_dictionary_get_int8(cmd, "return", &error8))
258 1.26 bouyer err(1, "prop_get(return)");
259 1.26 bouyer
260 1.26 bouyer if (error8) {
261 1.26 bouyer prop_object_release(dict);
262 1.26 bouyer if (error8 != EOPNOTSUPP) {
263 1.27 christos errno = error8;
264 1.27 christos warn("get %s quotas", qfextension[type]);
265 1.26 bouyer }
266 1.27 christos return error8;
267 1.26 bouyer }
268 1.26 bouyer datas = prop_dictionary_get(cmd, "data");
269 1.26 bouyer if (datas == NULL)
270 1.26 bouyer err(1, "prop_dict_get(datas)");
271 1.26 bouyer
272 1.26 bouyer if (strcmp("get version", cmdstr) == 0) {
273 1.26 bouyer data = prop_array_get(datas, 0);
274 1.26 bouyer if (data == NULL)
275 1.26 bouyer err(1, "prop_array_get(version)");
276 1.26 bouyer if (!prop_dictionary_get_int8(data, "version",
277 1.26 bouyer &version))
278 1.26 bouyer err(1, "prop_get_int8(version)");
279 1.26 bouyer continue;
280 1.26 bouyer }
281 1.26 bouyer dataiter = prop_array_iterator(datas);
282 1.26 bouyer if (dataiter == NULL)
283 1.26 bouyer err(1, "prop_array_iterator");
284 1.26 bouyer
285 1.26 bouyer valid[type] = 1;
286 1.26 bouyer while ((data = prop_object_iterator_next(dataiter)) != NULL) {
287 1.26 bouyer strid = NULL;
288 1.26 bouyer if (!prop_dictionary_get_uint32(data, "id", &id)) {
289 1.26 bouyer if (!prop_dictionary_get_cstring_nocopy(data,
290 1.26 bouyer "id", &strid))
291 1.26 bouyer errx(1, "can't find id in quota entry");
292 1.26 bouyer if (strcmp(strid, "default") != 0) {
293 1.26 bouyer errx(1,
294 1.26 bouyer "wrong id string %s in quota entry",
295 1.26 bouyer strid);
296 1.26 bouyer }
297 1.26 bouyer q2ep = &defaultq2e[type];
298 1.26 bouyer } else {
299 1.26 bouyer if ((fup = lookup(id, type)) == 0)
300 1.26 bouyer fup = addid(id, type, (char *)0);
301 1.26 bouyer q2ep = &fup->fu_q2e;
302 1.26 bouyer q2ep->q2e_uid = id;
303 1.26 bouyer }
304 1.26 bouyer
305 1.27 christos errno = quota2_dict_get_q2e_usage(data, q2ep);
306 1.27 christos if (errno)
307 1.27 christos err(1, "quota2_dict_get_q2e_usage");
308 1.26 bouyer }
309 1.26 bouyer prop_object_iterator_release(dataiter);
310 1.26 bouyer }
311 1.26 bouyer prop_object_iterator_release(cmditer);
312 1.26 bouyer prop_object_release(dict);
313 1.26 bouyer if (xflag == 0)
314 1.26 bouyer printquotas(type, vfs, version);
315 1.27 christos return 0;
316 1.26 bouyer }
317 1.26 bouyer
318 1.27 christos static int
319 1.27 christos repquota1(const struct statvfs *vfs, int type)
320 1.26 bouyer {
321 1.27 christos char qfpathname[MAXPATHLEN];
322 1.11 lukem struct fstab *fs;
323 1.11 lukem struct fileusage *fup;
324 1.1 cgd FILE *qf;
325 1.27 christos uint32_t id;
326 1.1 cgd struct dqblk dqbuf;
327 1.26 bouyer time_t bgrace = MAX_DQ_TIME, igrace = MAX_DQ_TIME;
328 1.1 cgd
329 1.26 bouyer setfsent();
330 1.26 bouyer while ((fs = getfsent()) != NULL) {
331 1.26 bouyer if (strcmp(fs->fs_vfstype, "ffs") == 0 &&
332 1.26 bouyer strcmp(fs->fs_file, vfs->f_mntonname) == 0)
333 1.26 bouyer break;
334 1.1 cgd }
335 1.26 bouyer endfsent();
336 1.26 bouyer if (fs == NULL) {
337 1.27 christos warnx("%s not found in fstab", vfs->f_mntonname);
338 1.26 bouyer return 1;
339 1.26 bouyer }
340 1.27 christos if (!hasquota(qfpathname, sizeof(qfpathname), fs, type))
341 1.26 bouyer return 0;
342 1.26 bouyer
343 1.1 cgd if ((qf = fopen(qfpathname, "r")) == NULL) {
344 1.27 christos warn("Cannot open `%s'", qfpathname);
345 1.27 christos return 1;
346 1.1 cgd }
347 1.1 cgd for (id = 0; ; id++) {
348 1.1 cgd fread(&dqbuf, sizeof(struct dqblk), 1, qf);
349 1.1 cgd if (feof(qf))
350 1.1 cgd break;
351 1.26 bouyer if (id == 0) {
352 1.26 bouyer if (dqbuf.dqb_btime > 0)
353 1.26 bouyer bgrace = dqbuf.dqb_btime;
354 1.26 bouyer if (dqbuf.dqb_itime > 0)
355 1.26 bouyer igrace = dqbuf.dqb_itime;
356 1.26 bouyer }
357 1.26 bouyer if (dqbuf.dqb_curinodes == 0 && dqbuf.dqb_curblocks == 0 &&
358 1.26 bouyer dqbuf.dqb_bsoftlimit == 0 && dqbuf.dqb_bhardlimit == 0 &&
359 1.26 bouyer dqbuf.dqb_isoftlimit == 0 && dqbuf.dqb_ihardlimit == 0)
360 1.1 cgd continue;
361 1.1 cgd if ((fup = lookup(id, type)) == 0)
362 1.1 cgd fup = addid(id, type, (char *)0);
363 1.26 bouyer dqblk2q2e(&dqbuf, &fup->fu_q2e);
364 1.26 bouyer fup->fu_q2e.q2e_val[QL_BLOCK].q2v_grace = bgrace;
365 1.26 bouyer fup->fu_q2e.q2e_val[QL_FILE].q2v_grace = igrace;
366 1.26 bouyer }
367 1.26 bouyer defaultq2e[type].q2e_val[QL_BLOCK].q2v_grace = bgrace;
368 1.26 bouyer defaultq2e[type].q2e_val[QL_FILE].q2v_grace = igrace;
369 1.26 bouyer defaultq2e[type].q2e_val[QL_BLOCK].q2v_softlimit =
370 1.26 bouyer defaultq2e[type].q2e_val[QL_BLOCK].q2v_hardlimit =
371 1.26 bouyer defaultq2e[type].q2e_val[QL_FILE].q2v_softlimit =
372 1.26 bouyer defaultq2e[type].q2e_val[QL_FILE].q2v_hardlimit = UQUAD_MAX;
373 1.26 bouyer fclose(qf);
374 1.26 bouyer valid[type] = 1;
375 1.26 bouyer if (xflag == 0)
376 1.26 bouyer printquotas(type, vfs, 1);
377 1.27 christos return 0;
378 1.26 bouyer }
379 1.26 bouyer
380 1.27 christos static void
381 1.26 bouyer printquotas(int type, const struct statvfs *vfs, int version)
382 1.26 bouyer {
383 1.26 bouyer static int multiple = 0;
384 1.27 christos uint32_t id;
385 1.26 bouyer int i;
386 1.26 bouyer struct fileusage *fup;
387 1.27 christos struct quota2_val *q;
388 1.26 bouyer const char *timemsg[N_QL];
389 1.26 bouyer char overchar[N_QL];
390 1.27 christos time_t now;
391 1.27 christos char b0[20], b1[20], b2[20], b3[20];
392 1.26 bouyer
393 1.26 bouyer switch(type) {
394 1.26 bouyer case GRPQUOTA:
395 1.26 bouyer {
396 1.26 bouyer struct group *gr;
397 1.26 bouyer setgrent();
398 1.26 bouyer while ((gr = getgrent()) != 0)
399 1.27 christos (void)addid(gr->gr_gid, GRPQUOTA, gr->gr_name);
400 1.26 bouyer endgrent();
401 1.26 bouyer break;
402 1.26 bouyer }
403 1.26 bouyer case USRQUOTA:
404 1.26 bouyer {
405 1.26 bouyer struct passwd *pw;
406 1.26 bouyer setpwent();
407 1.26 bouyer while ((pw = getpwent()) != 0)
408 1.27 christos (void)addid(pw->pw_uid, USRQUOTA, pw->pw_name);
409 1.26 bouyer endpwent();
410 1.26 bouyer break;
411 1.26 bouyer }
412 1.26 bouyer default:
413 1.27 christos errx(1, "unknown quota type %d", type);
414 1.1 cgd }
415 1.26 bouyer
416 1.27 christos time(&now);
417 1.26 bouyer
418 1.26 bouyer if (multiple++)
419 1.26 bouyer printf("\n");
420 1.26 bouyer if (vflag)
421 1.27 christos printf("*** Report for %s quotas on %s (%s, version %d)\n",
422 1.26 bouyer qfextension[type], vfs->f_mntonname, vfs->f_mntfromname,
423 1.26 bouyer version);
424 1.27 christos printf(" Block limits "
425 1.27 christos "File limits\n");
426 1.15 ad printf(type == USRQUOTA ? "User " : "Group");
427 1.27 christos printf(" used soft hard grace used"
428 1.27 christos "soft hard grace\n");
429 1.1 cgd for (id = 0; id <= highid[type]; id++) {
430 1.26 bouyer fup = qremove(id, type);
431 1.27 christos q = fup->fu_q2e.q2e_val;
432 1.1 cgd if (fup == 0)
433 1.1 cgd continue;
434 1.26 bouyer for (i = 0; i < N_QL; i++) {
435 1.27 christos switch (QL_STATUS(quota2_check_limit(&q[i], 1, now))) {
436 1.26 bouyer case QL_S_DENY_HARD:
437 1.26 bouyer case QL_S_DENY_GRACE:
438 1.26 bouyer case QL_S_ALLOW_SOFT:
439 1.27 christos timemsg[i] = timeprt(b0, 8, now, q[i].q2v_time);
440 1.26 bouyer overchar[i] = '+';
441 1.26 bouyer break;
442 1.26 bouyer default:
443 1.26 bouyer timemsg[i] = (vflag && version == 2) ?
444 1.27 christos timeprt(b0, 8, 0, q[i].q2v_grace) : "";
445 1.26 bouyer overchar[i] = '-';
446 1.26 bouyer break;
447 1.26 bouyer }
448 1.26 bouyer }
449 1.26 bouyer
450 1.27 christos if (q[QL_BLOCK].q2v_cur == 0 &&
451 1.27 christos q[QL_FILE].q2v_cur == 0 && vflag == 0 &&
452 1.26 bouyer overchar[QL_BLOCK] == '-' && overchar[QL_FILE] == '-')
453 1.1 cgd continue;
454 1.22 jdolecek if (strlen(fup->fu_name) > 9)
455 1.22 jdolecek printf("%s ", fup->fu_name);
456 1.22 jdolecek else
457 1.22 jdolecek printf("%-10s", fup->fu_name);
458 1.26 bouyer printf("%c%c%9s%9s%9s%7s",
459 1.26 bouyer overchar[QL_BLOCK], overchar[QL_FILE],
460 1.27 christos intprt(b1, 10, q[QL_BLOCK].q2v_cur, HN_B, hflag),
461 1.27 christos intprt(b2, 10, q[QL_BLOCK].q2v_softlimit, HN_B, hflag),
462 1.27 christos intprt(b3, 10, q[QL_BLOCK].q2v_hardlimit, HN_B, hflag),
463 1.26 bouyer timemsg[QL_BLOCK]);
464 1.26 bouyer printf(" %8s%8s%8s%7s\n",
465 1.27 christos intprt(b1, 9, q[QL_FILE].q2v_cur, 0, hflag),
466 1.27 christos intprt(b2, 9, q[QL_FILE].q2v_softlimit, 0, hflag),
467 1.27 christos intprt(b3, 9, q[QL_FILE].q2v_hardlimit, 0, hflag),
468 1.26 bouyer timemsg[QL_FILE]);
469 1.26 bouyer free(fup);
470 1.26 bouyer }
471 1.26 bouyer }
472 1.26 bouyer
473 1.27 christos static void
474 1.27 christos exportquotas(void)
475 1.26 bouyer {
476 1.27 christos uint32_t id;
477 1.26 bouyer struct fileusage *fup;
478 1.26 bouyer prop_dictionary_t dict, data;
479 1.26 bouyer prop_array_t cmds, datas;
480 1.26 bouyer int type;
481 1.26 bouyer
482 1.26 bouyer dict = quota2_prop_create();
483 1.26 bouyer cmds = prop_array_create();
484 1.26 bouyer
485 1.26 bouyer if (dict == NULL || cmds == NULL) {
486 1.26 bouyer errx(1, "can't allocate proplist");
487 1.26 bouyer }
488 1.26 bouyer
489 1.26 bouyer
490 1.26 bouyer for (type = 0; type < MAXQUOTAS; type++) {
491 1.26 bouyer if (valid[type] == 0)
492 1.26 bouyer continue;
493 1.26 bouyer datas = prop_array_create();
494 1.26 bouyer if (datas == NULL)
495 1.26 bouyer errx(1, "can't allocate proplist");
496 1.26 bouyer data = q2etoprop(&defaultq2e[type], 1);
497 1.26 bouyer if (data == NULL)
498 1.26 bouyer err(1, "q2etoprop(default)");
499 1.26 bouyer if (!prop_array_add_and_rel(datas, data))
500 1.26 bouyer err(1, "prop_array_add(data)");
501 1.26 bouyer
502 1.26 bouyer for (id = 0; id <= highid[type]; id++) {
503 1.26 bouyer fup = qremove(id, type);
504 1.26 bouyer if (fup == 0)
505 1.26 bouyer continue;
506 1.26 bouyer fup->fu_q2e.q2e_uid = id;
507 1.26 bouyer data = q2etoprop(&fup->fu_q2e, 0);
508 1.26 bouyer if (data == NULL)
509 1.26 bouyer err(1, "q2etoprop(default)");
510 1.26 bouyer if (!prop_array_add_and_rel(datas, data))
511 1.26 bouyer err(1, "prop_array_add(data)");
512 1.26 bouyer free(fup);
513 1.26 bouyer }
514 1.26 bouyer
515 1.26 bouyer if (!quota2_prop_add_command(cmds, "set",
516 1.26 bouyer qfextension[type], datas))
517 1.26 bouyer err(1, "prop_add_command");
518 1.1 cgd }
519 1.26 bouyer
520 1.26 bouyer if (!prop_dictionary_set(dict, "commands", cmds))
521 1.26 bouyer err(1, "prop_dictionary_set(command)");
522 1.26 bouyer
523 1.26 bouyer printf("%s\n", prop_dictionary_externalize(dict));
524 1.26 bouyer return;
525 1.1 cgd }
526 1.1 cgd
527 1.1 cgd /*
528 1.1 cgd * Check to see if target appears in list of size cnt.
529 1.1 cgd */
530 1.27 christos static int
531 1.27 christos oneof(const char *target, char *list[], int cnt)
532 1.1 cgd {
533 1.11 lukem int i;
534 1.1 cgd
535 1.1 cgd for (i = 0; i < cnt; i++)
536 1.1 cgd if (strcmp(target, list[i]) == 0)
537 1.27 christos return i;
538 1.27 christos return -1;
539 1.1 cgd }
540 1.1 cgd
541 1.1 cgd /*
542 1.1 cgd * Routines to manage the file usage table.
543 1.1 cgd *
544 1.1 cgd * Lookup an id of a specific type.
545 1.1 cgd */
546 1.1 cgd struct fileusage *
547 1.27 christos lookup(uint32_t id, int type)
548 1.1 cgd {
549 1.11 lukem struct fileusage *fup;
550 1.1 cgd
551 1.1 cgd for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
552 1.1 cgd if (fup->fu_id == id)
553 1.27 christos return fup;
554 1.27 christos return NULL;
555 1.1 cgd }
556 1.26 bouyer /*
557 1.26 bouyer * Lookup and remove an id of a specific type.
558 1.26 bouyer */
559 1.27 christos static struct fileusage *
560 1.27 christos qremove(uint32_t id, int type)
561 1.26 bouyer {
562 1.26 bouyer struct fileusage *fup, **fupp;
563 1.26 bouyer
564 1.26 bouyer for (fupp = &fuhead[type][id & (FUHASH-1)]; *fupp != 0;) {
565 1.26 bouyer fup = *fupp;
566 1.26 bouyer if (fup->fu_id == id) {
567 1.26 bouyer *fupp = fup->fu_next;
568 1.27 christos return fup;
569 1.26 bouyer }
570 1.26 bouyer fupp = &fup->fu_next;
571 1.26 bouyer }
572 1.27 christos return NULL;
573 1.26 bouyer }
574 1.1 cgd
575 1.1 cgd /*
576 1.1 cgd * Add a new file usage id if it does not already exist.
577 1.1 cgd */
578 1.27 christos static struct fileusage *
579 1.27 christos addid(uint32_t id, int type, const char *name)
580 1.1 cgd {
581 1.1 cgd struct fileusage *fup, **fhp;
582 1.26 bouyer struct group *gr = NULL;
583 1.26 bouyer struct passwd *pw = NULL;
584 1.27 christos size_t len;
585 1.1 cgd
586 1.26 bouyer if ((fup = lookup(id, type)) != NULL) {
587 1.27 christos return fup;
588 1.26 bouyer }
589 1.26 bouyer if (name == NULL) {
590 1.26 bouyer switch(type) {
591 1.26 bouyer case GRPQUOTA:
592 1.26 bouyer gr = getgrgid(id);
593 1.26 bouyer
594 1.26 bouyer if (gr != NULL)
595 1.26 bouyer name = gr->gr_name;
596 1.26 bouyer break;
597 1.26 bouyer case USRQUOTA:
598 1.26 bouyer pw = getpwuid(id);
599 1.26 bouyer if (pw)
600 1.26 bouyer name = pw->pw_name;
601 1.26 bouyer break;
602 1.26 bouyer default:
603 1.26 bouyer errx(1, "unknown quota type %d\n", type);
604 1.26 bouyer }
605 1.26 bouyer }
606 1.26 bouyer
607 1.1 cgd if (name)
608 1.1 cgd len = strlen(name);
609 1.1 cgd else
610 1.1 cgd len = 10;
611 1.27 christos if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
612 1.27 christos err(1, "out of memory for fileusage structures");
613 1.1 cgd fhp = &fuhead[type][id & (FUHASH - 1)];
614 1.1 cgd fup->fu_next = *fhp;
615 1.1 cgd *fhp = fup;
616 1.1 cgd fup->fu_id = id;
617 1.1 cgd if (id > highid[type])
618 1.1 cgd highid[type] = id;
619 1.1 cgd if (name) {
620 1.12 lukem memmove(fup->fu_name, name, len + 1);
621 1.1 cgd } else {
622 1.27 christos snprintf(fup->fu_name, sizeof(fup->fu_name), "%u", id);
623 1.1 cgd }
624 1.26 bouyer fup->fu_q2e = defaultq2e[type];
625 1.27 christos return fup;
626 1.1 cgd }
627