2 #include <dirent.h> /* readdir() */
5 #include "gcc-compat.h"
10 #include "portable_io.h"
15 /** In case a signal is received, its number is stored here. */
18 /** Command line and config file options. */
19 struct gengetopt_args_info conf;
21 /** Global dir count. */
22 uint64_t num_dirs = 0;
23 /** Global files count. */
24 uint64_t num_files = 0;
25 /** Global bytes count. */
26 uint64_t num_bytes = 0;
28 /** The number of different uids found so far. */
29 uint32_t num_uids = 0;
32 * Contains info for each user that owns at least one regular file.
34 * Even users that are not taken into account because of the --uid
35 * option occupy a slot in this hash table. This allows to find out
36 * quicky whether a uid is admissible. And yes, this has to be fast.
38 struct user_info *uid_hash_table = NULL;
41 * The table containing the directory names and statistics.
43 struct osl_table *dir_table = NULL;
46 * The array of all uid ranges that were given at the command line.
48 struct uid_range *admissible_uids;
50 /** Evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y. */
51 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
54 * Compare the size of two directories
56 * \param obj1 Pointer to the first object.
57 * \param obj2 Pointer to the second object.
59 * This function first compares the size values as usual integers. If they compare as
60 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
61 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
63 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
65 uint64_t d1 = *(uint64_t *)obj1->data;
66 uint64_t d2 = *(uint64_t *)obj2->data;
67 int ret = NUM_COMPARE(d2, d1);
71 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
72 return NUM_COMPARE(obj2->data, obj1->data);
76 * Compare two osl objects pointing to unsigned integers of 64 bit size.
78 * \param obj1 Pointer to the first integer.
79 * \param obj2 Pointer to the second integer.
81 * \return The values required for an osl compare function.
83 * \sa osl_compare_func, osl_hash_compare().
85 static int uint64_compare(const struct osl_object *obj1,
86 const struct osl_object *obj2)
88 uint64_t d1 = read_u64((const char *)obj1->data);
89 uint64_t d2 = read_u64((const char *)obj2->data);
98 static struct osl_column_description dir_table_cols[] = {
100 .storage_type = OSL_MAPPED_STORAGE,
105 .storage_type = OSL_MAPPED_STORAGE,
106 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
108 .compare_function = uint64_compare,
109 .data_size = sizeof(uint64_t)
112 .storage_type = OSL_MAPPED_STORAGE,
113 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
114 .name = "parent_num",
115 .compare_function = size_compare,
116 .data_size = sizeof(uint64_t)
119 .storage_type = OSL_MAPPED_STORAGE,
120 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
121 .compare_function = size_compare,
123 .data_size = sizeof(uint64_t)
126 .storage_type = OSL_MAPPED_STORAGE,
127 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
128 .compare_function = size_compare,
130 .data_size = sizeof(uint64_t)
134 static struct osl_table_description dir_table_desc = {
136 .num_columns = NUM_DT_COLUMNS,
138 .column_descriptions = dir_table_cols,
141 static struct osl_column_description user_table_cols[] = {
143 .storage_type = OSL_MAPPED_STORAGE,
144 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
146 .compare_function = uint64_compare,
147 .data_size = sizeof(uint64_t)
150 .storage_type = OSL_MAPPED_STORAGE,
151 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
152 .compare_function = size_compare,
154 .data_size = sizeof(uint64_t)
157 .storage_type = OSL_MAPPED_STORAGE,
158 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
159 .compare_function = size_compare,
161 .data_size = sizeof(uint64_t)
165 static int check_uid_arg(const char *arg, uint32_t *uid)
167 const uint32_t max = ~0U;
169 * we need an 64-bit int for string -> uid conversion because strtoll()
170 * returns a signed value.
173 int ret = atoi64(arg, &val);
177 if (val < 0 || val > max)
178 return -ERRNO_TO_ERROR(EINVAL);
183 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
186 char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
188 if (!p || p == arg) { /* -42 or 42 */
189 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
192 ur->low = p? 0 : ur->high;
199 ret = check_uid_arg(arg, &ur->low);
203 if (*p) { /* 42-4711 */
204 ret = check_uid_arg(p, &ur->high);
208 if (ur->low > ur->high)
209 ret = -ERRNO_TO_ERROR(EINVAL);
212 ERROR_LOG("bad uid option: %s\n", orig_arg);
214 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
223 * \param ll Loglevel.
224 * \param fml Usual format string.
226 * All XXX_LOG() macros use this function.
228 __printf_2_3 void __log(int ll, const char* fmt,...)
236 if (ll < conf.loglevel_arg)
241 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
242 fprintf(outfd, "%s ", str);
244 vfprintf(outfd, fmt, argp);
248 static int open_user_table(struct user_info *ui, int create)
253 ui->desc = adu_malloc(sizeof(*ui->desc));
254 ui->desc->num_columns = NUM_UT_COLUMNS;
256 ui->desc->column_descriptions = user_table_cols;
257 ui->desc->dir = adu_strdup(conf.database_dir_arg);
258 ui->desc->name = make_message("%u", (unsigned)ui->uid);
259 pw = getpwuid(ui->uid);
260 if (pw && pw->pw_name)
261 ui->pw_name = adu_strdup(pw->pw_name);
263 INFO_LOG(".............................uid #%u: %u\n",
264 (unsigned)num_uids, (unsigned)ui->uid);
266 ret = osl(osl_create_table(ui->desc));
271 ret = osl(osl_open_table(ui->desc, &ui->table));
276 free((char *)ui->desc->name);
277 free((char *)ui->desc->dir);
280 ui->desc->name = NULL;
281 ui->desc->dir = NULL;
288 #define uid_hash_bits 8
289 uint32_t uid_hash_table_size = 1 << uid_hash_bits;
290 #define PRIME1 0x811c9dc5
291 #define PRIME2 0x01000193
293 void create_hash_table(void)
295 uid_hash_table = adu_calloc(uid_hash_table_size
296 * sizeof(struct user_info));
299 static void free_hash_table(void)
301 free(uid_hash_table);
302 uid_hash_table = NULL;
305 static void close_dir_table(void)
311 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
313 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
314 free((char *)dir_table_desc.dir);
318 static void close_user_table(struct user_info *ui)
322 if (!ui || !ui_used(ui) || !ui_admissible(ui))
324 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
326 ERROR_LOG("failed to close user table %u: %s\n",
327 (unsigned) ui->uid, adu_strerror(-ret));
328 free((char *)ui->desc->name);
329 ui->desc->name = NULL;
330 free((char *)ui->desc->dir);
331 ui->desc->dir = NULL;
340 static void close_user_tables(void)
342 struct user_info *ui;
345 close_user_table(ui);
348 void close_all_tables(void)
355 static void signal_handler(int s)
360 void check_signals(void)
364 EMERG_LOG("caught signal %d\n", signum);
369 static int init_signals(void)
371 if (signal(SIGINT, &signal_handler) == SIG_ERR)
372 return -E_SIGNAL_SIG_ERR;
373 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
374 return -E_SIGNAL_SIG_ERR;
379 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
380 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
381 * unused slots in the table are used to store different uids that hash to the
384 * If a hash collision occurs, different slots are successively probed in order
385 * to find an unused slot for the new uid. Probing is implemented via a second
386 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
389 * An odd number is sufficient to make sure each entry of the hash table gets
390 * probed for probe_num between 0 and s-1 because s is a power of two, hence
391 * the second hash value has never a common divisor with the hash table size.
392 * IOW: h is invertible in the ring [0..s].
394 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
396 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
397 % uid_hash_table_size;
400 static int uid_is_admissible(uint32_t uid)
404 for (i = 0; i < conf.uid_given; i++) {
405 struct uid_range *ur = admissible_uids + i;
407 if (ur->low <= uid && ur->high >= uid)
410 i = !conf.uid_given || i < conf.uid_given;
411 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
416 int search_uid(uint32_t uid, enum search_uid_flags flags,
417 struct user_info **ui_ptr)
421 for (p = 0; p < uid_hash_table_size; p++) {
422 struct user_info *ui = uid_hash_table + double_hash(uid, p);
429 ui->flags |= UI_FL_SLOT_USED;
430 if (!uid_is_admissible(uid))
432 ui->flags |= UI_FL_ADMISSIBLE;
433 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
447 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
450 char *get_uid_list_name(void)
452 return make_message("%s/uid_list", conf.database_dir_arg);
455 int open_dir_table(int create)
457 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
460 int ret = osl(osl_create_table(&dir_table_desc));
462 free((char *)dir_table_desc.dir);
466 return osl(osl_open_table(&dir_table_desc, &dir_table));
469 static int check_args(void)
474 if (conf.create_given && !conf.base_dir_given)
477 /* remove trailing slashes from base-dir arg */
478 if (conf.base_dir_given) {
479 size_t len = strlen(conf.base_dir_arg);
481 if (!len) /* empty string */
482 return -ERRNO_TO_ERROR(EINVAL);
483 if (!--len) /* length 1 is always OK */
485 if (conf.base_dir_arg[len] != '/')
486 break; /* no trailing slash, also OK */
487 conf.base_dir_arg[len] = '\0';
492 admissible_uids = adu_malloc(conf.uid_given * sizeof(*admissible_uids));
493 for (i = 0; i < conf.uid_given; i++) {
494 ret = parse_uid_range(conf.uid_arg[i], admissible_uids + i);
500 free(admissible_uids);
501 admissible_uids = NULL;
505 int main(int argc, char **argv)
508 struct cmdline_parser_params params = {
512 .check_ambiguity = 0,
516 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
520 ret = init_signals();
524 if (conf.select_given)
531 free(admissible_uids);
533 ERROR_LOG("%s\n", adu_strerror(-ret));
534 return -EXIT_FAILURE;