2 #include <dirent.h> /* readdir() */
7 #include "gcc-compat.h"
12 #include "portable_io.h"
17 /** In case a signal is received, its number is stored here. */
20 /** Command line and config file options. */
21 struct gengetopt_args_info conf;
23 /** Options passed to --select-options. */
24 struct select_args_info select_conf;
26 /** The number of different uids found so far. */
27 uint32_t num_uids = 0;
29 /** This is always a power of two. It is set in create_hash_table(). */
30 static uint32_t uid_hash_table_size;
33 * Contains info for each user that owns at least one regular file.
35 * Even users that are not taken into account because of the --uid
36 * option occupy a slot in this hash table. This allows to find out
37 * quicky whether a uid is admissible. And yes, this has to be fast.
39 static struct user_info *uid_hash_table;
41 static inline int ui_used(struct user_info *ui)
43 return ui->flags & UI_FL_SLOT_USED;
46 static inline int ui_admissible(struct user_info *ui)
48 return ui->flags & UI_FL_ADMISSIBLE;
52 * The table containing the directory names and statistics.
54 struct osl_table *dir_table = NULL;
57 * Compare the size of two directories
59 * \param obj1 Pointer to the first object.
60 * \param obj2 Pointer to the second object.
62 * This function first compares the size values as usual integers. If they compare as
63 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
64 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
66 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
68 uint64_t d1 = *(uint64_t *)obj1->data;
69 uint64_t d2 = *(uint64_t *)obj2->data;
70 int ret = NUM_COMPARE(d2, d1);
74 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
75 return NUM_COMPARE(obj2->data, obj1->data);
79 * Compare two osl objects pointing to unsigned integers of 64 bit size.
81 * \param obj1 Pointer to the first integer.
82 * \param obj2 Pointer to the second integer.
84 * \return The values required for an osl compare function.
86 * \sa osl_compare_func, osl_hash_compare().
88 static int uint64_compare(const struct osl_object *obj1,
89 const struct osl_object *obj2)
91 uint64_t d1 = read_u64((const char *)obj1->data);
92 uint64_t d2 = read_u64((const char *)obj2->data);
101 static struct osl_column_description dir_table_cols[] = {
103 .storage_type = OSL_MAPPED_STORAGE,
108 .storage_type = OSL_MAPPED_STORAGE,
109 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
111 .compare_function = uint64_compare,
112 .data_size = sizeof(uint64_t)
115 .storage_type = OSL_MAPPED_STORAGE,
116 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
117 .name = "parent_num",
118 .compare_function = size_compare,
119 .data_size = sizeof(uint64_t)
122 .storage_type = OSL_MAPPED_STORAGE,
123 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
124 .compare_function = size_compare,
126 .data_size = sizeof(uint64_t)
129 .storage_type = OSL_MAPPED_STORAGE,
130 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
131 .compare_function = size_compare,
133 .data_size = sizeof(uint64_t)
137 static struct osl_table_description dir_table_desc = {
139 .num_columns = NUM_DT_COLUMNS,
141 .column_descriptions = dir_table_cols,
145 * The columns of the per-user tables.
147 * Adu tracks disk usage on a per-user basis. For each user, a user table is
148 * being created. The rows of the user table have three columns: The directory
149 * number that may be resolved to the path using the directory table, the
150 * number of bytes and the number of files in that directory owned by the given
153 static struct osl_column_description user_table_cols[] = {
155 .storage_type = OSL_MAPPED_STORAGE,
156 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
158 .compare_function = uint64_compare,
159 .data_size = sizeof(uint64_t)
162 .storage_type = OSL_MAPPED_STORAGE,
163 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
164 .compare_function = size_compare,
166 .data_size = sizeof(uint64_t)
169 .storage_type = OSL_MAPPED_STORAGE,
170 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
171 .compare_function = size_compare,
173 .data_size = sizeof(uint64_t)
180 * \param ll Loglevel.
181 * \param fml Usual format string.
183 * All XXX_LOG() macros use this function.
185 __printf_2_3 void __log(int ll, const char* fmt,...)
193 if (ll < conf.loglevel_arg)
198 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
199 fprintf(outfd, "%s ", str);
201 vfprintf(outfd, fmt, argp);
205 static int open_user_table(struct user_info *ui, int create)
210 ui->desc = adu_malloc(sizeof(*ui->desc));
211 ui->desc->num_columns = NUM_UT_COLUMNS;
213 ui->desc->column_descriptions = user_table_cols;
214 ui->desc->dir = adu_strdup(conf.database_dir_arg);
215 ui->desc->name = make_message("%u", (unsigned)ui->uid);
216 pw = getpwuid(ui->uid);
217 if (pw && pw->pw_name)
218 ui->pw_name = adu_strdup(pw->pw_name);
220 INFO_LOG(".............................uid #%u: %u\n",
221 (unsigned)num_uids, (unsigned)ui->uid);
223 ret = osl(osl_create_table(ui->desc));
228 ret = osl(osl_open_table(ui->desc, &ui->table));
233 free((char *)ui->desc->name);
234 free((char *)ui->desc->dir);
237 ui->desc->name = NULL;
238 ui->desc->dir = NULL;
245 int for_each_admissible_user(int (*func)(struct user_info *, void *),
248 struct user_info *ui = uid_hash_table;
251 return -ERRNO_TO_ERROR(EFAULT);
253 for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
256 if (!ui_used(ui) || !ui_admissible(ui))
258 ret = func(ui, data);
265 #define PRIME1 0xb11924e1
266 #define PRIME2 0x01000193
268 void create_hash_table(unsigned bits)
270 uid_hash_table_size = 1 << bits;
271 uid_hash_table = adu_calloc(uid_hash_table_size *
272 sizeof(struct user_info));
275 static void free_hash_table(void)
277 free(uid_hash_table);
278 uid_hash_table = NULL;
281 static void close_dir_table(void)
287 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
289 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
290 free((char *)dir_table_desc.dir);
294 static int close_user_table(struct user_info *ui, __a_unused void *data)
298 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
300 ERROR_LOG("failed to close user table %u: %s\n",
301 (unsigned) ui->uid, adu_strerror(-ret));
302 free((char *)ui->desc->name);
303 ui->desc->name = NULL;
304 free((char *)ui->desc->dir);
305 ui->desc->dir = NULL;
315 static void close_user_tables(void)
317 for_each_admissible_user(close_user_table, NULL);
320 void close_all_tables(void)
327 static void signal_handler(int s)
332 void check_signals(void)
336 EMERG_LOG("caught signal %d\n", signum);
341 static int init_signals(void)
343 if (signal(SIGINT, &signal_handler) == SIG_ERR)
344 return -E_SIGNAL_SIG_ERR;
345 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
346 return -E_SIGNAL_SIG_ERR;
347 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
348 return -E_SIGNAL_SIG_ERR;
353 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
354 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
355 * unused slots in the table are used to store different uids that hash to the
358 * If a hash collision occurs, different slots are successively probed in order
359 * to find an unused slot for the new uid. Probing is implemented via a second
360 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
363 * An odd number is sufficient to make sure each entry of the hash table gets
364 * probed for probe_num between 0 and s-1 because s is a power of two, hence
365 * the second hash value has never a common divisor with the hash table size.
366 * IOW: h is invertible in the ring [0..s].
368 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
370 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
371 % uid_hash_table_size;
374 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
376 struct uid_range *ur;
379 if (!urs) /* empty array means all uids are allowed */
381 FOR_EACH_UID_RANGE(ur, urs)
382 if (ur->low <= uid && ur->high >= uid)
386 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
391 int search_uid(uint32_t uid, struct uid_range *urs,
392 enum search_uid_flags flags, struct user_info **ui_ptr)
396 for (p = 0; p < uid_hash_table_size; p++) {
397 struct user_info *ui = uid_hash_table + double_hash(uid, p);
404 ui->flags |= UI_FL_SLOT_USED;
405 if (!uid_is_admissible(uid, urs))
407 ui->flags |= UI_FL_ADMISSIBLE;
408 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
422 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
425 char *get_uid_list_name(void)
427 return make_message("%s/uid_list", conf.database_dir_arg);
430 void sort_hash_table(int (*comp)(const void *, const void *))
432 qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
436 int open_dir_table(int create)
438 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
441 int ret = osl(osl_create_table(&dir_table_desc));
443 free((char *)dir_table_desc.dir);
447 return osl(osl_open_table(&dir_table_desc, &dir_table));
450 static int check_args(void)
452 if (conf.create_given && !conf.base_dir_given)
455 /* remove trailing slashes from base-dir arg */
456 if (conf.base_dir_given) {
457 size_t len = strlen(conf.base_dir_arg);
459 if (!len) /* empty string */
460 return -ERRNO_TO_ERROR(EINVAL);
461 if (!--len) /* length 1 is always OK */
463 if (conf.base_dir_arg[len] != '/')
464 break; /* no trailing slash, also OK */
465 conf.base_dir_arg[len] = '\0';
471 static int print_complete_help_and_die(void)
474 select_cmdline_parser_init(&select_conf);
476 printf("%s-%s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
477 printf("%s\n\n", gengetopt_args_info_purpose);
478 printf("%s\n\n", gengetopt_args_info_usage);
481 line = gengetopt_args_info_help;
483 line = gengetopt_args_info_detailed_help;
484 for (; *line; line++)
485 printf("%s\n", *line);
488 line = select_args_info_help;
490 line = select_args_info_detailed_help;
491 printf("Select options:\n");
492 for (; *line; line++)
493 printf("%s\n", *line);
495 printf("Interactive commands:\n");
496 print_interactive_help();
500 int main(int argc, char **argv)
503 struct cmdline_parser_params params = {
507 .check_ambiguity = 0,
510 /* ignore errors and print complete help if --help was given */
511 cmdline_parser_ext(argc, argv, &conf, ¶ms);
512 if (conf.help_given || conf.detailed_help_given)
513 print_complete_help_and_die();
514 params.check_required = 1;
515 params.check_ambiguity = 1;
516 params.print_errors = 1;
517 ret = cmdline_parser_ext(argc, argv, &conf, ¶ms);
524 ret = init_signals();
528 if (conf.select_given)
530 else if (conf.create_given)
533 ret = com_interactive();
538 ERROR_LOG("%s\n", adu_strerror(-ret));
539 return -EXIT_FAILURE;