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;
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,
145 static struct osl_column_description user_table_cols[] = {
147 .storage_type = OSL_MAPPED_STORAGE,
148 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
150 .compare_function = uint64_compare,
151 .data_size = sizeof(uint64_t)
154 .storage_type = OSL_MAPPED_STORAGE,
155 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
156 .compare_function = size_compare,
158 .data_size = sizeof(uint64_t)
161 .storage_type = OSL_MAPPED_STORAGE,
162 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
163 .compare_function = size_compare,
165 .data_size = sizeof(uint64_t)
169 static int check_uid_arg(const char *arg, uint32_t *uid)
171 const uint32_t max = ~0U;
173 * we need an 64-bit int for string -> uid conversion because strtoll()
174 * returns a signed value.
177 int ret = atoi64(arg, &val);
181 if (val < 0 || val > max)
182 return -ERRNO_TO_ERROR(EINVAL);
187 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
190 char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
192 if (!p || p == arg) { /* -42 or 42 */
193 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
196 ur->low = p? 0 : ur->high;
203 ret = check_uid_arg(arg, &ur->low);
207 if (*p) { /* 42-4711 */
208 ret = check_uid_arg(p, &ur->high);
212 if (ur->low > ur->high)
213 ret = -ERRNO_TO_ERROR(EINVAL);
216 ERROR_LOG("bad uid option: %s\n", orig_arg);
218 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
227 * \param ll Loglevel.
228 * \param fml Usual format string.
230 * All XXX_LOG() macros use this function.
232 __printf_2_3 void __log(int ll, const char* fmt,...)
240 if (ll < conf.loglevel_arg)
245 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
246 fprintf(outfd, "%s ", str);
248 vfprintf(outfd, fmt, argp);
252 static int open_user_table(struct user_info *ui, int create)
257 ui->desc = adu_malloc(sizeof(*ui->desc));
258 ui->desc->num_columns = NUM_UT_COLUMNS;
260 ui->desc->column_descriptions = user_table_cols;
261 ui->desc->dir = adu_strdup(conf.database_dir_arg);
262 ui->desc->name = make_message("%u", (unsigned)ui->uid);
263 pw = getpwuid(ui->uid);
264 if (pw && pw->pw_name)
265 ui->pw_name = adu_strdup(pw->pw_name);
267 INFO_LOG(".............................uid #%u: %u\n",
268 (unsigned)num_uids, (unsigned)ui->uid);
270 ret = osl(osl_create_table(ui->desc));
275 ret = osl(osl_open_table(ui->desc, &ui->table));
280 free((char *)ui->desc->name);
281 free((char *)ui->desc->dir);
284 ui->desc->name = NULL;
285 ui->desc->dir = NULL;
292 int for_each_admissible_user(int (*func)(struct user_info *, void *),
295 struct user_info *ui = uid_hash_table;
298 return -ERRNO_TO_ERROR(EFAULT);
300 for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
303 if (!ui_used(ui) || !ui_admissible(ui))
305 ret = func(ui, data);
312 #define PRIME1 0xb11924e1
313 #define PRIME2 0x01000193
315 void create_hash_table(unsigned bits)
317 uid_hash_table_size = 1 << bits;
318 uid_hash_table = adu_calloc(uid_hash_table_size *
319 sizeof(struct user_info));
322 static void free_hash_table(void)
324 free(uid_hash_table);
325 uid_hash_table = NULL;
328 static void close_dir_table(void)
334 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
336 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
337 free((char *)dir_table_desc.dir);
341 static int close_user_table(struct user_info *ui, __a_unused void *data)
345 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
347 ERROR_LOG("failed to close user table %u: %s\n",
348 (unsigned) ui->uid, adu_strerror(-ret));
349 free((char *)ui->desc->name);
350 ui->desc->name = NULL;
351 free((char *)ui->desc->dir);
352 ui->desc->dir = NULL;
362 static void close_user_tables(void)
364 for_each_admissible_user(close_user_table, NULL);
367 void close_all_tables(void)
374 static void signal_handler(int s)
379 void check_signals(void)
383 EMERG_LOG("caught signal %d\n", signum);
388 static int init_signals(void)
390 if (signal(SIGINT, &signal_handler) == SIG_ERR)
391 return -E_SIGNAL_SIG_ERR;
392 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
393 return -E_SIGNAL_SIG_ERR;
394 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
395 return -E_SIGNAL_SIG_ERR;
400 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
401 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
402 * unused slots in the table are used to store different uids that hash to the
405 * If a hash collision occurs, different slots are successively probed in order
406 * to find an unused slot for the new uid. Probing is implemented via a second
407 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
410 * An odd number is sufficient to make sure each entry of the hash table gets
411 * probed for probe_num between 0 and s-1 because s is a power of two, hence
412 * the second hash value has never a common divisor with the hash table size.
413 * IOW: h is invertible in the ring [0..s].
415 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
417 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
418 % uid_hash_table_size;
421 static int uid_is_admissible(uint32_t uid)
425 for (i = 0; i < conf.uid_given; i++) {
426 struct uid_range *ur = admissible_uids + i;
428 if (ur->low <= uid && ur->high >= uid)
431 i = !conf.uid_given || i < conf.uid_given;
432 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
437 int search_uid(uint32_t uid, enum search_uid_flags flags,
438 struct user_info **ui_ptr)
442 for (p = 0; p < uid_hash_table_size; p++) {
443 struct user_info *ui = uid_hash_table + double_hash(uid, p);
450 ui->flags |= UI_FL_SLOT_USED;
451 if (!uid_is_admissible(uid))
453 ui->flags |= UI_FL_ADMISSIBLE;
454 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
468 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
471 char *get_uid_list_name(void)
473 return make_message("%s/uid_list", conf.database_dir_arg);
476 void sort_hash_table(int (*comp)(const void *, const void *))
478 qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
482 int open_dir_table(int create)
484 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
487 int ret = osl(osl_create_table(&dir_table_desc));
489 free((char *)dir_table_desc.dir);
493 return osl(osl_open_table(&dir_table_desc, &dir_table));
496 static int check_args(void)
501 if (conf.create_given && !conf.base_dir_given)
504 /* remove trailing slashes from base-dir arg */
505 if (conf.base_dir_given) {
506 size_t len = strlen(conf.base_dir_arg);
508 if (!len) /* empty string */
509 return -ERRNO_TO_ERROR(EINVAL);
510 if (!--len) /* length 1 is always OK */
512 if (conf.base_dir_arg[len] != '/')
513 break; /* no trailing slash, also OK */
514 conf.base_dir_arg[len] = '\0';
519 admissible_uids = adu_malloc(conf.uid_given * sizeof(*admissible_uids));
520 for (i = 0; i < conf.uid_given; i++) {
521 ret = parse_uid_range(conf.uid_arg[i], admissible_uids + i);
527 free(admissible_uids);
528 admissible_uids = NULL;
532 int main(int argc, char **argv)
535 struct cmdline_parser_params params = {
539 .check_ambiguity = 1,
543 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
547 ret = init_signals();
551 if (conf.select_given)
558 free(admissible_uids);
560 ERROR_LOG("%s\n", adu_strerror(-ret));
561 return -EXIT_FAILURE;