[ovs-dev] [PATCH v4 09/21] db-ctl-base: Propagate errors from the commands parser.

Jakub Sitnicki jkbs at redhat.com
Thu Jul 19 12:36:20 UTC 2018


Let the caller decide how to handle the error. Prepare for using the
parser in ovn-nbctl daemon mode.

Signed-off-by: Jakub Sitnicki <jkbs at redhat.com>
---
 lib/db-ctl-base.c         | 30 +++++++++++++++++++++---------
 lib/db-ctl-base.h         |  6 +++---
 ovn/utilities/ovn-nbctl.c |  7 +++++--
 ovn/utilities/ovn-sbctl.c |  7 +++++--
 utilities/ovs-vsctl.c     |  7 +++++--
 vtep/vtep-ctl.c           |  7 +++++--
 6 files changed, 44 insertions(+), 20 deletions(-)

diff --git a/lib/db-ctl-base.c b/lib/db-ctl-base.c
index f92da7c61..4e0eb9c5c 100644
--- a/lib/db-ctl-base.c
+++ b/lib/db-ctl-base.c
@@ -2251,22 +2251,24 @@ ctl_add_cmd_options(struct option **options_p, size_t *n_options_p,
 }
 
 /* Parses command-line input for commands. */
-struct ctl_command *
+char *
 ctl_parse_commands(int argc, char *argv[], struct shash *local_options,
-                   size_t *n_commandsp)
+                   struct ctl_command **commandsp, size_t *n_commandsp)
 {
     struct ctl_command *commands;
     size_t n_commands, allocated_commands;
     int i, start;
+    char *error;
 
     commands = NULL;
     n_commands = allocated_commands = 0;
 
+    *commandsp = NULL;
+    *n_commandsp = 0;
+
     for (start = i = 0; i <= argc; i++) {
         if (i == argc || !strcmp(argv[i], "--")) {
             if (i > start) {
-                char *error;
-
                 if (n_commands >= allocated_commands) {
                     struct ctl_command *c;
 
@@ -2277,21 +2279,31 @@ ctl_parse_commands(int argc, char *argv[], struct shash *local_options,
                     }
                 }
                 error = parse_command(i - start, &argv[start], local_options,
-                                      &commands[n_commands++]);
+                                      &commands[n_commands]);
                 if (error) {
-                    ctl_fatal("%s", error);
+                    struct ctl_command *c;
+
+                    for (c = commands; c < &commands[n_commands]; c++) {
+                        shash_destroy_free_data(&c->options);
+                    }
+                    free(commands);
+
+                    return error;
                 }
+
+                n_commands++;
             } else if (!shash_is_empty(local_options)) {
-                ctl_fatal("missing command name (use --help for help)");
+                return xstrdup("missing command name (use --help for help)");
             }
             start = i + 1;
         }
     }
     if (!n_commands) {
-        ctl_fatal("missing command name (use --help for help)");
+        return xstrdup("missing command name (use --help for help)");
     }
+    *commandsp = commands;
     *n_commandsp = n_commands;
-    return commands;
+    return NULL;
 }
 
 /* Prints all registered commands. */
diff --git a/lib/db-ctl-base.h b/lib/db-ctl-base.h
index ba771a180..284b573d0 100644
--- a/lib/db-ctl-base.h
+++ b/lib/db-ctl-base.h
@@ -166,9 +166,9 @@ void ctl_print_options(const struct option *);
 void ctl_add_cmd_options(struct option **, size_t *n_options_p,
                          size_t *allocated_options_p, int opt_val);
 void ctl_register_commands(const struct ctl_command_syntax *);
-struct ctl_command *ctl_parse_commands(int argc, char *argv[],
-                                       struct shash *local_options,
-                                       size_t *n_commandsp);
+char * OVS_WARN_UNUSED_RESULT ctl_parse_commands(
+    int argc, char *argv[], struct shash *local_options,
+    struct ctl_command **commandsp, size_t *n_commandsp);
 
 /* Sometimes, it is desirable to print the table with weak reference to
  * rows in a 'cmd_show_table' table.  In that case, the 'weak_ref_table'
diff --git a/ovn/utilities/ovn-nbctl.c b/ovn/utilities/ovn-nbctl.c
index 25194b2fa..465f8f6d1 100644
--- a/ovn/utilities/ovn-nbctl.c
+++ b/ovn/utilities/ovn-nbctl.c
@@ -110,8 +110,11 @@ main(int argc, char *argv[])
     char *args = process_escape_args(argv);
     shash_init(&local_options);
     parse_options(argc, argv, &local_options);
-    commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
-                                  &n_commands);
+    char *error = ctl_parse_commands(argc - optind, argv + optind,
+                                     &local_options, &commands, &n_commands);
+    if (error) {
+        ctl_fatal("%s", error);
+    }
     VLOG(ctl_might_write_to_db(commands, n_commands) ? VLL_INFO : VLL_DBG,
          "Called as %s", args);
 
diff --git a/ovn/utilities/ovn-sbctl.c b/ovn/utilities/ovn-sbctl.c
index c47cf6df9..7022347ed 100644
--- a/ovn/utilities/ovn-sbctl.c
+++ b/ovn/utilities/ovn-sbctl.c
@@ -112,8 +112,11 @@ main(int argc, char *argv[])
     char *args = process_escape_args(argv);
     shash_init(&local_options);
     parse_options(argc, argv, &local_options);
-    commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
-                                  &n_commands);
+    char *error = ctl_parse_commands(argc - optind, argv + optind,
+                                     &local_options, &commands, &n_commands);
+    if (error) {
+        ctl_fatal("%s", error);
+    }
     VLOG(ctl_might_write_to_db(commands, n_commands) ? VLL_INFO : VLL_DBG,
          "Called as %s", args);
 
diff --git a/utilities/ovs-vsctl.c b/utilities/ovs-vsctl.c
index d390c5902..d14aa6cea 100644
--- a/utilities/ovs-vsctl.c
+++ b/utilities/ovs-vsctl.c
@@ -148,8 +148,11 @@ main(int argc, char *argv[])
     char *args = process_escape_args(argv);
     shash_init(&local_options);
     parse_options(argc, argv, &local_options);
-    commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
-                                  &n_commands);
+    char *error = ctl_parse_commands(argc - optind, argv + optind,
+                                     &local_options, &commands, &n_commands);
+    if (error) {
+        ctl_fatal("%s", error);
+    }
     VLOG(ctl_might_write_to_db(commands, n_commands) ? VLL_INFO : VLL_DBG,
          "Called as %s", args);
 
diff --git a/vtep/vtep-ctl.c b/vtep/vtep-ctl.c
index 3c5ffa745..35ab43588 100644
--- a/vtep/vtep-ctl.c
+++ b/vtep/vtep-ctl.c
@@ -111,8 +111,11 @@ main(int argc, char *argv[])
     char *args = process_escape_args(argv);
     shash_init(&local_options);
     parse_options(argc, argv, &local_options);
-    commands = ctl_parse_commands(argc - optind, argv + optind, &local_options,
-                                  &n_commands);
+    char *error = ctl_parse_commands(argc - optind, argv + optind,
+                                     &local_options, &commands, &n_commands);
+    if (error) {
+        ctl_fatal("%s", error);
+    }
     VLOG(ctl_might_write_to_db(commands, n_commands) ? VLL_INFO : VLL_DBG,
          "Called as %s", args);
 
-- 
2.14.4



More information about the dev mailing list