summaryrefslogtreecommitdiffstats
path: root/src/vlib/unix/cli.c
diff options
context:
space:
mode:
authorBenoît Ganne <bganne@cisco.com>2020-05-11 16:27:29 +0200
committerDamjan Marion <dmarion@me.com>2020-05-14 12:27:39 +0000
commita58be82dda89d6496f92e451b42eee31f0cf47b4 (patch)
tree8361fdf538dc853f99d4f4394537f488acc601d0 /src/vlib/unix/cli.c
parent0ef9153989fd22f18c5a803167c6581202155efb (diff)
vlib: fix unix cli commands crash without session
If a cli command is run while there are no cli session, then cm->cli_file_pool will not be initialized and we should not try to operate on it. Type: fix Change-Id: Iaea15a23f7efd5b17fab13e6c1cbb3a9a34080e0 Signed-off-by: Benoît Ganne <bganne@cisco.com>
Diffstat (limited to 'src/vlib/unix/cli.c')
-rw-r--r--src/vlib/unix/cli.c38
1 files changed, 23 insertions, 15 deletions
diff --git a/src/vlib/unix/cli.c b/src/vlib/unix/cli.c
index 2a13c84b92d..c7249eb2af4 100644
--- a/src/vlib/unix/cli.c
+++ b/src/vlib/unix/cli.c
@@ -3237,6 +3237,18 @@ vlib_unix_cli_set_prompt (char *prompt)
cm->cli_prompt = format (0, fmt, prompt);
}
+static unix_cli_file_t *
+unix_cli_file_if_interactive (unix_cli_main_t * cm)
+{
+ unix_cli_file_t *cf;
+ if (!cm->cli_file_pool)
+ return 0;
+ cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
+ if (!cf->is_interactive)
+ return 0;
+ return cf;
+}
+
/** CLI command to quit the terminal session.
* @note If this is a stdin session then this will
* shutdown VPP also.
@@ -3246,8 +3258,10 @@ unix_cli_quit (vlib_main_t * vm,
unformat_input_t * input, vlib_cli_command_t * cmd)
{
unix_cli_main_t *cm = &unix_cli_main;
- unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool,
- cm->current_input_file_index);
+ unix_cli_file_t *cf;
+
+ if (!(cf = unix_cli_file_if_interactive (cm)))
+ return clib_error_return (0, "invalid for non-interactive sessions");
/* Cosmetic: suppress the final prompt from appearing before we die */
cf->is_interactive = 0;
@@ -3499,9 +3513,7 @@ unix_cli_show_history (vlib_main_t * vm,
unix_cli_file_t *cf;
int i, j;
- cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
-
- if (!cf->is_interactive)
+ if (!(cf = unix_cli_file_if_interactive (cm)))
return clib_error_return (0, "invalid for non-interactive sessions");
if (cf->has_history && cf->history_limit)
@@ -3539,7 +3551,9 @@ unix_cli_show_terminal (vlib_main_t * vm,
unix_cli_file_t *cf;
vlib_node_t *n;
- cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
+ if (!(cf = unix_cli_file_if_interactive (cm)))
+ return clib_error_return (0, "invalid for non-interactive sessions");
+
n = vlib_get_node (vm, cf->process_node_index);
vlib_cli_output (vm, "Terminal name: %v\n", n->name);
@@ -3691,9 +3705,7 @@ unix_cli_set_terminal_pager (vlib_main_t * vm,
unformat_input_t _line_input, *line_input = &_line_input;
clib_error_t *error = 0;
- cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
-
- if (!cf->is_interactive)
+ if (!(cf = unix_cli_file_if_interactive (cm)))
return clib_error_return (0, "invalid for non-interactive sessions");
if (!unformat_user (input, unformat_line_input, line_input))
@@ -3750,9 +3762,7 @@ unix_cli_set_terminal_history (vlib_main_t * vm,
u32 limit;
clib_error_t *error = 0;
- cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
-
- if (!cf->is_interactive)
+ if (!(cf = unix_cli_file_if_interactive (cm)))
return clib_error_return (0, "invalid for non-interactive sessions");
if (!unformat_user (input, unformat_line_input, line_input))
@@ -3820,9 +3830,7 @@ unix_cli_set_terminal_ansi (vlib_main_t * vm,
unix_cli_main_t *cm = &unix_cli_main;
unix_cli_file_t *cf;
- cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
-
- if (!cf->is_interactive)
+ if (!(cf = unix_cli_file_if_interactive (cm)))
return clib_error_return (0, "invalid for non-interactive sessions");
if (unformat (input, "on"))