Move the code for the select command to its own file.
[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 /** Global dir count. */
22 uint64_t num_dirs = 0;
23 /** Global files count. */
24 uint64_t num_files = 0;
25 /** Global bytes count. */
26 uint64_t num_bytes = 0;
27
28 /** The number of different uids found so far. */
29 uint32_t num_uids = 0;
30
31 /**
32 * Contains info for each user that owns at least one regular file.
33 *
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.
37 */
38 struct user_info *uid_hash_table = NULL;
39
40 /**
41 * The table containing the directory names and statistics.
42 */
43 struct osl_table *dir_table = NULL;
44
45 /**
46 * The array of all uid ranges that were given at the command line.
47 */
48 struct uid_range *admissible_uids;
49
50 /** Evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y. */
51 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
52
53 /**
54 * Compare the size of two directories
55 *
56 * \param obj1 Pointer to the first object.
57 * \param obj2 Pointer to the second object.
58 *
59 * This function first compares the size values as usual integers. If they compare as
60 * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
61 * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
62 */
63 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
64 {
65 uint64_t d1 = *(uint64_t *)obj1->data;
66 uint64_t d2 = *(uint64_t *)obj2->data;
67 int ret = NUM_COMPARE(d2, d1);
68
69 if (ret)
70 return ret;
71 //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
72 return NUM_COMPARE(obj2->data, obj1->data);
73 }
74
75 /**
76 * Compare two osl objects pointing to unsigned integers of 64 bit size.
77 *
78 * \param obj1 Pointer to the first integer.
79 * \param obj2 Pointer to the second integer.
80 *
81 * \return The values required for an osl compare function.
82 *
83 * \sa osl_compare_func, osl_hash_compare().
84 */
85 static int uint64_compare(const struct osl_object *obj1,
86 const struct osl_object *obj2)
87 {
88 uint64_t d1 = read_u64((const char *)obj1->data);
89 uint64_t d2 = read_u64((const char *)obj2->data);
90
91 if (d1 < d2)
92 return 1;
93 if (d1 > d2)
94 return -1;
95 return 0;
96 }
97
98 static struct osl_column_description dir_table_cols[] = {
99 [DT_NAME] = {
100 .storage_type = OSL_MAPPED_STORAGE,
101 .storage_flags = 0,
102 .name = "dir",
103 },
104 [DT_NUM] = {
105 .storage_type = OSL_MAPPED_STORAGE,
106 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
107 .name = "num",
108 .compare_function = uint64_compare,
109 .data_size = sizeof(uint64_t)
110 },
111 [DT_PARENT_NUM] = {
112 .storage_type = OSL_MAPPED_STORAGE,
113 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
114 .name = "parent_num",
115 .compare_function = size_compare,
116 .data_size = sizeof(uint64_t)
117 },
118 [DT_BYTES] = {
119 .storage_type = OSL_MAPPED_STORAGE,
120 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
121 .compare_function = size_compare,
122 .name = "num_bytes",
123 .data_size = sizeof(uint64_t)
124 },
125 [DT_FILES] = {
126 .storage_type = OSL_MAPPED_STORAGE,
127 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
128 .compare_function = size_compare,
129 .name = "num_files",
130 .data_size = sizeof(uint64_t)
131 }
132 };
133
134 static struct osl_table_description dir_table_desc = {
135 .name = "dir_table",
136 .num_columns = NUM_DT_COLUMNS,
137 .flags = 0,
138 .column_descriptions = dir_table_cols,
139 };
140
141 static struct osl_column_description user_table_cols[] = {
142 [UT_DIR_NUM] = {
143 .storage_type = OSL_MAPPED_STORAGE,
144 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
145 .name = "dir_num",
146 .compare_function = uint64_compare,
147 .data_size = sizeof(uint64_t)
148 },
149 [UT_BYTES] = {
150 .storage_type = OSL_MAPPED_STORAGE,
151 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
152 .compare_function = size_compare,
153 .name = "num_bytes",
154 .data_size = sizeof(uint64_t)
155 },
156 [UT_FILES] = {
157 .storage_type = OSL_MAPPED_STORAGE,
158 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
159 .compare_function = size_compare,
160 .name = "num_files",
161 .data_size = sizeof(uint64_t)
162 },
163 };
164
165 static int check_uid_arg(const char *arg, uint32_t *uid)
166 {
167 const uint32_t max = ~0U;
168 /*
169 * we need an 64-bit int for string -> uid conversion because strtoll()
170 * returns a signed value.
171 */
172 int64_t val;
173 int ret = atoi64(arg, &val);
174
175 if (ret < 0)
176 return ret;
177 if (val < 0 || val > max)
178 return -ERRNO_TO_ERROR(EINVAL);
179 *uid = val;
180 return 1;
181 }
182
183 static int parse_uid_range(const char *orig_arg, struct uid_range *ur)
184 {
185 int ret;
186 char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
187
188 if (!p || p == arg) { /* -42 or 42 */
189 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
190 if (ret < 0)
191 goto out;
192 ur->low = p? 0 : ur->high;
193 ret = 1;
194 goto out;
195 }
196 /* 42- or 42-4711 */
197 *p = '\0';
198 p++;
199 ret = check_uid_arg(arg, &ur->low);
200 if (ret < 0)
201 goto out;
202 ur->high = ~0U;
203 if (*p) { /* 42-4711 */
204 ret = check_uid_arg(p, &ur->high);
205 if (ret < 0)
206 goto out;
207 }
208 if (ur->low > ur->high)
209 ret = -ERRNO_TO_ERROR(EINVAL);
210 out:
211 if (ret < 0)
212 ERROR_LOG("bad uid option: %s\n", orig_arg);
213 else
214 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
215 ur->high);
216 free(arg);
217 return ret;
218 }
219
220 /**
221 * The log function.
222 *
223 * \param ll Loglevel.
224 * \param fml Usual format string.
225 *
226 * All XXX_LOG() macros use this function.
227 */
228 __printf_2_3 void __log(int ll, const char* fmt,...)
229 {
230 va_list argp;
231 FILE *outfd;
232 struct tm *tm;
233 time_t t1;
234 char str[255] = "";
235
236 if (ll < conf.loglevel_arg)
237 return;
238 outfd = stderr;
239 time(&t1);
240 tm = localtime(&t1);
241 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
242 fprintf(outfd, "%s ", str);
243 va_start(argp, fmt);
244 vfprintf(outfd, fmt, argp);
245 va_end(argp);
246 }
247
248 static int add_directory(char *dirname, uint64_t *dir_num, uint64_t *parent_dir_num,
249 uint64_t *dir_size, uint64_t *dir_files)
250 {
251 struct osl_object dir_objects[NUM_DT_COLUMNS];
252
253 INFO_LOG("adding #%llu: %s\n", (long long unsigned)*dir_num, dirname);
254 dir_objects[DT_NAME].data = dirname;
255 dir_objects[DT_NAME].size = strlen(dirname) + 1;
256 dir_objects[DT_NUM].data = dir_num;
257 dir_objects[DT_NUM].size = sizeof(*dir_num);
258 dir_objects[DT_PARENT_NUM].data = parent_dir_num;
259 dir_objects[DT_PARENT_NUM].size = sizeof(*parent_dir_num);
260 dir_objects[DT_BYTES].data = dir_size;
261 dir_objects[DT_BYTES].size = sizeof(*dir_size);
262 dir_objects[DT_FILES].data = dir_files;
263 dir_objects[DT_FILES].size = sizeof(*dir_files);
264 return osl(osl_add_row(dir_table, dir_objects));
265 }
266
267 static int open_user_table(struct user_info *ui, int create)
268 {
269 int ret;
270 struct passwd *pw;
271
272 ui->desc = adu_malloc(sizeof(*ui->desc));
273 ui->desc->num_columns = NUM_UT_COLUMNS;
274 ui->desc->flags = 0;
275 ui->desc->column_descriptions = user_table_cols;
276 ui->desc->dir = adu_strdup(conf.database_dir_arg);
277 ui->desc->name = make_message("%u", (unsigned)ui->uid);
278 pw = getpwuid(ui->uid);
279 if (pw && pw->pw_name)
280 ui->pw_name = adu_strdup(pw->pw_name);
281
282 INFO_LOG(".............................uid #%u: %u\n",
283 (unsigned)num_uids, (unsigned)ui->uid);
284 if (create) {
285 ret = osl(osl_create_table(ui->desc));
286 if (ret < 0)
287 goto err;
288 num_uids++;
289 }
290 ret = osl(osl_open_table(ui->desc, &ui->table));
291 if (ret < 0)
292 goto err;
293 return 1;
294 err:
295 free((char *)ui->desc->name);
296 free((char *)ui->desc->dir);
297 free(ui->pw_name);
298 free(ui->desc);
299 ui->desc->name = NULL;
300 ui->desc->dir = NULL;
301 ui->desc = NULL;
302 ui->table = NULL;
303 ui->flags = 0;
304 return ret;
305 }
306
307 #define uid_hash_bits 8
308 uint32_t uid_hash_table_size = 1 << uid_hash_bits;
309 #define PRIME1 0x811c9dc5
310 #define PRIME2 0x01000193
311
312 void create_hash_table(void)
313 {
314 uid_hash_table = adu_calloc(uid_hash_table_size
315 * sizeof(struct user_info));
316 }
317
318 static void free_hash_table(void)
319 {
320 free(uid_hash_table);
321 uid_hash_table = NULL;
322 }
323
324 static int create_tables(void)
325 {
326 int ret;
327
328 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
329 ret = osl(osl_create_table(&dir_table_desc));
330 if (ret < 0)
331 return ret;
332 create_hash_table();
333 return 1;
334 }
335
336 static void close_dir_table(void)
337 {
338 int ret;
339
340 if (!dir_table)
341 return;
342 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
343 if (ret < 0)
344 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
345 free((char *)dir_table_desc.dir);
346 dir_table = NULL;
347 }
348
349 static void close_user_table(struct user_info *ui)
350 {
351 int ret;
352
353 if (!ui || !ui_used(ui) || !ui_admissible(ui))
354 return;
355 ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
356 if (ret < 0)
357 ERROR_LOG("failed to close user table %u: %s\n",
358 (unsigned) ui->uid, adu_strerror(-ret));
359 free((char *)ui->desc->name);
360 ui->desc->name = NULL;
361 free((char *)ui->desc->dir);
362 ui->desc->dir = NULL;
363 free(ui->pw_name);
364 ui->pw_name = NULL;
365 free(ui->desc);
366 ui->desc = NULL;
367 ui->table = NULL;
368 ui->flags = 0;
369 }
370
371 static void close_user_tables(void)
372 {
373 struct user_info *ui;
374
375 FOR_EACH_USER(ui)
376 close_user_table(ui);
377 }
378
379 void close_all_tables(void)
380 {
381 close_dir_table();
382 close_user_tables();
383 free_hash_table();
384 }
385
386 static void signal_handler(int s)
387 {
388 signum = s;
389 }
390
391 void check_signals(void)
392 {
393 if (likely(!signum))
394 return;
395 EMERG_LOG("caught signal %d\n", signum);
396 close_all_tables();
397 exit(EXIT_FAILURE);
398 }
399
400 static int init_signals(void)
401 {
402 if (signal(SIGINT, &signal_handler) == SIG_ERR)
403 return -E_SIGNAL_SIG_ERR;
404 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
405 return -E_SIGNAL_SIG_ERR;
406 return 1;
407 }
408
409 /*
410 * We use a hash table of size s=2^uid_hash_bits to map the uids into the
411 * interval [0..s]. Hash collisions are treated by open addressing, i.e.
412 * unused slots in the table are used to store different uids that hash to the
413 * same slot.
414 *
415 * If a hash collision occurs, different slots are successively probed in order
416 * to find an unused slot for the new uid. Probing is implemented via a second
417 * hash function that maps the uid to h=(uid * PRIME2) | 1, which is always an
418 * odd number.
419 *
420 * An odd number is sufficient to make sure each entry of the hash table gets
421 * probed for probe_num between 0 and s-1 because s is a power of two, hence
422 * the second hash value has never a common divisor with the hash table size.
423 * IOW: h is invertible in the ring [0..s].
424 */
425 static uint32_t double_hash(uint32_t uid, uint32_t probe_num)
426 {
427 return (uid * PRIME1 + ((uid * PRIME2) | 1) * probe_num)
428 % uid_hash_table_size;
429 }
430
431 static int uid_is_admissible(uint32_t uid)
432 {
433 int i;
434
435 for (i = 0; i < conf.uid_given; i++) {
436 struct uid_range *ur = admissible_uids + i;
437
438 if (ur->low <= uid && ur->high >= uid)
439 break;
440 }
441 i = !conf.uid_given || i < conf.uid_given;
442 DEBUG_LOG("uid %u is %sadmissible\n", (unsigned)uid,
443 i? "" : "not ");
444 return i;
445 }
446
447 int search_uid(uint32_t uid, enum search_uid_flags flags,
448 struct user_info **ui_ptr)
449 {
450 uint32_t p;
451
452 for (p = 0; p < uid_hash_table_size; p++) {
453 struct user_info *ui = uid_hash_table + double_hash(uid, p);
454
455 if (!ui_used(ui)) {
456 int ret;
457 if (!flags)
458 return -E_BAD_UID;
459 ui->uid = uid;
460 ui->flags |= UI_FL_SLOT_USED;
461 if (!uid_is_admissible(uid))
462 return 0;
463 ui->flags |= UI_FL_ADMISSIBLE;
464 ret = open_user_table(ui, flags & CREATE_USER_TABLE);
465 if (ret < 0)
466 return ret;
467
468 if (ui_ptr)
469 *ui_ptr = ui;
470 return 1;
471 }
472 if (ui->uid != uid)
473 continue;
474 if (ui_ptr)
475 *ui_ptr = ui;
476 return 0;
477 }
478 return flags? -E_HASH_TABLE_OVERFLOW : -E_BAD_UID;
479 }
480
481 static int update_user_row(struct osl_table *t, uint64_t dir_num,
482 uint64_t *add)
483 {
484 struct osl_row *row;
485 struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
486
487 int ret = osl(osl_get_row(t, UT_DIR_NUM, &obj, &row));
488
489 if (ret == -E_OSL && osl_errno != E_OSL_RB_KEY_NOT_FOUND)
490 return ret;
491 if (ret < 0) { /* this is the first file we add */
492 struct osl_object objects[NUM_UT_COLUMNS];
493 uint64_t num_files = 1;
494
495 objects[UT_DIR_NUM].data = &dir_num;
496 objects[UT_DIR_NUM].size = sizeof(dir_num);
497 objects[UT_BYTES].data = add;
498 objects[UT_BYTES].size = sizeof(*add);
499 objects[UT_FILES].data = &num_files;
500 objects[UT_FILES].size = sizeof(num_files);
501 INFO_LOG("######################### ret: %d\n", ret);
502 ret = osl(osl_add_row(t, objects));
503 INFO_LOG("######################### ret: %d\n", ret);
504 return ret;
505 } else { /* add size and increment file count */
506 uint64_t num;
507 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
508
509 ret = osl(osl_get_object(t, row, UT_BYTES, &obj1));
510 if (ret < 0)
511 return ret;
512 num = *(uint64_t *)obj1.data + *add;
513 ret = osl(osl_update_object(t, row, UT_BYTES, &obj2));
514 if (ret < 0)
515 return ret;
516 ret = osl(osl_get_object(t, row, UT_FILES, &obj1));
517 if (ret < 0)
518 return ret;
519 num = *(uint64_t *)obj1.data + 1;
520 return osl(osl_update_object(t, row, UT_FILES, &obj2));
521 }
522 }
523
524 /* id of the device containing the base dir. */
525 static dev_t device_id;
526
527 static int scan_dir(char *dirname, uint64_t *parent_dir_num)
528 {
529 DIR *dir;
530 struct dirent *entry;
531 int ret, cwd_fd, ret2;
532 uint64_t dir_size = 0, dir_files = 0;
533 uint64_t this_dir_num = ++num_dirs;
534
535 check_signals();
536 DEBUG_LOG("----------------- %llu: %s\n", (long long unsigned)num_dirs, dirname);
537 ret = adu_opendir(dirname, &dir, &cwd_fd);
538 if (ret < 0) {
539 if (ret != -ERRNO_TO_ERROR(EACCES))
540 return ret;
541 WARNING_LOG("permission denied for %s\n", dirname);
542 return 1;
543 }
544 while ((entry = readdir(dir))) {
545 mode_t m;
546 struct stat s;
547 uint32_t uid;
548 uint64_t size;
549 struct user_info *ui;
550
551 if (!strcmp(entry->d_name, "."))
552 continue;
553 if (!strcmp(entry->d_name, ".."))
554 continue;
555 if (lstat(entry->d_name, &s) == -1) {
556 WARNING_LOG("lstat error for %s/%s\n", dirname,
557 entry->d_name);
558 continue;
559 }
560 m = s.st_mode;
561 if (!S_ISREG(m) && !S_ISDIR(m))
562 continue;
563 if (S_ISDIR(m)) {
564 if (conf.one_file_system_given && s.st_dev != device_id)
565 continue;
566 ret = scan_dir(entry->d_name, &this_dir_num);
567 if (ret < 0)
568 goto out;
569 continue;
570 }
571 /* regular file */
572 size = s.st_size;
573 dir_size += size;
574 num_bytes += size;
575 dir_files++;
576 num_files++;
577 uid = s.st_uid;
578 ret = search_uid(uid, CREATE_USER_TABLE | OPEN_USER_TABLE, &ui);
579 if (ret < 0)
580 goto out;
581 ui->bytes += size;
582 ui->files++;
583 ret = update_user_row(ui->table, this_dir_num, &size);
584 if (ret < 0)
585 goto out;
586 }
587 ret = add_directory(dirname, &this_dir_num, parent_dir_num,
588 &dir_size, &dir_files);
589 out:
590 closedir(dir);
591 ret2 = adu_fchdir(cwd_fd);
592 if (ret2 < 0 && ret >= 0)
593 ret = ret2;
594 close(cwd_fd);
595 return ret;
596 }
597
598 char *get_uid_list_name(void)
599 {
600 return make_message("%s/uid_list", conf.database_dir_arg);
601 }
602
603 static int write_uid_list(void)
604 {
605 char *buf, *filename;
606 uint32_t count = 0;
607 struct user_info *ui;
608 size_t size = num_uids * sizeof(uint32_t);
609 int ret;
610
611 if (!num_uids)
612 return 0;
613 buf = adu_malloc(size);
614 FOR_EACH_USER(ui) {
615 if (!ui_used(ui) || !ui_admissible(ui))
616 continue;
617 DEBUG_LOG("saving uid %u\n", (unsigned) ui->uid);
618 write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
619 }
620 filename = get_uid_list_name();
621 ret = adu_write_file(filename, buf, size);
622 free(filename);
623 free(buf);
624 return ret;
625 }
626
627 int open_dir_table(void)
628 {
629 if (!dir_table_desc.dir) /* we did not create the table */
630 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
631 return osl(osl_open_table(&dir_table_desc, &dir_table));
632 }
633 static int com_create()
634 {
635 uint64_t zero = 0ULL;
636 int ret;
637 struct stat statbuf;
638
639 if (lstat(conf.base_dir_arg, &statbuf) == -1)
640 return -ERRNO_TO_ERROR(errno);
641 if (!S_ISDIR(statbuf.st_mode))
642 return -ERRNO_TO_ERROR(ENOTDIR);
643 device_id = statbuf.st_dev;
644 ret = create_tables();
645 if (ret < 0)
646 return ret;
647 check_signals();
648 ret = open_dir_table();
649 if (ret < 0)
650 return ret;
651 check_signals();
652 ret = scan_dir(conf.base_dir_arg, &zero);
653 if (ret < 0)
654 goto out;
655 ret = write_uid_list();
656 out:
657 close_all_tables();
658 return ret;
659 }
660
661 static int check_args(void)
662 {
663 int i, ret;
664
665 /* remove trailing slashes from base-dir arg */
666 if (conf.base_dir_given) {
667 size_t len = strlen(conf.base_dir_arg);
668 for (;;) {
669 if (!len) /* empty string */
670 return -ERRNO_TO_ERROR(EINVAL);
671 if (!--len) /* length 1 is always OK */
672 break;
673 if (conf.base_dir_arg[len] != '/')
674 break; /* no trailing slash, also OK */
675 conf.base_dir_arg[len] = '\0';
676 }
677 }
678 if (!conf.uid_given)
679 return 0;
680 admissible_uids = adu_malloc(conf.uid_given * sizeof(*admissible_uids));
681 for (i = 0; i < conf.uid_given; i++) {
682 ret = parse_uid_range(conf.uid_arg[i], admissible_uids + i);
683 if (ret < 0)
684 goto err;
685 }
686 return 1;
687 err:
688 free(admissible_uids);
689 admissible_uids = NULL;
690 return ret;
691 }
692
693 int main(int argc, char **argv)
694 {
695 int ret;
696 struct cmdline_parser_params params = {
697 .override = 0,
698 .initialize = 1,
699 .check_required = 0,
700 .check_ambiguity = 0,
701 .print_errors = 1
702 };
703
704 cmdline_parser_ext(argc, argv, &conf, &params); /* aborts on errors */
705 ret = init_signals();
706 if (ret < 0)
707 goto out;
708 ret = check_args();
709 if (ret < 0)
710 goto out;
711 ret = -E_SYNTAX;
712 if (conf.select_given)
713 ret = com_select();
714 else
715 ret = com_create();
716 if (ret < 0)
717 goto out;
718 out:
719 free(admissible_uids);
720 if (ret < 0) {
721 ERROR_LOG("%s\n", adu_strerror(-ret));
722 return -EXIT_FAILURE;
723 }
724 return EXIT_SUCCESS;
725 }