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 /** Options passed to --select-options. */
22 struct select_args_info select_conf;
24 /** The number of different uids found so far. */
25 uint32_t num_uids = 0;
27 /** This is always a power of two. It is set in create_hash_table(). */
28 static uint32_t uid_hash_table_size;
31 * Contains info for each user that owns at least one regular file.
33 * Even users that are not taken into account because of the --uid
34 * option occupy a slot in this hash table. This allows to find out
35 * quicky whether a uid is admissible. And yes, this has to be fast.
37 static struct user_info *uid_hash_table;
39 static inline int ui_used(struct user_info *ui)
41 return ui->flags & UI_FL_SLOT_USED;
44 static inline int ui_admissible(struct user_info *ui)
46 return ui->flags & UI_FL_ADMISSIBLE;
50 * The table containing the directory names and statistics.
52 struct osl_table *dir_table = NULL;
55 * Compare the size of two directories
57 * \param obj1 Pointer to the first object.
58 * \param obj2 Pointer to the second object.
60 * This function first compares the size values as usual integers. If they compare as
61 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
62 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
64 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
66 uint64_t d1 = *(uint64_t *)obj1->data;
67 uint64_t d2 = *(uint64_t *)obj2->data;
68 int ret = NUM_COMPARE(d2, d1);
72 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
73 return NUM_COMPARE(obj2->data, obj1->data);
77 * Compare two osl objects pointing to unsigned integers of 64 bit size.
79 * \param obj1 Pointer to the first integer.
80 * \param obj2 Pointer to the second integer.
82 * \return The values required for an osl compare function.
84 * \sa osl_compare_func, osl_hash_compare().
86 static int uint64_compare(const struct osl_object *obj1,
87 const struct osl_object *obj2)
89 uint64_t d1 = read_u64((const char *)obj1->data);
90 uint64_t d2 = read_u64((const char *)obj2->data);
99 static struct osl_column_description dir_table_cols[] = {
101 .storage_type = OSL_MAPPED_STORAGE,
106 .storage_type = OSL_MAPPED_STORAGE,
107 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
109 .compare_function = uint64_compare,
110 .data_size = sizeof(uint64_t)
113 .storage_type = OSL_MAPPED_STORAGE,
114 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
115 .name = "parent_num",
116 .compare_function = size_compare,
117 .data_size = sizeof(uint64_t)
120 .storage_type = OSL_MAPPED_STORAGE,
121 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
122 .compare_function = size_compare,
124 .data_size = sizeof(uint64_t)
127 .storage_type = OSL_MAPPED_STORAGE,
128 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
129 .compare_function = size_compare,
131 .data_size = sizeof(uint64_t)
135 static struct osl_table_description dir_table_desc = {
137 .num_columns = NUM_DT_COLUMNS,
139 .column_descriptions = dir_table_cols,
142 static struct osl_column_description user_table_cols[] = {
144 .storage_type = OSL_MAPPED_STORAGE,
145 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
147 .compare_function = uint64_compare,
148 .data_size = sizeof(uint64_t)
151 .storage_type = OSL_MAPPED_STORAGE,
152 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
153 .compare_function = size_compare,
155 .data_size = sizeof(uint64_t)
158 .storage_type = OSL_MAPPED_STORAGE,
159 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
160 .compare_function = size_compare,
162 .data_size = sizeof(uint64_t)
169 * \param ll Loglevel.
170 * \param fml Usual format string.
172 * All XXX_LOG() macros use this function.
174 __printf_2_3 void __log(int ll, const char* fmt,...)
182 if (ll < conf.loglevel_arg)
187 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
188 fprintf(outfd, "%s ", str);
190 vfprintf(outfd, fmt, argp);
194 static int open_user_table(struct user_info *ui, int create)
199 ui->desc = adu_malloc(sizeof(*ui->desc));
200 ui->desc->num_columns = NUM_UT_COLUMNS;
202 ui->desc->column_descriptions = user_table_cols;
203 ui->desc->dir = adu_strdup(conf.database_dir_arg);
204 ui->desc->name = make_message("%u", (unsigned)ui->uid);
205 pw = getpwuid(ui->uid);
206 if (pw && pw->pw_name)
207 ui->pw_name = adu_strdup(pw->pw_name);
209 INFO_LOG(".............................uid #%u: %u\n",
210 (unsigned)num_uids, (unsigned)ui->uid);
212 ret = osl(osl_create_table(ui->desc));
217 ret = osl(osl_open_table(ui->desc, &ui->table));
222 free((char *)ui->desc->name);
223 free((char *)ui->desc->dir);
226 ui->desc->name = NULL;
227 ui->desc->dir = NULL;
234 int for_each_admissible_user(int (*func)(struct user_info *, void *),
237 struct user_info *ui = uid_hash_table;
240 return -ERRNO_TO_ERROR(EFAULT);
242 for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
245 if (!ui_used(ui) || !ui_admissible(ui))
247 ret = func(ui, data);
254 #define PRIME1 0xb11924e1
255 #define PRIME2 0x01000193
257 void create_hash_table(unsigned bits)
259 uid_hash_table_size = 1 << bits;
260 uid_hash_table = adu_calloc(uid_hash_table_size *
261 sizeof(struct user_info));
264 static void free_hash_table(void)
266 free(uid_hash_table);
267 uid_hash_table = NULL;
270 static void close_dir_table(void)
276 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
278 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
279 free((char *)dir_table_desc.dir);
283 static int close_user_table(struct user_info *ui, __a_unused void *data)
287 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
289 ERROR_LOG("failed to close user table %u: %s\n",
290 (unsigned) ui->uid, adu_strerror(-ret));
291 free((char *)ui->desc->name);
292 ui->desc->name = NULL;
293 free((char *)ui->desc->dir);
294 ui->desc->dir = NULL;
304 static void close_user_tables(void)
306 for_each_admissible_user(close_user_table, NULL);
309 void close_all_tables(void)
316 static void signal_handler(int s)
321 void check_signals(void)
325 EMERG_LOG("caught signal %d\n", signum);
330 static int init_signals(void)
332 if (signal(SIGINT, &signal_handler) == SIG_ERR)
333 return -E_SIGNAL_SIG_ERR;
334 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
335 return -E_SIGNAL_SIG_ERR;
336 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
337 return -E_SIGNAL_SIG_ERR;
342 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
343 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
344 * unused slots in the table are used to store different uids that hash to the
347 * If a hash collision occurs, different slots are successively probed in order
348 * to find an unused slot for the new uid. Probing is implemented via a second
349 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
352 * An odd number is sufficient to make sure each entry of the hash table gets
353 * probed for probe_num between 0 and s-1 because s is a power of two, hence
354 * the second hash value has never a common divisor with the hash table size.
355 * IOW: h is invertible in the ring [0..s].
357 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
359 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
360 % uid_hash_table_size;
363 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
365 struct uid_range *ur;
368 if (!urs) /* empty array means all uids are allowed */
370 FOR_EACH_UID_RANGE(ur, urs)
371 if (ur->low <= uid && ur->high >= uid)
375 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
380 int search_uid(uint32_t uid, struct uid_range *urs,
381 enum search_uid_flags flags, struct user_info **ui_ptr)
385 for (p = 0; p < uid_hash_table_size; p++) {
386 struct user_info *ui = uid_hash_table + double_hash(uid, p);
393 ui->flags |= UI_FL_SLOT_USED;
394 if (!uid_is_admissible(uid, urs))
396 ui->flags |= UI_FL_ADMISSIBLE;
397 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
411 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
414 char *get_uid_list_name(void)
416 return make_message("%s/uid_list", conf.database_dir_arg);
419 void sort_hash_table(int (*comp)(const void *, const void *))
421 qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
425 int open_dir_table(int create)
427 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
430 int ret = osl(osl_create_table(&dir_table_desc));
432 free((char *)dir_table_desc.dir);
436 return osl(osl_open_table(&dir_table_desc, &dir_table));
439 static int check_args(void)
441 if (conf.create_given && !conf.base_dir_given)
444 /* remove trailing slashes from base-dir arg */
445 if (conf.base_dir_given) {
446 size_t len = strlen(conf.base_dir_arg);
448 if (!len) /* empty string */
449 return -ERRNO_TO_ERROR(EINVAL);
450 if (!--len) /* length 1 is always OK */
452 if (conf.base_dir_arg[len] != '/')
453 break; /* no trailing slash, also OK */
454 conf.base_dir_arg[len] = '\0';
460 int main(int argc, char **argv)
463 struct cmdline_parser_params params = {
467 .check_ambiguity = 1,
471 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
475 ret = init_signals();
479 if (conf.select_given)
481 else if (conf.create_given)
484 ret = com_interactive();
489 ERROR_LOG("%s\n", adu_strerror(-ret));
490 return -EXIT_FAILURE;