2 #include <dirent.h> /* readdir() */
5 #include "gcc-compat.h"
10 #include "portable_io.h"
11 #include "select.cmdline.h"
16 /** In case a signal is received, its number is stored here. */
19 /** Command line and config file options. */
20 struct gengetopt_args_info conf;
22 /** Options passed to --select-options. */
23 struct select_args_info select_conf;
25 /** The number of different uids found so far. */
26 uint32_t num_uids = 0;
28 /** This is always a power of two. It is set in create_hash_table(). */
29 static uint32_t uid_hash_table_size;
32 * Contains info for each user that owns at least one regular file.
34 * Even users that are not taken into account because of the --uid
35 * option occupy a slot in this hash table. This allows to find out
36 * quicky whether a uid is admissible. And yes, this has to be fast.
38 static struct user_info *uid_hash_table;
40 static inline int ui_used(struct user_info *ui)
42 return ui->flags & UI_FL_SLOT_USED;
45 static inline int ui_admissible(struct user_info *ui)
47 return ui->flags & UI_FL_ADMISSIBLE;
51 * The table containing the directory names and statistics.
53 struct osl_table *dir_table = NULL;
56 * Compare the size of two directories
58 * \param obj1 Pointer to the first object.
59 * \param obj2 Pointer to the second object.
61 * This function first compares the size values as usual integers. If they compare as
62 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
63 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
65 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
67 uint64_t d1 = *(uint64_t *)obj1->data;
68 uint64_t d2 = *(uint64_t *)obj2->data;
69 int ret = NUM_COMPARE(d2, d1);
73 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
74 return NUM_COMPARE(obj2->data, obj1->data);
78 * Compare two osl objects pointing to unsigned integers of 64 bit size.
80 * \param obj1 Pointer to the first integer.
81 * \param obj2 Pointer to the second integer.
83 * \return The values required for an osl compare function.
85 * \sa osl_compare_func, osl_hash_compare().
87 static int uint64_compare(const struct osl_object *obj1,
88 const struct osl_object *obj2)
90 uint64_t d1 = read_u64((const char *)obj1->data);
91 uint64_t d2 = read_u64((const char *)obj2->data);
100 static struct osl_column_description dir_table_cols[] = {
102 .storage_type = OSL_MAPPED_STORAGE,
107 .storage_type = OSL_MAPPED_STORAGE,
108 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
110 .compare_function = uint64_compare,
111 .data_size = sizeof(uint64_t)
114 .storage_type = OSL_MAPPED_STORAGE,
115 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
116 .name = "parent_num",
117 .compare_function = size_compare,
118 .data_size = sizeof(uint64_t)
121 .storage_type = OSL_MAPPED_STORAGE,
122 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
123 .compare_function = size_compare,
125 .data_size = sizeof(uint64_t)
128 .storage_type = OSL_MAPPED_STORAGE,
129 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
130 .compare_function = size_compare,
132 .data_size = sizeof(uint64_t)
136 static struct osl_table_description dir_table_desc = {
138 .num_columns = NUM_DT_COLUMNS,
140 .column_descriptions = dir_table_cols,
143 static struct osl_column_description user_table_cols[] = {
145 .storage_type = OSL_MAPPED_STORAGE,
146 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
148 .compare_function = uint64_compare,
149 .data_size = sizeof(uint64_t)
152 .storage_type = OSL_MAPPED_STORAGE,
153 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
154 .compare_function = size_compare,
156 .data_size = sizeof(uint64_t)
159 .storage_type = OSL_MAPPED_STORAGE,
160 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
161 .compare_function = size_compare,
163 .data_size = sizeof(uint64_t)
170 * \param ll Loglevel.
171 * \param fml Usual format string.
173 * All XXX_LOG() macros use this function.
175 __printf_2_3 void __log(int ll, const char* fmt,...)
183 if (ll < conf.loglevel_arg)
188 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
189 fprintf(outfd, "%s ", str);
191 vfprintf(outfd, fmt, argp);
195 static int open_user_table(struct user_info *ui, int create)
200 ui->desc = adu_malloc(sizeof(*ui->desc));
201 ui->desc->num_columns = NUM_UT_COLUMNS;
203 ui->desc->column_descriptions = user_table_cols;
204 ui->desc->dir = adu_strdup(conf.database_dir_arg);
205 ui->desc->name = make_message("%u", (unsigned)ui->uid);
206 pw = getpwuid(ui->uid);
207 if (pw && pw->pw_name)
208 ui->pw_name = adu_strdup(pw->pw_name);
210 INFO_LOG(".............................uid #%u: %u\n",
211 (unsigned)num_uids, (unsigned)ui->uid);
213 ret = osl(osl_create_table(ui->desc));
218 ret = osl(osl_open_table(ui->desc, &ui->table));
223 free((char *)ui->desc->name);
224 free((char *)ui->desc->dir);
227 ui->desc->name = NULL;
228 ui->desc->dir = NULL;
235 int for_each_admissible_user(int (*func)(struct user_info *, void *),
238 struct user_info *ui = uid_hash_table;
241 return -ERRNO_TO_ERROR(EFAULT);
243 for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
246 if (!ui_used(ui) || !ui_admissible(ui))
248 ret = func(ui, data);
255 #define PRIME1 0xb11924e1
256 #define PRIME2 0x01000193
258 void create_hash_table(unsigned bits)
260 uid_hash_table_size = 1 << bits;
261 uid_hash_table = adu_calloc(uid_hash_table_size *
262 sizeof(struct user_info));
265 static void free_hash_table(void)
267 free(uid_hash_table);
268 uid_hash_table = NULL;
271 static void close_dir_table(void)
277 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
279 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
280 free((char *)dir_table_desc.dir);
284 static int close_user_table(struct user_info *ui, __a_unused void *data)
288 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
290 ERROR_LOG("failed to close user table %u: %s\n",
291 (unsigned) ui->uid, adu_strerror(-ret));
292 free((char *)ui->desc->name);
293 ui->desc->name = NULL;
294 free((char *)ui->desc->dir);
295 ui->desc->dir = NULL;
305 static void close_user_tables(void)
307 for_each_admissible_user(close_user_table, NULL);
310 void close_all_tables(void)
317 static void signal_handler(int s)
322 void check_signals(void)
326 EMERG_LOG("caught signal %d\n", signum);
331 static int init_signals(void)
333 if (signal(SIGINT, &signal_handler) == SIG_ERR)
334 return -E_SIGNAL_SIG_ERR;
335 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
336 return -E_SIGNAL_SIG_ERR;
337 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
338 return -E_SIGNAL_SIG_ERR;
343 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
344 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
345 * unused slots in the table are used to store different uids that hash to the
348 * If a hash collision occurs, different slots are successively probed in order
349 * to find an unused slot for the new uid. Probing is implemented via a second
350 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
353 * An odd number is sufficient to make sure each entry of the hash table gets
354 * probed for probe_num between 0 and s-1 because s is a power of two, hence
355 * the second hash value has never a common divisor with the hash table size.
356 * IOW: h is invertible in the ring [0..s].
358 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
360 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
361 % uid_hash_table_size;
364 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
366 struct uid_range *ur;
369 if (!urs) /* empty array means all uids are allowed */
371 FOR_EACH_UID_RANGE(ur, urs)
372 if (ur->low <= uid && ur->high >= uid)
376 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
381 int search_uid(uint32_t uid, struct uid_range *urs,
382 enum search_uid_flags flags, struct user_info **ui_ptr)
386 for (p = 0; p < uid_hash_table_size; p++) {
387 struct user_info *ui = uid_hash_table + double_hash(uid, p);
394 ui->flags |= UI_FL_SLOT_USED;
395 if (!uid_is_admissible(uid, urs))
397 ui->flags |= UI_FL_ADMISSIBLE;
398 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
412 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
415 char *get_uid_list_name(void)
417 return make_message("%s/uid_list", conf.database_dir_arg);
420 void sort_hash_table(int (*comp)(const void *, const void *))
422 qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
426 int open_dir_table(int create)
428 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
431 int ret = osl(osl_create_table(&dir_table_desc));
433 free((char *)dir_table_desc.dir);
437 return osl(osl_open_table(&dir_table_desc, &dir_table));
440 static int check_args(void)
442 if (conf.create_given && !conf.base_dir_given)
445 /* remove trailing slashes from base-dir arg */
446 if (conf.base_dir_given) {
447 size_t len = strlen(conf.base_dir_arg);
449 if (!len) /* empty string */
450 return -ERRNO_TO_ERROR(EINVAL);
451 if (!--len) /* length 1 is always OK */
453 if (conf.base_dir_arg[len] != '/')
454 break; /* no trailing slash, also OK */
455 conf.base_dir_arg[len] = '\0';
461 int main(int argc, char **argv)
464 struct cmdline_parser_params params = {
468 .check_ambiguity = 1,
472 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
476 ret = init_signals();
480 if (conf.select_given)
482 else if (conf.create_given)
485 ret = com_interactive();
490 ERROR_LOG("%s\n", adu_strerror(-ret));
491 return -EXIT_FAILURE;