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 /** The number of different uids found so far. */
22 uint32_t num_uids = 0;
24 /** This is always a power of two. It is set in create_hash_table(). */
25 static uint32_t uid_hash_table_size;
28 * Contains info for each user that owns at least one regular file.
30 * Even users that are not taken into account because of the --uid
31 * option occupy a slot in this hash table. This allows to find out
32 * quicky whether a uid is admissible. And yes, this has to be fast.
34 static struct user_info *uid_hash_table;
36 static inline int ui_used(struct user_info *ui)
38 return ui->flags & UI_FL_SLOT_USED;
41 static inline int ui_admissible(struct user_info *ui)
43 return ui->flags & UI_FL_ADMISSIBLE;
47 * The table containing the directory names and statistics.
49 struct osl_table *dir_table = NULL;
52 * The array of all uid ranges that were given at the command line.
54 struct uid_range *admissible_uids;
56 /** Evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y. */
57 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
60 * Compare the size of two directories
62 * \param obj1 Pointer to the first object.
63 * \param obj2 Pointer to the second object.
65 * This function first compares the size values as usual integers. If they compare as
66 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
67 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
69 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
71 uint64_t d1 = *(uint64_t *)obj1->data;
72 uint64_t d2 = *(uint64_t *)obj2->data;
73 int ret = NUM_COMPARE(d2, d1);
77 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
78 return NUM_COMPARE(obj2->data, obj1->data);
82 * Compare two osl objects pointing to unsigned integers of 64 bit size.
84 * \param obj1 Pointer to the first integer.
85 * \param obj2 Pointer to the second integer.
87 * \return The values required for an osl compare function.
89 * \sa osl_compare_func, osl_hash_compare().
91 static int uint64_compare(const struct osl_object *obj1,
92 const struct osl_object *obj2)
94 uint64_t d1 = read_u64((const char *)obj1->data);
95 uint64_t d2 = read_u64((const char *)obj2->data);
104 static struct osl_column_description dir_table_cols[] = {
106 .storage_type = OSL_MAPPED_STORAGE,
111 .storage_type = OSL_MAPPED_STORAGE,
112 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
114 .compare_function = uint64_compare,
115 .data_size = sizeof(uint64_t)
118 .storage_type = OSL_MAPPED_STORAGE,
119 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
120 .name = "parent_num",
121 .compare_function = size_compare,
122 .data_size = sizeof(uint64_t)
125 .storage_type = OSL_MAPPED_STORAGE,
126 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
127 .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)
140 static struct osl_table_description dir_table_desc = {
142 .num_columns = NUM_DT_COLUMNS,
144 .column_descriptions = dir_table_cols,
147 static struct osl_column_description user_table_cols[] = {
149 .storage_type = OSL_MAPPED_STORAGE,
150 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
152 .compare_function = uint64_compare,
153 .data_size = sizeof(uint64_t)
156 .storage_type = OSL_MAPPED_STORAGE,
157 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
158 .compare_function = size_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)
171 static int check_uid_arg(const char *arg, uint32_t *uid)
173 const uint32_t max = ~0U;
175 * we need an 64-bit int for string -> uid conversion because strtoll()
176 * returns a signed value.
179 int ret = atoi64(arg, &val);
183 if (val < 0 || val > max)
184 return -ERRNO_TO_ERROR(EINVAL);
189 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
192 char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
194 if (!p || p == arg) { /* -42 or 42 */
195 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
198 ur->low = p? 0 : ur->high;
205 ret = check_uid_arg(arg, &ur->low);
209 if (*p) { /* 42-4711 */
210 ret = check_uid_arg(p, &ur->high);
214 if (ur->low > ur->high)
215 ret = -ERRNO_TO_ERROR(EINVAL);
218 ERROR_LOG("bad uid option: %s\n", orig_arg);
220 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
229 * \param ll Loglevel.
230 * \param fml Usual format string.
232 * All XXX_LOG() macros use this function.
234 __printf_2_3 void __log(int ll, const char* fmt,...)
242 if (ll < conf.loglevel_arg)
247 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
248 fprintf(outfd, "%s ", str);
250 vfprintf(outfd, fmt, argp);
254 static int open_user_table(struct user_info *ui, int create)
259 ui->desc = adu_malloc(sizeof(*ui->desc));
260 ui->desc->num_columns = NUM_UT_COLUMNS;
262 ui->desc->column_descriptions = user_table_cols;
263 ui->desc->dir = adu_strdup(conf.database_dir_arg);
264 ui->desc->name = make_message("%u", (unsigned)ui->uid);
265 pw = getpwuid(ui->uid);
266 if (pw && pw->pw_name)
267 ui->pw_name = adu_strdup(pw->pw_name);
269 INFO_LOG(".............................uid #%u: %u\n",
270 (unsigned)num_uids, (unsigned)ui->uid);
272 ret = osl(osl_create_table(ui->desc));
277 ret = osl(osl_open_table(ui->desc, &ui->table));
282 free((char *)ui->desc->name);
283 free((char *)ui->desc->dir);
286 ui->desc->name = NULL;
287 ui->desc->dir = NULL;
294 int for_each_admissible_user(int (*func)(struct user_info *, void *),
297 struct user_info *ui = uid_hash_table;
300 return -ERRNO_TO_ERROR(EFAULT);
302 for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
305 if (!ui_used(ui) || !ui_admissible(ui))
307 ret = func(ui, data);
314 #define PRIME1 0x811c9dc5
315 #define PRIME2 0x01000193
317 void create_hash_table(unsigned bits)
319 uid_hash_table_size = 1 << bits;
320 uid_hash_table = adu_calloc(uid_hash_table_size *
321 sizeof(struct user_info));
324 static void free_hash_table(void)
326 free(uid_hash_table);
327 uid_hash_table = NULL;
330 static void close_dir_table(void)
336 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
338 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
339 free((char *)dir_table_desc.dir);
343 static int close_user_table(struct user_info *ui, __a_unused void *data)
347 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
349 ERROR_LOG("failed to close user table %u: %s\n",
350 (unsigned) ui->uid, adu_strerror(-ret));
351 free((char *)ui->desc->name);
352 ui->desc->name = NULL;
353 free((char *)ui->desc->dir);
354 ui->desc->dir = NULL;
364 static void close_user_tables(void)
366 for_each_admissible_user(close_user_table, NULL);
369 void close_all_tables(void)
376 static void signal_handler(int s)
381 void check_signals(void)
385 EMERG_LOG("caught signal %d\n", signum);
390 static int init_signals(void)
392 if (signal(SIGINT, &signal_handler) == SIG_ERR)
393 return -E_SIGNAL_SIG_ERR;
394 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
395 return -E_SIGNAL_SIG_ERR;
396 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
397 return -E_SIGNAL_SIG_ERR;
402 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
403 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
404 * unused slots in the table are used to store different uids that hash to the
407 * If a hash collision occurs, different slots are successively probed in order
408 * to find an unused slot for the new uid. Probing is implemented via a second
409 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
412 * An odd number is sufficient to make sure each entry of the hash table gets
413 * probed for probe_num between 0 and s-1 because s is a power of two, hence
414 * the second hash value has never a common divisor with the hash table size.
415 * IOW: h is invertible in the ring [0..s].
417 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
419 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
420 % uid_hash_table_size;
423 static int uid_is_admissible(uint32_t uid)
427 for (i = 0; i < conf.uid_given; i++) {
428 struct uid_range *ur = admissible_uids + i;
430 if (ur->low <= uid && ur->high >= uid)
433 i = !conf.uid_given || i < conf.uid_given;
434 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
439 int search_uid(uint32_t uid, enum search_uid_flags flags,
440 struct user_info **ui_ptr)
444 for (p = 0; p < uid_hash_table_size; p++) {
445 struct user_info *ui = uid_hash_table + double_hash(uid, p);
452 ui->flags |= UI_FL_SLOT_USED;
453 if (!uid_is_admissible(uid))
455 ui->flags |= UI_FL_ADMISSIBLE;
456 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
470 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
473 char *get_uid_list_name(void)
475 return make_message("%s/uid_list", conf.database_dir_arg);
478 int open_dir_table(int create)
480 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
483 int ret = osl(osl_create_table(&dir_table_desc));
485 free((char *)dir_table_desc.dir);
489 return osl(osl_open_table(&dir_table_desc, &dir_table));
492 static int check_args(void)
497 if (conf.create_given && !conf.base_dir_given)
500 /* remove trailing slashes from base-dir arg */
501 if (conf.base_dir_given) {
502 size_t len = strlen(conf.base_dir_arg);
504 if (!len) /* empty string */
505 return -ERRNO_TO_ERROR(EINVAL);
506 if (!--len) /* length 1 is always OK */
508 if (conf.base_dir_arg[len] != '/')
509 break; /* no trailing slash, also OK */
510 conf.base_dir_arg[len] = '\0';
515 admissible_uids = adu_malloc(conf.uid_given * sizeof(*admissible_uids));
516 for (i = 0; i < conf.uid_given; i++) {
517 ret = parse_uid_range(conf.uid_arg[i], admissible_uids + i);
523 free(admissible_uids);
524 admissible_uids = NULL;
528 int main(int argc, char **argv)
531 struct cmdline_parser_params params = {
535 .check_ambiguity = 1,
539 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
543 ret = init_signals();
547 if (conf.select_given)
554 free(admissible_uids);
556 ERROR_LOG("%s\n", adu_strerror(-ret));
557 return -EXIT_FAILURE;