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;
31 /** This is always a power of two. It is set in create_hash_table(). */
32 static uint32_t uid_hash_table_size;
35 * Contains info for each user that owns at least one regular file.
37 * Even users that are not taken into account because of the --uid
38 * option occupy a slot in this hash table. This allows to find out
39 * quicky whether a uid is admissible. And yes, this has to be fast.
41 static struct user_info *uid_hash_table;
43 static inline int ui_used(struct user_info *ui)
45 return ui->flags & UI_FL_SLOT_USED;
48 static inline int ui_admissible(struct user_info *ui)
50 return ui->flags & UI_FL_ADMISSIBLE;
54 * The table containing the directory names and statistics.
56 struct osl_table *dir_table = NULL;
59 * The array of all uid ranges that were given at the command line.
61 struct uid_range *admissible_uids;
63 /** Evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y. */
64 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
67 * Compare the size of two directories
69 * \param obj1 Pointer to the first object.
70 * \param obj2 Pointer to the second object.
72 * This function first compares the size values as usual integers. If they compare as
73 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
74 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
76 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
78 uint64_t d1 = *(uint64_t *)obj1->data;
79 uint64_t d2 = *(uint64_t *)obj2->data;
80 int ret = NUM_COMPARE(d2, d1);
84 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
85 return NUM_COMPARE(obj2->data, obj1->data);
89 * Compare two osl objects pointing to unsigned integers of 64 bit size.
91 * \param obj1 Pointer to the first integer.
92 * \param obj2 Pointer to the second integer.
94 * \return The values required for an osl compare function.
96 * \sa osl_compare_func, osl_hash_compare().
98 static int uint64_compare(const struct osl_object *obj1,
99 const struct osl_object *obj2)
101 uint64_t d1 = read_u64((const char *)obj1->data);
102 uint64_t d2 = read_u64((const char *)obj2->data);
111 static struct osl_column_description dir_table_cols[] = {
113 .storage_type = OSL_MAPPED_STORAGE,
118 .storage_type = OSL_MAPPED_STORAGE,
119 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
121 .compare_function = uint64_compare,
122 .data_size = sizeof(uint64_t)
125 .storage_type = OSL_MAPPED_STORAGE,
126 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
127 .name = "parent_num",
128 .compare_function = size_compare,
129 .data_size = sizeof(uint64_t)
132 .storage_type = OSL_MAPPED_STORAGE,
133 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
134 .compare_function = size_compare,
136 .data_size = sizeof(uint64_t)
139 .storage_type = OSL_MAPPED_STORAGE,
140 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
141 .compare_function = size_compare,
143 .data_size = sizeof(uint64_t)
147 static struct osl_table_description dir_table_desc = {
149 .num_columns = NUM_DT_COLUMNS,
151 .column_descriptions = dir_table_cols,
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)
178 static int check_uid_arg(const char *arg, uint32_t *uid)
180 const uint32_t max = ~0U;
182 * we need an 64-bit int for string -> uid conversion because strtoll()
183 * returns a signed value.
186 int ret = atoi64(arg, &val);
190 if (val < 0 || val > max)
191 return -ERRNO_TO_ERROR(EINVAL);
196 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
199 char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
201 if (!p || p == arg) { /* -42 or 42 */
202 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
205 ur->low = p? 0 : ur->high;
212 ret = check_uid_arg(arg, &ur->low);
216 if (*p) { /* 42-4711 */
217 ret = check_uid_arg(p, &ur->high);
221 if (ur->low > ur->high)
222 ret = -ERRNO_TO_ERROR(EINVAL);
225 ERROR_LOG("bad uid option: %s\n", orig_arg);
227 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
236 * \param ll Loglevel.
237 * \param fml Usual format string.
239 * All XXX_LOG() macros use this function.
241 __printf_2_3 void __log(int ll, const char* fmt,...)
249 if (ll < conf.loglevel_arg)
254 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
255 fprintf(outfd, "%s ", str);
257 vfprintf(outfd, fmt, argp);
261 static int open_user_table(struct user_info *ui, int create)
266 ui->desc = adu_malloc(sizeof(*ui->desc));
267 ui->desc->num_columns = NUM_UT_COLUMNS;
269 ui->desc->column_descriptions = user_table_cols;
270 ui->desc->dir = adu_strdup(conf.database_dir_arg);
271 ui->desc->name = make_message("%u", (unsigned)ui->uid);
272 pw = getpwuid(ui->uid);
273 if (pw && pw->pw_name)
274 ui->pw_name = adu_strdup(pw->pw_name);
276 INFO_LOG(".............................uid #%u: %u\n",
277 (unsigned)num_uids, (unsigned)ui->uid);
279 ret = osl(osl_create_table(ui->desc));
284 ret = osl(osl_open_table(ui->desc, &ui->table));
289 free((char *)ui->desc->name);
290 free((char *)ui->desc->dir);
293 ui->desc->name = NULL;
294 ui->desc->dir = NULL;
301 int for_each_admissible_user(int (*func)(struct user_info *, void *),
304 struct user_info *ui = uid_hash_table;
307 return -ERRNO_TO_ERROR(EFAULT);
309 for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
312 if (!ui_used(ui) || !ui_admissible(ui))
314 ret = func(ui, data);
321 #define PRIME1 0x811c9dc5
322 #define PRIME2 0x01000193
324 void create_hash_table(unsigned bits)
326 uid_hash_table_size = 1 << bits;
327 uid_hash_table = adu_calloc(uid_hash_table_size *
328 sizeof(struct user_info));
331 static void free_hash_table(void)
333 free(uid_hash_table);
334 uid_hash_table = NULL;
337 static void close_dir_table(void)
343 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
345 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
346 free((char *)dir_table_desc.dir);
350 static int close_user_table(struct user_info *ui, __a_unused void *data)
354 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
356 ERROR_LOG("failed to close user table %u: %s\n",
357 (unsigned) ui->uid, adu_strerror(-ret));
358 free((char *)ui->desc->name);
359 ui->desc->name = NULL;
360 free((char *)ui->desc->dir);
361 ui->desc->dir = NULL;
371 static void close_user_tables(void)
373 for_each_admissible_user(close_user_table, NULL);
376 void close_all_tables(void)
383 static void signal_handler(int s)
388 void check_signals(void)
392 EMERG_LOG("caught signal %d\n", signum);
397 static int init_signals(void)
399 if (signal(SIGINT, &signal_handler) == SIG_ERR)
400 return -E_SIGNAL_SIG_ERR;
401 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
402 return -E_SIGNAL_SIG_ERR;
403 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
404 return -E_SIGNAL_SIG_ERR;
409 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
410 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
411 * unused slots in the table are used to store different uids that hash to the
414 * If a hash collision occurs, different slots are successively probed in order
415 * to find an unused slot for the new uid. Probing is implemented via a second
416 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
419 * An odd number is sufficient to make sure each entry of the hash table gets
420 * probed for probe_num between 0 and s-1 because s is a power of two, hence
421 * the second hash value has never a common divisor with the hash table size.
422 * IOW: h is invertible in the ring [0..s].
424 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
426 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
427 % uid_hash_table_size;
430 static int uid_is_admissible(uint32_t uid)
434 for (i = 0; i < conf.uid_given; i++) {
435 struct uid_range *ur = admissible_uids + i;
437 if (ur->low <= uid && ur->high >= uid)
440 i = !conf.uid_given || i < conf.uid_given;
441 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
446 int search_uid(uint32_t uid, enum search_uid_flags flags,
447 struct user_info **ui_ptr)
451 for (p = 0; p < uid_hash_table_size; p++) {
452 struct user_info *ui = uid_hash_table + double_hash(uid, p);
459 ui->flags |= UI_FL_SLOT_USED;
460 if (!uid_is_admissible(uid))
462 ui->flags |= UI_FL_ADMISSIBLE;
463 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
477 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
480 char *get_uid_list_name(void)
482 return make_message("%s/uid_list", conf.database_dir_arg);
485 int open_dir_table(int create)
487 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
490 int ret = osl(osl_create_table(&dir_table_desc));
492 free((char *)dir_table_desc.dir);
496 return osl(osl_open_table(&dir_table_desc, &dir_table));
499 static int check_args(void)
504 if (conf.create_given && !conf.base_dir_given)
507 /* remove trailing slashes from base-dir arg */
508 if (conf.base_dir_given) {
509 size_t len = strlen(conf.base_dir_arg);
511 if (!len) /* empty string */
512 return -ERRNO_TO_ERROR(EINVAL);
513 if (!--len) /* length 1 is always OK */
515 if (conf.base_dir_arg[len] != '/')
516 break; /* no trailing slash, also OK */
517 conf.base_dir_arg[len] = '\0';
522 admissible_uids = adu_malloc(conf.uid_given * sizeof(*admissible_uids));
523 for (i = 0; i < conf.uid_given; i++) {
524 ret = parse_uid_range(conf.uid_arg[i], admissible_uids + i);
530 free(admissible_uids);
531 admissible_uids = NULL;
535 int main(int argc, char **argv)
538 struct cmdline_parser_params params = {
542 .check_ambiguity = 1,
546 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
550 ret = init_signals();
554 if (conf.select_given)
561 free(admissible_uids);
563 ERROR_LOG("%s\n", adu_strerror(-ret));
564 return -EXIT_FAILURE;