db-ctl-base: make use of user supplied exit function
[cascardo/ovs.git] / utilities / ovs-vsctl.c
index 863bc73..4fb88b1 100644 (file)
@@ -84,6 +84,14 @@ static bool retry;
 static struct table_style table_style = TABLE_STYLE_DEFAULT;
 
 static void vsctl_cmd_init(void);
+
+/* The IDL we're using and the current transaction, if any.
+ * This is for use by vsctl_exit() only, to allow it to clean up.
+ * Other code should use its context arguments. */
+static struct ovsdb_idl *the_idl;
+static struct ovsdb_idl_txn *the_idl_txn;
+OVS_NO_RETURN static void vsctl_exit(int status);
+
 OVS_NO_RETURN static void usage(void);
 static void parse_options(int argc, char *argv[], struct shash *local_options);
 static void run_prerequisites(struct ctl_command[], size_t n_commands,
@@ -740,9 +748,6 @@ vsctl_context_populate_cache(struct ctl_context *ctx)
             continue;
         }
         br = add_bridge_to_cache(vsctl_ctx, br_cfg, br_cfg->name, NULL, 0);
-        if (!br) {
-            continue;
-        }
 
         for (j = 0; j < br_cfg->n_ports; j++) {
             struct ovsrec_port *port_cfg = br_cfg->ports[j];
@@ -1343,7 +1348,7 @@ cmd_br_exists(struct ctl_context *ctx)
 
     vsctl_context_populate_cache(ctx);
     if (!find_bridge(vsctl_ctx, ctx->argv[1], false)) {
-        ctl_exit(2);
+        vsctl_exit(2);
     }
 }
 
@@ -2137,7 +2142,7 @@ cmd_add_aa_mapping(struct ctl_context *ctx)
         br = br->parent;
     }
 
-    if (br && br->br_cfg) {
+    if (br->br_cfg) {
         if (!br->br_cfg->auto_attach) {
             struct ovsrec_autoattach *aa = ovsrec_autoattach_insert(ctx->txn);
             ovsrec_bridge_set_auto_attach(br->br_cfg, aa);
@@ -2197,7 +2202,7 @@ cmd_del_aa_mapping(struct ctl_context *ctx)
         br = br->parent;
     }
 
-    if (br && br->br_cfg && br->br_cfg->auto_attach &&
+    if (br->br_cfg && br->br_cfg->auto_attach &&
         br->br_cfg->auto_attach->key_mappings &&
         br->br_cfg->auto_attach->value_mappings) {
         size_t i;
@@ -2248,7 +2253,7 @@ cmd_get_aa_mapping(struct ctl_context *ctx)
 
     verify_auto_attach(br->br_cfg);
 
-    if (br && br->br_cfg && br->br_cfg->auto_attach &&
+    if (br->br_cfg && br->br_cfg->auto_attach &&
         br->br_cfg->auto_attach->key_mappings &&
         br->br_cfg->auto_attach->value_mappings) {
         size_t i;
@@ -2262,7 +2267,7 @@ cmd_get_aa_mapping(struct ctl_context *ctx)
 }
 
 \f
-const struct ctl_table_class tables[] = {
+static const struct ctl_table_class tables[] = {
     {&ovsrec_table_bridge,
      {{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
       {&ovsrec_table_flow_sample_collector_set, NULL,
@@ -2648,6 +2653,23 @@ try_again:
     free(error);
 }
 
+/* Frees the current transaction and the underlying IDL and then calls
+ * exit(status).
+ *
+ * Freeing the transaction and the IDL is not strictly necessary, but it makes
+ * for a clean memory leak report from valgrind in the normal case.  That makes
+ * it easier to notice real memory leaks. */
+static void
+vsctl_exit(int status)
+{
+    if (the_idl_txn) {
+        ovsdb_idl_txn_abort(the_idl_txn);
+        ovsdb_idl_txn_destroy(the_idl_txn);
+    }
+    ovsdb_idl_destroy(the_idl);
+    exit(status);
+}
+
 /*
  * Developers who add new commands to the 'struct ctl_command_syntax' must
  * define the 'arguments' member of the struct.  The following keywords are
@@ -2749,6 +2771,6 @@ static const struct ctl_command_syntax vsctl_commands[] = {
 static void
 vsctl_cmd_init(void)
 {
-    ctl_init();
+    ctl_init(tables, vsctl_exit);
     ctl_register_commands(vsctl_commands);
 }