2 * Copyright (c) 2011 François Tigeot <ftigeot@wolfpond.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
15 * 3. Neither the name of The DragonFly Project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific, prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/mount.h>
35 #include <sys/vfs_quota.h>
42 #include <libprop/proplib.h>
46 static bool flag_debug = 0;
48 static void usage(int);
49 static int get_dirsize(char *);
50 static int get_fslist(void);
55 fprintf(stderr, "usage: vquota [-D] check directory\n");
56 fprintf(stderr, " vquota [-D] lsfs\n");
57 fprintf(stderr, " vquota [-D] show mount_point\n");
62 get_dirsize(char* dirname)
67 uint64_t size_of_files = 0;
70 /* TODO: check directory name sanity */
71 fts_args[0] = dirname;
74 if ((fts = fts_open(fts_args, FTS_PHYSICAL, NULL)) == NULL)
75 err(1, "fts_open() failed");
77 while ((p = fts_read(fts)) != NULL) {
78 switch (p->fts_info) {
79 /* directories, ignore them */
84 /* read errors, warn, continue and flag */
88 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
92 size_of_files += p->fts_statp->st_size;
97 printf("%"PRIu64"\n", size_of_files);
101 /* print a list of filesystems with accounting enabled */
102 static int get_fslist(void) {
103 struct statfs *mntbufp;
106 /* read mount table from kernel */
107 nloc = getmntinfo(&mntbufp, MNT_NOWAIT|MNT_LOCAL);
109 perror("getmntinfo");
113 /* iterate mounted filesystems */
114 for (i=0; i<nloc; i++) {
115 /* vfs accounting enabled on this one ? */
116 if (mntbufp[i].f_flags & MNT_ACCOUNTING)
117 printf("%s on %s\n", mntbufp[i].f_mntfromname,
118 mntbufp[i].f_mntonname);
125 send_command(const char *path, const char *cmd,
126 prop_dictionary_t args, prop_dictionary_t *res) {
127 prop_dictionary_t dict;
128 struct plistref pref;
133 dict = prop_dictionary_create();
136 printf("send_command(): couldn't create dictionary\n");
140 rv = prop_dictionary_set_cstring(dict, "command", cmd);
142 printf("send_command(): couldn't initialize dictionary\n");
146 rv = prop_dictionary_set(dict, "arguments", args);
148 printf("prop_dictionary_set() failed\n");
152 error = prop_dictionary_send_syscall(dict, &pref);
154 printf("prop_dictionary_send_syscall() failed\n");
155 prop_object_release(dict);
160 printf("message to kernel:\n%s\n", prop_dictionary_externalize(dict));
162 error = vquotactl(path, &pref);
164 printf("send_command: vquotactl = %d\n", error);
168 error = prop_dictionary_recv_syscall(&pref, res);
170 printf("prop_dictionary_recv_syscall() failed\n");
174 printf("Message from kernel:\n%s\n", prop_dictionary_externalize(*res));
179 /* show collected statistics on mount point */
180 static int show_mp(char *path) {
181 prop_dictionary_t args, res;
182 prop_array_t reslist;
184 prop_object_iterator_t iter;
185 prop_dictionary_t item;
189 args = prop_dictionary_create();
190 res = prop_dictionary_create();
192 printf("couldn't create args dictionary\n");
194 rv = send_command(path, "get usage all", args, &res);
196 printf("show-mp(): failed to send message to kernel\n");
200 reslist = prop_dictionary_get(res, "get usage all");
201 if (reslist == NULL) {
202 printf("show_mp(): failed to get array of results");
207 iter = prop_array_iterator(reslist);
209 printf("show_mp(): failed to create iterator\n");
214 while ((item = prop_object_iterator_next(iter)) != NULL) {
215 rv = prop_dictionary_get_uint64(item, "space used", &space);
216 if (prop_dictionary_get_uint32(item, "uid", &id))
217 printf("uid %u:", id);
218 else if (prop_dictionary_get_uint32(item, "gid", &id))
219 printf("gid %u:", id);
221 printf("total space used");
222 printf(" %" PRIu64 "\n", space);
224 prop_object_iterator_release(iter);
227 prop_object_release(args);
228 prop_object_release(res);
233 main(int argc, char **argv) {
236 while ((ch = getopt(argc, argv, "D")) != -1) {
248 if (strcmp(argv[0], "check") == 0) {
251 return get_dirsize(argv[1]);
253 if (strcmp(argv[0], "lsfs") == 0) {
256 if (strcmp(argv[0], "show") == 0) {
259 return show_mp(argv[1]);