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