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,
143 * The columns of the per-user tables.
145 * Adu tracks disk usage on a per-user basis. For each user, a user table is
146 * being created. The rows of the user table have three columns: The directory
147 * number that may be resolved to the path using the directory table, the
148 * number of bytes and the number of files in that directory owned by the given
151 static struct osl_column_description user_table_cols[] = {
153 .storage_type = OSL_MAPPED_STORAGE,
154 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
156 .compare_function = uint64_compare,
157 .data_size = sizeof(uint64_t)
160 .storage_type = OSL_MAPPED_STORAGE,
161 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
162 .compare_function = size_compare,
164 .data_size = sizeof(uint64_t)
167 .storage_type = OSL_MAPPED_STORAGE,
168 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
169 .compare_function = size_compare,
171 .data_size = sizeof(uint64_t)
178 * \param ll Loglevel.
179 * \param fml Usual format string.
181 * All XXX_LOG() macros use this function.
183 __printf_2_3 void __log(int ll, const char* fmt,...)
191 if (ll < conf.loglevel_arg)
196 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
197 fprintf(outfd, "%s ", str);
199 vfprintf(outfd, fmt, argp);
203 static int open_user_table(struct user_info *ui, int create)
208 ui->desc = adu_malloc(sizeof(*ui->desc));
209 ui->desc->num_columns = NUM_UT_COLUMNS;
211 ui->desc->column_descriptions = user_table_cols;
212 ui->desc->dir = adu_strdup(conf.database_dir_arg);
213 ui->desc->name = make_message("%u", (unsigned)ui->uid);
214 pw = getpwuid(ui->uid);
215 if (pw && pw->pw_name)
216 ui->pw_name = adu_strdup(pw->pw_name);
218 INFO_LOG(".............................uid #%u: %u\n",
219 (unsigned)num_uids, (unsigned)ui->uid);
221 ret = osl(osl_create_table(ui->desc));
226 ret = osl(osl_open_table(ui->desc, &ui->table));
231 free((char *)ui->desc->name);
232 free((char *)ui->desc->dir);
235 ui->desc->name = NULL;
236 ui->desc->dir = NULL;
243 int for_each_admissible_user(int (*func)(struct user_info *, void *),
246 struct user_info *ui = uid_hash_table;
249 return -ERRNO_TO_ERROR(EFAULT);
251 for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
254 if (!ui_used(ui) || !ui_admissible(ui))
256 ret = func(ui, data);
263 #define PRIME1 0xb11924e1
264 #define PRIME2 0x01000193
266 void create_hash_table(unsigned bits)
268 uid_hash_table_size = 1 << bits;
269 uid_hash_table = adu_calloc(uid_hash_table_size *
270 sizeof(struct user_info));
273 static void free_hash_table(void)
275 free(uid_hash_table);
276 uid_hash_table = NULL;
279 static void close_dir_table(void)
285 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
287 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
288 free((char *)dir_table_desc.dir);
292 static int close_user_table(struct user_info *ui, __a_unused void *data)
296 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
298 ERROR_LOG("failed to close user table %u: %s\n",
299 (unsigned) ui->uid, adu_strerror(-ret));
300 free((char *)ui->desc->name);
301 ui->desc->name = NULL;
302 free((char *)ui->desc->dir);
303 ui->desc->dir = NULL;
313 static void close_user_tables(void)
315 for_each_admissible_user(close_user_table, NULL);
318 void close_all_tables(void)
325 static void signal_handler(int s)
330 void check_signals(void)
334 EMERG_LOG("caught signal %d\n", signum);
339 static int init_signals(void)
341 if (signal(SIGINT, &signal_handler) == SIG_ERR)
342 return -E_SIGNAL_SIG_ERR;
343 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
344 return -E_SIGNAL_SIG_ERR;
345 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
346 return -E_SIGNAL_SIG_ERR;
351 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
352 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
353 * unused slots in the table are used to store different uids that hash to the
356 * If a hash collision occurs, different slots are successively probed in order
357 * to find an unused slot for the new uid. Probing is implemented via a second
358 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
361 * An odd number is sufficient to make sure each entry of the hash table gets
362 * probed for probe_num between 0 and s-1 because s is a power of two, hence
363 * the second hash value has never a common divisor with the hash table size.
364 * IOW: h is invertible in the ring [0..s].
366 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
368 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
369 % uid_hash_table_size;
372 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
374 struct uid_range *ur;
377 if (!urs) /* empty array means all uids are allowed */
379 FOR_EACH_UID_RANGE(ur, urs)
380 if (ur->low <= uid && ur->high >= uid)
384 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
389 int search_uid(uint32_t uid, struct uid_range *urs,
390 enum search_uid_flags flags, struct user_info **ui_ptr)
394 for (p = 0; p < uid_hash_table_size; p++) {
395 struct user_info *ui = uid_hash_table + double_hash(uid, p);
402 ui->flags |= UI_FL_SLOT_USED;
403 if (!uid_is_admissible(uid, urs))
405 ui->flags |= UI_FL_ADMISSIBLE;
406 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
420 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
423 char *get_uid_list_name(void)
425 return make_message("%s/uid_list", conf.database_dir_arg);
428 void sort_hash_table(int (*comp)(const void *, const void *))
430 qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
434 int open_dir_table(int create)
436 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
439 int ret = osl(osl_create_table(&dir_table_desc));
441 free((char *)dir_table_desc.dir);
445 return osl(osl_open_table(&dir_table_desc, &dir_table));
448 static int check_args(void)
450 if (conf.create_given && !conf.base_dir_given)
453 /* remove trailing slashes from base-dir arg */
454 if (conf.base_dir_given) {
455 size_t len = strlen(conf.base_dir_arg);
457 if (!len) /* empty string */
458 return -ERRNO_TO_ERROR(EINVAL);
459 if (!--len) /* length 1 is always OK */
461 if (conf.base_dir_arg[len] != '/')
462 break; /* no trailing slash, also OK */
463 conf.base_dir_arg[len] = '\0';
469 int main(int argc, char **argv)
472 struct cmdline_parser_params params = {
476 .check_ambiguity = 1,
480 cmdline_parser_ext(argc, argv, &conf, ¶ms); /* aborts on errors */
484 ret = init_signals();
488 if (conf.select_given)
490 else if (conf.create_given)
493 ret = com_interactive();
498 ERROR_LOG("%s\n", adu_strerror(-ret));
499 return -EXIT_FAILURE;