2
0
mirror of https://github.com/openvswitch/ovs synced 2025-09-05 00:35:33 +00:00

db-ctl-base: Extend ctl_context with an error message.

Prepare for the command handlers (pre_cmd_*() cmd_*() functions) to
report errors by storing them in the context.

Signed-off-by: Jakub Sitnicki <jkbs@redhat.com>
Signed-off-by: Ben Pfaff <blp@ovn.org>
This commit is contained in:
Jakub Sitnicki
2018-07-02 12:50:10 +02:00
committed by Ben Pfaff
parent fd26f9a2bd
commit 675b152e99
6 changed files with 61 additions and 0 deletions

View File

@@ -2169,6 +2169,25 @@ ctl_might_write_to_db(const struct ctl_command *commands, size_t n)
return false;
}
/* Report an error while running in the command context. Caller should return
* to its caller immediately after reporting the error. */
void
ctl_error(struct ctl_context *ctx, const char *format, ...)
{
va_list args;
ovs_assert(ctx);
if (ctx->error) {
VLOG_ERR("Discarding unhandled error: %s", ctx->error);
free(ctx->error);
}
va_start(args, format);
ctx->error = xvasprintf(format, args);
va_end(args);
}
void
ctl_fatal(const char *format, ...)
{
@@ -2354,6 +2373,7 @@ ctl_context_init_command(struct ctl_context *ctx,
ds_swap(&ctx->output, &command->output);
ctx->table = command->table;
ctx->try_again = false;
ctx->error = NULL;
}
/* Initializes the entire 'ctx'. */
@@ -2379,6 +2399,8 @@ ctl_context_done_command(struct ctl_context *ctx,
{
ds_swap(&ctx->output, &command->output);
command->table = ctx->table;
free(ctx->error);
ctx->error = NULL;
}
/* Finishes up with 'ctx'.

View File

@@ -59,6 +59,8 @@ void ctl_init__(const struct ovsdb_idl_class *, const struct ctl_table_class *,
const struct cmd_show_table *cmd_show_tables,
void (*ctl_exit_func)(int status));
char *ctl_default_db(void);
void ctl_error(struct ctl_context *, const char *, ...)
OVS_PRINTF_FORMAT(2, 3);
OVS_NO_RETURN void ctl_fatal(const char *, ...) OVS_PRINTF_FORMAT(1, 2);
/* *ctl command syntax structure, to be defined by each command implementation.
@@ -222,6 +224,7 @@ struct ctl_context {
struct shash options;
/* Modifiable state. */
char *error;
struct ds output;
struct table *table;
struct ovsdb_idl *idl;

View File

@@ -3789,6 +3789,9 @@ run_prerequisites(struct ctl_command *commands, size_t n_commands,
ctl_context_init(&ctx, c, idl, NULL, NULL, NULL);
(c->syntax->prerequisites)(&ctx);
if (ctx.error) {
ctl_fatal("%s", ctx.error);
}
ctl_context_done(&ctx, c);
ovs_assert(!c->output.string);
@@ -3839,6 +3842,9 @@ do_nbctl(const char *args, struct ctl_command *commands, size_t n_commands,
if (c->syntax->run) {
(c->syntax->run)(&ctx);
}
if (ctx.error) {
ctl_fatal("%s", error);
}
ctl_context_done_command(&ctx, c);
if (ctx.try_again) {
@@ -3877,6 +3883,9 @@ do_nbctl(const char *args, struct ctl_command *commands, size_t n_commands,
if (c->syntax->postprocess) {
ctl_context_init(&ctx, c, idl, txn, symtab, NULL);
(c->syntax->postprocess)(&ctx);
if (ctx.error) {
ctl_fatal("%s", ctx.error);
}
ctl_context_done(&ctx, c);
}
}

View File

@@ -1239,6 +1239,9 @@ run_prerequisites(struct ctl_command *commands, size_t n_commands,
sbctl_context_init(&sbctl_ctx, c, idl, NULL, NULL);
(c->syntax->prerequisites)(&sbctl_ctx.base);
if (sbctl_ctx.base.error) {
ctl_fatal("%s", sbctl_ctx.base.error);
}
sbctl_context_done(&sbctl_ctx, c);
ovs_assert(!c->output.string);
@@ -1283,6 +1286,9 @@ do_sbctl(const char *args, struct ctl_command *commands, size_t n_commands,
if (c->syntax->run) {
(c->syntax->run)(&sbctl_ctx.base);
}
if (sbctl_ctx.base.error) {
ctl_fatal("%s", sbctl_ctx.base.error);
}
sbctl_context_done_command(&sbctl_ctx, c);
if (sbctl_ctx.base.try_again) {
@@ -1318,6 +1324,9 @@ do_sbctl(const char *args, struct ctl_command *commands, size_t n_commands,
if (c->syntax->postprocess) {
sbctl_context_init(&sbctl_ctx, c, idl, txn, symtab);
(c->syntax->postprocess)(&sbctl_ctx.base);
if (sbctl_ctx.base.error) {
ctl_fatal("%s", sbctl_ctx.base.error);
}
sbctl_context_done(&sbctl_ctx, c);
}
}

View File

@@ -2528,6 +2528,9 @@ run_prerequisites(struct ctl_command *commands, size_t n_commands,
vsctl_context_init(&vsctl_ctx, c, idl, NULL, NULL, NULL);
(c->syntax->prerequisites)(&vsctl_ctx.base);
if (vsctl_ctx.base.error) {
ctl_fatal("%s", vsctl_ctx.base.error);
}
vsctl_context_done(&vsctl_ctx, c);
ovs_assert(!c->output.string);
@@ -2623,6 +2626,9 @@ do_vsctl(const char *args, struct ctl_command *commands, size_t n_commands,
if (c->syntax->run) {
(c->syntax->run)(&vsctl_ctx.base);
}
if (vsctl_ctx.base.error) {
ctl_fatal("%s", vsctl_ctx.base.error);
}
vsctl_context_done_command(&vsctl_ctx, c);
if (vsctl_ctx.base.try_again) {
@@ -2661,6 +2667,9 @@ do_vsctl(const char *args, struct ctl_command *commands, size_t n_commands,
if (c->syntax->postprocess) {
vsctl_context_init(&vsctl_ctx, c, idl, txn, ovs, symtab);
(c->syntax->postprocess)(&vsctl_ctx.base);
if (vsctl_ctx.base.error) {
ctl_fatal("%s", vsctl_ctx.base.error);
}
vsctl_context_done(&vsctl_ctx, c);
}
}

View File

@@ -2261,6 +2261,9 @@ run_prerequisites(struct ctl_command *commands, size_t n_commands,
vtep_ctl_context_init(&vtepctl_ctx, c, idl, NULL, NULL, NULL);
(c->syntax->prerequisites)(&vtepctl_ctx.base);
if (vtepctl_ctx.base.error) {
ctl_fatal("%s", vtepctl_ctx.base.error);
}
vtep_ctl_context_done(&vtepctl_ctx, c);
ovs_assert(!c->output.string);
@@ -2306,6 +2309,9 @@ do_vtep_ctl(const char *args, struct ctl_command *commands,
if (c->syntax->run) {
(c->syntax->run)(&vtepctl_ctx.base);
}
if (vtepctl_ctx.base.error) {
ctl_fatal("%s", vtepctl_ctx.base.error);
}
vtep_ctl_context_done_command(&vtepctl_ctx, c);
if (vtepctl_ctx.base.try_again) {
@@ -2341,6 +2347,9 @@ do_vtep_ctl(const char *args, struct ctl_command *commands,
if (c->syntax->postprocess) {
vtep_ctl_context_init(&vtepctl_ctx, c, idl, txn, vtep_global, symtab);
(c->syntax->postprocess)(&vtepctl_ctx.base);
if (vtepctl_ctx.base.error) {
ctl_fatal("%s", vtepctl_ctx.base.error);
}
vtep_ctl_context_done(&vtepctl_ctx, c);
}
}