cd18beeb1e0f72e8bd8a10748f0f49fe0c5a9e4b
[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 <sys/types.h>
11 #include <regex.h>
12
13 #include "format.h"
14 #include "adu.h"
15 #include "gcc-compat.h"
16 #include "cmdline.h"
17 #include "fd.h"
18 #include "string.h"
19 #include "error.h"
20 #include "user.h"
21 #include "select.cmdline.h"
22
23 /* global list */
24 #define GLOBAL_LIST_ATOMS \
25 ATOM(size, SIZE) \
26 ATOM(files, COUNT) \
27 ATOM(dirname, STRING) \
28
29 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
30 struct atom global_list_atoms[] = {
31 GLOBAL_LIST_ATOMS
32 {.name = NULL}
33 };
34 #undef ATOM
35 #define ATOM(x, y) gla_ ## x,
36 enum global_list_atoms {GLOBAL_LIST_ATOMS};
37 #undef ATOM
38
39 /* global summary */
40 #define GLOBAL_SUMMARY_ATOMS \
41 ATOM(dirs, COUNT) \
42 ATOM(files, COUNT) \
43 ATOM(size, SIZE)
44
45 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
46 struct atom global_summary_atoms[] = {
47 GLOBAL_SUMMARY_ATOMS
48 {.name = NULL}
49 };
50 #undef ATOM
51 #define ATOM(x, y) gsa_ ## x,
52 enum global_summary_atoms {GLOBAL_SUMMARY_ATOMS};
53 #undef ATOM
54
55 /* user list */
56 #define USER_LIST_ATOMS \
57 ATOM(pw_name, STRING) \
58 ATOM(uid, ID) \
59 ATOM(size, SIZE) \
60 ATOM(files, COUNT) \
61 ATOM(dirname, STRING) \
62
63 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
64 struct atom user_list_atoms[] = {
65 USER_LIST_ATOMS
66 {.name = NULL}
67 };
68 #undef ATOM
69 #define ATOM(x, y) ula_ ## x,
70 enum user_list_atoms {USER_LIST_ATOMS};
71 #undef ATOM
72
73 /* user list header */
74 #define USER_LIST_HEADER_ATOMS \
75 ATOM(pw_name, STRING) \
76 ATOM(uid, ID)
77
78 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
79 struct atom user_list_header_atoms[] = {
80 USER_LIST_HEADER_ATOMS
81 {.name = NULL}
82 };
83 #undef ATOM
84 #define ATOM(x, y) ulha_ ## x,
85 enum user_list_header_atoms {USER_LIST_HEADER_ATOMS};
86 #undef ATOM
87
88 /* user summary */
89 #define USER_SUMMARY_ATOMS \
90 ATOM(pw_name, STRING) \
91 ATOM(uid, ID) \
92 ATOM(dirs, COUNT) \
93 ATOM(files, COUNT) \
94 ATOM(size, SIZE)
95
96 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
97 struct atom user_summary_atoms[] = {
98 USER_SUMMARY_ATOMS
99 {.name = NULL}
100 };
101 #undef ATOM
102 #define ATOM(x, y) usa_ ## x,
103 enum user_summary_atoms {USER_SUMMARY_ATOMS};
104 #undef ATOM
105
106 struct global_list_info {
107 uint32_t count;
108 int ret;
109 int osl_errno;
110 struct format_info *fi;
111 regex_t *preg;
112 int inverse_matching;
113 };
114
115 struct global_summary_info {
116 /** Global dir count. */
117 uint64_t num_dirs;
118 /** Global files count. */
119 uint64_t num_files;
120 /** Global bytes count. */
121 uint64_t num_bytes;
122 regex_t *preg;
123 int inverse_matching;
124 int ret;
125 int osl_errno;
126 };
127
128 struct user_list_info {
129 uint32_t count;
130 struct user_info *ui;
131 struct format_info *fi;
132 regex_t *preg;
133 int inverse_matching;
134 int ret;
135 int osl_errno;
136 };
137
138 struct user_list_format_info {
139 struct format_info *fi;
140 struct format_info *header_fi;
141 };
142
143 struct user_summary_info {
144 struct user_info *ui;
145 int ret;
146 int osl_errno;
147 regex_t *preg;
148 int inverse_matching;
149 };
150
151 struct user_summary_line_info {
152 struct format_info *fi;
153 uint32_t count;
154 };
155
156 static FILE *output_file;
157
158 __printf_1_2 static int output(const char const *fmt, ...)
159 {
160 va_list argp;
161 int ret;
162
163 va_start(argp, fmt);
164 ret = vfprintf(output_file, fmt, argp);
165 va_end(argp);
166 return ret < 0? -E_OUTPUT : 1;
167 }
168
169 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
170 {
171 char *result = NULL, *tmp;
172 struct osl_row *row;
173 uint64_t val = *dirnum;
174 struct osl_object obj;
175 int ret;
176 char *pfx;
177
178 again:
179 obj.data = &val;
180 obj.size = sizeof(val);
181 ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
182 if (ret < 0)
183 goto out;
184 ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
185 if (ret < 0)
186 goto out;
187 val = *(uint64_t *)obj.data;
188 ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
189 if (ret < 0)
190 goto out;
191 pfx = (select_conf.print_base_dir_given || val)? (char *)obj.data : ".";
192 tmp = make_message("%s/%s", pfx, result? result : "");
193 free(result);
194 result = tmp;
195 if (val)
196 goto again;
197 out:
198 if (ret < 0) {
199 free(result);
200 *name = NULL;
201 } else {
202 assert(result);
203 *name = result;
204 }
205 return ret;
206 }
207
208 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
209 {
210 struct osl_object obj;
211 int ret;
212
213 *name = NULL;
214 ret = osl(osl_get_object(dir_table, dir_table_row, DT_NUM, &obj));
215 if (ret < 0)
216 return ret;
217 return get_dir_name_by_number((uint64_t *)obj.data, name);
218 }
219
220 static int get_dir_name_of_user_row(struct osl_row *user_table_row,
221 struct user_info *ui, char **dirname)
222 {
223 struct osl_object obj;
224 int ret = osl(osl_get_object(ui->table, user_table_row,
225 UT_DIR_NUM, &obj));
226
227 if (ret < 0)
228 return ret;
229 return get_dir_name_by_number((uint64_t *)obj.data, dirname);
230 }
231
232 static int get_num_files_of_row(struct osl_row *row, uint64_t *num_files)
233 {
234 struct osl_object obj;
235 int ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
236 if (ret < 0)
237 return ret;
238 *num_files = *(uint64_t *)obj.data;
239 return 1;
240 }
241
242 static int get_num_user_files(struct osl_row *row, struct user_info *ui,
243 uint64_t *num_files)
244 {
245 struct osl_object obj;
246 int ret = osl(osl_get_object(ui->table, row, UT_FILES, &obj));
247
248 if (ret < 0)
249 return ret;
250 *num_files = *(uint64_t *)obj.data;
251 return 1;
252 }
253
254 static int get_num_bytes_of_row(struct osl_row *row, uint64_t *num_bytes)
255 {
256 struct osl_object obj;
257 int ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
258 if (ret < 0)
259 return ret;
260 *num_bytes = *(uint64_t *)obj.data;
261 return 1;
262 }
263
264 static int get_num_user_bytes(struct osl_row *row, struct user_info *ui,
265 uint64_t *num_bytes)
266 {
267 struct osl_object obj;
268 int ret = osl(osl_get_object(ui->table, row, UT_BYTES, &obj));
269
270 if (ret < 0)
271 return ret;
272 *num_bytes = *(uint64_t *)obj.data;
273 return 1;
274 }
275
276 static void free_regex(regex_t *preg)
277 {
278 if (!preg)
279 return;
280 regfree(preg);
281 free(preg);
282 }
283
284 static int compile_regex(regex_t **preg, int *invert)
285 {
286 int ret;
287 size_t size;
288 char *buf, *p = select_conf.pattern_arg;
289
290 if (!select_conf.pattern_given || !p[0]) {
291 *preg = NULL;
292 return 0;
293 }
294 if (p[0] == '!') {
295 if (!p[1]) {
296 *preg = NULL;
297 return -E_REGEX;
298 }
299 *invert = 1;
300 p++;
301 } else
302 *invert = 0;
303 *preg = adu_malloc(sizeof(regex_t));
304 ret = regcomp(*preg, p, 0);
305 if (!ret)
306 return 1;
307 size = regerror(ret, *preg, NULL, 0);
308 buf = adu_malloc(size);
309 regerror(ret, *preg, buf, size);
310 ERROR_LOG("%s\n", buf);
311 free(buf);
312 free_regex(*preg);
313 *preg = NULL;
314 return -E_REGEX;
315 }
316
317 static int dir_is_admissible(char *dirname, regex_t *preg, int inverse_matching)
318 {
319 int ret;
320
321 if (!preg)
322 return 1;
323 ret = regexec(preg, dirname, 0, NULL, 0);
324 if (ret == REG_NOMATCH && !inverse_matching)
325 return 0;
326 if (ret != REG_NOMATCH && inverse_matching)
327 return 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 global_summary_loop_function(struct osl_row *row, void *data)
357 {
358 struct global_summary_info *gsi = data;
359 int ret;
360 uint64_t num;
361
362 if (gsi->preg) {
363 char *dirname;
364 ret = get_dir_name_of_row(row, &dirname);
365 if (ret < 0)
366 goto err;
367 ret = dir_is_admissible(dirname, gsi->preg, gsi->inverse_matching);
368 free(dirname);
369 if (!ret)
370 return 1;
371 }
372
373 ret = get_num_files_of_row(row, &num);
374 if (ret < 0)
375 goto err;
376 gsi->num_files += num;
377
378 ret = get_num_bytes_of_row(row, &num);
379 if (ret < 0)
380 goto err;
381 gsi->num_bytes += num;
382 gsi->num_dirs++;
383 return 1;
384 err:
385 gsi->ret = ret;
386 gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
387 return ret;
388 }
389
390 static int print_global_summary(struct format_info *fi)
391 {
392 int ret;
393 char *buf;
394 struct global_summary_info gsi = {.num_dirs = 0};
395 char *header = select_conf.header_given? select_conf.header_arg :
396 "Global summary\n";
397
398 union atom_value values[] = {
399 [gsa_dirs] = {.num_value = 0ULL},
400 [gsa_files] = {.num_value = 0ULL},
401 [gsa_size] = {.num_value = 0ULL}
402 };
403
404 ret = compile_regex(&gsi.preg, &gsi.inverse_matching);
405 if (ret < 0)
406 return ret;
407 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
408 global_summary_loop_function, &gsi.ret, &gsi.osl_errno);
409 free_regex(gsi.preg);
410 if (ret < 0)
411 return ret;
412 values[gsa_dirs].num_value = (long long unsigned)gsi.num_dirs;
413 values[gsa_files].num_value = (long long unsigned)gsi.num_files;
414 values[gsa_size].num_value = (long long unsigned)gsi.num_bytes;
415
416 ret = output("%s", header);
417 if (ret < 0)
418 return ret;
419 buf = format_items(fi, values);
420 ret = output("%s", buf);
421 free(buf);
422 return ret;
423 }
424
425 static int user_summary_loop_function(struct osl_row *row, void *data)
426 {
427 struct user_summary_info *usi = data;
428 uint64_t num;
429 int ret;
430
431 if (usi->preg) {
432 char *dirname;
433 ret = get_dir_name_of_row(row, &dirname);
434 if (ret < 0)
435 goto err;
436 ret = dir_is_admissible(dirname, usi->preg, usi->inverse_matching);
437 free(dirname);
438 if (!ret)
439 return 1;
440 }
441 ret = get_num_user_files(row, usi->ui, &num);
442 if (ret < 0)
443 goto err;
444 usi->ui->files += num;
445 ret = get_num_user_bytes(row, usi->ui, &num);
446 if (ret < 0)
447 goto err;
448 usi->ui->bytes += num;
449 usi->ui->dirs++;
450 return 1;
451 err:
452 usi->ret = ret;
453 usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
454 return ret;
455 }
456
457 static int compute_user_summary(struct user_info *ui, __a_unused void *data)
458 {
459 struct user_summary_info usi = {.ui = ui};
460 int ret = compile_regex(&usi.preg, &usi.inverse_matching);
461
462 if (ret < 0)
463 return ret;
464 ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_summary_loop_function,
465 &usi.ret, &usi.osl_errno);
466 free_regex(usi.preg);
467 return ret;
468 }
469
470 static int print_user_summary_line(struct user_info *ui, void *data)
471 {
472 struct user_summary_line_info *usli = data;
473 union atom_value values[] = {
474 [usa_pw_name] = {.string_value = ui->pw_name?
475 ui->pw_name : "?"},
476 [usa_uid] = {.num_value = (long long unsigned)ui->uid},
477 [usa_dirs] = {.num_value = (long long unsigned)ui->dirs},
478 [usa_files] = {.num_value = (long long unsigned)ui->files},
479 [usa_size] = {.num_value = (long long unsigned)ui->bytes}
480 };
481 char *buf;
482 int ret = -E_LOOP_COMPLETE;
483
484 if (!usli->count)
485 return ret;
486
487 buf = format_items(usli->fi, values);
488 ret = output("%s", buf);
489 free(buf);
490 usli->count--;
491 return ret;
492 }
493
494 static int name_comp(struct user_info *a, struct user_info *b)
495 {
496 char *x = a->pw_name;
497 char *y = b->pw_name;
498
499 if (!x)
500 return 1;
501 if (!y)
502 return -1;
503 return strcmp(x, y);
504 }
505
506 static int uid_comp(struct user_info *a, struct user_info *b)
507 {
508 return -NUM_COMPARE(a->uid, b->uid);
509 }
510
511 static int dir_count_comp(struct user_info *a, struct user_info *b)
512 {
513 return NUM_COMPARE(a->dirs, b->dirs);
514 }
515
516 static int file_count_comp(struct user_info *a, struct user_info *b)
517 {
518 return NUM_COMPARE(a->files, b->files);
519 }
520
521 static int size_comp(struct user_info *a, struct user_info *b)
522 {
523 return NUM_COMPARE(a->bytes, b->bytes);
524 }
525
526 static int print_user_summary(struct format_info *fi)
527 {
528 int ret;
529 int (*comp)(struct user_info *a, struct user_info *b);
530 struct user_summary_line_info usli = {
531 .fi = fi,
532 .count = select_conf.limit_arg
533 };
534 char *header = select_conf.header_given? select_conf.header_arg :
535 "User summary\n";
536
537 ret = output("%s", header);
538 if (ret < 0)
539 return ret;
540 ret = for_each_admissible_user(compute_user_summary, NULL);
541 if (ret < 0)
542 return ret;
543 switch (select_conf.user_summary_sort_arg) {
544 case user_summary_sort_arg_name:
545 comp = name_comp;
546 break;
547 case user_summary_sort_arg_uid:
548 comp = uid_comp;
549 break;
550 case user_summary_sort_arg_dir_count:
551 comp = dir_count_comp;
552 break;
553 case user_summary_sort_arg_file_count:
554 comp = file_count_comp;
555 break;
556 case user_summary_sort_arg_size:
557 comp = size_comp;
558 break;
559 }
560 sort_hash_table(comp);
561 ret = for_each_admissible_user(print_user_summary_line, &usli);
562 if (ret == -E_LOOP_COMPLETE)
563 ret = 1;
564 return ret;
565 }
566
567 static int user_list_loop_function(struct osl_row *row, void *data)
568 {
569 struct user_list_info *uli = data;
570 union atom_value values[] = {
571 [ula_pw_name] = {.string_value = uli->ui->pw_name?
572 uli->ui->pw_name : "?"},
573 [ula_uid] = {.num_value = (long long unsigned)uli->ui->uid},
574 [ula_files] = {.num_value = 0ULL},
575 [ula_size] = {.num_value = 0ULL},
576 [ula_dirname] = {.string_value = NULL}
577 };
578 uint64_t num;
579 int ret;
580 char *dirname = NULL, *buf;
581
582 check_signals();
583 ret = -E_LOOP_COMPLETE;
584 if (!uli->count)
585 goto err;
586
587 ret = get_dir_name_of_user_row(row, uli->ui, &dirname);
588 if (ret < 0)
589 goto err;
590 if (!dir_is_admissible(dirname, uli->preg, uli->inverse_matching)) {
591 free(dirname);
592 return 1;
593 }
594 values[ula_dirname].string_value = dirname;
595
596 ret = get_num_user_files(row, uli->ui, &num);
597 if (ret < 0)
598 goto err;
599 values[ula_files].num_value = num;
600
601 ret = get_num_user_bytes(row, uli->ui, &num);
602 if (ret < 0)
603 goto err;
604 values[ula_size].num_value = num;
605
606 buf = format_items(uli->fi, values);
607 free(dirname);
608 dirname = NULL;
609 ret = output("%s", buf);
610 free(buf);
611 if (ret < 0)
612 goto err;
613 uli->count--;
614 return ret;
615 err:
616 free(dirname);
617 uli->ret = ret;
618 uli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
619 return ret;
620 }
621
622 static int print_user_list(struct user_info *ui, void *data)
623 {
624 struct user_list_format_info *ulfi = data;
625 int ret;
626 enum user_table_columns sort_column;
627 struct user_list_info uli = {
628 .ui = ui,
629 .fi = ulfi->fi,
630 .count = select_conf.limit_arg
631 };
632 union atom_value header_values[] = {
633 [ulha_uid] = {.num_value = (long long unsigned)ui->uid},
634 [ulha_pw_name] = {.string_value = ui->pw_name?
635 ui->pw_name : "?"}
636 };
637 char *buf = format_items(ulfi->header_fi, header_values);
638
639 ret = output("%s", buf);
640 free(buf);
641 if (ret < 0)
642 return ret;
643 if (select_conf.list_sort_arg == list_sort_arg_file_count)
644 sort_column = UT_FILES;
645 else
646 sort_column = UT_BYTES;
647
648 ret = compile_regex(&uli.preg, &uli.inverse_matching);
649 if (ret < 0)
650 return ret;
651 ret = adu_loop_reverse(ui->table, sort_column, &uli,
652 user_list_loop_function, &uli.ret, &uli.osl_errno);
653 free_regex(uli.preg);
654 return ret;
655 }
656
657 static int print_user_lists(struct format_info *fi)
658 {
659 struct user_list_format_info ulfi = {.fi = fi};
660 char *header_fmt = select_conf.header_given?
661 select_conf.header_arg : "uid %(uid)(%(pw_name)):\n";
662 int ret = parse_format_string(header_fmt,
663 user_list_header_atoms, &ulfi.header_fi);
664 if (ret < 0)
665 return ret;
666 ret = for_each_admissible_user(print_user_list, &ulfi);
667 free_format_info(ulfi.header_fi);
668 return ret;
669 }
670
671 static int global_list_loop_function(struct osl_row *row, void *data)
672 {
673 struct global_list_info *gli = data;
674 union atom_value values[] = {
675 [gla_size] = {.num_value = 0ULL},
676 [gla_files] = {.num_value = 0ULL},
677 [gla_dirname] = {.string_value = NULL}
678 };
679 uint64_t num_files, num_bytes;
680 char *dirname = NULL, *buf;
681 int ret;
682
683 check_signals();
684 ret = -E_LOOP_COMPLETE;
685 if (!gli->count)
686 goto err;
687
688 ret = get_dir_name_of_row(row, &dirname);
689 if (ret < 0)
690 goto err;
691 if (!dir_is_admissible(dirname, gli->preg, gli->inverse_matching)) {
692 free(dirname);
693 return 1;
694 }
695 values[gla_dirname].string_value = dirname;
696
697 ret = get_num_files_of_row(row, &num_files);
698 if (ret < 0)
699 goto err;
700 values[gla_files].num_value = (long long unsigned)num_files;
701
702 ret = get_num_bytes_of_row(row, &num_bytes);
703 if (ret < 0)
704 goto err;
705 values[gla_size].num_value = (long long unsigned)num_bytes;
706
707 buf = format_items(gli->fi, values);
708 free(dirname);
709 dirname = NULL;
710 ret = output("%s", buf);
711 free(buf);
712 if (ret < 0)
713 goto err;
714 if (gli->count > 0)
715 gli->count--;
716 return ret;
717 err:
718 free(dirname);
719 gli->ret = ret;
720 gli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
721 return -1;
722 }
723
724 static int print_global_list(struct format_info *fi)
725 {
726 int ret;
727 enum dir_table_columns sort_column;
728 struct global_list_info gli = {
729 .fi = fi,
730 .count = select_conf.limit_arg
731 };
732 char *header = select_conf.header_given?
733 select_conf.header_arg : "Global list\n";
734
735 ret = output("%s", header);
736 if (ret < 0)
737 return ret;
738 if (select_conf.list_sort_arg == list_sort_arg_file_count)
739 sort_column = DT_FILES;
740 else
741 sort_column = DT_BYTES;
742 ret = compile_regex(&gli.preg, &gli.inverse_matching);
743 if (ret < 0)
744 return ret;
745 ret = adu_loop_reverse(dir_table, sort_column, &gli,
746 global_list_loop_function, &gli.ret, &gli.osl_errno);
747 free_regex(gli.preg);
748 return ret;
749 }
750
751 static int print_statistics(struct format_info *fi)
752 {
753 switch (select_conf.select_mode_arg) {
754 case select_mode_arg_global_list:
755 return print_global_list(fi);
756 case select_mode_arg_global_summary:
757 return print_global_summary(fi);
758 case select_mode_arg_user_list:
759 return print_user_lists(fi);
760 case select_mode_arg_user_summary:
761 return print_user_summary(fi);
762 };
763 ERROR_LOG("bad select mode\n");
764 return -ERRNO_TO_ERROR(EINVAL);
765 }
766
767 static int open_pipe(char *path)
768 {
769 int p[2], ret, argc;
770 char **argv;
771
772 ret = pipe(p);
773 if (ret < 0)
774 return ERRNO_TO_ERROR(errno);
775 ret = fork();
776 if (ret < 0)
777 return ERRNO_TO_ERROR(errno);
778 if (ret) { /* parent */
779 DEBUG_LOG("created process %d\n", ret);
780 close(p[0]);
781 output_file = fdopen(p[1], "w");
782 if (!output_file)
783 return ERRNO_TO_ERROR(errno);
784 return 1;
785 }
786 close(p[1]);
787 if (p[0] != STDIN_FILENO)
788 dup2(p[0], STDIN_FILENO);
789 DEBUG_LOG("executing %s\n", path);
790 argc = split_args(path, &argv, " \t");
791 execvp(argv[0], argv);
792 ERROR_LOG("error executing %s: %s\n", path,
793 adu_strerror(ERRNO_TO_ERROR(errno)));
794 _exit(EXIT_FAILURE);
795 }
796
797 static int open_output_stream(void)
798 {
799 char *p;
800 int ret, flags = O_WRONLY | O_CREAT;
801
802 if (!select_conf.output_given)
803 goto use_stdout;
804 p = select_conf.output_arg;
805 switch (p[0]) {
806 case '\0': /* empty string */
807 goto bad_output_arg;
808 case '-':
809 if (!p[1]) /* "-" means stdout */
810 goto use_stdout;
811 /* string starting with a dash */
812 flags |= O_EXCL;
813 goto open_file;
814 case '>':
815 if (!p[1]) /* ">" is invalid */
816 goto bad_output_arg;
817 if (p[1] != '>') {
818 p++;
819 flags |= O_TRUNC;
820 goto open_file;
821 }
822 /* string starting with ">>" */
823 if (!p[2]) /* ">>" is invalid */
824 goto bad_output_arg;
825 flags |= O_APPEND;
826 p += 2;
827 goto open_file;
828 case '|':
829 if (!p[1]) /* "|" is invalid */
830 goto bad_output_arg;
831 p++;
832 return open_pipe(p);
833 default: /* args starts with no magic character */
834 flags |= O_EXCL;
835 goto open_file;
836 }
837 use_stdout:
838 output_file = stdout;
839 return 1;
840 bad_output_arg:
841 output_file = NULL;
842 return -E_BAD_OUTPUT_ARG;
843 open_file:
844 /*
845 * glibc's 'x' mode to fopen is not portable, so use open() and
846 * fdopen().
847 */
848 ret = open(p, flags, 0644);
849 if (ret < 0)
850 return -ERRNO_TO_ERROR(errno);
851 output_file = fdopen(ret, "w");
852 if (!output_file)
853 return -ERRNO_TO_ERROR(errno);
854 return 1;
855 }
856
857 int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
858 {
859 int ret = open_output_stream();
860
861 if (ret < 0)
862 goto out;
863 ret = open_dir_table(0);
864 if (ret < 0)
865 goto out;
866 check_signals();
867 ret = open_admissible_user_tables(admissible_uids);
868 if (ret < 0)
869 goto out;
870 check_signals();
871 ret = print_statistics(fi);
872 out:
873 if (output_file && output_file != stdout) {
874 fclose(output_file);
875 output_file = NULL;
876 }
877 return ret;
878 }
879
880 #define GLOBAL_LIST_DFLT_FMT "%(size:r:8) %(files:r:8) %(dirname)\n"
881 #define GLOBAL_SUMMARY_DFLT_FMT "#directories: %(dirs), #files: %(files), size: %(size)\n\n"
882 #define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n"
883 #define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:5) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
884
885 static int setup_format_string(char *fmt, struct format_info **fi)
886 {
887 struct atom *atoms;
888
889 if (!fmt)
890 INFO_LOG("using default format string\n");
891 switch (select_conf.select_mode_arg) {
892 case select_mode_arg_global_list:
893 if (!fmt)
894 fmt = GLOBAL_LIST_DFLT_FMT;
895 atoms = global_list_atoms;
896 break;
897 case select_mode_arg_global_summary:
898 if (!fmt)
899 fmt = GLOBAL_SUMMARY_DFLT_FMT;
900 atoms = global_summary_atoms;
901 break;
902 case select_mode_arg_user_list:
903 if (!fmt)
904 fmt = USER_LIST_DFLT_FMT;
905 atoms = user_list_atoms;
906 break;
907 case select_mode_arg_user_summary:
908 if (!fmt)
909 fmt = USER_SUMMARY_DFLT_FMT;
910 atoms = user_summary_atoms;
911 break;
912 default:
913 ERROR_LOG("bad select mode\n");
914 return -ERRNO_TO_ERROR(EINVAL);
915 };
916 INFO_LOG("format string: %s\n", fmt);
917 return parse_format_string(fmt, atoms, fi);
918 }
919
920 /* return: < 0: error, >0: OK, == 0: help given */
921 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
922 struct uid_range **admissible_uids, struct format_info **fi)
923 {
924 int ret, num_uid_ranges;
925 const char **line;
926 char *fmt = NULL;
927
928 if (string) {
929 int argc;
930 char **argv;
931
932 ret = create_argv(string, &argv);
933 if (ret < 0)
934 return ret;
935 argc = ret;
936 ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
937 free_argv(argv);
938 if (ret)
939 return -E_SYNTAX;
940 if (select_conf.help_given || select_conf.detailed_help_given)
941 goto help;
942 fmt = select_conf.format_arg;
943 }
944 ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
945 if (ret < 0)
946 return ret;
947 num_uid_ranges = ret;
948 ret = append_users(select_conf.user_arg, select_conf.user_given,
949 admissible_uids, num_uid_ranges);
950 if (ret < 0)
951 return ret;
952 return setup_format_string(fmt, fi);
953 help:
954 line = select_conf.detailed_help_given?
955 select_args_info_detailed_help : select_args_info_help;
956 if (!output_file)
957 output_file = stdout;
958 for (; *line; line++) {
959 ret = output("%s\n", *line);
960 if (ret < 0)
961 return ret;
962 }
963 return 0;
964 }
965
966 int com_select(void)
967 {
968 struct uid_range *admissible_uids = NULL;
969 int ret;
970 struct format_info *fi;
971 struct select_cmdline_parser_params params = {
972 .override = 1,
973 .initialize = 1,
974 .check_required = 1,
975 .check_ambiguity = 1,
976 .print_errors = 1
977 };
978
979 ret = parse_select_options(conf.select_options_arg, &params,
980 &admissible_uids, &fi);
981 if (ret > 0) {
982 ret = read_uid_file();
983 if (ret < 0)
984 goto out;
985 ret = run_select_query(admissible_uids, fi);
986 free_format_info(fi);
987 }
988 out:
989 select_cmdline_parser_free(&select_conf);
990 return ret;
991 }