]> git.tuebingen.mpg.de Git - adu.git/blob - adu.c
Make the array of summary comparators local.
[adu.git] / adu.c
1 #include "adu.h"
2 #include <dirent.h> /* readdir() */
3 #include <pwd.h>
4
5 #include "gcc-compat.h"
6 #include "cmdline.h"
7 #include "fd.h"
8 #include "string.h"
9 #include "error.h"
10 #include "portable_io.h"
11
12 DEFINE_ERRLIST;
13 int osl_errno;
14
15 /** In case a signal is received, its number is stored here. */
16 static int signum;
17
18 /** Command line and config file options. */
19 struct gengetopt_args_info conf;
20
21 /** Options passed to --select-options. */
22 struct select_args_info select_conf;
23
24 /** The number of different uids found so far. */
25 uint32_t num_uids = 0;
26
27 /** This is always a power of two. It is set in create_hash_table(). */
28 static uint32_t uid_hash_table_size;
29
30 /**
31  * Contains info for each user that owns at least one regular file.
32  *
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.
36  */
37 static struct user_info *uid_hash_table;
38
39 static inline int ui_used(struct user_info *ui)
40 {
41         return ui->flags & UI_FL_SLOT_USED;
42 }
43
44 static inline int ui_admissible(struct user_info *ui)
45 {
46         return ui->flags & UI_FL_ADMISSIBLE;
47 }
48
49 /**
50  * The table containing the directory names and statistics.
51  */
52 struct osl_table *dir_table = NULL;
53
54 /**
55  * Compare the size of two directories
56  *
57  * \param obj1 Pointer to the first object.
58  * \param obj2 Pointer to the second object.
59  *
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.
63  */
64 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
65 {
66         uint64_t d1 = *(uint64_t *)obj1->data;
67         uint64_t d2 = *(uint64_t *)obj2->data;
68         int ret = NUM_COMPARE(d2, d1);
69
70         if (ret)
71                 return ret;
72         //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
73         return NUM_COMPARE(obj2->data, obj1->data);
74 }
75
76 /**
77  * Compare two osl objects pointing to unsigned integers of 64 bit size.
78  *
79  * \param obj1 Pointer to the first integer.
80  * \param obj2 Pointer to the second integer.
81  *
82  * \return The values required for an osl compare function.
83  *
84  * \sa osl_compare_func, osl_hash_compare().
85  */
86 static int uint64_compare(const struct osl_object *obj1,
87                 const struct osl_object *obj2)
88 {
89         uint64_t d1 = read_u64((const char *)obj1->data);
90         uint64_t d2 = read_u64((const char *)obj2->data);
91
92         if (d1 < d2)
93                 return 1;
94         if (d1 > d2)
95                 return -1;
96         return 0;
97 }
98
99 static struct osl_column_description dir_table_cols[] = {
100         [DT_NAME] = {
101                 .storage_type = OSL_MAPPED_STORAGE,
102                 .storage_flags = 0,
103                 .name = "dir",
104         },
105         [DT_NUM] = {
106                 .storage_type = OSL_MAPPED_STORAGE,
107                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
108                 .name = "num",
109                 .compare_function = uint64_compare,
110                 .data_size = sizeof(uint64_t)
111         },
112         [DT_PARENT_NUM] = {
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)
118         },
119         [DT_BYTES] = {
120                 .storage_type = OSL_MAPPED_STORAGE,
121                 .storage_flags =  OSL_RBTREE | OSL_FIXED_SIZE,
122                 .compare_function = size_compare,
123                 .name = "num_bytes",
124                 .data_size = sizeof(uint64_t)
125         },
126         [DT_FILES] = {
127                 .storage_type = OSL_MAPPED_STORAGE,
128                 .storage_flags =  OSL_RBTREE | OSL_FIXED_SIZE,
129                 .compare_function = size_compare,
130                 .name = "num_files",
131                 .data_size = sizeof(uint64_t)
132         }
133 };
134
135 static struct osl_table_description dir_table_desc = {
136         .name = "dir_table",
137         .num_columns = NUM_DT_COLUMNS,
138         .flags = 0,
139         .column_descriptions = dir_table_cols,
140 };
141
142 /*
143  * The columns of the per-user tables.
144  *
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
149  * user.
150  */
151 static struct osl_column_description user_table_cols[] = {
152         [UT_DIR_NUM] = {
153                 .storage_type = OSL_MAPPED_STORAGE,
154                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
155                 .name = "dir_num",
156                 .compare_function = uint64_compare,
157                 .data_size = sizeof(uint64_t)
158         },
159         [UT_BYTES] = {
160                 .storage_type = OSL_MAPPED_STORAGE,
161                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
162                 .compare_function = size_compare,
163                 .name = "num_bytes",
164                 .data_size = sizeof(uint64_t)
165         },
166         [UT_FILES] = {
167                 .storage_type = OSL_MAPPED_STORAGE,
168                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
169                 .compare_function = size_compare,
170                 .name = "num_files",
171                 .data_size = sizeof(uint64_t)
172         },
173 };
174
175 /**
176  * The log function.
177  *
178  * \param ll Loglevel.
179  * \param fml Usual format string.
180  *
181  * All XXX_LOG() macros use this function.
182  */
183 __printf_2_3 void __log(int ll, const char* fmt,...)
184 {
185         va_list argp;
186         FILE *outfd;
187         struct tm *tm;
188         time_t t1;
189         char str[255] = "";
190
191         if (ll < conf.loglevel_arg)
192                 return;
193         outfd = stderr;
194         time(&t1);
195         tm = localtime(&t1);
196         strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
197         fprintf(outfd, "%s ", str);
198         va_start(argp, fmt);
199         vfprintf(outfd, fmt, argp);
200         va_end(argp);
201 }
202
203 static int open_user_table(struct user_info *ui, int create)
204 {
205         int ret;
206         struct passwd *pw;
207
208         ui->desc = adu_malloc(sizeof(*ui->desc));
209         ui->desc->num_columns = NUM_UT_COLUMNS;
210         ui->desc->flags = 0;
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);
217
218         INFO_LOG(".............................uid #%u: %u\n",
219                 (unsigned)num_uids, (unsigned)ui->uid);
220         if (create) {
221                 ret = osl(osl_create_table(ui->desc));
222                 if (ret < 0)
223                         goto err;
224                 num_uids++;
225         }
226         ret = osl(osl_open_table(ui->desc, &ui->table));
227         if (ret < 0)
228                 goto err;
229         return 1;
230 err:
231         free((char *)ui->desc->name);
232         free((char *)ui->desc->dir);
233         free(ui->pw_name);
234         free(ui->desc);
235         ui->desc->name = NULL;
236         ui->desc->dir = NULL;
237         ui->desc = NULL;
238         ui->table = NULL;
239         ui->flags = 0;
240         return ret;
241 }
242
243 int for_each_admissible_user(int (*func)(struct user_info *, void *),
244                 void *data)
245 {
246         struct user_info *ui = uid_hash_table;
247
248         if (!ui)
249                 return -ERRNO_TO_ERROR(EFAULT);
250
251         for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
252                 int ret;
253
254                 if (!ui_used(ui) || !ui_admissible(ui))
255                         continue;
256                 ret = func(ui, data);
257                 if (ret < 0)
258                         return ret;
259         }
260         return 1;
261 }
262
263 #define PRIME1 0xb11924e1
264 #define PRIME2 0x01000193
265
266 void create_hash_table(unsigned bits)
267 {
268         uid_hash_table_size = 1 << bits;
269         uid_hash_table = adu_calloc(uid_hash_table_size *
270                 sizeof(struct user_info));
271 }
272
273 static void free_hash_table(void)
274 {
275         free(uid_hash_table);
276         uid_hash_table = NULL;
277 }
278
279 static void close_dir_table(void)
280 {
281         int ret;
282
283         if (!dir_table)
284                 return;
285         ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
286         if (ret < 0)
287                 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
288         free((char *)dir_table_desc.dir);
289         dir_table = NULL;
290 }
291
292 static int close_user_table(struct user_info *ui, __a_unused void *data)
293 {
294         int ret;
295
296         ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
297         if (ret < 0)
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;
304         free(ui->pw_name);
305         ui->pw_name = NULL;
306         free(ui->desc);
307         ui->desc = NULL;
308         ui->table = NULL;
309         ui->flags = 0;
310         return 1;
311 }
312
313 static void close_user_tables(void)
314 {
315         for_each_admissible_user(close_user_table, NULL);
316 }
317
318 void close_all_tables(void)
319 {
320         close_dir_table();
321         close_user_tables();
322         free_hash_table();
323 }
324
325 static void signal_handler(int s)
326 {
327         signum = s;
328 }
329
330 void check_signals(void)
331 {
332         if (likely(!signum))
333                 return;
334         EMERG_LOG("caught signal %d\n", signum);
335         close_all_tables();
336         exit(EXIT_FAILURE);
337 }
338
339 static int init_signals(void)
340 {
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;
347         return 1;
348 }
349
350 /*
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
354  * same slot.
355  *
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
359  * odd number.
360  *
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].
365  */
366 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
367 {
368         return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
369                 % uid_hash_table_size;
370 }
371
372 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
373 {
374         struct uid_range *ur;
375         int ret = 1;
376
377         if (!urs) /* empty array means all uids are allowed */
378                 return 1;
379         FOR_EACH_UID_RANGE(ur, urs)
380                 if (ur->low <= uid && ur->high >= uid)
381                         goto out;
382         ret = 0;
383 out:
384         DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
385                 ret? "" : "not ");
386         return ret;
387 }
388
389 int search_uid(uint32_t uid, struct uid_range *urs,
390                 enum search_uid_flags flags, struct user_info **ui_ptr)
391 {
392         uint32_t p;
393
394         for (p = 0; p < uid_hash_table_size; p++) {
395                 struct user_info *ui = uid_hash_table + double_hash(uid, p);
396
397                 if (!ui_used(ui)) {
398                         int ret;
399                         if (!flags)
400                                 return -E_BAD_UID;
401                         ui->uid = uid;
402                         ui->flags |= UI_FL_SLOT_USED;
403                         if (!uid_is_admissible(uid, urs))
404                                 return 0;
405                         ui->flags |= UI_FL_ADMISSIBLE;
406                         ret = open_user_table(ui, flags & CREATE_USER_TABLE);
407                         if (ret < 0)
408                                 return ret;
409
410                         if (ui_ptr)
411                                 *ui_ptr = ui;
412                         return 1;
413                 }
414                 if (ui->uid != uid)
415                         continue;
416                 if (ui_ptr)
417                         *ui_ptr = ui;
418                 return 0;
419         }
420         return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
421 }
422
423 char *get_uid_list_name(void)
424 {
425         return make_message("%s/uid_list", conf.database_dir_arg);
426 }
427
428 void sort_hash_table(int (*comp)(const void *, const void *))
429 {
430         qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
431                 comp);
432 }
433
434 int open_dir_table(int create)
435 {
436         dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
437
438         if (create) {
439                 int ret = osl(osl_create_table(&dir_table_desc));
440                 if (ret < 0) {
441                         free((char *)dir_table_desc.dir);
442                         return ret;
443                 }
444         }
445         return osl(osl_open_table(&dir_table_desc, &dir_table));
446 }
447
448 static int check_args(void)
449 {
450         if (conf.create_given && !conf.base_dir_given)
451                 return -E_SYNTAX;
452
453         /* remove trailing slashes from base-dir arg */
454         if (conf.base_dir_given) {
455                 size_t len = strlen(conf.base_dir_arg);
456                 for (;;) {
457                         if (!len) /* empty string */
458                                 return -ERRNO_TO_ERROR(EINVAL);
459                         if (!--len) /* length 1 is always OK */
460                                 break;
461                         if (conf.base_dir_arg[len] != '/')
462                                 break; /* no trailing slash, also OK */
463                         conf.base_dir_arg[len] = '\0';
464                 }
465         }
466         return 1;
467 }
468
469 int main(int argc, char **argv)
470 {
471         int ret;
472         struct cmdline_parser_params params = {
473                 .override = 0,
474                 .initialize = 1,
475                 .check_required = 1,
476                 .check_ambiguity = 1,
477                 .print_errors = 1
478         };
479
480         cmdline_parser_ext(argc, argv, &conf, &params); /* aborts on errors */
481         ret = check_args();
482         if (ret < 0)
483                 goto out;
484         ret = init_signals();
485         if (ret < 0)
486                 goto out;
487         ret = -E_SYNTAX;
488         if (conf.select_given)
489                 ret = com_select();
490         else if (conf.create_given)
491                 ret = com_create();
492         else
493                 ret = com_interactive();
494         if (ret < 0)
495                 goto out;
496 out:
497         if (ret < 0) {
498                 ERROR_LOG("%s\n", adu_strerror(-ret));
499                 return -EXIT_FAILURE;
500         }
501         return EXIT_SUCCESS;
502 }