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)
172 * \param ll Loglevel.
173 * \param fml Usual format string.
175 * All XXX_LOG() macros use this function.
177 __printf_2_3 void __log(int ll, const char* fmt,...)
185 if (ll < conf.loglevel_arg)
190 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
191 fprintf(outfd, "%s ", str);
193 vfprintf(outfd, fmt, argp);
197 static int open_user_table(struct user_info *ui, int create)
202 ui->desc = adu_malloc(sizeof(*ui->desc));
203 ui->desc->num_columns = NUM_UT_COLUMNS;
205 ui->desc->column_descriptions = user_table_cols;
206 ui->desc->dir = adu_strdup(conf.database_dir_arg);
207 ui->desc->name = make_message("%u", (unsigned)ui->uid);
208 pw = getpwuid(ui->uid);
209 if (pw && pw->pw_name)
210 ui->pw_name = adu_strdup(pw->pw_name);
212 INFO_LOG(".............................uid #%u: %u\n",
213 (unsigned)num_uids, (unsigned)ui->uid);
215 ret = osl(osl_create_table(ui->desc));
220 ret = osl(osl_open_table(ui->desc, &ui->table));
225 free((char *)ui->desc->name);
226 free((char *)ui->desc->dir);
229 ui->desc->name = NULL;
230 ui->desc->dir = NULL;
237 int for_each_admissible_user(int (*func)(struct user_info *, void *),
240 struct user_info *ui = uid_hash_table;
243 return -ERRNO_TO_ERROR(EFAULT);
245 for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
248 if (!ui_used(ui) || !ui_admissible(ui))
250 ret = func(ui, data);
257 #define PRIME1 0xb11924e1
258 #define PRIME2 0x01000193
260 void create_hash_table(unsigned bits)
262 uid_hash_table_size = 1 << bits;
263 uid_hash_table = adu_calloc(uid_hash_table_size *
264 sizeof(struct user_info));
267 static void free_hash_table(void)
269 free(uid_hash_table);
270 uid_hash_table = NULL;
273 static void close_dir_table(void)
279 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
281 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
282 free((char *)dir_table_desc.dir);
286 static int close_user_table(struct user_info *ui, __a_unused void *data)
290 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
292 ERROR_LOG("failed to close user table %u: %s\n",
293 (unsigned) ui->uid, adu_strerror(-ret));
294 free((char *)ui->desc->name);
295 ui->desc->name = NULL;
296 free((char *)ui->desc->dir);
297 ui->desc->dir = NULL;
307 static void close_user_tables(void)
309 for_each_admissible_user(close_user_table, NULL);
312 void close_all_tables(void)
319 static void signal_handler(int s)
324 void check_signals(void)
328 EMERG_LOG("caught signal %d\n", signum);
333 static int init_signals(void)
335 if (signal(SIGINT, &signal_handler) == SIG_ERR)
336 return -E_SIGNAL_SIG_ERR;
337 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
338 return -E_SIGNAL_SIG_ERR;
339 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
340 return -E_SIGNAL_SIG_ERR;
345 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
346 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
347 * unused slots in the table are used to store different uids that hash to the
350 * If a hash collision occurs, different slots are successively probed in order
351 * to find an unused slot for the new uid. Probing is implemented via a second
352 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
355 * An odd number is sufficient to make sure each entry of the hash table gets
356 * probed for probe_num between 0 and s-1 because s is a power of two, hence
357 * the second hash value has never a common divisor with the hash table size.
358 * IOW: h is invertible in the ring [0..s].
360 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
362 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
363 % uid_hash_table_size;
366 static int uid_is_admissible(uint32_t uid)
370 for (i = 0; i < conf.uid_given; i++) {
371 struct uid_range *ur = admissible_uids + i;
373 if (ur->low <= uid && ur->high >= uid)
376 i = !conf.uid_given || i < conf.uid_given;
377 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
382 int search_uid(uint32_t uid, enum search_uid_flags flags,
383 struct user_info **ui_ptr)
387 for (p = 0; p < uid_hash_table_size; p++) {
388 struct user_info *ui = uid_hash_table + double_hash(uid, p);
395 ui->flags |= UI_FL_SLOT_USED;
396 if (!uid_is_admissible(uid))
398 ui->flags |= UI_FL_ADMISSIBLE;
399 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
413 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
416 char *get_uid_list_name(void)
418 return make_message("%s/uid_list", conf.database_dir_arg);
421 void sort_hash_table(int (*comp)(const void *, const void *))
423 qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
427 int open_dir_table(int create)
429 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
432 int ret = osl(osl_create_table(&dir_table_desc));
434 free((char *)dir_table_desc.dir);
438 return osl(osl_open_table(&dir_table_desc, &dir_table));
441 static int check_args(void)
446 if (conf.create_given && !conf.base_dir_given)
449 /* remove trailing slashes from base-dir arg */
450 if (conf.base_dir_given) {
451 size_t len = strlen(conf.base_dir_arg);
453 if (!len) /* empty string */
454 return -ERRNO_TO_ERROR(EINVAL);
455 if (!--len) /* length 1 is always OK */
457 if (conf.base_dir_arg[len] != '/')
458 break; /* no trailing slash, also OK */
459 conf.base_dir_arg[len] = '\0';
464 admissible_uids = adu_malloc(conf.uid_given * sizeof(*admissible_uids));
465 for (i = 0; i < conf.uid_given; i++) {
466 ret = parse_uid_range(conf.uid_arg[i], admissible_uids + i);
472 free(admissible_uids);
473 admissible_uids = NULL;
477 int main(int argc, char **argv)
480 struct cmdline_parser_params params = {
484 .check_ambiguity = 1,
488 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
492 ret = init_signals();
496 if (conf.select_given)
498 else if (conf.create_given)
501 ret = com_interactive();
505 free(admissible_uids);
507 ERROR_LOG("%s\n", adu_strerror(-ret));
508 return -EXIT_FAILURE;