repquota.c revision 1.37 1 1.37 dholland /* $NetBSD: repquota.c,v 1.37 2012/01/09 15:42:37 dholland 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.37 dholland __RCSID("$NetBSD: repquota.c,v 1.37 2012/01/09 15:42:37 dholland 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
57 1.11 lukem #include <errno.h>
58 1.26 bouyer #include <err.h>
59 1.1 cgd #include <fstab.h>
60 1.11 lukem #include <grp.h>
61 1.1 cgd #include <pwd.h>
62 1.1 cgd #include <stdio.h>
63 1.11 lukem #include <stdlib.h>
64 1.6 cgd #include <string.h>
65 1.11 lukem #include <unistd.h>
66 1.1 cgd
67 1.30 bouyer #include <quota/quota.h>
68 1.37 dholland #include <quota/quotaprop.h>
69 1.34 dholland #include <quota.h>
70 1.26 bouyer
71 1.27 christos #include "printquota.h"
72 1.1 cgd
73 1.1 cgd struct fileusage {
74 1.1 cgd struct fileusage *fu_next;
75 1.33 dholland struct quotaval fu_qv[QUOTA_NLIMITS];
76 1.27 christos uint32_t fu_id;
77 1.1 cgd char fu_name[1];
78 1.1 cgd /* actually bigger */
79 1.1 cgd };
80 1.1 cgd #define FUHASH 1024 /* must be power of two */
81 1.30 bouyer static struct fileusage *fuhead[QUOTA_NCLASS][FUHASH];
82 1.30 bouyer static uint32_t highid[QUOTA_NCLASS]; /* highest addid()'ed identifier per class */
83 1.30 bouyer int valid[QUOTA_NCLASS];
84 1.33 dholland static struct quotaval defaultqv[QUOTA_NCLASS][QUOTA_NLIMITS];
85 1.1 cgd
86 1.27 christos static int vflag = 0; /* verbose */
87 1.27 christos static int aflag = 0; /* all file systems */
88 1.27 christos static int Dflag = 0; /* debug */
89 1.27 christos static int hflag = 0; /* humanize */
90 1.27 christos static int xflag = 0; /* export */
91 1.27 christos
92 1.27 christos
93 1.27 christos static struct fileusage *addid(uint32_t, int, const char *);
94 1.27 christos static struct fileusage *lookup(uint32_t, int);
95 1.27 christos static struct fileusage *qremove(uint32_t, int);
96 1.35 dholland static int repquota(struct quotahandle *, int);
97 1.27 christos static void usage(void) __attribute__((__noreturn__));
98 1.35 dholland static void printquotas(int, struct quotahandle *);
99 1.27 christos static void exportquotas(void);
100 1.37 dholland static int oneof(const char *, char *[], int cnt);
101 1.11 lukem
102 1.11 lukem int
103 1.27 christos main(int argc, char **argv)
104 1.1 cgd {
105 1.1 cgd int gflag = 0, uflag = 0, errs = 0;
106 1.1 cgd long i, argnum, done = 0;
107 1.9 mark int ch;
108 1.26 bouyer struct statvfs *fst;
109 1.26 bouyer int nfst;
110 1.35 dholland struct quotahandle *qh;
111 1.1 cgd
112 1.26 bouyer while ((ch = getopt(argc, argv, "Daguhvx")) != -1) {
113 1.1 cgd switch(ch) {
114 1.1 cgd case 'a':
115 1.1 cgd aflag++;
116 1.1 cgd break;
117 1.1 cgd case 'g':
118 1.1 cgd gflag++;
119 1.1 cgd break;
120 1.1 cgd case 'u':
121 1.1 cgd uflag++;
122 1.1 cgd break;
123 1.26 bouyer case 'h':
124 1.26 bouyer hflag++;
125 1.26 bouyer break;
126 1.1 cgd case 'v':
127 1.1 cgd vflag++;
128 1.1 cgd break;
129 1.26 bouyer case 'D':
130 1.26 bouyer Dflag++;
131 1.26 bouyer break;
132 1.26 bouyer case 'x':
133 1.26 bouyer xflag++;
134 1.26 bouyer break;
135 1.1 cgd default:
136 1.1 cgd usage();
137 1.1 cgd }
138 1.1 cgd }
139 1.1 cgd argc -= optind;
140 1.1 cgd argv += optind;
141 1.26 bouyer if (xflag && (argc != 1 || aflag))
142 1.26 bouyer usage();
143 1.1 cgd if (argc == 0 && !aflag)
144 1.1 cgd usage();
145 1.1 cgd if (!gflag && !uflag) {
146 1.1 cgd if (aflag)
147 1.1 cgd gflag++;
148 1.1 cgd uflag++;
149 1.1 cgd }
150 1.26 bouyer
151 1.26 bouyer nfst = getmntinfo(&fst, MNT_WAIT);
152 1.26 bouyer if (nfst == 0)
153 1.27 christos errx(1, "no filesystems mounted!");
154 1.26 bouyer for (i = 0; i < nfst; i++) {
155 1.26 bouyer if ((fst[i].f_flag & ST_QUOTA) == 0)
156 1.1 cgd continue;
157 1.35 dholland /* check if we want this volume */
158 1.35 dholland if (!aflag) {
159 1.35 dholland argnum = oneof(fst[i].f_mntonname, argv, argc);
160 1.35 dholland if (argnum < 0) {
161 1.35 dholland argnum = oneof(fst[i].f_mntfromname,
162 1.35 dholland argv, argc);
163 1.35 dholland }
164 1.35 dholland if (argnum < 0) {
165 1.35 dholland continue;
166 1.35 dholland }
167 1.35 dholland done |= 1U << argnum;
168 1.35 dholland }
169 1.35 dholland
170 1.35 dholland qh = quota_open(fst[i].f_mntonname);
171 1.35 dholland if (qh == NULL) {
172 1.35 dholland /* XXX: check this errno */
173 1.35 dholland if (errno == EOPNOTSUPP || errno == ENXIO) {
174 1.35 dholland continue;
175 1.35 dholland }
176 1.35 dholland warn("%s: quota_open", fst[i].f_mntonname);
177 1.1 cgd continue;
178 1.1 cgd }
179 1.35 dholland
180 1.35 dholland if (gflag)
181 1.35 dholland errs += repquota(qh, QUOTA_IDTYPE_GROUP);
182 1.35 dholland if (uflag)
183 1.35 dholland errs += repquota(qh, QUOTA_IDTYPE_USER);
184 1.35 dholland
185 1.35 dholland quota_close(qh);
186 1.1 cgd }
187 1.26 bouyer if (xflag)
188 1.26 bouyer exportquotas();
189 1.1 cgd for (i = 0; i < argc; i++)
190 1.27 christos if ((done & (1U << i)) == 0)
191 1.27 christos warnx("%s not mounted", argv[i]);
192 1.27 christos return errs;
193 1.1 cgd }
194 1.1 cgd
195 1.27 christos static void
196 1.27 christos usage(void)
197 1.27 christos {
198 1.27 christos const char *p = getprogname();
199 1.27 christos fprintf(stderr, "usage: %s [-D] [-v] [-g] [-u] -a\n"
200 1.27 christos "\t%s [-D] [-v] [-g] [-u] filesys ...\n"
201 1.27 christos "\t%s -x [-D] [-g] [-u] filesys\n", p, p, p);
202 1.1 cgd exit(1);
203 1.1 cgd }
204 1.1 cgd
205 1.27 christos static int
206 1.35 dholland repquota(struct quotahandle *qh, int idtype)
207 1.26 bouyer {
208 1.36 dholland struct quotacursor *qc;
209 1.36 dholland struct quotakey qk;
210 1.36 dholland struct quotaval qv;
211 1.35 dholland struct quotaval *qvp;
212 1.35 dholland struct fileusage *fup;
213 1.35 dholland
214 1.36 dholland qc = quota_opencursor(qh);
215 1.36 dholland if (qc == NULL) {
216 1.35 dholland return 1;
217 1.35 dholland }
218 1.26 bouyer
219 1.36 dholland if (idtype == QUOTA_IDTYPE_USER) {
220 1.36 dholland quotacursor_skipidtype(qc, QUOTA_IDTYPE_GROUP);
221 1.36 dholland }
222 1.36 dholland if (idtype == QUOTA_IDTYPE_GROUP) {
223 1.36 dholland quotacursor_skipidtype(qc, QUOTA_IDTYPE_USER);
224 1.36 dholland }
225 1.36 dholland
226 1.36 dholland valid[idtype] = 0;
227 1.36 dholland while (!quotacursor_atend(qc)) {
228 1.36 dholland if (quotacursor_get(qc, &qk, &qv)) {
229 1.36 dholland err(1, "%s: quotacursor_get", quota_getmountpoint(qh));
230 1.36 dholland }
231 1.36 dholland if (qk.qk_idtype != idtype) {
232 1.36 dholland continue;
233 1.36 dholland }
234 1.36 dholland
235 1.36 dholland valid[idtype] = 1;
236 1.36 dholland if (qk.qk_id == QUOTA_DEFAULTID) {
237 1.36 dholland qvp = defaultqv[idtype];
238 1.36 dholland } else {
239 1.36 dholland if ((fup = lookup(qk.qk_id, idtype)) == 0)
240 1.36 dholland fup = addid(qk.qk_id, idtype, (char *)0);
241 1.36 dholland qvp = fup->fu_qv;
242 1.36 dholland }
243 1.36 dholland if (qk.qk_objtype == QUOTA_OBJTYPE_BLOCKS) {
244 1.36 dholland qvp[QUOTA_OBJTYPE_BLOCKS] = qv;
245 1.36 dholland } else if (qk.qk_objtype == QUOTA_OBJTYPE_FILES) {
246 1.36 dholland qvp[QUOTA_OBJTYPE_FILES] = qv;
247 1.26 bouyer }
248 1.36 dholland }
249 1.35 dholland
250 1.34 dholland if (xflag == 0 && valid[idtype])
251 1.35 dholland printquotas(idtype, qh);
252 1.35 dholland
253 1.27 christos return 0;
254 1.26 bouyer }
255 1.26 bouyer
256 1.27 christos static void
257 1.35 dholland printquotas(int idtype, struct quotahandle *qh)
258 1.26 bouyer {
259 1.26 bouyer static int multiple = 0;
260 1.27 christos uint32_t id;
261 1.26 bouyer int i;
262 1.26 bouyer struct fileusage *fup;
263 1.33 dholland struct quotaval *q;
264 1.30 bouyer const char *timemsg[QUOTA_NLIMITS];
265 1.30 bouyer char overchar[QUOTA_NLIMITS];
266 1.27 christos time_t now;
267 1.29 bouyer char b0[2][20], b1[20], b2[20], b3[20];
268 1.26 bouyer
269 1.34 dholland switch (idtype) {
270 1.34 dholland case QUOTA_IDTYPE_GROUP:
271 1.26 bouyer {
272 1.26 bouyer struct group *gr;
273 1.26 bouyer setgrent();
274 1.26 bouyer while ((gr = getgrent()) != 0)
275 1.34 dholland (void)addid(gr->gr_gid, idtype, gr->gr_name);
276 1.26 bouyer endgrent();
277 1.26 bouyer break;
278 1.26 bouyer }
279 1.34 dholland case QUOTA_IDTYPE_USER:
280 1.26 bouyer {
281 1.26 bouyer struct passwd *pw;
282 1.26 bouyer setpwent();
283 1.26 bouyer while ((pw = getpwent()) != 0)
284 1.34 dholland (void)addid(pw->pw_uid, idtype, pw->pw_name);
285 1.26 bouyer endpwent();
286 1.26 bouyer break;
287 1.26 bouyer }
288 1.26 bouyer default:
289 1.34 dholland errx(1, "Unknown quota ID type %d", idtype);
290 1.1 cgd }
291 1.26 bouyer
292 1.27 christos time(&now);
293 1.26 bouyer
294 1.26 bouyer if (multiple++)
295 1.26 bouyer printf("\n");
296 1.26 bouyer if (vflag)
297 1.35 dholland printf("*** Report for %s quotas on %s (%s: %s)\n",
298 1.35 dholland ufs_quota_class_names[idtype], quota_getmountpoint(qh),
299 1.35 dholland quota_getmountdevice(qh), quota_getimplname(qh));
300 1.27 christos printf(" Block limits "
301 1.27 christos "File limits\n");
302 1.34 dholland printf(idtype == QUOTA_IDTYPE_USER ? "User " : "Group");
303 1.27 christos printf(" used soft hard grace used"
304 1.27 christos "soft hard grace\n");
305 1.34 dholland for (id = 0; id <= highid[idtype]; id++) {
306 1.34 dholland fup = qremove(id, idtype);
307 1.33 dholland q = fup->fu_qv;
308 1.1 cgd if (fup == 0)
309 1.1 cgd continue;
310 1.30 bouyer for (i = 0; i < QUOTA_NLIMITS; i++) {
311 1.33 dholland switch (QL_STATUS(quota_check_limit(q[i].qv_usage, 1,
312 1.33 dholland q[i].qv_softlimit, q[i].qv_hardlimit,
313 1.33 dholland q[i].qv_expiretime, now))) {
314 1.26 bouyer case QL_S_DENY_HARD:
315 1.26 bouyer case QL_S_DENY_GRACE:
316 1.26 bouyer case QL_S_ALLOW_SOFT:
317 1.29 bouyer timemsg[i] = timeprt(b0[i], 8, now,
318 1.33 dholland q[i].qv_expiretime);
319 1.26 bouyer overchar[i] = '+';
320 1.26 bouyer break;
321 1.26 bouyer default:
322 1.35 dholland if (vflag && q[i].qv_grace != QUOTA_NOTIME) {
323 1.35 dholland timemsg[i] = timeprt(b0[i], 8, 0,
324 1.35 dholland q[i].qv_grace);
325 1.35 dholland } else {
326 1.35 dholland timemsg[i] = "";
327 1.35 dholland }
328 1.26 bouyer overchar[i] = '-';
329 1.26 bouyer break;
330 1.26 bouyer }
331 1.26 bouyer }
332 1.26 bouyer
333 1.33 dholland if (q[QUOTA_LIMIT_BLOCK].qv_usage == 0 &&
334 1.33 dholland q[QUOTA_LIMIT_FILE].qv_usage == 0 && vflag == 0 &&
335 1.30 bouyer overchar[QUOTA_LIMIT_BLOCK] == '-' &&
336 1.30 bouyer overchar[QUOTA_LIMIT_FILE] == '-')
337 1.1 cgd continue;
338 1.22 jdolecek if (strlen(fup->fu_name) > 9)
339 1.22 jdolecek printf("%s ", fup->fu_name);
340 1.22 jdolecek else
341 1.22 jdolecek printf("%-10s", fup->fu_name);
342 1.26 bouyer printf("%c%c%9s%9s%9s%7s",
343 1.30 bouyer overchar[QUOTA_LIMIT_BLOCK], overchar[QUOTA_LIMIT_FILE],
344 1.33 dholland intprt(b1, 10, q[QUOTA_LIMIT_BLOCK].qv_usage,
345 1.30 bouyer HN_B, hflag),
346 1.33 dholland intprt(b2, 10, q[QUOTA_LIMIT_BLOCK].qv_softlimit,
347 1.30 bouyer HN_B, hflag),
348 1.33 dholland intprt(b3, 10, q[QUOTA_LIMIT_BLOCK].qv_hardlimit,
349 1.30 bouyer HN_B, hflag),
350 1.30 bouyer timemsg[QUOTA_LIMIT_BLOCK]);
351 1.26 bouyer printf(" %8s%8s%8s%7s\n",
352 1.33 dholland intprt(b1, 9, q[QUOTA_LIMIT_FILE].qv_usage, 0, hflag),
353 1.33 dholland intprt(b2, 9, q[QUOTA_LIMIT_FILE].qv_softlimit,
354 1.30 bouyer 0, hflag),
355 1.33 dholland intprt(b3, 9, q[QUOTA_LIMIT_FILE].qv_hardlimit,
356 1.30 bouyer 0, hflag),
357 1.30 bouyer timemsg[QUOTA_LIMIT_FILE]);
358 1.26 bouyer free(fup);
359 1.26 bouyer }
360 1.26 bouyer }
361 1.26 bouyer
362 1.27 christos static void
363 1.27 christos exportquotas(void)
364 1.26 bouyer {
365 1.27 christos uint32_t id;
366 1.26 bouyer struct fileusage *fup;
367 1.26 bouyer prop_dictionary_t dict, data;
368 1.26 bouyer prop_array_t cmds, datas;
369 1.34 dholland int idtype;
370 1.30 bouyer uint64_t *valuesp[QUOTA_NLIMITS];
371 1.26 bouyer
372 1.30 bouyer dict = quota_prop_create();
373 1.26 bouyer cmds = prop_array_create();
374 1.26 bouyer
375 1.26 bouyer if (dict == NULL || cmds == NULL) {
376 1.26 bouyer errx(1, "can't allocate proplist");
377 1.26 bouyer }
378 1.26 bouyer
379 1.26 bouyer
380 1.34 dholland for (idtype = 0; idtype < QUOTA_NCLASS; idtype++) {
381 1.34 dholland if (valid[idtype] == 0)
382 1.26 bouyer continue;
383 1.26 bouyer datas = prop_array_create();
384 1.26 bouyer if (datas == NULL)
385 1.26 bouyer errx(1, "can't allocate proplist");
386 1.30 bouyer valuesp[QUOTA_LIMIT_BLOCK] =
387 1.34 dholland &defaultqv[idtype][QUOTA_LIMIT_BLOCK].qv_hardlimit;
388 1.30 bouyer valuesp[QUOTA_LIMIT_FILE] =
389 1.34 dholland &defaultqv[idtype][QUOTA_LIMIT_FILE].qv_hardlimit;
390 1.30 bouyer data = quota64toprop(0, 1, valuesp,
391 1.30 bouyer ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
392 1.30 bouyer ufs_quota_limit_names, QUOTA_NLIMITS);
393 1.26 bouyer if (data == NULL)
394 1.30 bouyer err(1, "quota64toprop(default)");
395 1.26 bouyer if (!prop_array_add_and_rel(datas, data))
396 1.26 bouyer err(1, "prop_array_add(data)");
397 1.26 bouyer
398 1.34 dholland for (id = 0; id <= highid[idtype]; id++) {
399 1.34 dholland fup = qremove(id, idtype);
400 1.26 bouyer if (fup == 0)
401 1.26 bouyer continue;
402 1.30 bouyer valuesp[QUOTA_LIMIT_BLOCK] =
403 1.33 dholland &fup->fu_qv[QUOTA_LIMIT_BLOCK].qv_hardlimit;
404 1.30 bouyer valuesp[QUOTA_LIMIT_FILE] =
405 1.33 dholland &fup->fu_qv[QUOTA_LIMIT_FILE].qv_hardlimit;
406 1.30 bouyer data = quota64toprop(id, 0, valuesp,
407 1.30 bouyer ufs_quota_entry_names, UFS_QUOTA_NENTRIES,
408 1.30 bouyer ufs_quota_limit_names, QUOTA_NLIMITS);
409 1.26 bouyer if (data == NULL)
410 1.30 bouyer err(1, "quota64toprop(id)");
411 1.26 bouyer if (!prop_array_add_and_rel(datas, data))
412 1.26 bouyer err(1, "prop_array_add(data)");
413 1.26 bouyer free(fup);
414 1.26 bouyer }
415 1.26 bouyer
416 1.30 bouyer if (!quota_prop_add_command(cmds, "set",
417 1.34 dholland ufs_quota_class_names[idtype], datas))
418 1.26 bouyer err(1, "prop_add_command");
419 1.1 cgd }
420 1.26 bouyer
421 1.26 bouyer if (!prop_dictionary_set(dict, "commands", cmds))
422 1.26 bouyer err(1, "prop_dictionary_set(command)");
423 1.26 bouyer
424 1.26 bouyer printf("%s\n", prop_dictionary_externalize(dict));
425 1.26 bouyer return;
426 1.1 cgd }
427 1.1 cgd
428 1.1 cgd /*
429 1.1 cgd * Routines to manage the file usage table.
430 1.1 cgd *
431 1.34 dholland * Lookup an id of a specific id type.
432 1.1 cgd */
433 1.1 cgd struct fileusage *
434 1.34 dholland lookup(uint32_t id, int idtype)
435 1.1 cgd {
436 1.11 lukem struct fileusage *fup;
437 1.1 cgd
438 1.34 dholland for (fup = fuhead[idtype][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
439 1.1 cgd if (fup->fu_id == id)
440 1.27 christos return fup;
441 1.27 christos return NULL;
442 1.1 cgd }
443 1.26 bouyer /*
444 1.34 dholland * Lookup and remove an id of a specific id type.
445 1.26 bouyer */
446 1.27 christos static struct fileusage *
447 1.34 dholland qremove(uint32_t id, int idtype)
448 1.26 bouyer {
449 1.26 bouyer struct fileusage *fup, **fupp;
450 1.26 bouyer
451 1.34 dholland for (fupp = &fuhead[idtype][id & (FUHASH-1)]; *fupp != 0;) {
452 1.26 bouyer fup = *fupp;
453 1.26 bouyer if (fup->fu_id == id) {
454 1.26 bouyer *fupp = fup->fu_next;
455 1.27 christos return fup;
456 1.26 bouyer }
457 1.26 bouyer fupp = &fup->fu_next;
458 1.26 bouyer }
459 1.27 christos return NULL;
460 1.26 bouyer }
461 1.1 cgd
462 1.1 cgd /*
463 1.1 cgd * Add a new file usage id if it does not already exist.
464 1.1 cgd */
465 1.27 christos static struct fileusage *
466 1.34 dholland addid(uint32_t id, int idtype, const char *name)
467 1.1 cgd {
468 1.1 cgd struct fileusage *fup, **fhp;
469 1.26 bouyer struct group *gr = NULL;
470 1.26 bouyer struct passwd *pw = NULL;
471 1.27 christos size_t len;
472 1.1 cgd
473 1.34 dholland if ((fup = lookup(id, idtype)) != NULL) {
474 1.27 christos return fup;
475 1.26 bouyer }
476 1.26 bouyer if (name == NULL) {
477 1.34 dholland switch(idtype) {
478 1.34 dholland case QUOTA_IDTYPE_GROUP:
479 1.26 bouyer gr = getgrgid(id);
480 1.26 bouyer
481 1.26 bouyer if (gr != NULL)
482 1.26 bouyer name = gr->gr_name;
483 1.26 bouyer break;
484 1.34 dholland case QUOTA_IDTYPE_USER:
485 1.26 bouyer pw = getpwuid(id);
486 1.26 bouyer if (pw)
487 1.26 bouyer name = pw->pw_name;
488 1.26 bouyer break;
489 1.26 bouyer default:
490 1.34 dholland errx(1, "Unknown quota ID type %d\n", idtype);
491 1.26 bouyer }
492 1.26 bouyer }
493 1.26 bouyer
494 1.1 cgd if (name)
495 1.1 cgd len = strlen(name);
496 1.1 cgd else
497 1.1 cgd len = 10;
498 1.27 christos if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
499 1.27 christos err(1, "out of memory for fileusage structures");
500 1.34 dholland fhp = &fuhead[idtype][id & (FUHASH - 1)];
501 1.1 cgd fup->fu_next = *fhp;
502 1.1 cgd *fhp = fup;
503 1.1 cgd fup->fu_id = id;
504 1.34 dholland if (id > highid[idtype])
505 1.34 dholland highid[idtype] = id;
506 1.1 cgd if (name) {
507 1.12 lukem memmove(fup->fu_name, name, len + 1);
508 1.1 cgd } else {
509 1.28 christos snprintf(fup->fu_name, len + 1, "%u", id);
510 1.1 cgd }
511 1.36 dholland /*
512 1.36 dholland * XXX nothing guarantees the default limits have been loaded yet
513 1.36 dholland */
514 1.34 dholland fup->fu_qv[QUOTA_LIMIT_BLOCK] = defaultqv[idtype][QUOTA_LIMIT_BLOCK];
515 1.34 dholland fup->fu_qv[QUOTA_LIMIT_FILE] = defaultqv[idtype][QUOTA_LIMIT_FILE];
516 1.27 christos return fup;
517 1.1 cgd }
518 1.37 dholland
519 1.37 dholland /*
520 1.37 dholland * Check to see if target appears in list of size cnt.
521 1.37 dholland */
522 1.37 dholland static int
523 1.37 dholland oneof(const char *target, char *list[], int cnt)
524 1.37 dholland {
525 1.37 dholland int i;
526 1.37 dholland
527 1.37 dholland for (i = 0; i < cnt; i++)
528 1.37 dholland if (strcmp(target, list[i]) == 0)
529 1.37 dholland return i;
530 1.37 dholland return -1;
531 1.37 dholland }
532