#include "vlog.h"
#define THIS_MODULE VLM_vsctl
+/* vsctl_fatal() also logs the error, so it is preferred in this file. */
+#define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
+
struct vsctl_context;
typedef void vsctl_handler_func(struct vsctl_context *);
/* All supported commands. */
static const struct vsctl_command_syntax all_commands[];
+/* 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;
+
+static void vsctl_exit(int status) NO_RETURN;
static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
static char *default_db(void);
static void usage(void) NO_RETURN;
/* Do basic command syntax checking. */
/* Now execute the commands. */
- idl = ovsdb_idl_create(db, &ovsrec_idl_class);
+ idl = the_idl = ovsdb_idl_create(db, &ovsrec_idl_class);
seqno = ovsdb_idl_get_seqno(idl);
trials = 0;
for (;;) {
case 't':
timeout = strtoul(optarg, NULL, 10);
if (timeout < 0) {
- ovs_fatal(0, "value %s on -t or --timeout is invalid",
- optarg);
+ vsctl_fatal("value %s on -t or --timeout is invalid",
+ optarg);
}
break;
vlog_set_levels(VLM_vsctl, VLF_CONSOLE, VLL_EMER);
VLOG_ERR("%s", message);
- ovs_fatal(0, "%s", message);
+ ovs_error(0, "%s", message);
+ vsctl_exit(EXIT_FAILURE);
+}
+
+/* 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);
}
static void
get_info(ctx->ovs, &info);
if (!find_bridge(&info, ctx->argv[1], false)) {
- exit(2);
+ vsctl_exit(2);
}
free_info(&info);
}
};
static const struct vsctl_column bridge_columns[] = {
+ {&ovsrec_bridge_col_controller, VSCF_READONLY, NULL},
{&ovsrec_bridge_col_datapath_id, VSCF_READONLY, NULL},
{&ovsrec_bridge_col_datapath_type, VSCF_READONLY, NULL},
- {&ovsrec_bridge_col_name, VSCF_READONLY, NULL},
- {&ovsrec_bridge_col_mirrors, VSCF_READONLY, NULL},
- {&ovsrec_bridge_col_other_config, 0, NULL},
+ {&ovsrec_bridge_col_external_ids, 0, NULL},
{&ovsrec_bridge_col_flood_vlans, 0, "[1,4095]"},
- {&ovsrec_bridge_col_controller, VSCF_READONLY, NULL},
+ {&ovsrec_bridge_col_mirrors, VSCF_READONLY, NULL},
+ {&ovsrec_bridge_col_name, VSCF_READONLY, NULL},
{&ovsrec_bridge_col_netflow, VSCF_READONLY, NULL},
- {&ovsrec_bridge_col_external_ids, 0, NULL},
+ {&ovsrec_bridge_col_other_config, 0, NULL},
{&ovsrec_bridge_col_ports, VSCF_READONLY, NULL},
{NULL, 0, NULL},
};
static const struct vsctl_column netflow_columns[] = {
{&ovsrec_netflow_col_active_timeout, 0, "[-1,]"},
{&ovsrec_netflow_col_add_id_to_interface, 0, NULL},
- {&ovsrec_netflow_col_targets, 0, IP_RE":"PORT_RE},
- {&ovsrec_netflow_col_engine_type, 0, "[0,255]"},
{&ovsrec_netflow_col_engine_id, 0, "[0,255]"},
+ {&ovsrec_netflow_col_engine_type, 0, "[0,255]"},
+ {&ovsrec_netflow_col_targets, 0, IP_RE":"PORT_RE},
{NULL, 0, NULL},
};
{&ovsrec_open_vswitch_col_bridges, VSCF_READONLY, NULL},
{&ovsrec_open_vswitch_col_controller, VSCF_READONLY, NULL},
{&ovsrec_open_vswitch_col_cur_cfg, VSCF_HIDDEN, NULL},
- {&ovsrec_open_vswitch_col_management_id, 0, "[0-9a-fA-F]{12}"},
{&ovsrec_open_vswitch_col_managers, 0, "p?(ssl|tcp|unix):.*"},
{&ovsrec_open_vswitch_col_next_cfg, VSCF_HIDDEN, NULL},
{&ovsrec_open_vswitch_col_ssl, VSCF_READONLY, NULL},
};
static const struct vsctl_column port_columns[] = {
- {&ovsrec_port_col_bond_updelay, 0, "[0,]"},
{&ovsrec_port_col_bond_downdelay, 0, "[0,]"},
{&ovsrec_port_col_bond_fake_iface, VSCF_READONLY, NULL},
+ {&ovsrec_port_col_bond_updelay, 0, "[0,]"},
{&ovsrec_port_col_external_ids, 0, NULL},
{&ovsrec_port_col_fake_bridge, VSCF_READONLY, NULL},
{&ovsrec_port_col_interfaces, VSCF_READONLY, NULL},
die_if_error(char *error)
{
if (error) {
- ovs_fatal(0, "%s", error);
+ vsctl_fatal("%s", error);
}
}
if (best_match) {
return best_match;
} else if (best_score) {
- ovs_fatal(0, "multiple table names match \"%s\"", table_name);
+ vsctl_fatal("multiple table names match \"%s\"", table_name);
} else {
- ovs_fatal(0, "unknown table \"%s\"", table_name);
+ vsctl_fatal("unknown table \"%s\"", table_name);
}
}
}
ovsdb_datum_destroy(&name, &id->name_column->type);
}
+ if (best_score && !referrer) {
+ vsctl_fatal("multiple rows in %s match \"%s\"",
+ table->class->name, record_id);
+ }
}
if (!referrer) {
return NULL;
{
const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
if (!row) {
- ovs_fatal(0, "no row \"%s\" in table %s",
- record_id, table->class->name);
+ vsctl_fatal("no row \"%s\" in table %s",
+ record_id, table->class->name);
}
return row;
}
goto error;
}
error = get_column(table, column_name, columnp);
+ free(column_name);
if (error) {
goto error;
}
- free(column_name);
}
/* Parse key string. */
*valuep = NULL;
}
if (*p != '\0') {
- error = xasprintf("%s: trailing garbage in argument at offset %td",
- arg, p - arg);
+ error = xasprintf("%s: trailing garbage \"%s\" in argument",
+ arg, p);
goto error;
}
}
static void
cmd_get(struct vsctl_context *ctx)
{
+ bool if_exists = shash_find(&ctx->options, "--if-exists");
const char *table_name = ctx->argv[1];
const char *record_id = ctx->argv[2];
const struct vsctl_table_class *table;
unsigned int idx;
if (column->idl->type.value_type == OVSDB_TYPE_VOID) {
- ovs_fatal(0, "cannot specify key to get for non-map column %s",
- column->idl->name);
+ vsctl_fatal("cannot specify key to get for non-map column %s",
+ column->idl->name);
}
die_if_error(ovsdb_atom_from_string(&key,
idx = ovsdb_datum_find_key(&datum, &key,
column->idl->type.key_type);
if (idx == UINT_MAX) {
- ovs_fatal(0, "no key %s in %s record \"%s\" column %s",
- key_string, table_name, record_id,
- column->idl->name);
-
+ if (!if_exists) {
+ vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
+ key_string, table->class->name, record_id,
+ column->idl->name);
+ }
+ } else {
+ ovsdb_atom_to_string(&datum.values[idx],
+ column->idl->type.value_type, out);
}
- ovsdb_atom_to_string(&datum.values[idx],
- column->idl->type.value_type, out);
-
ovsdb_atom_destroy(&key, column->idl->type.key_type);
} else {
ovsdb_datum_to_string(&datum, &column->idl->type, out);
size_t length = regerror(retval, &re, NULL, 0);
char *buffer = xmalloc(length);
regerror(retval, &re, buffer, length);
- ovs_fatal(0, "internal error compiling regular expression %s: %s",
- regex, buffer);
+ vsctl_fatal("internal error compiling regular expression %s: %s",
+ regex, buffer);
}
for (i = 0; i < datum->n; i++) {
const char *key = datum->keys[i].string;
if (regexec(&re, key, 0, NULL, 0)) {
- ovs_fatal(0, "%s is not valid (it does not match %s)", key, regex);
+ vsctl_fatal("%s is not valid (it does not match %s)", key, regex);
}
}
free(regex);
int64_t value = datum->keys[i].integer;
if (value < min || value > max) {
if (max == INT64_MAX) {
- ovs_fatal(0, "%"PRId64" is less than the minimum "
- "allowed value %"PRId64, value, min);
+ vsctl_fatal("%"PRId64" is less than the minimum "
+ "allowed value %"PRId64, value, min);
} else if (min == INT64_MIN) {
- ovs_fatal(0, "%"PRId64" is greater than the maximum "
- "allowed value %"PRId64, value, max);
+ vsctl_fatal("%"PRId64" is greater than the maximum "
+ "allowed value %"PRId64, value, max);
} else {
- ovs_fatal(0, "%"PRId64" is outside the valid range %"PRId64" "
- "to %"PRId64" (inclusive)", value, min, max);
+ vsctl_fatal("%"PRId64" is outside the valid range %"PRId64" "
+ "to %"PRId64" (inclusive)", value, min, max);
}
}
}
&value_string);
die_if_error(error);
if (column->flags & VSCF_READONLY && !force) {
- ovs_fatal(0, "%s: cannot modify read-only column %s in table %s",
- arg, column->idl->name, table->class->name);
+ vsctl_fatal("%s: cannot modify read-only column %s in table %s",
+ arg, column->idl->name, table->class->name);
}
if (!value_string) {
- ovs_fatal(0, "%s: missing value", arg);
+ vsctl_fatal("%s: missing value", arg);
}
if (key_string) {
struct ovsdb_datum old, new;
if (column->idl->type.value_type == OVSDB_TYPE_VOID) {
- ovs_fatal(0, "cannot specify key to set for non-map column %s",
- column->idl->name);
+ vsctl_fatal("cannot specify key to set for non-map column %s",
+ column->idl->name);
}
die_if_error(ovsdb_atom_from_string(&key,
ovsdb_datum_init_empty(&new);
ovsdb_datum_add_unsafe(&new, &key, &value, &column->idl->type);
+ ovsdb_atom_destroy(&key, column->idl->type.key_type);
+ ovsdb_atom_destroy(&value, column->idl->type.value_type);
+
ovsdb_idl_txn_read(row, column->idl, &old);
ovsdb_datum_union(&old, &new, &column->idl->type, true);
ovsdb_idl_txn_write(row, column->idl, &old);
}
free(key_string);
+ free(value_string);
}
static void
table = get_table(table_name);
row = must_get_row(ctx, table, record_id);
die_if_error(get_column(table, column_name, &column));
+ if (column->flags & VSCF_READONLY && !force) {
+ vsctl_fatal("cannot modify read-only column %s in table %s",
+ column->idl->name, table->class->name);
+ }
+
type = &column->idl->type;
ovsdb_idl_txn_read(row, column->idl, &old);
for (i = 4; i < ctx->argc; i++) {
struct ovsdb_type add_type;
struct ovsdb_datum add;
- if (column->flags & VSCF_READONLY && !force) {
- ovs_fatal(0, "%s: cannot modify read-only column %s in table %s",
- ctx->argv[i], column->idl->name, table_name);
- }
-
add_type = *type;
add_type.n_min = 1;
add_type.n_max = UINT_MAX;
ovsdb_datum_destroy(&add, type);
}
if (old.n > type->n_max) {
- ovs_fatal(0, "\"add\" operation would put %u %s in column %s of "
- "table %s but at most %u are allowed",
- old.n,
- type->value_type == OVSDB_TYPE_VOID ? "values" : "pairs",
- column->idl->name, table_name, type->n_max);
+ vsctl_fatal("\"add\" operation would put %u %s in column %s of "
+ "table %s but the maximum number is %u",
+ old.n,
+ type->value_type == OVSDB_TYPE_VOID ? "values" : "pairs",
+ column->idl->name, table->class->name, type->n_max);
}
ovsdb_idl_txn_write(row, column->idl, &old);
}
table = get_table(table_name);
row = must_get_row(ctx, table, record_id);
die_if_error(get_column(table, column_name, &column));
+ if (column->flags & VSCF_READONLY && !force) {
+ vsctl_fatal("cannot modify read-only column %s in table %s",
+ column->idl->name, table->class->name);
+ }
+
type = &column->idl->type;
ovsdb_idl_txn_read(row, column->idl, &old);
for (i = 4; i < ctx->argc; i++) {
struct ovsdb_datum rm;
char *error;
- if (column->flags & VSCF_READONLY && !force) {
- ovs_fatal(0, "%s: cannot modify read-only column %s in table %s",
- ctx->argv[i], column->idl->name, table_name);
- }
-
rm_type = *type;
rm_type.n_min = 1;
rm_type.n_max = UINT_MAX;
ovsdb_datum_destroy(&rm, &rm_type);
}
if (old.n < type->n_min) {
- ovs_fatal(0, "\"remove\" operation would put %u %s in column %s of "
- "table %s but at least %u are required",
- old.n,
- type->value_type == OVSDB_TYPE_VOID ? "values" : "pairs",
- column->idl->name, table_name, type->n_min);
+ vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
+ "table %s but the minimun number is %u",
+ old.n,
+ type->value_type == OVSDB_TYPE_VOID ? "values" : "pairs",
+ column->idl->name, table->class->name, type->n_min);
}
ovsdb_idl_txn_write(row, column->idl, &old);
}
type = &column->idl->type;
if (column->flags & VSCF_READONLY && !force) {
- ovs_fatal(0, "%s: cannot modify read-only column %s in table %s",
- ctx->argv[i], column->idl->name, table_name);
+ vsctl_fatal("cannot modify read-only column %s in table %s",
+ column->idl->name, table->class->name);
} else if (type->n_min > 0) {
- ovs_fatal(0, "\"clear\" operation cannot be applied to column %s "
- "of table %s, which is not allowed to be empty",
- column->idl->name, table_name);
+ vsctl_fatal("\"clear\" operation cannot be applied to column %s "
+ "of table %s, which is not allowed to be empty",
+ column->idl->name, table->class->name);
}
ovsdb_datum_init_empty(&datum);
int i;
if (!force) {
- ovs_fatal(0, "\"create\" requires --force");
+ vsctl_fatal("\"create\" requires --force");
}
table = get_table(table_name);
int i;
if (!force) {
- ovs_fatal(0, "\"destroy\" requires --force");
+ vsctl_fatal("\"destroy\" requires --force");
}
table = get_table(table_name);
int64_t next_cfg = 0;
char *comment;
- txn = ovsdb_idl_txn_create(idl);
+ txn = the_idl_txn = ovsdb_idl_txn_create(idl);
if (dry_run) {
ovsdb_idl_txn_set_dry_run(txn);
}
}
}
ovsdb_idl_txn_destroy(txn);
+ the_idl_txn = NULL;
switch (status) {
case TXN_INCOMPLETE:
} else {
fputs(ds_cstr(ds), stdout);
}
+ ds_destroy(&c->output);
+ shash_destroy(&c->options);
}
+ free(commands);
if (wait_for_reload && status != TXN_UNCHANGED) {
for (;;) {
}
done: ;
}
+ ovsdb_idl_destroy(idl);
exit(EXIT_SUCCESS);
}
{"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
/* Parameter commands. */
- {"get", 3, INT_MAX, cmd_get, NULL, ""},
+ {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
{"list", 1, INT_MAX, cmd_list, NULL, ""},
{"set", 3, INT_MAX, cmd_set, NULL, "--force"},
{"add", 4, INT_MAX, cmd_add, NULL, "--force"},