]> git.tuebingen.mpg.de Git - adu.git/blob - select.c
05302570ff5ba740731c3ba815c62b5f8da2e1fc
[adu.git] / select.c
1 /*
2  * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file select.c The select mode of adu. */
8
9 #include <dirent.h> /* readdir() */
10 #include "adu.h"
11 #include "gcc-compat.h"
12 #include "cmdline.h"
13 #include "fd.h"
14 #include "string.h"
15 #include "error.h"
16 #include "portable_io.h"
17 #include "select.cmdline.h"
18
19 /** Global dir count. */
20 static uint64_t num_dirs;
21 /** Global files count. */
22 static uint64_t num_files;
23 /** Global bytes count. */
24 static uint64_t num_bytes;
25
26 /** The decimal representation of an uint64_t never exceeds that size. */
27 #define FORMATED_VALUE_SIZE 25
28
29 /* these get filled in by the select command. */
30 static char count_unit_buf[4] = "( )", size_unit_buf[4] = "( )";
31
32 enum global_stats_flags {
33         GSF_PRINT_DIRNAME = 1,
34         GSF_PRINT_BYTES = 2,
35         GSF_PRINT_FILES = 4,
36         GSF_COMPUTE_SUMMARY = 8,
37 };
38
39 struct global_stats_info {
40         uint32_t count;
41         int ret;
42         int osl_errno;
43         enum global_stats_flags flags;
44 };
45
46 enum user_stats_flags {
47         USF_PRINT_DIRNAME = 1,
48         USF_PRINT_BYTES = 2,
49         USF_PRINT_FILES = 4,
50         USF_COMPUTE_SUMMARY = 8,
51 };
52
53 struct user_stats_info {
54         uint32_t count;
55         enum user_stats_flags flags;
56         int ret;
57         int osl_errno;
58         struct user_info *ui;
59 };
60
61 static const uint64_t size_unit_divisors[] = {
62         [size_unit_arg_b] = 1ULL,
63         [size_unit_arg_k] = 1024ULL,
64         [size_unit_arg_m] = 1024ULL * 1024ULL,
65         [size_unit_arg_g] = 1024ULL * 1024ULL * 1024ULL,
66         [size_unit_arg_t] = 1024ULL * 1024ULL * 1024ULL * 1024ULL,
67 };
68
69 static const uint64_t count_unit_divisors[] = {
70
71         [count_unit_arg_n] = 1ULL,
72         [count_unit_arg_k] = 1000ULL,
73         [count_unit_arg_m] = 1000ULL * 1000ULL,
74         [count_unit_arg_g] = 1000ULL * 1000ULL * 1000ULL,
75         [count_unit_arg_t] = 1000ULL * 1000ULL * 1000ULL * 1000ULL,
76 };
77
78 static const char size_unit_abbrevs[] = " BKMGT";
79 static const char count_unit_abbrevs[] = "  kmgt";
80 static enum enum_size_unit format_size_value(enum enum_size_unit unit,
81                 uint64_t value, int print_unit, char *result)
82 {
83         enum enum_size_unit u = unit;
84         char unit_buf[2] = "\0\0";
85
86         if (unit == size_unit_arg_h) /* human readable */
87                 for (u = size_unit_arg_b; u < size_unit_arg_t &&
88                                 value > size_unit_divisors[u + 1]; u++)
89                         ; /* nothing */
90         if (print_unit)
91                 unit_buf[0] = size_unit_abbrevs[u];
92         sprintf(result, "%llu%s",
93                 (long long unsigned)value / size_unit_divisors[u], unit_buf);
94         return u;
95 }
96
97 static enum enum_count_unit format_count_value(enum enum_count_unit unit,
98                 uint64_t value, int print_unit, char *result)
99 {
100         enum enum_count_unit u = unit;
101         char unit_buf[2] = "\0\0";
102
103         if (unit == count_unit_arg_h) /* human readable */
104                 for (u = count_unit_arg_n; u < count_unit_arg_t &&
105                                 value > count_unit_divisors[u + 1]; u++)
106                         ; /* nothing */
107         if (print_unit)
108                 unit_buf[0] = count_unit_abbrevs[u];
109         sprintf(result, "%llu%s",
110                 (long long unsigned)value / count_unit_divisors[u], unit_buf);
111         return u;
112 }
113
114 static FILE *output_file;
115
116 __printf_1_2 static int output(const char const *fmt, ...)
117 {
118         va_list argp;
119         int ret;
120
121         va_start(argp, fmt);
122         ret = vfprintf(output_file, fmt, argp);
123         va_end(argp);
124         return ret < 0? -E_OUTPUT : 1;
125 }
126
127 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
128 {
129         char *result = NULL, *tmp;
130         struct osl_row *row;
131         uint64_t val = *dirnum;
132         struct osl_object obj;
133         int ret;
134         char *pfx;
135
136 again:
137         obj.data = &val;
138         obj.size = sizeof(val);
139         ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
140         if (ret < 0)
141                 goto out;
142         ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
143         if (ret < 0)
144                 goto out;
145         val = *(uint64_t *)obj.data;
146         ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
147         if (ret < 0)
148                 goto out;
149         pfx = (select_conf.print_base_dir_given || val)? (char *)obj.data :  ".";
150         tmp = make_message("%s/%s", pfx, result? result : "");
151         free(result);
152         result = tmp;
153         if (val)
154                 goto again;
155 out:
156         if (ret < 0) {
157                 free(result);
158                 *name = NULL;
159         } else {
160                 assert(result);
161                 *name = result;
162         }
163         return ret;
164 }
165
166 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
167 {
168         struct osl_object obj;
169         int ret;
170
171         *name = NULL;
172         ret = osl(osl_get_object(dir_table, dir_table_row, DT_NUM, &obj));
173         if (ret < 0)
174                 return ret;
175         return get_dir_name_by_number((uint64_t *)obj.data, name);
176 }
177
178 static int user_stats_loop_function(struct osl_row *row, void *data)
179 {
180         struct user_stats_info *usi = data;
181         struct osl_object obj;
182         int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
183         char formated_value[FORMATED_VALUE_SIZE];
184
185         check_signals();
186         if (!usi->count && !summary) {
187                 ret = -E_LOOP_COMPLETE;
188                 goto err;
189         }
190         if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
191                 uint64_t files;
192                 ret = osl(osl_get_object(usi->ui->table, row, UT_FILES, &obj));
193                 if (ret < 0)
194                         goto err;
195                 files = *(uint64_t *)obj.data;
196                 if (usi->count && (usi->flags & USF_PRINT_FILES)) {
197                         format_count_value(select_conf.count_unit_arg, files,
198                                 select_conf.count_unit_arg == count_unit_arg_h,
199                                 formated_value);
200                         ret = output("\t%s%s", formated_value,
201                                 (usi->flags & (USF_PRINT_BYTES | USF_PRINT_DIRNAME))?
202                                         "\t" : "\n");
203                         if (ret < 0)
204                                 goto err;
205                 }
206                 if (summary)
207                         usi->ui->files += files;
208         }
209         if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
210                 uint64_t bytes;
211                 ret = osl(osl_get_object(usi->ui->table, row, UT_BYTES, &obj));
212                 if (ret < 0)
213                         goto err;
214                 bytes = *(uint64_t *)obj.data;
215                 if (usi->count && (usi->flags & USF_PRINT_BYTES)) {
216                         format_size_value(select_conf.size_unit_arg, bytes,
217                                 select_conf.size_unit_arg == size_unit_arg_h,
218                                 formated_value);
219                         ret = output("%s%s%s",
220                                 (usi->flags & USF_PRINT_FILES)? "" : "\t",
221                                 formated_value,
222                                 usi->flags & USF_PRINT_DIRNAME?  "\t" : "\n"
223                         );
224                         if (ret < 0)
225                                 goto err;
226                 }
227                 if (summary) {
228                         usi->ui->bytes += bytes;
229                         usi->ui->dirs++;
230                 }
231
232         }
233         if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
234                 char *dirname;
235                 ret = osl(osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj));
236                 if (ret < 0)
237                         goto err;
238                 ret = get_dir_name_by_number((uint64_t *)obj.data, &dirname);
239                 if (ret < 0)
240                         goto err;
241                 ret = output("%s%s\n",
242                         (usi->flags & (USF_PRINT_BYTES | USF_PRINT_FILES))? "" : "\t",
243                         dirname);
244                 free(dirname);
245                 if (ret < 0)
246                         goto err;
247         }
248         if (usi->count > 0)
249                 usi->count--;
250         return 1;
251 err:
252         usi->ret = ret;
253         usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
254         return -1;
255 }
256
257 static int global_stats_loop_function(struct osl_row *row, void *data)
258 {
259         struct global_stats_info *gsi = data;
260         struct osl_object obj;
261         char *dirname, formated_value[FORMATED_VALUE_SIZE];
262         int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
263
264         check_signals();
265         if (!gsi->count && !summary) {
266                 ret = -E_LOOP_COMPLETE;
267                 goto err;
268         }
269         if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
270                 uint64_t files;
271                 ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
272                 if (ret < 0)
273                         goto err;
274                 files = *(uint64_t *)obj.data;
275                 if (gsi->count && (gsi->flags & GSF_PRINT_FILES)) {
276                         format_count_value(select_conf.count_unit_arg, files,
277                                 select_conf.count_unit_arg == count_unit_arg_h,
278                                 formated_value);
279                         ret = output("\t%s%s", formated_value,
280                                 (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_DIRNAME))?
281                                 "\t" : "\n");
282                         if (ret < 0)
283                                 goto err;
284                 }
285                 if (summary)
286                         num_files += files;
287         }
288         if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
289                 uint64_t bytes;
290                 ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
291                 if (ret < 0)
292                         goto err;
293                 bytes = *(uint64_t *)obj.data;
294                 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES)) {
295                         format_size_value(select_conf.size_unit_arg, bytes,
296                                 select_conf.size_unit_arg == size_unit_arg_h,
297                                 formated_value);
298                         ret = output("%s%s%s",
299                                 (gsi->flags & GSF_PRINT_FILES)? "" : "\t",
300                                 formated_value,
301                                 (gsi->flags & GSF_PRINT_DIRNAME)? "\t" : "\n"
302                         );
303                         if (ret < 0)
304                                 goto err;
305                 }
306                 if (summary) {
307                         num_bytes += bytes;
308                         num_dirs++;
309                 }
310         }
311         if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
312                 ret = get_dir_name_of_row(row, &dirname);
313                 if (ret < 0)
314                         goto err;
315                 ret = output("%s%s\n",
316                         (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_FILES))? "" : "\t",
317                         dirname);
318                 free(dirname);
319                 if (ret < 0)
320                         goto err;
321         }
322         if (gsi->count > 0)
323                 gsi->count--;
324         return 1;
325 err:
326         gsi->ret = ret;
327         gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
328         return -1;
329 }
330
331 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
332 {
333         if (ret >= 0)
334                 return ret;
335         assert(ret == -E_OSL);
336         if (osl_errno != E_OSL_LOOP)
337                 /* error not caused by loop function returning negative. */
338                 return ret;
339         assert(loop_ret < 0);
340         if (loop_ret == -E_LOOP_COMPLETE) /* no error */
341                 return 1;
342         if (loop_ret == -E_OSL) { /* osl error in loop function */
343                 assert(loop_osl_errno);
344                 osl_errno = loop_osl_errno;
345         }
346         return loop_ret;
347 }
348
349 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
350                 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
351 {
352         int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
353         return check_loop_return(ret, *loop_ret, *loop_osl_errno);
354 }
355
356 static int print_global_summary(void)
357 {
358         char d[FORMATED_VALUE_SIZE], f[FORMATED_VALUE_SIZE],
359                 s[FORMATED_VALUE_SIZE];
360         enum enum_count_unit ud, uf;
361         enum enum_size_unit us;
362         int ret;
363
364         if (select_conf.no_global_summary_given)
365                 return 1;
366         ud = format_count_value(select_conf.count_unit_arg, num_dirs, 0, d);
367         uf = format_count_value(select_conf.count_unit_arg, num_files, 0, f);
368         us = format_size_value(select_conf.size_unit_arg, num_bytes, 0, s);
369
370         if (!select_conf.no_headers_given) {
371                 ret = output("Global summary "
372                         "(dirs(%c)/files(%c)/size(%c))\n",
373                         count_unit_abbrevs[ud],
374                         count_unit_abbrevs[uf],
375                         size_unit_abbrevs[us]
376                 );
377                 if (ret < 0)
378                         return ret;
379         }
380         return output("\t%s\t%s\t%s\n\n", d, f, s);
381 }
382
383 static int print_user_summary_line(struct user_info *ui, __a_unused void *data)
384 {
385         char formated_dir_count[FORMATED_VALUE_SIZE],
386                 formated_file_count[FORMATED_VALUE_SIZE],
387                 formated_bytes[FORMATED_VALUE_SIZE ];
388
389         format_count_value(select_conf.count_unit_arg, ui->dirs,
390                 select_conf.count_unit_arg == count_unit_arg_h,
391                 formated_dir_count);
392         format_count_value(select_conf.count_unit_arg, ui->files,
393                 select_conf.count_unit_arg == count_unit_arg_h,
394                 formated_file_count);
395         format_size_value(select_conf.size_unit_arg, ui->bytes,
396                 select_conf.size_unit_arg == size_unit_arg_h,
397                 formated_bytes);
398         return output("\t%s\t%u\t%s\t%s\t%s\n",
399                 ui->pw_name? ui->pw_name : "?",
400                 (unsigned)ui->uid,
401                 formated_dir_count,
402                 formated_file_count,
403                 formated_bytes
404         );
405 }
406
407 static int name_comp(const void *a, const void *b)
408 {
409         char *x = ((struct user_info *)a)->pw_name;
410         char *y = ((struct user_info *)b)->pw_name;
411
412         if (!x)
413                 return 1;
414         if (!y)
415                 return -1;
416         return strcmp(x, y);
417 }
418
419 static int uid_comp(const void *a, const void *b)
420 {
421         return -NUM_COMPARE(((struct user_info *)a)->uid,
422                 ((struct user_info *)b)->uid);
423 }
424
425 static int dir_count_comp(const void *a, const void *b)
426 {
427         return NUM_COMPARE(((struct user_info *)a)->dirs,
428                 ((struct user_info *)b)->dirs);
429 }
430
431 static int file_count_comp(const void *a, const void *b)
432 {
433         return NUM_COMPARE(((struct user_info *)a)->files,
434                 ((struct user_info *)b)->files);
435 }
436
437 static int size_comp(const void *a, const void *b)
438 {
439         return NUM_COMPARE(((struct user_info *)a)->bytes,
440                 ((struct user_info *)b)->bytes);
441 }
442
443 /*
444  * The comparators for sorting the user summary.
445  *
446  * This is an array of pointers to functions taking two constant void *
447  * pointers and returning an int.
448  */
449 static int (*summary_comparators[])(const void *, const void *) = {
450         [user_summary_sort_arg_name] = name_comp,
451         [user_summary_sort_arg_uid] = uid_comp,
452         [user_summary_sort_arg_dir_count] = dir_count_comp,
453         [user_summary_sort_arg_file_count] = file_count_comp,
454         [user_summary_sort_arg_size] = size_comp,
455 };
456
457 static int print_user_summary(void)
458 {
459         if (select_conf.no_user_summary_given)
460                 return 1;
461         if (!select_conf.no_headers_given) {
462                 int ret = output("User summary "
463                         "(pw_name/uid/dirs%s/files%s/size%s):\n",
464                         count_unit_buf, count_unit_buf, size_unit_buf);
465                 if (ret < 0)
466                         return ret;
467         }
468         sort_hash_table(summary_comparators[select_conf.user_summary_sort_arg]);
469         return for_each_admissible_user(print_user_summary_line, NULL);
470 }
471
472 static int print_user_list(struct user_info *ui, __a_unused void *data)
473 {
474         int ret;
475         struct user_stats_info usi;
476         enum enum_user_list ula = select_conf.user_list_arg;
477         int print_size_list = (ula == user_list_arg_size
478                 || ula == user_list_arg_both);
479
480         if (print_size_list) {
481                 usi.count = select_conf.limit_arg;
482                 usi.ui = ui;
483                 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
484                 if (!select_conf.no_headers_given) {
485                         ret = output("%s (uid %u), by size%s:\n",
486                                 ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
487                                 size_unit_buf);
488                         if (ret < 0)
489                                 return ret;
490                 }
491                 ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_stats_loop_function,
492                         &usi.ret, &usi.osl_errno);
493                 if (ret < 0)
494                         return ret;
495                 ret = output("\n");
496                 if (ret < 0)
497                         return ret;
498         }
499         if (ula == user_list_arg_file_count || ula == user_list_arg_both) {
500                 if (!select_conf.no_headers_given) {
501                         ret = output("%s (uid %u), by file count%s:\n",
502                                 ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
503                                 count_unit_buf);
504                         if (ret < 0)
505                                 return ret;
506                 }
507                 usi.count = select_conf.limit_arg,
508                 usi.ui = ui;
509                 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
510                 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
511                         &usi.ret, &usi.osl_errno);
512                 if (ret < 0)
513                         return ret;
514                 ret = output("\n");
515                 if (ret < 0)
516                         return ret;
517         }
518         if (ula == user_list_arg_none && !select_conf.no_user_summary_given) {
519                 usi.count = select_conf.limit_arg;
520                 usi.ui = ui;
521                 usi.flags = USF_COMPUTE_SUMMARY;
522                 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
523                         &usi.ret, &usi.osl_errno);
524                 if (ret < 0)
525                         return ret;
526         }
527         return 1;
528 }
529
530 static int print_user_lists(void)
531 {
532         return for_each_admissible_user(print_user_list, NULL);
533 }
534
535 static int print_global_lists(void)
536 {
537         struct global_stats_info gsi;
538         int ret;
539         enum enum_global_list gla = select_conf.global_list_arg;
540         int print_size_list = (gla == global_list_arg_size
541                 || gla == global_list_arg_both);
542
543         if (print_size_list) {
544                 gsi.count = select_conf.limit_arg;
545                 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY;
546                 if (!select_conf.no_headers_given) {
547                         ret = output("By size%s:\n", size_unit_buf);
548                         if (ret < 0)
549                                 return ret;
550                 }
551                 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
552                         global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
553                 if (ret < 0)
554                         return ret;
555                 ret = output("\n");
556                 if (ret < 0)
557                         return ret;
558         }
559         if (gla == global_list_arg_file_count || gla == global_list_arg_both) {
560                 gsi.count = select_conf.limit_arg;
561                 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
562                 if (!print_size_list)
563                         gsi.flags |= GSF_COMPUTE_SUMMARY;
564                 if (!select_conf.no_headers_given) {
565                         ret = output("By file count%s:\n", count_unit_buf);
566                         if (ret < 0)
567                                 return ret;
568                 }
569                 ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
570                         global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
571                 if (ret < 0)
572                         return ret;
573                 ret = output("\n");
574                 if (ret < 0)
575                         return ret;
576         }
577         if (gla == global_list_arg_none && !select_conf.no_global_summary_given) {
578                 /* must compute summary */
579                 gsi.count = select_conf.limit_arg;
580                 gsi.flags = GSF_COMPUTE_SUMMARY;
581                 ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
582                         global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
583                 if (ret < 0)
584                         return ret;
585         }
586         return 1;
587 }
588
589 static int print_statistics(void)
590 {
591         int ret;
592
593         ret = print_global_lists();
594         if (ret < 0)
595                 return ret;
596         ret = print_global_summary();
597         if (ret < 0)
598                 return ret;
599         ret = print_user_lists();
600         if (ret < 0)
601                 return ret;
602         ret = print_user_summary();
603         if (ret < 0)
604                 return ret;
605         return 1;
606 }
607
608 static int read_uid_file(struct uid_range *admissible_uids)
609 {
610         size_t size;
611         uint32_t n;
612         char *filename = get_uid_list_name(), *map;
613         int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
614         unsigned bits;
615
616         if (ret < 0) {
617                 INFO_LOG("failed to map %s\n", filename);
618                 free(filename);
619                 return ret;
620         }
621         num_uids = size / 4;
622         INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
623         free(filename);
624         /*
625          * Compute number of hash table bits. The hash table size must be a
626          * power of two and larger than the number of uids.
627          */
628         bits = 2;
629         while (1 << bits < num_uids)
630                 bits++;
631         create_hash_table(bits);
632         for (n = 0; n < num_uids; n++) {
633                 uint32_t uid = read_u32(map + n * sizeof(uid));
634                 ret = search_uid(uid, admissible_uids, OPEN_USER_TABLE, NULL);
635                 if (ret < 0)
636                         goto out;
637         }
638 out:
639         adu_munmap(map, size);
640         return ret;
641 }
642
643 int run_select_query(struct uid_range *admissible_uids)
644 {
645         int ret;
646
647         if (select_conf.output_given && strcmp(select_conf.output_arg, "-")) {
648                 output_file = fopen(select_conf.output_arg, "w");
649                 if (!output_file)
650                         return -ERRNO_TO_ERROR(errno);
651         } else
652                 output_file = stdout;
653
654         if (select_conf.count_unit_arg != count_unit_arg_h)
655                 count_unit_buf[1] = count_unit_abbrevs[select_conf.count_unit_arg];
656         else
657                 count_unit_buf[0] = '\0';
658         if (select_conf.size_unit_arg != size_unit_arg_h)
659                 size_unit_buf[1] = size_unit_abbrevs[select_conf.size_unit_arg];
660         else
661                 size_unit_buf[0] = '\0';
662
663         ret = open_dir_table(0);
664         if (ret < 0)
665                 goto out;
666         check_signals();
667         ret = read_uid_file(admissible_uids);
668         if (ret < 0)
669                 goto out;
670         check_signals();
671         ret = print_statistics();
672 out:
673         close_all_tables();
674         if (output_file != stdout)
675                 fclose(output_file);
676         return ret;
677 }
678
679 int com_select(void)
680 {
681         int ret;
682         struct uid_range *admissible_uids = NULL;
683
684         if (conf.select_options_given) {
685                 struct select_cmdline_parser_params params = {
686                         .override = 1,
687                         .initialize = 1,
688                         .check_required = 1,
689                         .check_ambiguity = 1,
690                         .print_errors = 1
691                 };
692
693                 if (select_cmdline_parser_string_ext(conf.select_options_arg,
694                         &select_conf, "select", &params))
695                         return -E_SYNTAX;
696                 ret = parse_uid_arg(select_conf.uid_arg, &admissible_uids);
697                 if (ret < 0)
698                         return ret;
699         }
700         return run_select_query(admissible_uids);
701 }