1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-18 10:04:20 +03:00

tools: Reinstate lvm script processing.

We check for a script if the command isn't recognised (ENO_SUCH_CMD).
(Also added a few comments and fixed some whitespace.)
This commit is contained in:
Alasdair G Kergon 2017-03-23 23:03:25 +00:00
parent fe3b9bb7d4
commit 2eaca7ab63
4 changed files with 50 additions and 53 deletions

View File

@ -1348,7 +1348,7 @@ static int copy_line(char *line, int max_line, int *position)
return 1; return 1;
} }
int define_commands(char *run_name) int define_commands(const char *run_name)
{ {
struct command *cmd = NULL; struct command *cmd = NULL;
char line[MAX_LINE]; char line[MAX_LINE];

View File

@ -255,7 +255,7 @@ struct lv_type {
}; };
int define_commands(char *run_name); int define_commands(const char *run_name);
int command_id_to_enum(const char *str); int command_id_to_enum(const char *str);
void print_usage(struct command *cmd, int longhelp, int desc_first); void print_usage(struct command *cmd, int longhelp, int desc_first);
void print_usage_common_cmd(struct command_name *cname, struct command *cmd); void print_usage_common_cmd(struct command_name *cname, struct command *cmd);

View File

@ -32,7 +32,7 @@ void *cmdlib_lvm2_init(unsigned static_compile);
void lvm_fin(struct cmd_context *cmd); void lvm_fin(struct cmd_context *cmd);
struct cmd_context *init_lvm(unsigned set_connections, unsigned set_filters); struct cmd_context *init_lvm(unsigned set_connections, unsigned set_filters);
int lvm_register_commands(char *name); int lvm_register_commands(const char *name);
int lvm_split(char *str, int *argc, char **argv, int max); int lvm_split(char *str, int *argc, char **argv, int max);
int lvm_run_command(struct cmd_context *cmd, int argc, char **argv); int lvm_run_command(struct cmd_context *cmd, int argc, char **argv);
int lvm_return_code(int ret); int lvm_return_code(int ret);

View File

@ -1237,7 +1237,7 @@ static const struct command_function *_find_command_id_function(int command_enum
return NULL; return NULL;
} }
int lvm_register_commands(char *name) int lvm_register_commands(const char *run_name)
{ {
int i; int i;
@ -1251,7 +1251,7 @@ int lvm_register_commands(char *name)
* populate commands[] array with command definitions * populate commands[] array with command definitions
* by parsing command-lines.in/command-lines-input.h * by parsing command-lines.in/command-lines-input.h
*/ */
if (!define_commands(name)) { if (!define_commands(run_name)) {
log_error(INTERNAL_ERROR "Failed to parse command definitions."); log_error(INTERNAL_ERROR "Failed to parse command definitions.");
return 0; return 0;
} }
@ -1974,14 +1974,10 @@ static int _process_command_line(struct cmd_context *cmd, int *argc, char ***arg
struct opt_name *a; struct opt_name *a;
struct arg_values *av; struct arg_values *av;
struct arg_value_group_list *current_group = NULL; struct arg_value_group_list *current_group = NULL;
struct command_name *cname;
int arg_enum; /* e.g. foo_ARG */ int arg_enum; /* e.g. foo_ARG */
int goval; /* the number returned from getopt_long identifying what it found */ int goval; /* the number returned from getopt_long identifying what it found */
int i; int i;
if (!(cname = find_command_name(cmd->name)))
return_0;
if (!(cmd->opt_arg_values = dm_pool_zalloc(cmd->mem, sizeof(*cmd->opt_arg_values) * ARG_COUNT))) { if (!(cmd->opt_arg_values = dm_pool_zalloc(cmd->mem, sizeof(*cmd->opt_arg_values) * ARG_COUNT))) {
log_fatal("Unable to allocate memory for command line arguments."); log_fatal("Unable to allocate memory for command line arguments.");
return 0; return 0;
@ -1992,8 +1988,9 @@ static int _process_command_line(struct cmd_context *cmd, int *argc, char ***arg
* array (opts) to pass to the getopt_long() function. IOW we generate * array (opts) to pass to the getopt_long() function. IOW we generate
* the arguments to pass to getopt_long() from the opt_names data. * the arguments to pass to getopt_long() from the opt_names data.
*/ */
for (i = 0; i < cname->num_args; i++) if (cmd->cname)
_add_getopt_arg(cname->valid_args[i], &ptr, &o); for (i = 0; i < cmd->cname->num_args; i++)
_add_getopt_arg(cmd->cname->valid_args[i], &ptr, &o);
*ptr = '\0'; *ptr = '\0';
memset(o, 0, sizeof(*o)); memset(o, 0, sizeof(*o));
@ -2673,8 +2670,11 @@ int lvm_run_command(struct cmd_context *cmd, int argc, char **argv)
if (!(cmd->cmd_line = _copy_command_line(cmd, argc, argv))) if (!(cmd->cmd_line = _copy_command_line(cmd, argc, argv)))
return_ECMD_FAILED; return_ECMD_FAILED;
/* Look up command - will be NULL if not recognised */
cmd->cname = find_command_name(cmd->name);
if (!_process_command_line(cmd, &argc, &argv)) { if (!_process_command_line(cmd, &argc, &argv)) {
log_error("Command name not found."); log_error("Error during parsing of command line.");
return EINVALID_CMD_LINE; return EINVALID_CMD_LINE;
} }
@ -2688,10 +2688,9 @@ int lvm_run_command(struct cmd_context *cmd, int argc, char **argv)
log_debug("Parsing: %s", cmd->cmd_line); log_debug("Parsing: %s", cmd->cmd_line);
if (!(cmd->cname = find_command_name(cmd->name))) { /* Having validated cmdline args, return if we didn't recognised the command */
log_error("Command name not found.\n"); if (!cmd->cname)
return EINVALID_CMD_LINE; return ENO_SUCH_CMD;
}
if (!(cmd->command = _find_command(cmd, cmd->name, &argc, argv))) if (!(cmd->command = _find_command(cmd, cmd->name, &argc, argv)))
return EINVALID_CMD_LINE; return EINVALID_CMD_LINE;
@ -3311,7 +3310,7 @@ int lvm2_main(int argc, char **argv)
int ret, alias = 0; int ret, alias = 0;
struct custom_fds custom_fds; struct custom_fds custom_fds;
struct cmd_context *cmd; struct cmd_context *cmd;
char *name; const char *run_name;
if (!argv) if (!argv)
return -1; return -1;
@ -3351,26 +3350,25 @@ int lvm2_main(int argc, char **argv)
if (!(cmd = init_lvm(0, 0))) if (!(cmd = init_lvm(0, 0)))
return -1; return -1;
/* Store original argv location so we may customise it if we become a daemon */
cmd->argv = argv; cmd->argv = argv;
if (!alias && argc == 1) /*
name = NULL; * If the invocation command name wasn't itself an alias, shift to the
else if (alias) * first arg. After this point, run_name holds one of:
name = (char *)dm_basename(argv[0]); * the LVM command name we want to run;
else * the LVM script name (handled through ENO_SUCH_CMD below);
name = argv[1]; * NULL for a shell (if readline is enabled).
*/
if (!lvm_register_commands(name)) { if (!alias) {
ret = ECMD_FAILED; argc--;
goto out; argv++;
} run_name = argv[0];
} else
run_name = dm_basename(argv[0]);
if (_lvm1_fallback(cmd)) { if (_lvm1_fallback(cmd)) {
/* Attempt to run equivalent LVM1 tool instead */ /* Attempt to run equivalent LVM1 tool instead */
if (!alias) {
argv++;
argc--;
}
if (!argc) { if (!argc) {
log_error("Falling back to LVM1 tools, but no " log_error("Falling back to LVM1 tools, but no "
"command specified."); "command specified.");
@ -3381,8 +3379,14 @@ int lvm2_main(int argc, char **argv)
ret = ECMD_FAILED; ret = ECMD_FAILED;
goto_out; goto_out;
} }
if (!lvm_register_commands(run_name)) {
ret = ECMD_FAILED;
goto out;
}
if (!alias && !argc) {
#ifdef READLINE_SUPPORT #ifdef READLINE_SUPPORT
if (!alias && argc == 1) {
_nonroot_warning(); _nonroot_warning();
if (!_prepare_profiles(cmd)) { if (!_prepare_profiles(cmd)) {
ret = ECMD_FAILED; ret = ECMD_FAILED;
@ -3390,19 +3394,12 @@ int lvm2_main(int argc, char **argv)
} }
ret = lvm_shell(cmd, &_cmdline); ret = lvm_shell(cmd, &_cmdline);
goto out; goto out;
} #else
#endif
if (!alias) {
if (argc < 2) {
log_fatal("Please supply an LVM command."); log_fatal("Please supply an LVM command.");
_display_help(); _display_help();
ret = EINVALID_CMD_LINE; ret = EINVALID_CMD_LINE;
goto out; goto out;
} #endif
argc--;
argv++;
} }
_nonroot_warning(); _nonroot_warning();