#include "fd.h"
#include "string.h"
#include "error.h"
-#include "portable_io.h"
+#include "user.h"
+#include "select.cmdline.h"
/* global list */
#define GLOBAL_LIST_ATOMS \
return print_user_summary(fi);
};
ERROR_LOG("bad select mode\n");
- return -ERRNO_TO_ERROR(-EINVAL);
+ return -ERRNO_TO_ERROR(EINVAL);
}
-static int read_uid_file(struct uid_range *admissible_uids)
+static int open_pipe(char *path)
{
- size_t size;
- uint32_t n;
- char *filename = get_uid_list_name(), *map;
- int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
- unsigned bits;
+ int p[2], ret, argc;
+ char **argv;
- if (ret < 0) {
- INFO_LOG("failed to map %s\n", filename);
- free(filename);
- return ret;
+ 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);
+ 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;
}
- num_uids = size / 4;
- INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
- free(filename);
+use_stdout:
+ output_file = stdout;
+ return 1;
+bad_output_arg:
+ output_file = NULL;
+ return -E_BAD_OUTPUT_ARG;
+open_file:
/*
- * Compute number of hash table bits. The hash table size must be a
- * power of two and larger than the number of uids.
+ * glibc's 'x' mode to fopen is not portable, so use open() and
+ * fdopen().
*/
- bits = 2;
- while (1 << bits < num_uids)
- bits++;
- create_hash_table(bits);
- for (n = 0; n < num_uids; n++) {
- uint32_t uid = read_u32(map + n * sizeof(uid));
- ret = search_uid(uid, admissible_uids, OPEN_USER_TABLE, NULL);
- if (ret < 0)
- goto out;
- }
-out:
- adu_munmap(map, size);
- return ret;
+ 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;
-
- if (select_conf.output_given && strcmp(select_conf.output_arg, "-")) {
- output_file = fopen(select_conf.output_arg, "w");
- if (!output_file)
- return -ERRNO_TO_ERROR(errno);
- } else
- output_file = stdout;
+ int ret = open_output_stream();
+ if (ret < 0)
+ goto out;
ret = open_dir_table(0);
if (ret < 0)
goto out;
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;
}
#define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n"
#define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:5) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
+static int setup_format_string(char *fmt, struct format_info **fi)
+{
+ struct atom *atoms;
+
+ if (!fmt)
+ INFO_LOG("using default format string\n");
+ switch (select_conf.select_mode_arg) {
+ case select_mode_arg_global_list:
+ if (!fmt)
+ fmt = GLOBAL_LIST_DFLT_FMT;
+ atoms = global_list_atoms;
+ break;
+ case select_mode_arg_global_summary:
+ if (!fmt)
+ fmt = GLOBAL_SUMMARY_DFLT_FMT;
+ atoms = global_summary_atoms;
+ break;
+ case select_mode_arg_user_list:
+ if (!fmt)
+ fmt = USER_LIST_DFLT_FMT;
+ atoms = user_list_atoms;
+ break;
+ case select_mode_arg_user_summary:
+ if (!fmt)
+ fmt = USER_SUMMARY_DFLT_FMT;
+ atoms = user_summary_atoms;
+ break;
+ default:
+ ERROR_LOG("bad select mode\n");
+ return -ERRNO_TO_ERROR(EINVAL);
+ };
+ INFO_LOG("format string: %s\n", fmt);
+ return parse_format_string(fmt, atoms, fi);
+}
+
/* return: < 0: error, >0: OK, == 0: help given */
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;
- struct atom *atoms;
if (string) {
int argc;
if (select_conf.help_given || select_conf.detailed_help_given)
goto help;
fmt = select_conf.format_arg;
-
}
ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
if (ret < 0)
return ret;
-
- if (!fmt)
- INFO_LOG("using default format string\n");
- switch (select_conf.select_mode_arg) {
- case select_mode_arg_global_list:
- if (!fmt)
- fmt = GLOBAL_LIST_DFLT_FMT;
- atoms = global_list_atoms;
- break;
- case select_mode_arg_global_summary:
- if (!fmt)
- fmt = GLOBAL_SUMMARY_DFLT_FMT;
- atoms = global_summary_atoms;
- break;
- case select_mode_arg_user_list:
- if (!fmt)
- fmt = USER_LIST_DFLT_FMT;
- atoms = user_list_atoms;
- break;
- case select_mode_arg_user_summary:
- if (!fmt)
- fmt = USER_SUMMARY_DFLT_FMT;
- atoms = user_summary_atoms;
- break;
- default:
- ERROR_LOG("bad select mode\n");
- return -ERRNO_TO_ERROR(-EINVAL);
- };
- INFO_LOG("format string: %s\n", fmt);
- return parse_format_string(fmt, atoms, fi);
+ 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);
help:
line = select_conf.detailed_help_given?
select_args_info_detailed_help : select_args_info_help;
.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 = run_select_query(admissible_uids, fi);
+ free_format_info(fi);
+ }
+ select_cmdline_parser_free(&select_conf);
return ret;
}