make clean: Remove more derived files.
[adu.git] / adu.c
1 #include "adu.h"
2 #include <dirent.h> /* readdir() */
3 #include <pwd.h>
4 #include "format.h"
5 #include "select.h"
6
7 #include "gcc-compat.h"
8 #include "cmdline.h"
9 #include "fd.h"
10 #include "string.h"
11 #include "error.h"
12 #include "portable_io.h"
13
14 DEFINE_ERRLIST;
15 int osl_errno;
16
17 /** In case a signal is received, its number is stored here. */
18 static int signum;
19
20 /** Command line and config file options. */
21 struct gengetopt_args_info conf;
22
23 /** Options passed to --select-options. */
24 struct select_args_info select_conf;
25
26 /** The number of different uids found so far. */
27 uint32_t num_uids = 0;
28
29 /** This is always a power of two. It is set in create_hash_table(). */
30 static uint32_t uid_hash_table_size;
31
32 /**
33  * Contains info for each user that owns at least one regular file.
34  *
35  * Even users that are not taken into account because of the --uid
36  * option occupy a slot in this hash table. This allows to find out
37  * quicky whether a uid is admissible. And yes, this has to be fast.
38  */
39 static struct user_info *uid_hash_table;
40
41 static inline int ui_used(struct user_info *ui)
42 {
43         return ui->flags & UI_FL_SLOT_USED;
44 }
45
46 static inline int ui_admissible(struct user_info *ui)
47 {
48         return ui->flags & UI_FL_ADMISSIBLE;
49 }
50
51 /**
52  * The table containing the directory names and statistics.
53  */
54 struct osl_table *dir_table = NULL;
55
56 /**
57  * Compare the size of two directories
58  *
59  * \param obj1 Pointer to the first object.
60  * \param obj2 Pointer to the second object.
61  *
62  * This function first compares the size values as usual integers. If they compare as
63  * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
64  * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
65  */
66 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
67 {
68         uint64_t d1 = *(uint64_t *)obj1->data;
69         uint64_t d2 = *(uint64_t *)obj2->data;
70         int ret = NUM_COMPARE(d2, d1);
71
72         if (ret)
73                 return ret;
74         //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
75         return NUM_COMPARE(obj2->data, obj1->data);
76 }
77
78 /**
79  * Compare two osl objects pointing to unsigned integers of 64 bit size.
80  *
81  * \param obj1 Pointer to the first integer.
82  * \param obj2 Pointer to the second integer.
83  *
84  * \return The values required for an osl compare function.
85  *
86  * \sa osl_compare_func, osl_hash_compare().
87  */
88 static int uint64_compare(const struct osl_object *obj1,
89                 const struct osl_object *obj2)
90 {
91         uint64_t d1 = read_u64((const char *)obj1->data);
92         uint64_t d2 = read_u64((const char *)obj2->data);
93
94         if (d1 < d2)
95                 return 1;
96         if (d1 > d2)
97                 return -1;
98         return 0;
99 }
100
101 static struct osl_column_description dir_table_cols[] = {
102         [DT_NAME] = {
103                 .storage_type = OSL_MAPPED_STORAGE,
104                 .storage_flags = 0,
105                 .name = "dir",
106         },
107         [DT_NUM] = {
108                 .storage_type = OSL_MAPPED_STORAGE,
109                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
110                 .name = "num",
111                 .compare_function = uint64_compare,
112                 .data_size = sizeof(uint64_t)
113         },
114         [DT_PARENT_NUM] = {
115                 .storage_type = OSL_MAPPED_STORAGE,
116                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
117                 .name = "parent_num",
118                 .compare_function = size_compare,
119                 .data_size = sizeof(uint64_t)
120         },
121         [DT_BYTES] = {
122                 .storage_type = OSL_MAPPED_STORAGE,
123                 .storage_flags =  OSL_RBTREE | OSL_FIXED_SIZE,
124                 .compare_function = size_compare,
125                 .name = "num_bytes",
126                 .data_size = sizeof(uint64_t)
127         },
128         [DT_FILES] = {
129                 .storage_type = OSL_MAPPED_STORAGE,
130                 .storage_flags =  OSL_RBTREE | OSL_FIXED_SIZE,
131                 .compare_function = size_compare,
132                 .name = "num_files",
133                 .data_size = sizeof(uint64_t)
134         }
135 };
136
137 static struct osl_table_description dir_table_desc = {
138         .name = "dir_table",
139         .num_columns = NUM_DT_COLUMNS,
140         .flags = 0,
141         .column_descriptions = dir_table_cols,
142 };
143
144 /*
145  * The columns of the per-user tables.
146  *
147  * Adu tracks disk usage on a per-user basis. For each user, a user table is
148  * being created. The rows of the user table have three columns: The directory
149  * number that may be resolved to the path using the directory table, the
150  * number of bytes and the number of files in that directory owned by the given
151  * user.
152  */
153 static struct osl_column_description user_table_cols[] = {
154         [UT_DIR_NUM] = {
155                 .storage_type = OSL_MAPPED_STORAGE,
156                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
157                 .name = "dir_num",
158                 .compare_function = uint64_compare,
159                 .data_size = sizeof(uint64_t)
160         },
161         [UT_BYTES] = {
162                 .storage_type = OSL_MAPPED_STORAGE,
163                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
164                 .compare_function = size_compare,
165                 .name = "num_bytes",
166                 .data_size = sizeof(uint64_t)
167         },
168         [UT_FILES] = {
169                 .storage_type = OSL_MAPPED_STORAGE,
170                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
171                 .compare_function = size_compare,
172                 .name = "num_files",
173                 .data_size = sizeof(uint64_t)
174         },
175 };
176
177 /**
178  * The log function.
179  *
180  * \param ll Loglevel.
181  * \param fml Usual format string.
182  *
183  * All XXX_LOG() macros use this function.
184  */
185 __printf_2_3 void __log(int ll, const char* fmt,...)
186 {
187         va_list argp;
188         FILE *outfd;
189         struct tm *tm;
190         time_t t1;
191         char str[255] = "";
192
193         if (ll < conf.loglevel_arg)
194                 return;
195         outfd = stderr;
196         time(&t1);
197         tm = localtime(&t1);
198         strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
199         fprintf(outfd, "%s ", str);
200         va_start(argp, fmt);
201         vfprintf(outfd, fmt, argp);
202         va_end(argp);
203 }
204
205 static int open_user_table(struct user_info *ui, int create)
206 {
207         int ret;
208         struct passwd *pw;
209
210         ui->desc = adu_malloc(sizeof(*ui->desc));
211         ui->desc->num_columns = NUM_UT_COLUMNS;
212         ui->desc->flags = 0;
213         ui->desc->column_descriptions = user_table_cols;
214         ui->desc->dir = adu_strdup(conf.database_dir_arg);
215         ui->desc->name = make_message("%u", (unsigned)ui->uid);
216         pw = getpwuid(ui->uid);
217         if (pw && pw->pw_name)
218                 ui->pw_name = adu_strdup(pw->pw_name);
219
220         INFO_LOG(".............................uid #%u: %u\n",
221                 (unsigned)num_uids, (unsigned)ui->uid);
222         if (create) {
223                 ret = osl(osl_create_table(ui->desc));
224                 if (ret < 0)
225                         goto err;
226                 num_uids++;
227         }
228         ret = osl(osl_open_table(ui->desc, &ui->table));
229         if (ret < 0)
230                 goto err;
231         return 1;
232 err:
233         free((char *)ui->desc->name);
234         free((char *)ui->desc->dir);
235         free(ui->pw_name);
236         free(ui->desc);
237         ui->desc->name = NULL;
238         ui->desc->dir = NULL;
239         ui->desc = NULL;
240         ui->table = NULL;
241         ui->flags = 0;
242         return ret;
243 }
244
245 int for_each_admissible_user(int (*func)(struct user_info *, void *),
246                 void *data)
247 {
248         struct user_info *ui = uid_hash_table;
249
250         if (!ui)
251                 return -ERRNO_TO_ERROR(EFAULT);
252
253         for (; ui < uid_hash_table + uid_hash_table_size; ui++) {
254                 int ret;
255
256                 if (!ui_used(ui) || !ui_admissible(ui))
257                         continue;
258                 ret = func(ui, data);
259                 if (ret < 0)
260                         return ret;
261         }
262         return 1;
263 }
264
265 #define PRIME1 0xb11924e1
266 #define PRIME2 0x01000193
267
268 void create_hash_table(unsigned bits)
269 {
270         uid_hash_table_size = 1 << bits;
271         uid_hash_table = adu_calloc(uid_hash_table_size *
272                 sizeof(struct user_info));
273 }
274
275 static void free_hash_table(void)
276 {
277         free(uid_hash_table);
278         uid_hash_table = NULL;
279 }
280
281 static void close_dir_table(void)
282 {
283         int ret;
284
285         if (!dir_table)
286                 return;
287         ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
288         if (ret < 0)
289                 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
290         free((char *)dir_table_desc.dir);
291         dir_table = NULL;
292 }
293
294 static int close_user_table(struct user_info *ui, __a_unused void *data)
295 {
296         int ret;
297
298         ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
299         if (ret < 0)
300                 ERROR_LOG("failed to close user table %u: %s\n",
301                         (unsigned) ui->uid, adu_strerror(-ret));
302         free((char *)ui->desc->name);
303         ui->desc->name = NULL;
304         free((char *)ui->desc->dir);
305         ui->desc->dir = NULL;
306         free(ui->pw_name);
307         ui->pw_name = NULL;
308         free(ui->desc);
309         ui->desc = NULL;
310         ui->table = NULL;
311         ui->flags = 0;
312         return 1;
313 }
314
315 static void close_user_tables(void)
316 {
317         for_each_admissible_user(close_user_table, NULL);
318 }
319
320 void close_all_tables(void)
321 {
322         close_dir_table();
323         close_user_tables();
324         free_hash_table();
325 }
326
327 static void signal_handler(int s)
328 {
329         signum = s;
330 }
331
332 void check_signals(void)
333 {
334         if (likely(!signum))
335                 return;
336         EMERG_LOG("caught signal %d\n", signum);
337         close_all_tables();
338         exit(EXIT_FAILURE);
339 }
340
341 static int init_signals(void)
342 {
343         if (signal(SIGINT, &signal_handler) == SIG_ERR)
344                 return -E_SIGNAL_SIG_ERR;
345         if (signal(SIGTERM, &signal_handler) == SIG_ERR)
346                 return -E_SIGNAL_SIG_ERR;
347         if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
348                 return -E_SIGNAL_SIG_ERR;
349         return 1;
350 }
351
352 /*
353  * We use a hash table of size s=2^uid_hash_bits to map the uids into the
354  * interval [0..s]. Hash collisions are treated by open addressing, i.e.
355  * unused slots in the table are used to store different uids that hash to the
356  * same slot.
357  *
358  * If a hash collision occurs, different slots are successively probed in order
359  * to find an unused slot for the new uid. Probing is implemented via a second
360  * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
361  * odd number.
362  *
363  * An odd number is sufficient to make sure each entry of the hash table gets
364  * probed for probe_num between 0 and s-1 because s is a power of two, hence
365  * the second hash value has never a common divisor with the hash table size.
366  * IOW: h is invertible in the ring [0..s].
367  */
368 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
369 {
370         return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
371                 % uid_hash_table_size;
372 }
373
374 static int uid_is_admissible(uint32_t uid, struct uid_range *urs)
375 {
376         struct uid_range *ur;
377         int ret = 1;
378
379         if (!urs) /* empty array means all uids are allowed */
380                 return 1;
381         FOR_EACH_UID_RANGE(ur, urs)
382                 if (ur->low <= uid && ur->high >= uid)
383                         goto out;
384         ret = 0;
385 out:
386         DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
387                 ret? "" : "not ");
388         return ret;
389 }
390
391 int search_uid(uint32_t uid, struct uid_range *urs,
392                 enum search_uid_flags flags, struct user_info **ui_ptr)
393 {
394         uint32_t p;
395
396         for (p = 0; p < uid_hash_table_size; p++) {
397                 struct user_info *ui = uid_hash_table + double_hash(uid, p);
398
399                 if (!ui_used(ui)) {
400                         int ret;
401                         if (!flags)
402                                 return -E_BAD_UID;
403                         ui->uid = uid;
404                         ui->flags |= UI_FL_SLOT_USED;
405                         if (!uid_is_admissible(uid, urs))
406                                 return 0;
407                         ui->flags |= UI_FL_ADMISSIBLE;
408                         ret = open_user_table(ui, flags & CREATE_USER_TABLE);
409                         if (ret < 0)
410                                 return ret;
411
412                         if (ui_ptr)
413                                 *ui_ptr = ui;
414                         return 1;
415                 }
416                 if (ui->uid != uid)
417                         continue;
418                 if (ui_ptr)
419                         *ui_ptr = ui;
420                 return 0;
421         }
422         return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
423 }
424
425 char *get_uid_list_name(void)
426 {
427         return make_message("%s/uid_list", conf.database_dir_arg);
428 }
429
430 void sort_hash_table(int (*comp)(const void *, const void *))
431 {
432         qsort(uid_hash_table, uid_hash_table_size, sizeof(struct user_info),
433                 comp);
434 }
435
436 int open_dir_table(int create)
437 {
438         dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
439
440         if (create) {
441                 int ret = osl(osl_create_table(&dir_table_desc));
442                 if (ret < 0) {
443                         free((char *)dir_table_desc.dir);
444                         return ret;
445                 }
446         }
447         return osl(osl_open_table(&dir_table_desc, &dir_table));
448 }
449
450 static int check_args(void)
451 {
452         if (conf.create_given && !conf.base_dir_given)
453                 return -E_SYNTAX;
454
455         /* remove trailing slashes from base-dir arg */
456         if (conf.base_dir_given) {
457                 size_t len = strlen(conf.base_dir_arg);
458                 for (;;) {
459                         if (!len) /* empty string */
460                                 return -ERRNO_TO_ERROR(EINVAL);
461                         if (!--len) /* length 1 is always OK */
462                                 break;
463                         if (conf.base_dir_arg[len] != '/')
464                                 break; /* no trailing slash, also OK */
465                         conf.base_dir_arg[len] = '\0';
466                 }
467         }
468         return 1;
469 }
470
471 static int print_complete_help_and_die(void)
472 {
473         const char **line;
474         select_cmdline_parser_init(&select_conf);
475
476         printf("%s-%s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
477         printf("%s\n\n", gengetopt_args_info_purpose);
478         printf("%s\n\n", gengetopt_args_info_usage);
479
480         if (conf.help_given)
481                 line = gengetopt_args_info_help;
482         else
483                 line = gengetopt_args_info_detailed_help;
484         for (; *line; line++)
485                 printf("%s\n", *line);
486
487         if (conf.help_given)
488                 line = select_args_info_help;
489         else
490                 line  = select_args_info_detailed_help;
491         printf("Select options:\n");
492         for (; *line; line++)
493                 printf("%s\n", *line);
494
495         printf("Interactive commands:\n");
496         print_interactive_help();
497         exit(EXIT_FAILURE);
498 }
499
500 int main(int argc, char **argv)
501 {
502         int ret;
503         struct cmdline_parser_params params = {
504                 .override = 0,
505                 .initialize = 1,
506                 .check_required = 0,
507                 .check_ambiguity = 0,
508                 .print_errors = 0
509         };
510         /* ignore errors and print complete help if --help was given */
511         cmdline_parser_ext(argc, argv, &conf, &params);
512         if (conf.help_given || conf.detailed_help_given)
513                 print_complete_help_and_die();
514         params.check_required = 1;
515         params.check_ambiguity = 1;
516         params.print_errors = 1;
517         ret = cmdline_parser_ext(argc, argv, &conf, &params);
518         if (ret)
519                 exit(EXIT_FAILURE);
520
521         ret = check_args();
522         if (ret < 0)
523                 goto out;
524         ret = init_signals();
525         if (ret < 0)
526                 goto out;
527         ret = -E_SYNTAX;
528         if (conf.select_given)
529                 ret = com_select();
530         else if (conf.create_given)
531                 ret = com_create();
532         else
533                 ret = com_interactive();
534         if (ret < 0)
535                 goto out;
536 out:
537         if (ret < 0) {
538                 ERROR_LOG("%s\n", adu_strerror(-ret));
539                 return -EXIT_FAILURE;
540         }
541         return EXIT_SUCCESS;
542 }