From 3221dca81ea45765bb8707fa81cf8f1c6da36f71 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Wed, 9 Mar 2016 07:25:02 -0500 Subject: [PATCH] vtysh: Set an erroneous exit code if dry run fails because of syntax error vtysh has a -C option to do a dry run of the quagga commands. However, the program always returns 0 even when there's an error detected in the command. Furthermore, it only parses vtysh.conf, not Quagga.conf. This patch makes vtysh -C parse Quagga.conf also and return a non-zero exit code so that network automation tools can catch this to flag errors in syntax. This non-zero exit code along with printing the exact error with the line number and offending line itself should help in fixing the error. But this lack of proper error code requires the automation tools to go through an additional hoop to validate the syntax. Signed-off-by: Dinesh G Dutt --- vtysh/vtysh.c | 9 ++++++++- vtysh/vtysh.h | 3 ++- vtysh/vtysh_config.c | 12 ++++++++---- vtysh/vtysh_main.c | 14 ++++++++++---- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/vtysh/vtysh.c b/vtysh/vtysh.c index 9af2461448..2d84b020f6 100644 --- a/vtysh/vtysh.c +++ b/vtysh/vtysh.c @@ -675,12 +675,13 @@ vtysh_mark_file (const char *filename) } /* Configration make from file. */ -void +int vtysh_config_from_file (struct vty *vty, FILE *fp) { int ret; struct cmd_element *cmd; int lineno = 0; + int retcode = CMD_SUCCESS; while (fgets (vty->buf, VTY_BUFSIZ, fp)) { @@ -693,15 +694,19 @@ vtysh_config_from_file (struct vty *vty, FILE *fp) case CMD_WARNING: if (vty->type == VTY_FILE) fprintf (stdout,"line %d: Warning...: %s\n", lineno, vty->buf); + retcode = 1; /* once we have an error, we remember & return that */ break; case CMD_ERR_AMBIGUOUS: fprintf (stdout,"line %d: %% Ambiguous command: %s\n", lineno, vty->buf); + retcode = 1; /* once we have an error, we remember & return that */ break; case CMD_ERR_NO_MATCH: fprintf (stdout,"line %d: %% Unknown command: %s", lineno, vty->buf); + retcode = 1; /* once we have an error, we remember & return that */ break; case CMD_ERR_INCOMPLETE: fprintf (stdout,"line %d: %% Command incomplete: %s\n", lineno, vty->buf); + retcode = 1; /* once we have an error, we remember & return that */ break; case CMD_SUCCESS_DAEMON: { @@ -726,6 +731,8 @@ vtysh_config_from_file (struct vty *vty, FILE *fp) } } } + + return (retcode); } /* We don't care about the point of the cursor when '?' is typed. */ diff --git a/vtysh/vtysh.h b/vtysh/vtysh.h index a24d615c3f..da6dd2a067 100644 --- a/vtysh/vtysh.h +++ b/vtysh/vtysh.h @@ -36,6 +36,7 @@ /* vtysh local configuration file. */ #define VTYSH_DEFAULT_CONFIG "vtysh.conf" +#define QUAGGA_DEFAULT_CONFIG "Quagga.conf" void vtysh_init_vty (void); void vtysh_init_cmd (void); @@ -50,7 +51,7 @@ char *vtysh_prompt (void); void vtysh_config_write (void); -void vtysh_config_from_file (struct vty *, FILE *); +int vtysh_config_from_file (struct vty *, FILE *); int vtysh_mark_file(const char *filename); diff --git a/vtysh/vtysh_config.c b/vtysh/vtysh_config.c index f435cf8719..6bb8fad42b 100644 --- a/vtysh/vtysh_config.c +++ b/vtysh/vtysh_config.c @@ -365,10 +365,11 @@ vtysh_config_dump (FILE *fp) } /* Read up configuration file from file_name. */ -static void +static int vtysh_read_file (FILE *confp) { struct vty *vty; + int ret; vty = vty_new (); vty->fd = 0; /* stdout */ @@ -379,12 +380,14 @@ vtysh_read_file (FILE *confp) vtysh_execute_no_pager ("configure terminal"); /* Execute configuration file. */ - vtysh_config_from_file (vty, confp); + ret = vtysh_config_from_file (vty, confp); vtysh_execute_no_pager ("end"); vtysh_execute_no_pager ("disable"); vty_close (vty); + + return (ret); } /* Read up configuration file from config_default_dir. */ @@ -392,16 +395,17 @@ int vtysh_read_config (const char *config_default_dir) { FILE *confp = NULL; + int ret; host_config_set (config_default_dir); confp = fopen (config_default_dir, "r"); if (confp == NULL) return (1); - vtysh_read_file (confp); + ret = vtysh_read_file (confp); fclose (confp); - return (0); + return (ret); } /* We don't write vtysh specific into file from vtysh. vtysh.conf should diff --git a/vtysh/vtysh_main.c b/vtysh/vtysh_main.c index befc86e6e2..d8b769ba20 100644 --- a/vtysh/vtysh_main.c +++ b/vtysh/vtysh_main.c @@ -42,6 +42,7 @@ char *progname; /* Configuration file name and directory. */ char config_default[] = SYSCONFDIR VTYSH_DEFAULT_CONFIG; +char quagga_config_default[] = SYSCONFDIR QUAGGA_DEFAULT_CONFIG; char history_file[MAXPATHLEN]; /* Flag for indicate executing child command. */ @@ -232,6 +233,7 @@ main (int argc, char **argv, char **env) int echo_command = 0; int no_error = 0; int markfile = 0; + int ret = 0; char *homedir = NULL; /* Preserve name of myself. */ @@ -330,9 +332,13 @@ main (int argc, char **argv, char **env) { if (inputfile) { - vtysh_read_config(inputfile); + ret = vtysh_read_config(inputfile); } - return(0); + else + { + ret = vtysh_read_config(quagga_config_default); + } + exit(ret); } /* Ignore error messages */ @@ -357,8 +363,8 @@ main (int argc, char **argv, char **env) if (inputfile) { - vtysh_read_config(inputfile); - exit(0); + ret = vtysh_read_config(inputfile); + exit(ret); } /* -- 2.39.5