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 * Compare the size of two directories
54 * \param obj1 Pointer to the first object.
55 * \param obj2 Pointer to the second object.
57 * This function first compares the size values as usual integers. If they compare as
58 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
59 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
61 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
63 uint64_t d1 = *(uint64_t *)obj1->data;
64 uint64_t d2 = *(uint64_t *)obj2->data;
65 int ret = NUM_COMPARE(d2, d1);
69 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
70 return NUM_COMPARE(obj2->data, obj1->data);
74 * Compare two osl objects pointing to unsigned integers of 64 bit size.
76 * \param obj1 Pointer to the first integer.
77 * \param obj2 Pointer to the second integer.
79 * \return The values required for an osl compare function.
81 * \sa osl_compare_func, osl_hash_compare().
83 static int uint64_compare(const struct osl_object *obj1,
84 const struct osl_object *obj2)
86 uint64_t d1 = read_u64((const char *)obj1->data);
87 uint64_t d2 = read_u64((const char *)obj2->data);
96 static struct osl_column_description dir_table_cols[] = {
98 .storage_type = OSL_MAPPED_STORAGE,
103 .storage_type = OSL_MAPPED_STORAGE,
104 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
106 .compare_function = uint64_compare,
107 .data_size = sizeof(uint64_t)
110 .storage_type = OSL_MAPPED_STORAGE,
111 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
112 .name = "parent_num",
113 .compare_function = size_compare,
114 .data_size = sizeof(uint64_t)
117 .storage_type = OSL_MAPPED_STORAGE,
118 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
119 .compare_function = size_compare,
121 .data_size = sizeof(uint64_t)
124 .storage_type = OSL_MAPPED_STORAGE,
125 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
126 .compare_function = size_compare,
128 .data_size = sizeof(uint64_t)
132 static struct osl_table_description dir_table_desc = {
134 .num_columns = NUM_DT_COLUMNS,
136 .column_descriptions = dir_table_cols,
139 static struct osl_column_description user_table_cols[] = {
141 .storage_type = OSL_MAPPED_STORAGE,
142 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
144 .compare_function = uint64_compare,
145 .data_size = sizeof(uint64_t)
148 .storage_type = OSL_MAPPED_STORAGE,
149 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
150 .compare_function = size_compare,
152 .data_size = sizeof(uint64_t)
155 .storage_type = OSL_MAPPED_STORAGE,
156 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
157 .compare_function = size_compare,
159 .data_size = sizeof(uint64_t)
166 * \param ll Loglevel.
167 * \param fml Usual format string.
169 * All XXX_LOG() macros use this function.
171 __printf_2_3 void __log(int ll, const char* fmt,...)
179 if (ll < conf.loglevel_arg)
184 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
185 fprintf(outfd, "%s ", str);
187 vfprintf(outfd, fmt, argp);
191 static int open_user_table(struct user_info *ui, int create)
196 ui->desc = adu_malloc(sizeof(*ui->desc));
197 ui->desc->num_columns = NUM_UT_COLUMNS;
199 ui->desc->column_descriptions = user_table_cols;
200 ui->desc->dir = adu_strdup(conf.database_dir_arg);
201 ui->desc->name = make_message("%u", (unsigned)ui->uid);
202 pw = getpwuid(ui->uid);
203 if (pw && pw->pw_name)
204 ui->pw_name = adu_strdup(pw->pw_name);
206 INFO_LOG(".............................uid #%u: %u\n",
207 (unsigned)num_uids, (unsigned)ui->uid);
209 ret = osl(osl_create_table(ui->desc));
214 ret = osl(osl_open_table(ui->desc, &ui->table));
219 free((char *)ui->desc->name);
220 free((char *)ui->desc->dir);
223 ui->desc->name = NULL;
224 ui->desc->dir = NULL;
231 int for_each_admissible_user(int (*func)(struct user_info *, void *),
234 struct user_info *ui = uid_hash_table;
237 return -ERRNO_TO_ERROR(EFAULT);
239 for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
242 if (!ui_used(ui) || !ui_admissible(ui))
244 ret = func(ui, data);
251 #define PRIME1 0xb11924e1
252 #define PRIME2 0x01000193
254 void create_hash_table(unsigned bits)
256 uid_hash_table_size = 1 << bits;
257 uid_hash_table = adu_calloc(uid_hash_table_size *
258 sizeof(struct user_info));
261 static void free_hash_table(void)
263 free(uid_hash_table);
264 uid_hash_table = NULL;
267 static void close_dir_table(void)
273 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
275 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
276 free((char *)dir_table_desc.dir);
280 static int close_user_table(struct user_info *ui, __a_unused void *data)
284 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
286 ERROR_LOG("failed to close user table %u: %s\n",
287 (unsigned) ui->uid, adu_strerror(-ret));
288 free((char *)ui->desc->name);
289 ui->desc->name = NULL;
290 free((char *)ui->desc->dir);
291 ui->desc->dir = NULL;
301 static void close_user_tables(void)
303 for_each_admissible_user(close_user_table, NULL);
306 void close_all_tables(void)
313 static void signal_handler(int s)
318 void check_signals(void)
322 EMERG_LOG("caught signal %d\n", signum);
327 static int init_signals(void)
329 if (signal(SIGINT, &signal_handler) == SIG_ERR)
330 return -E_SIGNAL_SIG_ERR;
331 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
332 return -E_SIGNAL_SIG_ERR;
333 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
334 return -E_SIGNAL_SIG_ERR;
339 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
340 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
341 * unused slots in the table are used to store different uids that hash to the
344 * If a hash collision occurs, different slots are successively probed in order
345 * to find an unused slot for the new uid. Probing is implemented via a second
346 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
349 * An odd number is sufficient to make sure each entry of the hash table gets
350 * probed for probe_num between 0 and s-1 because s is a power of two, hence
351 * the second hash value has never a common divisor with the hash table size.
352 * IOW: h is invertible in the ring [0..s].
354 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
356 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
357 % uid_hash_table_size;
360 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
362 struct uid_range *ur;
365 if (!urs) /* empty array means all uids are allowed */
367 FOR_EACH_UID_RANGE(ur, urs)
368 if (ur->low <= uid && ur->high >= uid)
372 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
377 int search_uid(uint32_t uid, struct uid_range *urs,
378 enum search_uid_flags flags, struct user_info **ui_ptr)
382 for (p = 0; p < uid_hash_table_size; p++) {
383 struct user_info *ui = uid_hash_table + double_hash(uid, p);
390 ui->flags |= UI_FL_SLOT_USED;
391 if (!uid_is_admissible(uid, urs))
393 ui->flags |= UI_FL_ADMISSIBLE;
394 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
408 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
411 char *get_uid_list_name(void)
413 return make_message("%s/uid_list", conf.database_dir_arg);
416 void sort_hash_table(int (*comp)(const void *, const void *))
418 qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
422 int open_dir_table(int create)
424 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
427 int ret = osl(osl_create_table(&dir_table_desc));
429 free((char *)dir_table_desc.dir);
433 return osl(osl_open_table(&dir_table_desc, &dir_table));
436 static int check_args(void)
438 if (conf.create_given && !conf.base_dir_given)
441 /* remove trailing slashes from base-dir arg */
442 if (conf.base_dir_given) {
443 size_t len = strlen(conf.base_dir_arg);
445 if (!len) /* empty string */
446 return -ERRNO_TO_ERROR(EINVAL);
447 if (!--len) /* length 1 is always OK */
449 if (conf.base_dir_arg[len] != '/')
450 break; /* no trailing slash, also OK */
451 conf.base_dir_arg[len] = '\0';
457 int main(int argc, char **argv)
460 struct cmdline_parser_params params = {
464 .check_ambiguity = 1,
468 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
472 ret = init_signals();
476 if (conf.select_given)
478 else if (conf.create_given)
481 ret = com_interactive();
486 ERROR_LOG("%s\n", adu_strerror(-ret));
487 return -EXIT_FAILURE;