X-Git-Url: http://git.tuebingen.mpg.de/?p=adu.git;a=blobdiff_plain;f=select.c;h=fd25da48b48d264a896abd74099fe56375c5d8e6;hp=46b5372492611d3b8ed315cf203372dae41ba580;hb=43b8c3a61559d52326fff1b6661904af470d9225;hpb=4113e8c585d3da46bfa5326b866621cff854a737 diff --git a/select.c b/select.c index 46b5372..fd25da4 100644 --- a/select.c +++ b/select.c @@ -366,10 +366,10 @@ static int print_user_summary_line(struct user_info *ui, void *data) return ret; } -static int name_comp(const void *a, const void *b) +static int name_comp(struct user_info *a, struct user_info *b) { - char *x = ((struct user_info *)a)->pw_name; - char *y = ((struct user_info *)b)->pw_name; + char *x = a->pw_name; + char *y = b->pw_name; if (!x) return 1; @@ -378,55 +378,57 @@ static int name_comp(const void *a, const void *b) return strcmp(x, y); } -static int uid_comp(const void *a, const void *b) +static int uid_comp(struct user_info *a, struct user_info *b) { - return -NUM_COMPARE(((struct user_info *)a)->uid, - ((struct user_info *)b)->uid); + return -NUM_COMPARE(a->uid, b->uid); } -static int dir_count_comp(const void *a, const void *b) +static int dir_count_comp(struct user_info *a, struct user_info *b) { - return NUM_COMPARE(((struct user_info *)a)->dirs, - ((struct user_info *)b)->dirs); + return NUM_COMPARE(a->dirs, b->dirs); } -static int file_count_comp(const void *a, const void *b) +static int file_count_comp(struct user_info *a, struct user_info *b) { - return NUM_COMPARE(((struct user_info *)a)->files, - ((struct user_info *)b)->files); + return NUM_COMPARE(a->files, b->files); } -static int size_comp(const void *a, const void *b) +static int size_comp(struct user_info *a, struct user_info *b) { - return NUM_COMPARE(((struct user_info *)a)->bytes, - ((struct user_info *)b)->bytes); + return NUM_COMPARE(a->bytes, b->bytes); } static int print_user_summary(struct format_info *fi) { - /* - * The comparators for sorting the user summary. - * - * This is an array of pointers to functions taking two constant void * - * pointers and returning an int. - */ - static int (*summary_comparators[])(const void *, const void *) = { - [user_summary_sort_arg_name] = name_comp, - [user_summary_sort_arg_uid] = uid_comp, - [user_summary_sort_arg_dir_count] = dir_count_comp, - [user_summary_sort_arg_file_count] = file_count_comp, - [user_summary_sort_arg_size] = size_comp, - }; + int ret; + int (*comp)(struct user_info *a, struct user_info *b); if (!select_conf.no_headers_given) { - int ret = output("User summary\n"); + ret = output("User summary\n"); if (ret < 0) return ret; } - int ret = for_each_admissible_user(compute_user_summary, fi); + ret = for_each_admissible_user(compute_user_summary, NULL); if (ret < 0) return ret; - sort_hash_table(summary_comparators[select_conf.user_summary_sort_arg]); + switch (select_conf.user_summary_sort_arg) { + case user_summary_sort_arg_name: + comp = name_comp; + break; + case user_summary_sort_arg_uid: + comp = uid_comp; + break; + case user_summary_sort_arg_dir_count: + comp = dir_count_comp; + break; + case user_summary_sort_arg_file_count: + comp = file_count_comp; + break; + case user_summary_sort_arg_size: + comp = size_comp; + break; + } + sort_hash_table(comp); return for_each_admissible_user(print_user_summary_line, fi); } @@ -583,33 +585,119 @@ static int print_statistics(struct format_info *fi) return print_user_summary(fi); }; ERROR_LOG("bad select mode\n"); - return -ERRNO_TO_ERROR(-EINVAL); + return -ERRNO_TO_ERROR(EINVAL); } -int run_select_query(struct uid_range *admissible_uids, struct format_info *fi) +static int open_pipe(char *path) { - int ret; + int p[2], ret, argc; + char **argv; - if (select_conf.output_given && strcmp(select_conf.output_arg, "-")) { - output_file = fopen(select_conf.output_arg, "w"); + ret = pipe(p); + if (ret < 0) + return ERRNO_TO_ERROR(errno); + ret = fork(); + if (ret < 0) + return ERRNO_TO_ERROR(errno); + if (ret) { /* parent */ + DEBUG_LOG("created process %d\n", ret); + close(p[0]); + output_file = fdopen(p[1], "w"); if (!output_file) - return -ERRNO_TO_ERROR(errno); - } else - output_file = stdout; + return ERRNO_TO_ERROR(errno); + return 1; + } + close(p[1]); + if (p[0] != STDIN_FILENO) + dup2(p[0], STDIN_FILENO); + DEBUG_LOG("executing %s\n", path); + argc = split_args(path, &argv, " \t"); + execvp(argv[0], argv); + ERROR_LOG("error executing %s: %s\n", path, + adu_strerror(ERRNO_TO_ERROR(errno))); + _exit(EXIT_FAILURE); +} +static int open_output_stream(void) +{ + char *p; + int ret, flags = O_WRONLY | O_CREAT; + + if (!select_conf.output_given) + goto use_stdout; + p = select_conf.output_arg; + switch (p[0]) { + case '\0': /* empty string */ + goto bad_output_arg; + case '-': + if (!p[1]) /* "-" means stdout */ + goto use_stdout; + /* string starting with a dash */ + flags |= O_EXCL; + goto open_file; + case '>': + if (!p[1]) /* ">" is invalid */ + goto bad_output_arg; + if (p[1] != '>') { + p++; + flags |= O_TRUNC; + goto open_file; + } + /* string starting with ">>" */ + if (!p[2]) /* ">>" is invalid */ + goto bad_output_arg; + flags |= O_APPEND; + p += 2; + goto open_file; + case '|': + if (!p[1]) /* "|" is invalid */ + goto bad_output_arg; + p++; + return open_pipe(p); + default: /* args starts with no magic character */ + flags |= O_EXCL; + goto open_file; + } +use_stdout: + output_file = stdout; + return 1; +bad_output_arg: + output_file = NULL; + return -E_BAD_OUTPUT_ARG; +open_file: + /* + * glibc's 'x' mode to fopen is not portable, so use open() and + * fdopen(). + */ + ret = open(p, flags, 0644); + if (ret < 0) + return -ERRNO_TO_ERROR(errno); + output_file = fdopen(ret, "w"); + if (!output_file) + return -ERRNO_TO_ERROR(errno); + return 1; +} + +int run_select_query(struct uid_range *admissible_uids, struct format_info *fi) +{ + int ret = open_output_stream(); + + if (ret < 0) + goto out; ret = open_dir_table(0); if (ret < 0) goto out; check_signals(); - ret = read_uid_file(admissible_uids); + ret = open_admissible_user_tables(admissible_uids); if (ret < 0) goto out; check_signals(); ret = print_statistics(fi); out: - close_all_tables(); - if (output_file != stdout) + if (output_file && output_file != stdout) { fclose(output_file); + output_file = NULL; + } return ret; } @@ -647,7 +735,7 @@ static int setup_format_string(char *fmt, struct format_info **fi) break; default: ERROR_LOG("bad select mode\n"); - return -ERRNO_TO_ERROR(-EINVAL); + return -ERRNO_TO_ERROR(EINVAL); }; INFO_LOG("format string: %s\n", fmt); return parse_format_string(fmt, atoms, fi); @@ -657,7 +745,7 @@ static int setup_format_string(char *fmt, struct format_info **fi) int parse_select_options(char *string, struct select_cmdline_parser_params *params, struct uid_range **admissible_uids, struct format_info **fi) { - int ret; + int ret, num_uid_ranges; const char **line; char *fmt = NULL; @@ -678,6 +766,11 @@ int parse_select_options(char *string, struct select_cmdline_parser_params *para fmt = select_conf.format_arg; } ret = parse_uid_arg(select_conf.uid_arg, admissible_uids); + if (ret < 0) + return ret; + num_uid_ranges = ret; + ret = append_users(select_conf.user_arg, select_conf.user_given, + admissible_uids, num_uid_ranges); if (ret < 0) return ret; return setup_format_string(fmt, fi); @@ -707,12 +800,16 @@ int com_select(void) .print_errors = 1 }; - select_cmdline_parser_init(&select_conf); ret = parse_select_options(conf.select_options_arg, ¶ms, &admissible_uids, &fi); - if (ret <= 0) /* do not run query if help was given */ - return ret; - ret = run_select_query(admissible_uids, fi); - free_format_info(fi); + if (ret > 0) { + ret = read_uid_file(); + if (ret < 0) + goto out; + ret = run_select_query(admissible_uids, fi); + free_format_info(fi); + } +out: + select_cmdline_parser_free(&select_conf); return ret; }