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;
44 * The table containing the directory names and statistics.
46 struct osl_table *dir_table = NULL;
49 * The array of all uid ranges that were given at the command line.
51 struct uid_range *admissible_uids;
53 /** Evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y. */
54 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
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,
144 static struct osl_column_description user_table_cols[] = {
146 .storage_type = OSL_MAPPED_STORAGE,
147 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
149 .compare_function = uint64_compare,
150 .data_size = sizeof(uint64_t)
153 .storage_type = OSL_MAPPED_STORAGE,
154 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
155 .compare_function = size_compare,
157 .data_size = sizeof(uint64_t)
160 .storage_type = OSL_MAPPED_STORAGE,
161 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
162 .compare_function = size_compare,
164 .data_size = sizeof(uint64_t)
168 static int check_uid_arg(const char *arg, uint32_t *uid)
170 const uint32_t max = ~0U;
172 * we need an 64-bit int for string -> uid conversion because strtoll()
173 * returns a signed value.
176 int ret = atoi64(arg, &val);
180 if (val < 0 || val > max)
181 return -ERRNO_TO_ERROR(EINVAL);
186 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
189 char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
191 if (!p || p == arg) { /* -42 or 42 */
192 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
195 ur->low = p? 0 : ur->high;
202 ret = check_uid_arg(arg, &ur->low);
206 if (*p) { /* 42-4711 */
207 ret = check_uid_arg(p, &ur->high);
211 if (ur->low > ur->high)
212 ret = -ERRNO_TO_ERROR(EINVAL);
215 ERROR_LOG("bad uid option: %s\n", orig_arg);
217 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
226 * \param ll Loglevel.
227 * \param fml Usual format string.
229 * All XXX_LOG() macros use this function.
231 __printf_2_3 void __log(int ll, const char* fmt,...)
239 if (ll < conf.loglevel_arg)
244 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
245 fprintf(outfd, "%s ", str);
247 vfprintf(outfd, fmt, argp);
251 static int open_user_table(struct user_info *ui, int create)
256 ui->desc = adu_malloc(sizeof(*ui->desc));
257 ui->desc->num_columns = NUM_UT_COLUMNS;
259 ui->desc->column_descriptions = user_table_cols;
260 ui->desc->dir = adu_strdup(conf.database_dir_arg);
261 ui->desc->name = make_message("%u", (unsigned)ui->uid);
262 pw = getpwuid(ui->uid);
263 if (pw && pw->pw_name)
264 ui->pw_name = adu_strdup(pw->pw_name);
266 INFO_LOG(".............................uid #%u: %u\n",
267 (unsigned)num_uids, (unsigned)ui->uid);
269 ret = osl(osl_create_table(ui->desc));
274 ret = osl(osl_open_table(ui->desc, &ui->table));
279 free((char *)ui->desc->name);
280 free((char *)ui->desc->dir);
283 ui->desc->name = NULL;
284 ui->desc->dir = NULL;
291 int for_each_admissible_user(int (*func)(struct user_info *, void *),
294 struct user_info *ui = uid_hash_table;
297 return -ERRNO_TO_ERROR(EFAULT);
299 for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
302 if (!ui_used(ui) || !ui_admissible(ui))
304 ret = func(ui, data);
311 #define PRIME1 0x811c9dc5
312 #define PRIME2 0x01000193
314 void create_hash_table(unsigned bits)
316 uid_hash_table_size = 1 << bits;
317 uid_hash_table = adu_calloc(uid_hash_table_size *
318 sizeof(struct user_info));
321 static void free_hash_table(void)
323 free(uid_hash_table);
324 uid_hash_table = NULL;
327 static void close_dir_table(void)
333 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
335 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
336 free((char *)dir_table_desc.dir);
340 static int close_user_table(struct user_info *ui, __a_unused void *data)
344 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
346 ERROR_LOG("failed to close user table %u: %s\n",
347 (unsigned) ui->uid, adu_strerror(-ret));
348 free((char *)ui->desc->name);
349 ui->desc->name = NULL;
350 free((char *)ui->desc->dir);
351 ui->desc->dir = NULL;
361 static void close_user_tables(void)
363 for_each_admissible_user(close_user_table, NULL);
366 void close_all_tables(void)
373 static void signal_handler(int s)
378 void check_signals(void)
382 EMERG_LOG("caught signal %d\n", signum);
387 static int init_signals(void)
389 if (signal(SIGINT, &signal_handler) == SIG_ERR)
390 return -E_SIGNAL_SIG_ERR;
391 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
392 return -E_SIGNAL_SIG_ERR;
393 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
394 return -E_SIGNAL_SIG_ERR;
399 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
400 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
401 * unused slots in the table are used to store different uids that hash to the
404 * If a hash collision occurs, different slots are successively probed in order
405 * to find an unused slot for the new uid. Probing is implemented via a second
406 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
409 * An odd number is sufficient to make sure each entry of the hash table gets
410 * probed for probe_num between 0 and s-1 because s is a power of two, hence
411 * the second hash value has never a common divisor with the hash table size.
412 * IOW: h is invertible in the ring [0..s].
414 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
416 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
417 % uid_hash_table_size;
420 static int uid_is_admissible(uint32_t uid)
424 for (i = 0; i < conf.uid_given; i++) {
425 struct uid_range *ur = admissible_uids + i;
427 if (ur->low <= uid && ur->high >= uid)
430 i = !conf.uid_given || i < conf.uid_given;
431 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
436 int search_uid(uint32_t uid, enum search_uid_flags flags,
437 struct user_info **ui_ptr)
441 for (p = 0; p < uid_hash_table_size; p++) {
442 struct user_info *ui = uid_hash_table + double_hash(uid, p);
449 ui->flags |= UI_FL_SLOT_USED;
450 if (!uid_is_admissible(uid))
452 ui->flags |= UI_FL_ADMISSIBLE;
453 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
467 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
470 char *get_uid_list_name(void)
472 return make_message("%s/uid_list", conf.database_dir_arg);
475 int open_dir_table(int create)
477 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
480 int ret = osl(osl_create_table(&dir_table_desc));
482 free((char *)dir_table_desc.dir);
486 return osl(osl_open_table(&dir_table_desc, &dir_table));
489 static int check_args(void)
494 if (conf.create_given && !conf.base_dir_given)
497 /* remove trailing slashes from base-dir arg */
498 if (conf.base_dir_given) {
499 size_t len = strlen(conf.base_dir_arg);
501 if (!len) /* empty string */
502 return -ERRNO_TO_ERROR(EINVAL);
503 if (!--len) /* length 1 is always OK */
505 if (conf.base_dir_arg[len] != '/')
506 break; /* no trailing slash, also OK */
507 conf.base_dir_arg[len] = '\0';
512 admissible_uids = adu_malloc(conf.uid_given * sizeof(*admissible_uids));
513 for (i = 0; i < conf.uid_given; i++) {
514 ret = parse_uid_range(conf.uid_arg[i], admissible_uids + i);
520 free(admissible_uids);
521 admissible_uids = NULL;
525 int main(int argc, char **argv)
528 struct cmdline_parser_params params = {
532 .check_ambiguity = 1,
536 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
540 ret = init_signals();
544 if (conf.select_given)
551 free(admissible_uids);
553 ERROR_LOG("%s\n", adu_strerror(-ret));
554 return -EXIT_FAILURE;