1 /** \file adu.c The main functions used by all modes of operation. */
3 #include <dirent.h> /* readdir() */
8 #include "gcc-compat.h"
13 #include "portable_io.h"
18 /** In case a signal is received, its number is stored here. */
21 /** Command line and config file options. */
22 struct gengetopt_args_info conf;
24 /** Options passed to --select-options. */
25 struct select_args_info select_conf;
27 /** The number of different uids found so far. */
28 uint32_t num_uids = 0;
30 /** This is always a power of two. It is set in create_hash_table(). */
31 static uint32_t uid_hash_table_size;
34 * Contains info for each user that owns at least one regular file.
36 * Even users that are not taken into account because of the --uid
37 * option occupy a slot in this hash table. This allows to find out
38 * quicky whether a uid is admissible. And yes, this has to be fast.
40 static struct user_info *uid_hash_table;
42 static inline int ui_used(struct user_info *ui)
44 return ui->flags & UI_FL_SLOT_USED;
47 static inline int ui_admissible(struct user_info *ui)
49 return ui->flags & UI_FL_ADMISSIBLE;
53 * The table containing the directory names and statistics.
55 struct osl_table *dir_table = NULL;
58 * Compare the size of two directories
60 * \param obj1 Pointer to the first object.
61 * \param obj2 Pointer to the second object.
63 * This function first compares the size values as usual integers. If they compare as
64 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
65 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
67 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
69 uint64_t d1 = *(uint64_t *)obj1->data;
70 uint64_t d2 = *(uint64_t *)obj2->data;
71 int ret = NUM_COMPARE(d2, d1);
75 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
76 return NUM_COMPARE(obj2->data, obj1->data);
80 * Compare two osl objects pointing to unsigned integers of 64 bit size.
82 * \param obj1 Pointer to the first integer.
83 * \param obj2 Pointer to the second integer.
85 * \return The values required for an osl compare function.
87 * \sa osl_compare_func, osl_hash_compare().
89 static int uint64_compare(const struct osl_object *obj1,
90 const struct osl_object *obj2)
92 uint64_t d1 = read_u64((const char *)obj1->data);
93 uint64_t d2 = read_u64((const char *)obj2->data);
102 static struct osl_column_description dir_table_cols[] = {
104 .storage_type = OSL_MAPPED_STORAGE,
109 .storage_type = OSL_MAPPED_STORAGE,
110 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
112 .compare_function = uint64_compare,
113 .data_size = sizeof(uint64_t)
116 .storage_type = OSL_MAPPED_STORAGE,
117 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
118 .name = "parent_num",
119 .compare_function = size_compare,
120 .data_size = sizeof(uint64_t)
123 .storage_type = OSL_MAPPED_STORAGE,
124 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
125 .compare_function = size_compare,
127 .data_size = sizeof(uint64_t)
130 .storage_type = OSL_MAPPED_STORAGE,
131 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
132 .compare_function = size_compare,
134 .data_size = sizeof(uint64_t)
138 static struct osl_table_description dir_table_desc = {
140 .num_columns = NUM_DT_COLUMNS,
142 .column_descriptions = dir_table_cols,
146 * The columns of the per-user tables.
148 * Adu tracks disk usage on a per-user basis. For each user, a user table is
149 * being created. The rows of the user table have three columns: The directory
150 * number that may be resolved to the path using the directory table, the
151 * number of bytes and the number of files in that directory owned by the given
154 static struct osl_column_description user_table_cols[] = {
156 .storage_type = OSL_MAPPED_STORAGE,
157 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
159 .compare_function = uint64_compare,
160 .data_size = sizeof(uint64_t)
163 .storage_type = OSL_MAPPED_STORAGE,
164 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
165 .compare_function = size_compare,
167 .data_size = sizeof(uint64_t)
170 .storage_type = OSL_MAPPED_STORAGE,
171 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
172 .compare_function = size_compare,
174 .data_size = sizeof(uint64_t)
181 * \param ll Loglevel.
182 * \param fmt Usual format string.
184 * All XXX_LOG() macros use this function.
186 __printf_2_3 void __log(int ll, const char* fmt,...)
194 if (ll < conf.loglevel_arg)
199 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
200 fprintf(outfd, "%s ", str);
202 vfprintf(outfd, fmt, argp);
206 static int open_user_table(struct user_info *ui, int create)
211 ui->desc = adu_malloc(sizeof(*ui->desc));
212 ui->desc->num_columns = NUM_UT_COLUMNS;
214 ui->desc->column_descriptions = user_table_cols;
215 ui->desc->dir = adu_strdup(conf.database_dir_arg);
216 ui->desc->name = make_message("%u", (unsigned)ui->uid);
217 pw = getpwuid(ui->uid);
218 if (pw && pw->pw_name)
219 ui->pw_name = adu_strdup(pw->pw_name);
221 INFO_LOG(".............................uid #%u: %u\n",
222 (unsigned)num_uids, (unsigned)ui->uid);
224 ret = osl(osl_create_table(ui->desc));
229 ret = osl(osl_open_table(ui->desc, &ui->table));
234 free((char *)ui->desc->name);
235 free((char *)ui->desc->dir);
238 ui->desc->name = NULL;
239 ui->desc->dir = NULL;
246 int for_each_admissible_user(int (*func)(struct user_info *, void *),
249 struct user_info *ui = uid_hash_table;
252 return -ERRNO_TO_ERROR(EFAULT);
254 for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
257 if (!ui_used(ui) || !ui_admissible(ui))
259 ret = func(ui, data);
266 #define PRIME1 0xb11924e1
267 #define PRIME2 0x01000193
269 void create_hash_table(unsigned bits)
271 uid_hash_table_size = 1 << bits;
272 uid_hash_table = adu_calloc(uid_hash_table_size *
273 sizeof(struct user_info));
276 static void free_hash_table(void)
278 free(uid_hash_table);
279 uid_hash_table = NULL;
282 static void close_dir_table(void)
288 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
290 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
291 free((char *)dir_table_desc.dir);
295 static int close_user_table(struct user_info *ui, __a_unused void *data)
299 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
301 ERROR_LOG("failed to close user table %u: %s\n",
302 (unsigned) ui->uid, adu_strerror(-ret));
303 free((char *)ui->desc->name);
304 ui->desc->name = NULL;
305 free((char *)ui->desc->dir);
306 ui->desc->dir = NULL;
316 static void close_user_tables(void)
318 for_each_admissible_user(close_user_table, NULL);
321 void close_all_tables(void)
328 static void signal_handler(int s)
333 void check_signals(void)
337 EMERG_LOG("caught signal %d\n", signum);
342 static int init_signals(void)
344 if (signal(SIGINT, &signal_handler) == SIG_ERR)
345 return -E_SIGNAL_SIG_ERR;
346 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
347 return -E_SIGNAL_SIG_ERR;
348 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
349 return -E_SIGNAL_SIG_ERR;
354 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
355 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
356 * unused slots in the table are used to store different uids that hash to the
359 * If a hash collision occurs, different slots are successively probed in order
360 * to find an unused slot for the new uid. Probing is implemented via a second
361 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
364 * An odd number is sufficient to make sure each entry of the hash table gets
365 * probed for probe_num between 0 and s-1 because s is a power of two, hence
366 * the second hash value has never a common divisor with the hash table size.
367 * IOW: h is invertible in the ring [0..s].
369 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
371 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
372 % uid_hash_table_size;
375 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
377 struct uid_range *ur;
380 if (!urs) /* empty array means all uids are allowed */
382 FOR_EACH_UID_RANGE(ur, urs)
383 if (ur->low <= uid && ur->high >= uid)
387 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
392 int search_uid(uint32_t uid, struct uid_range *urs,
393 enum search_uid_flags flags, struct user_info **ui_ptr)
397 for (p = 0; p < uid_hash_table_size; p++) {
398 struct user_info *ui = uid_hash_table + double_hash(uid, p);
405 ui->flags |= UI_FL_SLOT_USED;
406 if (!uid_is_admissible(uid, urs))
408 ui->flags |= UI_FL_ADMISSIBLE;
409 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
423 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
426 char *get_uid_list_name(void)
428 return make_message("%s/uid_list", conf.database_dir_arg);
431 void sort_hash_table(int (*comp)(const void *, const void *))
433 qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
437 int open_dir_table(int create)
439 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
442 int ret = osl(osl_create_table(&dir_table_desc));
444 free((char *)dir_table_desc.dir);
448 return osl(osl_open_table(&dir_table_desc, &dir_table));
451 static int check_args(void)
453 if (conf.create_given && !conf.base_dir_given)
456 /* remove trailing slashes from base-dir arg */
457 if (conf.base_dir_given) {
458 size_t len = strlen(conf.base_dir_arg);
460 if (!len) /* empty string */
461 return -ERRNO_TO_ERROR(EINVAL);
462 if (!--len) /* length 1 is always OK */
464 if (conf.base_dir_arg[len] != '/')
465 break; /* no trailing slash, also OK */
466 conf.base_dir_arg[len] = '\0';
472 static int print_complete_help_and_die(void)
475 select_cmdline_parser_init(&select_conf);
477 printf("%s-%s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
478 printf("%s\n\n", gengetopt_args_info_purpose);
479 printf("%s\n\n", gengetopt_args_info_usage);
482 line = gengetopt_args_info_help;
484 line = gengetopt_args_info_detailed_help;
485 for (; *line; line++)
486 printf("%s\n", *line);
489 line = select_args_info_help;
491 line = select_args_info_detailed_help;
492 printf("Select options:\n");
493 for (; *line; line++)
494 printf("%s\n", *line);
496 printf("Interactive commands:\n");
497 print_interactive_help();
501 int main(int argc, char **argv)
504 struct cmdline_parser_params params = {
508 .check_ambiguity = 0,
511 /* ignore errors and print complete help if --help was given */
512 cmdline_parser_ext(argc, argv, &conf, ¶ms);
513 if (conf.help_given || conf.detailed_help_given)
514 print_complete_help_and_die();
515 params.check_required = 1;
516 params.check_ambiguity = 1;
517 params.print_errors = 1;
518 ret = cmdline_parser_ext(argc, argv, &conf, ¶ms);
525 ret = init_signals();
529 if (conf.select_given)
531 else if (conf.create_given)
534 ret = com_interactive();
539 ERROR_LOG("%s\n", adu_strerror(-ret));
540 return -EXIT_FAILURE;